Skip to content

Commit fa5255e

Browse files
authored
[MLIR][LLVM] Enable export of DISubprograms on function declarations (#78026)
This commit changes the MLIR to LLVMIR export to also attach subprogram debug attachements to function declarations. This commit additonally fixes the two passes that produce subprograms to not attach the "Definition" flag to function declarations. This otherwise results in invalid LLVM IR.
1 parent 148e55c commit fa5255e

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,18 @@ void AddDebugFoundationPass::runOnOperation() {
9797
// Only definitions need a distinct identifier and a compilation unit.
9898
mlir::DistinctAttr id;
9999
mlir::LLVM::DICompileUnitAttr compilationUnit;
100+
auto subprogramFlags = mlir::LLVM::DISubprogramFlags::Optimized;
100101
if (!funcOp.isExternal()) {
101102
id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
102103
compilationUnit = cuAttr;
104+
subprogramFlags =
105+
subprogramFlags | mlir::LLVM::DISubprogramFlags::Definition;
103106
}
104-
mlir::LLVM::DISubprogramAttr spAttr = mlir::LLVM::DISubprogramAttr::get(
107+
auto spAttr = mlir::LLVM::DISubprogramAttr::get(
105108
context, id, compilationUnit, fileAttr, funcName, funcName,
106109
funcFileAttr,
107110
/*line=*/1,
108-
/*scopeline=*/1, mlir::LLVM::DISubprogramFlags::Definition,
109-
subTypeAttr);
111+
/*scopeline=*/1, subprogramFlags, subTypeAttr);
110112
funcOp->setLoc(builder.getFusedLoc({funcOp->getLoc()}, spAttr));
111113
});
112114
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module attributes { fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.dat
2424
// CHECK: #[[DECL_LOC:.*]] = loc("./simple.f90":10:1)
2525
// CHECK: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "Flang", isOptimized = false, emissionKind = LineTablesOnly>
2626
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #di_basic_type, #di_basic_type>
27-
// 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, type = #di_subroutine_type>
28-
// CHECK: #[[DECL_SUBPROGRAM:.*]] = #llvm.di_subprogram<scope = #di_file, name = "[[DECL_NAME]]", linkageName = "[[DECL_NAME]]", file = #di_file, line = 1, scopeLine = 1, subprogramFlags = Definition, type = #di_subroutine_type>
27+
// 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>
28+
// 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>
2929
// CHECK: #[[FUSED_SB_LOC]] = loc(fused<#[[SB_SUBPROGRAM]]>[#[[SB_LOC]]])
3030
// CHECK: #[[FUSED_DECL_LOC]] = loc(fused<#[[DECL_SUBPROGRAM]]>[#[[DECL_LOC]]])

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,19 @@ static void addScopeToFunction(LLVM::LLVMFuncOp llvmFunc,
6767

6868
StringAttr funcNameAttr = llvmFunc.getNameAttr();
6969
// Only definitions need a distinct identifier and a compilation unit.
70-
mlir::DistinctAttr id;
71-
if (!llvmFunc.isExternal())
70+
DistinctAttr id;
71+
auto subprogramFlags = LLVM::DISubprogramFlags::Optimized;
72+
if (!llvmFunc.isExternal()) {
7273
id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
73-
else
74+
subprogramFlags = subprogramFlags | LLVM::DISubprogramFlags::Definition;
75+
} else {
7476
compileUnitAttr = {};
75-
mlir::LLVM::DISubprogramAttr subprogramAttr = LLVM::DISubprogramAttr::get(
77+
}
78+
auto subprogramAttr = LLVM::DISubprogramAttr::get(
7679
context, id, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr,
7780
fileAttr,
7881
/*line=*/line,
79-
/*scopeline=*/col,
80-
LLVM::DISubprogramFlags::Definition | LLVM::DISubprogramFlags::Optimized,
81-
subroutineTypeAttr);
82+
/*scopeline=*/col, subprogramFlags, subroutineTypeAttr);
8283
llvmFunc->setLoc(FusedLoc::get(context, {loc}, subprogramAttr));
8384
}
8485

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,6 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
10741074
branchMapping.clear();
10751075
llvm::Function *llvmFunc = lookupFunction(func.getName());
10761076

1077-
// Translate the debug information for this function.
1078-
debugTranslation->translate(func, *llvmFunc);
1079-
10801077
// Add function arguments to the value remapping table.
10811078
for (auto [mlirArg, llvmArg] :
10821079
llvm::zip(func.getArguments(), llvmFunc->args()))
@@ -1261,6 +1258,9 @@ LogicalResult ModuleTranslation::convertFunctionSignatures() {
12611258

12621259
if (auto alignment = function.getAlignment())
12631260
llvmFunc->setAlignment(llvm::MaybeAlign(*alignment));
1261+
1262+
// Translate the debug information for this function.
1263+
debugTranslation->translate(function, *llvmFunc);
12641264
}
12651265

12661266
return success();

mlir/test/Target/LLVMIR/llvmir-debug.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ llvm.func @empty_types() {
169169

170170
// -----
171171

172+
#di_file = #llvm.di_file<"foo.mlir" in "/test/">
173+
#di_subprogram = #llvm.di_subprogram<
174+
scope = #di_file, name = "func_decl_with_subprogram", file = #di_file, subprogramFlags = "Optimized"
175+
>
176+
177+
// CHECK-LABEL: declare !dbg
178+
// CHECK-SAME: ![[SUBPROGRAM:.*]] i32 @func_decl_with_subprogram(
179+
llvm.func @func_decl_with_subprogram() -> (i32) loc(fused<#di_subprogram>["foo.mlir":2:1])
180+
181+
// CHECK: ![[SUBPROGRAM]] = !DISubprogram(name: "func_decl_with_subprogram", scope: ![[FILE:.*]], file: ![[FILE]], spFlags: DISPFlagOptimized)
182+
// CHECK: ![[FILE]] = !DIFile(filename: "foo.mlir", directory: "/test/")
183+
184+
// -----
185+
172186
#di_basic_type = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "int", sizeInBits = 32, encoding = DW_ATE_signed>
173187
#di_file = #llvm.di_file<"foo.mlir" in "/test/">
174188
#di_compile_unit = #llvm.di_compile_unit<

0 commit comments

Comments
 (0)