[Mlir-commits] [mlir] [mlir] Move GET_ATTR/TYPEDEF_CLASSES after declarations (PR #171444)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jan 27 08:04:23 PST 2026


https://github.com/Harald-R updated https://github.com/llvm/llvm-project/pull/171444

>From c476f1048afcbd0376c573e6937006a7a79b1bf5 Mon Sep 17 00:00:00 2001
From: Harald-R <rotuna.razvan at gmail.com>
Date: Tue, 9 Dec 2025 13:14:20 +0200
Subject: [PATCH] Move GET_ATTR/TYPEDEF_CLASSES after declarations

This allows the attribute and type class declarations to be included
separately if desired, which allows them to be used as forward
declarations.
---
 mlir/test/mlir-tblgen/attrdefs.td           |  7 +++++++
 mlir/test/mlir-tblgen/typedefs.td           |  8 ++++++++
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 18 ++++++++++++------
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/mlir/test/mlir-tblgen/attrdefs.td b/mlir/test/mlir-tblgen/attrdefs.td
index a809611fd0aec..bd0c663228015 100644
--- a/mlir/test/mlir-tblgen/attrdefs.td
+++ b/mlir/test/mlir-tblgen/attrdefs.td
@@ -4,6 +4,13 @@
 include "mlir/IR/AttrTypeBase.td"
 include "mlir/IR/OpBase.td"
 
+// DECL: namespace test {
+// DECL: class IndexAttr;
+// DECL: class SimpleAAttr;
+// DECL: class CompoundAAttr;
+// DECL: class SingleParameterAttr;
+// DECL: } // namespace test
+
 // DECL: #ifdef GET_ATTRDEF_CLASSES
 // DECL: #undef GET_ATTRDEF_CLASSES
 
diff --git a/mlir/test/mlir-tblgen/typedefs.td b/mlir/test/mlir-tblgen/typedefs.td
index b9e3a7954e361..ebdb172e9aff3 100644
--- a/mlir/test/mlir-tblgen/typedefs.td
+++ b/mlir/test/mlir-tblgen/typedefs.td
@@ -4,6 +4,14 @@
 include "mlir/IR/AttrTypeBase.td"
 include "mlir/IR/OpBase.td"
 
+// DECL: namespace test {
+// DECL: class SimpleAType;
+// DECL: class CompoundAType;
+// DECL: class IndexType;
+// DECL: class SingleParameterType;
+// DECL: class IntegerType;
+// DECL: } // namespace test
+
 // DECL: #ifdef GET_TYPEDEF_CLASSES
 // DECL: #undef GET_TYPEDEF_CLASSES
 
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 2a513c3b8cc9b..e51c776b905a8 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -869,29 +869,35 @@ class AsmPrinter;
 
 bool DefGenerator::emitDecls(StringRef selectedDialect) {
   emitSourceFileHeader((defType + "Def Declarations").str(), os);
-  llvm::IfDefEmitter scope(os, "GET_" + defType.upper() + "DEF_CLASSES");
-
-  // Output the common "header".
-  os << typeDefDeclHeader;
 
   SmallVector<AttrOrTypeDef, 16> defs;
   collectAllDefs(selectedDialect, defRecords, defs);
   if (defs.empty())
     return false;
+
   {
     DialectNamespaceEmitter nsEmitter(os, defs.front().getDialect());
-
-    // Declare all the def classes first (in case they reference each other).
+    // Declare all the def classes first (in case they reference each other and
+    // for forward declarations).
     for (const AttrOrTypeDef &def : defs) {
       tblgen::emitSummaryAndDescComments(os, def.getSummary(),
                                          def.getDescription());
       os << "class " << def.getCppClassName() << ";\n";
     }
+  }
 
+  llvm::IfDefEmitter scope(os, "GET_" + defType.upper() + "DEF_CLASSES");
+
+  // Output the common "header".
+  os << typeDefDeclHeader;
+
+  {
+    DialectNamespaceEmitter nsEmitter(os, defs.front().getDialect());
     // Emit the declarations.
     for (const AttrOrTypeDef &def : defs)
       DefGen(def).emitDecl(os);
   }
+
   // Emit the TypeID explicit specializations to have a single definition for
   // each of these.
   for (const AttrOrTypeDef &def : defs)



More information about the Mlir-commits mailing list