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

Jacques Pienaar llvmlistbot at llvm.org
Thu Nov 2 10:29:58 PDT 2023


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

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

>From ad418662d266c57645709cfd8a6958c930ab4e12 Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 27 Oct 2023 16:48:51 -0700
Subject: [PATCH] [mlir][attrtypedef] Sort by def order in file.

Enables having attribute that has another from same file as member.
---
 mlir/test/mlir-tblgen/attrdefs.td           | 44 ++++++++++-----------
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp |  7 +++-
 2 files changed, 28 insertions(+), 23 deletions(-)

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);



More information about the Mlir-commits mailing list