Skip to content

Commit ef9bcac

Browse files
[MachineSink][AArch64] Preserve debug location when rematerialising an instruction to replace a COPY (#72685)
Fixes a regression in `tools/lldb-dap/optimized/TestDAP_optimized.py` caused by enabling "sink-and-fold" in MachineSink.
1 parent 4ccdab3 commit ef9bcac

File tree

3 files changed

+226
-5
lines changed

3 files changed

+226
-5
lines changed

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
506506
MRI->clearKillFlags(UsedRegB);
507507

508508
for (auto &[SinkDst, MaybeAM] : SinkInto) {
509-
MachineInstr *New = nullptr;
509+
[[maybe_unused]] MachineInstr *New = nullptr;
510510
LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
511511
SinkDst->dump());
512512
if (SinkDst->isCopy()) {
@@ -524,9 +524,7 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
524524
MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
525525
Register DstReg = SinkDst->getOperand(0).getReg();
526526
TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI, *TRI);
527-
// Reuse the source location from the COPY.
528527
New = &*std::prev(InsertPt);
529-
New->setDebugLoc(SinkDst->getDebugLoc());
530528
} else {
531529
// Fold instruction into the addressing mode of a memory instruction.
532530
New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);

llvm/test/CodeGen/AArch64/sink-and-fold-drop-dbg.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ body: |
121121
; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !17, !DIExpression(), debug-location !20
122122
; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !18, !DIExpression(), debug-location !20
123123
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp, debug-location !22
124-
; CHECK-NEXT: $w0 = nsw ADDWri [[COPY]], 1, 0, debug-location !22
124+
; CHECK-NEXT: $w0 = nsw ADDWri [[COPY]], 1, 0, debug-location !21
125125
; CHECK-NEXT: BL @f, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $w0, implicit-def $sp, implicit-def $w0, debug-location !22
126126
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp, debug-location !22
127127
; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !19, !DIExpression(), debug-location !20
128-
; CHECK-NEXT: $w0 = nsw ADDWri [[COPY]], 1, 0, debug-location !23
128+
; CHECK-NEXT: $w0 = nsw ADDWri [[COPY]], 1, 0, debug-location !21
129129
; CHECK-NEXT: TCRETURNdi @f, 0, csr_aarch64_aapcs, implicit $sp, implicit $w0, debug-location !23
130130
DBG_VALUE $w0, $noreg, !16, !DIExpression(), debug-location !20
131131
%0:gpr32common = COPY $w0
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# RUN: llc %s -run-pass=machine-sink --aarch64-enable-sink-fold=true -o - | FileCheck %s
2+
--- |
3+
target triple = "aarch64-linux"
4+
5+
@.str = private unnamed_addr constant [12 x i8] c"result: %d\0A\00", align 1, !dbg !0
6+
7+
define i32 @foo(i32 %x, i32 %y) !dbg !18 {
8+
entry:
9+
%add = add i32 %x, 3, !dbg !25
10+
%add1 = add i32 %add, %y, !dbg !26
11+
ret i32 %add1, !dbg !27
12+
}
13+
14+
define i32 @baz(i32 %y) !dbg !28 {
15+
entry:
16+
%add1.i = add i32 %y, 23, !dbg !34
17+
tail call void @bar(ptr nonnull @.str, i32 %add1.i), !dbg !36
18+
ret i32 0, !dbg !37
19+
}
20+
21+
declare !dbg !38 void @bar(ptr noundef, i32 noundef)
22+
23+
!llvm.dbg.cu = !{!7}
24+
!llvm.module.flags = !{!9, !10, !11, !12, !13, !14, !15, !16}
25+
!llvm.ident = !{!17}
26+
27+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
28+
!1 = distinct !DIGlobalVariable(scope: null, file: !2, line: 9, type: !3, isLocal: true, isDefinition: true)
29+
!2 = !DIFile(filename: "m.c", directory: "/home/chill")
30+
!3 = !DICompositeType(tag: DW_TAG_array_type, baseType: !4, size: 96, elements: !5)
31+
!4 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_unsigned_char)
32+
!5 = !{!6}
33+
!6 = !DISubrange(count: 12)
34+
!7 = distinct !DICompileUnit(language: DW_LANG_C11, file: !2, producer: "clang version 18.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !8, splitDebugInlining: false, nameTableKind: None)
35+
!8 = !{!0}
36+
!9 = !{i32 7, !"Dwarf Version", i32 5}
37+
!10 = !{i32 2, !"Debug Info Version", i32 3}
38+
!11 = !{i32 1, !"wchar_size", i32 4}
39+
!12 = !{i32 8, !"PIC Level", i32 2}
40+
!13 = !{i32 7, !"PIE Level", i32 2}
41+
!14 = !{i32 7, !"uwtable", i32 2}
42+
!15 = !{i32 7, !"frame-pointer", i32 1}
43+
!16 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
44+
!17 = !{!"clang version 18.0.0"}
45+
!18 = distinct !DISubprogram(name: "foo", scope: !2, file: !2, line: 1, type: !19, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !7, retainedNodes: !22)
46+
!19 = !DISubroutineType(types: !20)
47+
!20 = !{!21, !21, !21}
48+
!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
49+
!22 = !{!23, !24}
50+
!23 = !DILocalVariable(name: "x", arg: 1, scope: !18, file: !2, line: 1, type: !21)
51+
!24 = !DILocalVariable(name: "y", arg: 2, scope: !18, file: !2, line: 1, type: !21)
52+
!25 = !DILocation(line: 2, column: 12, scope: !18)
53+
!26 = !DILocation(line: 2, column: 16, scope: !18)
54+
!27 = !DILocation(line: 2, column: 3, scope: !18)
55+
!28 = distinct !DISubprogram(name: "baz", scope: !2, file: !2, line: 7, type: !29, scopeLine: 7, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !7, retainedNodes: !31)
56+
!29 = !DISubroutineType(types: !30)
57+
!30 = !{!21, !21}
58+
!31 = !{!32, !33}
59+
!32 = !DILocalVariable(name: "y", arg: 1, scope: !28, file: !2, line: 7, type: !21)
60+
!33 = !DILocalVariable(name: "result", scope: !28, file: !2, line: 8, type: !21)
61+
!34 = !DILocation(line: 2, column: 16, scope: !18, inlinedAt: !35)
62+
!35 = distinct !DILocation(line: 8, column: 16, scope: !28)
63+
!36 = !DILocation(line: 9, column: 3, scope: !28)
64+
!37 = !DILocation(line: 10, column: 3, scope: !28)
65+
!38 = !DISubprogram(name: "bar", scope: !2, file: !2, line: 5, type: !39, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
66+
!39 = !DISubroutineType(types: !40)
67+
!40 = !{null, !41, !21}
68+
!41 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !42, size: 64)
69+
!42 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !4)
70+
71+
...
72+
---
73+
name: foo
74+
alignment: 4
75+
exposesReturnsTwice: false
76+
legalized: false
77+
regBankSelected: false
78+
selected: false
79+
failedISel: false
80+
tracksRegLiveness: true
81+
hasWinCFI: false
82+
callsEHReturn: false
83+
callsUnwindInit: false
84+
hasEHCatchret: false
85+
hasEHScopes: false
86+
hasEHFunclets: false
87+
isOutlined: false
88+
debugInstrRef: false
89+
failsVerification: false
90+
tracksDebugUserValues: false
91+
registers:
92+
- { id: 0, class: gpr32, preferred-register: '' }
93+
- { id: 1, class: gpr32, preferred-register: '' }
94+
- { id: 2, class: gpr32common, preferred-register: '' }
95+
- { id: 3, class: gpr32sp, preferred-register: '' }
96+
liveins:
97+
- { reg: '$w0', virtual-reg: '%0' }
98+
- { reg: '$w1', virtual-reg: '%1' }
99+
frameInfo:
100+
isFrameAddressTaken: false
101+
isReturnAddressTaken: false
102+
hasStackMap: false
103+
hasPatchPoint: false
104+
stackSize: 0
105+
offsetAdjustment: 0
106+
maxAlignment: 1
107+
adjustsStack: false
108+
hasCalls: false
109+
stackProtector: ''
110+
functionContext: ''
111+
maxCallFrameSize: 0
112+
cvBytesOfCalleeSavedRegisters: 0
113+
hasOpaqueSPAdjustment: false
114+
hasVAStart: false
115+
hasMustTailInVarArgFunc: false
116+
hasTailCall: false
117+
localFrameSize: 0
118+
savePoint: ''
119+
restorePoint: ''
120+
fixedStack: []
121+
stack: []
122+
entry_values: []
123+
callSites: []
124+
debugValueSubstitutions: []
125+
constants: []
126+
machineFunctionInfo: {}
127+
body: |
128+
bb.0.entry:
129+
liveins: $w0, $w1
130+
131+
; CHECK-LABEL: name: foo
132+
133+
; Check the source location of the ADDWri was reused (usually the
134+
; copy to $w0 carries the same debug location as the return)
135+
136+
; CHECK: $w0 = ADDWri {{.*}}, 3, 0, debug-location !26
137+
; CHECK-NEXT: RET_ReallyLR implicit $w0, debug-location !27
138+
%1:gpr32 = COPY $w1
139+
%0:gpr32 = COPY $w0
140+
%2:gpr32common = ADDWrr %0, %1, debug-location !25
141+
$w0 = ADDWri %2, 3, 0, debug-location !26
142+
RET_ReallyLR implicit $w0, debug-location !27
143+
144+
...
145+
---
146+
name: baz
147+
alignment: 4
148+
exposesReturnsTwice: false
149+
legalized: false
150+
regBankSelected: false
151+
selected: false
152+
failedISel: false
153+
tracksRegLiveness: true
154+
hasWinCFI: false
155+
callsEHReturn: false
156+
callsUnwindInit: false
157+
hasEHCatchret: false
158+
hasEHScopes: false
159+
hasEHFunclets: false
160+
isOutlined: false
161+
debugInstrRef: false
162+
failsVerification: false
163+
tracksDebugUserValues: false
164+
registers:
165+
- { id: 0, class: gpr32common, preferred-register: '' }
166+
- { id: 1, class: gpr32sp, preferred-register: '' }
167+
- { id: 2, class: gpr64common, preferred-register: '' }
168+
- { id: 3, class: gpr32all, preferred-register: '' }
169+
liveins:
170+
- { reg: '$w0', virtual-reg: '%0' }
171+
frameInfo:
172+
isFrameAddressTaken: false
173+
isReturnAddressTaken: false
174+
hasStackMap: false
175+
hasPatchPoint: false
176+
stackSize: 0
177+
offsetAdjustment: 0
178+
maxAlignment: 1
179+
adjustsStack: true
180+
hasCalls: true
181+
stackProtector: ''
182+
functionContext: ''
183+
maxCallFrameSize: 0
184+
cvBytesOfCalleeSavedRegisters: 0
185+
hasOpaqueSPAdjustment: false
186+
hasVAStart: false
187+
hasMustTailInVarArgFunc: false
188+
hasTailCall: false
189+
localFrameSize: 0
190+
savePoint: ''
191+
restorePoint: ''
192+
fixedStack: []
193+
stack: []
194+
entry_values: []
195+
callSites: []
196+
debugValueSubstitutions: []
197+
constants: []
198+
machineFunctionInfo: {}
199+
body: |
200+
bb.0.entry:
201+
liveins: $w0
202+
203+
; CHECK-LABEL: name: baz
204+
205+
; Check the source location of the ADDWri was reused (usually the
206+
; copy to $w1 carries the same debug location as the BL)
207+
208+
; CHECK: $x0 = COPY {{.*}}, debug-location !36
209+
; CHECK-NEXT: $w1 = ADDWri {{.*}}, 23, 0, debug-location !34
210+
; CHECK-NEXT: BL @bar, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $x0, implicit $w1, implicit-def $sp, debug-location !36
211+
212+
%0:gpr32common = COPY $w0
213+
ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp, debug-location !36
214+
%2:gpr64common = MOVaddr target-flags(aarch64-page) @.str, target-flags(aarch64-pageoff, aarch64-nc) @.str, debug-location !36
215+
$x0 = COPY %2, debug-location !36
216+
$w1 = ADDWri %0, 23, 0, debug-location !34
217+
BL @bar, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $x0, implicit $w1, implicit-def $sp, debug-location !36
218+
ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp, debug-location !36
219+
%3:gpr32all = COPY $wzr
220+
$w0 = COPY %3, debug-location !37
221+
RET_ReallyLR implicit $w0, debug-location !37
222+
223+
...

0 commit comments

Comments
 (0)