Skip to content

Fix tblgen properties printing #79243

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
Jan 24, 2024
Merged

Fix tblgen properties printing #79243

merged 1 commit into from
Jan 24, 2024

Conversation

arthurqiu
Copy link
Contributor

This is to fix the bug reported in https://discourse.llvm.org/t/whats-the-recommended-way-to-use-prop-dict/75921

When prop-dict is used in the assembly format, attr-dict should print discardable attributes only.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jan 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 24, 2024

@llvm/pr-subscribers-mlir

Author: None (arthurqiu)

Changes

This is to fix the bug reported in https://discourse.llvm.org/t/whats-the-recommended-way-to-use-prop-dict/75921

When prop-dict is used in the assembly format, attr-dict should print discardable attributes only.


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

2 Files Affected:

  • (modified) mlir/test/IR/properties.mlir (+5-5)
  • (modified) mlir/tools/mlir-tblgen/OpFormatGen.cpp (+14-3)
diff --git a/mlir/test/IR/properties.mlir b/mlir/test/IR/properties.mlir
index 2aef2abc81f7908..3c4bd57859ef917 100644
--- a/mlir/test/IR/properties.mlir
+++ b/mlir/test/IR/properties.mlir
@@ -2,31 +2,31 @@
 // # RUN: mlir-opt %s -mlir-print-op-generic -split-input-file  | mlir-opt -mlir-print-op-generic | FileCheck %s --check-prefix=GENERIC
 
 // CHECK:   test.with_properties
-// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
+// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>{{$}}
 // GENERIC:   "test.with_properties"()
 // GENERIC-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}> : () -> ()
 test.with_properties <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
 
 // CHECK:   test.with_nice_properties
-// CHECK-SAME:    "foo bar" is -3
+// CHECK-SAME:    "foo bar" is -3{{$}}
 // GENERIC: "test.with_nice_properties"()
 // GENERIC-SAME:  <{prop = {label = "foo bar", value = -3 : i32}}> : () -> ()
 test.with_nice_properties "foo bar" is -3
 
 // CHECK:   test.with_wrapped_properties
-// CHECK-SAME:    "content for properties"
+// CHECK-SAME:    <{prop = "content for properties"}>{{$}}
 // GENERIC: "test.with_wrapped_properties"()
 // GENERIC-SAME:  <{prop = "content for properties"}> : () -> ()
 test.with_wrapped_properties <{prop = "content for properties"}>
 
 // CHECK: test.using_property_in_custom
-// CHECK-SAME: [1, 4, 20]
+// CHECK-SAME: [1, 4, 20]{{$}}
 // GENERIC: "test.using_property_in_custom"()
 // GENERIC-SAME: prop = array<i64: 1, 4, 20>
 test.using_property_in_custom [1, 4, 20]
 
 // CHECK: test.using_property_ref_in_custom
-// CHECK-SAME: 1 + 4 = 5
+// CHECK-SAME: 1 + 4 = 5{{$}}
 // GENERIC: "test.using_property_ref_in_custom"()
 // GENERIC-SAME: <{
 // GENERIC-SAME: first = 1
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index b6405baef28e84a..34921d1d538cb0f 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -366,6 +366,9 @@ struct OperationFormat {
   /// Indicate whether attribute are stored in properties.
   bool useProperties;
 
+  /// Indicate whether prop-dict is used in the format
+  bool hasPropDict;
+
   /// The Operation class name
   StringRef opCppClassName;
 
@@ -1810,9 +1813,14 @@ static void genAttrDictPrinter(OperationFormat &fmt, Operator &op,
       body << "  }\n";
     }
   }
-  body << "  _odsPrinter.printOptionalAttrDict"
-       << (withKeyword ? "WithKeyword" : "")
-       << "((*this)->getAttrs(), elidedAttrs);\n";
+  if (fmt.hasPropDict)
+    body << "  _odsPrinter.printOptionalAttrDict"
+         << (withKeyword ? "WithKeyword" : "")
+         << "((*this)->getDiscardableAttrs(), elidedAttrs);\n";
+  else
+    body << "  _odsPrinter.printOptionalAttrDict"
+         << (withKeyword ? "WithKeyword" : "")
+         << "((*this)->getAttrs(), elidedAttrs);\n";
 }
 
 /// Generate the printer for a literal value. `shouldEmitSpace` is true if a
@@ -2560,6 +2568,9 @@ LogicalResult OpFormatParser::verify(SMLoc loc,
 
   // Collect the set of used attributes in the format.
   fmt.usedAttributes = seenAttrs.takeVector();
+
+  // Set whether prop-dict is used in the format
+  fmt.hasPropDict = hasPropDict;
   return success();
 }
 

