[DebugInfo][BDCE] does not salvage/invalidate debuginfo when removing and/or/xor instructions
Description
BDCE removes or, xor, and and instructions after determining that the affected bits are irrelevant to all remaining program uses. The transformations preserve program behavior, but the #dbg_value records for the source variables defined by those instructions are rewritten to describe the original operands without preserving the removed operations.
As a result, LLDB reports each source variable value as the function argument a rather than the result of the source-level bitwise expression.
This appears to be the same class of problem as #46640, where BDCE rewrites sext as zext, but here it occurs when BDCE removes bitwise binary operators.
Reproducer
case.c:
#include <stdint.h>
volatile uint64_t sink;
__attribute__((noinline)) void use(uint64_t value) { sink = value; }
void func_or(uint64_t a) {
uint64_t value = a | 0x3;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
void func_xor(uint64_t a) {
uint64_t value = a ^ 0x3;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
void func_and(uint64_t a) {
uint64_t value = a & 0x18;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
int main(void) {
func_or(0x1d);
func_xor(0x1d);
func_and(0x1d);
return 0;
}
Build pipeline:
clang -g -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm case.c -o case.ll
opt -passes=mem2reg -S case.ll -o src.ll
opt -passes=bdce -S src.ll -o tgt.ll
clang src.ll -o src.out
clang tgt.ll -o tgt.out
Here are the complete src.ll and tgt.ll.
lldb-commands.txt:
breakpoint set --file case.c --line 9
breakpoint set --file case.c --line 15
breakpoint set --file case.c --line 21
run
frame variable --format hex value
continue
frame variable --format hex value
continue
frame variable --format hex value
quit
Run LLDB on the binaries before and after BDCE:
lldb src.out -s lldb-commands.txt
lldb tgt.out -s lldb-commands.txt
Observed Behavior
Before BDCE, the IR records the results of the three source-level operations:
; func_or
%or = or i64 %a, 3, !dbg !29
#dbg_value(i64 %or, !30, !DIExpression(), !28)
; func_xor
%xor = xor i64 %a, 3, !dbg !37
#dbg_value(i64 %xor, !38, !DIExpression(), !36)
; func_and
%and = and i64 %a, 24, !dbg !45
#dbg_value(i64 %and, !46, !DIExpression(), !44)
After BDCE removes those instructions, each #dbg_value describes value directly with %a and an empty expression:
; func_or
#dbg_value(i64 %a, !29, !DIExpression(), !28)
; func_xor
#dbg_value(i64 %a, !36, !DIExpression(), !35)
; func_and
#dbg_value(i64 %a, !43, !DIExpression(), !42)
With a = 0x1d, LLDB reports the source-level results before BDCE:
== src.out ==
func_or, line 9: (uint64_t) value = 0x000000000000001f
func_xor, line 15: (uint64_t) value = 0x000000000000001e
func_and, line 21: (uint64_t) value = 0x0000000000000018
After BDCE, LLDB reports the argument value for all three variables:
== tgt.out ==
func_or, line 9: (uint64_t) value = 0x000000000000001d
func_xor, line 15: (uint64_t) value = 0x000000000000001d
func_and, line 21: (uint64_t) value = 0x000000000000001d
Expected Behavior
BDCE should salvage the removed bitwise operations in the debug expressions when possible, or invalidate the affected debug values when they cannot be represented. LLDB should not report value as the unmodified argument a.
At the three breakpoints, the debugger should report the source-level values 0x1f, 0x1e, and 0x18, respectively, or report the variables as unavailable.
Environment
clang version 24.0.0git
llvm-project revision: d35d0e69980f11c2acbd3670c65fec3cf574224a
LLVM version 24.0.0git
lldb version 24.0.0git
@llvm/issue-subscribers-debuginfo
Author: MrEven132
BDCE removes or, xor, and and instructions after determining that the affected bits are irrelevant to all remaining program uses. The transformations preserve program behavior, but the #dbg_value records for the source variables defined by those instructions are rewritten to describe the original operands without preserving the removed operations.
As a result, LLDB reports each source variable value as the function argument a rather than the result of the source-level bitwise expression.
This appears to be the same class of problem as #46640, where BDCE rewrites sext as zext, but here it occurs when BDCE removes bitwise binary operators.
Reproducer
case.c:
#include <stdint.h>
volatile uint64_t sink;
__attribute__((noinline)) void use(uint64_t value) { sink = value; }
void func_or(uint64_t a) {
uint64_t value = a | 0x3;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
void func_xor(uint64_t a) {
uint64_t value = a ^ 0x3;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
void func_and(uint64_t a) {
uint64_t value = a & 0x18;
[[gnu::nodebug]] uint64_t ret1 = value & 0x8;
use(ret1);
}
int main(void) {
func_or(0x1d);
func_xor(0x1d);
func_and(0x1d);
return 0;
}
Build pipeline:
clang -g -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm case.c -o case.ll
opt -passes=mem2reg -S case.ll -o src.ll
opt -passes=bdce -S src.ll -o tgt.ll
clang src.ll -o src.out
clang tgt.ll -o tgt.out
Here are the complete src.ll and tgt.ll.
lldb-commands.txt:
breakpoint set --file case.c --line 9
breakpoint set --file case.c --line 15
breakpoint set --file case.c --line 21
run
frame variable --format hex value
continue
frame variable --format hex value
continue
frame variable --format hex value
quit
Run LLDB on the binaries before and after BDCE:
lldb src.out -s lldb-commands.txt
lldb tgt.out -s lldb-commands.txt
Observed Behavior
Before BDCE, the IR records the results of the three source-level operations:
; func_or
%or = or i64 %a, 3, !dbg !29
#dbg_value(i64 %or, !30, !DIExpression(), !28)
; func_xor
%xor = xor i64 %a, 3, !dbg !37
#dbg_value(i64 %xor, !38, !DIExpression(), !36)
; func_and
%and = and i64 %a, 24, !dbg !45
#dbg_value(i64 %and, !46, !DIExpression(), !44)
After BDCE removes those instructions, each #dbg_value describes value directly with %a and an empty expression:
; func_or
#dbg_value(i64 %a, !29, !DIExpression(), !28)
; func_xor
#dbg_value(i64 %a, !36, !DIExpression(), !35)
; func_and
#dbg_value(i64 %a, !43, !DIExpression(), !42)
With a = 0x1d, LLDB reports the source-level results before BDCE:
== src.out ==
func_or, line 9: (uint64_t) value = 0x000000000000001f
func_xor, line 15: (uint64_t) value = 0x000000000000001e
func_and, line 21: (uint64_t) value = 0x0000000000000018
After BDCE, LLDB reports the argument value for all three variables:
== tgt.out ==
func_or, line 9: (uint64_t) value = 0x000000000000001d
func_xor, line 15: (uint64_t) value = 0x000000000000001d
func_and, line 21: (uint64_t) value = 0x000000000000001d
Expected Behavior
BDCE should salvage the removed bitwise operations in the debug expressions when possible, or invalidate the affected debug values when they cannot be represented. LLDB should not report value as the unmodified argument a.
At the three breakpoints, the debugger should report the source-level values 0x1f, 0x1e, and 0x18, respectively, or report the variables as unavailable.
Environment
clang version 24.0.0git
llvm-project revision: d35d0e69980f11c2acbd3670c65fec3cf574224a
Target: x86_64-unknown-linux-gnu
LLVM version 24.0.0git
lldb version 24.0.0git
Linux x86_64
This behaviour does indeed sound faulty -- however I've adapted the IR from a testcase into https://godbolt.org/z/j3rhceb1E , and it seems to successfully salvage the deleted xor and produce a correct DIExpression describing the deleted code. Could there be something in your test setup that's preventing that?
Thanks for looking into this. I believe your Godbolt example exercises a different case from the one reported here. In your example, the entire result of the xor instruction is dead except for its debug-info use. In this issue, some bits of the xor result remain live and are still used by the program.
I checked the 'BDCE' implementation, and these two cases are handled through different code paths.
Ah, right -- this is because BDCE doesn't edit users and then delete the instruction, it uses RAUW to set all users to the unmodified value. So indeed faulty.
One wonders whether this is a common pattern -- salvageDebugInfo exists for the common case of all users of an instruction being optimised away and then the instruction gets deleted, but I don't think there's any general facility in LLVM for this scenario of "the instruction is meaningless" instead of "this instruction is unused".
Adding a call to salvageDebugInfo can recover debug information that directly references the instruction being deleted, but it cannot guarantee that the incorrect replacement will not propagate to other debug information. I’m wondering whether such a partial fix would still be worth pursuing.
Hmmm -- as in if we have a chain of instructions:
%foo = xor %bar, 123
%baz = and %foo, 0FFFFFF00
%qux = add %baz, 5678
#dbg_value %qux, [...]
And %baz gets DCE'd in the manner of this issue, %qux would have a different meaning but it's harder to find the #dbg_value's attached when editing %baz? That is indeed tricky, and probably infeasibly complex to fix in the general case. However, we should still take a shot at addressing the first level, that's where 90% of the variable location coverage will come from, and there's no need for perfect to be the enemy of good.
I'm was wondering whether replaceAllUsesWith could grow an argument signalling that the replacement has a different meaning, and thus any debug-info referring to it should be invalidated... however, RAUW is so pervasive through the codebase that it feels unlikely that other developers will think about and maintain an optional flag, especially if it has a subtle meaning. Thus we probably need a Instruction::poisonDbgRecords or similar that safely terminates debug-info without using RAUW.
(Ideally we'd also audit the 700 calls to RAUW in llvm/lib/Transforms, but that's "future work").