[Mlir-commits] [llvm] [mlir] [mlir-c] expand the mlir llvm dialect C api with more attributes/enums (PR #84453)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 22 00:55:09 PDT 2024


https://github.com/edg-l updated https://github.com/llvm/llvm-project/pull/84453

>From d1c13c098e57231bfc2c74bf4fb0c894b98b1c07 Mon Sep 17 00:00:00 2001
From: Edgar Luque <git at edgarluque.com>
Date: Mon, 22 Jul 2024 09:51:45 +0200
Subject: [PATCH] [mlir-c] expand the mlir llvm dialect C api with more
 attributes/enums

---
 mlir/include/mlir-c/Dialect/LLVM.h            | 178 +++++++++---------
 .../mlir/Dialect/LLVMIR/CMakeLists.txt        |   1 +
 mlir/lib/CAPI/Dialect/LLVM.cpp                | 103 +++++++++-
 mlir/test/CAPI/llvm.c                         |  23 ++-
 mlir/tools/mlir-tblgen/EnumsGen.cpp           |  58 ++++++
 .../llvm-project-overlay/mlir/BUILD.bazel     |   4 +
 6 files changed, 266 insertions(+), 101 deletions(-)

diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h
index 902b45444d6c4..e2b5c9ea56c3c 100644
--- a/mlir/include/mlir-c/Dialect/LLVM.h
+++ b/mlir/include/mlir-c/Dialect/LLVM.h
@@ -12,6 +12,7 @@
 
 #include "mlir-c/IR.h"
 #include "mlir-c/Support.h"
+#include "mlir/Dialect/LLVMIR/LLVMOpsEnums.capi.h.inc"
 
 #ifdef __cplusplus
 extern "C" {
@@ -106,89 +107,14 @@ MLIR_CAPI_EXPORTED MlirLogicalResult
 mlirLLVMStructTypeSetBody(MlirType structType, intptr_t nFieldTypes,
                           MlirType const *fieldTypes, bool isPacked);
 
-enum MlirLLVMCConv {
-  MlirLLVMCConvC = 0,
-  MlirLLVMCConvFast = 8,
-  MlirLLVMCConvCold = 9,
-  MlirLLVMCConvGHC = 10,
-  MlirLLVMCConvHiPE = 11,
-  MlirLLVMCConvAnyReg = 13,
-  MlirLLVMCConvPreserveMost = 14,
-  MlirLLVMCConvPreserveAll = 15,
-  MlirLLVMCConvSwift = 16,
-  MlirLLVMCConvCXX_FAST_TLS = 17,
-  MlirLLVMCConvTail = 18,
-  MlirLLVMCConvCFGuard_Check = 19,
-  MlirLLVMCConvSwiftTail = 20,
-  MlirLLVMCConvX86_StdCall = 64,
-  MlirLLVMCConvX86_FastCall = 65,
-  MlirLLVMCConvARM_APCS = 66,
-  MlirLLVMCConvARM_AAPCS = 67,
-  MlirLLVMCConvARM_AAPCS_VFP = 68,
-  MlirLLVMCConvMSP430_INTR = 69,
-  MlirLLVMCConvX86_ThisCall = 70,
-  MlirLLVMCConvPTX_Kernel = 71,
-  MlirLLVMCConvPTX_Device = 72,
-  MlirLLVMCConvSPIR_FUNC = 75,
-  MlirLLVMCConvSPIR_KERNEL = 76,
-  MlirLLVMCConvIntel_OCL_BI = 77,
-  MlirLLVMCConvX86_64_SysV = 78,
-  MlirLLVMCConvWin64 = 79,
-  MlirLLVMCConvX86_VectorCall = 80,
-  MlirLLVMCConvDUMMY_HHVM = 81,
-  MlirLLVMCConvDUMMY_HHVM_C = 82,
-  MlirLLVMCConvX86_INTR = 83,
-  MlirLLVMCConvAVR_INTR = 84,
-  MlirLLVMCConvAVR_BUILTIN = 86,
-  MlirLLVMCConvAMDGPU_VS = 87,
-  MlirLLVMCConvAMDGPU_GS = 88,
-  MlirLLVMCConvAMDGPU_CS = 90,
-  MlirLLVMCConvAMDGPU_KERNEL = 91,
-  MlirLLVMCConvX86_RegCall = 92,
-  MlirLLVMCConvAMDGPU_HS = 93,
-  MlirLLVMCConvMSP430_BUILTIN = 94,
-  MlirLLVMCConvAMDGPU_LS = 95,
-  MlirLLVMCConvAMDGPU_ES = 96,
-  MlirLLVMCConvAArch64_VectorCall = 97,
-  MlirLLVMCConvAArch64_SVE_VectorCall = 98,
-  MlirLLVMCConvWASM_EmscriptenInvoke = 99,
-  MlirLLVMCConvAMDGPU_Gfx = 100,
-  MlirLLVMCConvM68k_INTR = 101,
-};
-typedef enum MlirLLVMCConv MlirLLVMCConv;
-
 /// Creates a LLVM CConv attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMCConvAttrGet(MlirContext ctx,
                                                       MlirLLVMCConv cconv);
 
-enum MlirLLVMComdat {
-  MlirLLVMComdatAny = 0,
-  MlirLLVMComdatExactMatch = 1,
-  MlirLLVMComdatLargest = 2,
-  MlirLLVMComdatNoDeduplicate = 3,
-  MlirLLVMComdatSameSize = 4,
-};
-typedef enum MlirLLVMComdat MlirLLVMComdat;
-
 /// Creates a LLVM Comdat attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMComdatAttrGet(MlirContext ctx,
                                                        MlirLLVMComdat comdat);
 
