authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-21 12:42:27-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-21 12:46:46-07:00
logc36a2c27a51039d486f4149018154687a300d1eb
tree00e8eb7535b6f1e26b69ee237d7a82ee6102cc50
parent74b9cbd8950f4752c5c80925a8baa5fb2492d99f

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.

2 files changed, 22 insertions(+), 24 deletions(-)

src/Module.zig+3-4
...@@ -5635,10 +5635,9 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {...@@ -5635,10 +5635,9 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
56355635
5636 // Save the error trace as our first action in the function.5636 // Save the error trace as our first action in the function.
5637 // If this is unnecessary after all, Liveness will clean it up for us.5637 // If this is unnecessary after all, Liveness will clean it up for us.
5638 const err_ret_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block);5638 const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block);
5639 inner_block.error_return_trace_index = err_ret_trace_index;5639 sema.error_return_trace_index_on_fn_entry = error_return_trace_index;
5640 inner_block.error_return_trace_index_on_block_entry = err_ret_trace_index;5640 inner_block.error_return_trace_index = error_return_trace_index;
5641 inner_block.error_return_trace_index_on_function_entry = err_ret_trace_index;
56425641
5643 sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {5642 sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
5644 // TODO make these unreachable instead of @panic5643 // TODO make these unreachable instead of @panic
src/Sema.zig+19-20
...@@ -32,6 +32,8 @@ owner_func: ?*Module.Fn,...@@ -32,6 +32,8 @@ owner_func: ?*Module.Fn,
32/// This starts out the same as `owner_func` and then diverges in the case of32/// This starts out the same as `owner_func` and then diverges in the case of
33/// an inline or comptime function call.33/// an inline or comptime function call.
34func: ?*Module.Fn,34func: ?*Module.Fn,
35/// Used to restore the error return trace when returning a non-error from a function.
36error_return_trace_index_on_fn_entry: Air.Inst.Ref = .none,
35/// When semantic analysis needs to know the return type of the function whose body37/// When semantic analysis needs to know the return type of the function whose body
36/// is being analyzed, this `Type` should be used instead of going through `func`.38/// is being analyzed, this `Type` should be used instead of going through `func`.
37/// This will correctly handle the case of a comptime/inline function call of a39/// This will correctly handle the case of a comptime/inline function call of a
...@@ -156,8 +158,6 @@ pub const Block = struct {...@@ -156,8 +158,6 @@ pub const Block = struct {
156 /// Keep track of the active error return trace index around blocks so that we can correctly158 /// Keep track of the active error return trace index around blocks so that we can correctly
157 /// pop the error trace upon block exit.159 /// pop the error trace upon block exit.
158 error_return_trace_index: Air.Inst.Ref = .none,160 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
162 /// when null, it is determined by build mode, changed by @setRuntimeSafety162 /// when null, it is determined by build mode, changed by @setRuntimeSafety
163 want_safety: ?bool = null,163 want_safety: ?bool = null,
...@@ -233,8 +233,6 @@ pub const Block = struct {...@@ -233,8 +233,6 @@ pub const Block = struct {
233 .c_import_buf = parent.c_import_buf,233 .c_import_buf = parent.c_import_buf,
234 .switch_else_err_ty = parent.switch_else_err_ty,234 .switch_else_err_ty = parent.switch_else_err_ty,
235 .error_return_trace_index = parent.error_return_trace_index,235 .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,
238 };236 };
239 }237 }
240238
...@@ -5039,8 +5037,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -5039,8 +5037,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErro
5039 .runtime_loop = parent_block.runtime_loop,5037 .runtime_loop = parent_block.runtime_loop,
5040 .runtime_index = parent_block.runtime_index,5038 .runtime_index = parent_block.runtime_index,
5041 .error_return_trace_index = parent_block.error_return_trace_index,5039 .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,
5044 };5040 };
50455041
5046 defer child_block.instructions.deinit(gpa);5042 defer child_block.instructions.deinit(gpa);
...@@ -6256,6 +6252,10 @@ fn analyzeCall(...@@ -6256,6 +6252,10 @@ fn analyzeCall(
6256 sema.func = module_fn;6252 sema.func = module_fn;
6257 defer sema.func = parent_func;6253 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
6259 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, fn_owner_decl.src_scope);6259 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, fn_owner_decl.src_scope);
6260 defer wip_captures.deinit();6260 defer wip_captures.deinit();
62616261
...@@ -6270,8 +6270,6 @@ fn analyzeCall(...@@ -6270,8 +6270,6 @@ fn analyzeCall(
6270 .inlining = &inlining,6270 .inlining = &inlining,
6271 .is_comptime = is_comptime_call,6271 .is_comptime = is_comptime_call,
6272 .error_return_trace_index = block.error_return_trace_index,6272 .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,
6275 };6273 };
62766274
6277 const merges = &child_block.inlining.?.merges;6275 const merges = &child_block.inlining.?.merges;
...@@ -7020,10 +7018,9 @@ fn instantiateGenericCall(...@@ -7020,10 +7018,9 @@ fn instantiateGenericCall(
70207018
7021 // Save the error trace as our first action in the function.7019 // Save the error trace as our first action in the function.
7022 // If this is unnecessary after all, Liveness will clean it up for us.7020 // If this is unnecessary after all, Liveness will clean it up for us.
7023 const err_ret_trace_index = try sema.analyzeSaveErrRetIndex(&child_block);7021 const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&child_block);
7024 child_block.error_return_trace_index = err_ret_trace_index;7022 child_sema.error_return_trace_index_on_fn_entry = error_return_trace_index;
7025 child_block.error_return_trace_index_on_block_entry = err_ret_trace_index;7023 child_block.error_return_trace_index = error_return_trace_index;
7026 child_block.error_return_trace_index_on_function_entry = err_ret_trace_index;
70277024
7028 const new_func_inst = child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst) catch |err| {7025 const new_func_inst = child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst) catch |err| {
7029 // TODO look up the compile error that happened here and attach a note to it7026 // 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...@@ -10218,8 +10215,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
10218 .runtime_loop = block.runtime_loop,10215 .runtime_loop = block.runtime_loop,
10219 .runtime_index = block.runtime_index,10216 .runtime_index = block.runtime_index,
10220 .error_return_trace_index = block.error_return_trace_index,10217 .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,
10223 };10218 };
10224 const merges = &child_block.label.?.merges;10219 const merges = &child_block.label.?.merges;
10225 defer child_block.instructions.deinit(gpa);10220 defer child_block.instructions.deinit(gpa);
...@@ -15741,8 +15736,6 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -15741,8 +15736,6 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
15741 .is_typeof = true,15736 .is_typeof = true,
15742 .want_safety = false,15737 .want_safety = false,
15743 .error_return_trace_index = block.error_return_trace_index,15738 .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,
15746 };15739 };
15747 defer child_block.instructions.deinit(sema.gpa);15740 defer child_block.instructions.deinit(sema.gpa);
1574815741
...@@ -16444,16 +16437,22 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)...@@ -16444,16 +16437,22 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
16444 while (true) {16437 while (true) {
16445 if (block.label) |label| {16438 if (block.label) |label| {
16446 if (label.zir_block == zir_block) {16439 if (label.zir_block == zir_block) {
16447 if (start_block.error_return_trace_index != block.error_return_trace_index_on_block_entry)16440 const target_trace_index = if (block.parent) |parent_block| tgt: {
16448 break :b block.error_return_trace_index_on_block_entry;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
16449 return; // No need to restore16447 return; // No need to restore
16450 }16448 }
16451 }16449 }
16452 block = block.parent.?;16450 block = block.parent.?;
16453 }16451 }
16454 } else b: {16452 } else b: {
16455 if (start_block.error_return_trace_index != 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)
16456 break :b start_block.error_return_trace_index_on_function_entry;16454 break :b sema.error_return_trace_index_on_fn_entry;
16455
16457 return; // No need to restore16456 return; // No need to restore
16458 };16457 };
1645916458