Skip to content

Commit c6ed8b2

Browse files
committed
[MLIR][LLVM] Make DISubprogramAttr cyclic
This commit implements LLVM_DIRecursiveTypeAttrInterface for the DISubprogramAttr to ensure cyclic subprograms can be imported properly. In the process multiple shortcuts around the recently introduced DIImportedEntityAttr can be removed.
1 parent 68d8b38 commit c6ed8b2

File tree

12 files changed

+200
-125
lines changed

12 files changed

+200
-125
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIBasicTypeAttrGet(
234234
MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
235235
MlirLLVMTypeEncoding encoding);
236236

237+
/// Creates a self-referencing LLVM DICompositeType attribute.
238+
MlirAttribute mlirLLVMDICompositeTypeAttrGetSelfRec(MlirAttribute recId);
239+
237240
/// Creates a LLVM DICompositeType attribute.
238241
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet(
239242
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
@@ -311,13 +314,16 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILocalVariableAttrGet(
311314
MlirAttribute diFile, unsigned int line, unsigned int arg,
312315
unsigned int alignInBits, MlirAttribute diType, int64_t flags);
313316

317+
/// Creates a self-referencing LLVM DISubprogramAttr attribute.
318+
MlirAttribute mlirLLVMDISubprogramAttrGetSelfRec(MlirAttribute recId);
319+
314320
/// Creates a LLVM DISubprogramAttr attribute.
315321
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet(
316-
MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
317-
MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
318-
MlirAttribute file, unsigned int line, unsigned int scopeLine,
319-
uint64_t subprogramFlags, MlirAttribute type, intptr_t nRetainedNodes,
320-
MlirAttribute const *retainedNodes);
322+
MlirContext ctx, MlirAttribute id, MlirAttribute recId,
323+
MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name,
324+
MlirAttribute linkageName, MlirAttribute file, unsigned int line,
325+
unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type,
326+
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes);
321327

322328
/// Gets the scope from this DISubprogramAttr.
323329
MLIR_CAPI_EXPORTED MlirAttribute
@@ -356,9 +362,9 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIModuleAttrGet(
356362

357363
/// Creates a LLVM DIImportedEntityAttr attribute.
358364
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);
365+
MlirContext ctx, unsigned int tag, MlirAttribute scope,
366+
MlirAttribute entity, MlirAttribute file, unsigned int line,
367+
MlirAttribute name, intptr_t nElements, MlirAttribute const *elements);
362368

363369
/// Gets the scope of this DIModuleAttr.
364370
MLIR_CAPI_EXPORTED MlirAttribute

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

+23-10
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,16 @@ def LLVM_DILocalVariableAttr : LLVM_Attr<"DILocalVariable", "di_local_variable",
554554
//===----------------------------------------------------------------------===//
555555

