authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-19 15:22:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
log529d01c2baf695c2844b5dd42642a74749bef0d0
treed5c54c9a9f9d835ab0a7f0e8789fda1957cb8224
parent8944dea23fb554290a4b54ca40b0594f6e3f77a9

resolve error tracing logic at module creation time

rather than checking multiple conditions in Sema

4 files changed, 34 insertions(+), 20 deletions(-)

src/Compilation/Config.zig+10
......@@ -408,9 +408,17 @@ pub fn resolve(options: Options) !Config {
408408 };
409409 };
410410
411 const backend_supports_error_tracing = target_util.backendSupportsFeature(
412 target.cpu.arch,
413 target.ofmt,
414 use_llvm,
415 .error_return_trace,
416 );
417
411418 const root_error_tracing = b: {
412419 if (options.root_error_tracing) |x| break :b x;
413420 if (root_strip) break :b false;
421 if (!backend_supports_error_tracing) break :b false;
414422 break :b switch (root_optimize_mode) {
415423 .Debug => true,
416424 .ReleaseSafe, .ReleaseFast, .ReleaseSmall => false,
......@@ -418,6 +426,8 @@ pub fn resolve(options: Options) !Config {
418426 };
419427
420428 const any_error_tracing = root_error_tracing or options.any_error_tracing;
429 if (any_error_tracing and !backend_supports_error_tracing)
430 return error.BackendLacksErrorTracing;
421431
422432 const rdynamic = options.rdynamic orelse false;
423433
src/Module.zig+1-10
......@@ -5589,16 +5589,7 @@ pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool {
55895589 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
55905590 const ofmt = zcu.root_mod.resolved_target.result.ofmt;
55915591 const use_llvm = zcu.comp.config.use_llvm;
5592 return switch (feature) {
5593 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
5594 .panic_unwrap_error => ofmt == .c or use_llvm,
5595 .safety_check_formatted => ofmt == .c or use_llvm,
5596 .error_return_trace => use_llvm,
5597 .is_named_enum_value => use_llvm,
5598 .error_set_has_value => use_llvm or cpu_arch.isWasm(),
5599 .field_reordering => use_llvm,
5600 .safety_checked_instructions => use_llvm,
5601 };
5592 return target_util.backendSupportsFeature(cpu_arch, ofmt, use_llvm, feature);
56025593}
56035594
56045595/// Shortcut for calling `intern_pool.get`.
src/Sema.zig+4-10
......@@ -2045,9 +2045,10 @@ fn analyzeAsType(
20452045
20462046pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) !void {
20472047 const mod = sema.mod;
2048 const comp = mod.comp;
20482049 const gpa = sema.gpa;
20492050 const ip = &mod.intern_pool;
2050 if (!mod.backendSupportsFeature(.error_return_trace)) return;
2051 if (!comp.config.any_error_tracing) return;
20512052
20522053 assert(!block.is_comptime);
20532054 var err_trace_block = block.makeSubBlock();
......@@ -6543,7 +6544,6 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
65436544 const gpa = sema.gpa;
65446545 const src = sema.src;
65456546
6546 if (!mod.backendSupportsFeature(.error_return_trace)) return .none;
65476547 if (!block.ownerModule().error_tracing) return .none;
65486548
65496549 if (block.is_comptime)
......@@ -6728,7 +6728,7 @@ fn zirCall(
67286728 input_is_error = false;
67296729 }
67306730
6731 if (mod.backendSupportsFeature(.error_return_trace) and block.ownerModule().error_tracing and
6731 if (block.ownerModule().error_tracing and
67326732 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
67336733 {
67346734 const return_ty = sema.typeOf(call_inst);
......@@ -18759,8 +18759,6 @@ fn retWithErrTracing(
1875918759
1876018760fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {
1876118761 const mod = sema.mod;
18762 if (!mod.backendSupportsFeature(.error_return_trace)) return false;
18763
1876418762 return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing;
1876518763}
1876618764
......@@ -18768,8 +18766,6 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1876818766 const mod = sema.mod;
1876918767 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].save_err_ret_index;
1877018768
18771 // TODO: replace all of these checks with logic in module creation
18772 if (!mod.backendSupportsFeature(.error_return_trace)) return;
1877318769 if (!block.ownerModule().error_tracing) return;
1877418770
1877518771 // This is only relevant at runtime.
......@@ -18795,7 +18791,6 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1879518791 const mod = sema.mod;
1879618792 const ip = &mod.intern_pool;
1879718793
18798 if (!mod.backendSupportsFeature(.error_return_trace)) return;
1879918794 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
1880018795 if (!start_block.ownerModule().error_tracing) return;
1880118796
......@@ -20068,8 +20063,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
2006820063
2006920064 if (sema.owner_func_index != .none and
2007020065 ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and
20071 block.ownerModule().error_tracing and
20072 mod.backendSupportsFeature(.error_return_trace))
20066 block.ownerModule().error_tracing)
2007320067 {
2007420068 return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);
2007520069 }
src/target.zig+19
......@@ -2,6 +2,7 @@ const std = @import("std");
22const Type = @import("type.zig").Type;
33const AddressSpace = std.builtin.AddressSpace;
44const Alignment = @import("InternPool.zig").Alignment;
5const Feature = @import("Module.zig").Feature;
56
67pub const default_stack_protector_buffer_size = 4;
78
......@@ -665,6 +666,24 @@ pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBacken
665666 };
666667}
667668
669pub fn backendSupportsFeature(
670 cpu_arch: std.Target.Cpu.Arch,
671 ofmt: std.Target.ObjectFormat,
672 use_llvm: bool,
673 feature: Feature,
674) bool {
675 return switch (feature) {
676 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
677 .panic_unwrap_error => ofmt == .c or use_llvm,
678 .safety_check_formatted => ofmt == .c or use_llvm,
679 .error_return_trace => use_llvm,
680 .is_named_enum_value => use_llvm,
681 .error_set_has_value => use_llvm or cpu_arch.isWasm(),
682 .field_reordering => use_llvm,
683 .safety_checked_instructions => use_llvm,
684 };
685}
686
668687pub fn defaultEntrySymbolName(
669688 target: std.Target,
670689 /// May be `undefined` when `target` is not WASI.