-enum MlirLLVMLinkage {
-  MlirLLVMLinkagePrivate = 0,
-  MlirLLVMLinkageInternal = 1,
-  MlirLLVMLinkageAvailableExternally = 2,
-  MlirLLVMLinkageLinkonce = 3,
-  MlirLLVMLinkageWeak = 4,
-  MlirLLVMLinkageCommon = 5,
-  MlirLLVMLinkageAppending = 6,
-  MlirLLVMLinkageExternWeak = 7,
-  MlirLLVMLinkageLinkonceODR = 8,
-  MlirLLVMLinkageWeakODR = 9,
-  MlirLLVMLinkageExternal = 10,
-};
-typedef enum MlirLLVMLinkage MlirLLVMLinkage;
-
 /// Creates a LLVM Linkage attribute.
 MLIR_CAPI_EXPORTED MlirAttribute
 mlirLLVMLinkageAttrGet(MlirContext ctx, MlirLLVMLinkage linkage);
@@ -258,7 +184,7 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIStringTypeAttrGet(
     MlirLLVMTypeEncoding encoding);
 
 /// Constant to represent std::nullopt for dwarfAddressSpace to omit the field.
-#define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL -1
+#define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL (-1)
 
 /// Gets the base type from a LLVM DIDerivedType attribute.
 MLIR_CAPI_EXPORTED MlirAttribute
@@ -269,22 +195,6 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFileAttrGet(MlirContext ctx,
                                                        MlirAttribute name,
                                                        MlirAttribute directory);
 
-enum MlirLLVMDIEmissionKind {
-  MlirLLVMDIEmissionKindNone = 0,
-  MlirLLVMDIEmissionKindFull = 1,
-  MlirLLVMDIEmissionKindLineTablesOnly = 2,
-  MlirLLVMDIEmissionKindDebugDirectivesOnly = 3,
-};
-typedef enum MlirLLVMDIEmissionKind MlirLLVMDIEmissionKind;
-
-enum MlirLLVMDINameTableKind {
-  MlirLLVMDINameTableKindDefault = 0,
-  MlirLLVMDINameTableKindGNU = 1,
-  MlirLLVMDINameTableKindNone = 2,
-  MlirLLVMDINameTableKindApple = 3,
-};
-typedef enum MlirLLVMDINameTableKind MlirLLVMDINameTableKind;
-
 /// Creates a LLVM DICompileUnit attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet(
     MlirContext ctx, MlirAttribute id, unsigned int sourceLanguage,