556556
def LLVM_DISubprogramAttr : LLVM_Attr<"DISubprogram", "di_subprogram",
557-
/*traits=*/[], "DIScopeAttr"> {
557+
[LLVM_DIRecursiveTypeAttrInterface],
558+
"DIScopeAttr"> {
558559
let parameters = (ins
559560
OptionalParameter<"DistinctAttr">:$id,
561+
OptionalParameter<"DistinctAttr">:$recId,
560562
OptionalParameter<"DICompileUnitAttr">:$compileUnit,
561-
"DIScopeAttr":$scope,
563+
OptionalParameter<"DIScopeAttr">:$scope,
562564
OptionalParameter<"StringAttr">:$name,
563565
OptionalParameter<"StringAttr">:$linkageName,
564-
"DIFileAttr":$file,
566+
OptionalParameter<"DIFileAttr">:$file,
565567
OptionalParameter<"unsigned">:$line,
566568
OptionalParameter<"unsigned">:$scopeLine,
567569
OptionalParameter<"DISubprogramFlags">:$subprogramFlags,
@@ -577,13 +579,28 @@ def LLVM_DISubprogramAttr : LLVM_Attr<"DISubprogram", "di_subprogram",
577579
"ArrayRef<DINodeAttr>":$retainedNodes
578580
), [{
579581
MLIRContext *ctx = file.getContext();
580-
return $_get(ctx, id, compileUnit, scope, StringAttr::get(ctx, name),
582+
return $_get(ctx, id, /*recId=*/nullptr, compileUnit, scope,
583+
StringAttr::get(ctx, name),
581584
StringAttr::get(ctx, linkageName), file, line,
582585
scopeLine, subprogramFlags, type, retainedNodes);
583586
}]>
584587
];
585-
586588
let assemblyFormat = "`<` struct(params) `>`";
589+
let extraClassDeclaration = [{
590+
/// Requirements of DIRecursiveTypeAttrInterface.
591+
/// @{
592+
593+
/// Get whether this attr describes a recursive self reference.
594+
bool isRecSelf() { return !getScope(); }
595+
596+
/// Get a copy of this type attr but with the recursive ID set to `recId`.
597+
DIRecursiveTypeAttrInterface withRecId(DistinctAttr recId);
598+
599+
/// Build a rec-self instance using the provided `recId`.
600+
static DIRecursiveTypeAttrInterface getRecSelf(DistinctAttr recId);
601+
602+
/// @}
603+
}];
587604
}
588605

589606
//===----------------------------------------------------------------------===//
@@ -627,13 +644,9 @@ def LLVM_DINamespaceAttr : LLVM_Attr<"DINamespace", "di_namespace",
627644

628645
def LLVM_DIImportedEntityAttr : LLVM_Attr<"DIImportedEntity", "di_imported_entity",
629646
/*traits=*/[], "DINodeAttr"> {
630-
/// TODO: DIImportedEntity has a 'scope' field which represents the scope where
631-
/// this entity is imported. Currently, we are not adding a 'scope' field in
632-
/// DIImportedEntityAttr to avoid cyclic dependency. As DIImportedEntityAttr
633-
/// entries will be contained inside a scope entity (e.g. DISubprogramAttr),
634-
/// the scope can easily be inferred.
635647
let parameters = (ins
636648
LLVM_DITagParameter:$tag,
649+
"DIScopeAttr":$scope,
637650
"DINodeAttr":$entity,
638651
OptionalParameter<"DIFileAttr">:$file,
639652
OptionalParameter<"unsigned">:$line,

mlir/lib/CAPI/Dialect/LLVM.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ MlirAttribute mlirLLVMDIBasicTypeAttrGet(MlirContext ctx, unsigned int tag,
159159
unwrap(ctx), tag, cast<StringAttr>(unwrap(name)), sizeInBits, encoding));
160160
}
161161

162+
MlirAttribute mlirLLVMDICompositeTypeAttrGetSelfRec(MlirAttribute recId) {
163+
return wrap(
164+
DICompositeTypeAttr::getRecSelf(cast<DistinctAttr>(unwrap(recId))));
165+
}
166+
162167
MlirAttribute mlirLLVMDICompositeTypeAttrGet(
163168
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
164169
MlirAttribute file, uint32_t line, MlirAttribute scope,
@@ -289,16 +294,21 @@ MlirAttribute mlirLLVMDISubroutineTypeAttrGet(MlirContext ctx,
289294
[](Attribute a) { return cast<DITypeAttr>(a); })));
290295
}
291296

297+
MlirAttribute mlirLLVMDISubprogramAttrGetSelfRec(MlirAttribute recId) {
298+
return wrap(DISubprogramAttr::getRecSelf(cast<DistinctAttr>(unwrap(recId))));
299+
}
300+
292301
MlirAttribute mlirLLVMDISubprogramAttrGet(
293-
MlirContext ctx, MlirAttribute id, MlirAttribute compileUnit,
294-
MlirAttribute scope, MlirAttribute name, MlirAttribute linkageName,
295-
MlirAttribute file, unsigned int line, unsigned int scopeLine,
296-
uint64_t subprogramFlags, MlirAttribute type, intptr_t nRetainedNodes,
297-
MlirAttribute const *retainedNodes) {
302+
MlirContext ctx, MlirAttribute id, MlirAttribute recId,
303+
MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name,
304+
MlirAttribute linkageName, MlirAttribute file, unsigned int line,
305+
unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type,
306+
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes) {
298307
SmallVector<Attribute> nodesStorage;
299308
nodesStorage.reserve(nRetainedNodes);
300309
return wrap(DISubprogramAttr::get(
301310
unwrap(ctx), cast<DistinctAttr>(unwrap(id)),
311+
cast<DistinctAttr>(unwrap(recId)),
302312
cast<DICompileUnitAttr>(unwrap(compileUnit)),
303313
cast<DIScopeAttr>(unwrap(scope)), cast<StringAttr>(unwrap(name)),
304314
cast<StringAttr>(unwrap(linkageName)), cast<DIFileAttr>(unwrap(file)),
@@ -353,14 +363,15 @@ MlirAttribute mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule) {
353363
}
354364

355365
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) {
366+
MlirContext ctx, unsigned int tag, MlirAttribute scope,
367+
MlirAttribute entity, MlirAttribute file, unsigned int line,
368+
MlirAttribute name, intptr_t nElements, MlirAttribute const *elements) {
359369
SmallVector<Attribute> elementsStorage;
360370
elementsStorage.reserve(nElements);
361371
return wrap(DIImportedEntityAttr::get(
362-
unwrap(ctx), tag, cast<DINodeAttr>(unwrap(entity)),
363-
cast<DIFileAttr>(unwrap(file)), line, cast<StringAttr>(unwrap(name)),
372+
unwrap(ctx), tag, cast<DIScopeAttr>(unwrap(scope)),
373+
cast<DINodeAttr>(unwrap(entity)), cast<DIFileAttr>(unwrap(file)), line,
374+
cast<StringAttr>(unwrap(name)),
364375
llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
365376
[](Attribute a) { return cast<DINodeAttr>(a); })));
366377
}

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@ DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
215215
{}, DIFlags(), 0, 0, {}, {}, {}, {}, {});
216216
}
217217

