Skip to content

Commit 28a7b09

Browse files
committed
[DebugInfo] Fix undef debug values being removed
1 parent 7e380ba commit 28a7b09

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

docs/HowToUpdateDebugInfo.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ debug_value undef : $Int, let, name "x", expr op_consts:1:op_fragment:#Int._valu
218218
```
219219

220220
> [!Caution]
221-
> This currently doesn't work, such debug values are dropped.
221+
> This currently doesn't work, an implicit op_deref is added.
222222
223223
### Undef variables
224224

@@ -229,9 +229,6 @@ optimized away, an undef debug value should still be kept:
229229
debug_value undef : $Int, let, name "x"
230230
```
231231

232-
> [!Caution]
233-
> This currently doesn't work, such debug values are dropped.
234-
235232
Additionally, if a previous `debug_value` exists for the variable, a debug value
236233
of undef invalidates the previous value, in case the value of the variable isn't
237234
known anymore:

lib/IRGen/IRGenSIL.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,11 +5598,10 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
55985598

55995599
auto VarInfo = i->getVarInfo();
56005600
assert(VarInfo && "debug_value without debug info");
5601-
if (isa<SILUndef>(SILVal)) {
5601+
if (isa<SILUndef>(SILVal) && VarInfo->Name == "$error") {
56025602
// We cannot track the location of inlined error arguments because it has no
56035603
// representation in SIL.
5604-
if (!IsAddrVal &&
5605-
!i->getDebugScope()->InlinedCallSite && VarInfo->Name == "$error") {
5604+
if (!IsAddrVal && !i->getDebugScope()->InlinedCallSite) {
56065605
auto funcTy = CurSILFn->getLoweredFunctionType();
56075606
emitErrorResultVar(funcTy, funcTy->getErrorResult(), i);
56085607
}

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ static bool seemsUseful(SILInstruction *I) {
7777
}
7878

7979
// Is useful if it's associating with a function argument
80+
// If undef, it is useful and it doesn't cost anything.
8081
if (isa<DebugValueInst>(I))
81-
return isa<SILFunctionArgument>(I->getOperand(0));
82+
return isa<SILFunctionArgument>(I->getOperand(0))
83+
|| isa<SILUndef>(I->getOperand(0));
8284

8385
return false;
8486
}

test/DebugInfo/irgen_undef.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swiftc_driver -Xfrontend -disable-debugger-shadow-copies -O -g -emit-ir %s | %FileCheck %s
2+
sil_stage canonical
3+
4+
import Swift
5+
import Builtin
6+
7+
// CHECK-LABEL: define {{.*}} @just_undef
8+
sil @just_undef : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
9+
bb0(%0 : $Builtin.Int64):
10+
// CHECK: call void @llvm.dbg.value(metadata i64 undef, metadata ![[JUST_UNDEF_VAR:[0-9]+]], metadata !DIExpression())
11+
debug_value undef : $Builtin.Int64, var, name "optimizedout"
12+
return %0 : $Builtin.Int64
13+
}
14+
15+
// CHECK: ![[JUST_UNDEF_VAR]] = !DILocalVariable(name: "optimizedout"
16+
17+
18+
// TO ADD AS TESTS:
19+
//debug_value %0 : $Builtin.Int64, var, name "previous", type $Int, expr op_consts:1:op_minus // :op_fragment:#Int._value
20+
//debug_value %0 : $Builtin.Int64, var, name "current", type $Int, expr op_fragment:#Int._value
21+
//debug_value %0 : $Builtin.Int64, var, name "offseta", type $Int, expr op_consts:1:op_fragment:#Int._value
22+
//debug_value undef : $Builtin.Int64, var, name "offsetb", type $Int, expr op_consts:1:op_fragment:#Int._value
23+
//debug_value undef : $Builtin.Int64, var, name "offsetc", expr op_consts:1
24+
// debug_value %0 : $Builtin.Int64, var, name "offsetd", expr op_consts:1

0 commit comments

Comments
 (0)