[Mlir-commits] [mlir] 8bb8421 - [mlir] Add an overload of parseOptionalAttribute for SymbolRefAttr

River Riddle llvmlistbot at llvm.org
Sat Dec 17 20:08:30 PST 2022


Author: River Riddle
Date: 2022-12-17T19:54:44-08:00
New Revision: 8bb8421b0d5e2c032131b077c700b15dd1b3d739

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

LOG: [mlir] Add an overload of parseOptionalAttribute for SymbolRefAttr

Avoids parsing other types of attributes when checking if there is
a SymbolRefAttr.

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpImplementation.h
    mlir/lib/AsmParser/AsmParserImpl.h
    mlir/lib/AsmParser/AttributeParser.cpp
    mlir/lib/AsmParser/Parser.h
    mlir/test/lib/Dialect/Test/TestOps.td
    mlir/test/mlir-tblgen/op-format.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index 913d65e68d04f..8a917b45c6c30 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -973,6 +973,10 @@ class AsmParser {
   virtual OptionalParseResult parseOptionalAttribute(StringAttr &result,
                                                      Type type = {}) = 0;
 
+  /// Parse an optional symbol ref attribute and return it in result.
+  virtual OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result,
+                                                     Type type = {}) = 0;
+
   /// Parse an optional attribute of a specific type and add it to the list with
   /// the specified name.
   template <typename AttrType>

diff  --git a/mlir/lib/AsmParser/AsmParserImpl.h b/mlir/lib/AsmParser/AsmParserImpl.h
index cf9b774b4d6e0..a457bf768f881 100644
--- a/mlir/lib/AsmParser/AsmParserImpl.h
+++ b/mlir/lib/AsmParser/AsmParserImpl.h
@@ -430,6 +430,10 @@ class AsmParserImpl : public BaseT {
                                              Type type) override {
     return parser.parseOptionalAttribute(result, type);
   }
+  OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result,
+                                             Type type) override {
+    return parser.parseOptionalAttribute(result, type);
+  }
 
   /// Parse a named dictionary into 'result' if it is present.
   ParseResult parseOptionalAttrDict(NamedAttrList &result) override {

diff  --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index c8e2c09ec0ba2..835341c71b60f 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -280,6 +280,10 @@ OptionalParseResult Parser::parseOptionalAttribute(StringAttr &attribute,
                                                    Type type) {
   return parseOptionalAttributeWithToken(Token::string, attribute, type);
 }
+OptionalParseResult Parser::parseOptionalAttribute(SymbolRefAttr &result,
+                                                   Type type) {
+  return parseOptionalAttributeWithToken(Token::at_identifier, result, type);
+}
 
 /// Attribute dictionary.
 ///

diff  --git a/mlir/lib/AsmParser/Parser.h b/mlir/lib/AsmParser/Parser.h
index 633faf358c954..477aff08ea25f 100644
--- a/mlir/lib/AsmParser/Parser.h
+++ b/mlir/lib/AsmParser/Parser.h
@@ -229,6 +229,7 @@ class Parser {
                                              Type type = {});
   OptionalParseResult parseOptionalAttribute(ArrayAttr &attribute, Type type);
   OptionalParseResult parseOptionalAttribute(StringAttr &attribute, Type type);
+  OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type);
 
   /// Parse an optional attribute that is demarcated by a specific token.
   template <typename AttributeT>

diff  --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 08b3fd6079d20..d027db4863a21 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -1967,6 +1967,12 @@ def FormatOptSymbolNameAttrOp : TEST_Op<"format_opt_symbol_name_attr_op"> {
   let assemblyFormat = "($opt_attr^)? attr-dict";
 }
 
+// Test that we format optional symbol reference attributes properly.
+def FormatOptSymbolRefAttrOp : TEST_Op<"format_opt_symbol_ref_attr_op"> {
+  let arguments = (ins OptionalAttr<SymbolRefAttr>:$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, OptionalAttr<I64Attr>:$opt_attr);

diff  --git a/mlir/test/mlir-tblgen/op-format.mlir b/mlir/test/mlir-tblgen/op-format.mlir
index 01a63f6c34241..14e1cdb07db39 100644
--- a/mlir/test/mlir-tblgen/op-format.mlir
+++ b/mlir/test/mlir-tblgen/op-format.mlir
@@ -35,6 +35,11 @@ test.format_symbol_name_attr_op @name
 test.format_symbol_name_attr_op @opt_name
 test.format_opt_symbol_name_attr_op
 
+// CHECK: test.format_opt_symbol_ref_attr_op @foo
+// CHECK: test.format_opt_symbol_ref_attr_op {test.unit}
+test.format_opt_symbol_ref_attr_op @foo {test.unit}
+test.format_opt_symbol_ref_attr_op {test.unit}
+
 // CHECK: test.format_attr_dict_w_keyword attributes {attr = 10 : i64}
 test.format_attr_dict_w_keyword attributes {attr = 10 : i64}
 


        


More information about the Mlir-commits mailing list