Skip to content

Commit cd8094e

Browse files
jpienaarsuperbobryJokeren
authored
[mlir] Translate nested debug information (#140915)
This backports changes from Triton with the exception that for fused locations, use the first one with file info rather than just first. --------- Co-authored-by: Sergei Lebedev <slebedev@google.com> Co-authored-by: Keren Zhou <kerenzhou@openai.com>
1 parent e98b095 commit cd8094e

File tree

2 files changed

+132
-27
lines changed

2 files changed

+132
-27
lines changed

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

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ static FileLineColLoc extractFileLoc(Location loc) {
3131
return extractFileLoc(nameLoc.getChildLoc());
3232
if (auto opaqueLoc = dyn_cast<OpaqueLoc>(loc))
3333
return extractFileLoc(opaqueLoc.getFallbackLocation());
34+
if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
35+
for (auto loc : fusedLoc.getLocations()) {
36+
if (auto fileLoc = extractFileLoc(loc))
37+
return fileLoc;
38+
}
39+
}
40+
if (auto callerLoc = dyn_cast<CallSiteLoc>(loc))
41+
return extractFileLoc(callerLoc.getCaller());
3442
return FileLineColLoc();
3543
}
3644

@@ -41,47 +49,84 @@ static void addScopeToFunction(LLVM::LLVMFuncOp llvmFunc,
4149
LLVM::DICompileUnitAttr compileUnitAttr) {
4250

4351
Location loc = llvmFunc.getLoc();
44-
if (loc->findInstanceOf<mlir::FusedLocWith<LLVM::DISubprogramAttr>>())
52+
if (loc->findInstanceOf<FusedLocWith<LLVM::DISubprogramAttr>>())
4553
return;
4654

4755
MLIRContext *context = llvmFunc->getContext();
4856

49-
// Filename, line and colmun to associate to the function.
57+
// Filename and line associate to the function.
5058
LLVM::DIFileAttr fileAttr;
51-
int64_t line = 1, col = 1;
52-
FileLineColLoc fileLoc = extractFileLoc(loc);
53-
if (!fileLoc && compileUnitAttr) {
54-
fileAttr = compileUnitAttr.getFile();
55-
} else if (!fileLoc) {
56-
fileAttr = LLVM::DIFileAttr::get(context, "<unknown>", "");
57-
} else {
59+
int64_t line = 1;
60+
if (FileLineColLoc fileLoc = extractFileLoc(loc)) {
5861
line = fileLoc.getLine();
59-
col = fileLoc.getColumn();
6062
StringRef inputFilePath = fileLoc.getFilename().getValue();
6163
fileAttr =
6264
LLVM::DIFileAttr::get(context, llvm::sys::path::filename(inputFilePath),
6365
llvm::sys::path::parent_path(inputFilePath));
66+
} else {
67+
fileAttr = compileUnitAttr
68+
? compileUnitAttr.getFile()
69+
: LLVM::DIFileAttr::get(context, "<unknown>", "");
6470
}
6571
auto subroutineTypeAttr =
6672
LLVM::DISubroutineTypeAttr::get(context, llvm::dwarf::DW_CC_normal, {});
6773

68-
// Only definitions need a distinct identifier and a compilation unit.
74+
// Figure out debug information (`subprogramFlags` and `compileUnitAttr`) to
75+
// attach to the function definition / declaration. External functions are
76+
// declarations only and are defined in a different compile unit, so mark
77+
// them appropriately in `subprogramFlags` and set an empty `compileUnitAttr`.
6978
DistinctAttr id;
7079
auto subprogramFlags = LLVM::DISubprogramFlags::Optimized;
7180
if (!llvmFunc.isExternal()) {
72-
id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
81+
id = DistinctAttr::create(UnitAttr::get(context));
7382
subprogramFlags = subprogramFlags | LLVM::DISubprogramFlags::Definition;
7483
} else {
7584
compileUnitAttr = {};
7685
}
77-
auto funcName = StringAttr::get(context, llvmFunc.getName());
86+
auto funcNameAttr = llvmFunc.getNameAttr();
7887
auto subprogramAttr = LLVM::DISubprogramAttr::get(
79-
context, id, compileUnitAttr, fileAttr, funcName, funcName, fileAttr,
80-
/*line=*/line, /*scopeline=*/col, subprogramFlags, subroutineTypeAttr,
88+
context, id, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr,
89+
fileAttr,
90+
/*line=*/line, /*scopeLine=*/line, subprogramFlags, subroutineTypeAttr,
8191
/*retainedNodes=*/{}, /*annotations=*/{});
8292
llvmFunc->setLoc(FusedLoc::get(context, {loc}, subprogramAttr));
8393
}
8494

95+
// Get a nested loc for inlined functions.
96+
static Location getNestedLoc(Operation *op, LLVM::DIScopeAttr scopeAttr,
97+
Location calleeLoc) {
98+
auto calleeFileName = extractFileLoc(calleeLoc).getFilename();
99+
auto *context = op->getContext();
100+
LLVM::DIFileAttr calleeFileAttr =
101+
LLVM::DIFileAttr::get(context, llvm::sys::path::filename(calleeFileName),
102+
llvm::sys::path::parent_path(calleeFileName));
103+
auto lexicalBlockFileAttr = LLVM::DILexicalBlockFileAttr::get(
104+
context, scopeAttr, calleeFileAttr, /*discriminator=*/0);
105+
Location loc = calleeLoc;
106+
// Recurse if the callee location is again a call site.
107+
if (auto callSiteLoc = dyn_cast<CallSiteLoc>(calleeLoc)) {
108+
auto nestedLoc = callSiteLoc.getCallee();
109+
loc = getNestedLoc(op, lexicalBlockFileAttr, nestedLoc);
110+
}
111+
return FusedLoc::get(context, {loc}, lexicalBlockFileAttr);
112+
}
113+
114+
static void setLexicalBlockFileAttr(Operation *op) {
115+
if (auto callSiteLoc = dyn_cast<CallSiteLoc>(op->getLoc())) {
116+
auto callerLoc = callSiteLoc.getCaller();
117+
auto calleeLoc = callSiteLoc.getCallee();
118+
LLVM::DIScopeAttr scopeAttr;
119+
// We assemble the full inline stack so the parent of this loc must be a
120+
// function
121+
auto funcOp = op->getParentOfType<LLVM::LLVMFuncOp>();
122+
if (auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc())) {
123+
scopeAttr = cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
124+
op->setLoc(
125+
CallSiteLoc::get(getNestedLoc(op, scopeAttr, calleeLoc), callerLoc));
126+
}
127+
}
128+
}
129+
85130
namespace {
86131
/// Add a debug info scope to LLVMFuncOp that are missing it.
87132
struct DIScopeForLLVMFuncOpPass
@@ -99,15 +144,12 @@ struct DIScopeForLLVMFuncOpPass
99144
return signalPassFailure();
100145
}
101146