@@ -293,7 +203,7 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet(
 
 /// Creates a LLVM DIFlags attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx,
-                                                        uint64_t value);
+                                                        MlirLLVMDIFlags value);
 
 /// Creates a LLVM DILexicalBlock attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILexicalBlockAttrGet(
@@ -311,12 +221,18 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILocalVariableAttrGet(
     MlirAttribute diFile, unsigned int line, unsigned int arg,
     unsigned int alignInBits, MlirAttribute diType);
 
+/// Creates a LLVM DINamespaceAttr attribute.
+MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDINamespaceAttrGet(MlirContext ctx,
+                                                            MlirAttribute name,
+                                                            MlirAttribute scope,
+                                                            bool exportSymbols);
+
 /// Creates a LLVM DISubprogramAttr attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet(
     MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
     MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
     MlirAttribute file, unsigned int line, unsigned int scopeLine,
-    uint64_t subprogramFlags, MlirAttribute type);
+    MlirLLVMDISubprogramFlags subprogramFlags, MlirAttribute type);
 
 /// Gets the scope from this DISubprogramAttr.
 MLIR_CAPI_EXPORTED MlirAttribute
@@ -330,6 +246,18 @@ mlirLLVMDISubprogramAttrGetLine(MlirAttribute diSubprogram);
 MLIR_CAPI_EXPORTED unsigned int
 mlirLLVMDISubprogramAttrGetScopeLine(MlirAttribute diSubprogram);
 
+/// Gets the linkage name from this DISubprogramAttr.
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirLLVMDISubprogramAttrGetLinkageName(MlirAttribute diSubprogram);
+
+/// Gets the name from this DISubprogramAttr.
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirLLVMDISubprogramAttrGetName(MlirAttribute diSubprogram);
+
+/// Gets the subprogram flags from this DISubprogramAttr.
+MLIR_CAPI_EXPORTED MlirLLVMDISubprogramFlags
+mlirLLVMDISubprogramAttrGetSubprogramFlags(MlirAttribute diSubprogram);
+
 /// Gets the compile unit from this DISubprogram.
 MLIR_CAPI_EXPORTED MlirAttribute
 mlirLLVMDISubprogramAttrGetCompileUnit(MlirAttribute diSubprogram);
@@ -357,6 +285,68 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIModuleAttrGet(
 MLIR_CAPI_EXPORTED MlirAttribute
 mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule);
 
+/// Gets the api notes of this DIModuleAttr.
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirLLVMDIModuleAttrGetApinotes(MlirAttribute diModule);
+
+/// Gets the config macros of this DIModuleAttr.
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirLLVMDIModuleAttrGetConfigMacros(MlirAttribute diModule);
+
+/// Gets the file of this DIModuleAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMDIModuleAttrGetFile(MlirAttribute diModule);
+
+/// Gets the include path of this DIModuleAttr.
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirLLVMDIModuleAttrGetIncludePath(MlirAttribute diModule);
+
+/// Gets whether this DIModuleAttr is a declaration.
+MLIR_CAPI_EXPORTED bool mlirLLVMDIModuleAttrGetIsDecl(MlirAttribute diModule);
+
+/// Creates a LLVM DISubrange attribute.
+///
+/// All parameters have the type IntegerAttr.
+MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubrangeAttrGet(
+    MlirContext ctx, MlirAttribute count, MlirAttribute lowerBound,
+    MlirAttribute upperBound, MlirAttribute stride);
+
+/// Creates a LLVM AtomicOrderingAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMAtomicOrderingAttrGet(MlirContext ctx, MlirLLVMAtomicOrdering ordering);
+
+/// Creates a LLVM AtomicBinOpAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMAtomicBinOpAttrGet(MlirContext ctx, MlirLLVMAtomicBinOp val);
+
+/// Creates a LLVM VisibilityAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMVisibilityAttrGet(MlirContext ctx, MlirLLVMVisibility visibility);
+
+/// Creates a LLVM UnnamedAddrAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMUnnamedAddrAttrGet(MlirContext ctx, MlirLLVMUnnamedAddr val);
+
+/// Creates a LLVM ICmpPredicateAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMICmpPredicateAttrGet(MlirContext ctx, MlirLLVMICmpPredicate val);
+
+/// Creates a LLVM FCmpPredicateAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMFCmpPredicateAttrGet(MlirContext ctx, MlirLLVMFCmpPredicate val);
+
+/// Creates a LLVM FramePointerKindAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMFramePointerKindAttrGet(MlirContext ctx, MlirLLVMFramePointerKind val);
+
+/// Creates a LLVM FastmathFlagsAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMFastmathFlagsAttrGet(MlirContext ctx, MlirLLVMFastmathFlags val);
+
+/// Creates a LLVM ModRefInfoAttr.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLLVMModRefInfoAttrGet(MlirContext ctx, MlirLLVMModRefInfo val);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
index 759de745440c2..7e2f3a9f85df6 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
@@ -6,6 +6,7 @@ mlir_tablegen(LLVMOps.cpp.inc -gen-op-defs)
 mlir_tablegen(LLVMOpsDialect.h.inc -gen-dialect-decls)
 mlir_tablegen(LLVMOpsDialect.cpp.inc -gen-dialect-defs)
 mlir_tablegen(LLVMOpsEnums.h.inc -gen-enum-decls)
