[Mlir-commits] [mlir] [MLIR][TableGen] Add gen-attrdef-list (PR #126332)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Feb 7 17:47:12 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Andy Kaylor (andykaylor)

<details>
<summary>Changes</summary>

This adds a new mlir-tblgen option (-gen-attrdef-list) to generate a list of AttrDefs from a .td file in a format suitable for use in patterns where a macro is defined to expand various repeated code snippets for each item in the list. Specifically, the file will contain a list in this format

ATTRDEF(MyAttr)

This will be used in ClangIR to create an attribute visitor.

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


2 Files Affected:

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


``````````diff
diff --git a/mlir/test/mlir-tblgen/attrdefs.td b/mlir/test/mlir-tblgen/attrdefs.td
index 35d2c49619ee69c..ecb1c93108597ee 100644
--- a/mlir/test/mlir-tblgen/attrdefs.td
+++ b/mlir/test/mlir-tblgen/attrdefs.td
@@ -1,5 +1,6 @@
 // RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
 // RUN: mlir-tblgen -gen-attrdef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
+// RUN: mlir-tblgen -gen-attrdef-list -I %S/../../include %s | FileCheck %s --check-prefix=LIST
 
 include "mlir/IR/AttrTypeBase.td"
 include "mlir/IR/OpBase.td"
@@ -19,6 +20,12 @@ include "mlir/IR/OpBase.td"
 // DEF: ::test::CompoundAAttr,
 // DEF: ::test::SingleParameterAttr
 
+// LIST: ATTRDEF(IndexAttr)
+// LIST: ATTRDEF(SimpleAAttr)
+// LIST: ATTRDEF(CompoundAAttr)
+// LIST: ATTRDEF(SingleParameterAttr)
+
+// LIST: #undef ATTRDEF
 // DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
 // DEF-SAME: ::mlir::AsmParser &parser,
 // DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 6a39424bd463fd7..a5a9a5c55908788 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -690,6 +690,7 @@ class DefGenerator {
 public:
   bool emitDecls(StringRef selectedDialect);
   bool emitDefs(StringRef selectedDialect);
+  bool emitList(StringRef selectedDialect);
 
 protected:
   DefGenerator(ArrayRef<const Record *> defs, raw_ostream &os,
@@ -1025,6 +1026,23 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
   return false;
 }
 
+bool DefGenerator::emitList(StringRef selectedDialect) {
+  emitSourceFileHeader(("List of " + defType + "Def Definitions").str(), os);
+
+  SmallVector<AttrOrTypeDef, 16> defs;
+  collectAllDefs(selectedDialect, defRecords, defs);
+  if (defs.empty())
+    return false;
+
+  auto interleaveFn = [&](const AttrOrTypeDef &def) {
+    os << defType.upper() << "DEF(" << def.getCppClassName() << ")";
+  };
+  llvm::interleave(defs, os, interleaveFn, "\n");
+  os << "\n\n";
+  os << "#undef " << defType.upper() << "DEF" << "\n";
+  return false;
+}
+
 //===----------------------------------------------------------------------===//
 // Type Constraints
 //===----------------------------------------------------------------------===//
@@ -1099,7 +1117,12 @@ static mlir::GenRegistration
                    AttrDefGenerator generator(records, os);
                    return generator.emitDecls(attrDialect);
                  });
-
+static mlir::GenRegistration
+    genAttrList("gen-attrdef-list", "Generate an AttrDef list",
+                [](const RecordKeeper &records, raw_ostream &os) {
+                  AttrDefGenerator generator(records, os);
+                  return generator.emitList(attrDialect);
+                });
 //===----------------------------------------------------------------------===//
 // TypeDef
 

``````````

</details>


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


More information about the Mlir-commits mailing list