Skip to content

Commit e765ea9

Browse files
authored
[mlir][debug] Make DICompileUnitAttr recursive. (llvm#190808)
This PR add `DIRecursiveTypeAttrInterface` to `DICompileUnitAttr`. It should fix the circular dependency problem we have since `importedEntities` field was added.
1 parent c73872a commit e765ea9

File tree

14 files changed

+189
-31
lines changed

14 files changed

+189
-31
lines changed

flang/test/Transforms/debug-line-table-inc-file.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module {
3030
// CHECK: #[[MODULE_LOC]] = loc("{{.*}}simple.f90":0:0)
3131
// CHECK: #[[LOC_INC_FILE:.*]] = loc("{{.*}}inc.f90":1:1)
3232
// CHECK: #[[LOC_FILE:.*]] = loc("{{.*}}simple.f90":3:1)
33-
// CHECK: #[[DI_CU:.*]] = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #[[DI_FILE]], producer = "{{.*}}flang{{.*}}", isOptimized = false, emissionKind = LineTablesOnly>
33+
// CHECK: #[[DI_CU:.*]] = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #[[DI_FILE]], producer = "{{.*}}flang{{.*}}", emissionKind = LineTablesOnly>
3434
// CHECK: #[[DI_SP_INC:.*]] = #llvm.di_subprogram<{{.*}}id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "sinc", linkageName = "_QPsinc", file = #[[DI_INC_FILE]], {{.*}}>
3535
// CHECK: #[[DI_SP:.*]] = #llvm.di_subprogram<{{.*}}id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "_QQmain", linkageName = "_QQmain", file = #[[DI_FILE]], {{.*}}>
3636
// CHECK: #[[FUSED_LOC_INC_FILE]] = loc(fused<#[[DI_SP_INC]]>[#[[LOC_INC_FILE]]])

flang/test/Transforms/debug-line-table.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ module {
2424
// CHECK: #[[MODULE_LOC]] = loc("[[DIR_NAME]]/[[FILE_NAME]]":1:1)
2525
// CHECK: #[[SB_LOC]] = loc("./simple.f90":2:1)
2626
// CHECK: #[[DECL_LOC:.*]] = loc("./simple.f90":10:1)
27-
// FULL: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "{{.*}}flang{{.*}}", isOptimized = false, emissionKind = Full>
27+
// FULL: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "{{.*}}flang{{.*}}", emissionKind = Full>
2828
// OPT: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "{{.*}}flang{{.*}}", isOptimized = true, emissionKind = Full>
29-
// LINETABLE: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "{{.*}}flang{{.*}}", isOptimized = false, emissionKind = LineTablesOnly>
29+
// LINETABLE: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "{{.*}}flang{{.*}}", emissionKind = LineTablesOnly>
3030
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #di_basic_type, #di_basic_type>
3131
// CHECK: #[[SB_SUBPROGRAM:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #di_compile_unit, scope = #di_file, name = "[[SB_NAME]]", linkageName = "[[SB_NAME]]", file = #di_file, line = 1, scopeLine = 1, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
3232
// CHECK: #[[DECL_SUBPROGRAM:.*]] = #llvm.di_subprogram<scope = #di_file, name = "[[DECL_NAME]]", linkageName = "[[DECL_NAME]]", file = #di_file, line = 1, scopeLine = 1, subprogramFlags = Optimized, type = #di_subroutine_type>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,18 @@ enum MlirLLVMDINameTableKind {
362362

363363
typedef enum MlirLLVMDINameTableKind MlirLLVMDINameTableKind;
364364

365+
/// Creates a self-referencing LLVM DICompileUnitAttr attribute.
366+
MLIR_CAPI_EXPORTED MlirAttribute
367+
mlirLLVMDICompileUnitAttrGetRecSelf(MlirAttribute recId);
368+
365369
/// Creates a LLVM DICompileUnit attribute.
366370
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet(
367-
MlirContext ctx, MlirAttribute id, unsigned int sourceLanguage,
368-
MlirAttribute file, MlirAttribute producer, bool isOptimized,
369-
MlirLLVMDIEmissionKind emissionKind, bool isDebugInfoForProfiling,
370-
MlirLLVMDINameTableKind nameTableKind, MlirAttribute splitDebugFilename,
371-
intptr_t nImportedEntities, MlirAttribute const *importedEntities);
371+
MlirContext ctx, MlirAttribute recId, bool isRecSelf, MlirAttribute id,
372+
unsigned int sourceLanguage, MlirAttribute file, MlirAttribute producer,
373+
bool isOptimized, MlirLLVMDIEmissionKind emissionKind,
374+
bool isDebugInfoForProfiling, MlirLLVMDINameTableKind nameTableKind,
375+
MlirAttribute splitDebugFilename, intptr_t nImportedEntities,
376+
MlirAttribute const *importedEntities);
372377

373378
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMDICompileUnitAttrGetName(void);
374379

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def LLVM_DIEncodingParameter : LLVM_DIParameter<
335335
>;
336336

337337
def LLVM_DILanguageParameter : LLVM_DIParameter<
338-
"language", /*default=*/"", "Language", /*errorCase=*/"0"
338+
"language", /*default=*/"0", "Language", /*errorCase=*/"0"
339339
>;
340340

341341
def LLVM_DITagParameter : LLVM_DIParameter<
@@ -417,14 +417,19 @@ def LLVM_DIBasicTypeAttr : LLVM_Attr<"DIBasicType", "di_basic_type",
417417
//===----------------------------------------------------------------------===//
418418

419419
def LLVM_DICompileUnitAttr : LLVM_Attr<"DICompileUnit", "di_compile_unit",
420-
/*traits=*/[], "DIScopeAttr"> {
420+
[LLVM_DIRecursiveTypeAttrInterface],
421+
"DIScopeAttr"> {
421422
let parameters = (ins
422-
"DistinctAttr":$id,
423+
// DIRecursiveTypeAttrInterface specific parameters.
424+
OptionalParameter<"DistinctAttr">:$recId,
425+
OptionalParameter<"bool">:$isRecSelf,
426+
// DICompileUnitAttr specific parameters.
427+
OptionalParameter<"DistinctAttr">:$id,
423428
LLVM_DILanguageParameter:$sourceLanguage,
424-
"DIFileAttr":$file,
429+
OptionalParameter<"DIFileAttr">:$file,
425430
OptionalParameter<"StringAttr">:$producer,
426-
"bool":$isOptimized,
427-
"DIEmissionKind":$emissionKind,
431+
OptionalParameter<"bool">:$isOptimized,
432+
OptionalParameter<"DIEmissionKind">:$emissionKind,
428433
OptionalParameter<"bool">:$isDebugInfoForProfiling,
429434
OptionalParameter<"DINameTableKind">:$nameTableKind,
430435
OptionalParameter<"StringAttr">:$splitDebugFilename,
@@ -440,12 +445,25 @@ def LLVM_DICompileUnitAttr : LLVM_Attr<"DICompileUnit", "di_compile_unit",
440445
CArg<"StringAttr", "{}">:$splitDebugFilename,
441446
CArg<"ArrayRef<DINodeAttr>", "{}">:$importedEntities
442447
), [{
443-
return $_get(id.getContext(), id, sourceLanguage, file, producer,
444-
isOptimized, emissionKind, isDebugInfoForProfiling,
445-
nameTableKind, splitDebugFilename, importedEntities);
448+
return $_get(id.getContext(), /*recId=*/nullptr, /*isRecSelf=*/false, id,
449+
sourceLanguage, file, producer, isOptimized, emissionKind,
450+
isDebugInfoForProfiling, nameTableKind, splitDebugFilename,
451+
importedEntities);
446452
}]>
447453
];
448454
let assemblyFormat = "`<` struct(params) `>`";
455+
let extraClassDeclaration = [{
456+
/// Requirements of DIRecursiveTypeAttrInterface.
457+
/// @{
458+
459+
/// Get a copy of this attr but with the recursive ID set to `recId`.
460+
DIRecursiveTypeAttrInterface withRecId(DistinctAttr recId);
461+
462+
/// Build a rec-self instance using the provided `recId`.
463+
static DIRecursiveTypeAttrInterface getRecSelf(DistinctAttr recId);
464+
465+
/// @}
466+
}];
449467

450468
// Generate mnemonic alias for the attribute.
451469
let genMnemonicAlias = 1;

mlir/include/mlir/Dialect/LLVMIR/LLVMDialectBytecode.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ def DISubroutineTypeAttr : DialectAttribute<(attr
129129
//===----------------------------------------------------------------------===//
130130

131131
def DICompileUnitAttr : DialectAttribute<(attr
132-
Attr<"DistinctAttr">:$id,
132+
OptionalAttribute<"DistinctAttr">:$recId,
133+
Bool:$isRecSelf,
134+
OptionalAttribute<"DistinctAttr">:$id,
133135
VarInt:$sourceLanguage,
134-
Attr<"DIFileAttr">:$file,
136+
OptionalAttribute<"DIFileAttr">:$file,
135137
OptionalAttribute<"StringAttr">:$producer,
136138
Bool:$isOptimized,
137139
EnumClassFlag<"DIEmissionKind", "getEmissionKind()">:$_rawEmissionKind,

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,22 @@ MlirAttribute mlirLLVMDIFileAttrGet(MlirContext ctx, MlirAttribute name,
342342

343343
MlirStringRef mlirLLVMDIFileAttrGetName(void) { return wrap(DIFileAttr::name); }
344344

345+
MlirAttribute mlirLLVMDICompileUnitAttrGetRecSelf(MlirAttribute recId) {
346+
return wrap(DICompileUnitAttr::getRecSelf(cast<DistinctAttr>(unwrap(recId))));
347+
}
348+
345349
MlirAttribute mlirLLVMDICompileUnitAttrGet(
346-
MlirContext ctx, MlirAttribute id, unsigned int sourceLanguage,
347-
MlirAttribute file, MlirAttribute producer, bool isOptimized,
348-
MlirLLVMDIEmissionKind emissionKind, bool isDebugInfoForProfiling,
349-
MlirLLVMDINameTableKind nameTableKind, MlirAttribute splitDebugFilename,
350-
intptr_t nImportedEntities, MlirAttribute const *importedEntities) {
350+
MlirContext ctx, MlirAttribute recId, bool isRecSelf, MlirAttribute id,
351+
unsigned int sourceLanguage, MlirAttribute file, MlirAttribute producer,
352+
bool isOptimized, MlirLLVMDIEmissionKind emissionKind,
353+
bool isDebugInfoForProfiling, MlirLLVMDINameTableKind nameTableKind,
354+
MlirAttribute splitDebugFilename, intptr_t nImportedEntities,
355+
MlirAttribute const *importedEntities) {
351356
SmallVector<Attribute> importsStorage;
352357
importsStorage.reserve(nImportedEntities);
353358
return wrap(DICompileUnitAttr::get(
354-
unwrap(ctx), cast<DistinctAttr>(unwrap(id)), sourceLanguage,
359+
unwrap(ctx), cast<DistinctAttr>(unwrap(recId)), isRecSelf,
360+
cast<DistinctAttr>(unwrap(id)), sourceLanguage,
355361
cast<DIFileAttr>(unwrap(file)), cast<StringAttr>(unwrap(producer)),
356362
isOptimized, DIEmissionKind(emissionKind), isDebugInfoForProfiling,
357363
DINameTableKind(nameTableKind),

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
330330
{}, {}, {});
331331
}
332332

333+
//===----------------------------------------------------------------------===//
334+
// DICompileUnitAttr
335+
//===----------------------------------------------------------------------===//
336+
337+
DIRecursiveTypeAttrInterface DICompileUnitAttr::withRecId(DistinctAttr recId) {
338+
return DICompileUnitAttr::get(
339+
getContext(), recId, getIsRecSelf(), getId(), getSourceLanguage(),
340+
getFile(), getProducer(), getIsOptimized(), getEmissionKind(),
341+
getIsDebugInfoForProfiling(), getNameTableKind(), getSplitDebugFilename(),
342+
getImportedEntities());
343+
}
344+
345+
DIRecursiveTypeAttrInterface DICompileUnitAttr::getRecSelf(DistinctAttr recId) {
346+
347+
return DICompileUnitAttr::get(
348+
recId.getContext(), recId, /*isRecSelf=*/true, /*id=*/{},
349+
/*sourceLanguage=*/0u, /*file=*/{}, /*producer=*/{},
350+
/*isOptimized=*/false, DIEmissionKind::None,
351+
/*isDebugInfoForProfiling=*/false, DINameTableKind::Default,
352+
/*splitDebugFilename=*/{}, /*importedEntities=*/{});
353+
}
354+
333355
//===----------------------------------------------------------------------===//
334356
// DISubprogramAttr
335357
//===----------------------------------------------------------------------===//

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
6565
imports.push_back(nodeAttr);
6666
}
6767
return DICompileUnitAttr::get(
68-
context, getOrCreateDistinctID(node),
68+
context, /*recId=*/DistinctAttr{}, /*isRecSelf=*/false,
69+
getOrCreateDistinctID(node),
6970
node->getSourceLanguage().getUnversionedName(),
7071
translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
7172
node->isOptimized(), emissionKind.value(),
@@ -434,8 +435,9 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
434435
return nullptr;
435436
}
436437

437-
/// Get the `getRecSelf` constructor for the translated type of `node` if its
438-
/// translated DITypeAttr supports recursion. Otherwise, returns nullptr.
438+
/// Get the `getRecSelf` constructor for the translated node if it participates
439+
/// in CyclicReplacerCache cycle breaking (recursive composite types,
440+
/// subprograms, or compile units).
439441
static function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>
440442
getRecSelfConstructor(llvm::DINode *node) {
441443
using CtorType = function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>;
@@ -446,6 +448,9 @@ getRecSelfConstructor(llvm::DINode *node) {
446448
.Case([&](llvm::DISubprogram *) {
447449
return CtorType(DISubprogramAttr::getRecSelf);
448450
})
451+
.Case([&](llvm::DICompileUnit *) {
452+
return CtorType(DICompileUnitAttr::getRecSelf);
453+
})
449454
.Default(CtorType());
450455
}
451456

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,32 @@ llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
119119
/*AlignInBits=*/0, attr.getEncoding(), llvm::DINode::FlagZero);
120120
}
121121

122+
llvm::TempDICompileUnit
123+
DebugTranslation::translateTemporaryImpl(DICompileUnitAttr attr) {
124+
return llvm::DICompileUnit::getTemporary(
125+
llvmCtx,
126+
static_cast<llvm::DISourceLanguageName>(attr.getSourceLanguage()),
127+
/*File=*/nullptr, "", attr.getIsOptimized(),
128+
/*Flags=*/"", /*RuntimeVersion=*/0,
129+
/*splitDebugFileName=*/"",
130+
static_cast<llvm::DICompileUnit::DebugEmissionKind>(
131+
attr.getEmissionKind()),
132+
/*EnumTypes=*/nullptr, /*RetainedTypes=*/nullptr,
133+
/*GlobalVariables=*/nullptr, /*ImportedEntities=*/nullptr,
134+
/*Macros=*/nullptr,
135+
/*DWOId=*/0, /*SplitDebugInlining=*/true,
136+
attr.getIsDebugInfoForProfiling(),
137+
static_cast<llvm::DICompileUnit::DebugNameTableKind>(
138+
attr.getNameTableKind()),
139+
/*RangesBaseAddress=*/false, /*SysRoot=*/"", /*SDK=*/"");
140+
}
141+
122142
llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
143+
if (attr.getId())
144+
if (auto iter = distinctAttrToNode.find(attr.getId());
145+
iter != distinctAttrToNode.end())
146+
return cast<llvm::DICompileUnit>(iter->second);
147+
123148
llvm::DIBuilder builder(llvmModule);
124149
llvm::DICompileUnit *cu = builder.createCompileUnit(
125150
attr.getSourceLanguage(), translate(attr.getFile()),
@@ -140,6 +165,9 @@ llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
140165
if (!importNodes.empty())
141166
cu->replaceImportedEntities(llvm::MDTuple::get(llvmCtx, importNodes));
142167

168+
if (attr.getId())
169+
distinctAttrToNode.try_emplace(attr.getId(), cu);
170+
143171
return cu;
144172
}
145173

@@ -310,6 +338,13 @@ DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
310338
auto *concrete = translateImpl(attr);
311339
temporary->replaceAllUsesWith(concrete);
312340
return concrete;
341+
})
342+
.Case([&](DICompileUnitAttr attr) {
343+
auto temporary = translateTemporaryImpl(attr);
344+
setRecursivePlaceholder(temporary.get());
345+
auto *concrete = translateImpl(attr);
346+
temporary->replaceAllUsesWith(concrete);
347+
return concrete;
313348
});
314349

315350
assert(recursiveNodeMap.back().first == recursiveId &&

mlir/lib/Target/LLVMIR/DebugTranslation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class DebugTranslation {
104104
/// corresponding type.
105105
llvm::TempDICompositeType translateTemporaryImpl(DICompositeTypeAttr attr);
106106
llvm::TempDISubprogram translateTemporaryImpl(DISubprogramAttr attr);
107+
llvm::TempDICompileUnit translateTemporaryImpl(DICompileUnitAttr attr);
107108

108109
/// Constructs a string metadata node from the string attribute. Returns
109110
/// nullptr if `stringAttr` is null or contains and empty string.

0 commit comments

Comments
 (0)