Skip to content

Commit

Permalink
[DebugInfo] Fix undef debug values being removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowy1803 committed Apr 26, 2024
1 parent 7e380ba commit ea41bbb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
5 changes: 1 addition & 4 deletions docs/HowToUpdateDebugInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ debug_value undef : $Int, let, name "x", expr op_consts:1:op_fragment:#Int._valu
```

> [!Caution]
> This currently doesn't work, such debug values are dropped.
> This currently doesn't work, an implicit op_deref is added.
### Undef variables

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

> [!Caution]
> This currently doesn't work, such debug values are dropped.
Additionally, if a previous `debug_value` exists for the variable, a debug value
of undef invalidates the previous value, in case the value of the variable isn't
known anymore:
Expand Down
5 changes: 2 additions & 3 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5598,11 +5598,10 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {

auto VarInfo = i->getVarInfo();
assert(VarInfo && "debug_value without debug info");
if (isa<SILUndef>(SILVal)) {
if (isa<SILUndef>(SILVal) && VarInfo->Name == "$error") {
// We cannot track the location of inlined error arguments because it has no
// representation in SIL.
if (!IsAddrVal &&
!i->getDebugScope()->InlinedCallSite && VarInfo->Name == "$error") {
if (!IsAddrVal && !i->getDebugScope()->InlinedCallSite) {
auto funcTy = CurSILFn->getLoweredFunctionType();
emitErrorResultVar(funcTy, funcTy->getErrorResult(), i);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ static bool seemsUseful(SILInstruction *I) {
}

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

return false;
}
Expand Down
24 changes: 24 additions & 0 deletions test/DebugInfo/irgen_undef.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %target-swiftc_driver -Xfrontend -disable-debugger-shadow-copies -O -g -emit-ir %s | %FileCheck %s
sil_stage canonical

import Swift
import Builtin

// CHECK-LABEL: define {{.*}} @just_undef
sil @just_undef : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
bb0(%0 : $Builtin.Int64):
// CHECK: call void @llvm.dbg.value(metadata i64 undef, metadata ![[JUST_UNDEF_VAR:[0-9]+]], metadata !DIExpression())
debug_value undef : $Builtin.Int64, var, name "optimizedout"
return %0 : $Builtin.Int64
}

// CHECK: ![[JUST_UNDEF_VAR]] = !DILocalVariable(name: "optimizedout"


// TO ADD AS TESTS:
//debug_value %0 : $Builtin.Int64, var, name "previous", type $Int, expr op_consts:1:op_minus // :op_fragment:#Int._value
//debug_value %0 : $Builtin.Int64, var, name "current", type $Int, expr op_fragment:#Int._value
//debug_value %0 : $Builtin.Int64, var, name "offseta", type $Int, expr op_consts:1:op_fragment:#Int._value
//debug_value undef : $Builtin.Int64, var, name "offsetb", type $Int, expr op_consts:1:op_fragment:#Int._value
//debug_value undef : $Builtin.Int64, var, name "offsetc", expr op_consts:1
// debug_value %0 : $Builtin.Int64, var, name "offsetd", expr op_consts:1
1 change: 1 addition & 0 deletions test/SILOptimizer/dead_alloc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public func deadClassInstance() {

// CHECK-LABEL: sil @$s10dead_alloc0A13ManagedBufferyyF :
// CHECK: bb0:
// CHECK-NEXT: debug_value undef
// CHECK-NEXT: tuple
// CHECK-NEXT: return
// CHECK-NEXT: } // end sil function '$s10dead_alloc0A13ManagedBufferyyF'
Expand Down

0 comments on commit ea41bbb

Please sign in to comment.