authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-28 02:41:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:56-07:00
log3b6ca1d35b950d67fff5964f0063dadf01f30e2d
tree953815632535b965c7d32318ca515d8926cf4c4b
parentd40b83de45db27c8c3e7a1f2ccf892563df43637

Module: move memoized data to the intern pool

This avoids memory management bugs with the previous implementation.

11 files changed, 264 insertions(+), 140 deletions(-)

src/InternPool.zig+106-3
......@@ -217,6 +217,11 @@ pub const Key = union(enum) {
217217 /// An instance of a union.
218218 un: Union,
219219
220 /// A declaration with a memoized value.
221 memoized_decl: MemoizedDecl,
222 /// A comptime function call with a memoized result.
223 memoized_call: Key.MemoizedCall,
224
220225 pub const IntType = std.builtin.Type.Int;
221226
222227 pub const ErrorUnionType = struct {
......@@ -609,6 +614,17 @@ pub const Key = union(enum) {
609614 };
610615 };
611616
617 pub const MemoizedDecl = struct {
618 val: Index,
619 decl: Module.Decl.Index,
620 };
621
622 pub const MemoizedCall = struct {
623 func: Module.Fn.Index,
624 arg_values: []const Index,
625 result: Index,
626 };
627
612628 pub fn hash32(key: Key, ip: *const InternPool) u32 {
613629 return @truncate(u32, key.hash64(ip));
614630 }
......@@ -786,6 +802,13 @@ pub const Key = union(enum) {
786802 std.hash.autoHash(hasher, func_type.is_generic);
787803 std.hash.autoHash(hasher, func_type.is_noinline);
788804 },
805
806 .memoized_decl => |memoized_decl| std.hash.autoHash(hasher, memoized_decl.val),
807
808 .memoized_call => |memoized_call| {
809 std.hash.autoHash(hasher, memoized_call.func);
810 for (memoized_call.arg_values) |arg| std.hash.autoHash(hasher, arg);
811 },
789812 }
790813 }
791814
......@@ -1054,6 +1077,17 @@ pub const Key = union(enum) {
10541077 a_info.is_generic == b_info.is_generic and
10551078 a_info.is_noinline == b_info.is_noinline;
10561079 },
1080
1081 .memoized_decl => |a_info| {
1082 const b_info = b.memoized_decl;
1083 return a_info.val == b_info.val;
1084 },
1085
1086 .memoized_call => |a_info| {
1087 const b_info = b.memoized_call;
1088 return a_info.func == b_info.func and
1089 std.mem.eql(Index, a_info.arg_values, b_info.arg_values);
1090 },
10571091 }
10581092 }
10591093
......@@ -1105,6 +1139,10 @@ pub const Key = union(enum) {
11051139 .@"unreachable" => .noreturn_type,
11061140 .generic_poison => .generic_poison_type,
11071141 },
1142
1143 .memoized_decl,
1144 .memoized_call,
1145 => unreachable,
11081146 };
11091147 }
11101148};
......@@ -1380,6 +1418,14 @@ pub const Index = enum(u32) {
13801418 bytes: struct { data: *Bytes },
13811419 aggregate: struct { data: *Aggregate },
13821420 repeated: struct { data: *Repeated },
1421
1422 memoized_decl: struct { data: *Key.MemoizedDecl },
1423 memoized_call: struct {
1424 const @"data.args_len" = opaque {};
1425 data: *MemoizedCall,
1426 @"trailing.arg_values.len": *@"data.args_len",
1427 trailing: struct { arg_values: []Index },
1428 },
13831429 }) void {
13841430 _ = self;
13851431 const map_fields = @typeInfo(@typeInfo(@TypeOf(tag_to_encoding_map)).Pointer.child).Struct.fields;
......@@ -1875,6 +1921,13 @@ pub const Tag = enum(u8) {
18751921 /// An instance of an array or vector with every element being the same value.
18761922 /// data is extra index to `Repeated`.
18771923 repeated,
1924
1925 /// A memoized declaration value.
1926 /// data is extra index to `Key.MemoizedDecl`
1927 memoized_decl,
1928 /// A memoized comptime function call result.
1929 /// data is extra index to `MemoizedFunc`
1930 memoized_call,
18781931};
18791932
18801933/// Trailing:
......@@ -2271,6 +2324,14 @@ pub const Float128 = struct {
22712324 }
22722325};
22732326
2327/// Trailing:
2328/// 0. arg value: Index for each args_len
2329pub const MemoizedCall = struct {
2330 func: Module.Fn.Index,
2331 args_len: u32,
2332 result: Index,
2333};
2334
22742335pub fn init(ip: *InternPool, gpa: Allocator) !void {
22752336 assert(ip.items.len == 0);
22762337
......@@ -2758,6 +2819,16 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
27582819 },
27592820 .enum_literal => .{ .enum_literal = @intToEnum(NullTerminatedString, data) },
27602821 .enum_tag => .{ .enum_tag = ip.extraData(Key.EnumTag, data) },
2822
2823 .memoized_decl => .{ .memoized_decl = ip.extraData(Key.MemoizedDecl, data) },
2824 .memoized_call => {
2825 const extra = ip.extraDataTrail(MemoizedCall, data);
2826 return .{ .memoized_call = .{
2827 .func = extra.data.func,
2828 .arg_values = @ptrCast([]const Index, ip.extra.items[extra.end..][0..extra.data.args_len]),
2829 .result = extra.data.result,
2830 } };
2831 },
27612832 };
27622833}
27632834
......@@ -3724,6 +3795,29 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
37243795 .data = try ip.addExtra(gpa, un),
37253796 });
37263797 },
3798
3799 .memoized_decl => |memoized_decl| {
3800 assert(memoized_decl.val != .none);
3801 ip.items.appendAssumeCapacity(.{
3802 .tag = .memoized_decl,
3803 .data = try ip.addExtra(gpa, memoized_decl),
3804 });
3805 },
3806
3807 .memoized_call => |memoized_call| {
3808 for (memoized_call.arg_values) |arg| assert(arg != .none);
3809 try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(MemoizedCall).Struct.fields.len +
3810 memoized_call.arg_values.len);
3811 ip.items.appendAssumeCapacity(.{
3812 .tag = .memoized_call,
3813 .data = ip.addExtraAssumeCapacity(MemoizedCall{
3814 .func = memoized_call.func,
3815 .args_len = @intCast(u32, memoized_call.arg_values.len),
3816 .result = memoized_call.result,
3817 }),
3818 });
3819 ip.extra.appendSliceAssumeCapacity(@ptrCast([]const u32, memoized_call.arg_values));
3820 },
37273821 }
37283822 return @intToEnum(Index, ip.items.len - 1);
37293823}
......@@ -3788,7 +3882,7 @@ pub fn getIncompleteEnum(
37883882 ip: *InternPool,
37893883 gpa: Allocator,
37903884 enum_type: Key.IncompleteEnumType,
3791) Allocator.Error!InternPool.IncompleteEnumType {
3885) Allocator.Error!IncompleteEnumType {
37923886 switch (enum_type.tag_mode) {
37933887 .auto => return getIncompleteEnumAuto(ip, gpa, enum_type),
37943888 .explicit => return getIncompleteEnumExplicit(ip, gpa, enum_type, .type_enum_explicit),
......@@ -3800,7 +3894,7 @@ pub fn getIncompleteEnumAuto(
38003894 ip: *InternPool,
38013895 gpa: Allocator,
38023896 enum_type: Key.IncompleteEnumType,
3803) Allocator.Error!InternPool.IncompleteEnumType {
3897) Allocator.Error!IncompleteEnumType {
38043898 // Although the integer tag type will not be stored in the `EnumAuto` struct,
38053899 // `InternPool` logic depends on it being present so that `typeOf` can be infallible.
38063900 // Ensure it is present here:
......@@ -3849,7 +3943,7 @@ fn getIncompleteEnumExplicit(
38493943 gpa: Allocator,
38503944 enum_type: Key.IncompleteEnumType,
38513945 tag: Tag,
3852) Allocator.Error!InternPool.IncompleteEnumType {
3946) Allocator.Error!IncompleteEnumType {
38533947 // We must keep the map in sync with `items`. The hash and equality functions
38543948 // for enum types only look at the decl field, which is present even in
38553949 // an `IncompleteEnumType`.
......@@ -4704,6 +4798,12 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
47044798 .func => @sizeOf(Key.Func) + @sizeOf(Module.Fn) + @sizeOf(Module.Decl),
47054799 .only_possible_value => 0,
47064800 .union_value => @sizeOf(Key.Union),
4801
4802 .memoized_decl => @sizeOf(Key.MemoizedDecl),
4803 .memoized_call => b: {
4804 const info = ip.extraData(MemoizedCall, data);
4805 break :b @sizeOf(MemoizedCall) + (@sizeOf(Index) * info.args_len);
4806 },
47074807 });
47084808 }
47094809 const SortContext = struct {
......@@ -5215,6 +5315,9 @@ pub fn zigTypeTagOrPoison(ip: InternPool, index: Index) error{GenericPoison}!std
52155315 .bytes,
52165316 .aggregate,
52175317 .repeated,
5318 // memoization, not types
5319 .memoized_decl,
5320 .memoized_call,
52185321 => unreachable,
52195322 },
52205323 .none => unreachable, // special tag
src/Module.zig+12-58
......@@ -88,18 +88,10 @@ embed_table: std.StringHashMapUnmanaged(*EmbedFile) = .{},
8888/// Stores all Type and Value objects; periodically garbage collected.
8989intern_pool: InternPool = .{},
9090
91/// This is currently only used for string literals, however the end-game once the lang spec
92/// is settled will be to make this behavior consistent across all types.
93memoized_decls: std.AutoHashMapUnmanaged(InternPool.Index, Decl.Index) = .{},
94
9591/// The set of all the generic function instantiations. This is used so that when a generic
9692/// function is called twice with the same comptime parameter arguments, both calls dispatch
9793/// to the same function.
9894monomorphed_funcs: MonomorphedFuncsSet = .{},
99/// The set of all comptime function calls that have been cached so that future calls
100/// with the same parameters will get the same return value.
101memoized_calls: MemoizedCallSet = .{},
102memoized_call_args: MemoizedCall.Args = .{},
10395/// Contains the values from `@setAlignStack`. A sparse table is used here
10496/// instead of a field of `Fn` because usage of `@setAlignStack` is rare, while
10597/// functions are many.
......@@ -223,42 +215,6 @@ const MonomorphedFuncsContext = struct {
223215 }
224216};
225217
226pub const MemoizedCallSet = std.HashMapUnmanaged(
227 MemoizedCall.Key,
228 MemoizedCall.Result,
229 MemoizedCall,
230 std.hash_map.default_max_load_percentage,
231);
232
233pub const MemoizedCall = struct {
234 args: *const Args,
235
236 pub const Args = std.ArrayListUnmanaged(InternPool.Index);
237
238 pub const Key = struct {
239 func: Fn.Index,
240 args_index: u32,
241 args_count: u32,
242
243 pub fn args(key: Key, ctx: MemoizedCall) []InternPool.Index {
244 return ctx.args.items[key.args_index..][0..key.args_count];
245 }
246 };
247
248 pub const Result = InternPool.Index;
249
250 pub fn eql(ctx: MemoizedCall, a: Key, b: Key) bool {
251 return a.func == b.func and mem.eql(InternPool.Index, a.args(ctx), b.args(ctx));
252 }
253
254 pub fn hash(ctx: MemoizedCall, key: Key) u64 {
255 var hasher = std.hash.Wyhash.init(0);
256 std.hash.autoHash(&hasher, key.func);
257 std.hash.autoHashStrat(&hasher, key.args(ctx), .Deep);
258 return hasher.final();
259 }
260};
261
262218pub const SetAlignStack = struct {
263219 alignment: u32,
264220 /// TODO: This needs to store a non-lazy source location for the case of an inline function
......@@ -605,7 +561,6 @@ pub const Decl = struct {
605561 }
606562 mod.destroyFunc(func);
607563 }
608 _ = mod.memoized_decls.remove(decl.val.ip_index);
609564 if (decl.value_arena) |value_arena| {
610565 value_arena.deinit(gpa);
611566 decl.value_arena = null;
......@@ -3314,8 +3269,6 @@ pub fn deinit(mod: *Module) void {
33143269 mod.test_functions.deinit(gpa);
33153270 mod.align_stack_fns.deinit(gpa);
33163271 mod.monomorphed_funcs.deinit(gpa);
3317 mod.memoized_call_args.deinit(gpa);
3318 mod.memoized_calls.deinit(gpa);
33193272
33203273 mod.decls_free_list.deinit(gpa);
33213274 mod.allocated_decls.deinit(gpa);
......@@ -3325,8 +3278,6 @@ pub fn deinit(mod: *Module) void {
33253278 mod.namespaces_free_list.deinit(gpa);
33263279 mod.allocated_namespaces.deinit(gpa);
33273280
3328 mod.memoized_decls.deinit(gpa);
3329
33303281 mod.intern_pool.deinit(gpa);
33313282}
33323283
......@@ -5438,6 +5389,17 @@ pub fn abortAnonDecl(mod: *Module, decl_index: Decl.Index) void {
54385389 mod.destroyDecl(decl_index);
54395390}
54405391
5392/// Finalize the creation of an anon decl.
5393pub fn finalizeAnonDecl(mod: *Module, decl_index: Decl.Index) Allocator.Error!void {
5394 // The Decl starts off with alive=false and the codegen backend will set alive=true
5395 // if the Decl is referenced by an instruction or another constant. Otherwise,
5396 // the Decl will be garbage collected by the `codegen_decl` task instead of sent
5397 // to the linker.
5398 if (mod.declPtr(decl_index).ty.isFnOrHasRuntimeBits(mod)) {
5399 try mod.comp.anon_work_queue.writeItem(.{ .codegen_decl = decl_index });
5400 }
5401}
5402
54415403/// Delete all the Export objects that are caused by this Decl. Re-analysis of
54425404/// this Decl will cause them to be re-created (or not).
54435405fn deleteDeclExports(mod: *Module, decl_index: Decl.Index) Allocator.Error!void {
......@@ -5875,7 +5837,7 @@ pub fn initNewAnonDecl(
58755837 namespace: Namespace.Index,
58765838 typed_value: TypedValue,
58775839 name: [:0]u8,
5878) !void {
5840) Allocator.Error!void {
58795841 assert(typed_value.ty.toIntern() == mod.intern_pool.typeOf(typed_value.val.toIntern()));
58805842 errdefer mod.gpa.free(name);
58815843
......@@ -5892,14 +5854,6 @@ pub fn initNewAnonDecl(
58925854 new_decl.generation = mod.generation;
58935855
58945856 try mod.namespacePtr(namespace).anon_decls.putNoClobber(mod.gpa, new_decl_index, {});
5895
5896 // The Decl starts off with alive=false and the codegen backend will set alive=true
5897 // if the Decl is referenced by an instruction or another constant. Otherwise,
5898 // the Decl will be garbage collected by the `codegen_decl` task instead of sent
5899 // to the linker.
5900 if (typed_value.ty.isFnOrHasRuntimeBits(mod)) {
5901 try mod.comp.anon_work_queue.writeItem(.{ .codegen_decl = new_decl_index });
5902 }
59035857}
59045858
59055859pub fn errNoteNonLazy(
src/Sema.zig+95-78
......@@ -734,6 +734,7 @@ pub const Block = struct {
734734 errdefer sema.mod.abortAnonDecl(new_decl_index);
735735 try new_decl.finalizeNewArena(&wad.new_decl_arena);
736736 wad.finished = true;
737 try sema.mod.finalizeAnonDecl(new_decl_index);
737738 return new_decl_index;
738739 }
739740 };
......@@ -2292,7 +2293,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
22922293 defer reference_stack.deinit();
22932294
22942295 // Avoid infinite loops.
2295 var seen = std.AutoHashMap(Module.Decl.Index, void).init(gpa);
2296 var seen = std.AutoHashMap(Decl.Index, void).init(gpa);
22962297 defer seen.deinit();
22972298
22982299 var cur_reference_trace: u32 = 0;
......@@ -2742,7 +2743,9 @@ fn zirStructDecl(
27422743
27432744 try sema.analyzeStructDecl(new_decl, inst, struct_index);
27442745 try new_decl.finalizeNewArena(&new_decl_arena);
2745 return sema.analyzeDeclVal(block, src, new_decl_index);
2746 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
2747 try mod.finalizeAnonDecl(new_decl_index);
2748 return decl_val;
27462749}
27472750
27482751fn createAnonymousDeclTypeNamed(
......@@ -2941,6 +2944,7 @@ fn zirEnumDecl(
29412944 new_namespace.ty = incomplete_enum.index.toType();
29422945
29432946 const decl_val = try sema.analyzeDeclVal(block, src, new_decl_index);
2947 try mod.finalizeAnonDecl(new_decl_index);
29442948 done = true;
29452949
29462950 const int_tag_ty = ty: {
......@@ -3193,7 +3197,9 @@ fn zirUnionDecl(
31933197 _ = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
31943198
31953199 try new_decl.finalizeNewArena(&new_decl_arena);
3196 return sema.analyzeDeclVal(block, src, new_decl_index);
3200 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
3201 try mod.finalizeAnonDecl(new_decl_index);
3202 return decl_val;
31973203}
31983204
31993205fn zirOpaqueDecl(
......@@ -3257,7 +3263,9 @@ fn zirOpaqueDecl(
32573263 extra_index = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
32583264
32593265 try new_decl.finalizeNewArena(&new_decl_arena);
3260 return sema.analyzeDeclVal(block, src, new_decl_index);
3266 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
3267 try mod.finalizeAnonDecl(new_decl_index);
3268 return decl_val;
32613269}
32623270
32633271fn zirErrorSetDecl(
......@@ -3298,7 +3306,9 @@ fn zirErrorSetDecl(
32983306 new_decl.owns_tv = true;
32993307 errdefer mod.abortAnonDecl(new_decl_index);
33003308
3301 return sema.analyzeDeclVal(block, src, new_decl_index);
3309 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
3310 try mod.finalizeAnonDecl(new_decl_index);
3311 return decl_val;
33023312}
33033313
33043314fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
......@@ -5133,32 +5143,35 @@ fn zirStr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
51335143 return sema.addStrLit(block, bytes);
51345144}
51355145
5136fn addStrLit(sema: *Sema, block: *Block, zir_bytes: []const u8) CompileError!Air.Inst.Ref {
5137 // `zir_bytes` references memory inside the ZIR module, which can get deallocated
5138 // after semantic analysis is complete, for example in the case of the initialization
5139 // expression of a variable declaration.
5146fn addStrLit(sema: *Sema, block: *Block, bytes: []const u8) CompileError!Air.Inst.Ref {
51405147 const mod = sema.mod;
5141 const gpa = sema.gpa;
5142 const ty = try mod.arrayType(.{
5143 .len = zir_bytes.len,
5144 .child = .u8_type,
5145 .sentinel = .zero_u8,
5146 });
5147 const val = try mod.intern(.{ .aggregate = .{
5148 .ty = ty.toIntern(),
5149 .storage = .{ .bytes = zir_bytes },
5150 } });
5151 const gop = try mod.memoized_decls.getOrPut(gpa, val);
5152 if (!gop.found_existing) {
5153 var anon_decl = try block.startAnonDecl();
5154 defer anon_decl.deinit();
5148 const memoized_decl_index = memoized: {
5149 const ty = try mod.arrayType(.{
5150 .len = bytes.len,
5151 .child = .u8_type,
5152 .sentinel = .zero_u8,
5153 });
5154 const val = try mod.intern(.{ .aggregate = .{
5155 .ty = ty.toIntern(),
5156 .storage = .{ .bytes = bytes },
5157 } });
51555158
5156 const decl_index = try anon_decl.finish(ty, val.toValue(), 0);
5159 _ = try sema.typeHasRuntimeBits(ty);
5160 const new_decl_index = try mod.createAnonymousDecl(block, .{ .ty = ty, .val = val.toValue() });
5161 errdefer mod.abortAnonDecl(new_decl_index);
51575162
5158 gop.key_ptr.* = val;
5159 gop.value_ptr.* = decl_index;
5160 }
5161 return sema.analyzeDeclRef(gop.value_ptr.*);
5163 const memoized_index = try mod.intern(.{ .memoized_decl = .{
5164 .val = val,
5165 .decl = new_decl_index,
5166 } });
5167 const memoized_decl_index = mod.intern_pool.indexToKey(memoized_index).memoized_decl.decl;
5168 if (memoized_decl_index != new_decl_index)
5169 mod.abortAnonDecl(new_decl_index)
5170 else
5171 try mod.finalizeAnonDecl(new_decl_index);
5172 break :memoized memoized_decl_index;
5173 };
5174 return sema.analyzeDeclRef(memoized_decl_index);
51625175}
51635176
51645177fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -6868,30 +6881,15 @@ fn analyzeCall(
68686881 defer child_block.instructions.deinit(gpa);
68696882 defer merges.deinit(gpa);
68706883
6871 // If it's a comptime function call, we need to memoize it as long as no external
6872 // comptime memory is mutated.
6873 var memoized_call_key = Module.MemoizedCall.Key{
6874 .func = module_fn_index,
6875 .args_index = @intCast(u32, mod.memoized_call_args.items.len),
6876 .args_count = @intCast(u32, func_ty_info.param_types.len),
6877 };
6878 var delete_memoized_call_key = false;
6879 defer if (delete_memoized_call_key) {
6880 assert(mod.memoized_call_args.items.len >= memoized_call_key.args_index and
6881 mod.memoized_call_args.items.len < memoized_call_key.args_index + memoized_call_key.args_count);
6882 mod.memoized_call_args.shrinkRetainingCapacity(memoized_call_key.args_index);
6883 };
6884 if (is_comptime_call) {
6885 try mod.memoized_call_args.ensureUnusedCapacity(gpa, memoized_call_key.args_count);
6886 delete_memoized_call_key = true;
6887 }
6888
68896884 try sema.emitBackwardBranch(block, call_src);
68906885
6891 // Whether this call should be memoized, set to false if the call can mutate
6892 // comptime state.
6886 // Whether this call should be memoized, set to false if the call can mutate comptime state.
68936887 var should_memoize = true;
68946888
6889 // If it's a comptime function call, we need to memoize it as long as no external
6890 // comptime memory is mutated.
6891 const memoized_arg_values = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
6892
68956893 var new_fn_info = mod.typeToFunc(fn_owner_decl.ty).?;
68966894 new_fn_info.param_types = try sema.arena.alloc(InternPool.Index, new_fn_info.param_types.len);
68976895 new_fn_info.comptime_bits = 0;
......@@ -6918,6 +6916,7 @@ fn analyzeCall(
69186916 uncasted_args,
69196917 is_comptime_call,
69206918 &should_memoize,
6919 memoized_arg_values,
69216920 mod.typeToFunc(func_ty).?.param_types,
69226921 func,
69236922 &has_comptime_args,
......@@ -6935,6 +6934,7 @@ fn analyzeCall(
69356934 uncasted_args,
69366935 is_comptime_call,
69376936 &should_memoize,
6937 memoized_arg_values,
69386938 mod.typeToFunc(func_ty).?.param_types,
69396939 func,
69406940 &has_comptime_args,
......@@ -6988,28 +6988,18 @@ fn analyzeCall(
69886988 // bug generating invalid LLVM IR.
69896989 const res2: Air.Inst.Ref = res2: {
69906990 if (should_memoize and is_comptime_call) {
6991 const gop = try mod.memoized_calls.getOrPutContext(
6992 gpa,
6993 memoized_call_key,
6994 .{ .args = &mod.memoized_call_args },
6995 );
6996 if (gop.found_existing) {
6997 assert(mod.memoized_call_args.items.len == memoized_call_key.args_index + memoized_call_key.args_count);
6998 mod.memoized_call_args.shrinkRetainingCapacity(memoized_call_key.args_index);
6999 delete_memoized_call_key = false;
7000
7001 // We need to use the original memoized error set instead of fn_ret_ty.
7002 const result = gop.value_ptr.*;
7003 assert(result != .none); // recursive memoization?
7004
7005 break :res2 try sema.addConstant(mod.intern_pool.typeOf(result).toType(), result.toValue());
6991 if (mod.intern_pool.getIfExists(.{ .memoized_call = .{
6992 .func = module_fn_index,
6993 .arg_values = memoized_arg_values,
6994 .result = .none,
6995 } })) |memoized_call_index| {
6996 const memoized_call = mod.intern_pool.indexToKey(memoized_call_index).memoized_call;
6997 break :res2 try sema.addConstant(
6998 mod.intern_pool.typeOf(memoized_call.result).toType(),
6999 memoized_call.result.toValue(),
7000 );
70067001 }
7007 gop.value_ptr.* = .none;
7008 } else if (delete_memoized_call_key) {
7009 assert(mod.memoized_call_args.items.len == memoized_call_key.args_index + memoized_call_key.args_count);
7010 mod.memoized_call_args.shrinkRetainingCapacity(memoized_call_key.args_index);
70117002 }
7012 delete_memoized_call_key = false;
70137003
70147004 const new_func_resolved_ty = try mod.funcType(new_fn_info);
70157005 if (!is_comptime_call and !block.is_typeof) {
......@@ -7067,10 +7057,14 @@ fn analyzeCall(
70677057
70687058 if (should_memoize and is_comptime_call) {
70697059 const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, "");
7070 mod.memoized_calls.getPtrContext(
7071 memoized_call_key,
7072 .{ .args = &mod.memoized_call_args },
7073 ).?.* = try result_val.intern(fn_ret_ty, mod);
7060
7061 // TODO: check whether any external comptime memory was mutated by the
7062 // comptime function call. If so, then do not memoize the call here.
7063 _ = try mod.intern(.{ .memoized_call = .{
7064 .func = module_fn_index,
7065 .arg_values = memoized_arg_values,
7066 .result = try result_val.intern(fn_ret_ty, mod),
7067 } });
70747068 }
70757069
70767070 break :res2 result;
......@@ -7216,6 +7210,7 @@ fn analyzeInlineCallArg(
72167210 uncasted_args: []const Air.Inst.Ref,
72177211 is_comptime_call: bool,
72187212 should_memoize: *bool,
7213 memoized_arg_values: []InternPool.Index,
72197214 raw_param_types: []const InternPool.Index,
72207215 func_inst: Air.Inst.Ref,
72217216 has_comptime_args: *bool,
......@@ -7279,7 +7274,7 @@ fn analyzeInlineCallArg(
72797274 },
72807275 }
72817276 should_memoize.* = should_memoize.* and !arg_val.canMutateComptimeVarState(mod);
7282 mod.memoized_call_args.appendAssumeCapacity(try arg_val.intern(param_ty.toType(), mod));
7277 memoized_arg_values[arg_i.*] = try arg_val.intern(param_ty.toType(), mod);
72837278 } else {
72847279 sema.inst_map.putAssumeCapacityNoClobber(inst, casted_arg);
72857280 }
......@@ -7315,7 +7310,7 @@ fn analyzeInlineCallArg(
73157310 },
73167311 }
73177312 should_memoize.* = should_memoize.* and !arg_val.canMutateComptimeVarState(mod);
7318 mod.memoized_call_args.appendAssumeCapacity(try arg_val.intern(sema.typeOf(uncasted_arg), mod));
7313 memoized_arg_values[arg_i.*] = try arg_val.intern(sema.typeOf(uncasted_arg), mod);
73197314 } else {
73207315 if (zir_tags[inst] == .param_anytype_comptime) {
73217316 _ = try sema.resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, "parameter is comptime");
......@@ -19363,7 +19358,9 @@ fn zirReify(
1936319358 }
1936419359 }
1936519360
19366 return sema.analyzeDeclVal(block, src, new_decl_index);
19361 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
19362 try mod.finalizeAnonDecl(new_decl_index);
19363 return decl_val;
1936719364 },
1936819365 .Opaque => {
1936919366 const fields = ip.typeOf(union_val.val).toType().structFields(mod);
......@@ -19407,7 +19404,9 @@ fn zirReify(
1940719404 new_namespace.ty = opaque_ty.toType();
1940819405
1940919406 try new_decl.finalizeNewArena(&new_decl_arena);
19410 return sema.analyzeDeclVal(block, src, new_decl_index);
19407 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
19408 try mod.finalizeAnonDecl(new_decl_index);
19409 return decl_val;
1941119410 },
1941219411 .Union => {
1941319412 const fields = ip.typeOf(union_val.val).toType().structFields(mod);
......@@ -19604,7 +19603,9 @@ fn zirReify(
1960419603 }
1960519604
1960619605 try new_decl.finalizeNewArena(&new_decl_arena);
19607 return sema.analyzeDeclVal(block, src, new_decl_index);
19606 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
19607 try mod.finalizeAnonDecl(new_decl_index);
19608 return decl_val;
1960819609 },
1960919610 .Fn => {
1961019611 const fields = ip.typeOf(union_val.val).toType().structFields(mod);
......@@ -19902,7 +19903,9 @@ fn reifyStruct(
1990219903 }
1990319904
1990419905 try new_decl.finalizeNewArena(&new_decl_arena);
19905 return sema.analyzeDeclVal(block, src, new_decl_index);
19906 const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
19907 try mod.finalizeAnonDecl(new_decl_index);
19908 return decl_val;
1990619909}
1990719910
1990819911fn zirAddrSpaceCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
......@@ -31865,6 +31868,9 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3186531868 .opt,
3186631869 .aggregate,
3186731870 .un,
31871 // memoization, not types
31872 .memoized_decl,
31873 .memoized_call,
3186831874 => unreachable,
3186931875 },
3187031876 };
......@@ -32997,6 +33003,8 @@ fn generateUnionTagTypeNumbered(
3299733003 .ty = Type.type,
3299833004 .val = undefined,
3299933005 }, name);
33006 errdefer mod.abortAnonDecl(new_decl_index);
33007
3300033008 const new_decl = mod.declPtr(new_decl_index);
3300133009 new_decl.name_fully_qualified = true;
3300233010 new_decl.owns_tv = true;
......@@ -33016,6 +33024,7 @@ fn generateUnionTagTypeNumbered(
3301633024
3301733025 new_decl.val = enum_ty.toValue();
3301833026
33027 try mod.finalizeAnonDecl(new_decl_index);
3301933028 return enum_ty.toType();
3302033029}
3302133030
......@@ -33049,6 +33058,7 @@ fn generateUnionTagTypeSimple(
3304933058 mod.declPtr(new_decl_index).name_fully_qualified = true;
3305033059 break :new_decl_index new_decl_index;
3305133060 };
33061 errdefer mod.abortAnonDecl(new_decl_index);
3305233062
3305333063 const enum_ty = try mod.intern(.{ .enum_type = .{
3305433064 .decl = new_decl_index,
......@@ -33066,6 +33076,7 @@ fn generateUnionTagTypeSimple(
3306633076 new_decl.owns_tv = true;
3306733077 new_decl.val = enum_ty.toValue();
3306833078
33079 try mod.finalizeAnonDecl(new_decl_index);
3306933080 return enum_ty.toType();
3307033081}
3307133082
......@@ -33358,6 +33369,9 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3335833369 .opt,
3335933370 .aggregate,
3336033371 .un,
33372 // memoization, not types
33373 .memoized_decl,
33374 .memoized_call,
3336133375 => unreachable,
3336233376 },
3336333377 };
......@@ -33843,6 +33857,9 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3384333857 .opt,
3384433858 .aggregate,
3384533859 .un,
33860 // memoization, not types
33861 .memoized_decl,
33862 .memoized_call,
3384633863 => unreachable,
3384733864 },
3384833865 };
src/TypedValue.zig+3
......@@ -278,6 +278,9 @@ pub fn print(
278278 } else try writer.writeAll("...");
279279 return writer.writeAll(" }");
280280 },
281 .memoized_decl,
282 .memoized_call,
283 => unreachable,
281284 },
282285 };
283286}
src/arch/wasm/CodeGen.zig+3
......@@ -3254,6 +3254,9 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
32543254 else => unreachable,
32553255 },
32563256 .un => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(mod)}),
3257 .memoized_decl,
3258 .memoized_call,
3259 => unreachable,
32573260 }
32583261}
32593262
src/codegen.zig+3
......@@ -605,6 +605,9 @@ pub fn generateSymbol(
605605 }
606606 }
607607 },
608 .memoized_decl,
609 .memoized_call,
610 => unreachable,
608611 }
609612 return .ok;
610613}
src/codegen/c.zig+5-1
......@@ -1090,6 +1090,7 @@ pub const DeclGen = struct {
10901090 };
10911091
10921092 switch (mod.intern_pool.indexToKey(val.ip_index)) {
1093 // types, not values
10931094 .int_type,
10941095 .ptr_type,
10951096 .array_type,
......@@ -1106,7 +1107,10 @@ pub const DeclGen = struct {
11061107 .func_type,
11071108 .error_set_type,
11081109 .inferred_error_set_type,
1109 => unreachable, // types, not values
1110 // memoization, not values
1111 .memoized_decl,
1112 .memoized_call,
1113 => unreachable,
11101114
11111115 .undef, .runtime_value => unreachable, // handled above
11121116 .simple_value => |simple_value| switch (simple_value) {
src/codegen/llvm.zig+3
......@@ -3793,6 +3793,9 @@ pub const DeclGen = struct {
37933793 return llvm_union_ty.constNamedStruct(&fields, fields_len);
37943794 }
37953795 },
3796 .memoized_decl,
3797 .memoized_call,
3798 => unreachable,
37963799 }
37973800 }
37983801
src/codegen/spirv.zig+3
......@@ -830,6 +830,9 @@ pub const DeclGen = struct {
830830
831831 try self.addUndef(layout.padding);
832832 },
833 .memoized_decl,
834 .memoized_call,
835 => unreachable,
833836 }
834837 }
835838 };
src/type.zig+27
......@@ -400,6 +400,9 @@ pub const Type = struct {
400400 .opt,
401401 .aggregate,
402402 .un,
403 // memoization, not types
404 .memoized_decl,
405 .memoized_call,
403406 => unreachable,
404407 }
405408 }
......@@ -613,6 +616,9 @@ pub const Type = struct {
613616 .opt,
614617 .aggregate,
615618 .un,
619 // memoization, not types
620 .memoized_decl,
621 .memoized_call,
616622 => unreachable,
617623 },
618624 };
......@@ -719,6 +725,9 @@ pub const Type = struct {
719725 .opt,
720726 .aggregate,
721727 .un,
728 // memoization, not types
729 .memoized_decl,
730 .memoized_call,
722731 => unreachable,
723732 };
724733 }
......@@ -1050,6 +1059,9 @@ pub const Type = struct {
10501059 .opt,
10511060 .aggregate,
10521061 .un,
1062 // memoization, not types
1063 .memoized_decl,
1064 .memoized_call,
10531065 => unreachable,
10541066 },
10551067 }
......@@ -1464,6 +1476,9 @@ pub const Type = struct {
14641476 .opt,
14651477 .aggregate,
14661478 .un,
1479 // memoization, not types
1480 .memoized_decl,
1481 .memoized_call,
14671482 => unreachable,
14681483 },
14691484 }
......@@ -1695,6 +1710,9 @@ pub const Type = struct {
16951710 .opt,
16961711 .aggregate,
16971712 .un,
1713 // memoization, not types
1714 .memoized_decl,
1715 .memoized_call,
16981716 => unreachable,
16991717 }
17001718 }
......@@ -2250,6 +2268,9 @@ pub const Type = struct {
22502268 .opt,
22512269 .aggregate,
22522270 .un,
2271 // memoization, not types
2272 .memoized_decl,
2273 .memoized_call,
22532274 => unreachable,
22542275 },
22552276 };
......@@ -2586,6 +2607,9 @@ pub const Type = struct {
25862607 .opt,
25872608 .aggregate,
25882609 .un,
2610 // memoization, not types
2611 .memoized_decl,
2612 .memoized_call,
25892613 => unreachable,
25902614 },
25912615 };
......@@ -2728,6 +2752,9 @@ pub const Type = struct {
27282752 .opt,
27292753 .aggregate,
27302754 .un,
2755 // memoization, not types
2756 .memoized_decl,
2757 .memoized_call,
27312758 => unreachable,
27322759 },
27332760 };
src/value.zig+4
......@@ -476,6 +476,10 @@ pub const Value = struct {
476476 .tag = un.tag.toValue(),
477477 .val = un.val.toValue(),
478478 }),
479
480 .memoized_decl,
481 .memoized_call,
482 => unreachable,
479483 };
480484 }
481485