Skip to content

Commit 2ea60f4

Browse files
authored
[MLIR][LLVM] Fuse Scope into CallsiteLoc Callee (#74546)
There's an issue in the translator today where, for a CallsiteLoc, if the callee does not have a DI scope (perhaps due to compile options or optimizations), it may get propagated the DI scope of its callsite's parent function, which will create a non-existent DILocation combining line & col number from one file, and the filename from another. The root problem is we cannot propagate the parent scope when translating the callee location, as it no longer applies to inlined locations (see code diff and hopefully this will make sense). To facilitate this, the importer is also changed so that callee scopes are fused with the callee FileLineCol loc, instead of on the Callsite loc itself. This comes with the benefit that we now have a symmetric Callsite loc representation. If we required the callee scope be always annotated on the Callsite loc, it would be hard for generic inlining passes to maintain that, since it would have to somehow understand the semantics of the fused metadata and pull it out while inlining.
1 parent 114325b commit 2ea60f4

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,15 @@ Location DebugImporter::translateLoc(llvm::DILocation *loc) {
282282
Location result = FileLineColLoc::get(context, loc->getFilename(),
283283
loc->getLine(), loc->getColumn());
284284

285-
// Add call site information, if available.
286-
if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
287-
result = CallSiteLoc::get(result, translateLoc(inlinedAt));
288-
289285
// Add scope information.
290286
assert(loc->getScope() && "expected non-null scope");
291287
result = FusedLocWith<DIScopeAttr>::get({result}, translate(loc->getScope()),
292288
context);
289+
290+
// Add call site information, if available.
291+
if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
292+
result = CallSiteLoc::get(result, translateLoc(inlinedAt));
293+
293294
return result;
294295
}
295296

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ llvm::DILocation *DebugTranslation::translateLoc(Location loc,
330330
if (auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
331331
// For callsites, the caller is fed as the inlinedAt for the callee.
332332
auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
333-
llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
333+
llvmLoc = translateLoc(callLoc.getCallee(), nullptr, callerLoc);
334+
// Fallback: Ignore callee if it has no debug scope.
335+
if (!llvmLoc)
336+
llvmLoc = callerLoc;
334337

335338
} else if (auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
336339
// A scope of a DILocation cannot be null.

mlir/test/Target/LLVMIR/Import/debug-info.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ define i32 @instruction_loc(i32 %arg1) {
3333
; CHECK-DAG: #[[SP:.+]] = #llvm.di_subprogram<compileUnit = #{{.*}}, scope = #{{.*}}, name = "instruction_loc"
3434
; CHECK-DAG: #[[CALLEE:.+]] = #llvm.di_subprogram<compileUnit = #{{.*}}, scope = #{{.*}}, name = "callee"
3535
; CHECK-DAG: #[[FILE_LOC]] = loc(fused<#[[SP]]>[#[[RAW_FILE_LOC]]])
36-
; CHECK-DAG: #[[CALLEE_LOC:.+]] = loc("debug-info.ll":7:4)
36+
; CHECK-DAG: #[[RAW_CALLEE_LOC:.+]] = loc("debug-info.ll":7:4)
37+
; CHECK-DAG: #[[CALLEE_LOC:.+]] = loc(fused<#[[CALLEE]]>[#[[RAW_CALLEE_LOC]]])
3738
; CHECK-DAG: #[[RAW_CALLER_LOC:.+]] = loc("debug-info.ll":2:2)
3839
; CHECK-DAG: #[[CALLER_LOC:.+]] = loc(fused<#[[SP]]>[#[[RAW_CALLER_LOC]]])
39-
; CHECK-DAG: #[[RAW_CALLSITE_LOC:.+]] = loc(callsite(#[[CALLEE_LOC]] at #[[CALLER_LOC]]))
40-
; CHECK-DAG: #[[CALLSITE_LOC]] = loc(fused<#[[CALLEE]]>[#[[RAW_CALLSITE_LOC]]])
40+
; CHECK-DAG: #[[CALLSITE_LOC:.+]] = loc(callsite(#[[CALLEE_LOC]] at #[[CALLER_LOC]]))
4141

4242
!llvm.dbg.cu = !{!1}
4343
!llvm.module.flags = !{!0}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,23 @@ llvm.func @func_with_debug(%arg: i64) {
100100
// CHECK: call void @llvm.dbg.value(metadata i64 %[[ARG]], metadata ![[NO_NAME_VAR:[0-9]+]], metadata !DIExpression())
101101
llvm.intr.dbg.value #noNameVariable = %arg : i64
102102

103-
// CHECK: call void @func_no_debug(), !dbg ![[CALLSITE_LOC:[0-9]+]]
104-
llvm.call @func_no_debug() : () -> () loc(callsite("mysource.cc":3:4 at "mysource.cc":5:6))
105-
106103
// CHECK: call void @func_no_debug(), !dbg ![[FILE_LOC:[0-9]+]]
107104
llvm.call @func_no_debug() : () -> () loc("foo.mlir":1:2)
108105

109106
// CHECK: call void @func_no_debug(), !dbg ![[NAMED_LOC:[0-9]+]]
110107
llvm.call @func_no_debug() : () -> () loc("named"("foo.mlir":10:10))
111108

109+
// CHECK: call void @func_no_debug(), !dbg ![[CALLSITE_LOC:[0-9]+]]
110+
llvm.call @func_no_debug() : () -> () loc(callsite("nodebug.cc":3:4 at "mysource.cc":5:6))
111+
112+
// CHECK: call void @func_no_debug(), !dbg ![[CALLSITE_LOC:[0-9]+]]
113+
llvm.call @func_no_debug() : () -> () loc(callsite("nodebug.cc":3:4 at fused<#sp0>["mysource.cc":5:6]))
114+
112115
// CHECK: call void @func_no_debug(), !dbg ![[FUSED_LOC:[0-9]+]]
113-
llvm.call @func_no_debug() : () -> () loc(fused[callsite("mysource.cc":5:6 at "mysource.cc":1:1), "mysource.cc":1:1])
116+
llvm.call @func_no_debug() : () -> () loc(fused[callsite(fused<#callee>["mysource.cc":5:6] at "mysource.cc":1:1), "mysource.cc":1:1])
114117

115118
// CHECK: add i64 %[[ARG]], %[[ARG]], !dbg ![[FUSEDWITH_LOC:[0-9]+]]
116-
%sum = llvm.add %arg, %arg : i64 loc(fused<#callee>[callsite("foo.mlir":2:4 at fused<#sp0>["foo.mlir":28:5])])
119+
%sum = llvm.add %arg, %arg : i64 loc(callsite(fused<#callee>["foo.mlir":2:4] at fused<#sp0>["foo.mlir":28:5]))
117120

118121
llvm.return
119122
} loc(fused<#sp0>["foo.mlir":1:1])
@@ -148,7 +151,7 @@ llvm.func @empty_types() {
148151
// CHECK: ![[BLOCK_LOC]] = distinct !DILexicalBlock(scope: ![[FUNC_LOC]])
149152
// CHECK: ![[NO_NAME_VAR]] = !DILocalVariable(scope: ![[BLOCK_LOC]])
150153

151-
// CHECK-DAG: ![[CALLSITE_LOC]] = !DILocation(line: 3, column: 4,
154+
// CHECK-DAG: ![[CALLSITE_LOC]] = !DILocation(line: 5, column: 6,
152155
// CHECK-DAG: ![[FILE_LOC]] = !DILocation(line: 1, column: 2,
153156
// CHECK-DAG: ![[NAMED_LOC]] = !DILocation(line: 10, column: 10
154157
// CHECK-DAG: ![[FUSED_LOC]] = !DILocation(line: 1, column: 1
@@ -186,17 +189,17 @@ llvm.func @empty_types() {
186189
#di_label = #llvm.di_label<scope = #di_lexical_block_file, name = "label", file = #di_file, line = 42>
187190

188191
#loc0 = loc("foo.mlir":0:0)
189-
#loc1 = loc(callsite(#loc0 at fused<#di_subprogram>["foo.mlir":4:2]))
192+
#loc1 = loc(callsite(fused<#di_lexical_block_file>[#loc0] at fused<#di_subprogram>["foo.mlir":4:2]))
190193

191194
// CHECK-LABEL: define i32 @func_with_inlined_dbg_value(
192195
// CHECK-SAME: i32 %[[ARG:.*]]) !dbg ![[OUTER_FUNC:[0-9]+]]
193196
llvm.func @func_with_inlined_dbg_value(%arg0: i32) -> (i32) {
194197
// CHECK: call void @llvm.dbg.value(metadata i32 %[[ARG]], metadata ![[VAR_LOC0:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG_LOC0:.*]]
195198
llvm.intr.dbg.value #di_local_variable0 = %arg0 : i32 loc(fused<#di_subprogram>[#loc0])
196199
// CHECK: call void @llvm.dbg.value(metadata i32 %[[ARG]], metadata ![[VAR_LOC1:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG_LOC1:.*]]
197-
llvm.intr.dbg.value #di_local_variable1 = %arg0 : i32 loc(fused<#di_lexical_block_file>[#loc1])
200+
llvm.intr.dbg.value #di_local_variable1 = %arg0 : i32 loc(#loc1)
198201
// CHECK: call void @llvm.dbg.label(metadata ![[LABEL:[0-9]+]]), !dbg ![[DBG_LOC1:.*]]
199-
llvm.intr.dbg.label #di_label loc(fused<#di_lexical_block_file>[#loc1])
202+
llvm.intr.dbg.label #di_label loc(#loc1)
200203
llvm.return %arg0 : i32
201204
} loc(fused<#di_subprogram>["caller"])
202205

0 commit comments

Comments
 (0)