https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128881
Bug ID: 128881
Summary: [LTO] Nested lexical blocks become siblings in late
DWARF
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: debug
Assignee: unassigned at gcc dot gnu.org
Reporter: luolongjuna at gmail dot com
Target Milestone: ---
Consider the following case (reduced from LevelDB's
BytewiseComparatorImpl::FindShortSuccessor).
--- shorten.c ---
__attribute__((noipa))
static unsigned shorten(unsigned char *p, unsigned n)
{
for (unsigned i = 0; i < n; ++i) {
unsigned char byte = p[i];
if (byte != 255) {
p[i] = byte + 1;
return i + 1;
}
}
return n;
}
int main(void)
{
unsigned char p[] = {255, 'a', 'c'};
return shorten(p, sizeof p) != 2 || p[1] != 'b';
}
Compile and run under GDB:
gcc -O2 -g -flto shorten.c -o repro
gdb -q -nx -batch -ex 'break shorten.c:6 if i == 1' -ex run \
-ex 'info locals' -ex 'print byte' -ex 'print/u p[i]' ./repro
Reproduced on x86_64 GNU/Linux with GCC 17.0.0 (g:695282a69cfc) and Fedora
GCC 16.2.1, using GDB 17.2 and binutils 2.46.1.
In 'shorten', the for statement declares 'i' and the loop body declares
'byte'. In the LTO build, GDB reports 'byte' as <optimized out> at
'if (byte != 255)'.
The breakpoint stops on the second iteration. The -flto and -fno-lto
builds generate identical machine code for 'shorten', and 'print/u p[i]'
gives 97 in both.
With -flto With -fno-lto
(gdb) info locals (gdb) info locals
i = 1 byte = 97 'a'
byte = <optimized out> i = 1
(gdb) print byte (gdb) print byte
$1 = <optimized out> $1 = 97 'a'
Both trees below come from 'readelf -wi repro' on the LTO executable.
The arrows follow DW_AT_abstract_origin.
Early DWARF Late DWARF
shorten shorten -> early shorten
outer (i) outer (i) -> early outer
body (byte) body (byte) -> early body
Both concrete blocks cover the breakpoint. GDB selects the outer block,
where it finds a locationless 'byte' inherited from the early loop body.
The concrete 'byte' in the sibling block is in rcx and evaluates to 97.
In PR128621, GCC prunes a typedef-only outer block. Here the outer block
survives with 'i' but is split after scheduling. The fragment containing
the loop body has no DIE of its own, so late DWARF places the body
directly under 'shorten'. With -fno-schedule-insns2 the blocks stay
nested and 'print byte' gives 97 'a'.