102-
// To find a DICompileUnitAttr attached to a parent (the module for
103-
// example), otherwise create a default one.
104-
// Find a DICompileUnitAttr attached to the module, otherwise create a
105-
// default one.
147+
// Find a DICompileUnitAttr attached to a parent (the module for example),
148+
// otherwise create a default one.
106149
LLVM::DICompileUnitAttr compileUnitAttr;
107-
auto fusedCompileUnitAttr =
108-
module->getLoc()
109-
->findInstanceOf<mlir::FusedLocWith<LLVM::DICompileUnitAttr>>();
110-
if (fusedCompileUnitAttr) {
150+
if (auto fusedCompileUnitAttr =
151+
module->getLoc()
152+
->findInstanceOf<FusedLocWith<LLVM::DICompileUnitAttr>>()) {
111153
compileUnitAttr = fusedCompileUnitAttr.getMetadata();
112154
} else {
113155
LLVM::DIFileAttr fileAttr;
@@ -126,9 +168,14 @@ struct DIScopeForLLVMFuncOpPass
126168
/*isOptimized=*/true, emissionKind);
127169
}
128170

129-
// Create subprograms for each function with the same distinct compile unit.
130-
module.walk([&](LLVM::LLVMFuncOp func) {
131-
addScopeToFunction(func, compileUnitAttr);
171+
module.walk<WalkOrder::PreOrder>([&](Operation *op) -> void {
172+
if (auto funcOp = dyn_cast<LLVM::LLVMFuncOp>(op)) {
173+
// Create subprograms for each function with the same distinct compile
174+
// unit.
175+
addScopeToFunction(funcOp, compileUnitAttr);
176+
} else {
177+
setLexicalBlockFileAttr(op);
178+
}
132179
});
133180
}
134181
};

