Skip to content

Commit c36a2c2

Browse files
committed
Change how Block propagates (error return) trace index
Instead of adding 3 fields to every `Block`, this adds just one. The function-level information is saved in the `Sema` struct instead, which is created/copied more rarely.
1 parent 74b9cbd commit c36a2c2

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/Module.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,10 +5635,9 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
56355635

56365636
// Save the error trace as our first action in the function.
56375637
// If this is unnecessary after all, Liveness will clean it up for us.
5638-
const err_ret_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block);
5639-
inner_block.error_return_trace_index = err_ret_trace_index;
5640-
inner_block.error_return_trace_index_on_block_entry = err_ret_trace_index;
5641-
inner_block.error_return_trace_index_on_function_entry = err_ret_trace_index;
5638+
const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block);
5639+
sema.error_return_trace_index_on_fn_entry = error_return_trace_index;
5640+
inner_block.error_return_trace_index = error_return_trace_index;
56425641

56435642
sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
56445643
// TODO make these unreachable instead of @panic

src/Sema.zig

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ owner_func: ?*Module.Fn,
3232
/// This starts out the same as `owner_func` and then diverges in the case of
3333
/// an inline or comptime function call.
3434
func: ?*Module.Fn,
35+
/// Used to restore the error return trace when returning a non-error from a function.
36+
error_return_trace_index_on_fn_entry: Air.Inst.Ref = .none,
3537
/// When semantic analysis needs to know the return type of the function whose body
3638
/// is being analyzed, this `Type` should be used instead of going through `func`.
3739
/// This will correctly handle the case of a comptime/inline function call of a
@@ -156,8 +158,6 @@ pub const Block = struct {
156158
/// Keep track of the active error return trace index around blocks so that we can correctly
157159
/// pop the error trace upon block exit.
158160
error_return_trace_index: Air.Inst.Ref = .none,
159-
error_return_trace_index_on_block_entry: Air.Inst.Ref = .none,
160-
error_return_trace_index_on_function_entry: Air.Inst.Ref = .none,
161161

162162
/// when null, it is determined by build mode, changed by @setRuntimeSafety
163163
want_safety: ?bool = null,
@@ -233,8 +233,6 @@ pub const Block = struct {
233233
.c_import_buf = parent.c_import_buf,
234234
.switch_else_err_ty = parent.switch_else_err_ty,
235235
.error_return_trace_index = parent.error_return_trace_index,
236-
.error_return_trace_index_on_block_entry = parent.error_return_trace_index,
237-
.error_return_trace_index_on_function_entry = parent.error_return_trace_index_on_function_entry,
238236
};
239237
}
240238

@@ -5039,8 +5037,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErro
50395037
.runtime_loop = parent_block.runtime_loop,
50405038
.runtime_index = parent_block.runtime_index,
50415039
.error_return_trace_index = parent_block.error_return_trace_index,
5042-
.error_return_trace_index_on_block_entry = parent_block.error_return_trace_index,
5043-
.error_return_trace_index_on_function_entry = parent_block.error_return_trace_index_on_function_entry,
50445040
};
50455041

50465042
defer child_block.instructions.deinit(gpa);
@@ -6256,6 +6252,10 @@ fn analyzeCall(
62566252
sema.func = module_fn;
62576253
defer sema.func = parent_func;
62586254

6255+
const parent_err_ret_index = sema.error_return_trace_index_on_fn_entry;
6256+
sema.error_return_trace_index_on_fn_entry = block.error_return_trace_index;
6257+
defer sema.error_return_trace_index_on_fn_entry = parent_err_ret_index;
6258+
62596259
var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, fn_owner_decl.src_scope);
62606260
defer wip_captures.deinit();
62616261

@@ -6270,8 +6270,6 @@ fn analyzeCall(
62706270
.inlining = &inlining,
62716271
.is_comptime = is_comptime_call,
62726272
.error_return_trace_index = block.error_return_trace_index,
6273-
.error_return_trace_index_on_block_entry = block.error_return_trace_index,
6274-
.error_return_trace_index_on_function_entry = block.error_return_trace_index,
62756273
};
62766274

62776275
const merges = &child_block.inlining.?.merges;
@@ -7020,10 +7018,9 @@ fn instantiateGenericCall(
70207018

70217019
// Save the error trace as our first action in the function.
70227020
// If this is unnecessary after all, Liveness will clean it up for us.
7023-
const err_ret_trace_index = try sema.analyzeSaveErrRetIndex(&child_block);
7024-
child_block.error_return_trace_index = err_ret_trace_index;
7025-
child_block.error_return_trace_index_on_block_entry = err_ret_trace_index;
7026-
child_block.error_return_trace_index_on_function_entry = err_ret_trace_index;
7021+
const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&child_block);
7022+
child_sema.error_return_trace_index_on_fn_entry = error_return_trace_index;
7023+
child_block.error_return_trace_index = error_return_trace_index;
70277024

70287025
const new_func_inst = child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst) catch |err| {
70297026
// TODO look up the compile error that happened here and attach a note to it
@@ -10218,8 +10215,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1021810215
.runtime_loop = block.runtime_loop,
1021910216
.runtime_index = block.runtime_index,
1022010217
.error_return_trace_index = block.error_return_trace_index,
10221-
.error_return_trace_index_on_block_entry = block.error_return_trace_index,
10222-
.error_return_trace_index_on_function_entry = block.error_return_trace_index_on_function_entry,
1022310218
};
1022410219
const merges = &child_block.label.?.merges;
1022510220
defer child_block.instructions.deinit(gpa);
@@ -15741,8 +15736,6 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1574115736
.is_typeof = true,
1574215737
.want_safety = false,
1574315738
.error_return_trace_index = block.error_return_trace_index,
15744-
.error_return_trace_index_on_block_entry = block.error_return_trace_index,
15745-
.error_return_trace_index_on_function_entry = block.error_return_trace_index_on_function_entry,
1574615739
};
1574715740
defer child_block.instructions.deinit(sema.gpa);
1574815741

@@ -16444,16 +16437,22 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1644416437
while (true) {
1644516438
if (block.label) |label| {
1644616439
if (label.zir_block == zir_block) {
16447-
if (start_block.error_return_trace_index != block.error_return_trace_index_on_block_entry)
16448-
break :b block.error_return_trace_index_on_block_entry;
16440+
const target_trace_index = if (block.parent) |parent_block| tgt: {
16441+
break :tgt parent_block.error_return_trace_index;
16442+
} else sema.error_return_trace_index_on_fn_entry;
16443+
16444+
if (start_block.error_return_trace_index != target_trace_index)
16445+
break :b target_trace_index;
16446+
1644916447
return; // No need to restore
1645016448
}
1645116449
}
1645216450
block = block.parent.?;
1645316451
}
1645416452
} else b: {
16455-
if (start_block.error_return_trace_index != start_block.error_return_trace_index_on_function_entry)
16456-
break :b start_block.error_return_trace_index_on_function_entry;
16453+
if (start_block.error_return_trace_index != sema.error_return_trace_index_on_fn_entry)
16454+
break :b sema.error_return_trace_index_on_fn_entry;
16455+
1645716456
return; // No need to restore
1645816457
};
1645916458

0 commit comments

Comments
 (0)