[PATCH] D80113: [mlir] Support optional attributes in assembly formats

Tobias Gysi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 18 02:38:18 PDT 2020


gysit created this revision.
gysit added reviewers: ftynse, Kayjukh.
Herald added subscribers: llvm-commits, jurahul, frgossen, grosul1, Joonsoo, stephenneuendorffer, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

This revision adds support for assembly formats with optional attributes. It elides optional attributes that are part of the syntax from the attribute dictionary.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80113

Files:
  mlir/test/lib/Dialect/Test/TestOps.td
  mlir/test/mlir-tblgen/op-format.mlir
  mlir/tools/mlir-tblgen/OpFormatGen.cpp


Index: mlir/tools/mlir-tblgen/OpFormatGen.cpp
===================================================================
--- mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -886,9 +886,15 @@
                                OpMethodBody &body, bool withKeyword) {
   // Collect all of the attributes used in the format, these will be elided.
   SmallVector<const NamedAttribute *, 1> usedAttributes;
-  for (auto &it : fmt.elements)
+  for (auto &it : fmt.elements) {
     if (auto *attr = dyn_cast<AttributeVariable>(it.get()))
       usedAttributes.push_back(attr->getVar());
+    // Collect the optional attributes.
+    if (auto *opt = dyn_cast<OptionalElement>(it.get()))
+      for (auto &elem : opt->getElements())
+        if (auto *attr = dyn_cast<AttributeVariable>(&elem))
+          usedAttributes.push_back(attr->getVar());
+  }
 
   body << "  p.printOptionalAttrDict" << (withKeyword ? "WithKeyword" : "")
        << "(getAttrs(), /*elidedAttrs=*/{";
Index: mlir/test/mlir-tblgen/op-format.mlir
===================================================================
--- mlir/test/mlir-tblgen/op-format.mlir
+++ mlir/test/mlir-tblgen/op-format.mlir
@@ -12,9 +12,16 @@
 // CHECK-NOT: {attr
 test.format_attr_op 10
 
+// CHECK: test.format_opt_attr_op(10)
+// CHECK-NOT: {opt_attr
+test.format_opt_attr_op(10)
+
 // CHECK: test.format_attr_dict_w_keyword attributes {attr = 10 : i64}
 test.format_attr_dict_w_keyword attributes {attr = 10 : i64}
 
+// CHECK: test.format_attr_dict_w_keyword attributes {attr = 10 : i64, opt_attr = 10 : i64}
+test.format_attr_dict_w_keyword attributes {attr = 10 : i64, opt_attr = 10 : i64}
+
 // CHECK: test.format_buildable_type_op %[[I64]]
 %ignored = test.format_buildable_type_op %i64
 
Index: mlir/test/lib/Dialect/Test/TestOps.td
===================================================================
--- mlir/test/lib/Dialect/Test/TestOps.td
+++ mlir/test/lib/Dialect/Test/TestOps.td
@@ -1214,9 +1214,15 @@
   let assemblyFormat = "$attr attr-dict";
 }
 
+// Test that we elide optional attributes that are within the syntax.
+def FormatOptAttrOp : TEST_Op<"format_opt_attr_op"> {
+  let arguments = (ins OptionalAttr<I64Attr>:$opt_attr);
+  let assemblyFormat = "(`(`$opt_attr^`)`)? attr-dict";
+}
+
 // Test that we elide attributes that are within the syntax.
 def FormatAttrDictWithKeywordOp : TEST_Op<"format_attr_dict_w_keyword"> {
-  let arguments = (ins I64Attr:$attr);
+  let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$opt_attr);
   let assemblyFormat = "attr-dict-with-keyword";
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80113.264565.patch
Type: text/x-patch
Size: 2582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200518/cc4cd2d1/attachment.bin>


More information about the llvm-commits mailing list