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 {...@@ -408,9 +408,17 @@ pub fn resolve(options: Options) !Config {
408 };408 };
409 };409 };
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
411 const root_error_tracing = b: {418 const root_error_tracing = b: {
412 if (options.root_error_tracing) |x| break :b x;419 if (options.root_error_tracing) |x| break :b x;
413 if (root_strip) break :b false;420 if (root_strip) break :b false;
421 if (!backend_supports_error_tracing) break :b false;
414 break :b switch (root_optimize_mode) {422 break :b switch (root_optimize_mode) {
415 .Debug => true,423 .Debug => true,
416 .ReleaseSafe, .ReleaseFast, .ReleaseSmall => false,424 .ReleaseSafe, .ReleaseFast, .ReleaseSmall => false,
...@@ -418,6 +426,8 @@ pub fn resolve(options: Options) !Config {...@@ -418,6 +426,8 @@ pub fn resolve(options: Options) !Config {
418 };426 };
419427
420 const any_error_tracing = root_error_tracing or options.any_error_tracing;428 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
422 const rdynamic = options.rdynamic orelse false;432 const rdynamic = options.rdynamic orelse false;
423433
src/Module.zig+1-10
...@@ -5589,16 +5589,7 @@ pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool {...@@ -5589,16 +5589,7 @@ pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool {
5589 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;5589 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
5590 const ofmt = zcu.root_mod.resolved_target.result.ofmt;5590 const ofmt = zcu.root_mod.resolved_target.result.ofmt;
5591 const use_llvm = zcu.comp.config.use_llvm;5591 const use_llvm = zcu.comp.config.use_llvm;
5592 return switch (feature) {5592 return target_util.backendSupportsFeature(cpu_arch, ofmt, use_llvm, 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 };
5602}5593}
56035594
5604/// Shortcut for calling `intern_pool.get`.5595/// Shortcut for calling `intern_pool.get`.
src/Sema.zig+4-10
...@@ -2045,9 +2045,10 @@ fn analyzeAsType(...@@ -2045,9 +2045,10 @@ fn analyzeAsType(
20452045
2046pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) !void {2046pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) !void {
2047 const mod = sema.mod;2047 const mod = sema.mod;
2048 const comp = mod.comp;
2048 const gpa = sema.gpa;2049 const gpa = sema.gpa;
2049 const ip = &mod.intern_pool;2050 const ip = &mod.intern_pool;
2050 if (!mod.backendSupportsFeature(.error_return_trace)) return;2051 if (!comp.config.any_error_tracing) return;
20512052
2052 assert(!block.is_comptime);2053 assert(!block.is_comptime);
2053 var err_trace_block = block.makeSubBlock();2054 var err_trace_block = block.makeSubBlock();
...@@ -6543,7 +6544,6 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref...@@ -6543,7 +6544,6 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
6543 const gpa = sema.gpa;6544 const gpa = sema.gpa;
6544 const src = sema.src;6545 const src = sema.src;
65456546
6546 if (!mod.backendSupportsFeature(.error_return_trace)) return .none;
6547 if (!block.ownerModule().error_tracing) return .none;6547 if (!block.ownerModule().error_tracing) return .none;
65486548
6549 if (block.is_comptime)6549 if (block.is_comptime)
...@@ -6728,7 +6728,7 @@ fn zirCall(...@@ -6728,7 +6728,7 @@ fn zirCall(
6728 input_is_error = false;6728 input_is_error = false;
6729 }6729 }
67306730
6731 if (mod.backendSupportsFeature(.error_return_trace) and block.ownerModule().error_tracing and6731 if (block.ownerModule().error_tracing and
6732 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))6732 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
6733 {6733 {
6734 const return_ty = sema.typeOf(call_inst);6734 const return_ty = sema.typeOf(call_inst);
...@@ -18759,8 +18759,6 @@ fn retWithErrTracing(...@@ -18759,8 +18759,6 @@ fn retWithErrTracing(
1875918759
18760fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {18760fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {
18761 const mod = sema.mod;18761 const mod = sema.mod;
18762 if (!mod.backendSupportsFeature(.error_return_trace)) return false;
18763
18764 return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing;18762 return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing;
18765}18763}
1876618764
...@@ -18768,8 +18766,6 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE...@@ -18768,8 +18766,6 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
18768 const mod = sema.mod;18766 const mod = sema.mod;
18769 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].save_err_ret_index;18767 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;
18773 if (!block.ownerModule().error_tracing) return;18769 if (!block.ownerModule().error_tracing) return;
1877418770
18775 // This is only relevant at runtime.18771 // This is only relevant at runtime.
...@@ -18795,7 +18791,6 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)...@@ -18795,7 +18791,6 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
18795 const mod = sema.mod;18791 const mod = sema.mod;
18796 const ip = &mod.intern_pool;18792 const ip = &mod.intern_pool;
1879718793
18798 if (!mod.backendSupportsFeature(.error_return_trace)) return;
18799 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;18794 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
18800 if (!start_block.ownerModule().error_tracing) return;18795 if (!start_block.ownerModule().error_tracing) return;
1880118796
...@@ -20068,8 +20063,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {...@@ -20068,8 +20063,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
2006820063
20069 if (sema.owner_func_index != .none and20064 if (sema.owner_func_index != .none and
20070 ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and20065 ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and
20071 block.ownerModule().error_tracing and20066 block.ownerModule().error_tracing)
20072 mod.backendSupportsFeature(.error_return_trace))
20073 {20067 {
20074 return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);20068 return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);
20075 }20069 }
src/target.zig+19
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const Type = @import("type.zig").Type;2const Type = @import("type.zig").Type;
3const AddressSpace = std.builtin.AddressSpace;3const AddressSpace = std.builtin.AddressSpace;
4const Alignment = @import("InternPool.zig").Alignment;4const Alignment = @import("InternPool.zig").Alignment;
5const Feature = @import("Module.zig").Feature;
56
6pub const default_stack_protector_buffer_size = 4;7pub const default_stack_protector_buffer_size = 4;
78
...@@ -665,6 +666,24 @@ pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBacken...@@ -665,6 +666,24 @@ pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBacken
665 };666 };
666}667}
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
668pub fn defaultEntrySymbolName(687pub fn defaultEntrySymbolName(
669 target: std.Target,688 target: std.Target,
670 /// May be `undefined` when `target` is not WASI.689 /// May be `undefined` when `target` is not WASI.