Skip to content

Commit d20a1b8

Browse files
committed
[WebAssembly] Handle DebugLoc in DebugValueManager
According to https://llvm.org/docs/HowToUpdateDebugInfo.html#when-to-preserve-an-instruction-location, when moving (and in our case cloning) within the same BB, the debug location is preserved. But when moving / cloning to a different BB, we preserve the debug location only if the destination BB contains the same location. Currently we preserve the debug loc unconditionally in all cases. This CL correctly handles the debug locs in DebugValueManager. Reviewed By: dschuff Differential Revision: https://reviews.llvm.org/D148115
1 parent 7d7b178 commit d20a1b8

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyDebugValueManager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ bool WebAssemblyDebugValueManager::isInsertSamePlace(
226226
return true;
227227
}
228228

229+
// Returns true if any instruction in MBB has the same debug location as DL.
230+
// Also returns true if DL is an empty location.
231+
static bool hasSameDebugLoc(const MachineBasicBlock *MBB, DebugLoc DL) {
232+
for (const auto &MI : *MBB)
233+
if (MI.getDebugLoc() == DL)
234+
return true;
235+
return false;
236+
}
237+
229238
// Sink 'Def', and also sink its eligible DBG_VALUEs to the place before
230239
// 'Insert'. Convert the original DBG_VALUEs into undefs.
231240
//
@@ -263,6 +272,12 @@ void WebAssemblyDebugValueManager::sink(MachineInstr *Insert) {
263272
getSinkableDebugValues(Insert);
264273

265274
// Sink Def first.
275+
//
276+
// When moving to a different BB, we preserve the debug loc only if the
277+
// destination BB contains the same location. See
278+
// https://llvm.org/docs/HowToUpdateDebugInfo.html#when-to-preserve-an-instruction-location.
279+
if (Def->getParent() != MBB && !hasSameDebugLoc(MBB, Def->getDebugLoc()))
280+
Def->setDebugLoc(DebugLoc());
266281
MBB->splice(Insert, Def->getParent(), Def);
267282

268283
if (DbgValues.empty())
@@ -344,6 +359,11 @@ void WebAssemblyDebugValueManager::cloneSink(MachineInstr *Insert,
344359
// Clone Def first.
345360
if (CloneDef) {
346361
MachineInstr *Clone = MF->CloneMachineInstr(Def);
362+
// When cloning to a different BB, we preserve the debug loc only if the
363+
// destination BB contains the same location. See
364+
// https://llvm.org/docs/HowToUpdateDebugInfo.html#when-to-preserve-an-instruction-location.
365+
if (Def->getParent() != MBB && !hasSameDebugLoc(MBB, Def->getDebugLoc()))
366+
Clone->setDebugLoc(DebugLoc());
347367
if (NewReg != CurrentReg && NewReg.isValid())
348368
Clone->getOperand(0).setReg(NewReg);
349369
MBB->insert(Insert, Clone);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# RUN: llc -run-pass wasm-reg-stackify %s -o - | FileCheck %s
2+
3+
--- |
4+
target triple = "wasm32-unknown-unknown"
5+
6+
declare void @use(i32)
7+
8+
define void @sink_same_bb() {
9+
unreachable
10+
}
11+
define void @clone_same_bb() {
12+
unreachable
13+
}
14+
define void @clone_different_bb_0() {
15+
unreachable
16+
}
17+
define void @clone_different_bb_1() {
18+
unreachable
19+
}
20+
21+
!llvm.dbg.cu = !{!0}
22+
!llvm.module.flags = !{!2, !3, !4}
23+
24+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
25+
!1 = !DIFile(filename: "test.c", directory: "")
26+
!2 = !{i32 7, !"Dwarf Version", i32 5}
27+
!3 = !{i32 2, !"Debug Info Version", i32 3}
28+
!4 = !{i32 1, !"wchar_size", i32 4}
29+
!6 = distinct !DISubprogram(name: "sink_same_bb", scope: !1, file: !1, line: 1, type: !7, scopeLine: 1, unit: !0)
30+
!7 = !DISubroutineType(types: !8)
31+
!8 = !{null}
32+
...
33+
34+
---
35+
# Sinking within the same BB preserves the debug location.
36+
# CHECK-LABEL: name: sink_same_bb
37+
name: sink_same_bb
38+
liveins:
39+
- { reg: '$arguments' }
40+
tracksRegLiveness: true
41+
body: |
42+
bb.0:
43+
liveins: $arguments
44+
%0:i32 = CONST_I32 1, implicit-def $arguments, debug-location !DILocation(line:10, scope:!6)
45+
NOP implicit-def $arguments
46+
CALL @use, %0:i32, implicit-def $arguments
47+
RETURN implicit-def $arguments
48+
49+
; CHECK: %0:i32 = CONST_I32 1, {{.*}}, debug-location !DILocation(line: 10
50+
; CHECK-NEXT: CALL @use
51+
...
52+
53+
---
54+
# Cloning within the same BB preserves the debug location.
55+
# CHECK-LABEL: name: clone_same_bb
56+
name: clone_same_bb
57+
liveins:
58+
- { reg: '$arguments' }
59+
tracksRegLiveness: true
60+
body: |
61+
bb.0:
62+
liveins: $arguments
63+
%0:i32 = CONST_I32 1, implicit-def $arguments, debug-location !DILocation(line:10, scope:!6)
64+
NOP implicit-def $arguments
65+
CALL @use, %0:i32, implicit-def $arguments
66+
CALL @use, %0:i32, implicit-def $arguments
67+
RETURN implicit-def $arguments
68+
69+
; CHECK: CALL @use
70+
; CHECK-NEXT: %1:i32 = CONST_I32 1, {{.*}}, debug-location !DILocation(line: 10
71+
; CHECK-NEXT: CALL @use
72+
...
73+
74+
---
75+
# Cloning to a different BB preserves the debug location in this case because
76+
# the destination BB has an instruction that has the same debug location
77+
# (test.c:10).
78+
# CHECK-LABEL: name: clone_different_bb_0
79+
name: clone_different_bb_0
80+
liveins:
81+
- { reg: '$arguments' }
82+
tracksRegLiveness: true
83+
body: |
84+
bb.0:
85+
successors: %bb.1
86+
liveins: $arguments
87+
%0:i32 = CONST_I32 1, implicit-def $arguments, debug-location !DILocation(line:10, scope:!6)
88+
BR %bb.1, implicit-def $arguments
89+
90+
bb.1:
91+
; predecessors: %bb.0
92+
CALL @use, %0:i32, implicit-def $arguments, debug-location !DILocation(line:10, scope:!6)
93+
RETURN implicit-def $arguments
94+
95+
; CHECK: bb.1:
96+
; CHECK: %1:i32 = CONST_I32 1, {{.*}}, debug-location !DILocation(line: 10
97+
; CHECK-NEXT: CALL @use, %1, {{.*}}, debug-location !DILocation(line: 10
98+
...
99+
100+
---
101+
# Cloning to a different BB does NOT preserve the debug location in this case
102+
# because the destination BB doesn't have an instruction that has the same debug
103+
# location (It has test.c:20 but not test.c:10).
104+
# CHECK-LABEL: name: clone_different_bb_1
105+
name: clone_different_bb_1
106+
liveins:
107+
- { reg: '$arguments' }
108+
tracksRegLiveness: true
109+
body: |
110+
bb.0:
111+
successors: %bb.1
112+
liveins: $arguments
113+
%0:i32 = CONST_I32 1, implicit-def $arguments, debug-location !DILocation(line:10, scope:!6)
114+
BR %bb.1, implicit-def $arguments
115+
116+
bb.1:
117+
; predecessors: %bb.0
118+
CALL @use, %0:i32, implicit-def $arguments, debug-location !DILocation(line:20, scope:!6)
119+
RETURN implicit-def $arguments
120+
121+
; CHECK: bb.1:
122+
; CHECK: %1:i32 = CONST_I32 1
123+
; CHECK-NOT: %1:i32 = CONST_I32 1, {{.*}}, debug-location !DILocation(line: 10
124+
; CHECK-NEXT: CALL @use, %1, {{.*}}, debug-location !DILocation(line: 20
125+
...

0 commit comments

Comments
 (0)