authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-28 02:05:10+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-29 23:38:18+00:00
log321045cf33268e7b75e2972d898010ecacc345dc
tree0d296a3879ad1e6feb059947150dbbac933b547f
parentf51d9ab892caeb63c40fcd2c1da4ade70038119c
signaturelock-open Commit is signed but in an unrecognized format.

codegen: handle dbg_var scoping correctly after eliding more ZIR blocks

Since we now elide more ZIR blocks in AstGen, care must be taken in codegen to introduce lexical scopes for every body, not just `block`s. Also, elide a few unnecessary AIR blocks in Sema.

2 files changed, 10 insertions(+), 8 deletions(-)

src/Sema.zig+4-2
......@@ -11508,6 +11508,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1150811508 sub_block.runtime_loop = null;
1150911509 sub_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(main_operand_src, mod);
1151011510 sub_block.runtime_index.increment();
11511 sub_block.need_debug_scope = null; // this body is emitted regardless
1151111512 defer sub_block.instructions.deinit(gpa);
1151211513
1151311514 try sema.analyzeBodyRuntimeBreak(&sub_block, non_error_case.body);
......@@ -12243,6 +12244,7 @@ fn analyzeSwitchRuntimeBlock(
1224312244 case_block.runtime_loop = null;
1224412245 case_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(operand_src, mod);
1224512246 case_block.runtime_index.increment();
12247 case_block.need_debug_scope = null; // this body is emitted regardless
1224612248 defer case_block.instructions.deinit(gpa);
1224712249
1224812250 var extra_index: usize = special.end;
......@@ -18967,8 +18969,7 @@ fn zirCondbr(
1896718969 const body = if (cond_val.toBool()) then_body else else_body;
1896818970
1896918971 try sema.maybeErrorUnwrapCondbr(parent_block, body, extra.data.condition, cond_src);
18970 // We use `analyzeBodyInner` since we want to propagate any possible
18971 // `error.ComptimeBreak` to the caller.
18972 // We use `analyzeBodyInner` since we want to propagate any comptime control flow to the caller.
1897218973 return sema.analyzeBodyInner(parent_block, body);
1897318974 }
1897418975
......@@ -18980,6 +18981,7 @@ fn zirCondbr(
1898018981 sub_block.runtime_loop = null;
1898118982 sub_block.runtime_cond = mod.declPtr(parent_block.src_decl).toSrcLoc(cond_src, mod);
1898218983 sub_block.runtime_index.increment();
18984 sub_block.need_debug_scope = null; // this body is emitted regardless
1898318985 defer sub_block.instructions.deinit(gpa);
1898418986
1898518987 try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
src/codegen/llvm.zig+6-6
......@@ -5903,10 +5903,10 @@ pub const FuncGen = struct {
59035903 _ = try self.wip.brCond(cond, then_block, else_block);
59045904
59055905 self.wip.cursor = .{ .block = then_block };
5906 try self.genBody(then_body);
5906 try self.genBodyDebugScope(then_body);
59075907
59085908 self.wip.cursor = .{ .block = else_block };
5909 try self.genBody(else_body);
5909 try self.genBodyDebugScope(else_body);
59105910
59115911 // No need to reset the insert cursor since this instruction is noreturn.
59125912 return .none;
......@@ -5987,7 +5987,7 @@ pub const FuncGen = struct {
59875987 _ = try fg.wip.brCond(is_err, return_block, continue_block);
59885988
59895989 fg.wip.cursor = .{ .block = return_block };
5990 try fg.genBody(body);
5990 try fg.genBodyDebugScope(body);
59915991
59925992 fg.wip.cursor = .{ .block = continue_block };
59935993 }
......@@ -6060,13 +6060,13 @@ pub const FuncGen = struct {
60606060 }
60616061
60626062 self.wip.cursor = .{ .block = case_block };
6063 try self.genBody(case_body);
6063 try self.genBodyDebugScope(case_body);
60646064 }
60656065
60666066 self.wip.cursor = .{ .block = else_block };
60676067 const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]);
60686068 if (else_body.len != 0) {
6069 try self.genBody(else_body);
6069 try self.genBodyDebugScope(else_body);
60706070 } else {
60716071 _ = try self.wip.@"unreachable"();
60726072 }
......@@ -6085,7 +6085,7 @@ pub const FuncGen = struct {
60856085 _ = try self.wip.br(loop_block);
60866086
60876087 self.wip.cursor = .{ .block = loop_block };
6088 try self.genBody(body);
6088 try self.genBodyDebugScope(body);
60896089
60906090 // TODO instead of this logic, change AIR to have the property that
60916091 // every block is guaranteed to end with a noreturn instruction.