Skip to content

Commit 3c27ab1

Browse files
[InstrRef] Skip clobbered EntryValue register recovery
This changes the final stage of InstrRef, i.e. the TransferTracker (which combines the values locations with the variable values), so that it treats a DEBUG_VALUE of an EntryValue just like a DEBUG_VALUE of a constant: a location that is never clobbered and can be propagated to subsequent BBs as long as no other DEBUG_VALUE intrinsics updated the variable. We add two tests here: 1. `entry_value_clobbered_stack_copy` that saves a register on the stack, uses this register as an entry value DBG_VALUE location, and then clobbers it. Prior to this patch, this test would crash because we would try to describe a new location for the variable in terms of what was saved on the stack, and use an invalid expression to do so. This is not needed as an EntryValue can never be clobbered. 2. `entry_value_gets_propagated`, that tests that an EntryValue DBG_VALUE is propagated in a diamond-shaped CFG. This patch is trying to reland #77938 but also fixes the bug with InstrRef based LiveDebugValues, where entry values were not being propagated in a diamond-shaped CFG.
1 parent bdcfcf6 commit 3c27ab1

File tree

3 files changed

+171
-5
lines changed

3 files changed

+171
-5
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ class TransferTracker {
359359
SmallVector<ResolvedDbgOp> ResolvedDbgOps;
360360
bool IsValueValid = true;
361361
unsigned LastUseBeforeDef = 0;
362+
bool DbgLocAvailableAndIsEntryVal = false;
362363

363364
// If every value used by the incoming DbgValue is available at block
364365
// entry, ResolvedDbgOps will contain the machine locations/constants for
@@ -412,6 +413,8 @@ class TransferTracker {
412413
// live range.
413414
LocIdx M = ValuesPreferredLoc->second.getLoc();
414415
ResolvedDbgOps.push_back(M);
416+
if (Value.Properties.DIExpr->isEntryValue())
417+
DbgLocAvailableAndIsEntryVal = true;
415418
}
416419

417420
// If we cannot produce a valid value for the LiveIn value within this
@@ -428,12 +431,14 @@ class TransferTracker {
428431
// The LiveIn value is available at block entry, begin tracking and record
429432
// the transfer.
430433
for (const ResolvedDbgOp &Op : ResolvedDbgOps)
431-
if (!Op.IsConst)
434+
if (!Op.IsConst && !DbgLocAvailableAndIsEntryVal)
432435
ActiveMLocs[Op.Loc].insert(VarID);
433436
auto NewValue = ResolvedDbgValue{ResolvedDbgOps, Value.Properties};
434-
auto Result = ActiveVLocs.insert(std::make_pair(VarID, NewValue));
435-
if (!Result.second)
436-
Result.first->second = NewValue;
437+
if (!DbgLocAvailableAndIsEntryVal) {
438+
auto Result = ActiveVLocs.insert(std::make_pair(VarID, NewValue));
439+
if (!Result.second)
440+
Result.first->second = NewValue;
441+
}
437442
auto &[Var, DILoc] = DVMap.lookupDVID(VarID);
438443
PendingDbgValues.push_back(
439444
std::make_pair(VarID, &*MTracker->emitLoc(ResolvedDbgOps, Var, DILoc,
@@ -668,6 +673,16 @@ class TransferTracker {
668673

669674
auto &[Var, DILoc] = DVMap.lookupDVID(VarID);
670675

676+
// If the expression is a DW_OP_entry_value, emit the variable location
677+
// as-is.
678+
if (DIExpr->isEntryValue()) {
679+
Register Reg = MTracker->LocIdxToLocID[Num.getLoc()];
680+
MachineOperand MO = MachineOperand::CreateReg(Reg, false);
681+
PendingDbgValues.push_back(std::make_pair(
682+
VarID, &*emitMOLoc(MO, Var, {DIExpr, Prop.Indirect, false})));
683+
return true;
684+
}
685+
671686
// Is the variable appropriate for entry values (i.e., is a parameter).
672687
if (!isEntryValueVariable(Var, DIExpr))
673688
return false;
@@ -694,7 +709,7 @@ class TransferTracker {
694709
DebugVariableID VarID = DVMap.getDVID(Var);
695710

696711
// Ignore non-register locations, we don't transfer those.
697-
if (MI.isUndefDebugValue() ||
712+
if (MI.isUndefDebugValue() || MI.getDebugExpression()->isEntryValue() ||
698713
all_of(MI.debug_operands(),
699714
[](const MachineOperand &MO) { return !MO.isReg(); })) {
700715
auto It = ActiveVLocs.find(VarID);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# RUN: llc --run-pass=livedebugvalues -o - %s | FileCheck %s
2+
# REQUIRES: x86-registered-target
3+
4+
# This test covers the scenario where a DBG_VALUE created prior to LiveDebugValues has an entry-value expression.
5+
# It tests that an EntryValue DBG_VALUE is propagated in a diamond-shaped CFG.
6+
7+
--- |
8+
target triple = "x86_64-"
9+
define void @foo(ptr swiftasync %0) !dbg !4 {
10+
call void @llvm.dbg.value(metadata ptr %0, metadata !9, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !17
11+
ret void
12+
}
13+
declare void @llvm.dbg.value(metadata, metadata, metadata)
14+
15+
!llvm.module.flags = !{!0}
16+
!llvm.dbg.cu = !{!1}
17+
18+
!0 = !{i32 2, !"Debug Info Version", i32 3}
19+
!1 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !2, producer: "blah", isOptimized: true, flags: "blah", runtimeVersion: 5, emissionKind: FullDebug)
20+
!2 = !DIFile(filename: "blah", directory: "blah")
21+
!3 = !{}
22+
!4 = distinct !DISubprogram(name: "blah", linkageName: "blah", scope: !2, file: !2, line: 284, type: !7, unit: !1)
23+
!7 = !DISubroutineType(types: !3)
24+
!9 = !DILocalVariable(name: "self", arg: 3, scope: !4, file: !2, line: 328, type: !12, flags: DIFlagArtificial)
25+
!12 = !DICompositeType(tag: DW_TAG_structure_type, name: "blah", scope: !2, file: !2, size: 64, elements: !3)
26+
!17 = !DILocation(line: 328, column: 17, scope: !4)
27+
28+
...
29+
---
30+
name: foo
31+
alignment: 16
32+
debugInstrRef: true
33+
tracksDebugUserValues: true
34+
liveins:
35+
- { reg: '$r14', virtual-reg: '' }
36+
stack:
37+
- { id: 0, name: '', type: spill-slot, offset: -64, size: 8, alignment: 8,
38+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
39+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
40+
body: |
41+
bb.0:
42+
liveins: $r14
43+
; Put a copy of r14 on the stack.
44+
MOV64mr $rbp, 1, $noreg, -48, $noreg, $r14 :: (store (s64) into %stack.0)
45+
DBG_VALUE $r14, $noreg, !9, !DIExpression(DW_OP_LLVM_entry_value, 1), debug-location !17
46+
MOV64mi32 $noreg, 1, $noreg, 0, $noreg, 0, debug-location !17 :: (store (s64) into `ptr null`)
47+
$r14 = MOV64rr killed $r13
48+
; Clobber $r14
49+
RETI64 24
50+
# CHECK: bb.0:
51+
# CHECK: MOV64mr $rbp, 1, $noreg, -48, $noreg, $r14
52+
# CHECK-NEXT: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)
53+
# CHECK-NOT: DBG_VALUE
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# RUN: llc --run-pass=livedebugvalues -o - %s | FileCheck %s --implicit-check-not=DBG_VALUE
2+
# REQUIRES: x86-registered-target
3+
4+
# This test covers the scenario where a DBG_VALUE created prior to LiveDebugValues has an entry-value expression.
5+
# It ensures that a clobbered stack copy doesn't crash if used as an entry-value because entry-values can't be clobbered.
6+
7+
--- |
8+
target triple = "x86_64-"
9+
10+
define i32 @baz(i32 swiftasync %arg1, i32 noundef %arg2, i1 %cond) !dbg !9 {
11+
tail call void @llvm.dbg.value(metadata i32 %arg1, metadata !17, metadata !DIExpression(DW_OP_LLVM_entry_value, 1)), !dbg !19
12+
br i1 %cond, label %if.then, label %if.else, !dbg !22
13+
if.then:
14+
%call = call i32 @foo(i32 noundef %arg1), !dbg !23
15+
br label %if.end, !dbg !25
16+
if.else:
17+
%call1 = call i32 @foo(i32 noundef %arg2), !dbg !26
18+
br label %if.end
19+
if.end:
20+
%temp.0 = phi i32 [ %call, %if.then ], [ %call1, %if.else ], !dbg !28
21+
ret i32 %temp.0, !dbg !29
22+
}
23+
24+
declare i32 @foo(i32)
25+
declare void @llvm.dbg.value(metadata, metadata, metadata)
26+
27+
!llvm.dbg.cu = !{!0}
28+
!llvm.module.flags = !{!2, !3}
29+
30+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "ha", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
31+
!1 = !DIFile(filename: "test.c", directory: "hah")
32+
!2 = !{i32 7, !"Dwarf Version", i32 4}
33+
!3 = !{i32 2, !"Debug Info Version", i32 3}
34+
!9 = distinct !DISubprogram(name: "baz", scope: !1, file: !1, line: 3, type: !10, scopeLine: 3, unit: !0, retainedNodes: !13)
35+
!10 = !DISubroutineType(types: !11)
36+
!11 = !{!12, !12, !12, !12}
37+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
38+
!13 = !{!14, !15, !16, !17}
39+
!14 = !DILocalVariable(name: "arg1", arg: 1, scope: !9, file: !1, line: 3, type: !12)
40+
!15 = !DILocalVariable(name: "arg2", arg: 2, scope: !9, file: !1, line: 3, type: !12)
41+
!16 = !DILocalVariable(name: "cond", arg: 3, scope: !9, file: !1, line: 3, type: !12)
42+
!17 = !DILocalVariable(name: "local", scope: !9, file: !1, line: 4, type: !12)
43+
!19 = !DILocation(line: 0, scope: !9)
44+
!20 = !DILocation(line: 7, column: 7, scope: !21)
45+
!21 = distinct !DILexicalBlock(scope: !9, file: !1, line: 7, column: 7)
46+
!22 = !DILocation(line: 7, column: 7, scope: !9)
47+
!23 = !DILocation(line: 8, column: 12, scope: !24)
48+
!24 = distinct !DILexicalBlock(scope: !21, file: !1, line: 7, column: 13)
49+
!25 = !DILocation(line: 9, column: 3, scope: !24)
50+
!26 = !DILocation(line: 10, column: 12, scope: !27)
51+
!27 = distinct !DILexicalBlock(scope: !21, file: !1, line: 9, column: 10)
52+
!28 = !DILocation(line: 0, scope: !21)
53+
!29 = !DILocation(line: 13, column: 3, scope: !9)
54+
55+
...
56+
---
57+
name: baz
58+
alignment: 16
59+
debugInstrRef: true
60+
tracksDebugUserValues: true
61+
liveins:
62+
- { reg: '$r14', virtual-reg: '' }
63+
- { reg: '$edi', virtual-reg: '' }
64+
- { reg: '$esi', virtual-reg: '' }
65+
- { reg: '$edx', virtual-reg: '' }
66+
body: |
67+
bb.0:
68+
successors: %bb.2(0x40000000), %bb.1(0x40000000)
69+
liveins: $r14, $edi, $edx, $esi
70+
DBG_VALUE $r14, $noreg, !14, !DIExpression(DW_OP_LLVM_entry_value, 1), debug-location !19
71+
CMP32ri killed renamable $edx, 0, implicit-def $eflags, debug-location !20
72+
JCC_1 %bb.2, 4, implicit killed $eflags, debug-location !22
73+
bb.1.if.then:
74+
successors: %bb.3(0x80000000)
75+
liveins: $edi, $r13
76+
CALL64pcrel32 @foo, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit-def $eax, debug-location !23
77+
$r14 = MOV64ri 0, debug-location !20
78+
JMP_1 %bb.3, debug-location !25
79+
bb.2.if.else:
80+
successors: %bb.3(0x80000000)
81+
liveins: $esi, $r13
82+
$edi = MOV32rr killed $esi, debug-location !26
83+
CALL64pcrel32 @foo, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit-def $eax, debug-location !26
84+
bb.3.if.end:
85+
liveins: $eax
86+
$rbp = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !29
87+
RET64 implicit $eax, debug-location !29
88+
# CHECK-LABEL: bb.0:
89+
# CHECK: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)
90+
# CHECK-LABEL: bb.1.if.then:
91+
# CHECK: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)
92+
# CHECK-NEXT: CALL64pcrel32 @foo
93+
# CHECK-NEXT: $r14 = MOV64ri 0
94+
# CHECK-NOT: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)
95+
# CHECK-LABEL: bb.2.if.else:
96+
# CHECK: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)
97+
# CHECK-LABEL: bb.3.if.end:
98+
# CHECK: DBG_VALUE $r14, {{.*}}, !DIExpression(DW_OP_LLVM_entry_value, 1)

0 commit comments

Comments
 (0)