authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-31 15:00:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:58-07:00
logbb526426e75ed456a7db6afa32447e5a76ac7ca1
tree8bfdcc7aac33275554b0216ade8cde0b80b2e698
parent870e3843c5736def21234ca8b7159b179985505c

InternPool: remove memoized_decl

This is neither a type nor a value. Simplifies `addStrLit` as well as the many places that switch on `InternPool.Key`. This is a partial revert of bec29b9e498e08202679aa29a45dab2a06a69a1e.

11 files changed, 31 insertions(+), 92 deletions(-)

src/InternPool.zig+1-35
...@@ -221,8 +221,6 @@ pub const Key = union(enum) {...@@ -221,8 +221,6 @@ pub const Key = union(enum) {
221 /// An instance of a union.221 /// An instance of a union.
222 un: Union,222 un: Union,
223223
224 /// A declaration with a memoized value.
225 memoized_decl: MemoizedDecl,
226 /// A comptime function call with a memoized result.224 /// A comptime function call with a memoized result.
227 memoized_call: Key.MemoizedCall,225 memoized_call: Key.MemoizedCall,
228226
...@@ -639,11 +637,6 @@ pub const Key = union(enum) {...@@ -639,11 +637,6 @@ pub const Key = union(enum) {
639 };637 };
640 };638 };
641639
642 pub const MemoizedDecl = struct {
643 val: Index,
644 decl: Module.Decl.Index,
645 };
646
647 pub const MemoizedCall = struct {640 pub const MemoizedCall = struct {
648 func: Module.Fn.Index,641 func: Module.Fn.Index,
649 arg_values: []const Index,642 arg_values: []const Index,
...@@ -853,8 +846,6 @@ pub const Key = union(enum) {...@@ -853,8 +846,6 @@ pub const Key = union(enum) {
853 return hasher.final();846 return hasher.final();
854 },847 },
855848
856 .memoized_decl => |x| WyhashKing.hash(seed, asBytes(&x.val)),
857
858 .memoized_call => |memoized_call| {849 .memoized_call => |memoized_call| {
859 var hasher = std.hash.Wyhash.init(seed);850 var hasher = std.hash.Wyhash.init(seed);
860 std.hash.autoHash(&hasher, memoized_call.func);851 std.hash.autoHash(&hasher, memoized_call.func);
...@@ -1134,11 +1125,6 @@ pub const Key = union(enum) {...@@ -1134,11 +1125,6 @@ pub const Key = union(enum) {
1134 a_info.is_noinline == b_info.is_noinline;1125 a_info.is_noinline == b_info.is_noinline;
1135 },1126 },
11361127
1137 .memoized_decl => |a_info| {
1138 const b_info = b.memoized_decl;
1139 return a_info.val == b_info.val;
1140 },
1141
1142 .memoized_call => |a_info| {1128 .memoized_call => |a_info| {
1143 const b_info = b.memoized_call;1129 const b_info = b.memoized_call;
1144 return a_info.func == b_info.func and1130 return a_info.func == b_info.func and
...@@ -1197,9 +1183,7 @@ pub const Key = union(enum) {...@@ -1197,9 +1183,7 @@ pub const Key = union(enum) {
1197 .generic_poison => .generic_poison_type,1183 .generic_poison => .generic_poison_type,
1198 },1184 },
11991185
1200 .memoized_decl,1186 .memoized_call => unreachable,
1201 .memoized_call,
1202 => unreachable,
1203 };1187 };
1204 }1188 }
1205};1189};
...@@ -1481,7 +1465,6 @@ pub const Index = enum(u32) {...@@ -1481,7 +1465,6 @@ pub const Index = enum(u32) {
1481 },1465 },
1482 repeated: struct { data: *Repeated },1466 repeated: struct { data: *Repeated },
14831467
1484 memoized_decl: struct { data: *Key.MemoizedDecl },
1485 memoized_call: struct {1468 memoized_call: struct {
1486 const @"data.args_len" = opaque {};1469 const @"data.args_len" = opaque {};
1487 data: *MemoizedCall,1470 data: *MemoizedCall,
...@@ -1989,9 +1972,6 @@ pub const Tag = enum(u8) {...@@ -1989,9 +1972,6 @@ pub const Tag = enum(u8) {
1989 /// data is extra index to `Repeated`.1972 /// data is extra index to `Repeated`.
1990 repeated,1973 repeated,
19911974
1992 /// A memoized declaration value.
1993 /// data is extra index to `Key.MemoizedDecl`
1994 memoized_decl,
1995 /// A memoized comptime function call result.1975 /// A memoized comptime function call result.
1996 /// data is extra index to `MemoizedCall`1976 /// data is extra index to `MemoizedCall`
1997 memoized_call,1977 memoized_call,
...@@ -2004,7 +1984,6 @@ pub const Tag = enum(u8) {...@@ -2004,7 +1984,6 @@ pub const Tag = enum(u8) {
2004 const ExternFunc = Key.ExternFunc;1984 const ExternFunc = Key.ExternFunc;
2005 const Func = Key.Func;1985 const Func = Key.Func;
2006 const Union = Key.Union;1986 const Union = Key.Union;
2007 const MemoizedDecl = Key.MemoizedDecl;
2008 const TypePointer = Key.PtrType;1987 const TypePointer = Key.PtrType;
20091988
2010 fn Payload(comptime tag: Tag) type {1989 fn Payload(comptime tag: Tag) type {
...@@ -2082,7 +2061,6 @@ pub const Tag = enum(u8) {...@@ -2082,7 +2061,6 @@ pub const Tag = enum(u8) {
2082 .bytes => Bytes,2061 .bytes => Bytes,
2083 .aggregate => Aggregate,2062 .aggregate => Aggregate,
2084 .repeated => Repeated,2063 .repeated => Repeated,
2085 .memoized_decl => MemoizedDecl,
2086 .memoized_call => MemoizedCall,2064 .memoized_call => MemoizedCall,
2087 };2065 };
2088 }2066 }
...@@ -3000,7 +2978,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {...@@ -3000,7 +2978,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
3000 .enum_literal => .{ .enum_literal = @intToEnum(NullTerminatedString, data) },2978 .enum_literal => .{ .enum_literal = @intToEnum(NullTerminatedString, data) },
3001 .enum_tag => .{ .enum_tag = ip.extraData(Tag.EnumTag, data) },2979 .enum_tag => .{ .enum_tag = ip.extraData(Tag.EnumTag, data) },
30022980
3003 .memoized_decl => .{ .memoized_decl = ip.extraData(Key.MemoizedDecl, data) },
3004 .memoized_call => {2981 .memoized_call => {
3005 const extra = ip.extraDataTrail(MemoizedCall, data);2982 const extra = ip.extraDataTrail(MemoizedCall, data);
3006 return .{ .memoized_call = .{2983 return .{ .memoized_call = .{
...@@ -3995,14 +3972,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -3995,14 +3972,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
3995 });3972 });
3996 },3973 },
39973974
3998 .memoized_decl => |memoized_decl| {
3999 assert(memoized_decl.val != .none);
4000 ip.items.appendAssumeCapacity(.{
4001 .tag = .memoized_decl,
4002 .data = try ip.addExtra(gpa, memoized_decl),
4003 });
4004 },
4005
4006 .memoized_call => |memoized_call| {3975 .memoized_call => |memoized_call| {
4007 for (memoized_call.arg_values) |arg| assert(arg != .none);3976 for (memoized_call.arg_values) |arg| assert(arg != .none);
4008 try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(MemoizedCall).Struct.fields.len +3977 try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(MemoizedCall).Struct.fields.len +
...@@ -5005,7 +4974,6 @@ fn dumpFallible(ip: *const InternPool, arena: Allocator) anyerror!void {...@@ -5005,7 +4974,6 @@ fn dumpFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
5005 .only_possible_value => 0,4974 .only_possible_value => 0,
5006 .union_value => @sizeOf(Key.Union),4975 .union_value => @sizeOf(Key.Union),
50074976
5008 .memoized_decl => @sizeOf(Key.MemoizedDecl),
5009 .memoized_call => b: {4977 .memoized_call => b: {
5010 const info = ip.extraData(MemoizedCall, data);4978 const info = ip.extraData(MemoizedCall, data);
5011 break :b @sizeOf(MemoizedCall) + (@sizeOf(Index) * info.args_len);4979 break :b @sizeOf(MemoizedCall) + (@sizeOf(Index) * info.args_len);
...@@ -5383,7 +5351,6 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index {...@@ -5383,7 +5351,6 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index {
53835351
5384 .float_comptime_float => .comptime_float_type,5352 .float_comptime_float => .comptime_float_type,
53855353
5386 .memoized_decl => unreachable,
5387 .memoized_call => unreachable,5354 .memoized_call => unreachable,
5388 },5355 },
53895356
...@@ -5624,7 +5591,6 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois...@@ -5624,7 +5591,6 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois
5624 .aggregate,5591 .aggregate,
5625 .repeated,5592 .repeated,
5626 // memoization, not types5593 // memoization, not types
5627 .memoized_decl,
5628 .memoized_call,5594 .memoized_call,
5629 => unreachable,5595 => unreachable,
5630 },5596 },
src/Module.zig+5
...@@ -88,6 +88,9 @@ embed_table: std.StringHashMapUnmanaged(*EmbedFile) = .{},...@@ -88,6 +88,9 @@ embed_table: std.StringHashMapUnmanaged(*EmbedFile) = .{},
88/// Stores all Type and Value objects; periodically garbage collected.88/// Stores all Type and Value objects; periodically garbage collected.
89intern_pool: InternPool = .{},89intern_pool: InternPool = .{},
9090
91/// This is currently only used for string literals.
92memoized_decls: std.AutoHashMapUnmanaged(InternPool.Index, Decl.Index) = .{},
93
91/// The set of all the generic function instantiations. This is used so that when a generic94/// The set of all the generic function instantiations. This is used so that when a generic
92/// function is called twice with the same comptime parameter arguments, both calls dispatch95/// function is called twice with the same comptime parameter arguments, both calls dispatch
93/// to the same function.96/// to the same function.
...@@ -561,6 +564,7 @@ pub const Decl = struct {...@@ -561,6 +564,7 @@ pub const Decl = struct {
561 }564 }
562 mod.destroyFunc(func);565 mod.destroyFunc(func);
563 }566 }
567 _ = mod.memoized_decls.remove(decl.val.ip_index);
564 if (decl.value_arena) |value_arena| {568 if (decl.value_arena) |value_arena| {
565 value_arena.deinit(gpa);569 value_arena.deinit(gpa);
566 decl.value_arena = null;570 decl.value_arena = null;
...@@ -3285,6 +3289,7 @@ pub fn deinit(mod: *Module) void {...@@ -3285,6 +3289,7 @@ pub fn deinit(mod: *Module) void {
3285 mod.namespaces_free_list.deinit(gpa);3289 mod.namespaces_free_list.deinit(gpa);
3286 mod.allocated_namespaces.deinit(gpa);3290 mod.allocated_namespaces.deinit(gpa);
32873291
3292 mod.memoized_decls.deinit(gpa);
3288 mod.intern_pool.deinit(gpa);3293 mod.intern_pool.deinit(gpa);
3289}3294}
32903295
src/Sema.zig+19-29
...@@ -5183,33 +5183,26 @@ fn zirStr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins...@@ -5183,33 +5183,26 @@ fn zirStr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
51835183
5184fn addStrLit(sema: *Sema, block: *Block, bytes: []const u8) CompileError!Air.Inst.Ref {5184fn addStrLit(sema: *Sema, block: *Block, bytes: []const u8) CompileError!Air.Inst.Ref {
5185 const mod = sema.mod;5185 const mod = sema.mod;
5186 const memoized_decl_index = memoized: {5186 const gpa = sema.gpa;
5187 const ty = try mod.arrayType(.{5187 const ty = try mod.arrayType(.{
5188 .len = bytes.len,5188 .len = bytes.len,
5189 .child = .u8_type,5189 .child = .u8_type,
5190 .sentinel = .zero_u8,5190 .sentinel = .zero_u8,
5191 });
5192 const val = try mod.intern(.{ .aggregate = .{
5193 .ty = ty.toIntern(),
5194 .storage = .{ .bytes = bytes },
5195 } });
5196 const gop = try mod.memoized_decls.getOrPut(gpa, val);
5197 if (!gop.found_existing) {
5198 const new_decl_index = try mod.createAnonymousDecl(block, .{
5199 .ty = ty,
5200 .val = val.toValue(),
5191 });5201 });
5192 const val = try mod.intern(.{ .aggregate = .{5202 gop.value_ptr.* = new_decl_index;
5193 .ty = ty.toIntern(),5203 try mod.finalizeAnonDecl(new_decl_index);
5194 .storage = .{ .bytes = bytes },5204 }
5195 } });5205 return sema.analyzeDeclRef(gop.value_ptr.*);
5196
5197 _ = try sema.typeHasRuntimeBits(ty);
5198 const new_decl_index = try mod.createAnonymousDecl(block, .{ .ty = ty, .val = val.toValue() });
5199 errdefer mod.abortAnonDecl(new_decl_index);
5200
5201 const memoized_index = try mod.intern(.{ .memoized_decl = .{
5202 .val = val,
5203 .decl = new_decl_index,
5204 } });
5205 const memoized_decl_index = mod.intern_pool.indexToKey(memoized_index).memoized_decl.decl;
5206 if (memoized_decl_index != new_decl_index)
5207 mod.abortAnonDecl(new_decl_index)
5208 else
5209 try mod.finalizeAnonDecl(new_decl_index);
5210 break :memoized memoized_decl_index;
5211 };
5212 return sema.analyzeDeclRef(memoized_decl_index);
5213}5206}
52145207
5215fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {5208fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -32156,7 +32149,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {...@@ -32156,7 +32149,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
32156 .aggregate,32149 .aggregate,
32157 .un,32150 .un,
32158 // memoization, not types32151 // memoization, not types
32159 .memoized_decl,
32160 .memoized_call,32152 .memoized_call,
32161 => unreachable,32153 => unreachable,
32162 },32154 },
...@@ -33666,7 +33658,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {...@@ -33666,7 +33658,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
33666 .aggregate,33658 .aggregate,
33667 .un,33659 .un,
33668 // memoization, not types33660 // memoization, not types
33669 .memoized_decl,
33670 .memoized_call,33661 .memoized_call,
33671 => unreachable,33662 => unreachable,
33672 },33663 },
...@@ -34155,7 +34146,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {...@@ -34155,7 +34146,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
34155 .aggregate,34146 .aggregate,
34156 .un,34147 .un,
34157 // memoization, not types34148 // memoization, not types
34158 .memoized_decl,
34159 .memoized_call,34149 .memoized_call,
34160 => unreachable,34150 => unreachable,
34161 },34151 },
src/TypedValue.zig+1-3
...@@ -279,9 +279,7 @@ pub fn print(...@@ -279,9 +279,7 @@ pub fn print(
279 } else try writer.writeAll("...");279 } else try writer.writeAll("...");
280 return writer.writeAll(" }");280 return writer.writeAll(" }");
281 },281 },
282 .memoized_decl,282 .memoized_call => unreachable,
283 .memoized_call,
284 => unreachable,
285 },283 },
286 };284 };
287}285}
src/arch/wasm/CodeGen.zig+1-3
...@@ -3255,9 +3255,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {...@@ -3255,9 +3255,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
3255 else => unreachable,3255 else => unreachable,
3256 },3256 },
3257 .un => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(mod)}),3257 .un => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(mod)}),
3258 .memoized_decl,3258 .memoized_call => unreachable,
3259 .memoized_call,
3260 => unreachable,
3261 }3259 }
3262}3260}
32633261
src/codegen.zig+1-3
...@@ -610,9 +610,7 @@ pub fn generateSymbol(...@@ -610,9 +610,7 @@ pub fn generateSymbol(
610 }610 }
611 }611 }
612 },612 },
613 .memoized_decl,613 .memoized_call => unreachable,
614 .memoized_call,
615 => unreachable,
616 }614 }
617 return .ok;615 return .ok;
618}616}
src/codegen/c.zig-1
...@@ -925,7 +925,6 @@ pub const DeclGen = struct {...@@ -925,7 +925,6 @@ pub const DeclGen = struct {
925 .error_set_type,925 .error_set_type,
926 .inferred_error_set_type,926 .inferred_error_set_type,
927 // memoization, not values927 // memoization, not values
928 .memoized_decl,
929 .memoized_call,928 .memoized_call,
930 => unreachable,929 => unreachable,
931930
src/codegen/llvm.zig+1-3
...@@ -3796,9 +3796,7 @@ pub const DeclGen = struct {...@@ -3796,9 +3796,7 @@ pub const DeclGen = struct {
3796 return llvm_union_ty.constNamedStruct(&fields, fields_len);3796 return llvm_union_ty.constNamedStruct(&fields, fields_len);
3797 }3797 }
3798 },3798 },
3799 .memoized_decl,3799 .memoized_call => unreachable,
3800 .memoized_call,
3801 => unreachable,
3802 }3800 }
3803 }3801 }
38043802
src/codegen/spirv.zig+1-3
...@@ -831,9 +831,7 @@ pub const DeclGen = struct {...@@ -831,9 +831,7 @@ pub const DeclGen = struct {
831831
832 try self.addUndef(layout.padding);832 try self.addUndef(layout.padding);
833 },833 },
834 .memoized_decl,834 .memoized_call => unreachable,
835 .memoized_call,
836 => unreachable,
837 }835 }
838 }836 }
839 };837 };
src/type.zig-9
...@@ -446,7 +446,6 @@ pub const Type = struct {...@@ -446,7 +446,6 @@ pub const Type = struct {
446 .aggregate,446 .aggregate,
447 .un,447 .un,
448 // memoization, not types448 // memoization, not types
449 .memoized_decl,
450 .memoized_call,449 .memoized_call,
451 => unreachable,450 => unreachable,
452 }451 }
...@@ -663,7 +662,6 @@ pub const Type = struct {...@@ -663,7 +662,6 @@ pub const Type = struct {
663 .aggregate,662 .aggregate,
664 .un,663 .un,
665 // memoization, not types664 // memoization, not types
666 .memoized_decl,
667 .memoized_call,665 .memoized_call,
668 => unreachable,666 => unreachable,
669 },667 },
...@@ -773,7 +771,6 @@ pub const Type = struct {...@@ -773,7 +771,6 @@ pub const Type = struct {
773 .aggregate,771 .aggregate,
774 .un,772 .un,
775 // memoization, not types773 // memoization, not types
776 .memoized_decl,
777 .memoized_call,774 .memoized_call,
778 => unreachable,775 => unreachable,
779 };776 };
...@@ -1108,7 +1105,6 @@ pub const Type = struct {...@@ -1108,7 +1105,6 @@ pub const Type = struct {
1108 .aggregate,1105 .aggregate,
1109 .un,1106 .un,
1110 // memoization, not types1107 // memoization, not types
1111 .memoized_decl,
1112 .memoized_call,1108 .memoized_call,
1113 => unreachable,1109 => unreachable,
1114 },1110 },
...@@ -1526,7 +1522,6 @@ pub const Type = struct {...@@ -1526,7 +1522,6 @@ pub const Type = struct {
1526 .aggregate,1522 .aggregate,
1527 .un,1523 .un,
1528 // memoization, not types1524 // memoization, not types
1529 .memoized_decl,
1530 .memoized_call,1525 .memoized_call,
1531 => unreachable,1526 => unreachable,
1532 },1527 },
...@@ -1761,7 +1756,6 @@ pub const Type = struct {...@@ -1761,7 +1756,6 @@ pub const Type = struct {
1761 .aggregate,1756 .aggregate,
1762 .un,1757 .un,
1763 // memoization, not types1758 // memoization, not types
1764 .memoized_decl,
1765 .memoized_call,1759 .memoized_call,
1766 => unreachable,1760 => unreachable,
1767 }1761 }
...@@ -2315,7 +2309,6 @@ pub const Type = struct {...@@ -2315,7 +2309,6 @@ pub const Type = struct {
2315 .aggregate,2309 .aggregate,
2316 .un,2310 .un,
2317 // memoization, not types2311 // memoization, not types
2318 .memoized_decl,
2319 .memoized_call,2312 .memoized_call,
2320 => unreachable,2313 => unreachable,
2321 },2314 },
...@@ -2666,7 +2659,6 @@ pub const Type = struct {...@@ -2666,7 +2659,6 @@ pub const Type = struct {
2666 .aggregate,2659 .aggregate,
2667 .un,2660 .un,
2668 // memoization, not types2661 // memoization, not types
2669 .memoized_decl,
2670 .memoized_call,2662 .memoized_call,
2671 => unreachable,2663 => unreachable,
2672 },2664 },
...@@ -2812,7 +2804,6 @@ pub const Type = struct {...@@ -2812,7 +2804,6 @@ pub const Type = struct {
2812 .aggregate,2804 .aggregate,
2813 .un,2805 .un,
2814 // memoization, not types2806 // memoization, not types
2815 .memoized_decl,
2816 .memoized_call,2807 .memoized_call,
2817 => unreachable,2808 => unreachable,
2818 },2809 },
src/value.zig+1-3
...@@ -478,9 +478,7 @@ pub const Value = struct {...@@ -478,9 +478,7 @@ pub const Value = struct {
478 .val = un.val.toValue(),478 .val = un.val.toValue(),
479 }),479 }),
480480
481 .memoized_decl,481 .memoized_call => unreachable,
482 .memoized_call,
483 => unreachable,
484 };482 };
485 }483 }
486484