+mlir_tablegen(LLVMOpsEnums.capi.h.inc -gen-enum-capi-decls)
 mlir_tablegen(LLVMOpsEnums.cpp.inc -gen-enum-defs)
 mlir_tablegen(LLVMOpsAttrDefs.h.inc -gen-attrdef-decls
               -attrdefs-dialect=llvm)
diff --git a/mlir/lib/CAPI/Dialect/LLVM.cpp b/mlir/lib/CAPI/Dialect/LLVM.cpp
index 754c94511524d..e0c87ba2541ee 100644
--- a/mlir/lib/CAPI/Dialect/LLVM.cpp
+++ b/mlir/lib/CAPI/Dialect/LLVM.cpp
@@ -243,7 +243,7 @@ mlirLLVMDICompileUnitAttrGet(MlirContext ctx, MlirAttribute id,
       DINameTableKind(nameTableKind)));
 }
 
-MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx, uint64_t value) {
+MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx, MlirLLVMDIFlags value) {
   return wrap(DIFlagsAttr::get(unwrap(ctx), DIFlags(value)));
 }
 
@@ -290,11 +290,19 @@ MlirAttribute mlirLLVMDISubroutineTypeAttrGet(MlirContext ctx,
                           [](Attribute a) { return cast<DITypeAttr>(a); })));
 }
 
