Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 1f1e02f

Browse files
authored
[mlir][debug] Handle DIImportedEntity. (#103055)
The `DIImporedEntity` can be used to represent imported entities like C++'s namespace with using directive or fortran's moudule with use statement. This PR adds `DIImportedEntityAttr` and 2-way translation from `DIImportedEntity` to `DIImportedEntityAttr` and vice versa. When an entity is imported in a function, the `retainedNodes` field of the `DISubprogram` contains all the imported nodes. See the C++ code and the LLVM IR below. ``` void test() { using namespace n1; ... } !2 = !DINamespace(name: "n1", scope: null) !16 = distinct !DISubprogram(name: "test", ..., retainedNodes: !19) !19 = !{!20} !20 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !16, entity: !2 ...) ``` This PR makes sure that the translation from mlir to `retainedNodes` field happens correctly both ways. To side step the cyclic dependency between `DISubprogramAttr` and `DIImportedEntityAttr`, we have decided to not have `scope` field in the `DIImportedEntityAttr` and it is inferred from the entity which hold the list of `DIImportedEntityAttr`. A `retainedNodes` field has been added in the `DISubprogramAttr` which contains the list of `DIImportedEntityAttr` for that function. This PR currently does not handle entities imported in a global scope but that should be easy to handle in a subsequent PR.
1 parent 99c0ab4 commit 1f1e02f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet(
316316
MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
317317
MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
318318
MlirAttribute file, unsigned int line, unsigned int scopeLine,
319-
uint64_t subprogramFlags, MlirAttribute type);
319+
uint64_t subprogramFlags, MlirAttribute type, intptr_t nRetainedNodes,
320+
MlirAttribute const *retainedNodes);
320321

321322
/// Gets the scope from this DISubprogramAttr.
322323
MLIR_CAPI_EXPORTED MlirAttribute
@@ -353,6 +354,12 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIModuleAttrGet(
353354
MlirAttribute name, MlirAttribute configMacros, MlirAttribute includePath,
354355
MlirAttribute apinotes, unsigned int line, bool isDecl);
355356

357+
/// Creates a LLVM DIImportedEntityAttr attribute.
358+
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIImportedEntityAttrGet(
359+
MlirContext ctx, unsigned int tag, MlirAttribute entity, MlirAttribute file,
360+
unsigned int line, MlirAttribute name, intptr_t nElements,
361+
MlirAttribute const *elements);
362+
356363
/// Gets the scope of this DIModuleAttr.
357364
MLIR_CAPI_EXPORTED MlirAttribute
358365
mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule);

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,20 @@ MlirAttribute mlirLLVMDISubprogramAttrGet(
293293
MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
294294
MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
295295
MlirAttribute file, unsigned int line, unsigned int scopeLine,
296-
uint64_t subprogramFlags, MlirAttribute type) {
296+
uint64_t subprogramFlags, MlirAttribute type, intptr_t nRetainedNodes,
297+
MlirAttribute const *retainedNodes) {
298+
SmallVector<Attribute> nodesStorage;
299+
nodesStorage.reserve(nRetainedNodes);
297300
return wrap(DISubprogramAttr::get(
298301
unwrap(ctx), cast<DistinctAttr>(unwrap(id)),
299302
cast<DICompileUnitAttr>(unwrap(compileUnit)),
300303
cast<DIScopeAttr>(unwrap(scope)), cast<StringAttr>(unwrap(name)),
301304
cast<StringAttr>(unwrap(linkageName)), cast<DIFileAttr>(unwrap(file)),
302305
line, scopeLine, DISubprogramFlags(subprogramFlags),
303-
cast<DISubroutineTypeAttr>(unwrap(type))));
306+
cast<DISubroutineTypeAttr>(unwrap(type)),
307+
llvm::map_to_vector(
308+
unwrapList(nRetainedNodes, retainedNodes, nodesStorage),
309+
[](Attribute a) { return cast<DINodeAttr>(a); })));
304310
}
305311

306312
MlirAttribute mlirLLVMDISubprogramAttrGetScope(MlirAttribute diSubprogram) {
@@ -345,3 +351,16 @@ MlirAttribute mlirLLVMDIModuleAttrGet(MlirContext ctx, MlirAttribute file,
345351
MlirAttribute mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule) {
346352
return wrap(cast<DIModuleAttr>(unwrap(diModule)).getScope());
347353
}
354+
355+
MlirAttribute mlirLLVMDIImportedEntityAttrGet(
356+
MlirContext ctx, unsigned int tag, MlirAttribute entity, MlirAttribute file,
357+
unsigned int line, MlirAttribute name, intptr_t nElements,
358+
MlirAttribute const *elements) {
359+
SmallVector<Attribute> elementsStorage;
360+
elementsStorage.reserve(nElements);
361+
return wrap(DIImportedEntityAttr::get(
362+
unwrap(ctx), tag, cast<DINodeAttr>(unwrap(entity)),
363+
cast<DIFileAttr>(unwrap(file)), line, cast<StringAttr>(unwrap(name)),
364+
llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
365+
[](Attribute a) { return cast<DINodeAttr>(a); })));
366+
}

0 commit comments

Comments
 (0)