Skip to content

Commit 34d4f66

Browse files
authored
[mlir] Fix the emission of prop-dict when operations have no properties (#112851)
When an operation has no properties, no property struct is emitted. To avoid a compilation error, we should also skip emitting `setPropertiesFromParsedAttr`, `parseProperties` and `printProperties` in such cases. Compilation error: ``` error: ‘Properties’ has not been declared static ::llvm::LogicalResult setPropertiesFromParsedAttr(Properties &prop, ::mlir::Attribute attr, ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError); ```
1 parent b6e9ba0 commit 34d4f66

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

mlir/test/IR/properties.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ test.with_nice_properties "foo bar" is -3
1919
// GENERIC-SAME: <{prop = "content for properties"}> : () -> ()
2020
test.with_wrapped_properties <{prop = "content for properties"}>
2121

22+
// CHECK: test.empty_properties
23+
// GENERIC: "test.empty_properties"()
24+
test.empty_properties
25+
2226
// CHECK: test.using_property_in_custom
2327
// CHECK-SAME: [1, 4, 20]{{$}}
2428
// GENERIC: "test.using_property_in_custom"()

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,11 @@ def TestOpWithWrappedProperties : TEST_Op<"with_wrapped_properties"> {
30063006
);
30073007
}
30083008

3009+
def TestOpWithEmptyProperties : TEST_Op<"empty_properties"> {
3010+
let assemblyFormat = "prop-dict attr-dict";
3011+
let arguments = (ins);
3012+
}
3013+
30093014
def TestOpUsingPropertyInCustom : TEST_Op<"using_property_in_custom"> {
30103015
let assemblyFormat = "custom<UsingPropertyInCustom>($prop) attr-dict";
30113016
let arguments = (ins IntArrayProperty<"int64_t">:$prop);

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ OpEmitter::OpEmitter(const Operator &op,
11061106
genFolderDecls();
11071107
genTypeInterfaceMethods();
11081108
genOpInterfaceMethods();
1109-
generateOpFormat(op, opClass);
1109+
generateOpFormat(op, opClass, emitHelper.hasProperties());
11101110
genSideEffectInterfaceMethods();
11111111
}
11121112
void OpEmitter::emitDecl(

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,8 @@ struct OperationFormat {
339339
Optional
340340
};
341341

342-
OperationFormat(const Operator &op)
343-
: useProperties(op.getDialect().usePropertiesForAttributes() &&
344-
!op.getAttributes().empty()),
345-
opCppClassName(op.getCppClassName()) {
342+
OperationFormat(const Operator &op, bool hasProperties)
343+
: useProperties(hasProperties), opCppClassName(op.getCppClassName()) {
346344
operandTypes.resize(op.getNumOperands(), TypeResolution());
347345
resultTypes.resize(op.getNumResults(), TypeResolution());
348346

@@ -397,7 +395,7 @@ struct OperationFormat {
397395
/// A flag indicating if this operation has the SingleBlock trait.
398396
bool hasSingleBlockTrait;
399397

400-
/// Indicate whether attribute are stored in properties.
398+
/// Indicate whether we need to use properties for the current operator.
401399
bool useProperties;
402400

403401
/// Indicate whether prop-dict is used in the format
@@ -1275,8 +1273,8 @@ static void genAttrParser(AttributeVariable *attr, MethodBody &body,
12751273
// 'prop-dict' dictionary attr.
12761274
static void genParsedAttrPropertiesSetter(OperationFormat &fmt, Operator &op,
12771275
OpClass &opClass) {
1278-
// Not required unless 'prop-dict' is present.
1279-
if (!fmt.hasPropDict)
1276+
// Not required unless 'prop-dict' is present or we are not using properties.
1277+
if (!fmt.hasPropDict || !fmt.useProperties)
12801278
return;
12811279

12821280
SmallVector<MethodParameter> paramList;
@@ -1621,8 +1619,10 @@ void OperationFormat::genElementParser(FormatElement *element, MethodBody &body,
16211619
body.unindent() << "}\n";
16221620
body.unindent();
16231621
} else if (isa<PropDictDirective>(element)) {
1624-
body << " if (parseProperties(parser, result))\n"
1625-
<< " return ::mlir::failure();\n";
1622+
if (useProperties) {
1623+
body << " if (parseProperties(parser, result))\n"
1624+
<< " return ::mlir::failure();\n";
1625+
}
16261626
} else if (auto *customDir = dyn_cast<CustomDirective>(element)) {
16271627
genCustomDirectiveParser(customDir, body, useProperties, opCppClassName);
16281628
} else if (isa<OperandsDirective>(element)) {
@@ -2047,9 +2047,11 @@ static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
20472047
}
20482048
}
20492049

2050-
body << " _odsPrinter << \" \";\n"
2051-
<< " printProperties(this->getContext(), _odsPrinter, "
2052-
"getProperties(), elidedProps);\n";
2050+
if (fmt.useProperties) {
2051+
body << " _odsPrinter << \" \";\n"
2052+
<< " printProperties(this->getContext(), _odsPrinter, "
2053+
"getProperties(), elidedProps);\n";
2054+
}
20532055
}
20542056

20552057
/// Generate the printer for the 'attr-dict' directive.
@@ -3771,7 +3773,8 @@ LogicalResult OpFormatParser::verifyOptionalGroupElement(SMLoc loc,
37713773
// Interface
37723774
//===----------------------------------------------------------------------===//
37733775

3774-
void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass) {
3776+
void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass,
3777+
bool hasProperties) {
37753778
// TODO: Operator doesn't expose all necessary functionality via
37763779
// the const interface.
37773780
Operator &op = const_cast<Operator &>(constOp);
@@ -3782,7 +3785,7 @@ void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass) {
37823785
llvm::SourceMgr mgr;
37833786
mgr.AddNewSourceBuffer(
37843787
llvm::MemoryBuffer::getMemBuffer(op.getAssemblyFormat()), SMLoc());
3785-
OperationFormat format(op);
3788+
OperationFormat format(op, hasProperties);
37863789
OpFormatParser parser(mgr, format, op);
37873790
FailureOr<std::vector<FormatElement *>> elements = parser.parse();
37883791
if (failed(elements)) {

mlir/tools/mlir-tblgen/OpFormatGen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class OpClass;
2020
class Operator;
2121

2222
// Generate the assembly format for the given operator.
23-
void generateOpFormat(const Operator &constOp, OpClass &opClass);
23+
void generateOpFormat(const Operator &constOp, OpClass &opClass,
24+
bool hasProperties);
2425

2526
} // namespace tblgen
2627
} // namespace mlir

0 commit comments

Comments
 (0)