mlir/test/Dialect/LLVMIR/add-debuginfo-func-scope.mlir

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module {
5757
// CHECK-DAG: #[[DI_FILE_FUNC:.+]] = #llvm.di_file<"file.mlir" in "">
5858
// CHECK-DAG: #loc[[FUNCFILELOC:[0-9]+]] = loc("file.mlir":9:8)
5959
// CHECK-DAG: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_C, file = #[[DI_FILE_MODULE]], producer = "MLIR", isOptimized = true, emissionKind = LineTablesOnly>
60-
// CHECK-DAG: #di_subprogram = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #di_compile_unit, scope = #[[DI_FILE_FUNC]], name = "propagate_compile_unit", linkageName = "propagate_compile_unit", file = #[[DI_FILE_FUNC]], line = 9, scopeLine = 8, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
60+
// CHECK-DAG: #di_subprogram = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #di_compile_unit, scope = #[[DI_FILE_FUNC]], name = "propagate_compile_unit", linkageName = "propagate_compile_unit", file = #[[DI_FILE_FUNC]], line = 9, scopeLine = 9, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
6161
// CHECK-DAG: #loc[[MODULELOC]] = loc(fused<#di_compile_unit>[#loc])
6262
// CHECK-DAG: #loc[[FUNCLOC]] = loc(fused<#di_subprogram>[#loc[[FUNCFILELOC]]
6363
module {
@@ -83,3 +83,61 @@ module @multiple_funcs {
8383
llvm.return loc(unknown)
8484
} loc(unknown)
8585
} loc(unknown)
86+
87+
// -----
88+
89+
// CHECK-LABEL: llvm.func @func_inlined()
90+
// CHECK: #di_file = #llvm.di_file<"base.py" in "testing">
91+
// CHECK: #di_file1 = #llvm.di_file<"gpu_test.py" in "">
92+
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
93+
// CHECK: #di_compile_unit = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #di_file, producer = "MLIR", isOptimized = true, emissionKind = LineTablesOnly>
94+
// CHECK: #loc3 = loc(callsite(#loc1 at #loc2))
95+
// CHECK: #di_subprogram = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #di_compile_unit, scope = #di_file1, name = "func_inlined", linkageName = "func_inlined", file = #di_file1, line = 1150, scopeLine = 1150, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
96+
// CHECK: #di_lexical_block_file = #llvm.di_lexical_block_file<scope = #di_subprogram, file = #di_file, discriminator = 0>
97+
98+
99+
#loc = loc("gpu_test.py":1150:34 to :43)
100+
#loc1 = loc("testing/parameterized.py":321:17 to :53)
101+
#loc2 = loc("testing/base.py":2904:19 to :56)
102+
#loc_1 = loc(callsite(#loc at #loc1))
103+
#loc21 = loc(callsite(#loc2 at #loc_1))
104+
105+
module {
106+
llvm.func @func_inlined() {
107+
llvm.return loc(#loc21)
108+
} loc(#loc)
109+
} loc(#loc2)
110+
111+
// -----
112+
113+
// CHECK-LABEL: llvm.func @func_name_with_child()
114+
// CHECK: #di_file = #llvm.di_file<"file" in "/tmp">
115+
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
116+
// CHECK: #di_subprogram = #llvm.di_subprogram<scope = #di_file, name = "func_name_with_child", linkageName = "func_name_with_child", file = #di_file, line = 100, scopeLine = 100, subprogramFlags = Optimized, type = #di_subroutine_type>
117+
118+
module {
119+
llvm.func @func_name_with_child() loc("foo"("/tmp/file":100))
120+
} loc(unknown)
121+
122+
// -----
123+
124+
// CHECK-LABEL: llvm.func @func_fusion()
125+
// CHECK: #di_file = #llvm.di_file<"file" in "/tmp">
126+
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
127+
// CHECK: #di_subprogram = #llvm.di_subprogram<scope = #di_file, name = "func_fusion", linkageName = "func_fusion", file = #di_file, line = 20, scopeLine = 20, subprogramFlags = Optimized, type = #di_subroutine_type>
128+
129+
module {
130+
llvm.func @func_fusion() loc(fused<"myPass">["foo", "/tmp/file":20])
131+
} loc(unknown)
132+
133+
// -----
134+
135+
// CHECK-LABEL: llvm.func @func_callsiteloc()
136+
// CHECK: #di_file = #llvm.di_file<"mysource.cc" in "">
137+
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
138+
// CHECK: #di_subprogram = #llvm.di_subprogram<scope = #di_file, name = "func_callsiteloc", linkageName = "func_callsiteloc", file = #di_file, line = 10, scopeLine = 10, subprogramFlags = Optimized, type = #di_subroutine_type>
139+
140+
module {
141+
llvm.func @func_callsiteloc() loc(callsite("foo" at "mysource.cc":10:8))
142+
} loc(unknown)
143+

0 commit comments

Comments
 (0)