Skip to content

Commit ae4bbd0

Browse files
authored
[MLIR][Python] Forward the name of MLIR attrs to Python side (llvm#174756)
This PR is quite similiar to llvm#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.
1 parent 88b77d5 commit ae4bbd0

17 files changed

Lines changed: 276 additions & 0 deletions

File tree

mlir/include/mlir-c/BuiltinAttributes.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAAffineMap(MlirAttribute attr);
4343
/// belongs to the same context as the affine map.
4444
MLIR_CAPI_EXPORTED MlirAttribute mlirAffineMapAttrGet(MlirAffineMap map);
4545

46+
MLIR_CAPI_EXPORTED MlirStringRef mlirAffineMapAttrGetName(void);
47+
4648
/// Returns the affine map wrapped in the given affine map attribute.
4749
MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapAttrGetValue(MlirAttribute attr);
4850

@@ -61,6 +63,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAArray(MlirAttribute attr);
6163
MLIR_CAPI_EXPORTED MlirAttribute mlirArrayAttrGet(
6264
MlirContext ctx, intptr_t numElements, MlirAttribute const *elements);
6365

66+
MLIR_CAPI_EXPORTED MlirStringRef mlirArrayAttrGetName(void);
67+
6468
/// Returns the number of elements stored in the given array attribute.
6569
MLIR_CAPI_EXPORTED intptr_t mlirArrayAttrGetNumElements(MlirAttribute attr);
6670

@@ -83,6 +87,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsADictionary(MlirAttribute attr);
8387
MLIR_CAPI_EXPORTED MlirAttribute mlirDictionaryAttrGet(
8488
MlirContext ctx, intptr_t numElements, MlirNamedAttribute const *elements);
8589

90+
MLIR_CAPI_EXPORTED MlirStringRef mlirDictionaryAttrGetName(void);
91+
8692
/// Returns the number of attributes contained in a dictionary attribute.
8793
MLIR_CAPI_EXPORTED intptr_t
8894
mlirDictionaryAttrGetNumElements(MlirAttribute attr);
@@ -143,6 +149,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAInteger(MlirAttribute attr);
143149
MLIR_CAPI_EXPORTED MlirAttribute mlirIntegerAttrGet(MlirType type,
144150
int64_t value);
145151

152+
MLIR_CAPI_EXPORTED MlirStringRef mlirIntegerAttrGetName(void);
153+
146154
/// Returns the value stored in the given integer attribute, assuming the value
147155
/// is of signless type and fits into a signed 64-bit integer.
148156
MLIR_CAPI_EXPORTED int64_t mlirIntegerAttrGetValueInt(MlirAttribute attr);
@@ -182,6 +190,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAIntegerSet(MlirAttribute attr);
182190
/// belongs to the same context as the integer set.
183191
MLIR_CAPI_EXPORTED MlirAttribute mlirIntegerSetAttrGet(MlirIntegerSet set);
184192

193+
MLIR_CAPI_EXPORTED MlirStringRef mlirIntegerSetAttrGetName(void);
194+
185195
/// Returns the integer set wrapped in the given integer set attribute.
186196
MLIR_CAPI_EXPORTED MlirIntegerSet
187197
mlirIntegerSetAttrGetValue(MlirAttribute attr);
@@ -203,6 +213,8 @@ MLIR_CAPI_EXPORTED MlirAttribute
203213
mlirOpaqueAttrGet(MlirContext ctx, MlirStringRef dialectNamespace,
204214
intptr_t dataLength, const char *data, MlirType type);
205215

216+
MLIR_CAPI_EXPORTED MlirStringRef mlirOpaqueAttrGetName(void);
217+
206218
/// Returns the namespace of the dialect with which the given opaque attribute
207219
/// is associated. The namespace string is owned by the context.
208220
MLIR_CAPI_EXPORTED MlirStringRef
@@ -227,6 +239,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAString(MlirAttribute attr);
227239
MLIR_CAPI_EXPORTED MlirAttribute mlirStringAttrGet(MlirContext ctx,
228240
MlirStringRef str);
229241

242+
MLIR_CAPI_EXPORTED MlirStringRef mlirStringAttrGetName(void);
243+
230244
/// Creates a string attribute in the given context containing the given string.
231245
/// Additionally, the attribute has the given type.
232246
MLIR_CAPI_EXPORTED MlirAttribute mlirStringAttrTypedGet(MlirType type,
@@ -253,6 +267,8 @@ MLIR_CAPI_EXPORTED MlirAttribute
253267
mlirSymbolRefAttrGet(MlirContext ctx, MlirStringRef symbol,
254268
intptr_t numReferences, MlirAttribute const *references);
255269

270+
MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolRefAttrGetName(void);
271+
256272
/// Returns the string reference to the root referenced symbol. The data remains
257273
/// live as long as the context in which the attribute lives.
258274
MLIR_CAPI_EXPORTED MlirStringRef
@@ -291,6 +307,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAFlatSymbolRef(MlirAttribute attr);
291307
MLIR_CAPI_EXPORTED MlirAttribute mlirFlatSymbolRefAttrGet(MlirContext ctx,
292308
MlirStringRef symbol);
293309

310+
MLIR_CAPI_EXPORTED MlirStringRef mlirFlatSymbolRefAttrGetName(void);
311+
294312
/// Returns the referenced symbol as a string reference. The data remains live
295313
/// as long as the context in which the attribute lives.
296314
MLIR_CAPI_EXPORTED MlirStringRef
@@ -307,6 +325,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAType(MlirAttribute attr);
307325
/// type.
308326
MLIR_CAPI_EXPORTED MlirAttribute mlirTypeAttrGet(MlirType type);
309327

328+
MLIR_CAPI_EXPORTED MlirStringRef mlirTypeAttrGetName(void);
329+
310330
/// Returns the type stored in the given type attribute.
311331
MLIR_CAPI_EXPORTED MlirType mlirTypeAttrGetValue(MlirAttribute attr);
312332

@@ -323,6 +343,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAUnit(MlirAttribute attr);
323343
/// Creates a unit attribute in the given context.
324344
MLIR_CAPI_EXPORTED MlirAttribute mlirUnitAttrGet(MlirContext ctx);
325345

346+
MLIR_CAPI_EXPORTED MlirStringRef mlirUnitAttrGetName(void);
347+
326348
/// Returns the typeID of a Unit attribute.
327349
MLIR_CAPI_EXPORTED MlirTypeID mlirUnitAttrGetTypeID(void);
328350

@@ -590,6 +612,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseResourceElementsAttrGet(
590612
size_t align),
591613
void *userData);
592614

615+
MLIR_CAPI_EXPORTED MlirStringRef mlirDenseResourceElementsAttrGetName(void);
616+
593617
MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseBoolResourceElementsAttrGet(
594618
MlirType shapedType, MlirStringRef name, intptr_t numElements,
595619
const int *elements);
@@ -697,6 +721,8 @@ MLIR_CAPI_EXPORTED MlirAttribute
697721
mlirStridedLayoutAttrGet(MlirContext ctx, int64_t offset, intptr_t numStrides,
698722
const int64_t *strides);
699723

724+
MLIR_CAPI_EXPORTED MlirStringRef mlirStridedLayoutAttrGetName(void);
725+
700726
// Returns the offset in the given strided layout layout attribute.
701727
MLIR_CAPI_EXPORTED int64_t mlirStridedLayoutAttrGetOffset(MlirAttribute attr);
702728

mlir/include/mlir-c/Dialect/EmitC.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAEmitCCmpPredicate(MlirAttribute attr);
125125
MLIR_CAPI_EXPORTED MlirAttribute
126126
mlirEmitCCmpPredicateAttrGet(MlirContext ctx, enum MlirEmitCCmpPredicate val);
127127

128+
MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCCmpPredicateAttrGetName(void);
129+
128130
MLIR_CAPI_EXPORTED enum MlirEmitCCmpPredicate
129131
mlirEmitCCmpPredicateAttrGetValue(MlirAttribute attr);
130132

@@ -139,6 +141,8 @@ MLIR_CAPI_EXPORTED bool mlirAttributeIsAEmitCOpaque(MlirAttribute attr);
139141
MLIR_CAPI_EXPORTED MlirAttribute mlirEmitCOpaqueAttrGet(MlirContext ctx,
140142
MlirStringRef value);
141143

144+
MLIR_CAPI_EXPORTED MlirStringRef mlirEmitCOpaqueAttrGetName(void);
145+
142146
MLIR_CAPI_EXPORTED MlirStringRef
143147
mlirEmitCOpaqueAttrGetValue(MlirAttribute attr);
144148

mlir/include/mlir-c/Dialect/GPU.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ MLIR_CAPI_EXPORTED MlirAttribute
3939
mlirGPUObjectAttrGet(MlirContext mlirCtx, MlirAttribute target, uint32_t format,
4040
MlirStringRef objectStrRef, MlirAttribute mlirObjectProps);
4141

42+
MLIR_CAPI_EXPORTED MlirStringRef mlirGPUObjectAttrGetName(void);
43+
4244
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetWithKernels(
4345
MlirContext mlirCtx, MlirAttribute target, uint32_t format,
4446
MlirStringRef objectStrRef, MlirAttribute mlirObjectProps,

mlir/include/mlir-c/Dialect/IRDL.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ MLIR_CAPI_EXPORTED MlirLogicalResult mlirLoadIRDLDialects(MlirModule module);
2929
MLIR_CAPI_EXPORTED MlirAttribute
3030
mlirIRDLVariadicityAttrGet(MlirContext ctx, MlirStringRef value);
3131

32+
MLIR_CAPI_EXPORTED MlirStringRef mlirIRDLVariadicityAttrGetName(void);
33+
3234
//===----------------------------------------------------------------------===//
3335
// VariadicityArrayAttr
3436
//===----------------------------------------------------------------------===//
3537

3638
MLIR_CAPI_EXPORTED MlirAttribute mlirIRDLVariadicityArrayAttrGet(
3739
MlirContext ctx, intptr_t nValues, MlirAttribute const *values);
3840

41+
MLIR_CAPI_EXPORTED MlirStringRef mlirIRDLVariadicityArrayAttrGetName(void);
42+
3943
#ifdef __cplusplus
4044
}
4145
#endif

mlir/include/mlir-c/Dialect/LLVM.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ typedef enum MlirLLVMCConv MlirLLVMCConv;
188188
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMCConvAttrGet(MlirContext ctx,
189189
MlirLLVMCConv cconv);
190190

191+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMCConvAttrGetName(void);
192+
191193
enum MlirLLVMComdat {
192194
MlirLLVMComdatAny = 0,
193195
MlirLLVMComdatExactMatch = 1,
@@ -201,6 +203,8 @@ typedef enum MlirLLVMComdat MlirLLVMComdat;
201203
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMComdatAttrGet(MlirContext ctx,
202204
MlirLLVMComdat comdat);
203205

206+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMComdatAttrGetName(void);
207+
204208
enum MlirLLVMLinkage {
205209
MlirLLVMLinkageExternal = 0,
206210
MlirLLVMLinkageAvailableExternally = 1,
@@ -220,18 +224,26 @@ typedef enum MlirLLVMLinkage MlirLLVMLinkage;
220224
MLIR_CAPI_EXPORTED MlirAttribute
221225
mlirLLVMLinkageAttrGet(MlirContext ctx, MlirLLVMLinkage linkage);
222226

227+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMLinkageAttrGetName(void);
228+
223229
/// Creates a LLVM DINullType attribute.
224230
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDINullTypeAttrGet(MlirContext ctx);
225231

232+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDINullTypeAttrGetName(void);
233+
226234
/// Creates a LLVM DIExpressionElem attribute.
227235
MLIR_CAPI_EXPORTED MlirAttribute
228236
mlirLLVMDIExpressionElemAttrGet(MlirContext ctx, unsigned int opcode,
229237
intptr_t nArguments, uint64_t const *arguments);
230238

239+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIExpressionElemAttrGetName(void);
240+
231241
/// Creates a LLVM DIExpression attribute.
232242
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIExpressionAttrGet(
233243
MlirContext ctx, intptr_t nOperations, MlirAttribute const *operations);
234244

245+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIExpressionAttrGetName(void);
246+
235247
enum MlirLLVMTypeEncoding {
236248
MlirLLVMTypeEncodingAddress = 0x1,
237249
MlirLLVMTypeEncodingBoolean = 0x2,
@@ -261,6 +273,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIBasicTypeAttrGet(
261273
MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
262274
MlirLLVMTypeEncoding encoding);
263275

276+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIBasicTypeAttrGetName(void);
277+
264278
/// Creates a self-referencing LLVM DICompositeType attribute.
265279
MLIR_CAPI_EXPORTED MlirAttribute
266280
mlirLLVMDICompositeTypeAttrGetRecSelf(MlirAttribute recId);
@@ -274,6 +288,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet(
274288
MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
275289
MlirAttribute associated);
276290

291+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDICompositeTypeAttrGetName(void);
292+
277293
/// Creates a LLVM DIDerivedType attribute. Note that `dwarfAddressSpace` is an
278294
/// optional field, where `MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL` indicates null
279295
/// and non-negative values indicate a value present.
@@ -282,12 +298,16 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
282298
MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits,
283299
uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData);
284300

301+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIDerivedTypeAttrGetName(void);
302+
285303
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIStringTypeAttrGet(
286304
MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
287305
uint32_t alignInBits, MlirAttribute stringLength,
288306
MlirAttribute stringLengthExp, MlirAttribute stringLocationExp,
289307
MlirLLVMTypeEncoding encoding);
290308

309+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIStringTypeAttrGetName(void);
310+
291311
/// Constant to represent std::nullopt for dwarfAddressSpace to omit the field.
292312
#define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL -1
293313

@@ -300,6 +320,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFileAttrGet(MlirContext ctx,
300320
MlirAttribute name,
301321
MlirAttribute directory);
302322

323+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIFileAttrGetName(void);
324+
303325
enum MlirLLVMDIEmissionKind {
304326
MlirLLVMDIEmissionKindNone = 0,
305327
MlirLLVMDIEmissionKindFull = 1,
@@ -323,26 +345,36 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet(
323345
MlirLLVMDIEmissionKind emissionKind, MlirLLVMDINameTableKind nameTableKind,
324346
MlirAttribute splitDebugFilename);
325347

348+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDICompileUnitAttrGetName(void);
349+
326350
/// Creates a LLVM DIFlags attribute.
327351
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx,
328352
uint64_t value);
329353

354+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIFlagsAttrGetName(void);
355+
330356
/// Creates a LLVM DILexicalBlock attribute.
331357
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILexicalBlockAttrGet(
332358
MlirContext ctx, MlirAttribute scope, MlirAttribute file, unsigned int line,
333359
unsigned int column);
334360

361+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDILexicalBlockAttrGetName(void);
362+
335363
/// Creates a LLVM DILexicalBlockFile attribute.
336364
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILexicalBlockFileAttrGet(
337365
MlirContext ctx, MlirAttribute scope, MlirAttribute file,
338366
unsigned int discriminator);
339367

368+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDILexicalBlockFileAttrGetName(void);
369+
340370
/// Creates a LLVM DILocalVariableAttr attribute.
341371
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILocalVariableAttrGet(
342372
MlirContext ctx, MlirAttribute scope, MlirAttribute name,
343373
MlirAttribute diFile, unsigned int line, unsigned int arg,
344374
unsigned int alignInBits, MlirAttribute diType, int64_t flags);
345375

376+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDILocalVariableAttrGetName(void);
377+
346378
/// Creates a self-referencing LLVM DISubprogramAttr attribute.
347379
MLIR_CAPI_EXPORTED MlirAttribute
348380
mlirLLVMDISubprogramAttrGetRecSelf(MlirAttribute recId);
@@ -356,10 +388,14 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet(
356388
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes,
357389
intptr_t nAnnotations, MlirAttribute const *annotations);
358390

391+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDISubprogramAttrGetName(void);
392+
359393
/// Creates a LLVM DIAnnotation attribute.
360394
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIAnnotationAttrGet(
361395
MlirContext ctx, MlirAttribute name, MlirAttribute value);
362396

397+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIAnnotationAttrGetName(void);
398+
363399
/// Gets the scope from this DISubprogramAttr.
364400
MLIR_CAPI_EXPORTED MlirAttribute
365401
mlirLLVMDISubprogramAttrGetScope(MlirAttribute diSubprogram);
@@ -389,18 +425,24 @@ MLIR_CAPI_EXPORTED MlirAttribute
389425
mlirLLVMDISubroutineTypeAttrGet(MlirContext ctx, unsigned int callingConvention,
390426
intptr_t nTypes, MlirAttribute const *types);
391427

428+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDISubroutineTypeAttrGetName(void);
429+
392430
/// Creates a LLVM DIModuleAttr attribute.
393431
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIModuleAttrGet(
394432
MlirContext ctx, MlirAttribute file, MlirAttribute scope,
395433
MlirAttribute name, MlirAttribute configMacros, MlirAttribute includePath,
396434
MlirAttribute apinotes, unsigned int line, bool isDecl);
397435

436+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIModuleAttrGetName(void);
437+
398438
/// Creates a LLVM DIImportedEntityAttr attribute.
399439
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIImportedEntityAttrGet(
400440
MlirContext ctx, unsigned int tag, MlirAttribute scope,
401441
MlirAttribute entity, MlirAttribute file, unsigned int line,
402442
MlirAttribute name, intptr_t nElements, MlirAttribute const *elements);
403443

444+
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDIImportedEntityAttrGetName(void);
445+
404446
/// Gets the scope of this DIModuleAttr.
405447
MLIR_CAPI_EXPORTED MlirAttribute
406448
mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule);

mlir/include/mlir-c/Dialect/SparseTensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet(
5757
MlirAffineMap lvlTodim, int posWidth, int crdWidth,
5858
MlirAttribute explicitVal, MlirAttribute implicitVal);
5959

60+
MLIR_CAPI_EXPORTED MlirStringRef mlirSparseTensorEncodingAttrGetName(void);
61+
6062
/// Returns the level-rank of the `sparse_tensor.encoding` attribute.
6163
MLIR_CAPI_EXPORTED intptr_t
6264
mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr);

0 commit comments

Comments
 (0)