Skip to content

Commit 84dae7c

Browse files
committed
[DebugInfo] Add support for constant debug values
1 parent 8ad3066 commit 84dae7c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

docs/HowToUpdateDebugInfo.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ value.
218218
debug_value undef : $Int, let, name "x", expr op_consts:1:op_fragment:#Int._value
219219
```
220220

221-
> [!Caution]
222-
> This currently doesn't work, these variables are dropped by IRGen.
223-
224221
### Undef variables
225222

226223
If the value of the variable cannot be recovered as the value is entirely

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,6 +3432,16 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
34323432
// /always/ emit an llvm.dbg.value of undef.
34333433
// If we have undef, always emit a llvm.dbg.value in the current position.
34343434
if (isa<llvm::UndefValue>(Storage)) {
3435+
if (Expr->getNumElements() &&
3436+
(Expr->getElement(0) == llvm::dwarf::DW_OP_consts
3437+
|| Expr->getElement(0) == llvm::dwarf::DW_OP_constu)) {
3438+
/// Convert `undef, expr op_consts:N:...` to `N, expr ...`
3439+
Storage = llvm::ConstantInt::get(
3440+
llvm::IntegerType::getInt64Ty(Builder.getContext()),
3441+
Expr->getElement(1));
3442+
Expr = llvm::DIExpression::get(Builder.getContext(),
3443+
Expr->getElements().drop_front(2));
3444+
}
34353445
DBuilder.insertDbgValueIntrinsic(Storage, Var, Expr, DL, ParentBlock);
34363446
return;
34373447
}

test/DebugInfo/irgen_undef.sil

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@ return %0 : $Builtin.Int64
1616
sil @arithmetic : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
1717
bb0(%0 : $Builtin.Int64):
1818
// CHECK: call void @llvm.dbg.value(metadata i64 %0, metadata ![[CURRENT_VAR:[0-9]+]], metadata !DIExpression())
19-
debug_value %0 : $Builtin.Int64, var, name "current", type $Int, expr op_fragment:#Int._value
19+
debug_value %0 : $Builtin.Int64, var, name "current", type $Int64, expr op_fragment:#Int64._value
2020
// FIXME: It should work with the fragment, as it should be noop.
2121
// CHECK: call void @llvm.dbg.value(metadata i64 %0, metadata ![[PREVIOUS_VAR:[0-9]+]], metadata !DIExpression(DW_OP_consts, 1, DW_OP_minus, DW_OP_stack_value))
22-
debug_value %0 : $Builtin.Int64, var, name "previous", type $Int, expr op_consts:1:op_minus //:op_fragment:#Int._value
22+
debug_value %0 : $Builtin.Int64, var, name "previous", type $Int64, expr op_consts:1:op_minus //:op_fragment:#Int64._value
2323
// CHECK: call void @llvm.dbg.value(metadata i64 %0, metadata ![[NEXT_VAR:[0-9]+]], metadata !DIExpression(DW_OP_constu, 12, DW_OP_plus, DW_OP_stack_value))
24-
debug_value %0 : $Builtin.Int64, var, name "next", type $Int, expr op_constu:12:op_plus //:op_fragment:#Int._value
24+
debug_value %0 : $Builtin.Int64, var, name "next", type $Int64, expr op_constu:12:op_plus //:op_fragment:#Int64._value
2525
return %0 : $Builtin.Int64
2626
}
2727

28+
// CHECK-LABEL: define {{.*}} @constant
29+
sil @constant : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
30+
bb0(%0 : $Builtin.Int64):
31+
// CHECK: call void @llvm.dbg.value(metadata i64 42, metadata ![[CONST_VAR:[0-9]+]], metadata !DIExpression(DW_OP_stack_value))
32+
debug_value undef : $Builtin.Int64, var, name "const", expr op_consts:42
33+
// CHECK: call void @llvm.dbg.value(metadata i64 3645, metadata ![[CONSTINT_VAR:[0-9]+]], metadata !DIExpression(DW_OP_stack_value))
34+
debug_value undef : $Builtin.Int64, var, name "constint", type $Int64, expr op_constu:3645:op_fragment:#Int64._value
35+
// CHECK: call void @llvm.dbg.value(metadata i64 6, metadata ![[CONSTUPLE_VAR:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_fragment, 0, 64))
36+
debug_value undef : $Builtin.Int64, var, name "constuple", type $(Int64, Int64), expr op_consts:6:op_tuple_fragment:$(Int64, Int64):0:op_fragment:#Int64._value
37+
38+
return %0 : $Builtin.Int64
39+
}
40+
41+
2842
// CHECK: ![[JUST_UNDEF_VAR]] = !DILocalVariable(name: "optimizedout"
2943
// CHECK: ![[CURRENT_VAR]] = !DILocalVariable(name: "current"
3044
// CHECK: ![[PREVIOUS_VAR]] = !DILocalVariable(name: "previous"
3145
// CHECK: ![[NEXT_VAR]] = !DILocalVariable(name: "next"
46+
// CHECK: ![[CONST_VAR]] = !DILocalVariable(name: "const"
47+
// CHECK: ![[CONSTINT_VAR]] = !DILocalVariable(name: "constint"
48+
// CHECK: ![[CONSTUPLE_VAR]] = !DILocalVariable(name: "constuple"

0 commit comments

Comments
 (0)