From 9e9d10c69fdbb6113a0cd26b8e0088a977a6a35d Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:10:55 +0100 Subject: [PATCH 1/9] embld: keep a MIPS object's SHT_MIPS_DWARF debug sections 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. --- src/link/link.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/link/link.c b/src/link/link.c index 02121256..1d57d7f1 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -1538,7 +1538,12 @@ static void collect_sections(struct linker *l, struct object *o) * Concatenated per name; the offset this object's piece * landed at is what its relocations resolve against. */ const char *nm = o->shstr + sh->sh_name; - if (l->keep_debug && sh->sh_type == SHT_PROGBITS && + /* (MIPS objects from GCC and clang type theirs + * SHT_MIPS_DWARF, 0x7000001e, not PROGBITS: GNU ld keeps + * them, and this dropped every one, so a clang-built MIPS + * image had no debug info at all) */ + if (l->keep_debug && (sh->sh_type == SHT_PROGBITS || + sh->sh_type == 0x7000001eu) && strncmp(nm, ".debug_", 7) == 0 && sh->sh_size && !(sh->sh_flags & 0x80000000u /* SHF_EXCLUDE */)) { int d = dbgsec_for(l, nm); From fa90a565a8224a6d5ceef0230f79eb66ea55649b Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:10:55 +0100 Subject: [PATCH 2/9] dwarf: lexical blocks, and where each variable was declared 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. --- src/arch/regalloc.c | 11 +- src/debug/dwarf.c | 215 ++++++++++++++++++++++++++------- src/ir/ir.h | 13 ++ src/ir/irgen.c | 101 ++++++++++++++-- src/opt/inline.c | 5 + tests/golden/debug-embedded.sh | 16 +-- tests/golden/embdbg-remote.sh | 2 +- tests/golden/thumb-codegen.sh | 2 +- 8 files changed, 296 insertions(+), 69 deletions(-) diff --git a/src/arch/regalloc.c b/src/arch/regalloc.c index 2e97f810..bce02927 100644 --- a/src/arch/regalloc.c +++ b/src/arch/regalloc.c @@ -2487,12 +2487,19 @@ void ra_debug_locs(struct ir_func *fn, const struct ir_vhome *home) /* -g: room for each instruction's code offset (ir_func.ins_pc), when the * function has a variable mem2reg followed; NULL otherwise. */ +/* -g: does a local live in a nested block? Its DW_TAG_lexical_block needs + * its instructions' code, so ins_pc (dwarf.c, emit_scopes). */ +static int has_block_locals(const struct ir_func *fn) +{ + return fn->nscopes > 1 && fn->scope_up; +} + void ra_debug_pc_begin(struct ir_func *fn) { free(fn->ins_pc); fn->ins_pc = NULL; - if (((target_debug_info() && fn->vreg_var) || target_stack_info()) && - fn->nins) { + if (((target_debug_info() && (fn->vreg_var || has_block_locals(fn))) || + target_stack_info()) && fn->nins) { fn->ins_pc = xmalloc(((size_t)fn->nins + 1) * sizeof *fn->ins_pc); for (int k = 0; k <= fn->nins; k++) fn->ins_pc[k] = -1; diff --git a/src/debug/dwarf.c b/src/debug/dwarf.c index db97b2c7..4452cd9a 100644 --- a/src/debug/dwarf.c +++ b/src/debug/dwarf.c @@ -18,6 +18,7 @@ #define DW_TAG_union_type 0x17 #define DW_TAG_member 0x0d #define DW_TAG_subrange_type 0x21 +#define DW_TAG_lexical_block 0x0b #define DW_CHILDREN_no 0x00 #define DW_CHILDREN_yes 0x01 #define DW_AT_name 0x03 @@ -37,6 +38,8 @@ #define DW_AT_upper_bound 0x2f #define DW_AT_data_member_location 0x38 #define DW_AT_data_bit_offset 0x6b +#define DW_AT_decl_file 0x3a +#define DW_AT_decl_line 0x3b #define DW_FORM_addr 0x01 #define DW_FORM_block1 0x0a #define DW_FORM_data1 0x0b @@ -149,6 +152,7 @@ #define AB_PARAM_LL 22 #define AB_VAR_T_LL 23 /* variable, with type, location list */ #define AB_VAR_LL 24 +#define AB_LEXBLOCK 25 /* lexical_block: a nested scope's code */ static int dw_ver = 5; @@ -370,11 +374,15 @@ static void emit_abbrev(struct dbuf *b) sub[3] = sub_t[5] = DW_FORM_addrx; sub[5] = sub_t[7] = DW_FORM_data4; } + /* (each with the file and line it was declared at: the line table's + * one file, 1) */ static const unsigned char param_t[] = { - DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc }; static const unsigned char param[] = { - DW_AT_name, DW_FORM_string, DW_AT_location, DW_FORM_exprloc }; + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_location, DW_FORM_exprloc }; static const unsigned char base[] = { DW_AT_name, DW_FORM_string, DW_AT_encoding, DW_FORM_data1, DW_AT_byte_size, DW_FORM_data1 }; @@ -398,10 +406,10 @@ static void emit_abbrev(struct dbuf *b) one_abbrev(b, AB_CU, DW_TAG_compile_unit, DW_CHILDREN_yes, cu, dw_split ? 4 : 7); /* (split: its pc and lines the skeleton's) */ one_abbrev(b, AB_SUBPROGRAM, DW_TAG_subprogram, DW_CHILDREN_yes, sub, 4); - one_abbrev(b, AB_PARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t, 3); - one_abbrev(b, AB_PARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, param, 2); - one_abbrev(b, AB_VAR_T, DW_TAG_variable, DW_CHILDREN_no, param_t, 3); - one_abbrev(b, AB_VAR, DW_TAG_variable, DW_CHILDREN_no, param, 2); + one_abbrev(b, AB_PARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t, 5); + one_abbrev(b, AB_PARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, param, 4); + one_abbrev(b, AB_VAR_T, DW_TAG_variable, DW_CHILDREN_no, param_t, 5); + one_abbrev(b, AB_VAR, DW_TAG_variable, DW_CHILDREN_no, param, 4); one_abbrev(b, AB_BASE, DW_TAG_base_type, DW_CHILDREN_no, base, 3); one_abbrev(b, AB_PTR_T, DW_TAG_pointer_type, DW_CHILDREN_no, ptr_t, 2); one_abbrev(b, AB_PTR, DW_TAG_pointer_type, DW_CHILDREN_no, ptr, 1); @@ -417,16 +425,26 @@ static void emit_abbrev(struct dbuf *b) one_abbrev(b, AB_SUBRANGE_NB, DW_TAG_subrange_type, DW_CHILDREN_no, subrange, 0); one_abbrev(b, AB_CU_RANGES, DW_TAG_compile_unit, DW_CHILDREN_yes, cu_ranges, 7); unsigned char param_t_ll[] = { - DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4, DW_AT_location, DW_FORM_sec_offset }; unsigned char param_ll[] = { - DW_AT_name, DW_FORM_string, DW_AT_location, DW_FORM_sec_offset }; + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_location, DW_FORM_sec_offset }; if (dw_split) - param_t_ll[5] = param_ll[3] = DW_FORM_loclistx; - one_abbrev(b, AB_PARAM_T_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t_ll, 3); - one_abbrev(b, AB_PARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_ll, 2); - one_abbrev(b, AB_VAR_T_LL, DW_TAG_variable, DW_CHILDREN_no, param_t_ll, 3); - one_abbrev(b, AB_VAR_LL, DW_TAG_variable, DW_CHILDREN_no, param_ll, 2); + param_t_ll[9] = param_ll[7] = DW_FORM_loclistx; + one_abbrev(b, AB_PARAM_T_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t_ll, 5); + one_abbrev(b, AB_PARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_ll, 4); + one_abbrev(b, AB_VAR_T_LL, DW_TAG_variable, DW_CHILDREN_no, param_t_ll, 5); + one_abbrev(b, AB_VAR_LL, DW_TAG_variable, DW_CHILDREN_no, param_ll, 4); + /* a nested block's scope: where its code is, its variables inside */ + unsigned char lexb[] = { + DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_addr }; + if (dw_split) { + lexb[1] = DW_FORM_addrx; + lexb[3] = DW_FORM_data4; + } + one_abbrev(b, AB_LEXBLOCK, DW_TAG_lexical_block, DW_CHILDREN_yes, lexb, 2); db_uleb(b, 0); /* end of the abbrev table */ } @@ -670,6 +688,9 @@ static void emit_globals(struct dwarf_out *out, struct dbuf *b, continue; db_uleb(b, AB_VAR_T); db_str(b, g->name); + db_u8(b, 1); /* decl_file */ + db_uleb(b, (unsigned long)(g->name_line > 0 ? g->name_line + : g->line > 0 ? g->line : 0)); db_u32(b, (unsigned long)toff); long dsp = target_get() == TARGET_AVR ? AVR_DATA_SPACE : 0; if (dw_split) { @@ -793,6 +814,141 @@ static int list_header(struct dbuf *b) return at; } +/* One parameter's or local's DIE: its name, where it was declared, its + * type, and its location -- a frame slot, or where the optimizer keeps it, + * range by range (ra_debug_locs). */ +static void emit_dbgvar(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, int v, long lo) +{ + struct ir_dbgvar *dv = &fn->dbgvars[v]; + int toff = type_lookup(tm, dv->ty); + int ll = fn->ndbglocs && fn->ndbglocs[v] > 0 && + (fn->var_off[dv->vreg] == IR_VAR_NO_LOC || dv->moved); + int code = dv->is_param + ? (toff >= 0 ? (ll ? AB_PARAM_T_LL : AB_PARAM_T) + : (ll ? AB_PARAM_LL : AB_PARAM)) + : (toff >= 0 ? (ll ? AB_VAR_T_LL : AB_VAR_T) + : (ll ? AB_VAR_LL : AB_VAR)); + db_uleb(b, code); + db_str(b, dv->name); + db_u8(b, 1); /* decl_file */ + db_uleb(b, (unsigned long)(dv->line > 0 ? dv->line : 0)); + if (toff >= 0) db_u32(b, (unsigned long)toff); + if (ll && dw_split) { + if (dw_nll == dw_capll) { + dw_capll = dw_capll ? dw_capll * 2 : 16; + dw_lloff = xrealloc(dw_lloff, (size_t)dw_capll * sizeof *dw_lloff); + } + dw_lloff[dw_nll] = loc->len; + db_uleb(b, (unsigned long)dw_nll++); + emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); + } else if (ll) { + reloc(out, DWSEC_INFO, b->len, 4, DWTGT_LOC, loc->len); + db_u32(b, 0); + emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); + } else { + loc_fbreg(b, fn->var_off[dv->vreg]); + } +} + +/* Where instruction k's code starts: its ins_pc, or for one fused into + * the instruction before it (no offset of its own), where the next one's + * does; within the function. */ +static long ins_start(const struct ir_func *fn, int k, long lo, long hi) +{ + for (; k < fn->nins; k++) + if (fn->ins_pc[k] >= 0) + break; + long pc = fn->ins_pc[k < fn->nins ? k : fn->nins]; + if (pc < 0) + pc = hi; + return pc < lo ? lo : pc > hi ? hi : pc; +} + +/* One block's DIE, its variables, and the blocks nested in it. */ +static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, long lo, + int s, const long *smin, const long *smax) +{ + if (s > 0) { + db_uleb(b, AB_LEXBLOCK); + if (dw_split) { + db_uleb(b, (unsigned long)addr_index(DWTGT_TEXT, smin[s], NULL, 0)); + db_u32(b, (unsigned long)(smax[s] - smin[s])); + } else { + reloc(out, DWSEC_INFO, b->len, addr_bytes(), DWTGT_TEXT, smin[s]); + db_addr(b, 0); + reloc_end(out, DWSEC_INFO, b->len, addr_bytes(), smax[s]); + db_addr(b, 0); + } + } + for (int v = 0; v < fn->ndbgvars; v++) { + int vs = fn->dbgvars[v].scope; + if (vs < 0 && smin != NULL) + continue; /* its block has no code */ + if (vs < 0 || vs >= fn->nscopes || smin == NULL) + vs = 0; + if (vs == s) + emit_dbgvar(out, b, loc, tm, fn, v, lo); + } + if (smin) + for (int c = s + 1; c < fn->nscopes; c++) + if (fn->scope_up[c] == s && smin[c] < smax[c]) + emit_block(out, b, loc, tm, fn, lo, c, smin, smax); + if (s > 0) + db_u8(b, 0); +} + +/* A function's parameters and locals, each in its scope. A local declared + * in a nested block -- or in a for statement's first clause -- lives in a + * DW_TAG_lexical_block over its block's code, so `for (int i ...)` twice + * is two variables a debugger shows one at a time, and an inner `s` hides + * the outer one only inside its block. They were all the function's, and + * gdb printed the first `i` in the second loop -- at -O0, from its own, + * stale, slot. A block's code is that of every instruction irgen made in + * it or in a block inside it (ir_ins.scope, which instructions keep + * through every pass), its extent from the first to the last. A block the + * optimizer left with no code has no DIE, nor do its variables: nothing + * can stop inside it, and at the function's level its variable would + * shadow the function's own of that name. Where the backend recorded no + * ins_pc, every variable is the function's, as before. */ +static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, long lo, + long hi) +{ + if (fn->ndbgvars <= 0) + return; + if (!fn->ins_pc || fn->nscopes <= 1 || !fn->scope_up || fn->nins <= 0) { + emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL); + return; + } + int ns = fn->nscopes; + long *smin = xmalloc((size_t)ns * sizeof *smin); + long *smax = xmalloc((size_t)ns * sizeof *smax); + for (int s = 0; s < ns; s++) { + smin[s] = hi; + smax[s] = lo; + } + for (int k = 0; k < fn->nins; k++) { + long a = ins_start(fn, k, lo, hi), z = ins_start(fn, k + 1, lo, hi); + if (z <= a) + continue; + for (int t = fn->ins[k].scope; t > 0 && t < ns; t = fn->scope_up[t]) { + if (a < smin[t]) smin[t] = a; + if (z > smax[t]) smax[t] = z; + } + } + /* a variable of a block with no code: shown nowhere */ + for (int v = 0; v < fn->ndbgvars; v++) { + int vs = fn->dbgvars[v].scope; + if (vs > 0 && vs < ns && smin[vs] >= smax[vs]) + fn->dbgvars[v].scope = -1; + } + emit_block(out, b, loc, tm, fn, lo, 0, smin, smax); + free(smin); + free(smax); +} + static void emit_info(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, struct ir_unit *iu, const char *filename, @@ -944,38 +1100,7 @@ static void emit_info(struct dwarf_out *out, struct dbuf *b, } } - for (int v = 0; v < fn->ndbgvars; v++) { - struct ir_dbgvar *dv = &fn->dbgvars[v]; - int toff = type_lookup(&tm, dv->ty); - /* a variable the optimizer took out of memory: where it is, - * range by range (ra_debug_locs) */ - int ll = fn->ndbglocs && fn->ndbglocs[v] > 0 && - (fn->var_off[dv->vreg] == IR_VAR_NO_LOC || dv->moved); - int code = dv->is_param - ? (toff >= 0 ? (ll ? AB_PARAM_T_LL : AB_PARAM_T) - : (ll ? AB_PARAM_LL : AB_PARAM)) - : (toff >= 0 ? (ll ? AB_VAR_T_LL : AB_VAR_T) - : (ll ? AB_VAR_LL : AB_VAR)); - db_uleb(b, code); - db_str(b, dv->name); - if (toff >= 0) db_u32(b, (unsigned long)toff); - if (ll && dw_split) { - if (dw_nll == dw_capll) { - dw_capll = dw_capll ? dw_capll * 2 : 16; - dw_lloff = xrealloc(dw_lloff, - (size_t)dw_capll * sizeof *dw_lloff); - } - dw_lloff[dw_nll] = loc->len; - db_uleb(b, (unsigned long)dw_nll++); - emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); - } else if (ll) { - reloc(out, DWSEC_INFO, b->len, 4, DWTGT_LOC, loc->len); - db_u32(b, 0); - emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); - } else { - loc_fbreg(b, fn->var_off[dv->vreg]); - } - } + emit_scopes(out, b, loc, &tm, fn, lo, hi); db_u8(b, 0); /* end of this subprogram's children */ } db_u8(b, 0); /* end of the CU's children */ diff --git a/src/ir/ir.h b/src/ir/ir.h index e60712ca..e8e448a6 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -300,6 +300,11 @@ struct ir_ins { int line; int col; int synth; + /* -g: the lexical scope the instruction's code belongs to -- 0 the + * function's own, else an index into ir_func.scope_up (irgen). Copied + * with the instruction, as its line is; an instruction a pass invents + * reads 0, which leaves its code outside any nested block. */ + int scope; int dst, a, b; int c; /* IR_CMPXCHG: the third operand (desired value) */ int nrp; /* IR_RET of a struct as its register units @@ -536,6 +541,7 @@ struct ir_dbgvar { int is_param; struct type *ty; int line, col; + int scope; /* its block's lexical scope (ir_ins.scope) */ /* mem2reg took this variable out of memory although something * assigned it: its slot no longer follows it. Its only reader is a * backend deciding whether the slot is a true DW_AT_location -- for a @@ -636,6 +642,13 @@ struct ir_func { * function-level locals span the whole function; only nested-block locals get * a narrower range. Length nvars; unused (NULL) when there are no locals. */ int *var_scope_lo, *var_scope_hi; + /* -g: the lexical scopes of the blocks that declare locals, 1 to + * nscopes - 1 (0 is the function's own): scope_up[s] the one it is + * nested in. Instructions name theirs (ir_ins.scope), and the DWARF + * writer makes a DW_TAG_lexical_block of each from its instructions' + * code -- which, unlike var_scope_lo/hi, no reordering makes stale. */ + int *scope_up; + int nscopes; /* exception regions (C++'s lowering), the catch types their landing * pads' selectors number (1-based; NULL: catch-all), and — codegen — * every call's code */ diff --git a/src/ir/irgen.c b/src/ir/irgen.c index 36154968..642caa8a 100644 --- a/src/ir/irgen.c +++ b/src/ir/irgen.c @@ -183,6 +183,22 @@ void ir_locals_fill(struct ir_func *fn, struct func *f, int nvars) } } +/* -g: the lexical scope being generated (ir_ins.scope), and the blocks + * collect_locals gave one: each STMT_BLOCK or STMT_FOR that declares a + * local. */ +static int g_cur_scope; +static struct stmt **g_scope_stmt; +static int *g_scope_id; +static int g_nscope_stmt, g_capscope_stmt; + +static int scope_of_stmt(const struct stmt *s) +{ + for (int k = 0; k < g_nscope_stmt; k++) + if (g_scope_stmt[k] == s) + return g_scope_id[k]; + return -1; +} + struct ir_ins *emit(struct ir_func *fn) { if (fn->nins == fn->cap) { @@ -200,6 +216,7 @@ struct ir_ins *emit(struct ir_func *fn) i->line = g_cur_line; i->col = g_cur_col; i->synth = g_synth; + i->scope = g_cur_scope; i->dst = i->a = i->b = -1; i->w = 4; i->size = 4; @@ -6871,6 +6888,10 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, lc.cont = new_label(fn); lc.brk = new_label(fn); int for_vla = g_nvla; + int for_lo = fn->nins; + int for_scope = g_cur_scope, fs = scope_of_stmt(s); + if (fs >= 0) + g_cur_scope = fs; lc.brk_vla = lc.cont_vla = g_nvla; if (s->initdecl) gen_stmt(fn, s->initdecl, &lc); @@ -6893,6 +6914,17 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, emit_label(fn, lc.brk); vla_release(fn, for_vla); g_nvla = for_vla; + /* `for (int i = 0; ...)`: i's scope is the for statement + * (C11 6.8.5p5), as a block's local's is its block -- so two + * loops' i are two variables a debugger tells apart, and may + * share a slot */ + for (struct stmt *c = s->initdecl; c; c = c->next) + if (c->kind == STMT_DECL && !c->is_extern && !c->sglob && + c->var_index >= 0 && c->var_index < fn->nvars) { + fn->var_scope_lo[c->var_index] = for_lo; + fn->var_scope_hi[c->var_index] = fn->nins; + } + g_cur_scope = for_scope; break; } case STMT_BLOCK: { @@ -6902,7 +6934,11 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, * (codegen). Static/extern decls have no frame slot — skip them. */ int lo = fn->nins; int depth = g_nvla; + int up_scope = g_cur_scope, bs = scope_of_stmt(s); + if (bs >= 0) + g_cur_scope = bs; gen_stmt(fn, s->body, loop); + g_cur_scope = up_scope; vla_release(fn, depth); /* the block's VLAs end with it */ g_nvla = depth; int hi = fn->nins; @@ -6983,42 +7019,78 @@ static void add_dbgvar(struct ir_func *fn, const char *name, int vreg, v->line = line; v->col = col; v->moved = 0; + v->scope = 0; } /* -g: walk the body for block-scope locals. Each STMT_DECL owns a var slot * (var_index); a static local became a global (sglob) and has no frame slot, * so it is skipped. Flattened into the subprogram — lexical-block scoping is a * later refinement, not needed to print a local by name. */ -static void collect_locals(struct ir_func *fn, struct stmt *s) +/* Does this list of statements declare a local of its own (a slot, not an + * extern or a static)? */ +static int declares_local(const struct stmt *s) +{ + for (; s; s = s->next) + if (s->kind == STMT_DECL && !s->is_extern && !s->is_vm_typedef && + !s->sglob) + return 1; + return 0; +} + +/* A new lexical scope for block s, inside scope `up`. */ +static int new_scope(struct ir_func *fn, struct stmt *s, int up) +{ + int id = fn->nscopes++; + fn->scope_up = xrealloc(fn->scope_up, (size_t)fn->nscopes * sizeof *fn->scope_up); + fn->scope_up[id] = up; + if (g_nscope_stmt == g_capscope_stmt) { + g_capscope_stmt = g_capscope_stmt ? g_capscope_stmt * 2 : 16; + g_scope_stmt = xrealloc(g_scope_stmt, (size_t)g_capscope_stmt * sizeof *g_scope_stmt); + g_scope_id = xrealloc(g_scope_id, (size_t)g_capscope_stmt * sizeof *g_scope_id); + } + g_scope_stmt[g_nscope_stmt] = s; + g_scope_id[g_nscope_stmt++] = id; + return id; +} + +static void collect_locals(struct ir_func *fn, struct stmt *s, int scope) { for (; s; s = s->next) { switch (s->kind) { case STMT_DECL: if (s->is_extern || s->is_vm_typedef) /* no local slot at all */ break; - if (!s->sglob) + if (!s->sglob) { add_dbgvar(fn, s->name, s->var_index, 0, fn->src->var_tys[s->var_index], s->line, s->col); + fn->dbgvars[fn->ndbgvars - 1].scope = scope; + } break; case STMT_IF: - collect_locals(fn, s->thn); - collect_locals(fn, s->els); + collect_locals(fn, s->thn, scope); + collect_locals(fn, s->els, scope); break; case STMT_EHREGION: - collect_locals(fn, s->body); - collect_locals(fn, s->thn); + collect_locals(fn, s->body, scope); + collect_locals(fn, s->thn, scope); break; case STMT_WHILE: case STMT_DO: - collect_locals(fn, s->body); + collect_locals(fn, s->body, scope); break; - case STMT_FOR: - collect_locals(fn, s->initdecl); - collect_locals(fn, s->body); + case STMT_FOR: { + /* `for (int i ...)`: i's scope is the for statement */ + int in = declares_local(s->initdecl) ? new_scope(fn, s, scope) : scope; + collect_locals(fn, s->initdecl, in); + collect_locals(fn, s->body, in); break; + } case STMT_BLOCK: + collect_locals(fn, s->body, + declares_local(s->body) ? new_scope(fn, s, scope) : scope); + break; case STMT_SWITCH: - collect_locals(fn, s->body); + collect_locals(fn, s->body, scope); break; default: break; @@ -7194,7 +7266,12 @@ static void gen_func_(struct ir_func *fn, struct func *f) for (int i = 0; i < f->nparams; i++) add_dbgvar(fn, f->params[i], i, 1, f->param_tys[i], f->param_lines[i], f->param_cols[i]); - collect_locals(fn, f->body); + g_nscope_stmt = 0; + g_cur_scope = 0; + fn->nscopes = 1; + fn->scope_up = xmalloc(sizeof *fn->scope_up); + fn->scope_up[0] = -1; + collect_locals(fn, f->body, 0); /* Local scope ranges: default to the whole function ([0, +inf), narrowed to * the real end below) so any local not inside a nested block never coalesces * — the safe default. gen_stmt's STMT_BLOCK case narrows nested-block locals. */ diff --git a/src/opt/inline.c b/src/opt/inline.c index 71e00dd2..0efb4393 100644 --- a/src/opt/inline.c +++ b/src/opt/inline.c @@ -456,6 +456,11 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) * than left as an absent location the verifier cannot tell from a bug. */ lb->synth = 1; + /* The callee's code is the call's, scope and all: its lexical scopes + * are the callee's own, which the caller has no record of (-g). */ + for (int k = 0; k < m; k++) + buf[k].scope = call.scope; + /* 3. Splice buf over the call. */ int newn = fn->nins - 1 + m; struct ir_ins *ni = xmalloc((size_t)newn * sizeof *ni); diff --git a/tests/golden/debug-embedded.sh b/tests/golden/debug-embedded.sh index f3f6a68d..41af373e 100755 --- a/tests/golden/debug-embedded.sh +++ b/tests/golden/debug-embedded.sh @@ -104,7 +104,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # p is the first parameter: fbreg +0, and the prologue stores # the first argument register there. off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) [ -n "$off" ] || { echo "FAIL $t: no location for 'p'"; fail=1; continue; } n=$(printf '%d' "$off") @@ -123,7 +123,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and MIPS, whose frame base is $sp too: p arrives in $4 (a0) o="$out/mipsel-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "sw[[:space:]]+\\\$4,[[:space:]]*0x$(printf '%x' "$(printf '%d' "$off")")\(\\\$sp\)"; then @@ -135,7 +135,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and LoongArch64, whose frame base is $sp (r3): p arrives in a0 o="$out/loongarch64-unknown-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "st\.d[[:space:]]+\\\$a0, \\\$sp, $(printf '%d' "$off")\$"; then @@ -147,7 +147,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and PowerPC, whose frame base is r1: p arrives in r3 o="$out/powerpc-none-eabi.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "stw[[:space:]]+3,[[:space:]]*$(printf '%d' "$off")\(1\)"; then @@ -160,7 +160,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # or not: p arrives in %i0 and the prologue stores it at %fp - n o="$out/sparc-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) # (LLVM 18's llvm-objdump prints the 13-bit offset unsigned: %fp+8172 # for %fp+-0x14) @@ -179,7 +179,7 @@ XOBJDUMP=$XTENSA_REF_DIR/bin/xtensa-esp-elf-objdump if [ -x "$XOBJDUMP" ]; then o="$out/xtensa-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$XOBJDUMP" -d "$o" 2>/dev/null | grep -qE "s32i[[:space:]]+a2, a1, $(printf '%d' "$off")\$"; then @@ -377,7 +377,7 @@ for t in thumbv7m-none-eabi riscv32-unknown-elf mipsel-none-elf \ grep -q "DW_AT_frame_base.*DW_OP_$dw" "$out/al-$t.dw" || { echo "FAIL $t: an alloca function's frame base is not $reg:" grep frame_base "$out/al-$t.dw"; fail=1; continue; } - off=$(grep -A3 'DW_AT_name.*"n"' "$out/al-$t.dw" | + off=$(grep -A5 'DW_AT_name.*"n"' "$out/al-$t.dw" | sed -n 's/.*DW_OP_fbreg +\([0-9]*\).*/\1/p' | head -1) hex=$(printf '0x%x' "$off") # (a zero offset disassembles as `[r7]` and `0(s0)`; LoongArch's is @@ -400,7 +400,7 @@ done t=powerpc-none-eabi if "$EMBCC" --target=$t -g -O0 -c "$out/al.c" -o "$out/al-$t.o" 2>/dev/null; then "$DWDUMP" --debug-info "$out/al-$t.o" > "$out/al-$t.dw" 2>/dev/null - off=$(grep -A3 'DW_AT_name.*"n"' "$out/al-$t.dw" | + off=$(grep -A5 'DW_AT_name.*"n"' "$out/al-$t.dw" | sed -n 's/.*DW_OP_fbreg +\([0-9]*\).*/\1/p' | head -1) grep -q "DW_AT_frame_base.*DW_OP_breg31" "$out/al-$t.dw" && "$OBJDUMP" -d "$out/al-$t.o" 2>/dev/null | diff --git a/tests/golden/embdbg-remote.sh b/tests/golden/embdbg-remote.sh index c8ad8389..8ffde848 100755 --- a/tests/golden/embdbg-remote.sh +++ b/tests/golden/embdbg-remote.sh @@ -191,7 +191,7 @@ CEOF if command -v llvm-dwarfdump >/dev/null 2>&1 && command -v llvm-nm >/dev/null 2>&1; then sym=$(llvm-nm "$d/fwa.elf" | awk '$3 == "acc" { print $1 }') dw=$(llvm-dwarfdump --debug-info "$d/fwa.elf" | - grep -A3 'DW_AT_name ("acc")' | sed -n 's/.*DW_OP_addr 0x\([0-9a-f]*\).*/\1/p') + grep -A5 'DW_AT_name ("acc")' | sed -n 's/.*DW_OP_addr 0x\([0-9a-f]*\).*/\1/p') [ -n "$sym" ] && [ -n "$dw" ] && [ $((0x$dw)) -eq $((0x800000 + 0x$sym)) ] || { printf "avr: acc's DW_OP_addr is 0x%s; it is at 0x%x in SRAM, so 0x%x\n" "$dw" $((0x$sym)) $((0x800000 + 0x$sym)) diff --git a/tests/golden/thumb-codegen.sh b/tests/golden/thumb-codegen.sh index e759f3db..176d402e 100755 --- a/tests/golden/thumb-codegen.sh +++ b/tests/golden/thumb-codegen.sh @@ -323,7 +323,7 @@ if command -v llvm-dwarfdump > /dev/null 2>&1; then llvm-dwarfdump "$out/g.o" > "$out/g.dw" 2>&1 for v in a b s; do # (a location list's entries are on the lines after it) - loc=$(grep -A4 "DW_AT_name (\"$v\")" "$out/g.dw" | + loc=$(grep -A6 "DW_AT_name (\"$v\")" "$out/g.dw" | sed -n '/DW_AT_location/,$p' | tr -s ' \n' ' ') case $o,$loc in -Og,*DW_OP_fbreg*) ;; From 5307253a0b24719ed5fa820847629ef0fa1ae6be Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:10:55 +0100 Subject: [PATCH 3/9] tests: optimized code's DWARF, checked by debugging against clang's 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. --- .github/workflows/ci.yml | 2 +- tests/golden/debug-optimized.sh | 94 +++++++++++++++++++++++++++ tests/golden/debug-optimized/cmp.py | 42 ++++++++++++ tests/golden/debug-optimized/prog.c | 92 ++++++++++++++++++++++++++ tests/golden/debug-optimized/trace.py | 84 ++++++++++++++++++++++++ 5 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 tests/golden/debug-optimized.sh create mode 100644 tests/golden/debug-optimized/cmp.py create mode 100644 tests/golden/debug-optimized/prog.c create mode 100644 tests/golden/debug-optimized/trace.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c842a42c..de919367 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -212,7 +212,7 @@ jobs: sudo apt-get install -y --no-install-recommends \ qemu-system-arm qemu-system-misc qemu-system-mips \ qemu-system-ppc qemu-system-sparc qemu-system-x86 \ - ${{ matrix.qemu-riscv }} libglib2.0-dev \ + ${{ matrix.qemu-riscv }} libglib2.0-dev gdb-multiarch \ clang-${{ matrix.llvm }} llvm-${{ matrix.llvm }} lld-${{ matrix.llvm }} echo "/usr/lib/llvm-${{ matrix.llvm }}/bin" >> "$GITHUB_PATH" # The TriCore harness's output and the instruction counts are QEMU diff --git a/tests/golden/debug-optimized.sh b/tests/golden/debug-optimized.sh new file mode 100644 index 00000000..b90aa07b --- /dev/null +++ b/tests/golden/debug-optimized.sh @@ -0,0 +1,94 @@ +#!/bin/sh +# -g at -O2 and -Os, proven by DEBUGGING against a reference: the same +# program, built by EmbCC at -O0, -O2 and -Os and by clang at -O0, runs on +# QEMU's boards under gdb, which stops on every line and reads every +# parameter and local in scope (debug-optimized/trace.py). Every value +# EmbCC's DWARF gives must be the one clang's -O0 build shows at the same +# visit of the same line (debug-optimized/cmp.py): a location list that +# names the wrong register, a value that went stale, or a variable shown +# outside its block -- the second of two `for (int i ...)` loops showing +# the first's i, a block's `s` hiding the function's own after the block +# -- is a failure. What the optimized code does not keep is and counts as unavailable, not wrong; the share available is +# printed. A local is not compared up to its declaration's line (the +# DWARF scope is its block, C's starts at the declarator), and a line +# visited a different number of times in the two builds (a rotated loop's +# test) is not compared at all. +# +# clang's -O0 is the reference because a build of EmbCC's own can be +# wrong the same way at every level: before lexical blocks, -O0 and -O2 +# both showed the first loop's i in the second loop, and agreed. +# +# Skips honestly without a gdb that has Python and the board's +# architecture (gdb-multiarch), clang, or the board's QEMU. +set -u +echo "TEST-MARKER debug-optimized" +. "$(dirname "$0")/../lib.sh" + +EMBCC=${EMBCC:-./embcc} +here=$EMBCC_ROOT/tests/golden/debug-optimized +out=$EMBCC_ROOT/tests/golden/out/debug-optimized +rm -rf "$out"; mkdir -p "$out" +GDB=${EMBCC_GDB:-} +if [ -z "$GDB" ]; then + for g in gdb-multiarch gdb; do + command -v $g >/dev/null 2>&1 && + $g -batch -nx -ex "python import gdb" -ex "set architecture armv7e-m" \ + >/dev/null 2>&1 && { GDB=$g; break; } + done +fi +[ -n "$GDB" ] || { echo "skipped: no gdb with Python and ARM (gdb-multiarch)"; exit 0; } +command -v clang >/dev/null 2>&1 || { echo "skipped: no clang for the reference"; exit 0; } + +fail=0 ran=0 +board() { # NAME TRIPLE HARNESS HARNESS-VAR GDB-ARCH "CLANG-FLAGS" QEMU... + b=$1 X=$2 H=$3 HV=$4 A=$5 CF=$6; shift 6 + command -v "$1" >/dev/null 2>&1 || { echo " $b: skipped ($1 absent)"; return; } + $GDB -batch -nx -ex "set architecture $A" >/dev/null 2>&1 || + { echo " $b: skipped ($GDB has no $A)"; return; } + d=$out/$b; mkdir -p "$d" + for f in boot io; do + "$EMBCC" --target=$X -c tests/harness/$H/$f.c -o "$d/$f.o" || { fail=1; return; } + done + for o in clang -O0 -O2 -Os; do + if [ $o = clang ]; then + # shellcheck disable=SC2086 + clang --target=$X $CF -O0 -g -ffreestanding -c "$here/prog.c" -o "$d/prog$o.o" + else + "$EMBCC" --target=$X $o -g -c "$here/prog.c" -o "$d/prog$o.o" + fi || { echo " $b $o: does not compile"; fail=1; continue; } + env $HV="$d" sh tests/harness/$H/link.sh "$d/prog$o.elf" "$d/prog$o.o" \ + build/libc/$X/librt.a > "$d/$o.ld" 2>&1 || + { echo " $b $o: does not link"; head -3 "$d/$o.ld"; fail=1; continue; } + port=$((20000 + ($$ * 7 + ran) % 20000)) + "$@" -nographic -kernel "$d/prog$o.elf" -S -gdb tcp::$port > "$d/$o.qemu" 2>&1 & + q=$! + sleep 1 + TRACE_PORT=$port TRACE_SRC=prog.c TRACE_LINES="1-$(wc -l < "$here/prog.c")" \ + TRACE_OUT="$d/$o.jsonl" TRACE_MAX=3000 \ + $GDB -batch -nx -ex "set architecture $A" -ex "file $d/prog$o.elf" \ + -x "$here/trace.py" > "$d/$o.gdb" 2>&1 + kill $q 2>/dev/null; wait $q 2>/dev/null + ran=$((ran + 1)) + done + for o in -O0 -O2 -Os; do + [ -s "$d/clang.jsonl" ] && [ -s "$d/$o.jsonl" ] || + { echo " $b $o: no trace"; fail=1; continue; } + r=$(python3 "$here/cmp.py" "$d/clang.jsonl" "$d/$o.jsonl") + echo " $b $o: $(echo "$r" | head -1 | sed 's/^[0-9]* stops at -O2, //')" + case $(echo "$r" | head -1) in + *", 0 DISAGREE;"*) ;; + *) echo "$r" | sed -n '2,12p'; fail=1 ;; + esac + done +} +board m3 thumbv7m-none-eabi thumb EMBCC_THUMB_HARNESS armv7e-m \ + "-mcpu=cortex-m3 -mfloat-abi=soft" qemu-system-arm -M lm3s6965evb -cpu cortex-m3 +board m0 thumbv6m-none-eabi thumb-m0 EMBCC_THUMB_M0_HARNESS armv6-m \ + "-mcpu=cortex-m0 -mfloat-abi=soft" qemu-system-arm -M microbit +board rv32 riscv32-unknown-elf riscv EMBCC_RISCV_HARNESS riscv:rv32 \ + "-march=rv32imac -mabi=ilp32 -mno-relax" qemu-system-riscv32 -M virt -bios none -m 8 +# (not MIPS yet: gdb 17 maps no address of clang's MIPS image to a line, +# though llvm-dwarfdump reads the table, so there is no reference there) +[ $fail = 0 ] || { echo "FAIL: debug-optimized"; exit 1; } +echo "debug-optimized: every value EmbCC's DWARF gives at -O0, -O2 and -Os is clang's -O0 build's" diff --git a/tests/golden/debug-optimized/cmp.py b/tests/golden/debug-optimized/cmp.py new file mode 100644 index 00000000..40aa4099 --- /dev/null +++ b/tests/golden/debug-optimized/cmp.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""cmp.py REF.jsonl GOT.jsonl [-v] -- every value GOT's debug info gives must +be the unoptimized build's at the same visit of the same line. Prints stops, how many values +the optimized build makes available, and every disagreement.""" +import json, sys +def load(p): + r = {} + for l in open(p): + j = json.loads(l); r[(j["key"], j["k"])] = j + return r +a, b = load(sys.argv[1]), load(sys.argv[2]) +verbose = "-v" in sys.argv +cnt0 = {}; cnt2 = {} +for (k, n) in a: cnt0[k] = cnt0.get(k, 0) + 1 +for (k, n) in b: cnt2[k] = cnt2.get(k, 0) + 1 +avail = total = agree = 0; bad = []; unaligned = set() +for key, j in sorted(b.items(), key=lambda kv: kv[1]["pc"]): + line = key[0] + if cnt0.get(line) != cnt2.get(line): + unaligned.add(line); continue + ref = a.get(key) + if not ref: continue + for v, s in j["vals"].items(): + # up to its declaration's line a local holds nothing yet: its block + # is its scope in the DWARF, but C's starts at the declarator + d = ref.get("decl", {}).get(v) or j.get("decl", {}).get(v) or 0 + if d and j.get("line", 0) <= d: + continue + total += 1 + if s is None: continue + avail += 1 + r = ref["vals"].get(v) + if r == s: agree += 1 + else: bad.append((key[0], key[1], v, s, r)) +print("%d stops at -O2, %d aligned with -O0; %d values, %d available (%.0f%%), %d agree, %d DISAGREE; %d lines visited a different number of times" + % (len(b), sum(1 for k in b if k[0] not in unaligned), total, avail, 100.0 * avail / total if total else 0, agree, len(bad), len(unaligned))) +from collections import Counter +c = Counter((x[0], x[2]) for x in bad) +for (line, v), n in sorted(c.items()): + ex = [x for x in bad if x[0] == line and x[2] == v][0] + print(" %-14s %-6s x%-3d e.g. visit %d: -O2 says %s, -O0 has %s" % (line, v, n, ex[1], ex[3], ex[4])) +if verbose: print("unaligned:", sorted(unaligned)) diff --git a/tests/golden/debug-optimized/prog.c b/tests/golden/debug-optimized/prog.c new file mode 100644 index 00000000..2afff9ad --- /dev/null +++ b/tests/golden/debug-optimized/prog.c @@ -0,0 +1,92 @@ +void puts_(const char *s); +void putn(long v); + +struct acc { int sum; int n; unsigned max; }; + +__attribute__((noinline)) static unsigned crc8(const unsigned char *p, int len) +{ + unsigned crc = 0xff; + for (int i = 0; i < len; i++) { + unsigned b = p[i]; + crc ^= b; + for (int k = 0; k < 8; k++) { + unsigned top = crc & 0x80; + crc = (crc << 1) & 0xff; + if (top) + crc ^= 0x31; + } + } + return crc; +} + +__attribute__((noinline)) static void add(struct acc *a, int v) +{ + int old = a->sum; + a->sum = old + v; + a->n++; + if ((unsigned)v > a->max) + a->max = (unsigned)v; +} + +__attribute__((noinline)) static long long mix(long long x, int s, short t) +{ + long long y = x * 3 + s; + int z = t * 7 - s; + y ^= (long long)z << 5; + return y + z; +} + +__attribute__((noinline)) static int search(const int *v, int n, int want) +{ + int lo = 0, hi = n - 1; + while (lo <= hi) { + int mid = (lo + hi) / 2; + int got = v[mid]; + if (got == want) + return mid; + if (got < want) + lo = mid + 1; + else + hi = mid - 1; + } + return -1; +} + +/* names declared again in sibling and inner blocks */ +__attribute__((noinline)) static int shadow(int n) +{ + int s = 0; + for (int i = 0; i < n; i++) + s += i * 3; + for (int i = n; i > 0; i--) { + int t = i + s; + s ^= t; + } + { + int s = n * 11; + n += s; + } + return s + n; +} + +static const unsigned char msg[] = "hello, location lists"; +static const int sorted[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + +int main(void) +{ + struct acc a = { 0, 0, 0 }; + unsigned c = crc8(msg, (int)sizeof msg - 1); + for (int i = 0; i < 5; i++) + add(&a, (int)c - i * 40); + long long m = mix(0x123456789LL, a.sum, (short)a.n); + int where = search(sorted, 11, 19); + int none = search(sorted, 11, 4); + int sh = shadow(6); + putn((long)c); + putn(a.sum); + putn((long)(m & 0xffff)); + putn(where + none); + putn(sh); + puts_("\n==END==\n"); + return 0; +} diff --git a/tests/golden/debug-optimized/trace.py b/tests/golden/debug-optimized/trace.py new file mode 100644 index 00000000..4c43d57e --- /dev/null +++ b/tests/golden/debug-optimized/trace.py @@ -0,0 +1,84 @@ +# gdb -batch -x trace.py, with TRACE_PORT, TRACE_SRC (a basename), TRACE_LINES +# ("12,13,20-31"), TRACE_OUT and TRACE_MAX in the environment: connect to the +# stub, break on each of those lines of the source, and at every stop record +# the function, the line, which visit of that line it is, and every parameter +# and local in scope -- its value as gdb prints it, or None when gdb says it is +# optimized out. Written as JSON lines to TRACE_OUT. +import gdb, json, os + +port = os.environ["TRACE_PORT"] +src = os.environ["TRACE_SRC"] +mx = int(os.environ.get("TRACE_MAX", "4000")) +lines = [] +for part in os.environ["TRACE_LINES"].split(","): + if "-" in part: + a, b = part.split("-") + lines += range(int(a), int(b) + 1) + elif part: + lines.append(int(part)) + +gdb.execute("set pagination off") +gdb.execute("set confirm off") +gdb.execute("set print pretty off") +gdb.execute("set print address on") +gdb.execute("target remote :" + port) +for l in lines: + try: + gdb.execute("break %s:%d" % (src, l), to_string=True) + except gdb.error: + pass +# where a harness goes once main returns: the trace ends there (a board that +# then waits forever would never stop again) +for f in ("_exit", "exit", "abort"): + try: + gdb.execute("break " + f, to_string=True) + except gdb.error: + pass + +out = open(os.environ["TRACE_OUT"], "w") +seen = {} +for n in range(mx): + try: + gdb.execute("continue", to_string=True) + except gdb.error: + break + try: + f = gdb.selected_frame() + except gdb.error: + break + sal = f.find_sal() + if not sal.symtab or os.path.basename(sal.symtab.filename) != src: + break + fn = f.name() + key = "%s:%d" % (fn, sal.line) + k = seen.get(key, 0) + seen[key] = k + 1 + vals = {} + decl = {} + try: + blk = f.block() + except RuntimeError: + blk = None + while blk is not None: + for sym in blk: + if not (sym.is_variable or sym.is_argument) or sym.name in vals: + continue + decl[sym.name] = 0 if sym.is_argument else sym.line + try: + v = sym.value(f) + if v.is_optimized_out: + vals[sym.name] = None + else: + ptr = v.type.strip_typedefs().code == gdb.TYPE_CODE_PTR + s = str(v) + if ptr: # stack addresses differ between builds: null or not + s = "ptr:" + ("null" if int(v) == 0 else "set") + vals[sym.name] = s + except Exception as e: + vals[sym.name] = "ERR " + str(e)[:60] + if blk.function is not None: + break + blk = blk.superblock + out.write(json.dumps({"key": key, "line": sal.line, "k": k, "pc": int(f.pc()), + "vals": vals, "decl": decl}) + "\n") +out.close() From 403287064fe31ffa0e26f2c04c0ef6ab9e0fb81b Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:38:50 +0100 Subject: [PATCH 4/9] dwarf: an inlined call is a DW_TAG_inlined_subroutine 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. --- src/debug/dwarf.c | 164 +++++++++++++++++++++++++++++++++++++++++++++- src/ir/ir.h | 10 +++ src/ir/irgen.c | 2 + src/opt/inline.c | 64 +++++++++++++++++- 4 files changed, 236 insertions(+), 4 deletions(-) diff --git a/src/debug/dwarf.c b/src/debug/dwarf.c index 4452cd9a..7162453b 100644 --- a/src/debug/dwarf.c +++ b/src/debug/dwarf.c @@ -19,6 +19,7 @@ #define DW_TAG_member 0x0d #define DW_TAG_subrange_type 0x21 #define DW_TAG_lexical_block 0x0b +#define DW_TAG_inlined_subroutine 0x1d #define DW_CHILDREN_no 0x00 #define DW_CHILDREN_yes 0x01 #define DW_AT_name 0x03 @@ -40,6 +41,11 @@ #define DW_AT_data_bit_offset 0x6b #define DW_AT_decl_file 0x3a #define DW_AT_decl_line 0x3b +#define DW_AT_inline 0x20 +#define DW_AT_abstract_origin 0x31 +#define DW_AT_call_column 0x57 +#define DW_AT_call_file 0x58 +#define DW_AT_call_line 0x59 #define DW_FORM_addr 0x01 #define DW_FORM_block1 0x0a #define DW_FORM_data1 0x0b @@ -153,6 +159,17 @@ #define AB_VAR_T_LL 23 /* variable, with type, location list */ #define AB_VAR_LL 24 #define AB_LEXBLOCK 25 /* lexical_block: a nested scope's code */ +#define AB_ABSFN 26 /* subprogram: an inlined function's abstract instance */ +#define AB_ABSFN_T 27 /* ...with a return type */ +#define AB_ABSPARAM_T 28 /* its parameter, with type, no location */ +#define AB_ABSPARAM 29 +#define AB_ABSVAR_T 30 /* its local */ +#define AB_ABSVAR 31 +#define AB_INLSUB 32 /* inlined_subroutine: one inlined call */ +#define AB_IPARAM 33 /* its parameter: the abstract one's, and where */ +#define AB_IPARAM_LL 34 +#define AB_IVAR 35 +#define AB_IVAR_LL 36 static int dw_ver = 5; @@ -445,6 +462,45 @@ static void emit_abbrev(struct dbuf *b) lexb[3] = DW_FORM_data4; } one_abbrev(b, AB_LEXBLOCK, DW_TAG_lexical_block, DW_CHILDREN_yes, lexb, 2); + /* an inlined function's abstract instance (3.3.8.1): its name, that + * it was inlined, and its parameters and locals without locations */ + static const unsigned char absfn[] = { + DW_AT_name, DW_FORM_string, DW_AT_inline, DW_FORM_data1 }; + static const unsigned char absfn_t[] = { + DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_inline, DW_FORM_data1 }; + static const unsigned char absv_t[] = { + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4 }; + static const unsigned char absv[] = { + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata }; + one_abbrev(b, AB_ABSFN, DW_TAG_subprogram, DW_CHILDREN_yes, absfn, 2); + one_abbrev(b, AB_ABSFN_T, DW_TAG_subprogram, DW_CHILDREN_yes, absfn_t, 3); + one_abbrev(b, AB_ABSPARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, absv_t, 4); + one_abbrev(b, AB_ABSPARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, absv, 3); + one_abbrev(b, AB_ABSVAR_T, DW_TAG_variable, DW_CHILDREN_no, absv_t, 4); + one_abbrev(b, AB_ABSVAR, DW_TAG_variable, DW_CHILDREN_no, absv, 3); + /* one inlined call: what was inlined, its code, where it was called */ + unsigned char inl[] = { + DW_AT_abstract_origin, DW_FORM_ref4, DW_AT_low_pc, DW_FORM_addr, + DW_AT_high_pc, DW_FORM_addr, DW_AT_call_file, DW_FORM_data1, + DW_AT_call_line, DW_FORM_udata, DW_AT_call_column, DW_FORM_udata }; + if (dw_split) { + inl[3] = DW_FORM_addrx; + inl[5] = DW_FORM_data4; + } + one_abbrev(b, AB_INLSUB, DW_TAG_inlined_subroutine, DW_CHILDREN_yes, inl, 6); + unsigned char iv[] = { DW_AT_abstract_origin, DW_FORM_ref4, + DW_AT_location, DW_FORM_exprloc }; + unsigned char iv_ll[] = { DW_AT_abstract_origin, DW_FORM_ref4, + DW_AT_location, DW_FORM_sec_offset }; + if (dw_split) + iv_ll[3] = DW_FORM_loclistx; + one_abbrev(b, AB_IPARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, iv, 2); + one_abbrev(b, AB_IPARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, iv_ll, 2); + one_abbrev(b, AB_IVAR, DW_TAG_variable, DW_CHILDREN_no, iv, 2); + one_abbrev(b, AB_IVAR_LL, DW_TAG_variable, DW_CHILDREN_no, iv_ll, 2); db_uleb(b, 0); /* end of the abbrev table */ } @@ -814,6 +870,88 @@ static int list_header(struct dbuf *b) return at; } +/* The abstract instance of each function inlined somewhere in the unit: + * its DIE's offset, and its own variables' (by dbgvars index; -1 for one + * that came from a call inlined into it). */ +struct absfn { const struct func *f; int off; int *voff; int nv; }; +static struct absfn *dw_abs; +static int dw_nabs; + +static const struct absfn *abs_of(const struct func *f) +{ + for (int k = 0; k < dw_nabs; k++) + if (dw_abs[k].f == f) + return &dw_abs[k]; + return NULL; +} + +/* One abstract instance per function some call to it was inlined: a + * DW_TAG_subprogram with DW_AT_inline and its parameters and locals, + * none with a location. Each inlined call's DIE refers to it, so gdb + * names the call and shows its frame in a backtrace. CU children, before + * the subprograms, as the types are. */ +static void emit_abstracts(struct dbuf *b, struct typemap *tm, + struct ir_unit *iu) +{ + free(dw_abs); + dw_abs = NULL; + dw_nabs = 0; + for (int n = 0; n < iu->nfuncs; n++) { + struct ir_func *fn = &iu->funcs[n]; + if (!fn->scope_fn) + continue; + for (int s = 1; s < fn->nscopes; s++) { + const struct func *f = fn->scope_fn[s]; + if (!f || abs_of(f)) + continue; + /* the inlined function's own variables, from the copies of + * them its calls left in the callers (the function itself is + * often gone, every call inlined): by their index there */ + int nv = 0; + for (int m = 0; m < iu->nfuncs; m++) + for (int v = 0; v < iu->funcs[m].ndbgvars; v++) { + const struct ir_dbgvar *dv = &iu->funcs[m].dbgvars[v]; + if (dv->origin == f && dv->origin_idx >= nv) + nv = dv->origin_idx + 1; + } + const struct ir_dbgvar **pick = xcalloc((size_t)(nv ? nv : 1), sizeof *pick); + for (int m = 0; m < iu->nfuncs; m++) + for (int v = 0; v < iu->funcs[m].ndbgvars; v++) { + const struct ir_dbgvar *dv = &iu->funcs[m].dbgvars[v]; + if (dv->origin == f && dv->origin_idx >= 0 && !pick[dv->origin_idx]) + pick[dv->origin_idx] = dv; + } + dw_abs = xrealloc(dw_abs, (size_t)(dw_nabs + 1) * sizeof *dw_abs); + struct absfn *A = &dw_abs[dw_nabs++]; + A->f = f; + A->off = b->len; + A->nv = nv; + A->voff = xmalloc((size_t)(nv ? nv : 1) * sizeof *A->voff); + int rt = type_lookup(tm, f->ret_ty); + db_uleb(b, rt >= 0 ? AB_ABSFN_T : AB_ABSFN); + db_str(b, f->name); + if (rt >= 0) db_u32(b, (unsigned long)rt); + db_u8(b, 1); /* DW_INL_inlined */ + for (int v = 0; v < nv; v++) { + const struct ir_dbgvar *dv = pick[v]; + A->voff[v] = -1; + if (!dv) + continue; + int toff = type_lookup(tm, dv->ty); + A->voff[v] = b->len; + db_uleb(b, dv->is_param ? (toff >= 0 ? AB_ABSPARAM_T : AB_ABSPARAM) + : (toff >= 0 ? AB_ABSVAR_T : AB_ABSVAR)); + db_str(b, dv->name); + db_u8(b, 1); + db_uleb(b, (unsigned long)(dv->line > 0 ? dv->line : 0)); + if (toff >= 0) db_u32(b, (unsigned long)toff); + } + db_u8(b, 0); + free(pick); + } + } +} + /* One parameter's or local's DIE: its name, where it was declared, its * type, and its location -- a frame slot, or where the optimizer keeps it, * range by range (ra_debug_locs). */ @@ -824,6 +962,17 @@ static void emit_dbgvar(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, int toff = type_lookup(tm, dv->ty); int ll = fn->ndbglocs && fn->ndbglocs[v] > 0 && (fn->var_off[dv->vreg] == IR_VAR_NO_LOC || dv->moved); + const struct absfn *A = dv->origin ? abs_of(dv->origin) : NULL; + int ao = A && dv->origin_idx >= 0 && dv->origin_idx < A->nv + ? A->voff[dv->origin_idx] : -1; + if (ao >= 0) { + /* an inlined call's: the abstract one's name and type, and where + * it is in this copy */ + db_uleb(b, dv->is_param ? (ll ? AB_IPARAM_LL : AB_IPARAM) + : (ll ? AB_IVAR_LL : AB_IVAR)); + db_u32(b, (unsigned long)ao); + goto location; + } int code = dv->is_param ? (toff >= 0 ? (ll ? AB_PARAM_T_LL : AB_PARAM_T) : (ll ? AB_PARAM_LL : AB_PARAM)) @@ -834,6 +983,7 @@ static void emit_dbgvar(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, db_u8(b, 1); /* decl_file */ db_uleb(b, (unsigned long)(dv->line > 0 ? dv->line : 0)); if (toff >= 0) db_u32(b, (unsigned long)toff); +location: if (ll && dw_split) { if (dw_nll == dw_capll) { dw_capll = dw_capll ? dw_capll * 2 : 16; @@ -870,8 +1020,14 @@ static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, struct typemap *tm, struct ir_func *fn, long lo, int s, const long *smin, const long *smax) { - if (s > 0) { + const struct absfn *A = s > 0 && fn->scope_fn ? abs_of(fn->scope_fn[s]) : NULL; + if (A) { + db_uleb(b, AB_INLSUB); + db_u32(b, (unsigned long)A->off); + } else if (s > 0) { db_uleb(b, AB_LEXBLOCK); + } + if (s > 0) { if (dw_split) { db_uleb(b, (unsigned long)addr_index(DWTGT_TEXT, smin[s], NULL, 0)); db_u32(b, (unsigned long)(smax[s] - smin[s])); @@ -882,6 +1038,11 @@ static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, db_addr(b, 0); } } + if (A) { + db_u8(b, 1); /* call_file */ + db_uleb(b, (unsigned long)(fn->scope_cline[s] > 0 ? fn->scope_cline[s] : 0)); + db_uleb(b, (unsigned long)(fn->scope_ccol[s] > 0 ? fn->scope_ccol[s] : 0)); + } for (int v = 0; v < fn->ndbgvars; v++) { int vs = fn->dbgvars[v].scope; if (vs < 0 && smin != NULL) @@ -1006,6 +1167,7 @@ static void emit_info(struct dwarf_out *out, struct dbuf *b, (void)ensure_type(b, &tm, iu->funcs[n].dbgvars[v].ty); } emit_globals(out, b, &tm, iu); + emit_abstracts(b, &tm, iu); for (int n = 0; n < iu->nfuncs; n++) { struct ir_func *fn = &iu->funcs[n]; diff --git a/src/ir/ir.h b/src/ir/ir.h index e8e448a6..a1d14b77 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -542,6 +542,11 @@ struct ir_dbgvar { struct type *ty; int line, col; int scope; /* its block's lexical scope (ir_ins.scope) */ + /* A variable of an inlined call: the function it was declared in and + * its index among that function's dbgvars (the DWARF's abstract + * origin); NULL and -1 for the function's own. */ + struct func *origin; + int origin_idx; /* mem2reg took this variable out of memory although something * assigned it: its slot no longer follows it. Its only reader is a * backend deciding whether the slot is a true DW_AT_location -- for a @@ -649,6 +654,11 @@ struct ir_func { * code -- which, unlike var_scope_lo/hi, no reordering makes stale. */ int *scope_up; int nscopes; + /* ...and where a scope is an inlined call (the inliner), the function + * inlined there and the call's line and column; NULL and 0 for a + * block. Each array nscopes long, or NULL while no call is inlined. */ + struct func **scope_fn; + int *scope_cline, *scope_ccol; /* exception regions (C++'s lowering), the catch types their landing * pads' selectors number (1-based; NULL: catch-all), and — codegen — * every call's code */ diff --git a/src/ir/irgen.c b/src/ir/irgen.c index 642caa8a..94297937 100644 --- a/src/ir/irgen.c +++ b/src/ir/irgen.c @@ -7020,6 +7020,8 @@ static void add_dbgvar(struct ir_func *fn, const char *name, int vreg, v->col = col; v->moved = 0; v->scope = 0; + v->origin = NULL; + v->origin_idx = -1; } /* -g: walk the body for block-scope locals. Each STMT_DECL owns a var slot diff --git a/src/opt/inline.c b/src/opt/inline.c index 0efb4393..7e25d9bf 100644 --- a/src/opt/inline.c +++ b/src/opt/inline.c @@ -456,10 +456,44 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) * than left as an absent location the verifier cannot tell from a bug. */ lb->synth = 1; - /* The callee's code is the call's, scope and all: its lexical scopes - * are the callee's own, which the caller has no record of (-g). */ + /* -g: the callee's code is an inlined call, a scope of the caller's + * inside the call's, and the callee's own scopes inside that one -- + * its blocks, and the calls inlined into it before -- renumbered from + * `base`. The DWARF writer makes it a DW_TAG_inlined_subroutine. */ + int base = -1; + if (fn->scope_up && cf->scope_up && cf->nscopes > 0) { + base = fn->nscopes; + int ns = base + cf->nscopes; + int had = fn->scope_fn != NULL; + fn->scope_up = xrealloc(fn->scope_up, (size_t)ns * sizeof *fn->scope_up); + fn->scope_fn = xrealloc(fn->scope_fn, (size_t)ns * sizeof *fn->scope_fn); + fn->scope_cline = xrealloc(fn->scope_cline, (size_t)ns * sizeof *fn->scope_cline); + fn->scope_ccol = xrealloc(fn->scope_ccol, (size_t)ns * sizeof *fn->scope_ccol); + if (!had) + for (int t = 0; t < base; t++) { + fn->scope_fn[t] = NULL; + fn->scope_cline[t] = fn->scope_ccol[t] = 0; + } + for (int t = 0; t < cf->nscopes; t++) { + int id = base + t; + if (t == 0) { + fn->scope_up[id] = call.scope; + fn->scope_fn[id] = cf->src; + fn->scope_cline[id] = call.line; + fn->scope_ccol[id] = call.col; + } else { + fn->scope_up[id] = base + cf->scope_up[t]; + fn->scope_fn[id] = cf->scope_fn ? cf->scope_fn[t] : NULL; + fn->scope_cline[id] = cf->scope_cline ? cf->scope_cline[t] : 0; + fn->scope_ccol[id] = cf->scope_ccol ? cf->scope_ccol[t] : 0; + } + } + fn->nscopes = ns; + } for (int k = 0; k < m; k++) - buf[k].scope = call.scope; + buf[k].scope = base < 0 ? call.scope + : base + (buf[k].scope > 0 && buf[k].scope < cf->nscopes + ? buf[k].scope : 0); /* 3. Splice buf over the call. */ int newn = fn->nins - 1 + m; @@ -515,6 +549,30 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) fn->var_scope_lo = lo; fn->var_scope_hi = hi; } fn->nvars = nv; + + /* -g: the callee's parameters and locals are the caller's slots V + k + * now, so the caller's own mem2reg follows them (vreg_var) and they get + * location lists like any local; each in its inlined scope, its origin + * the callee's variable (or, inlined into the callee before, that + * one's own). */ + if (base >= 0) + for (int d = 0; d < cf->ndbgvars; d++) { + struct ir_dbgvar x = cf->dbgvars[d]; + if (x.vreg < 0 || x.vreg >= v) + continue; + x.vreg = V + x.vreg; + x.scope = base + (x.scope > 0 && x.scope < cf->nscopes ? x.scope : 0); + if (!x.origin) { + x.origin = cf->src; + x.origin_idx = d; + } + if (fn->ndbgvars == fn->dbgvarcap) { + fn->dbgvarcap = fn->dbgvarcap ? fn->dbgvarcap * 2 : 8; + fn->dbgvars = xrealloc(fn->dbgvars, + (size_t)fn->dbgvarcap * sizeof *fn->dbgvars); + } + fn->dbgvars[fn->ndbgvars++] = x; + } } /* How many calls in the whole unit name this function, and does its From 6e4676d8432ffc7c13a2b11334ee737b113d8798 Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:38:50 +0100 Subject: [PATCH 5/9] opt: main is never inlined 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. --- src/opt/inline.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/opt/inline.c b/src/opt/inline.c index 7e25d9bf..b2775d1a 100644 --- a/src/opt/inline.c +++ b/src/opt/inline.c @@ -240,6 +240,11 @@ static int inlinable(struct ir_func *cf, int force, int sole, const char **why, detail[0] = 0; if (c->is_varargs) { *why = "callee-is-varargs"; return 0; } + /* main stays a frame of its own, as GCC keeps it: called once, from a + * reset handler or crt0, it gains nothing inlined, and inlined it is + * an inline frame of its caller -- gdb's backtrace then runs past the + * entry function into 0x0, and `break main` stops in reset's code */ + if (cf->name && !strcmp(cf->name, "main")) { *why = "callee-is-main"; return 0; } if (cf->nins == 0) { *why = "callee-not-defined-here"; return 0; } /* __attribute__((always_inline)) overrides the SIZE budget and * nothing else. Every other test below is a thing this inliner From 461fb2e024f560053d6ea2ea94c2e99941ed1e40 Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:38:50 +0100 Subject: [PATCH 6/9] tests: debug-optimized pairs stops by call, and checks inlined frames 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). --- tests/golden/debug-optimized.sh | 14 ++-- tests/golden/debug-optimized/cmp.py | 105 +++++++++++++++++--------- tests/golden/debug-optimized/prog.c | 15 ++++ tests/golden/debug-optimized/trace.py | 16 +++- 4 files changed, 109 insertions(+), 41 deletions(-) diff --git a/tests/golden/debug-optimized.sh b/tests/golden/debug-optimized.sh index b90aa07b..e3c1ab4a 100644 --- a/tests/golden/debug-optimized.sh +++ b/tests/golden/debug-optimized.sh @@ -4,7 +4,10 @@ # QEMU's boards under gdb, which stops on every line and reads every # parameter and local in scope (debug-optimized/trace.py). Every value # EmbCC's DWARF gives must be the one clang's -O0 build shows at the same -# visit of the same line (debug-optimized/cmp.py): a location list that +# stop -- the same call of each function on the stack, the same line, the +# same visit of it within that call (debug-optimized/cmp.py). A call +# EmbCC inlined is a frame of its own to gdb (DW_TAG_inlined_subroutine), +# so its stops pair with clang's real call. A location list that # names the wrong register, a value that went stale, or a variable shown # outside its block -- the second of two `for (int i ...)` loops showing # the first's i, a block's `s` hiding the function's own after the block @@ -12,8 +15,9 @@ # out> and counts as unavailable, not wrong; the share available is # printed. A local is not compared up to its declaration's line (the # DWARF scope is its block, C's starts at the declarator), and a line -# visited a different number of times in the two builds (a rotated loop's -# test) is not compared at all. +# visited a different number of times within one call (a rotated loop's +# test, which the optimized line table gives several addresses) is not +# compared in that call. # # clang's -O0 is the reference because a build of EmbCC's own can be # wrong the same way at every level: before lexical blocks, -O0 and -O2 @@ -75,9 +79,9 @@ board() { # NAME TRIPLE HARNESS HARNESS-VAR GDB-ARCH "CLANG-FLAGS" QEMU... [ -s "$d/clang.jsonl" ] && [ -s "$d/$o.jsonl" ] || { echo " $b $o: no trace"; fail=1; continue; } r=$(python3 "$here/cmp.py" "$d/clang.jsonl" "$d/$o.jsonl") - echo " $b $o: $(echo "$r" | head -1 | sed 's/^[0-9]* stops at -O2, //')" + echo " $b $o: $(echo "$r" | head -1)" case $(echo "$r" | head -1) in - *", 0 DISAGREE;"*) ;; + *", 0 DISAGREE") ;; *) echo "$r" | sed -n '2,12p'; fail=1 ;; esac done diff --git a/tests/golden/debug-optimized/cmp.py b/tests/golden/debug-optimized/cmp.py index 40aa4099..bc9d909c 100644 --- a/tests/golden/debug-optimized/cmp.py +++ b/tests/golden/debug-optimized/cmp.py @@ -1,42 +1,77 @@ #!/usr/bin/env python3 -"""cmp.py REF.jsonl GOT.jsonl [-v] -- every value GOT's debug info gives must -be the unoptimized build's at the same visit of the same line. Prints stops, how many values -the optimized build makes available, and every disagreement.""" +"""cmp.py REF.jsonl GOT.jsonl -- every value GOT's debug info gives must be REF's at the same +stop. A stop is named by its invocation -- which call of each function on its stack -- its line, +and which visit of that line within the innermost call. A line visited a different number of +times within one call in the two builds (a rotated loop's test) is not compared in that call. + +A stop inside a call GOT inlined (gdb's inline frame) cannot be told apart from the next call +of the same function when no stop comes between: there the value must be one REF shows for that +variable at that line in some call made from the same caller invocation.""" import json, sys +from collections import Counter + def load(p): - r = {} - for l in open(p): - j = json.loads(l); r[(j["key"], j["k"])] = j - return r -a, b = load(sys.argv[1]), load(sys.argv[2]) -verbose = "-v" in sys.argv -cnt0 = {}; cnt2 = {} -for (k, n) in a: cnt0[k] = cnt0.get(k, 0) + 1 -for (k, n) in b: cnt2[k] = cnt2.get(k, 0) + 1 -avail = total = agree = 0; bad = []; unaligned = set() -for key, j in sorted(b.items(), key=lambda kv: kv[1]["pc"]): - line = key[0] - if cnt0.get(line) != cnt2.get(line): - unaligned.add(line); continue - ref = a.get(key) - if not ref: continue - for v, s in j["vals"].items(): - # up to its declaration's line a local holds nothing yet: its block - # is its scope in the DWARF, but C's starts at the declarator - d = ref.get("decl", {}).get(v) or j.get("decl", {}).get(v) or 0 - if d and j.get("line", 0) <= d: + rows = [json.loads(l) for l in open(p)] + out = {}; calls = {}; prev = []; visits = {}; path = []; groups = {} + for r in rows: + st = r.get("stack") or [r["key"].split(":")[0]] + n = 0 + while n < len(prev) and n < len(st) and prev[n] == st[n] and n < len(path): + n += 1 + path = path[:n] + for d in range(n, len(st)): + parent = tuple(path) + c = calls.get((parent, st[d]), 0) + calls[(parent, st[d])] = c + 1 + path.append((st[d], c)) + prev = st + inv = tuple(path) + vk = (inv, r["line"]) + v = visits.get(vk, 0); visits[vk] = v + 1 + r["_inv"] = inv + out[(inv, r["line"], v)] = r + # every level's caller: (the calls above it, the functions from it down, the line) + for d in range(len(st)): + g = groups.setdefault((tuple(path[:d]), tuple(st[d:]), r["line"]), {}) + for name, val in r["vals"].items(): + g.setdefault(name, set()).add(val) + return out, visits, groups + +a, va, ga = load(sys.argv[1]); b, vb, gb = load(sys.argv[2]) +total = avail = agree = skipped = 0; bad = [] +for key, j in b.items(): + inv, line, v = key + flags = j.get("inl") or [] + first = next((d for d, x in enumerate(flags) if x), -1) + if first >= 0: + st = j.get("stack") + refset = ga.get((tuple(inv[:first]), tuple(st[first:]), line)) + ref = None + if refset is None: + skipped += 1; continue + else: + if va.get((inv, line)) != vb.get((inv, line)): + skipped += 1; continue + ref = a.get(key) + if ref is None: + skipped += 1; continue + for name, s in j["vals"].items(): + decl = (ref or {}).get("decl", {}).get(name) or j.get("decl", {}).get(name) or 0 + if decl and line <= decl: continue total += 1 if s is None: continue avail += 1 - r = ref["vals"].get(v) - if r == s: agree += 1 - else: bad.append((key[0], key[1], v, s, r)) -print("%d stops at -O2, %d aligned with -O0; %d values, %d available (%.0f%%), %d agree, %d DISAGREE; %d lines visited a different number of times" - % (len(b), sum(1 for k in b if k[0] not in unaligned), total, avail, 100.0 * avail / total if total else 0, agree, len(bad), len(unaligned))) -from collections import Counter -c = Counter((x[0], x[2]) for x in bad) -for (line, v), n in sorted(c.items()): - ex = [x for x in bad if x[0] == line and x[2] == v][0] - print(" %-14s %-6s x%-3d e.g. visit %d: -O2 says %s, -O0 has %s" % (line, v, n, ex[1], ex[3], ex[4])) -if verbose: print("unaligned:", sorted(unaligned)) + if ref is None: + ok = s in refset.get(name, set()) + r = "one of " + ",".join(sorted(str(x) for x in refset.get(name, set())))[:60] + else: + r = ref["vals"].get(name); ok = r == s + if ok: agree += 1 + else: bad.append((j["key"], name, s, r)) +print("%d stops, %d compared, %d not (visited differently within a call); %d values, %d available (%.0f%%), %d agree, %d DISAGREE" + % (len(b), len(b) - skipped, skipped, total, avail, 100.0 * avail / total if total else 0, agree, len(bad))) +c = Counter((x[0], x[1]) for x in bad) +for (key, v), n in sorted(c.items()): + ex = [x for x in bad if x[0] == key and x[1] == v][0] + print(" %-14s %-6s x%-3d e.g. got %s, reference %s" % (key, v, n, ex[2], ex[3])) diff --git a/tests/golden/debug-optimized/prog.c b/tests/golden/debug-optimized/prog.c index 2afff9ad..6936736c 100644 --- a/tests/golden/debug-optimized/prog.c +++ b/tests/golden/debug-optimized/prog.c @@ -69,6 +69,17 @@ __attribute__((noinline)) static int shadow(int n) return s + n; } +/* small enough to inline at -O2: its calls are inlined frames */ +static int clamp(int v, int lo, int hi) +{ + int r = v; + if (r < lo) + r = lo; + if (r > hi) + r = hi; + return r; +} + static const unsigned char msg[] = "hello, location lists"; static const int sorted[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; @@ -82,11 +93,15 @@ int main(void) int where = search(sorted, 11, 19); int none = search(sorted, 11, 4); int sh = shadow(6); + int cl = 0; + for (int j = -3; j < 9; j += 2) + cl += clamp(j * (int)(c % 7), 0, 4); putn((long)c); putn(a.sum); putn((long)(m & 0xffff)); putn(where + none); putn(sh); + putn(cl); puts_("\n==END==\n"); return 0; } diff --git a/tests/golden/debug-optimized/trace.py b/tests/golden/debug-optimized/trace.py index 4c43d57e..5e54752b 100644 --- a/tests/golden/debug-optimized/trace.py +++ b/tests/golden/debug-optimized/trace.py @@ -79,6 +79,20 @@ if blk.function is not None: break blk = blk.superblock + # the call stack, outermost first: which invocation a stop is in + stack = [] + inl = [] + g = f + while g is not None and len(stack) < 16: + stack.append(g.name() or "?") + inl.append(g.type() == gdb.INLINE_FRAME) + try: + g = g.older() + except gdb.error: + break + stack.reverse() + inl.reverse() out.write(json.dumps({"key": key, "line": sal.line, "k": k, "pc": int(f.pc()), - "vals": vals, "decl": decl}) + "\n") + "vals": vals, "decl": decl, "stack": stack, + "inl": inl}) + "\n") out.close() From 0ca98c46928ab92805f0cd5999a1f5db3ddeed96 Mon Sep 17 00:00:00 2001 From: teo1747 Date: Sun, 8 Nov 2026 23:42:20 +0100 Subject: [PATCH 7/9] dwarf: a block or inlined call whose code is in pieces gets DW_AT_ranges 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. --- src/debug/dwarf.c | 117 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 9 deletions(-) diff --git a/src/debug/dwarf.c b/src/debug/dwarf.c index 7162453b..710f7552 100644 --- a/src/debug/dwarf.c +++ b/src/debug/dwarf.c @@ -170,6 +170,8 @@ #define AB_IPARAM_LL 34 #define AB_IVAR 35 #define AB_IVAR_LL 36 +#define AB_LEXBLOCK_R 37 /* lexical_block whose code is in pieces */ +#define AB_INLSUB_R 38 /* inlined_subroutine whose code is in pieces */ static int dw_ver = 5; @@ -501,6 +503,14 @@ static void emit_abbrev(struct dbuf *b) one_abbrev(b, AB_IPARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, iv_ll, 2); one_abbrev(b, AB_IVAR, DW_TAG_variable, DW_CHILDREN_no, iv, 2); one_abbrev(b, AB_IVAR_LL, DW_TAG_variable, DW_CHILDREN_no, iv_ll, 2); + /* ...and both, where their code is not one run: DW_AT_ranges */ + static const unsigned char lexb_r[] = { DW_AT_ranges, DW_FORM_sec_offset }; + static const unsigned char inl_r[] = { + DW_AT_abstract_origin, DW_FORM_ref4, DW_AT_ranges, DW_FORM_sec_offset, + DW_AT_call_file, DW_FORM_data1, DW_AT_call_line, DW_FORM_udata, + DW_AT_call_column, DW_FORM_udata }; + one_abbrev(b, AB_LEXBLOCK_R, DW_TAG_lexical_block, DW_CHILDREN_yes, lexb_r, 1); + one_abbrev(b, AB_INLSUB_R, DW_TAG_inlined_subroutine, DW_CHILDREN_yes, inl_r, 5); db_uleb(b, 0); /* end of the abbrev table */ } @@ -874,6 +884,60 @@ static int list_header(struct dbuf *b) * its DIE's offset, and its own variables' (by dbgvars index; -1 for one * that came from a call inlined into it). */ struct absfn { const struct func *f; int off; int *voff; int nv; }; + +/* The range lists of the blocks and inlined calls whose code is in more + * than one piece, appended to .debug_rnglists (.debug_ranges) after the + * unit's own list: dw_brg_base is where they start in that section. */ +static struct dbuf dw_brg; +static long dw_brg_base; + +/* A scope's code, as the ranges [lo[k], hi[k]) in ascending order. */ +struct sranges { long *lo, *hi; int n, cap; }; + +static void sr_add(struct sranges *r, long a, long z) +{ + if (r->n && r->hi[r->n - 1] >= a) { /* touching or overlapping */ + if (z > r->hi[r->n - 1]) r->hi[r->n - 1] = z; + return; + } + if (r->n == r->cap) { + r->cap = r->cap ? r->cap * 2 : 4; + r->lo = xrealloc(r->lo, (size_t)r->cap * sizeof *r->lo); + r->hi = xrealloc(r->hi, (size_t)r->cap * sizeof *r->hi); + } + r->lo[r->n] = a; + r->hi[r->n] = z; + r->n++; +} + +/* A list of r's ranges in dw_brg; its offset in the section. DWARF 5: + * start_end entries, each address relocated; DWARF 4: a base address + * selection entry of 0, then the address pairs, then the end pair. */ +static long emit_brg(struct dwarf_out *out, const struct sranges *r) +{ + long at = dw_brg_base + dw_brg.len; + if (dw_ver < 5) { + db_addr(&dw_brg, addr_bytes() == 8 ? ~0UL : 0xffffffffUL); + db_addr(&dw_brg, 0); + } + for (int k = 0; k < r->n; k++) { + if (dw_ver >= 5) + db_u8(&dw_brg, DW_RLE_start_end); + reloc(out, DWSEC_RANGES, (int)(dw_brg_base + dw_brg.len), addr_bytes(), + DWTGT_TEXT, r->lo[k]); + db_addr(&dw_brg, 0); + reloc_end(out, DWSEC_RANGES, (int)(dw_brg_base + dw_brg.len), addr_bytes(), + r->hi[k]); + db_addr(&dw_brg, 0); + } + if (dw_ver >= 5) { + db_u8(&dw_brg, DW_RLE_end_of_list); + } else { + db_addr(&dw_brg, 0); + db_addr(&dw_brg, 0); + } + return at; +} static struct absfn *dw_abs; static int dw_nabs; @@ -1018,16 +1082,22 @@ static long ins_start(const struct ir_func *fn, int k, long lo, long hi) /* One block's DIE, its variables, and the blocks nested in it. */ static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, struct typemap *tm, struct ir_func *fn, long lo, - int s, const long *smin, const long *smax) + int s, const long *smin, const long *smax, + const struct sranges *sr) { const struct absfn *A = s > 0 && fn->scope_fn ? abs_of(fn->scope_fn[s]) : NULL; + /* in pieces (not in a .dwo, which keeps the extent): DW_AT_ranges */ + int pieces = s > 0 && sr && sr[s].n > 1 && !dw_split; if (A) { - db_uleb(b, AB_INLSUB); + db_uleb(b, pieces ? AB_INLSUB_R : AB_INLSUB); db_u32(b, (unsigned long)A->off); } else if (s > 0) { - db_uleb(b, AB_LEXBLOCK); + db_uleb(b, pieces ? AB_LEXBLOCK_R : AB_LEXBLOCK); } - if (s > 0) { + if (pieces) { + reloc(out, DWSEC_INFO, b->len, 4, DWTGT_RANGES, emit_brg(out, &sr[s])); + db_u32(b, 0); + } else if (s > 0) { if (dw_split) { db_uleb(b, (unsigned long)addr_index(DWTGT_TEXT, smin[s], NULL, 0)); db_u32(b, (unsigned long)(smax[s] - smin[s])); @@ -1055,7 +1125,7 @@ static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, if (smin) for (int c = s + 1; c < fn->nscopes; c++) if (fn->scope_up[c] == s && smin[c] < smax[c]) - emit_block(out, b, loc, tm, fn, lo, c, smin, smax); + emit_block(out, b, loc, tm, fn, lo, c, smin, smax, sr); if (s > 0) db_u8(b, 0); } @@ -1080,12 +1150,13 @@ static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, if (fn->ndbgvars <= 0) return; if (!fn->ins_pc || fn->nscopes <= 1 || !fn->scope_up || fn->nins <= 0) { - emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL); + emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL, NULL); return; } int ns = fn->nscopes; long *smin = xmalloc((size_t)ns * sizeof *smin); long *smax = xmalloc((size_t)ns * sizeof *smax); + struct sranges *sr = xcalloc((size_t)ns, sizeof *sr); for (int s = 0; s < ns; s++) { smin[s] = hi; smax[s] = lo; @@ -1097,6 +1168,7 @@ static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, for (int t = fn->ins[k].scope; t > 0 && t < ns; t = fn->scope_up[t]) { if (a < smin[t]) smin[t] = a; if (z > smax[t]) smax[t] = z; + sr_add(&sr[t], a, z); } } /* a variable of a block with no code: shown nowhere */ @@ -1105,7 +1177,12 @@ static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, if (vs > 0 && vs < ns && smin[vs] >= smax[vs]) fn->dbgvars[v].scope = -1; } - emit_block(out, b, loc, tm, fn, lo, 0, smin, smax); + emit_block(out, b, loc, tm, fn, lo, 0, smin, smax, sr); + for (int s = 0; s < ns; s++) { + free(sr[s].lo); + free(sr[s].hi); + } + free(sr); free(smin); free(smax); } @@ -1652,6 +1729,19 @@ static void emit_all(struct ir_unit *iu, const char *filename, int lc_empty = lc.len; emit_abbrev(&ab); emit_frame(out, &fr, iu); + /* the blocks' range lists start after the header and the unit's own + * list, whose size is known before any is written */ + { + int nf = 0; + for (int n = 0; n < iu->nfuncs; n++) + if (iu->funcs[n].src->code_len > 0) + nf++; + long ab2 = 2L * addr_bytes(); + dw_brg_base = dw_ver >= 5 ? 12 + (split ? nf * (1 + ab2) + 1 : 0) + : (split ? (nf + 1) * ab2 : 0); + free(dw_brg.p); + memset(&dw_brg, 0, sizeof dw_brg); + } emit_info(out, &in, &lc, iu, filename, lo, hi, split); emit_line(out, &ln, iu, filename); if (dw_split) { @@ -1662,11 +1752,12 @@ static void emit_all(struct ir_unit *iu, const char *filename, } else if (lc_at >= 0) { db_patch_u32(&lc, lc_at, (unsigned long)(lc.len - lc_at - 4)); } - if (split) { + if (split || dw_brg.len) { /* the ranges: [begin, end) per function, each relocated into the * function's own section -- .debug_rnglists' start_end entries * (DWARF 5), or .debug_ranges' address pairs ended by two zeros */ int rg_at = dw_ver >= 5 ? list_header(&rg) : -1; + if (split) { for (int n = 0; n < iu->nfuncs; n++) { if (iu->funcs[n].src->code_len <= 0) continue; long f_lo = iu->funcs[n].src->code_off; @@ -1680,11 +1771,19 @@ static void emit_all(struct ir_unit *iu, const char *filename, } if (dw_ver >= 5) { db_u8(&rg, DW_RLE_end_of_list); - db_patch_u32(&rg, rg_at, (unsigned long)(rg.len - rg_at - 4)); } else { db_addr(&rg, 0); db_addr(&rg, 0); } + } + if (rg.len != dw_brg_base) + internal_error("dwarf: the unit's range list is %d bytes, not the " + "%ld its blocks' lists were placed after", rg.len, + dw_brg_base); + for (int k = 0; k < dw_brg.len; k++) + db_u8(&rg, dw_brg.p[k]); + if (dw_ver >= 5) + db_patch_u32(&rg, rg_at, (unsigned long)(rg.len - rg_at - 4)); } out->sec[DWSEC_ABBREV] = ab.p; out->seclen[DWSEC_ABBREV] = ab.len; From 7653cfbc029b48b9ca40a15679a858ee6083e9bb Mon Sep 17 00:00:00 2001 From: teo1747 Date: Mon, 9 Nov 2026 00:07:02 +0100 Subject: [PATCH 8/9] irgen: a block's scope is kept in its statement, and found in one step 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. --- src/debug/dwarf.c | 55 +++++++++++++++++++++++++++++++++++------------ src/ir/ir.h | 2 +- src/ir/irgen.c | 34 ++++++++++++----------------- src/opt/inline.c | 1 + src/parse/ast.h | 3 +++ 5 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/debug/dwarf.c b/src/debug/dwarf.c index 710f7552..e9293d1d 100644 --- a/src/debug/dwarf.c +++ b/src/debug/dwarf.c @@ -1080,10 +1080,15 @@ static long ins_start(const struct ir_func *fn, int k, long lo, long hi) } /* One block's DIE, its variables, and the blocks nested in it. */ +/* Each scope's first child and next sibling, and its first variable and + * the next one in the same scope: built once, so a function of 200,000 + * blocks is not 200,000 scans of them all. */ +struct stree { int *kid, *sib, *var, *vnext; }; + static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, struct typemap *tm, struct ir_func *fn, long lo, int s, const long *smin, const long *smax, - const struct sranges *sr) + const struct sranges *sr, const struct stree *st) { const struct absfn *A = s > 0 && fn->scope_fn ? abs_of(fn->scope_fn[s]) : NULL; /* in pieces (not in a .dwo, which keeps the extent): DW_AT_ranges */ @@ -1113,19 +1118,16 @@ static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, db_uleb(b, (unsigned long)(fn->scope_cline[s] > 0 ? fn->scope_cline[s] : 0)); db_uleb(b, (unsigned long)(fn->scope_ccol[s] > 0 ? fn->scope_ccol[s] : 0)); } - for (int v = 0; v < fn->ndbgvars; v++) { - int vs = fn->dbgvars[v].scope; - if (vs < 0 && smin != NULL) - continue; /* its block has no code */ - if (vs < 0 || vs >= fn->nscopes || smin == NULL) - vs = 0; - if (vs == s) + if (!st) { + for (int v = 0; v < fn->ndbgvars; v++) + emit_dbgvar(out, b, loc, tm, fn, v, lo); + } else { + for (int v = st->var[s]; v >= 0; v = st->vnext[v]) emit_dbgvar(out, b, loc, tm, fn, v, lo); + for (int c = st->kid[s]; c >= 0; c = st->sib[c]) + if (smin[c] < smax[c]) + emit_block(out, b, loc, tm, fn, lo, c, smin, smax, sr, st); } - if (smin) - for (int c = s + 1; c < fn->nscopes; c++) - if (fn->scope_up[c] == s && smin[c] < smax[c]) - emit_block(out, b, loc, tm, fn, lo, c, smin, smax, sr); if (s > 0) db_u8(b, 0); } @@ -1150,7 +1152,7 @@ static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, if (fn->ndbgvars <= 0) return; if (!fn->ins_pc || fn->nscopes <= 1 || !fn->scope_up || fn->nins <= 0) { - emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL, NULL); + emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL, NULL, NULL); return; } int ns = fn->nscopes; @@ -1177,7 +1179,32 @@ static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, if (vs > 0 && vs < ns && smin[vs] >= smax[vs]) fn->dbgvars[v].scope = -1; } - emit_block(out, b, loc, tm, fn, lo, 0, smin, smax, sr); + struct stree st; + st.kid = xmalloc((size_t)ns * sizeof *st.kid); + st.sib = xmalloc((size_t)ns * sizeof *st.sib); + st.var = xmalloc((size_t)ns * sizeof *st.var); + st.vnext = xmalloc((size_t)(fn->ndbgvars ? fn->ndbgvars : 1) * sizeof *st.vnext); + for (int s = 0; s < ns; s++) + st.kid[s] = st.sib[s] = st.var[s] = -1; + for (int c = ns - 1; c > 0; c--) { /* in id order, each list */ + int u = fn->scope_up[c]; + if (u >= 0 && u < ns) { + st.sib[c] = st.kid[u]; + st.kid[u] = c; + } + } + for (int v = fn->ndbgvars - 1; v >= 0; v--) { + int vs = fn->dbgvars[v].scope; + st.vnext[v] = -1; + if (vs < 0) + continue; /* its block has no code */ + if (vs >= ns) + vs = 0; + st.vnext[v] = st.var[vs]; + st.var[vs] = v; + } + emit_block(out, b, loc, tm, fn, lo, 0, smin, smax, sr, &st); + free(st.kid); free(st.sib); free(st.var); free(st.vnext); for (int s = 0; s < ns; s++) { free(sr[s].lo); free(sr[s].hi); diff --git a/src/ir/ir.h b/src/ir/ir.h index a1d14b77..03e5dd22 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -653,7 +653,7 @@ struct ir_func { * writer makes a DW_TAG_lexical_block of each from its instructions' * code -- which, unlike var_scope_lo/hi, no reordering makes stale. */ int *scope_up; - int nscopes; + int nscopes, scope_cap; /* ...and where a scope is an inlined call (the inliner), the function * inlined there and the call's line and column; NULL and 0 for a * block. Each array nscopes long, or NULL while no call is inlined. */ diff --git a/src/ir/irgen.c b/src/ir/irgen.c index 94297937..188ef01c 100644 --- a/src/ir/irgen.c +++ b/src/ir/irgen.c @@ -183,20 +183,15 @@ void ir_locals_fill(struct ir_func *fn, struct func *f, int nvars) } } -/* -g: the lexical scope being generated (ir_ins.scope), and the blocks - * collect_locals gave one: each STMT_BLOCK or STMT_FOR that declares a - * local. */ +/* -g: the lexical scope being generated (ir_ins.scope). collect_locals + * gives each STMT_BLOCK or STMT_FOR that declares a local its scope, in + * the statement itself (dbg_scope): a lookup by pointer was a search, and + * 200,000 blocks in one function made it quadratic (many-declarations). */ static int g_cur_scope; -static struct stmt **g_scope_stmt; -static int *g_scope_id; -static int g_nscope_stmt, g_capscope_stmt; static int scope_of_stmt(const struct stmt *s) { - for (int k = 0; k < g_nscope_stmt; k++) - if (g_scope_stmt[k] == s) - return g_scope_id[k]; - return -1; + return s->dbg_scope - 1; } struct ir_ins *emit(struct ir_func *fn) @@ -7042,16 +7037,13 @@ static int declares_local(const struct stmt *s) /* A new lexical scope for block s, inside scope `up`. */ static int new_scope(struct ir_func *fn, struct stmt *s, int up) { + if (fn->nscopes == fn->scope_cap) { + fn->scope_cap = fn->scope_cap ? fn->scope_cap * 2 : 8; + fn->scope_up = xrealloc(fn->scope_up, (size_t)fn->scope_cap * sizeof *fn->scope_up); + } int id = fn->nscopes++; - fn->scope_up = xrealloc(fn->scope_up, (size_t)fn->nscopes * sizeof *fn->scope_up); fn->scope_up[id] = up; - if (g_nscope_stmt == g_capscope_stmt) { - g_capscope_stmt = g_capscope_stmt ? g_capscope_stmt * 2 : 16; - g_scope_stmt = xrealloc(g_scope_stmt, (size_t)g_capscope_stmt * sizeof *g_scope_stmt); - g_scope_id = xrealloc(g_scope_id, (size_t)g_capscope_stmt * sizeof *g_scope_id); - } - g_scope_stmt[g_nscope_stmt] = s; - g_scope_id[g_nscope_stmt++] = id; + s->dbg_scope = id + 1; return id; } @@ -7082,12 +7074,14 @@ static void collect_locals(struct ir_func *fn, struct stmt *s, int scope) break; case STMT_FOR: { /* `for (int i ...)`: i's scope is the for statement */ + s->dbg_scope = 0; int in = declares_local(s->initdecl) ? new_scope(fn, s, scope) : scope; collect_locals(fn, s->initdecl, in); collect_locals(fn, s->body, in); break; } case STMT_BLOCK: + s->dbg_scope = 0; collect_locals(fn, s->body, declares_local(s->body) ? new_scope(fn, s, scope) : scope); break; @@ -7268,10 +7262,10 @@ static void gen_func_(struct ir_func *fn, struct func *f) for (int i = 0; i < f->nparams; i++) add_dbgvar(fn, f->params[i], i, 1, f->param_tys[i], f->param_lines[i], f->param_cols[i]); - g_nscope_stmt = 0; g_cur_scope = 0; fn->nscopes = 1; - fn->scope_up = xmalloc(sizeof *fn->scope_up); + fn->scope_cap = 8; + fn->scope_up = xmalloc((size_t)fn->scope_cap * sizeof *fn->scope_up); fn->scope_up[0] = -1; collect_locals(fn, f->body, 0); /* Local scope ranges: default to the whole function ([0, +inf), narrowed to diff --git a/src/opt/inline.c b/src/opt/inline.c index b2775d1a..3c30b389 100644 --- a/src/opt/inline.c +++ b/src/opt/inline.c @@ -471,6 +471,7 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) int ns = base + cf->nscopes; int had = fn->scope_fn != NULL; fn->scope_up = xrealloc(fn->scope_up, (size_t)ns * sizeof *fn->scope_up); + fn->scope_cap = ns; fn->scope_fn = xrealloc(fn->scope_fn, (size_t)ns * sizeof *fn->scope_fn); fn->scope_cline = xrealloc(fn->scope_cline, (size_t)ns * sizeof *fn->scope_cline); fn->scope_ccol = xrealloc(fn->scope_ccol, (size_t)ns * sizeof *fn->scope_ccol); diff --git a/src/parse/ast.h b/src/parse/ast.h index 65bc60a4..3fd6ef9b 100644 --- a/src/parse/ast.h +++ b/src/parse/ast.h @@ -273,6 +273,9 @@ struct asm_stmt { struct stmt { enum stmt_kind kind; + /* -g: 1 + the lexical scope irgen gave this block or for statement + * (ir_func.scope_up), 0 when it declares no local of its own */ + int dbg_scope; int line; int col; /* 1-based column, for caret diagnostics (0 = unknown) */ const char *name; /* STMT_DECL */ From 2298480a84ef37b14d0d94bdc3b456e3f76721b9 Mon Sep 17 00:00:00 2001 From: teo1747 Date: Mon, 9 Nov 2026 00:07:02 +0100 Subject: [PATCH 9/9] tests: the debug tests as CI runs them, and MIPS 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. --- tests/golden/debug-live.sh | 5 +++++ tests/golden/debug-optimized.sh | 8 ++++++-- tests/golden/debug-optimized/cmp.py | 4 ++++ tests/golden/debug-optimized/prog.c | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/golden/debug-live.sh b/tests/golden/debug-live.sh index 6f8aee31..08dd00e4 100644 --- a/tests/golden/debug-live.sh +++ b/tests/golden/debug-live.sh @@ -70,6 +70,11 @@ if [ "$ARCH" = aarch64 ]; then image="$out/live.elf"; symbols="$out/live.elf" qargs="-M virt -cpu cortex-a72 -semihosting -nographic -kernel $image" else + # the QEMU harness links with newlib, whatever runs the corpus (a CI + # runner has no newlib; until it had gdb, this skipped for that) + [ -d "$X86_NEWLIB/lib" ] || { + echo "skipped: no x86_64 newlib at $X86_NEWLIB for the QEMU harness (set EMBCC_X86_NEWLIB)" + exit 0; } EMBCC_HARNESS_KEEP64=1 sh "$EMBCC_ROOT/tests/harness/x86_64/link.sh" \ -o "$out/live.elf" "$out/live.o" || { echo "harness link failed"; exit 1; } # QEMU boots the ELF32 Multiboot relabel; gdb reads the 64-bit original diff --git a/tests/golden/debug-optimized.sh b/tests/golden/debug-optimized.sh index e3c1ab4a..e462d69a 100644 --- a/tests/golden/debug-optimized.sh +++ b/tests/golden/debug-optimized.sh @@ -92,7 +92,11 @@ board m0 thumbv6m-none-eabi thumb-m0 EMBCC_THUMB_M0_HARNESS armv6-m \ "-mcpu=cortex-m0 -mfloat-abi=soft" qemu-system-arm -M microbit board rv32 riscv32-unknown-elf riscv EMBCC_RISCV_HARNESS riscv:rv32 \ "-march=rv32imac -mabi=ilp32 -mno-relax" qemu-system-riscv32 -M virt -bios none -m 8 -# (not MIPS yet: gdb 17 maps no address of clang's MIPS image to a line, -# though llvm-dwarfdump reads the table, so there is no reference there) +# clang's reference for MIPS is DWARF 4: gdb 17 maps no address of a +# linked MIPS image whose unit's start is a DWARF 5 .debug_addr entry +# (DW_FORM_addrx, which it does not sign-extend as it does the line +# table's KSEG0 addresses), so clang's DWARF 5 image has no lines to it +board mips mipsel-none-elf mips EMBCC_MIPS_HARNESS mips:isa32r2 \ + "-mcpu=mips32r2 -msoft-float -gdwarf-4" qemu-system-mipsel -M malta -cpu 24Kc [ $fail = 0 ] || { echo "FAIL: debug-optimized"; exit 1; } echo "debug-optimized: every value EmbCC's DWARF gives at -O0, -O2 and -Os is clang's -O0 build's" diff --git a/tests/golden/debug-optimized/cmp.py b/tests/golden/debug-optimized/cmp.py index bc9d909c..b702fb92 100644 --- a/tests/golden/debug-optimized/cmp.py +++ b/tests/golden/debug-optimized/cmp.py @@ -59,6 +59,10 @@ def load(p): decl = (ref or {}).get("decl", {}).get(name) or j.get("decl", {}).get(name) or 0 if decl and line <= decl: continue + # where the reference's own gdb cannot read it (an older gdb, at + # clang's epilogue), there is nothing to compare with + if ref is not None and str(ref["vals"].get(name, "")).startswith("ERR"): + continue total += 1 if s is None: continue avail += 1 diff --git a/tests/golden/debug-optimized/prog.c b/tests/golden/debug-optimized/prog.c index 6936736c..e1524b1f 100644 --- a/tests/golden/debug-optimized/prog.c +++ b/tests/golden/debug-optimized/prog.c @@ -86,7 +86,7 @@ static const int sorted[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; int main(void) { struct acc a = { 0, 0, 0 }; - unsigned c = crc8(msg, (int)sizeof msg - 1); + unsigned c = crc8(msg, 6); /* (each byte is 8 rounds of stops) */ for (int i = 0; i < 5; i++) add(&a, (int)c - i * 40); long long m = mix(0x123456789LL, a.sum, (short)a.n);