Summary: This revision adds support for assembly formats with optional attributes. It elides optional attributes that are part of the syntax from the attribute dictionary.
Reviewers: ftynse, Kayjukh
Reviewed By: ftynse, Kayjukh
Subscribers: mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, stephenneuendorffer, Joonsoo, grosul1, frgossen, jurahul, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80113
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";
}
// 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
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=*/{";