Skip to content

[MLIR][Python] Forward the name of MLIR types to Python side#174700

Merged
PragmaTwice merged 4 commits into
llvm:mainfrom
PragmaTwice:mlir-python-type-name-v2
Jan 7, 2026
Merged

[MLIR][Python] Forward the name of MLIR types to Python side#174700
PragmaTwice merged 4 commits into
llvm:mainfrom
PragmaTwice:mlir-python-type-name-v2

Conversation

@PragmaTwice
Copy link
Copy Markdown
Member

In this PR, I added a C API for each (upstream) MLIR type to retrieve its type name (for example, IntegerType -> mlirIntegerTypeGetName() -> "builtin.integer"), and exposed a corresponding type_name class attribute in the Python bindings (e.g., IntegerType.type_name -> "builtin.integer"). This can be used in various places to avoid hard-coded strings, such as eliminating the manual string in irdl.base("!builtin.integer").

Note that parts of this PR (mainly mechanical changes) were produced via GitHub Copilot and GPT-5.2. I have manually reviewed the changes and verified them with tests to ensure correctness.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 7, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Jan 7, 2026

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-backend-amdgpu

Author: Twice (PragmaTwice)

Changes

In this PR, I added a C API for each (upstream) MLIR type to retrieve its type name (for example, IntegerType -> mlirIntegerTypeGetName() -> "builtin.integer"), and exposed a corresponding type_name class attribute in the Python bindings (e.g., IntegerType.type_name -> "builtin.integer"). This can be used in various places to avoid hard-coded strings, such as eliminating the manual string in irdl.base("!builtin.integer").

Note that parts of this PR (mainly mechanical changes) were produced via GitHub Copilot and GPT-5.2. I have manually reviewed the changes and verified them with tests to ensure correctness.


Patch is 75.99 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/174700.diff

31 Files Affected:

  • (modified) mlir/include/mlir-c/BuiltinTypes.h (+56)
  • (modified) mlir/include/mlir-c/Dialect/AMDGPU.h (+6)
  • (modified) mlir/include/mlir-c/Dialect/EmitC.h (+14)
  • (modified) mlir/include/mlir-c/Dialect/GPU.h (+2)
  • (modified) mlir/include/mlir-c/Dialect/LLVM.h (+10)
  • (modified) mlir/include/mlir-c/Dialect/NVGPU.h (+2)
  • (modified) mlir/include/mlir-c/Dialect/PDL.h (+10)
  • (modified) mlir/include/mlir-c/Dialect/Quant.h (+11)
  • (modified) mlir/include/mlir-c/Dialect/SMT.h (+6)
  • (modified) mlir/include/mlir-c/Dialect/Transform.h (+10)
  • (modified) mlir/include/mlir/Bindings/Python/IRCore.h (+8)
  • (modified) mlir/include/mlir/Bindings/Python/IRTypes.h (+29)
  • (modified) mlir/lib/Bindings/Python/DialectAMDGPU.cpp (+3)
  • (modified) mlir/lib/Bindings/Python/DialectGPU.cpp (+1)
  • (modified) mlir/lib/Bindings/Python/DialectLLVM.cpp (+2)
  • (modified) mlir/lib/Bindings/Python/DialectNVGPU.cpp (+2)
  • (modified) mlir/lib/Bindings/Python/DialectPDL.cpp (+5)
  • (modified) mlir/lib/Bindings/Python/DialectQuant.cpp (+7)
  • (modified) mlir/lib/Bindings/Python/DialectSMT.cpp (+3)
  • (modified) mlir/lib/Bindings/Python/DialectTransform.cpp (+5)
  • (modified) mlir/lib/CAPI/Dialect/AMDGPU.cpp (+12)
  • (modified) mlir/lib/CAPI/Dialect/EmitC.cpp (+28)
  • (modified) mlir/lib/CAPI/Dialect/GPU.cpp (+4)
  • (modified) mlir/lib/CAPI/Dialect/LLVM.cpp (+18)
  • (modified) mlir/lib/CAPI/Dialect/NVGPU.cpp (+4)
  • (modified) mlir/lib/CAPI/Dialect/PDL.cpp (+18)
  • (modified) mlir/lib/CAPI/Dialect/Quant.cpp (+20)
  • (modified) mlir/lib/CAPI/Dialect/SMT.cpp (+8)
  • (modified) mlir/lib/CAPI/Dialect/Transform.cpp (+20)
  • (modified) mlir/lib/CAPI/IR/BuiltinTypes.cpp (+84)
  • (modified) mlir/test/python/ir/builtin_types.py (+43)
