authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-26 01:43:55+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-26 01:46:09+01:00
log3df2f356eba9b0882ee3fa09704aae7dc173f3d4
tree8148aa73bb87733ef5c221c0ebf60a9c07cfd657
parent2e23ddbe7ba48857fbdcf014da3251f1decca00d
signaturelock-open Commit is signed but in an unrecognized format.

Sema: avoid emitting sequential dbg_stmt instructions

Often, a `dbg_stmt` ends up being associated with no real code because whatever it referred to was eliminated by semantic analysis. In these cases, Sema can replace the last `dbg_stmt` with the new one to avoid redundant AIR instructions which at best are nops and at worst cause backends to emit useless info (e.g. CBE does this).

1 files changed, 13 insertions(+), 0 deletions(-)

src/Sema.zig+13
...@@ -5852,6 +5852,19 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi...@@ -5852,6 +5852,19 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
5852 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;5852 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
58535853
5854 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;5854 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;
5855
5856 if (block.instructions.items.len != 0) {
5857 const idx = block.instructions.items[block.instructions.items.len - 1];
5858 if (sema.air_instructions.items(.tag)[idx] == .dbg_stmt) {
5859 // The previous dbg_stmt didn't correspond to any actual code, so replace it.
5860 sema.air_instructions.items(.data)[idx].dbg_stmt = .{
5861 .line = inst_data.line,
5862 .column = inst_data.column,
5863 };
5864 return;
5865 }
5866 }
5867
5855 _ = try block.addInst(.{5868 _ = try block.addInst(.{
5856 .tag = .dbg_stmt,5869 .tag = .dbg_stmt,
5857 .data = .{ .dbg_stmt = .{5870 .data = .{ .dbg_stmt = .{