authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 04:06:39+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 13:48:06+00:00
log26a94e8481385619ae049143dd67e551f333fa3f
tree2106272eed6fb7b15a8309c6fc5137e6fec93234
parent152a2ceaf738301cd59165a4f17d915391321bdc
signaturelock-open Commit is signed but in an unrecognized format.

Zcu: eliminate `Decl.alive` field

Legacy anon decls now have three uses: * Type owner decls * Function owner decls * `@export` and `@extern` Therefore, there are no longer any cases where we wish to explicitly omit legacy anon decls from the binary. This means we can remove the concept of an "alive" vs "dead" `Decl`, which also allows us to remove the separate `anon_work_queue` in `Compilation`.

10 files changed, 2 insertions(+), 101 deletions(-)

src/Compilation.zig+1-18
......@@ -102,7 +102,6 @@ link_errors: std.ArrayListUnmanaged(link.File.ErrorMsg) = .{},
102102lld_errors: std.ArrayListUnmanaged(LldError) = .{},
103103
104104work_queue: std.fifo.LinearFifo(Job, .Dynamic),
105anon_work_queue: std.fifo.LinearFifo(Job, .Dynamic),
106105
107106/// These jobs are to invoke the Clang compiler to create an object file, which
108107/// gets linked with the Compilation.
......@@ -1417,7 +1416,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
14171416 .emit_llvm_ir = options.emit_llvm_ir,
14181417 .emit_llvm_bc = options.emit_llvm_bc,
14191418 .work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),
1420 .anon_work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),
14211419 .c_object_work_queue = std.fifo.LinearFifo(*CObject, .Dynamic).init(gpa),
14221420 .win32_resource_work_queue = if (build_options.only_core_functionality) {} else std.fifo.LinearFifo(*Win32Resource, .Dynamic).init(gpa),
14231421 .astgen_work_queue = std.fifo.LinearFifo(*Module.File, .Dynamic).init(gpa),
......@@ -1840,7 +1838,6 @@ pub fn destroy(comp: *Compilation) void {
18401838 if (comp.module) |zcu| zcu.deinit();
18411839 comp.cache_use.deinit();
18421840 comp.work_queue.deinit();
1843 comp.anon_work_queue.deinit();
18441841 comp.c_object_work_queue.deinit();
18451842 if (!build_options.only_core_functionality) {
18461843 comp.win32_resource_work_queue.deinit();
......@@ -3354,18 +3351,11 @@ pub fn performAllTheWork(
33543351 mod.sema_prog_node = undefined;
33553352 };
33563353
3357 // In this main loop we give priority to non-anonymous Decls in the work queue, so
3358 // that they can establish references to anonymous Decls, setting alive=true in the
3359 // backend, preventing anonymous Decls from being prematurely destroyed.
33603354 while (true) {
33613355 if (comp.work_queue.readItem()) |work_item| {
33623356 try processOneJob(comp, work_item, main_progress_node);
33633357 continue;
33643358 }
3365 if (comp.anon_work_queue.readItem()) |work_item| {
3366 try processOneJob(comp, work_item, main_progress_node);
3367 continue;
3368 }
33693359 if (comp.module) |zcu| {
33703360 // If there's no work queued, check if there's anything outdated
33713361 // which we need to work on, and queue it if so.
......@@ -3413,14 +3403,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
34133403
34143404 assert(decl.has_tv);
34153405
3416 if (decl.alive) {
3417 try module.linkerUpdateDecl(decl_index);
3418 return;
3419 }
3420
3421 // Instead of sending this decl to the linker, we actually will delete it
3422 // because we found out that it in fact was never referenced.
3423 module.deleteUnusedDecl(decl_index);
3406 try module.linkerUpdateDecl(decl_index);
34243407 return;
34253408 },
34263409 }
src/InternPool.zig-1
......@@ -6740,7 +6740,6 @@ fn finishFuncInstance(
67406740 .zir_decl_index = fn_owner_decl.zir_decl_index,
67416741 .is_pub = fn_owner_decl.is_pub,
67426742 .is_exported = fn_owner_decl.is_exported,
6743 .alive = true,
67446743 .kind = .anon,
67456744 });
67466745 errdefer ip.destroyDecl(gpa, decl_index);
src/Module.zig+1-63
......@@ -394,15 +394,6 @@ pub const Decl = struct {
394394 is_pub: bool,
395395 /// Whether the corresponding AST decl has a `export` keyword.
396396 is_exported: bool,
397 /// Flag used by garbage collection to mark and sweep.
398 /// Decls which correspond to an AST node always have this field set to `true`.
399 /// Anonymous Decls are initialized with this field set to `false` and then it
400 /// is the responsibility of machine code backends to mark it `true` whenever
401 /// a `decl_ref` Value is encountered that points to this Decl.
402 /// When the `codegen_decl` job is encountered in the main work queue, if the
403 /// Decl is marked alive, then it sends the Decl to the linker. Otherwise it
404 /// deletes the Decl on the spot.
405 alive: bool,
406397 /// If true `name` is already fully qualified.
407398 name_fully_qualified: bool = false,
408399 /// What kind of a declaration is this.
......@@ -3525,7 +3516,6 @@ fn semaFile(mod: *Module, file: *File) SemaError!void {
35253516 new_decl.is_exported = false;
35263517 new_decl.alignment = .none;
35273518 new_decl.@"linksection" = .none;
3528 new_decl.alive = true; // This Decl corresponds to a File and is therefore always alive.
35293519 new_decl.analysis = .in_progress;
35303520
35313521 if (file.status != .success_zir) {
......@@ -4375,7 +4365,6 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
43754365 const decl = zcu.declPtr(decl_index);
43764366 const was_exported = decl.is_exported;
43774367 assert(decl.kind == kind); // ZIR tracking should preserve this
4378 assert(decl.alive);
43794368 decl.name = decl_name;
43804369 decl.src_node = decl_node;
43814370 decl.src_line = line;
......@@ -4392,7 +4381,6 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
43924381 new_decl.is_pub = declaration.flags.is_pub;
43934382 new_decl.is_exported = declaration.flags.is_export;
43944383 new_decl.zir_decl_index = tracked_inst.toOptional();
4395 new_decl.alive = true; // This Decl corresponds to an AST node and is therefore always alive.
43964384 break :decl_index .{ false, new_decl_index };
43974385 };
43984386
......@@ -4470,12 +4458,8 @@ pub fn abortAnonDecl(mod: *Module, decl_index: Decl.Index) void {
44704458
44714459/// Finalize the creation of an anon decl.
44724460pub fn finalizeAnonDecl(mod: *Module, decl_index: Decl.Index) Allocator.Error!void {
4473 // The Decl starts off with alive=false and the codegen backend will set alive=true
4474 // if the Decl is referenced by an instruction or another constant. Otherwise,
4475 // the Decl will be garbage collected by the `codegen_decl` task instead of sent
4476 // to the linker.
44774461 if (mod.declPtr(decl_index).typeOf(mod).isFnOrHasRuntimeBits(mod)) {
4478 try mod.comp.anon_work_queue.writeItem(.{ .codegen_decl = decl_index });
4462 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
44794463 }
44804464}
44814465
......@@ -4815,7 +4799,6 @@ pub fn allocateNewDecl(
48154799 .zir_decl_index = .none,
48164800 .is_pub = false,
48174801 .is_exported = false,
4818 .alive = false,
48194802 .kind = .anon,
48204803 });
48214804
......@@ -5582,51 +5565,6 @@ fn reportRetryableFileError(
55825565 gop.value_ptr.* = err_msg;
55835566}
55845567
5585pub fn markReferencedDeclsAlive(mod: *Module, val: Value) Allocator.Error!void {
5586 switch (mod.intern_pool.indexToKey(val.toIntern())) {
5587 .variable => |variable| try mod.markDeclIndexAlive(variable.decl),
5588 .extern_func => |extern_func| try mod.markDeclIndexAlive(extern_func.decl),
5589 .func => |func| try mod.markDeclIndexAlive(func.owner_decl),
5590 .error_union => |error_union| switch (error_union.val) {
5591 .err_name => {},
5592 .payload => |payload| try mod.markReferencedDeclsAlive(Value.fromInterned(payload)),
5593 },
5594 .slice => |slice| {
5595 try mod.markReferencedDeclsAlive(Value.fromInterned(slice.ptr));
5596 try mod.markReferencedDeclsAlive(Value.fromInterned(slice.len));
5597 },
5598 .ptr => |ptr| switch (ptr.addr) {
5599 .decl => |decl| try mod.markDeclIndexAlive(decl),
5600 .anon_decl => {},
5601 .int, .comptime_field, .comptime_alloc => {},
5602 .eu_payload, .opt_payload => |parent| try mod.markReferencedDeclsAlive(Value.fromInterned(parent)),
5603 .elem, .field => |base_index| try mod.markReferencedDeclsAlive(Value.fromInterned(base_index.base)),
5604 },
5605 .opt => |opt| if (opt.val != .none) try mod.markReferencedDeclsAlive(Value.fromInterned(opt.val)),
5606 .aggregate => |aggregate| for (aggregate.storage.values()) |elem|
5607 try mod.markReferencedDeclsAlive(Value.fromInterned(elem)),
5608 .un => |un| {
5609 if (un.tag != .none) try mod.markReferencedDeclsAlive(Value.fromInterned(un.tag));
5610 try mod.markReferencedDeclsAlive(Value.fromInterned(un.val));
5611 },
5612 else => {},
5613 }
5614}
5615
5616pub fn markDeclAlive(mod: *Module, decl: *Decl) Allocator.Error!void {
5617 if (decl.alive) return;
5618 decl.alive = true;
5619
5620 // This is the first time we are marking this Decl alive. We must
5621 // therefore recurse into its value and mark any Decl it references
5622 // as also alive, so that any Decl referenced does not get garbage collected.
5623 try mod.markReferencedDeclsAlive(decl.val);
5624}
5625
5626fn markDeclIndexAlive(mod: *Module, decl_index: Decl.Index) Allocator.Error!void {
5627 return mod.markDeclAlive(mod.declPtr(decl_index));
5628}
5629
56305568pub fn addGlobalAssembly(mod: *Module, decl_index: Decl.Index, source: []const u8) !void {
56315569 const gop = try mod.global_assembly.getOrPut(mod.gpa, decl_index);
56325570 if (gop.found_existing) {
src/Sema.zig-2
......@@ -6445,8 +6445,6 @@ pub fn analyzeExport(
64456445 return sema.fail(block, src, "export target cannot be extern", .{});
64466446 }
64476447
6448 // This decl is alive no matter what, since it's being exported
6449 try mod.markDeclAlive(exported_decl);
64506448 try sema.maybeQueueFuncBodyAnalysis(exported_decl_index);
64516449
64526450 try addExport(mod, .{
src/arch/wasm/CodeGen.zig-2
......@@ -3121,7 +3121,6 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, offset: u32) InnerError!WValue
31213121fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {
31223122 const mod = func.bin_file.base.comp.module.?;
31233123 const decl = mod.declPtr(decl_index);
3124 try mod.markDeclAlive(decl);
31253124 const ptr_ty = try mod.singleMutPtrType(decl.typeOf(mod));
31263125 return func.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index, offset);
31273126}
......@@ -3178,7 +3177,6 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.Decl
31783177 return WValue{ .imm32 = 0xaaaaaaaa };
31793178 }
31803179
3181 try mod.markDeclAlive(decl);
31823180 const atom_index = try func.bin_file.getOrCreateAtomForDecl(decl_index);
31833181 const atom = func.bin_file.getAtom(atom_index);
31843182
src/arch/x86_64/CodeGen.zig-2
......@@ -12263,7 +12263,6 @@ fn genCall(self: *Self, info: union(enum) {
1226312263 },
1226412264 }) {
1226512265 .func => |func| {
12266 try mod.markDeclAlive(mod.declPtr(func.owner_decl));
1226712266 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
1226812267 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl);
1226912268 const sym = elf_file.symbol(sym_index);
......@@ -12323,7 +12322,6 @@ fn genCall(self: *Self, info: union(enum) {
1232312322 },
1232412323 .extern_func => |extern_func| {
1232512324 const owner_decl = mod.declPtr(extern_func.decl);
12326 try mod.markDeclAlive(owner_decl);
1232712325 const lib_name = mod.intern_pool.stringToSliceUnwrap(extern_func.lib_name);
1232812326 const decl_name = mod.intern_pool.stringToSlice(owner_decl.name);
1232912327 try self.genExternSymbolRef(.call, lib_name, decl_name);
src/codegen.zig-4
......@@ -835,8 +835,6 @@ fn lowerDeclRef(
835835 return Result.ok;
836836 }
837837
838 try zcu.markDeclAlive(decl);
839
840838 const vaddr = try lf.getDeclVAddr(decl_index, .{
841839 .parent_atom_index = reloc_info.parent_atom_index,
842840 .offset = code.items.len,
......@@ -958,8 +956,6 @@ fn genDeclRef(
958956 }
959957 }
960958
961 try zcu.markDeclAlive(decl);
962
963959 const decl_namespace = zcu.namespacePtr(decl.src_namespace);
964960 const single_threaded = decl_namespace.file_scope.mod.single_threaded;
965961 const is_threadlocal = tv.val.isPtrToThreadLocal(zcu) and !single_threaded;
src/codegen/c.zig-1
......@@ -2010,7 +2010,6 @@ pub const DeclGen = struct {
20102010 fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: InternPool.DeclIndex, export_index: u32) !void {
20112011 const mod = dg.module;
20122012 const decl = mod.declPtr(decl_index);
2013 try mod.markDeclAlive(decl);
20142013
20152014 if (mod.decl_exports.get(decl_index)) |exports| {
20162015 try writer.print("{ }", .{
src/codegen/llvm.zig-7
......@@ -3722,15 +3722,11 @@ pub const Object = struct {
37223722 => unreachable, // non-runtime values
37233723 .extern_func => |extern_func| {
37243724 const fn_decl_index = extern_func.decl;
3725 const fn_decl = mod.declPtr(fn_decl_index);
3726 try mod.markDeclAlive(fn_decl);
37273725 const function_index = try o.resolveLlvmFunction(fn_decl_index);
37283726 return function_index.ptrConst(&o.builder).global.toConst();
37293727 },
37303728 .func => |func| {
37313729 const fn_decl_index = func.owner_decl;
3732 const fn_decl = mod.declPtr(fn_decl_index);
3733 try mod.markDeclAlive(fn_decl);
37343730 const function_index = try o.resolveLlvmFunction(fn_decl_index);
37353731 return function_index.ptrConst(&o.builder).global.toConst();
37363732 },
......@@ -4262,7 +4258,6 @@ pub const Object = struct {
42624258 fn lowerParentPtrDecl(o: *Object, decl_index: InternPool.DeclIndex) Allocator.Error!Builder.Constant {
42634259 const mod = o.module;
42644260 const decl = mod.declPtr(decl_index);
4265 try mod.markDeclAlive(decl);
42664261 const ptr_ty = try mod.singleMutPtrType(decl.typeOf(mod));
42674262 return o.lowerDeclRefValue(ptr_ty, decl_index);
42684263 }
......@@ -4455,8 +4450,6 @@ pub const Object = struct {
44554450 if ((!is_fn_body and !decl_ty.hasRuntimeBits(mod)) or
44564451 (is_fn_body and mod.typeToFunc(decl_ty).?.is_generic)) return o.lowerPtrToVoid(ty);
44574452
4458 try mod.markDeclAlive(decl);
4459
44604453 const llvm_global = if (is_fn_body)
44614454 (try o.resolveLlvmFunction(decl_index)).ptrConst(&o.builder).global
44624455 else
src/codegen/spirv.zig-1
......@@ -255,7 +255,6 @@ pub const Object = struct {
255255 pub fn resolveDecl(self: *Object, mod: *Module, decl_index: InternPool.DeclIndex) !SpvModule.Decl.Index {
256256 const decl = mod.declPtr(decl_index);
257257 assert(decl.has_tv); // TODO: Do we need to handle a situation where this is false?
258 try mod.markDeclAlive(decl);
259258
260259 const entry = try self.decl_link.getOrPut(self.gpa, decl_index);
261260 if (!entry.found_existing) {