Skip to content

Commit d06d34d

Browse files
committed
[DebugInfo] Fix undef debug values being removed
(cherry picked from commit c2c16f5)
1 parent c73ac32 commit d06d34d

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

docs/HowToUpdateDebugInfo.md

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

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

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

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

lib/IRGen/IRGenSIL.cpp

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

54805480
auto VarInfo = i->getVarInfo();
54815481
assert(VarInfo && "debug_value without debug info");
5482-
if (isa<SILUndef>(SILVal)) {
5482+
if (isa<SILUndef>(SILVal) && VarInfo->Name == "$error") {
54835483
// We cannot track the location of inlined error arguments because it has no
54845484
// representation in SIL.
5485-
if (!IsAddrVal &&
5486-
!i->getDebugScope()->InlinedCallSite && VarInfo->Name == "$error") {
5485+
if (!IsAddrVal && !i->getDebugScope()->InlinedCallSite) {
54875486
auto funcTy = CurSILFn->getLoweredFunctionType();
54885487
emitErrorResultVar(funcTy, funcTy->getErrorResult(), i);
54895488
}

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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"

test/SILOptimizer/dead_alloc.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public func deadClassInstance() {
5252

5353
// CHECK-LABEL: sil @$s10dead_alloc0A13ManagedBufferyyF :
5454
// CHECK: bb0:
55+
// CHECK-NEXT: debug_value undef
5556
// CHECK-NEXT: tuple
5657
// CHECK-NEXT: return
5758
// CHECK-NEXT: } // end sil function '$s10dead_alloc0A13ManagedBufferyyF'

0 commit comments

Comments
 (0)