Skip to content

[mlir] Fix the emission of prop-dict when operations have no properties #112851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

chelini
Copy link
Contributor

@chelini chelini commented Oct 18, 2024

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);
    

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Oct 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: lorenzo chelini (chelini)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/112851.diff

3 Files Affected:

  • (modified) mlir/test/IR/properties.mlir (+4)
  • (modified) mlir/test/lib/Dialect/Test/TestOps.td (+5)
  • (modified) mlir/tools/mlir-tblgen/OpFormatGen.cpp (+12-7)
diff --git a/mlir/test/IR/properties.mlir b/mlir/test/IR/properties.mlir
index 418b81dcbb034f..9a1c49cb7dabf3 100644
--- a/mlir/test/IR/properties.mlir
+++ b/mlir/test/IR/properties.mlir
@@ -19,6 +19,10 @@ test.with_nice_properties "foo bar" is -3
 // GENERIC-SAME:  <{prop = "content for properties"}> : () -> ()
 test.with_wrapped_properties <{prop = "content for properties"}>
 
+// CHECK: test.empty_properties
+// GENERIC: "test.empty_properties"()
+test.empty_properties
+
 // CHECK: test.using_property_in_custom
 // CHECK-SAME: [1, 4, 20]{{$}}
 // GENERIC: "test.using_property_in_custom"()
diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 9e19966414d1d7..bc6c6cf213ea4b 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -3006,6 +3006,11 @@ def TestOpWithWrappedProperties : TEST_Op<"with_wrapped_properties"> {
   );
 }
 
+def TestOpWithEmptyProperties : TEST_Op<"empty_properties"> {
+  let assemblyFormat = "prop-dict attr-dict";
+  let arguments = (ins);
+}
+
 def TestOpUsingPropertyInCustom : TEST_Op<"using_property_in_custom"> {
   let assemblyFormat = "custom<UsingPropertyInCustom>($prop) attr-dict";
   let arguments = (ins IntArrayProperty<"int64_t">:$prop);
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index c99c71572bec23..4703de67025ded 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -1275,8 +1275,9 @@ static void genAttrParser(AttributeVariable *attr, MethodBody &body,
 // 'prop-dict' dictionary attr.
 static void genParsedAttrPropertiesSetter(OperationFormat &fmt, Operator &op,
                                           OpClass &opClass) {
-  // Not required unless 'prop-dict' is present.
-  if (!fmt.hasPropDict)
+  // Not required unless 'prop-dict' is present or we
+  // are not using properties.
+  if (!fmt.hasPropDict || !fmt.useProperties)
     return;
 
   SmallVector<MethodParameter> paramList;
@@ -1621,8 +1622,10 @@ void OperationFormat::genElementParser(FormatElement *element, MethodBody &body,
     body.unindent() << "}\n";
     body.unindent();
   } else if (isa<PropDictDirective>(element)) {
-    body << "  if (parseProperties(parser, result))\n"
-         << "    return ::mlir::failure();\n";
+    if (useProperties) {
+      body << "  if (parseProperties(parser, result))\n"
+           << "    return ::mlir::failure();\n";
+    }
   } else if (auto *customDir = dyn_cast<CustomDirective>(element)) {
     genCustomDirectiveParser(customDir, body, useProperties, opCppClassName);
   } else if (isa<OperandsDirective>(element)) {
@@ -2047,9 +2050,11 @@ static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
     }
   }
 
-  body << "  _odsPrinter << \" \";\n"
-       << "  printProperties(this->getContext(), _odsPrinter, "
-          "getProperties(), elidedProps);\n";
+  if (fmt.useProperties) {
+    body << "  _odsPrinter << \" \";\n"
+         << "  printProperties(this->getContext(), _odsPrinter, "
+            "getProperties(), elidedProps);\n";
+  }
 }
 
 /// Generate the printer for the 'attr-dict' directive.

@chelini chelini marked this pull request as draft October 18, 2024 08:26
@chelini chelini force-pushed the lchelini/mlir/properties-tbgen branch from 7e5fbe8 to 182119c Compare October 18, 2024 09:24
@zero9178
Copy link
Member

Curious about the motivation here: Is there a problem with always emitting these methods that this PR solves?

@chelini
Copy link
Contributor Author

chelini commented Oct 18, 2024

Curious about the motivation here: Is there a problem with always emitting these methods that this PR solves?

yes, if we did not emit any property struct for the current operation we should not inject these method, otherwise you end up with a compilation error.

error: ‘Properties’ has not been declared
46380 |   static ::llvm::LogicalResult setPropertiesFromParsedAttr(Properties &prop, ::mlir::Attribute attr, ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError);

@chelini chelini marked this pull request as ready for review October 18, 2024 12:17
@chelini chelini requested a review from River707 October 18, 2024 16:04
Copy link
Contributor

@River707 River707 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a second I wasn't sure what was broken from the description, but it seems like this PR fixes emission of prop-dict when the operation doesn't have properties?

@joker-eph
Copy link
Collaborator

Thanks for the fix!

Can you please edit the description to make it more clear what is the problem fixed here?

@chelini
Copy link
Contributor Author

chelini commented Oct 21, 2024

For a second I wasn't sure what was broken from the description, but it seems like this PR fixes emission of prop-dict when the operation doesn't have properties?

yes, correct. Let me improve the commit msg.

…roperties

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);

```
@chelini chelini force-pushed the lchelini/mlir/properties-tbgen branch from 182119c to d5acba5 Compare October 21, 2024 15:22
@chelini chelini changed the title [mlir] Fix TableGen emission for ops without properties [mlir] Fix emission of prop-dict when the operation does not have properties Oct 21, 2024
@chelini
Copy link
Contributor Author

chelini commented Oct 21, 2024

Thanks for the fix!

Can you please edit the description to make it more clear what is the problem fixed here?

Thanks, updated. Hopefully it is more clear now!

@chelini chelini changed the title [mlir] Fix emission of prop-dict when the operation does not have properties [mlir] Fix emission of prop-dict when operations do not have properties Oct 21, 2024
@chelini chelini changed the title [mlir] Fix emission of prop-dict when operations do not have properties [mlir] Fix the emission of prop-dict when operations have no properties Oct 21, 2024
@chelini chelini merged commit 34d4f66 into llvm:main Oct 21, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants