From 321045cf33268e7b75e2972d898010ecacc345dc Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 28 Feb 2024 02:05:10 +0000 Subject: [PATCH] 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. --- src/Sema.zig | 6 ++++-- src/codegen/llvm.zig | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 7bc96c8e217bdf88690d9e4b0a1a5b51332e35ec..426c32c2b66f53d40f5e0c816612e8857fd8c665 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11508,6 +11508,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp sub_block.runtime_loop = null; sub_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(main_operand_src, mod); sub_block.runtime_index.increment(); + sub_block.need_debug_scope = null; // this body is emitted regardless defer sub_block.instructions.deinit(gpa); try sema.analyzeBodyRuntimeBreak(&sub_block, non_error_case.body); @@ -12243,6 +12244,7 @@ fn analyzeSwitchRuntimeBlock( case_block.runtime_loop = null; case_block.runtime_cond = mod.declPtr(child_block.src_decl).toSrcLoc(operand_src, mod); case_block.runtime_index.increment(); + case_block.need_debug_scope = null; // this body is emitted regardless defer case_block.instructions.deinit(gpa); var extra_index: usize = special.end; @@ -18967,8 +18969,7 @@ fn zirCondbr( const body = if (cond_val.toBool()) then_body else else_body; try sema.maybeErrorUnwrapCondbr(parent_block, body, extra.data.condition, cond_src); - // We use `analyzeBodyInner` since we want to propagate any possible - // `error.ComptimeBreak` to the caller. + // We use `analyzeBodyInner` since we want to propagate any comptime control flow to the caller. return sema.analyzeBodyInner(parent_block, body); } @@ -18980,6 +18981,7 @@ fn zirCondbr( sub_block.runtime_loop = null; sub_block.runtime_cond = mod.declPtr(parent_block.src_decl).toSrcLoc(cond_src, mod); sub_block.runtime_index.increment(); + sub_block.need_debug_scope = null; // this body is emitted regardless defer sub_block.instructions.deinit(gpa); try sema.analyzeBodyRuntimeBreak(&sub_block, then_body); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 06d5e16eb56d2d94e836fea7c1f2fe8366df4095..82b204cacb141321a7cf5e838de4ea85a5469984 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -5903,10 +5903,10 @@ pub const FuncGen = struct { _ = try self.wip.brCond(cond, then_block, else_block); self.wip.cursor = .{ .block = then_block }; - try self.genBody(then_body); + try self.genBodyDebugScope(then_body); self.wip.cursor = .{ .block = else_block }; - try self.genBody(else_body); + try self.genBodyDebugScope(else_body); // No need to reset the insert cursor since this instruction is noreturn. return .none; @@ -5987,7 +5987,7 @@ pub const FuncGen = struct { _ = try fg.wip.brCond(is_err, return_block, continue_block); fg.wip.cursor = .{ .block = return_block }; - try fg.genBody(body); + try fg.genBodyDebugScope(body); fg.wip.cursor = .{ .block = continue_block }; } @@ -6060,13 +6060,13 @@ pub const FuncGen = struct { } self.wip.cursor = .{ .block = case_block }; - try self.genBody(case_body); + try self.genBodyDebugScope(case_body); } self.wip.cursor = .{ .block = else_block }; const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]); if (else_body.len != 0) { - try self.genBody(else_body); + try self.genBodyDebugScope(else_body); } else { _ = try self.wip.@"unreachable"(); } @@ -6085,7 +6085,7 @@ pub const FuncGen = struct { _ = try self.wip.br(loop_block); self.wip.cursor = .{ .block = loop_block }; - try self.genBody(body); + try self.genBodyDebugScope(body); // TODO instead of this logic, change AIR to have the property that // every block is guaranteed to end with a noreturn instruction. -- 2.54.0