+MlirAttribute mlirLLVMDINamespaceAttrGet(MlirContext ctx, MlirAttribute name,
+                                         MlirAttribute scope,
+                                         bool exportSymbols) {
+  return wrap(DINamespaceAttr::get(unwrap(ctx), cast<StringAttr>(unwrap(name)),
+                                   cast<DIScopeAttr>(unwrap(scope)),
+                                   exportSymbols));
+}
+
 MlirAttribute mlirLLVMDISubprogramAttrGet(
     MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
     MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
     MlirAttribute file, unsigned int line, unsigned int scopeLine,
-    uint64_t subprogramFlags, MlirAttribute type) {
+    MlirLLVMDISubprogramFlags subprogramFlags, MlirAttribute type) {
   return wrap(DISubprogramAttr::get(
       unwrap(ctx), cast<DistinctAttr>(unwrap(id)),
       cast<DICompileUnitAttr>(unwrap(compileUnit)),
@@ -316,6 +324,21 @@ unsigned int mlirLLVMDISubprogramAttrGetScopeLine(MlirAttribute diSubprogram) {
   return cast<DISubprogramAttr>(unwrap(diSubprogram)).getScopeLine();
 }
 
+MlirIdentifier
+mlirLLVMDISubprogramAttrGetLinkageName(MlirAttribute diSubprogram) {
+  return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getLinkageName());
+}
+
+MlirIdentifier mlirLLVMDISubprogramAttrGetName(MlirAttribute diSubprogram) {
+  return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getName());
+}
+
+MlirLLVMDISubprogramFlags
+mlirLLVMDISubprogramAttrGetSubprogramFlags(MlirAttribute diSubprogram) {
+  return static_cast<MlirLLVMDISubprogramFlags>(
+      cast<DISubprogramAttr>(unwrap(diSubprogram)).getSubprogramFlags());
+}
+
 MlirAttribute
 mlirLLVMDISubprogramAttrGetCompileUnit(MlirAttribute diSubprogram) {
   return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getCompileUnit());
@@ -346,3 +369,79 @@ MlirAttribute mlirLLVMDIModuleAttrGet(MlirContext ctx, MlirAttribute file,
 MlirAttribute mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule) {
   return wrap(cast<DIModuleAttr>(unwrap(diModule)).getScope());
 }
+
+MlirIdentifier mlirLLVMDIModuleAttrGetApinotes(MlirAttribute diModule) {
+  return wrap(cast<DIModuleAttr>(unwrap(diModule)).getApinotes());
+}
+
+MlirIdentifier mlirLLVMDIModuleAttrGetConfigMacros(MlirAttribute diModule) {
+  return wrap(cast<DIModuleAttr>(unwrap(diModule)).getConfigMacros());
+}
+
+MlirAttribute mlirLLVMDIModuleAttrGetFile(MlirAttribute diModule) {
+  return wrap(cast<DIModuleAttr>(unwrap(diModule)).getFile());
+}
+
+MlirIdentifier mlirLLVMDIModuleAttrGetIncludePath(MlirAttribute diModule) {
+  return wrap(cast<DIModuleAttr>(unwrap(diModule)).getIncludePath());
+}
+
+bool mlirLLVMDIModuleAttrGetIsDecl(MlirAttribute diModule) {
+  return cast<DIModuleAttr>(unwrap(diModule)).getIsDecl();
+}
+
+MlirAttribute mlirLLVMDISubrangeAttrGet(MlirContext ctx, MlirAttribute count,
+                                        MlirAttribute lowerBound,
+                                        MlirAttribute upperBound,
+                                        MlirAttribute stride) {
+  return wrap(DISubrangeAttr::get(unwrap(ctx), cast<IntegerAttr>(unwrap(count)),
+                                  cast<IntegerAttr>(unwrap(lowerBound)),
+                                  cast<IntegerAttr>(unwrap(upperBound)),
+                                  cast<IntegerAttr>(unwrap(stride))));
+}
+
+MlirAttribute mlirLLVMAtomicOrderingAttrGet(MlirContext ctx,
+                                            MlirLLVMAtomicOrdering ordering) {
+  return wrap(AtomicOrderingAttr::get(unwrap(ctx), AtomicOrdering(ordering)));
+}
+
+MlirAttribute mlirLLVMAtomicBinOpAttrGet(MlirContext ctx,
+                                         MlirLLVMAtomicBinOp val) {
+  return wrap(AtomicBinOpAttr::get(unwrap(ctx), AtomicBinOp(val)));
+}
+
+MlirAttribute mlirLLVMVisibilityAttrGet(MlirContext ctx,
+                                        MlirLLVMVisibility visibility) {
+  return wrap(VisibilityAttr::get(unwrap(ctx), Visibility(visibility)));
+}
+
+MlirAttribute mlirLLVMUnnamedAddrAttrGet(MlirContext ctx,
+                                         MlirLLVMUnnamedAddr val) {
+  return wrap(UnnamedAddrAttr::get(unwrap(ctx), UnnamedAddr(val)));
+}
+
+MlirAttribute mlirLLVMICmpPredicateAttrGet(MlirContext ctx,
+                                           MlirLLVMICmpPredicate val) {
+  return wrap(ICmpPredicateAttr::get(unwrap(ctx), ICmpPredicate(val)));
+}
+
+MlirAttribute mlirLLVMFCmpPredicateAttrGet(MlirContext ctx,
+                                           MlirLLVMFCmpPredicate val) {
+  return wrap(FCmpPredicateAttr::get(unwrap(ctx), FCmpPredicate(val)));
+}
+
+MlirAttribute mlirLLVMFramePointerKindAttrGet(MlirContext ctx,
+                                              MlirLLVMFramePointerKind val) {
+  return wrap(FramePointerKindAttr::get(
+      unwrap(ctx), framePointerKind::FramePointerKind(val)));
+}
+
+MlirAttribute mlirLLVMFastmathFlagsAttrGet(MlirContext ctx,
+                                           MlirLLVMFastmathFlags val) {
+  return wrap(FastmathFlagsAttr::get(unwrap(ctx), FastmathFlags(val)));
+}
+
+MlirAttribute mlirLLVMModRefInfoAttrGet(MlirContext ctx,
+                                        MlirLLVMModRefInfo val) {
+  return wrap(ModRefInfoAttr::get(unwrap(ctx), ModRefInfo(val)));
+}
diff --git a/mlir/test/CAPI/llvm.c b/mlir/test/CAPI/llvm.c
index 082ddc53e6282..5b1faca87a168 100644
--- a/mlir/test/CAPI/llvm.c
+++ b/mlir/test/CAPI/llvm.c
@@ -281,8 +281,8 @@ static void testDebugInfoAttributes(MlirContext ctx) {
   // CHECK: #llvm.di_compile_unit<{{.*}}>
   mlirAttributeDump(mlirLLVMDIModuleAttrGetScope(di_module));
 
-  // CHECK: 1 : i32
-  mlirAttributeDump(mlirLLVMDIFlagsAttrGet(ctx, 0x1));
+  // CHECK: 16777216 : i32
+  mlirAttributeDump(mlirLLVMDIFlagsAttrGet(ctx, MlirLLVMDIFlagsEnumClass));
 
   // CHECK: #llvm.di_lexical_block<{{.*}}>
   mlirAttributeDump(
@@ -312,9 +312,9 @@ static void testDebugInfoAttributes(MlirContext ctx) {
   // CHECK: #llvm.di_subroutine_type<{{.*}}>
   mlirAttributeDump(subroutine_type);
 
-  MlirAttribute di_subprogram =
-      mlirLLVMDISubprogramAttrGet(ctx, id, compile_unit, compile_unit, foo, bar,
-                                  file, 1, 2, 0, subroutine_type);
+  MlirAttribute di_subprogram = mlirLLVMDISubprogramAttrGet(
+      ctx, id, compile_unit, compile_unit, foo, bar, file, 1, 2,
+      MlirLLVMDISubprogramFlagsDefinition, subroutine_type);
   // CHECK: #llvm.di_subprogram<{{.*}}>
   mlirAttributeDump(di_subprogram);
 
@@ -348,6 +348,19 @@ static void testDebugInfoAttributes(MlirContext ctx) {
   mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
       ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type,
       expression, expression, expression, expression));
+
+  mlirAttributeDump(mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem));
+
+  // CHECK: #llvm.di_namespace<{{.*}}>
+  mlirAttributeDump(mlirLLVMDINamespaceAttrGet(ctx, foo, compile_unit, true));
+
+  MlirType type = mlirIntegerTypeGet(ctx, 64);
+
+  // CHECK: #llvm.di_subrange<count = 1 : i64, lowerBound = 1 : i64, upperBound
+  // CHECK-SAME: = 10 : i64, stride = 1 : i64>
+  mlirAttributeDump(mlirLLVMDISubrangeAttrGet(
+      ctx, mlirIntegerAttrGet(type, 1), mlirIntegerAttrGet(type, 1),
+      mlirIntegerAttrGet(type, 10), mlirIntegerAttrGet(type, 1)));
 }
 
 int main(void) {
diff --git a/mlir/tools/mlir-tblgen/EnumsGen.cpp b/mlir/tools/mlir-tblgen/EnumsGen.cpp
index f1d7a233b66a9..5fb13c0254753 100644
--- a/mlir/tools/mlir-tblgen/EnumsGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumsGen.cpp
@@ -67,6 +67,27 @@ static void emitEnumClass(const Record &enumDef, StringRef enumName,
   os << "};\n\n";
 }
 
+static void emitCEnum(const Record &enumDef, StringRef enumName,
+                      StringRef underlyingType, StringRef description,
+                      const std::vector<EnumAttrCase> &enumerants,
+                      raw_ostream &os) {
+  os << "// " << description << "\n";
+  os << "enum MlirLLVM" << enumName << " {\n";
+
+  for (const auto &enumerant : enumerants) {
+    auto symbol = makeIdentifier(enumerant.getSymbol());
+    auto value = enumerant.getValue();
+    if (value >= 0) {
+      os << formatv("  MlirLLVM{0}{1} = {2},\n", enumName, symbol, value);
+    } else {
+      os << formatv("  MlirLLVM{0}{0},\n", enumName, symbol);
+    }
+  }
+  os << "};\n";
+  os << "typedef enum MlirLLVM" << enumName << " MlirLLVM" << enumName
+     << ";\n\n";
+}
+
 static void emitParserPrinter(const EnumAttr &enumAttr, StringRef qualName,
                               StringRef cppNamespace, raw_ostream &os) {
   if (enumAttr.getUnderlyingType().empty() ||
@@ -642,6 +663,17 @@ class {1} : public ::mlir::{2} {
   emitDenseMapInfo(qualName, underlyingType, cppNamespace, os);
 }
 
+static void emitCEnumDecl(const Record &enumDef, raw_ostream &os) {
+  EnumAttr enumAttr(enumDef);
+  StringRef enumName = enumAttr.getEnumClassName();
+  std::string underlyingType = std::string(enumAttr.getUnderlyingType());
+  StringRef description = enumAttr.getSummary();
+  auto enumerants = enumAttr.getAllCases();
+
+  // Emit the enum class definition
+  emitCEnum(enumDef, enumName, underlyingType, description, enumerants, os);
+}
+
 static bool emitEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
   llvm::emitSourceFileHeader("Enum Utility Declarations", os, recordKeeper);
 
@@ -652,6 +684,24 @@ static bool emitEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
   return false;
 }
 
+static bool emitCEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
+  llvm::emitSourceFileHeader("Enum C Utility Declarations", os, recordKeeper);
+
+  os << "#ifdef __cplusplus\n";
+  os << "extern \"C\" {\n";
+  os << "#endif\n\n";
+
+  auto defs = recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo");
+  for (const auto *def : defs)
+    emitCEnumDecl(*def, os);
+
+  os << "\n\n#ifdef __cplusplus\n";
+  os << "}\n";
+  os << "#endif\n";
+
+  return false;
+}
+
 static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
   EnumAttr enumAttr(enumDef);
   StringRef cppNamespace = enumAttr.getCppNamespace();
@@ -697,6 +747,14 @@ static mlir::GenRegistration
                    return emitEnumDecls(records, os);
                  });
 
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genCEnumDecls("gen-enum-capi-decls",
+                  "Generate CAPI enum utility declarations",
+                  [](const RecordKeeper &records, raw_ostream &os) {
+                    return emitCEnumDecls(records, os);
+                  });
+
 // Registers the enum utility generator to mlir-tblgen.
 static mlir::GenRegistration
     genEnumDefs("gen-enum-defs", "Generate enum utility definitions",
diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 0d0224e6d6802..64b20f7a31a3e 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -6108,6 +6108,10 @@ gentbl_cc_library(
             ["-gen-enum-decls"],
             "include/mlir/Dialect/LLVMIR/LLVMOpsEnums.h.inc",
         ),
+        (
+            ["-gen-enum-capi-decls"],
+            "include/mlir/Dialect/LLVMIR/LLVMOpsEnums.capi.h.inc"
+        )
         (
             ["-gen-enum-defs"],
             "include/mlir/Dialect/LLVMIR/LLVMOpsEnums.cpp.inc",



More information about the Mlir-commits mailing list