@llvmbot
Copy link
Member

llvmbot commented Jan 24, 2024

@llvm/pr-subscribers-mlir-core

Author: None (arthurqiu)

Changes

This is to fix the bug reported in https://discourse.llvm.org/t/whats-the-recommended-way-to-use-prop-dict/75921

When prop-dict is used in the assembly format, attr-dict should print discardable attributes only.


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

2 Files Affected:

  • (modified) mlir/test/IR/properties.mlir (+5-5)
  • (modified) mlir/tools/mlir-tblgen/OpFormatGen.cpp (+14-3)
diff --git a/mlir/test/IR/properties.mlir b/mlir/test/IR/properties.mlir
index 2aef2abc81f7908..3c4bd57859ef917 100644
--- a/mlir/test/IR/properties.mlir
+++ b/mlir/test/IR/properties.mlir
@@ -2,31 +2,31 @@
 // # RUN: mlir-opt %s -mlir-print-op-generic -split-input-file  | mlir-opt -mlir-print-op-generic | FileCheck %s --check-prefix=GENERIC
 
 // CHECK:   test.with_properties
-// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
+// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>{{$}}
 // GENERIC:   "test.with_properties"()
 // GENERIC-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}> : () -> ()
 test.with_properties <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
 
 // CHECK:   test.with_nice_properties
-// CHECK-SAME:    "foo bar" is -3
+// CHECK-SAME:    "foo bar" is -3{{$}}
 // GENERIC: "test.with_nice_properties"()
 // GENERIC-SAME:  <{prop = {label = "foo bar", value = -3 : i32}}> : () -> ()
 test.with_nice_properties "foo bar" is -3
 
 // CHECK:   test.with_wrapped_properties
-// CHECK-SAME:    "content for properties"
+// CHECK-SAME:    <{prop = "content for properties"}>{{$}}
 // GENERIC: "test.with_wrapped_properties"()
 // GENERIC-SAME:  <{prop = "content for properties"}> : () -> ()
 test.with_wrapped_properties <{prop = "content for properties"}>
 
 // CHECK: test.using_property_in_custom
-// CHECK-SAME: [1, 4, 20]
+// CHECK-SAME: [1, 4, 20]{{$}}
 // GENERIC: "test.using_property_in_custom"()
 // GENERIC-SAME: prop = array<i64: 1, 4, 20>
 test.using_property_in_custom [1, 4, 20]
 
 // CHECK: test.using_property_ref_in_custom
-// CHECK-SAME: 1 + 4 = 5
+// CHECK-SAME: 1 + 4 = 5{{$}}
 // GENERIC: "test.using_property_ref_in_custom"()
 // GENERIC-SAME: <{
 // GENERIC-SAME: first = 1
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index b6405baef28e84a..34921d1d538cb0f 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -366,6 +366,9 @@ struct OperationFormat {
   /// Indicate whether attribute are stored in properties.
   bool useProperties;
 
+  /// Indicate whether prop-dict is used in the format
+  bool hasPropDict;
+
   /// The Operation class name
   StringRef opCppClassName;
 
@@ -1810,9 +1813,14 @@ static void genAttrDictPrinter(OperationFormat &fmt, Operator &op,
       body << "  }\n";
     }
   }
-  body << "  _odsPrinter.printOptionalAttrDict"
-       << (withKeyword ? "WithKeyword" : "")
-       << "((*this)->getAttrs(), elidedAttrs);\n";
+  if (fmt.hasPropDict)
+    body << "  _odsPrinter.printOptionalAttrDict"
+         << (withKeyword ? "WithKeyword" : "")
+         << "((*this)->getDiscardableAttrs(), elidedAttrs);\n";
+  else
+    body << "  _odsPrinter.printOptionalAttrDict"
+         << (withKeyword ? "WithKeyword" : "")
+         << "((*this)->getAttrs(), elidedAttrs);\n";
 }
 
 /// Generate the printer for a literal value. `shouldEmitSpace` is true if a
@@ -2560,6 +2568,9 @@ LogicalResult OpFormatParser::verify(SMLoc loc,
 
   // Collect the set of used attributes in the format.
   fmt.usedAttributes = seenAttrs.takeVector();
+
+  // Set whether prop-dict is used in the format
+  fmt.hasPropDict = hasPropDict;
   return success();
 }
 

@joker-eph joker-eph merged commit ce21721 into llvm:main Jan 24, 2024
@jpienaar
Copy link
Member

Thanks!

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.

4 participants