[Mlir-commits] [mlir] [mlir][attrtypedef] Sort by def order in file. (PR #71083)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Nov 2 10:30:28 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Jacques Pienaar (jpienaar)

<details>
<summary>Changes</summary>

Enables having attribute that has another from same file as member.

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


2 Files Affected:

- (modified) mlir/test/mlir-tblgen/attrdefs.td (+22-22) 
- (modified) mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp (+6-1) 


``````````diff
diff --git a/mlir/test/mlir-tblgen/attrdefs.td b/mlir/test/mlir-tblgen/attrdefs.td
index d7228368e71eb05..0fb6958799b2ecf 100644
--- a/mlir/test/mlir-tblgen/attrdefs.td
+++ b/mlir/test/mlir-tblgen/attrdefs.td
@@ -14,9 +14,9 @@ include "mlir/IR/OpBase.td"
 
 // DEF: #ifdef GET_ATTRDEF_LIST
 // DEF: #undef GET_ATTRDEF_LIST
+// DEF: ::test::IndexAttr,
 // DEF: ::test::SimpleAAttr,
 // DEF: ::test::CompoundAAttr,
-// DEF: ::test::IndexAttr,
 // DEF: ::test::SingleParameterAttr
 
 // DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
@@ -24,13 +24,13 @@ include "mlir/IR/OpBase.td"
 // DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,
 // DEF-SAME: ::mlir::Attribute &value) {
 // DEF: return ::mlir::AsmParser::KeywordSwitch<::mlir::OptionalParseResult>(parser)
+// DEF: .Case(::test::IndexAttr::getMnemonic()
+// DEF-NEXT:   value = ::test::IndexAttr::parse(parser, type);
+// DEF-NEXT:   return ::mlir::success(!!value);
 // DEF: .Case(::test::CompoundAAttr::getMnemonic()
 // DEF-NEXT: value = ::test::CompoundAAttr::parse(parser, type);
 // DEF-NEXT: return ::mlir::success(!!value);
 // DEF-NEXT: })
-// DEF-NEXT: .Case(::test::IndexAttr::getMnemonic()
-// DEF-NEXT:   value = ::test::IndexAttr::parse(parser, type);
-// DEF-NEXT:   return ::mlir::success(!!value);
 // DEF: .Default([&](llvm::StringRef keyword,
 // DEF-NEXT:   *mnemonic = keyword;
 // DEF-NEXT:   return std::nullopt;
@@ -44,6 +44,24 @@ def Test_Dialect: Dialect {
 
 class TestAttr<string name> : AttrDef<Test_Dialect, name> { }
 
+def C_IndexAttr : TestAttr<"Index"> {
+    let mnemonic = "index";
+
+    let parameters = (
+      ins
+      StringRefParameter<"Label for index">:$label
+    );
+  let hasCustomAssemblyFormat = 1;
+
+// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
+// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
+// DECL:   return {"index"};
+// DECL: }
+// DECL: static ::mlir::Attribute parse(
+// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
+// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
+}
+
 def A_SimpleAttrA : TestAttr<"SimpleA"> {
 // DECL: class SimpleAAttr : public ::mlir::Attribute
 }
@@ -100,24 +118,6 @@ def B_CompoundAttrA : TestAttr<"CompoundA"> {
 // DEF-NEXT: return getImpl()->inner;
 }
 
-def C_IndexAttr : TestAttr<"Index"> {
-    let mnemonic = "index";
-
-    let parameters = (
-      ins
-      StringRefParameter<"Label for index">:$label
-    );
-  let hasCustomAssemblyFormat = 1;
-
-// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
-// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
-// DECL:   return {"index"};
-// DECL: }
-// DECL: static ::mlir::Attribute parse(
-// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
-// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
-}
-
 def D_SingleParameterAttr : TestAttr<"SingleParameter"> {
   let parameters = (
     ins
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 51f24de2442b957..8889dff7a587242 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -587,7 +587,12 @@ class DefGenerator {
   DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
                StringRef defType, StringRef valueType, bool isAttrGenerator)
       : defRecords(std::move(defs)), os(os), defType(defType),
-        valueType(valueType), isAttrGenerator(isAttrGenerator) {}
+        valueType(valueType), isAttrGenerator(isAttrGenerator) {
+    // Sort by occurrence in file.
+    llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
+      return lhs->getID() < rhs->getID();
+    });
+  }
 
   /// Emit the list of def type names.
   void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);

``````````

</details>


https://github.com/llvm/llvm-project/pull/71083


More information about the Mlir-commits mailing list