authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-26 00:24:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:55-07:00
log9a738c0be54c9bda0e57de9da84f86fc73bd5198
treeae754aeda344d8d9c359ccddb565155f45468d5e
parentf37c0a459382fa033cefc9bb139277436a78b25e

Module: intern the values of decls when they are marked alive

I'm not sure if this is the right place for this to happen, and it should become obsolete when comptime mutation is rewritten and the remaining legacy value tags are remove, so keeping this as a separate revertable commit.

7 files changed, 29 insertions(+), 27 deletions(-)

src/Module.zig+19-17
...@@ -6603,47 +6603,49 @@ fn reportRetryableFileError(...@@ -6603,47 +6603,49 @@ fn reportRetryableFileError(
6603 gop.value_ptr.* = err_msg;6603 gop.value_ptr.* = err_msg;
6604}6604}
66056605
6606pub fn markReferencedDeclsAlive(mod: *Module, val: Value) void {6606pub fn markReferencedDeclsAlive(mod: *Module, val: Value) Allocator.Error!void {
6607 switch (mod.intern_pool.indexToKey(val.toIntern())) {6607 switch (mod.intern_pool.indexToKey(val.toIntern())) {
6608 .variable => |variable| mod.markDeclIndexAlive(variable.decl),6608 .variable => |variable| try mod.markDeclIndexAlive(variable.decl),
6609 .extern_func => |extern_func| mod.markDeclIndexAlive(extern_func.decl),6609 .extern_func => |extern_func| try mod.markDeclIndexAlive(extern_func.decl),
6610 .func => |func| mod.markDeclIndexAlive(mod.funcPtr(func.index).owner_decl),6610 .func => |func| try mod.markDeclIndexAlive(mod.funcPtr(func.index).owner_decl),
6611 .error_union => |error_union| switch (error_union.val) {6611 .error_union => |error_union| switch (error_union.val) {
6612 .err_name => {},6612 .err_name => {},
6613 .payload => |payload| mod.markReferencedDeclsAlive(payload.toValue()),6613 .payload => |payload| try mod.markReferencedDeclsAlive(payload.toValue()),
6614 },6614 },
6615 .ptr => |ptr| {6615 .ptr => |ptr| {
6616 switch (ptr.addr) {6616 switch (ptr.addr) {
6617 .decl => |decl| mod.markDeclIndexAlive(decl),6617 .decl => |decl| try mod.markDeclIndexAlive(decl),
6618 .mut_decl => |mut_decl| mod.markDeclIndexAlive(mut_decl.decl),6618 .mut_decl => |mut_decl| try mod.markDeclIndexAlive(mut_decl.decl),
6619 .int, .comptime_field => {},6619 .int, .comptime_field => {},
6620 .eu_payload, .opt_payload => |parent| mod.markReferencedDeclsAlive(parent.toValue()),6620 .eu_payload, .opt_payload => |parent| try mod.markReferencedDeclsAlive(parent.toValue()),
6621 .elem, .field => |base_index| mod.markReferencedDeclsAlive(base_index.base.toValue()),6621 .elem, .field => |base_index| try mod.markReferencedDeclsAlive(base_index.base.toValue()),
6622 }6622 }
6623 if (ptr.len != .none) mod.markReferencedDeclsAlive(ptr.len.toValue());6623 if (ptr.len != .none) try mod.markReferencedDeclsAlive(ptr.len.toValue());
6624 },6624 },
6625 .opt => |opt| if (opt.val != .none) mod.markReferencedDeclsAlive(opt.val.toValue()),6625 .opt => |opt| if (opt.val != .none) try mod.markReferencedDeclsAlive(opt.val.toValue()),
6626 .aggregate => |aggregate| for (aggregate.storage.values()) |elem|6626 .aggregate => |aggregate| for (aggregate.storage.values()) |elem|
6627 mod.markReferencedDeclsAlive(elem.toValue()),6627 try mod.markReferencedDeclsAlive(elem.toValue()),
6628 .un => |un| {6628 .un => |un| {
6629 mod.markReferencedDeclsAlive(un.tag.toValue());6629 try mod.markReferencedDeclsAlive(un.tag.toValue());
6630 mod.markReferencedDeclsAlive(un.val.toValue());6630 try mod.markReferencedDeclsAlive(un.val.toValue());
6631 },6631 },
6632 else => {},6632 else => {},
6633 }6633 }
6634}6634}
66356635
6636pub fn markDeclAlive(mod: *Module, decl: *Decl) void {6636pub fn markDeclAlive(mod: *Module, decl: *Decl) Allocator.Error!void {
6637 if (decl.alive) return;6637 if (decl.alive) return;
6638 decl.alive = true;6638 decl.alive = true;
66396639
6640 decl.val = (try decl.val.intern(decl.ty, mod)).toValue();
6641
6640 // This is the first time we are marking this Decl alive. We must6642 // This is the first time we are marking this Decl alive. We must
6641 // therefore recurse into its value and mark any Decl it references6643 // therefore recurse into its value and mark any Decl it references
6642 // as also alive, so that any Decl referenced does not get garbage collected.6644 // as also alive, so that any Decl referenced does not get garbage collected.
6643 mod.markReferencedDeclsAlive(decl.val);6645 try mod.markReferencedDeclsAlive(decl.val);
6644}6646}
66456647
6646fn markDeclIndexAlive(mod: *Module, decl_index: Decl.Index) void {6648fn markDeclIndexAlive(mod: *Module, decl_index: Decl.Index) Allocator.Error!void {
6647 return mod.markDeclAlive(mod.declPtr(decl_index));6649 return mod.markDeclAlive(mod.declPtr(decl_index));
6648}6650}
66496651
src/Sema.zig+1-1
...@@ -5807,7 +5807,7 @@ pub fn analyzeExport(...@@ -5807,7 +5807,7 @@ pub fn analyzeExport(
5807 }5807 }
58085808
5809 // This decl is alive no matter what, since it's being exported5809 // This decl is alive no matter what, since it's being exported
5810 mod.markDeclAlive(exported_decl);5810 try mod.markDeclAlive(exported_decl);
5811 try sema.maybeQueueFuncBodyAnalysis(exported_decl_index);5811 try sema.maybeQueueFuncBodyAnalysis(exported_decl_index);
58125812
5813 const gpa = sema.gpa;5813 const gpa = sema.gpa;
src/arch/wasm/CodeGen.zig+2-2
...@@ -3019,7 +3019,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value) InnerError!WValue {...@@ -3019,7 +3019,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value) InnerError!WValue {
3019fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: Module.Decl.Index, offset: u32) InnerError!WValue {3019fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: Module.Decl.Index, offset: u32) InnerError!WValue {
3020 const mod = func.bin_file.base.options.module.?;3020 const mod = func.bin_file.base.options.module.?;
3021 const decl = mod.declPtr(decl_index);3021 const decl = mod.declPtr(decl_index);
3022 mod.markDeclAlive(decl);3022 try mod.markDeclAlive(decl);
3023 const ptr_ty = try mod.singleMutPtrType(decl.ty);3023 const ptr_ty = try mod.singleMutPtrType(decl.ty);
3024 return func.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index, offset);3024 return func.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index, offset);
3025}3025}
...@@ -3035,7 +3035,7 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: Module.Decl.Ind...@@ -3035,7 +3035,7 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: Module.Decl.Ind
3035 return WValue{ .imm32 = 0xaaaaaaaa };3035 return WValue{ .imm32 = 0xaaaaaaaa };
3036 }3036 }
30373037
3038 mod.markDeclAlive(decl);3038 try mod.markDeclAlive(decl);
3039 const atom_index = try func.bin_file.getOrCreateAtomForDecl(decl_index);3039 const atom_index = try func.bin_file.getOrCreateAtomForDecl(decl_index);
3040 const atom = func.bin_file.getAtom(atom_index);3040 const atom = func.bin_file.getAtom(atom_index);
30413041
src/codegen.zig+2-2
...@@ -673,7 +673,7 @@ fn lowerDeclRef(...@@ -673,7 +673,7 @@ fn lowerDeclRef(
673 return Result.ok;673 return Result.ok;
674 }674 }
675675
676 mod.markDeclAlive(decl);676 try mod.markDeclAlive(decl);
677677
678 const vaddr = try bin_file.getDeclVAddr(decl_index, .{678 const vaddr = try bin_file.getDeclVAddr(decl_index, .{
679 .parent_atom_index = reloc_info.parent_atom_index,679 .parent_atom_index = reloc_info.parent_atom_index,
...@@ -782,7 +782,7 @@ fn genDeclRef(...@@ -782,7 +782,7 @@ fn genDeclRef(
782 }782 }
783 }783 }
784784
785 mod.markDeclAlive(decl);785 try mod.markDeclAlive(decl);
786786
787 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !bin_file.options.single_threaded;787 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !bin_file.options.single_threaded;
788788
src/codegen/c.zig+1-1
...@@ -1923,7 +1923,7 @@ pub const DeclGen = struct {...@@ -1923,7 +1923,7 @@ pub const DeclGen = struct {
1923 fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: Decl.Index, export_index: u32) !void {1923 fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: Decl.Index, export_index: u32) !void {
1924 const mod = dg.module;1924 const mod = dg.module;
1925 const decl = mod.declPtr(decl_index);1925 const decl = mod.declPtr(decl_index);
1926 mod.markDeclAlive(decl);1926 try mod.markDeclAlive(decl);
19271927
1928 if (mod.decl_exports.get(decl_index)) |exports| {1928 if (mod.decl_exports.get(decl_index)) |exports| {
1929 try writer.writeAll(exports.items[export_index].options.name);1929 try writer.writeAll(exports.items[export_index].options.name);
src/codegen/llvm.zig+3-3
...@@ -3252,7 +3252,7 @@ pub const DeclGen = struct {...@@ -3252,7 +3252,7 @@ pub const DeclGen = struct {
3252 else => unreachable,3252 else => unreachable,
3253 };3253 };
3254 const fn_decl = dg.module.declPtr(fn_decl_index);3254 const fn_decl = dg.module.declPtr(fn_decl_index);
3255 dg.module.markDeclAlive(fn_decl);3255 try dg.module.markDeclAlive(fn_decl);
3256 return dg.resolveLlvmFunction(fn_decl_index);3256 return dg.resolveLlvmFunction(fn_decl_index);
3257 },3257 },
3258 .int => |int| {3258 .int => |int| {
...@@ -3831,7 +3831,7 @@ pub const DeclGen = struct {...@@ -3831,7 +3831,7 @@ pub const DeclGen = struct {
3831 ) Error!*llvm.Value {3831 ) Error!*llvm.Value {
3832 const mod = dg.module;3832 const mod = dg.module;
3833 const decl = mod.declPtr(decl_index);3833 const decl = mod.declPtr(decl_index);
3834 mod.markDeclAlive(decl);3834 try mod.markDeclAlive(decl);
3835 const ptr_ty = try mod.singleMutPtrType(decl.ty);3835 const ptr_ty = try mod.singleMutPtrType(decl.ty);
3836 return try dg.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index);3836 return try dg.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index);
3837 }3837 }
...@@ -4006,7 +4006,7 @@ pub const DeclGen = struct {...@@ -4006,7 +4006,7 @@ pub const DeclGen = struct {
4006 return self.lowerPtrToVoid(tv.ty);4006 return self.lowerPtrToVoid(tv.ty);
4007 }4007 }
40084008
4009 mod.markDeclAlive(decl);4009 try mod.markDeclAlive(decl);
40104010
4011 const llvm_decl_val = if (is_fn_body)4011 const llvm_decl_val = if (is_fn_body)
4012 try self.resolveLlvmFunction(decl_index)4012 try self.resolveLlvmFunction(decl_index)
src/codegen/spirv.zig+1-1
...@@ -256,7 +256,7 @@ pub const DeclGen = struct {...@@ -256,7 +256,7 @@ pub const DeclGen = struct {
256 /// Note: Function does not actually generate the decl.256 /// Note: Function does not actually generate the decl.
257 fn resolveDecl(self: *DeclGen, decl_index: Module.Decl.Index) !SpvModule.Decl.Index {257 fn resolveDecl(self: *DeclGen, decl_index: Module.Decl.Index) !SpvModule.Decl.Index {
258 const decl = self.module.declPtr(decl_index);258 const decl = self.module.declPtr(decl_index);
259 self.module.markDeclAlive(decl);259 try self.module.markDeclAlive(decl);
260260
261 const entry = try self.decl_link.getOrPut(decl_index);261 const entry = try self.decl_link.getOrPut(decl_index);
262 if (!entry.found_existing) {262 if (!entry.found_existing) {