Skip to content

Commit 182119c

Browse files
committed
[mlir] Fix TableGen emission for ops without properties
The current condition to emit `setPropertiesFromParsedAttr` is not strict enough since we emit the method even when we do not have properties at all. Similarly we should not emit `parseProperties` and `printProperties` if we don't have properties.
1 parent 9d0616c commit 182119c

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-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: 18 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,9 @@ 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
1277+
// are not using properties.
1278+
if (!fmt.hasPropDict || !fmt.useProperties)
12801279
return;
12811280

12821281
SmallVector<MethodParameter> paramList;
@@ -1621,8 +1620,10 @@ void OperationFormat::genElementParser(FormatElement *element, MethodBody &body,
16211620
body.unindent() << "}\n";
16221621
body.unindent();
16231622
} else if (isa<PropDictDirective>(element)) {
1624-
body << " if (parseProperties(parser, result))\n"
1625-
<< " return ::mlir::failure();\n";
1623+
if (useProperties) {
1624+
body << " if (parseProperties(parser, result))\n"
1625+
<< " return ::mlir::failure();\n";
1626+
}
16261627
} else if (auto *customDir = dyn_cast<CustomDirective>(element)) {
16271628
genCustomDirectiveParser(customDir, body, useProperties, opCppClassName);
16281629
} else if (isa<OperandsDirective>(element)) {
@@ -2047,9 +2048,11 @@ static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
20472048
}
20482049
}
20492050

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

20552058
/// Generate the printer for the 'attr-dict' directive.
@@ -3771,7 +3774,8 @@ LogicalResult OpFormatParser::verifyOptionalGroupElement(SMLoc loc,
37713774
// Interface
37723775
//===----------------------------------------------------------------------===//
37733776

3774-
void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass) {
3777+
void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass,
3778+
bool hasProperties) {
37753779
// TODO: Operator doesn't expose all necessary functionality via
37763780
// the const interface.
37773781
Operator &op = const_cast<Operator &>(constOp);
@@ -3782,7 +3786,7 @@ void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass) {
37823786
llvm::SourceMgr mgr;
37833787
mgr.AddNewSourceBuffer(
37843788
llvm::MemoryBuffer::getMemBuffer(op.getAssemblyFormat()), SMLoc());
3785-
OperationFormat format(op);
3789+
OperationFormat format(op, hasProperties);
37863790
OpFormatParser parser(mgr, format, op);
37873791
FailureOr<std::vector<FormatElement *>> elements = parser.parse();
37883792
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)