[Mlir-commits] [mlir] ce21721 - [MLIR] Fix tblgen properties printing to filter them out of discardable attrs dict (#79243)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jan 24 13:53:57 PST 2024


Author: arthurqiu
Date: 2024-01-24T13:53:52-08:00
New Revision: ce21721d1dd0f11c3ff38d16050e65861908a395

URL: https://github.com/llvm/llvm-project/commit/ce21721d1dd0f11c3ff38d16050e65861908a395
DIFF: https://github.com/llvm/llvm-project/commit/ce21721d1dd0f11c3ff38d16050e65861908a395.diff

LOG: [MLIR] Fix tblgen properties printing to filter them out of discardable attrs dict (#79243)

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.

Co-authored-by: Arthurq Qiu <arthurq at nvidia.com>

Added: 
    

Modified: 
    mlir/test/IR/properties.mlir
    mlir/tools/mlir-tblgen/OpFormatGen.cpp

Removed: 
    


################################################################################
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..31ceb05ad1dbfd7 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" : "")
+         << "(llvm::to_vector((*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();
 }
 


        


More information about the Mlir-commits mailing list