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...@@ -11508,6 +11508,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11508 sub_block.runtime_loop = null;11508 sub_block.runtime_loop = null;
11509 sub_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(main_operand_src, mod);11509 sub_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(main_operand_src, mod);
11510 sub_block.runtime_index.increment();11510 sub_block.runtime_index.increment();
11511 sub_block.need_debug_scope = null; // this body is emitted regardless
11511 defer sub_block.instructions.deinit(gpa);11512 defer sub_block.instructions.deinit(gpa);
1151211513
11513 try sema.analyzeBodyRuntimeBreak(&sub_block, non_error_case.body);11514 try sema.analyzeBodyRuntimeBreak(&sub_block, non_error_case.body);
...@@ -12243,6 +12244,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12243,6 +12244,7 @@ fn analyzeSwitchRuntimeBlock(
12243 case_block.runtime_loop = null;12244 case_block.runtime_loop = null;
12244 case_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(operand_src, mod);12245 case_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(operand_src, mod);
12245 case_block.runtime_index.increment();12246 case_block.runtime_index.increment();
12247 case_block.need_debug_scope = null; // this body is emitted regardless
12246 defer case_block.instructions.deinit(gpa);12248 defer case_block.instructions.deinit(gpa);
1224712249
12248 var extra_index: usize = special.end;12250 var extra_index: usize = special.end;
...@@ -18967,8 +18969,7 @@ fn zirCondbr(...@@ -18967,8 +18969,7 @@ fn zirCondbr(
18967 const body = if (cond_val.toBool()) then_body else else_body;18969 const body = if (cond_val.toBool()) then_body else else_body;
1896818970
18969 try sema.maybeErrorUnwrapCondbr(parent_block, body, extra.data.condition, cond_src);18971 try sema.maybeErrorUnwrapCondbr(parent_block, body, extra.data.condition, cond_src);
18970 // We use `analyzeBodyInner` since we want to propagate any possible18972 // We use `analyzeBodyInner` since we want to propagate any comptime control flow to the caller.
18971 // `error.ComptimeBreak` to the caller.
18972 return sema.analyzeBodyInner(parent_block, body);18973 return sema.analyzeBodyInner(parent_block, body);
18973 }18974 }
1897418975
...@@ -18980,6 +18981,7 @@ fn zirCondbr(...@@ -18980,6 +18981,7 @@ fn zirCondbr(
18980 sub_block.runtime_loop = null;18981 sub_block.runtime_loop = null;
18981 sub_block.runtime_cond = mod.declPtr(parent_block.src_decl).toSrcLoc(cond_src, mod);18982 sub_block.runtime_cond = mod.declPtr(parent_block.src_decl).toSrcLoc(cond_src, mod);
18982 sub_block.runtime_index.increment();18983 sub_block.runtime_index.increment();
18984 sub_block.need_debug_scope = null; // this body is emitted regardless
18983 defer sub_block.instructions.deinit(gpa);18985 defer sub_block.instructions.deinit(gpa);
1898418986
18985 try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);18987 try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
src/codegen/llvm.zig+6-6
...@@ -5903,10 +5903,10 @@ pub const FuncGen = struct {...@@ -5903,10 +5903,10 @@ pub const FuncGen = struct {
5903 _ = try self.wip.brCond(cond, then_block, else_block);5903 _ = try self.wip.brCond(cond, then_block, else_block);
59045904
5905 self.wip.cursor = .{ .block = then_block };5905 self.wip.cursor = .{ .block = then_block };
5906 try self.genBody(then_body);5906 try self.genBodyDebugScope(then_body);
59075907
5908 self.wip.cursor = .{ .block = else_block };5908 self.wip.cursor = .{ .block = else_block };
5909 try self.genBody(else_body);5909 try self.genBodyDebugScope(else_body);
59105910
5911 // No need to reset the insert cursor since this instruction is noreturn.5911 // No need to reset the insert cursor since this instruction is noreturn.
5912 return .none;5912 return .none;
...@@ -5987,7 +5987,7 @@ pub const FuncGen = struct {...@@ -5987,7 +5987,7 @@ pub const FuncGen = struct {
5987 _ = try fg.wip.brCond(is_err, return_block, continue_block);5987 _ = try fg.wip.brCond(is_err, return_block, continue_block);
59885988
5989 fg.wip.cursor = .{ .block = return_block };5989 fg.wip.cursor = .{ .block = return_block };
5990 try fg.genBody(body);5990 try fg.genBodyDebugScope(body);
59915991
5992 fg.wip.cursor = .{ .block = continue_block };5992 fg.wip.cursor = .{ .block = continue_block };
5993 }5993 }
...@@ -6060,13 +6060,13 @@ pub const FuncGen = struct {...@@ -6060,13 +6060,13 @@ pub const FuncGen = struct {
6060 }6060 }
60616061
6062 self.wip.cursor = .{ .block = case_block };6062 self.wip.cursor = .{ .block = case_block };
6063 try self.genBody(case_body);6063 try self.genBodyDebugScope(case_body);
6064 }6064 }
60656065
6066 self.wip.cursor = .{ .block = else_block };6066 self.wip.cursor = .{ .block = else_block };
6067 const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]);6067 const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]);
6068 if (else_body.len != 0) {6068 if (else_body.len != 0) {
6069 try self.genBody(else_body);6069 try self.genBodyDebugScope(else_body);
6070 } else {6070 } else {
6071 _ = try self.wip.@"unreachable"();6071 _ = try self.wip.@"unreachable"();
6072 }6072 }
...@@ -6085,7 +6085,7 @@ pub const FuncGen = struct {...@@ -6085,7 +6085,7 @@ pub const FuncGen = struct {
6085 _ = try self.wip.br(loop_block);6085 _ = try self.wip.br(loop_block);
60866086
6087 self.wip.cursor = .{ .block = loop_block };6087 self.wip.cursor = .{ .block = loop_block };
6088 try self.genBody(body);6088 try self.genBodyDebugScope(body);
60896089
6090 // TODO instead of this logic, change AIR to have the property that6090 // TODO instead of this logic, change AIR to have the property that
6091 // every block is guaranteed to end with a noreturn instruction.6091 // every block is guaranteed to end with a noreturn instruction.