diff --git a/mlir/include/mlir-c/BuiltinTypes.h b/mlir/include/mlir-c/BuiltinTypes.h
index c981bfd0967c5..f6c30f375cb1a 100644
--- a/mlir/include/mlir-c/BuiltinTypes.h
+++ b/mlir/include/mlir-c/BuiltinTypes.h
@@ -33,6 +33,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAInteger(MlirType type);
 MLIR_CAPI_EXPORTED MlirType mlirIntegerTypeGet(MlirContext ctx,
                                                unsigned bitwidth);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirIntegerTypeGetName(void);
+
 /// Creates a signed integer type of the given bitwidth in the context. The type
 /// is owned by the context.
 MLIR_CAPI_EXPORTED MlirType mlirIntegerTypeSignedGet(MlirContext ctx,
@@ -69,6 +71,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAIndex(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirIndexTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirIndexTypeGetName(void);
+
 //===----------------------------------------------------------------------===//
 // Floating-point types.
 //===----------------------------------------------------------------------===//
@@ -89,6 +93,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat4E2M1FN(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat4E2M1FNTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat4E2M1FNTypeGetName(void);
+
 /// Returns the typeID of an Float6E2M3FN type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E2M3FNTypeGetTypeID(void);
 
@@ -99,6 +105,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat6E2M3FN(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat6E2M3FNTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat6E2M3FNTypeGetName(void);
+
 /// Returns the typeID of an Float6E3M2FN type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E3M2FNTypeGetTypeID(void);
 
@@ -109,6 +117,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat6E3M2FN(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat6E3M2FNTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat6E3M2FNTypeGetName(void);
+
 /// Returns the typeID of an Float8E5M2 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E5M2TypeGetTypeID(void);
 
@@ -119,6 +129,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E5M2(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E5M2TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E5M2TypeGetName(void);
+
 /// Returns the typeID of an Float8E4M3 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E4M3TypeGetTypeID(void);
 
@@ -129,6 +141,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E4M3(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E4M3TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E4M3TypeGetName(void);
+
 /// Returns the typeID of an Float8E4M3FN type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E4M3FNTypeGetTypeID(void);
 
@@ -139,6 +153,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E4M3FN(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E4M3FNTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E4M3FNTypeGetName(void);
+
 /// Returns the typeID of an Float8E5M2FNUZ type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E5M2FNUZTypeGetTypeID(void);
 
@@ -149,6 +165,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E5M2FNUZ(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E5M2FNUZTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E5M2FNUZTypeGetName(void);
+
 /// Returns the typeID of an Float8E4M3FNUZ type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E4M3FNUZTypeGetTypeID(void);
 
@@ -159,6 +177,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E4M3FNUZ(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E4M3FNUZTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E4M3FNUZTypeGetName(void);
+
 /// Returns the typeID of an Float8E4M3B11FNUZ type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E4M3B11FNUZTypeGetTypeID(void);
 
@@ -169,6 +189,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E4M3B11FNUZ(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E4M3B11FNUZTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E4M3B11FNUZTypeGetName(void);
+
 /// Returns the typeID of an Float8E3M4 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E3M4TypeGetTypeID(void);
 
@@ -179,6 +201,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E3M4(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E3M4TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E3M4TypeGetName(void);
+
 /// Returns the typeID of an Float8E8M0FNU type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E8M0FNUTypeGetTypeID(void);
 
@@ -189,6 +213,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E8M0FNU(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirFloat8E8M0FNUTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFloat8E8M0FNUTypeGetName(void);
+
 /// Returns the typeID of an BFloat16 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirBFloat16TypeGetTypeID(void);
 
@@ -199,6 +225,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsABF16(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirBF16TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirBF16TypeGetName(void);
+
 /// Returns the typeID of an Float16 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat16TypeGetTypeID(void);
 
@@ -209,6 +237,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAF16(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirF16TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirF16TypeGetName(void);
+
 /// Returns the typeID of an Float32 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat32TypeGetTypeID(void);
 
@@ -219,6 +249,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAF32(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirF32TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirF32TypeGetName(void);
+
 /// Returns the typeID of an Float64 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloat64TypeGetTypeID(void);
 
@@ -229,6 +261,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAF64(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirF64TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirF64TypeGetName(void);
+
 /// Returns the typeID of a TF32 type.
 MLIR_CAPI_EXPORTED MlirTypeID mlirFloatTF32TypeGetTypeID(void);
 
@@ -239,6 +273,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsATF32(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirTF32TypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirTF32TypeGetName(void);
+
 //===----------------------------------------------------------------------===//
 // None type.
 //===----------------------------------------------------------------------===//
@@ -253,6 +289,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsANone(MlirType type);
 /// context.
 MLIR_CAPI_EXPORTED MlirType mlirNoneTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirNoneTypeGetName(void);
+
 //===----------------------------------------------------------------------===//
 // Complex type.
 //===----------------------------------------------------------------------===//
@@ -267,6 +305,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAComplex(MlirType type);
 /// the element type. The type is owned by the context.
 MLIR_CAPI_EXPORTED MlirType mlirComplexTypeGet(MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirComplexTypeGetName(void);
+
 /// Returns the element type of the given complex type.
 MLIR_CAPI_EXPORTED MlirType mlirComplexTypeGetElementType(MlirType type);
 
@@ -341,6 +381,8 @@ MLIR_CAPI_EXPORTED MlirType mlirVectorTypeGet(intptr_t rank,
                                               const int64_t *shape,
                                               MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirVectorTypeGetName(void);
+
 /// Same as "mlirVectorTypeGet" but returns a nullptr wrapping MlirType on
 /// illegal arguments, emitting appropriate diagnostics.
 MLIR_CAPI_EXPORTED MlirType mlirVectorTypeGetChecked(MlirLocation loc,
@@ -402,6 +444,8 @@ MLIR_CAPI_EXPORTED MlirType mlirRankedTensorTypeGet(intptr_t rank,
                                                     MlirType elementType,
                                                     MlirAttribute encoding);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirRankedTensorTypeGetName(void);
+
 /// Same as "mlirRankedTensorTypeGet" but returns a nullptr wrapping MlirType on
 /// illegal arguments, emitting appropriate diagnostics.
 MLIR_CAPI_EXPORTED MlirType mlirRankedTensorTypeGetChecked(
@@ -416,6 +460,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirRankedTensorTypeGetEncoding(MlirType type);
 /// context as the element type. The type is owned by the context.
 MLIR_CAPI_EXPORTED MlirType mlirUnrankedTensorTypeGet(MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirUnrankedTensorTypeGetName(void);
+
 /// Same as "mlirUnrankedTensorTypeGet" but returns a nullptr wrapping MlirType
 /// on illegal arguments, emitting appropriate diagnostics.
 MLIR_CAPI_EXPORTED MlirType
@@ -446,6 +492,8 @@ MLIR_CAPI_EXPORTED MlirType mlirMemRefTypeGet(MlirType elementType,
                                               MlirAttribute layout,
                                               MlirAttribute memorySpace);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirMemRefTypeGetName(void);
+
 /// Same as "mlirMemRefTypeGet" but returns a nullptr-wrapping MlirType o
 /// illegal arguments, emitting appropriate diagnostics.
 MLIR_CAPI_EXPORTED MlirType mlirMemRefTypeGetChecked(
@@ -471,6 +519,8 @@ MLIR_CAPI_EXPORTED MlirType mlirMemRefTypeContiguousGetChecked(
 MLIR_CAPI_EXPORTED MlirType
 mlirUnrankedMemRefTypeGet(MlirType elementType, MlirAttribute memorySpace);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirUnrankedMemRefTypeGetName(void);
+
 /// Same as "mlirUnrankedMemRefTypeGet" but returns a nullptr wrapping
 /// MlirType on illegal arguments, emitting appropriate diagnostics.
 MLIR_CAPI_EXPORTED MlirType mlirUnrankedMemRefTypeGetChecked(
@@ -511,6 +561,8 @@ MLIR_CAPI_EXPORTED MlirType mlirTupleTypeGet(MlirContext ctx,
                                              intptr_t numElements,
                                              MlirType const *elements);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirTupleTypeGetName(void);
+
 /// Returns the number of types contained in a tuple.
 MLIR_CAPI_EXPORTED intptr_t mlirTupleTypeGetNumTypes(MlirType type);
 
@@ -534,6 +586,8 @@ MLIR_CAPI_EXPORTED MlirType mlirFunctionTypeGet(MlirContext ctx,
                                                 intptr_t numResults,
                                                 MlirType const *results);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirFunctionTypeGetName(void);
+
 /// Returns the number of input types.
 MLIR_CAPI_EXPORTED intptr_t mlirFunctionTypeGetNumInputs(MlirType type);
 
@@ -565,6 +619,8 @@ MLIR_CAPI_EXPORTED MlirType mlirOpaqueTypeGet(MlirContext ctx,
                                               MlirStringRef dialectNamespace,
                                               MlirStringRef typeData);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirOpaqueTypeGetName(void);
+
 /// Returns the namespace of the dialect with which the given opaque type
 /// is associated. The namespace string is owned by the context.
 MLIR_CAPI_EXPORTED MlirStringRef
diff --git a/mlir/include/mlir-c/Dialect/AMDGPU.h b/mlir/include/mlir-c/Dialect/AMDGPU.h
index 83cfe8f5dd65e..7ce6bcc8e6942 100644
--- a/mlir/include/mlir-c/Dialect/AMDGPU.h
+++ b/mlir/include/mlir-c/Dialect/AMDGPU.h
@@ -29,6 +29,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirAMDGPUTDMBaseTypeGetTypeID();
 MLIR_CAPI_EXPORTED MlirType mlirAMDGPUTDMBaseTypeGet(MlirContext ctx,
                                                      MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirAMDGPUTDMBaseTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // TDMDescriptorType
 //===---------------------------------------------------------------------===//
@@ -39,6 +41,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirAMDGPUTDMDescriptorTypeGetTypeID();
 
 MLIR_CAPI_EXPORTED MlirType mlirAMDGPUTDMDescriptorTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirAMDGPUTDMDescriptorTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // TDMGatherBaseType
 //===---------------------------------------------------------------------===//
@@ -51,6 +55,8 @@ MLIR_CAPI_EXPORTED MlirType mlirAMDGPUTDMGatherBaseTypeGet(MlirContext ctx,
                                                            MlirType elementType,
                                                            MlirType indexType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirAMDGPUTDMGatherBaseTypeGetName(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mlir/include/mlir-c/Dialect/EmitC.h b/mlir/include/mlir-c/Dialect/EmitC.h
index a0e3ea08a5a9f..78e09ffe53ff8 100644
--- a/mlir/include/mlir-c/Dialect/EmitC.h
+++ b/mlir/include/mlir-c/Dialect/EmitC.h
@@ -41,6 +41,8 @@ MLIR_CAPI_EXPORTED MlirType mlirEmitCArrayTypeGet(intptr_t nDims,
                                                   int64_t *shape,
                                                   MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCArrayTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // LValueType
 //===---------------------------------------------------------------------===//
@@ -51,6 +53,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCLValueTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirEmitCLValueTypeGet(MlirType valueType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCLValueTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // OpaqueType
 //===---------------------------------------------------------------------===//
@@ -62,6 +66,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCOpaqueTypeGetTypeID(void);
 MLIR_CAPI_EXPORTED MlirType mlirEmitCOpaqueTypeGet(MlirContext ctx,
                                                    MlirStringRef value);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCOpaqueTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // PointerType
 //===---------------------------------------------------------------------===//
@@ -72,6 +78,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCPointerTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirEmitCPointerTypeGet(MlirType pointee);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCPointerTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // PtrDiffTType
 //===---------------------------------------------------------------------===//
@@ -82,6 +90,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCPtrDiffTTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirEmitCPtrDiffTTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCPtrDiffTTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // SignedSizeTType
 //===---------------------------------------------------------------------===//
@@ -92,6 +102,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCSignedSizeTTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirEmitCSignedSizeTTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCSignedSizeTTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // SizeTType
 //===---------------------------------------------------------------------===//
@@ -102,6 +114,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirEmitCSizeTTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirEmitCSizeTTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCSizeTTypeGetName(void);
+
 //===----------------------------------------------------------------------===//
 // CmpPredicate attribute.
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir-c/Dialect/GPU.h b/mlir/include/mlir-c/Dialect/GPU.h
index 321c1122c3370..4e7448d427cda 100644
--- a/mlir/include/mlir-c/Dialect/GPU.h
+++ b/mlir/include/mlir-c/Dialect/GPU.h
@@ -27,6 +27,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAGPUAsyncTokenType(MlirType type);
 
 MLIR_CAPI_EXPORTED MlirType mlirGPUAsyncTokenTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirGPUAsyncTokenTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // ObjectAttr
 //===---------------------------------------------------------------------===//
diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h
index cc7f09f71d028..35f3717ad2372 100644
--- a/mlir/include/mlir-c/Dialect/LLVM.h
+++ b/mlir/include/mlir-c/Dialect/LLVM.h
@@ -23,6 +23,8 @@ MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(LLVM, llvm);
 MLIR_CAPI_EXPORTED MlirType mlirLLVMPointerTypeGet(MlirContext ctx,
                                                    unsigned addressSpace);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMPointerTypeGetName(void);
+
 MLIR_CAPI_EXPORTED MlirTypeID mlirLLVMPointerTypeGetTypeID(void);
 
 /// Returns `true` if the type is an LLVM dialect pointer type.
@@ -35,10 +37,14 @@ mlirLLVMPointerTypeGetAddressSpace(MlirType pointerType);
 /// Creates an llmv.void type.
 MLIR_CAPI_EXPORTED MlirType mlirLLVMVoidTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMVoidTypeGetName(void);
+
 /// Creates an llvm.array type.
 MLIR_CAPI_EXPORTED MlirType mlirLLVMArrayTypeGet(MlirType elementType,
                                                  unsigned numElements);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMArrayTypeGetName(void);
+
 /// Returns the element type of the llvm.array type.
 MLIR_CAPI_EXPORTED MlirType mlirLLVMArrayTypeGetElementType(MlirType type);
 
@@ -47,6 +53,8 @@ MLIR_CAPI_EXPORTED MlirType
 mlirLLVMFunctionTypeGet(MlirType resultType, intptr_t nArgumentTypes,
                         MlirType const *argumentTypes, bool isVarArg);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMFunctionTypeGetName(void);
+
 /// Returns the number of input types.
 MLIR_CAPI_EXPORTED intptr_t mlirLLVMFunctionTypeGetNumInputs(MlirType type);
 
@@ -62,6 +70,8 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type);
 
 MLIR_CAPI_EXPORTED MlirTypeID mlirLLVMStructTypeGetTypeID(void);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetName(void);
+
 /// Returns `true` if the type is a literal (unnamed) LLVM struct type.
 MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsLiteral(MlirType type);
 
diff --git a/mlir/include/mlir-c/Dialect/NVGPU.h b/mlir/include/mlir-c/Dialect/NVGPU.h
index e58015a4a3421..3f1284ed8d68d 100644
--- a/mlir/include/mlir-c/Dialect/NVGPU.h
+++ b/mlir/include/mlir-c/Dialect/NVGPU.h
@@ -29,6 +29,8 @@ MLIR_CAPI_EXPORTED MlirType mlirNVGPUTensorMapDescriptorTypeGet(
     MlirContext ctx, MlirType tensorMemrefType, int swizzle, int l2promo,
     int oobFill, int interleave);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirNVGPUTensorMapDescriptorTypeGetName(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mlir/include/mlir-c/Dialect/PDL.h b/mlir/include/mlir-c/Dialect/PDL.h
index d04f69e391b13..e247c770438c6 100644
--- a/mlir/include/mlir-c/Dialect/PDL.h
+++ b/mlir/include/mlir-c/Dialect/PDL.h
@@ -34,6 +34,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirPDLAttributeTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirPDLAttributeTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirPDLAttributeTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // OperationType
 //===---------------------------------------------------------------------===//
@@ -44,6 +46,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirPDLOperationTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirPDLOperationTypeGet(MlirContext ctx);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirPDLOperationTypeGetName(void);
+
 //===---------------------------------------------------------------------===//
 // RangeType
 //===---------------------------------------------------------------------===//
@@ -54,6 +58,8 @@ MLIR_CAPI_EXPORTED MlirTypeID mlirPDLRangeTypeGetTypeID(void);
 
 MLIR_CAPI_EXPORTED MlirType mlirPDLRangeTypeGet(MlirType elementType);
 
+MLIR_CAPI_EXPORTED MlirStringRef mlirPDLRangeTypeGetName(void);
+
 MLIR_CAPI_EXPORTED ...
[truncated]

@PragmaTwice
Copy link
Copy Markdown
Member Author

I'll merge it soon and start the attribute part.

@PragmaTwice PragmaTwice merged commit b919d62 into llvm:main Jan 7, 2026
15 checks passed
@PragmaTwice PragmaTwice deleted the mlir-python-type-name-v2 branch January 7, 2026 09:17
@llvm-ci
Copy link
Copy Markdown

llvm-ci commented Jan 7, 2026

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building mlir at step 3 "clean-build-dir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/35971

Here is the relevant piece of the build log for the reference
Step 3 (clean-build-dir) failure: Delete failed. (failure) (timed out)
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: IR/properties.mlir (3785 of 3796)
PASS: MLIR-Unit :: IR/./MLIRIRTests/0/131 (3786 of 3796)
PASS: MLIR-Unit :: IR/./MLIRIRTests/100/131 (3787 of 3796)
PASS: MLIR-Unit :: IR/./MLIRIRTests/38/131 (3788 of 3796)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/11/22 (3789 of 3796)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/13/22 (3790 of 3796)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/12/22 (3791 of 3796)
PASS: MLIR-Unit :: Pass/./MLIRPassTests/11/14 (3792 of 3796)
PASS: MLIR-Unit :: IR/./MLIRIRTests/101/131 (3793 of 3796)
PASS: MLIR :: mlir-reduce/dce-test.mlir (3794 of 3796)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1721.452650

@PragmaTwice
Copy link
Copy Markdown
Member Author

PragmaTwice commented Jan 7, 2026

command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1

It seems just a time out?

PragmaTwice added a commit that referenced this pull request Jan 7, 2026
This PR is quite similiar to #174700.

In this PR, I added a C API for each (upstream) MLIR attributes to
retrieve its name (for example, `StringAttr -> mlirStringAttrGetName()
-> "builtin.string"`), and exposed a corresponding type_name class
attribute in the Python bindings (e.g., `StringAttr.attr_name ->
"builtin.string"`). This can be used in various places to avoid
hard-coded strings, such as eliminating the manual string in
`irdl.base("#builtin.string")`.

Note that parts of this PR (mainly mechanical changes) were produced via
GitHub Copilot and GPT-5.2. I have manually reviewed the changes and
verified them with tests to ensure correctness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants