Skip to content

Debugging optimized code: lexical blocks, and a gdb test against clang - #197

Merged
teo1747 merged 10 commits into
mainfrom
loclists
Nov 9, 2026
Merged

teo1747 merged 10 commits into
mainfrom
loclists

Conversation

@teo1747

@teo1747 teo1747 commented Nov 8, 2026

Copy link
Copy Markdown
Member

Debugging optimized code. This is the start of the 1.0 debugging promise: "bt and info locals are right at every stop of the GDB test on every board family, at -O2."

Location lists already existed. They give 82–83% of values at -O2 on Cortex-M3, M0 and RV32 (llvm-dwarfdump --statistics puts their libc coverage near clang's). What was wrong was scope.

  • dwarf: lexical blocks, and where each variable was declared.
    • Every local was a child of its function, so in a second for (int i …) loop gdb printed the first loop's i, even at -O0.
    • Locals in blocks and for clauses now live in DW_TAG_lexical_blocks.
    • Each IR instruction carries its block's scope through every pass, as it carries its line. The old index ranges were dropped by mem2reg and sccp, so at -O2 every block was gone.
    • Variables now have DW_AT_decl_file/DW_AT_decl_line.
  • embld: keep SHT_MIPS_DWARF sections. A clang- or GCC-built MIPS object's debug info was silently dropped from the image.
  • tests: debug-optimized.sh.
    • gdb stops on every line of one program on QEMU's Cortex-M3, Cortex-M0 and RV32 and reads every variable.
    • Every value EmbCC's DWARF gives, at -O0, -O2 and -Os, must match clang's -O0 build at the same visit of the same line.
    • main's compiler fails it with 162 wrong or out-of-scope values at -O0; this branch has none.
    • CI now installs gdb-multiarch, so this test and debug-live.sh run on every pull request.

Still open:

  • MIPS in the test: gdb can't map clang's MIPS image to lines.
  • Lines visited a different number of times in the two builds (rotated loops) aren't compared yet.
  • Block extents are the hull of their code, not DWARF range lists.
  • Inlined functions have no DW_TAG_inlined_subroutine.

GCC and clang type a MIPS object's .debug_* sections SHT_MIPS_DWARF
(0x7000001e), not SHT_PROGBITS, and embld carried only PROGBITS into
the image. So a clang-built MIPS program linked by embld had no debug
information at all: gdb said "No symbol table is loaded". GNU ld keeps
them, and so does embld now. The merged sections are written as
PROGBITS, as before.
Every local was a child of its function's DIE, so `for (int i ...)`
twice was two variables named i at one level. gdb printed the first
one's i in the second loop, even at -O0, where it read that loop's own
stale slot. An inner block's `s` hid the function's `s` everywhere.
Against clang's -O0 build of the same program, EmbCC's -O0 showed 162
wrong or out-of-scope values on a Cortex-M3.

A local declared in a block, or in a for statement's first clause, now
lives in a DW_TAG_lexical_block over its block's code. irgen gives each
such block a scope (ir_func.scope_up) and stamps it on every instruction
it makes there (ir_ins.scope). An instruction carries its scope through
every pass as it carries its line. The inliner gives spliced code the
call's scope, and an instruction a pass invents reads 0, the function's.
The DWARF writer takes each block's extent from its instructions'
ins_pc, which the backend now records under -g whenever a function has
such a block. A block the optimizer left with no code gets no DIE, nor
do its variables. The scope cannot be irgen's var_scope_lo/hi:
mem2reg and sccp renumber instructions and drop those, so at -O2 every
block was gone.

The for statement's first-clause locals also get the for statement as
their var_scope, as a block's locals have their block (C11 6.8.5p5).
Two sibling loops' counters may then share a stack slot.

Every parameter and variable, globals included, now has DW_AT_decl_file
and DW_AT_decl_line, as GCC and clang write them. Three goldens read a
fixed number of llvm-dwarfdump lines after a variable's name, and now
read two more.
tests/golden/debug-optimized.sh runs one program on QEMU's Cortex-M3,
Cortex-M0 and RV32 boards under gdb. The program has loops, calls, a
struct, a 64-bit value, a search, and names declared again in sibling
and inner blocks. gdb stops on every line and reads every parameter and
local in scope (debug-optimized/trace.py). The program is built by
EmbCC at -O0, -O2 and -Os, and by clang at -O0. Every value EmbCC's
DWARF gives must be the one clang's build shows at the same visit of
the same line (debug-optimized/cmp.py). A location list that names the
wrong register, a stale value, or a variable shown outside its block
fails. An optimized-out value counts as unavailable, not wrong.

The reference is clang because EmbCC can be wrong the same way at every
level: before lexical blocks, EmbCC's -O0 and -O2 agreed with each other
and both showed the first loop's i in the second loop. That build fails
this test (162 values at -O0); this branch has none wrong, with 82-83%
of values available at -O2 and -Os on all three boards. Not MIPS yet:
gdb maps no address of clang's MIPS image to a line, so there is no
reference there.

CI installs gdb-multiarch, so this and debug-live.sh run on every pull
request instead of skipping.
A call the inliner took left no trace in the DWARF. Its code was the
caller's, its parameters and locals were gone, and gdb's backtrace in it
named only the caller, at the callee's lines.

The inliner now makes the spliced code a scope of the caller's: the
inlined function, inside the call's scope, with the call's line and
column (ir_func.scope_fn, scope_cline, scope_ccol). The callee's own
scopes are renumbered under it, its blocks and the calls inlined into
it before. The callee's parameters and locals become caller variables
in those scopes. They are the caller's slots V + k by then, so the
caller's mem2reg follows them, and they get location lists like any
local. Each one records its origin, the callee's variable.

The DWARF writer emits one abstract instance per inlined function, a
DW_TAG_subprogram with DW_AT_inline and its parameters and locals
without locations, ahead of the subprograms as the types are. It is
built from the copies, because a static function inlined everywhere has
no IR function left. Each inlined call is a DW_TAG_inlined_subroutine:
the abstract instance as its origin, its code's extent, the call's
file, line and column. Its variables refer to their abstract
counterparts and say where they are in that copy. gdb shows the call
as a frame of its own (`main > clamp`), with clamp's v, lo, hi and r.
main is called once, from a reset handler or crt0, so inlining it saves
a call and a return at most. Inlined, it is an inline frame of its
caller. gdb's backtrace at -O2 then went past the entry function into
0x0 (debug-frame.sh on RV32 and RV64, once inlined calls had DIEs),
and `break main` stopped in the reset handler's code. GCC keeps main
out of line too. The remark says callee-is-main.
A stop was named by its line and its visit of that line in the whole
run, so any line visited a different number of times in the two builds
was not compared anywhere: a rotated loop's test, but also every line of
a function called more often from a loop rotated differently. A third
of the stops went unchecked.

trace.py now records each stop's call stack, and which frames gdb shows
as inlined. cmp.py names a stop by its call (which call of each function
on the stack), its line and its visit within that call, and skips only
the lines whose visit count differs within one call: 77 to 98% of stops
are compared, from 69%. A call EmbCC inlined cannot be told from the
next call of the same function when no stop comes between them. There,
a value must be one clang's build shows at that line in a call from the
same caller invocation.

prog.c gains clamp(), small enough to inline at -O2, called in a loop.
When the inliner maps each inlined variable to the next slot, the test
fails on all three boards (5 to 10 values).
A lexical block or an inlined call spanned the first to the last byte of
its code. Where the optimizer interleaves other code with it, such as a
rotated loop's test or the next unrolled call, that span covers code
that is not the block's. A debugger stopped there sees the block's
variables, or an inlined frame that is not running.

Each scope's code is now collected as its ranges, touching ones merged.
A scope in one piece keeps DW_AT_low_pc and DW_AT_high_pc. One in
several gets DW_AT_ranges into .debug_rnglists (DWARF 5: start_end
entries) or .debug_ranges (DWARF 4: a base address selection entry of 0
and address pairs), each address relocated. The lists follow the unit's
own list, whose size is known before any DIE is written, so a block's
offset is known as its DIE is. A .dwo keeps the extent. The libc's
format.c, tz.c and regex.c at -O2 have 26 to 31 such blocks each.
llvm-dwarfdump --verify passes for DWARF 4 and 5, with and without
-ffunction-sections.
collect_locals recorded each block's scope in a table that gen_stmt
searched by pointer, and grew scope_up one element at a time. The
200,000 one-declaration blocks of many-declarations.sh's typedefs.c made
both quadratic: the file no longer compiled in the test's 120 seconds
(CI on #197). The scope is now a field of the statement (dbg_scope), and
scope_up doubles. The DWARF writer also built each scope's children and
variables by scanning all of them for every scope. It now builds the
lists once. The file compiles in 8 seconds again.
debug-live.sh used to skip on CI for want of gdb, and now that CI has
gdb-multiarch it failed linking: the x86-64 QEMU harness needs newlib,
which a CI runner has not. It now skips for that, by name.

debug-optimized.sh:
- Where the reference's own gdb cannot read a value, it is not
  compared. CI's older gdb cannot read clang's RV32 frame at main's
  last stop ("Cannot access memory").
- crc8 runs over 6 bytes, not 21: its inner loop was two thirds of the
  stops, and the test took 297 s on a CI runner. It now takes 49 s
  here, with 366 stops per board.
- MIPS is back, with clang's reference at DWARF 4. gdb maps no address
  of a linked MIPS image whose unit starts at a DWARF 5 .debug_addr
  entry, so clang's DWARF 5 image had no lines to it. With DWARF 4 all
  three levels agree, with 72% of values available at -O2.
@teo1747
teo1747 marked this pull request as ready for review November 9, 2026 00:11
@teo1747
teo1747 merged commit 382b17f into main Nov 9, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant