authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-14 19:49:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:19-07:00
log2596f5d92593a5f7dad21d9a54f2d08f33639432
treee44cd8e057f87b736a915550393bd023f151b771
parent7e7d5dc958b08dc0f671cd1b119cd831fc877d16

update bin_file.options references in Sema

mainly pertaining to error return tracing

5 files changed, 66 insertions(+), 45 deletions(-)

src/Compilation/Config.zig+22
......@@ -9,6 +9,10 @@ link_libunwind: bool,
99any_unwind_tables: bool,
1010any_c_source_files: bool,
1111any_non_single_threaded: bool,
12/// This is true if any Module has error_tracing set to true. Function types
13/// and function calling convention depend on this global value, however, other
14/// kinds of error tracing are omitted depending on the per-Module setting.
15any_error_tracing: bool,
1216pie: bool,
1317/// If this is true then linker code is responsible for making an LLVM IR
1418/// Module, outputting it to an object file, and then linking that together
......@@ -34,6 +38,8 @@ is_test: bool,
3438test_evented_io: bool,
3539entry: ?[]const u8,
3640debug_format: DebugFormat,
41root_strip: bool,
42root_error_tracing: bool,
3743
3844pub const CFrontend = enum { clang, aro };
3945
......@@ -51,6 +57,7 @@ pub const Options = struct {
5157 emit_bin: bool,
5258 root_optimize_mode: ?std.builtin.OptimizeMode = null,
5359 root_strip: ?bool = null,
60 root_error_tracing: ?bool = null,
5461 link_mode: ?std.builtin.LinkMode = null,
5562 ensure_libc_on_non_freestanding: bool = false,
5663 ensure_libcpp_on_non_freestanding: bool = false,
......@@ -60,6 +67,7 @@ pub const Options = struct {
6067 any_dyn_libs: bool = false,
6168 any_c_source_files: bool = false,
6269 any_non_stripped: bool = false,
70 any_error_tracing: bool = false,
6371 emit_llvm_ir: bool = false,
6472 emit_llvm_bc: bool = false,
6573 link_libc: ?bool = null,
......@@ -395,6 +403,17 @@ pub fn resolve(options: Options) !Config {
395403 };
396404 };
397405
406 const root_error_tracing = b: {
407 if (options.root_error_tracing) |x| break :b x;
408 if (root_strip) break :b false;
409 break :b switch (root_optimize_mode) {
410 .Debug => true,
411 .ReleaseSafe, .ReleaseFast, .ReleaseSmall => false,
412 };
413 };
414
415 const any_error_tracing = root_error_tracing or options.any_error_tracing;
416
398417 return .{
399418 .output_mode = options.output_mode,
400419 .have_zcu = options.have_zcu,
......@@ -407,6 +426,8 @@ pub fn resolve(options: Options) !Config {
407426 .any_unwind_tables = any_unwind_tables,
408427 .any_c_source_files = options.any_c_source_files,
409428 .any_non_single_threaded = options.any_non_single_threaded,
429 .any_error_tracing = any_error_tracing,
430 .root_error_tracing = root_error_tracing,
410431 .pie = pie,
411432 .lto = lto,
412433 .import_memory = import_memory,
......@@ -419,6 +440,7 @@ pub fn resolve(options: Options) !Config {
419440 .entry = entry,
420441 .wasi_exec_model = wasi_exec_model,
421442 .debug_format = debug_format,
443 .root_strip = root_strip,
422444 };
423445}
424446
src/Package/Module.zig+2-8
......@@ -114,9 +114,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
114114 const strip = b: {
115115 if (options.inherited.strip) |x| break :b x;
116116 if (options.parent) |p| break :b p.strip;
117 if (optimize_mode == .ReleaseSmall) break :b true;
118 if (!target_util.hasDebugInfo(target)) break :b true;
119 break :b false;
117 break :b options.global.root_strip;
120118 };
121119
122120 const valgrind = b: {
......@@ -156,11 +154,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
156154 const error_tracing = b: {
157155 if (options.inherited.error_tracing) |x| break :b x;
158156 if (options.parent) |p| break :b p.error_tracing;
159 if (strip) break :b false;
160 break :b switch (optimize_mode) {
161 .Debug => true,
162 .ReleaseSafe, .ReleaseFast, .ReleaseSmall => false,
163 };
157 break :b options.global.root_error_tracing;
164158 };
165159
166160 const pic = b: {
src/Sema.zig+32-32
......@@ -1329,13 +1329,13 @@ fn analyzeBodyInner(
13291329 },
13301330 .dbg_block_begin => {
13311331 dbg_block_begins += 1;
1332 try sema.zirDbgBlockBegin(block);
1332 try zirDbgBlockBegin(block);
13331333 i += 1;
13341334 continue;
13351335 },
13361336 .dbg_block_end => {
13371337 dbg_block_begins -= 1;
1338 try sema.zirDbgBlockEnd(block);
1338 try zirDbgBlockEnd(block);
13391339 i += 1;
13401340 continue;
13411341 },
......@@ -1830,17 +1830,17 @@ fn analyzeBodyInner(
18301830 };
18311831
18321832 // balance out dbg_block_begins in case of early noreturn
1833 const noreturn_inst = block.instructions.popOrNull();
1834 while (dbg_block_begins > 0) {
1835 dbg_block_begins -= 1;
1836 if (block.is_comptime or mod.comp.bin_file.options.strip) continue;
1837
1838 _ = try block.addInst(.{
1839 .tag = .dbg_block_end,
1840 .data = undefined,
1841 });
1833 if (!block.is_comptime and !block.ownerModule().strip) {
1834 const noreturn_inst = block.instructions.popOrNull();
1835 while (dbg_block_begins > 0) {
1836 dbg_block_begins -= 1;
1837 _ = try block.addInst(.{
1838 .tag = .dbg_block_end,
1839 .data = undefined,
1840 });
1841 }
1842 if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some);
18421843 }
1843 if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some);
18441844
18451845 // We may have overwritten the capture scope due to a `repeat` instruction where
18461846 // the body had a capture; restore it now.
......@@ -6272,7 +6272,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
62726272 // ZIR code that possibly will need to generate runtime code. So error messages
62736273 // and other source locations must not rely on sema.src being set from dbg_stmt
62746274 // instructions.
6275 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
6275 if (block.is_comptime or block.ownerModule().strip) return;
62766276
62776277 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
62786278
......@@ -6297,8 +6297,8 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
62976297 });
62986298}
62996299
6300fn zirDbgBlockBegin(sema: *Sema, block: *Block) CompileError!void {
6301 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
6300fn zirDbgBlockBegin(block: *Block) CompileError!void {
6301 if (block.is_comptime or block.ownerModule().strip) return;
63026302
63036303 _ = try block.addInst(.{
63046304 .tag = .dbg_block_begin,
......@@ -6306,8 +6306,8 @@ fn zirDbgBlockBegin(sema: *Sema, block: *Block) CompileError!void {
63066306 });
63076307}
63086308
6309fn zirDbgBlockEnd(sema: *Sema, block: *Block) CompileError!void {
6310 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
6309fn zirDbgBlockEnd(block: *Block) CompileError!void {
6310 if (block.is_comptime or block.ownerModule().strip) return;
63116311
63126312 _ = try block.addInst(.{
63136313 .tag = .dbg_block_end,
......@@ -6321,7 +6321,7 @@ fn zirDbgVar(
63216321 inst: Zir.Inst.Index,
63226322 air_tag: Air.Inst.Tag,
63236323) CompileError!void {
6324 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
6324 if (block.is_comptime or block.ownerModule().strip) return;
63256325
63266326 const str_op = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_op;
63276327 const operand = try sema.resolveInst(str_op.operand);
......@@ -6519,7 +6519,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
65196519 const src = sema.src;
65206520
65216521 if (!mod.backendSupportsFeature(.error_return_trace)) return .none;
6522 if (!mod.comp.bin_file.options.error_return_tracing) return .none;
6522 if (!block.ownerModule().error_tracing) return .none;
65236523
65246524 if (block.is_comptime)
65256525 return .none;
......@@ -6703,7 +6703,7 @@ fn zirCall(
67036703 input_is_error = false;
67046704 }
67056705
6706 if (mod.backendSupportsFeature(.error_return_trace) and mod.comp.bin_file.options.error_return_tracing and
6706 if (mod.backendSupportsFeature(.error_return_trace) and block.ownerModule().error_tracing and
67076707 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
67086708 {
67096709 const return_ty = sema.typeOf(call_inst);
......@@ -7456,7 +7456,7 @@ fn analyzeCall(
74567456 new_fn_info.return_type = sema.fn_ret_ty.toIntern();
74577457 const new_func_resolved_ty = try mod.funcType(new_fn_info);
74587458 if (!is_comptime_call and !block.is_typeof) {
7459 try sema.emitDbgInline(block, prev_fn_index, module_fn_index, new_func_resolved_ty, .dbg_inline_begin);
7459 try emitDbgInline(block, prev_fn_index, module_fn_index, new_func_resolved_ty, .dbg_inline_begin);
74607460
74617461 const zir_tags = sema.code.instructions.items(.tag);
74627462 for (fn_info.param_body) |param| switch (zir_tags[@intFromEnum(param)]) {
......@@ -7494,7 +7494,7 @@ fn analyzeCall(
74947494 if (!is_comptime_call and !block.is_typeof and
74957495 sema.typeOf(result).zigTypeTag(mod) != .NoReturn)
74967496 {
7497 try sema.emitDbgInline(
7497 try emitDbgInline(
74987498 block,
74997499 module_fn_index,
75007500 prev_fn_index,
......@@ -8067,15 +8067,13 @@ fn resolveTupleLazyValues(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type)
80678067}
80688068
80698069fn emitDbgInline(
8070 sema: *Sema,
80718070 block: *Block,
80728071 old_func: InternPool.Index,
80738072 new_func: InternPool.Index,
80748073 new_func_ty: Type,
80758074 tag: Air.Inst.Tag,
80768075) CompileError!void {
8077 const mod = sema.mod;
8078 if (mod.comp.bin_file.options.strip) return;
8076 if (block.ownerModule().strip) return;
80798077
80808078 // Recursive inline call; no dbg_inline needed.
80818079 if (old_func == new_func) return;
......@@ -9109,7 +9107,7 @@ fn handleExternLibName(
91099107 );
91109108 break :blk;
91119109 }
9112 if (!target.isWasm() and !comp.bin_file.options.pic) {
9110 if (!target.isWasm() and !block.ownerModule().pic) {
91139111 return sema.fail(
91149112 block,
91159113 src_loc,
......@@ -18738,16 +18736,16 @@ fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {
1873818736 const mod = sema.mod;
1873918737 if (!mod.backendSupportsFeature(.error_return_trace)) return false;
1874018738
18741 return fn_ret_ty.isError(mod) and
18742 mod.comp.bin_file.options.error_return_tracing;
18739 return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing;
1874318740}
1874418741
1874518742fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
1874618743 const mod = sema.mod;
1874718744 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].save_err_ret_index;
1874818745
18746 // TODO: replace all of these checks with logic in module creation
1874918747 if (!mod.backendSupportsFeature(.error_return_trace)) return;
18750 if (!mod.comp.bin_file.options.error_return_tracing) return;
18748 if (!block.ownerModule().error_tracing) return;
1875118749
1875218750 // This is only relevant at runtime.
1875318751 if (block.is_comptime or block.is_typeof) return;
......@@ -18774,7 +18772,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1877418772
1877518773 if (!mod.backendSupportsFeature(.error_return_trace)) return;
1877618774 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
18777 if (!mod.comp.bin_file.options.error_return_tracing) return;
18775 if (!start_block.ownerModule().error_tracing) return;
1877818776
1877918777 const tracy = trace(@src());
1878018778 defer tracy.end();
......@@ -20045,7 +20043,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
2004520043
2004620044 if (sema.owner_func_index != .none and
2004720045 ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and
20048 mod.comp.bin_file.options.error_return_tracing and
20046 mod.ownerModule().error_tracing and
2004920047 mod.backendSupportsFeature(.error_return_trace))
2005020048 {
2005120049 return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);
......@@ -34504,7 +34502,9 @@ pub fn resolveFnTypes(sema: *Sema, fn_ty: Type) CompileError!void {
3450434502
3450534503 try sema.resolveTypeFully(Type.fromInterned(fn_ty_info.return_type));
3450634504
34507 if (mod.comp.bin_file.options.error_return_tracing and Type.fromInterned(fn_ty_info.return_type).isError(mod)) {
34505 if (mod.comp.config.any_error_tracing and
34506 Type.fromInterned(fn_ty_info.return_type).isError(mod))
34507 {
3450834508 // Ensure the type exists so that backends can assume that.
3450934509 _ = try sema.getBuiltinType("StackTrace");
3451034510 }
src/codegen/llvm.zig+5-5
......@@ -1398,7 +1398,7 @@ pub const Object = struct {
13981398 };
13991399
14001400 const err_return_tracing = Type.fromInterned(fn_info.return_type).isError(mod) and
1401 mod.comp.bin_file.options.error_return_tracing;
1401 mod.comp.config.any_error_tracing;
14021402
14031403 const err_ret_trace: Builder.Value = if (err_return_tracing) param: {
14041404 const param = wip.arg(llvm_arg_i);
......@@ -2820,7 +2820,7 @@ pub const Object = struct {
28202820 }
28212821
28222822 if (Type.fromInterned(fn_info.return_type).isError(mod) and
2823 o.module.comp.bin_file.options.error_return_tracing)
2823 o.module.comp.config.any_error_tracing)
28242824 {
28252825 const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType());
28262826 try param_di_types.append(try o.lowerDebugType(ptr_ty, .full));
......@@ -2988,7 +2988,7 @@ pub const Object = struct {
29882988 }
29892989
29902990 const err_return_tracing = Type.fromInterned(fn_info.return_type).isError(mod) and
2991 mod.comp.bin_file.options.error_return_tracing;
2991 mod.comp.config.any_error_tracing;
29922992
29932993 if (err_return_tracing) {
29942994 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
......@@ -3677,7 +3677,7 @@ pub const Object = struct {
36773677 }
36783678
36793679 if (Type.fromInterned(fn_info.return_type).isError(mod) and
3680 mod.comp.bin_file.options.error_return_tracing)
3680 mod.comp.config.any_error_tracing)
36813681 {
36823682 const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType());
36833683 try llvm_params.append(o.gpa, try o.lowerType(ptr_ty));
......@@ -5142,7 +5142,7 @@ pub const FuncGen = struct {
51425142 };
51435143
51445144 const err_return_tracing = return_type.isError(mod) and
5145 o.module.comp.bin_file.options.error_return_tracing;
5145 o.module.comp.config.any_error_tracing;
51465146 if (err_return_tracing) {
51475147 assert(self.err_ret_trace != .none);
51485148 try llvm_args.append(self.err_ret_trace);
src/main.zig+5
......@@ -1055,6 +1055,8 @@ fn buildOutputType(
10551055 create_module.opts.any_unwind_tables = true;
10561056 if (mod_opts.strip == false)
10571057 create_module.opts.any_non_stripped = true;
1058 if (mod_opts.error_tracing == true)
1059 create_module.opts.any_error_tracing = true;
10581060
10591061 const root_src = try introspect.resolvePath(arena, root_src_orig);
10601062 try create_module.modules.put(arena, mod_name, .{
......@@ -2535,6 +2537,8 @@ fn buildOutputType(
25352537 create_module.opts.any_unwind_tables = true;
25362538 if (mod_opts.strip == false)
25372539 create_module.opts.any_non_stripped = true;
2540 if (mod_opts.error_tracing == true)
2541 create_module.opts.any_error_tracing = true;
25382542
25392543 const src_path = try introspect.resolvePath(arena, unresolved_src_path);
25402544 try create_module.modules.put(arena, "main", .{
......@@ -3741,6 +3745,7 @@ fn createModule(
37413745 create_module.opts.resolved_target = resolved_target;
37423746 create_module.opts.root_optimize_mode = cli_mod.inherited.optimize_mode;
37433747 create_module.opts.root_strip = cli_mod.inherited.strip;
3748 create_module.opts.root_error_tracing = cli_mod.inherited.error_tracing;
37443749 const target = resolved_target.result;
37453750
37463751 // First, remove libc, libc++, and compiler_rt libraries from the system libraries list.