[Mlir-commits] [mlir] [MLIR][tblgen] generate EnumAttr C bindings (PR #186633)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Mar 14 18:04:33 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- mlir/tools/mlir-tblgen/AttrOrTypeCAPIGen.cpp mlir/include/mlir-c/Dialect/Index.h mlir/lib/CAPI/Dialect/Index.cpp mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.h --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeCAPIGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeCAPIGen.cpp
index 45a6e5b52..b83d5caf9 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeCAPIGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeCAPIGen.cpp
@@ -83,7 +83,7 @@ struct CAPIDefGenerator : DefGenerator {
       : DefGenerator(defs, os, defType, valueType, isAttrGenerator) {}
 
   bool emitDecls(StringRef selectedDialect) override;
-  bool emitDefs(StringRef selectedDialect) override;  
+  bool emitDefs(StringRef selectedDialect) override;
 };
 } // namespace
 
@@ -99,8 +99,8 @@ static bool isUnsupportedParam(const AttrOrTypeParameter &param) {
 }
 
 static std::string toLower(std::string s) {
-    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
-    return s;
+  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
+  return s;
 }
 
 static std::string mapParamTypeToCAPI(const AttrOrTypeParameter &param) {
@@ -135,7 +135,7 @@ getGettorParams(ArrayRef<AttrOrTypeParameter> params,
 }
 
 static bool isEnumParam(const AttrOrTypeParameter &param) {
-  // Do I need a case for StringInits? I've seen cases where some types are 
+  // Do I need a case for StringInits? I've seen cases where some types are
   // encoded as strings
   if (const llvm::DefInit *defInit = dyn_cast<llvm::DefInit>(param.getDef())) {
     const Record *rec = defInit->getDef();
@@ -145,8 +145,10 @@ static bool isEnumParam(const AttrOrTypeParameter &param) {
   }
 }
 
-static void emitGettorDeclOrDef(StringRef name, ArrayRef<AttrOrTypeParameter> params,
-                                raw_ostream &os, bool isAttrGenerator, bool isDeclGenerator) {
+static void emitGettorDeclOrDef(StringRef name,
+                                ArrayRef<AttrOrTypeParameter> params,
+                                raw_ostream &os, bool isAttrGenerator,
+                                bool isDeclGenerator) {
   os << "MLIR_CAPI_EXPORTED ";
   if (isAttrGenerator)
     os << "MlirAttribute ";
@@ -167,11 +169,12 @@ static void emitGettorDeclOrDef(StringRef name, ArrayRef<AttrOrTypeParameter> pa
     os << name << "::get(unwrap(context)";
     if (params.size() > 0) {
       os << ", ";
-    } 
+    }
     for (auto [i, param] : llvm::enumerate(params)) {
       // If this is an enum, just use C++ static cast
       if (isEnumParam(param)) {
-        os << "static_cast<" << param.getCppType() << ">(" << param.getName() << ")";
+        os << "static_cast<" << param.getCppType() << ">(" << param.getName()
+           << ")";
       } else {
         os << "llvm::cast<" << param.getCppType() << ">";
         // Is this a wrapped type? If so unwrap it, otherwised don't
@@ -256,7 +259,8 @@ static bool emitEnumDecls(ArrayRef<const Record *> records, raw_ostream &os) {
     }
     os << "};\n";
     // Add convenience typedef
-    os << formatv("typedef enum {0}{1} {0}{1}; \n", toLower(namespacePrefix()), enumInfo.getEnumClassName());
+    os << formatv("typedef enum {0}{1} {0}{1}; \n", toLower(namespacePrefix()),
+                  enumInfo.getEnumClassName());
   }
 
   os << "\n";
@@ -293,7 +297,8 @@ static bool emitEnumAttrDecls(ArrayRef<const Record *> records, raw_ostream &os,
 
     os << "MLIR_CAPI_EXPORTED MlirAttribute " << namespacePrefix()
        << enumInfo.getEnumClassName() << "AttrGet(MlirContext context, "
-       << toLower(namespacePrefix()) << enumInfo.getEnumClassName() << " value);\n";
+       << toLower(namespacePrefix()) << enumInfo.getEnumClassName()
+       << " value);\n";
 
     std::string name = enumInfo.getEnumClassName().str() + "Attr";
     emitTypeIDDecl(name, os);
@@ -301,7 +306,8 @@ static bool emitEnumAttrDecls(ArrayRef<const Record *> records, raw_ostream &os,
 
     os << "MLIR_CAPI_EXPORTED " << toLower(namespacePrefix())
        << enumInfo.getEnumClassName() << " ";
-    os << toLower(namespacePrefix()) << name << "GetValue(MlirAttribute attr);\n";
+    os << toLower(namespacePrefix()) << name
+       << "GetValue(MlirAttribute attr);\n";
   }
 
   os << "\n";
@@ -358,7 +364,7 @@ bool CAPIDefGenerator::emitDefs(StringRef selectedDialect) {
     StringRef name = def.getCppClassName();
     ArrayRef<AttrOrTypeParameter> params = def.getParameters();
     if (!llvm::any_of(params, isUnsupportedParam)) {
-        emitGettorDeclOrDef(name, params, os, isAttrGenerator, false);
+      emitGettorDeclOrDef(name, params, os, isAttrGenerator, false);
     }
   }
 
@@ -406,12 +412,13 @@ static mlir::GenRegistration genAttrDecls(
       return generator.emitDecls(capiDialect);
     });
 
-static mlir::GenRegistration genAttrDefs(
-  "gen-attrdef-capi-defs", "Generate AttrDef C API definitions",
-  [](const RecordKeeper &records, raw_ostream &os) {
-      CAPIAttrDefGenerator generator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os);
-      return generator.emitDefs(capiDialect);
-});
+static mlir::GenRegistration
+    genAttrDefs("gen-attrdef-capi-defs", "Generate AttrDef C API definitions",
+                [](const RecordKeeper &records, raw_ostream &os) {
+                  CAPIAttrDefGenerator generator(
+                      records.getAllDerivedDefinitionsIfDefined("AttrDef"), os);
+                  return generator.emitDefs(capiDialect);
+                });
 
 //===----------------------------------------------------------------------===//
 // TypeDef

``````````

</details>


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


More information about the Mlir-commits mailing list