218+
//===----------------------------------------------------------------------===//
219+
// DISubprogramAttr
220+
//===----------------------------------------------------------------------===//
221+
222+
DIRecursiveTypeAttrInterface DISubprogramAttr::withRecId(DistinctAttr recId) {
223+
return DISubprogramAttr::get(
224+
getContext(), getId(), recId, getCompileUnit(), getScope(), getName(),
225+
getLinkageName(), getFile(), getLine(), getScopeLine(),
226+
getSubprogramFlags(), getType(), getRetainedNodes());
227+
}
228+
229+
DIRecursiveTypeAttrInterface DISubprogramAttr::getRecSelf(DistinctAttr recId) {
230+
return DISubprogramAttr::get(recId.getContext(), {}, recId, {}, {}, {}, {}, 0,
231+
0, {}, {}, {}, {});
232+
}
233+
218234
//===----------------------------------------------------------------------===//
219235
// TargetFeaturesAttr
220236
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3155,9 +3155,9 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
31553155
.Case<AccessGroupAttr, AliasScopeAttr, AliasScopeDomainAttr,
31563156
DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
31573157
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
3158-
DIGlobalVariableExpressionAttr, DILabelAttr, DILexicalBlockAttr,
3159-
DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
3160-
DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
3158+
DIGlobalVariableExpressionAttr, DIImportedEntityAttr, DILabelAttr,
3159+
DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
3160+
DIModuleAttr, DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
31613161
DISubprogramAttr, DISubroutineTypeAttr, LoopAnnotationAttr,
31623162
LoopVectorizeAttr, LoopInterleaveAttr, LoopUnrollAttr,
31633163
LoopUnrollAndJamAttr, LoopLICMAttr, LoopDistributeAttr,

mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ static void addScopeToFunction(LLVM::LLVMFuncOp llvmFunc,
7676
compileUnitAttr = {};
7777
}
7878
auto subprogramAttr = LLVM::DISubprogramAttr::get(
79-
context, id, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr,
80-
fileAttr,
79+
context, id, /*recId=*/{}, compileUnitAttr, fileAttr, funcNameAttr,
80+
funcNameAttr, fileAttr,
8181
/*line=*/line,
8282
/*scopeline=*/col, subprogramFlags, subroutineTypeAttr,
8383
/*retainedNodes=*/{});

mlir/lib/Target/LLVMIR/DebugImporter.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
217217
}
218218

219219
return DIImportedEntityAttr::get(
220-
context, node->getTag(), translate(node->getEntity()),
221-
translate(node->getFile()), node->getLine(),
220+
context, node->getTag(), translate(node->getScope()),
221+
translate(node->getEntity()), translate(node->getFile()), node->getLine(),
222222
getStringAttrOrNull(node->getRawName()), elements);
223223
}
224224

@@ -227,6 +227,7 @@ DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
227227
mlir::DistinctAttr id;
228228
if (node->isDistinct())
229229
id = getOrCreateDistinctID(node);
230+
230231
// Return nullptr if the scope or type is invalid.
231232
DIScopeAttr scope = translate(node->getScope());
232233
if (node->getScope() && !scope)
@@ -238,16 +239,19 @@ DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
238239
if (node->getType() && !type)
239240
return nullptr;
240241

242+
// Convert the retained nodes but drop all of them if one of them is invalid.
241243
SmallVector<DINodeAttr> retainedNodes;
242244
for (llvm::DINode *retainedNode : node->getRetainedNodes())
243245
retainedNodes.push_back(translate(retainedNode));
246+
if (llvm::is_contained(retainedNodes, nullptr))
247+
retainedNodes.clear();
244248

245-
return DISubprogramAttr::get(context, id, translate(node->getUnit()), scope,
246-
getStringAttrOrNull(node->getRawName()),
247-
getStringAttrOrNull(node->getRawLinkageName()),
248-
translate(node->getFile()), node->getLine(),
249-
node->getScopeLine(), *subprogramFlags, type,
250-
retainedNodes);
249+
return DISubprogramAttr::get(
250+
context, id, /*recId=*/{}, translate(node->getUnit()), scope,
251+
getStringAttrOrNull(node->getRawName()),
252+
getStringAttrOrNull(node->getRawLinkageName()),
253+
translate(node->getFile()), node->getLine(), node->getScopeLine(),
254+
*subprogramFlags, type, retainedNodes);
251255
}
252256

253257
DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
@@ -374,6 +378,9 @@ getRecSelfConstructor(llvm::DINode *node) {
374378
.Case([&](llvm::DICompositeType *) {
375379
return CtorType(DICompositeTypeAttr::getRecSelf);
376380
})
381+
.Case([&](llvm::DISubprogram *) {
382+
return CtorType(DISubprogramAttr::getRecSelf);
383+
})
377384
.Default(CtorType());
378385
}
379386

0 commit comments

Comments
 (0)