diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2024-03-22 23:39:44 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2024-03-25 14:49:41 +0000 |
| commit | 9c3670fc930d21215e91849ce5a1fc44c9410fd0 (patch) | |
| tree | a66f2323f44750129d0229f80f58ce1e6c764fba /src/InternPool.zig | |
| parent | 5c628312b16ce972cc3108ed44eed47960e17af7 (diff) | |
| download | zig-9c3670fc930d21215e91849ce5a1fc44c9410fd0.tar.gz zig-9c3670fc930d21215e91849ce5a1fc44c9410fd0.zip | |
compiler: implement analysis-local comptime-mutable memory
This commit changes how we represent comptime-mutable memory
(`comptime var`) in the compiler in order to implement the intended
behavior that references to such memory can only exist at comptime.
It does *not* clean up the representation of mutable values, improve the
representation of comptime-known pointers, or fix the many bugs in the
comptime pointer access code. These will be future enhancements.
Comptime memory lives for the duration of a single Sema, and is not
permitted to escape that one analysis, either by becoming runtime-known
or by becoming comptime-known to other analyses. These restrictions mean
that we can represent comptime allocations not via Decl, but with state
local to Sema - specifically, the new `Sema.comptime_allocs` field. All
comptime-mutable allocations, as well as any comptime-known const allocs
containing references to such memory, live in here. This allows for
relatively fast checking of whether a value references any
comptime-mtuable memory, since we need only traverse values up to
pointers: pointers to Decls can never reference comptime-mutable memory,
and pointers into `Sema.comptime_allocs` always do.
This change exposed some faulty pointer access logic in `Value.zig`.
I've fixed the important cases, but there are some TODOs I've put in
which are definitely possible to hit with sufficiently esoteric code. I
plan to resolve these by auditing all direct accesses to pointers (most
of them ought to use Sema to perform the pointer access!), but for now
this is sufficient for all realistic code and to get tests passing.
This change eliminates `Zcu.tmp_hack_arena`, instead using the Sema
arena for comptime memory mutations, which is possible since comptime
memory is now local to the current Sema.
This change should allow `Decl` to store only an `InternPool.Index`
rather than a full-blown `ty: Type, val: Value`. This commit does not
perform this refactor.
Diffstat (limited to 'src/InternPool.zig')
| -rw-r--r-- | src/InternPool.zig | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/src/InternPool.zig b/src/InternPool.zig index 8611a67564..daec1f5c0d 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -389,6 +389,8 @@ pub const RuntimeIndex = enum(u32) { } }; +pub const ComptimeAllocIndex = enum(u32) { _ }; + pub const DeclIndex = std.zig.DeclIndex; pub const OptionalDeclIndex = std.zig.OptionalDeclIndex; @@ -979,7 +981,7 @@ pub const Key = union(enum) { const Tag = @typeInfo(Addr).Union.tag_type.?; decl: DeclIndex, - mut_decl: MutDecl, + comptime_alloc: ComptimeAllocIndex, anon_decl: AnonDecl, comptime_field: Index, int: Index, @@ -1172,20 +1174,14 @@ pub const Key = union(enum) { const seed2 = seed + @intFromEnum(addr); const common = asBytes(&ptr.ty); return switch (ptr.addr) { - .decl => |x| Hash.hash(seed2, common ++ asBytes(&x)), - - .mut_decl => |x| Hash.hash( - seed2, - common ++ asBytes(&x.decl) ++ asBytes(&x.runtime_index), - ), - - .anon_decl => |x| Hash.hash(seed2, common ++ asBytes(&x)), - + inline .decl, + .comptime_alloc, + .anon_decl, .int, .eu_payload, .opt_payload, .comptime_field, - => |int| Hash.hash(seed2, common ++ asBytes(&int)), + => |x| Hash.hash(seed2, common ++ asBytes(&x)), .elem, .field => |x| Hash.hash( seed2, @@ -1452,7 +1448,7 @@ pub const Key = union(enum) { return switch (a_info.addr) { .decl => |a_decl| a_decl == b_info.addr.decl, - .mut_decl => |a_mut_decl| std.meta.eql(a_mut_decl, b_info.addr.mut_decl), + .comptime_alloc => |a_alloc| a_alloc == b_info.addr.comptime_alloc, .anon_decl => |ad| ad.val == b_info.addr.anon_decl.val and ad.orig_ty == b_info.addr.anon_decl.orig_ty, .int => |a_int| a_int == b_info.addr.int, @@ -2787,7 +2783,7 @@ pub const Index = enum(u32) { undef: DataIsIndex, simple_value: struct { data: SimpleValue }, ptr_decl: struct { data: *PtrDecl }, - ptr_mut_decl: struct { data: *PtrMutDecl }, + ptr_comptime_alloc: struct { data: *PtrComptimeAlloc }, ptr_anon_decl: struct { data: *PtrAnonDecl }, ptr_anon_decl_aligned: struct { data: *PtrAnonDeclAligned }, ptr_comptime_field: struct { data: *PtrComptimeField }, @@ -3243,8 +3239,8 @@ pub const Tag = enum(u8) { /// data is extra index of `PtrDecl`, which contains the type and address. ptr_decl, /// A pointer to a decl that can be mutated at comptime. - /// data is extra index of `PtrMutDecl`, which contains the type and address. - ptr_mut_decl, + /// data is extra index of `PtrComptimeAlloc`, which contains the type and address. + ptr_comptime_alloc, /// A pointer to an anonymous decl. /// data is extra index of `PtrAnonDecl`, which contains the pointer type and decl value. /// The alignment of the anonymous decl is communicated via the pointer type. @@ -3448,7 +3444,7 @@ pub const Tag = enum(u8) { .undef => unreachable, .simple_value => unreachable, .ptr_decl => PtrDecl, - .ptr_mut_decl => PtrMutDecl, + .ptr_comptime_alloc => PtrComptimeAlloc, .ptr_anon_decl => PtrAnonDecl, .ptr_anon_decl_aligned => PtrAnonDeclAligned, .ptr_comptime_field => PtrComptimeField, @@ -4129,10 +4125,9 @@ pub const PtrAnonDeclAligned = struct { orig_ty: Index, }; -pub const PtrMutDecl = struct { +pub const PtrComptimeAlloc = struct { ty: Index, - decl: DeclIndex, - runtime_index: RuntimeIndex, + index: ComptimeAllocIndex, }; pub const PtrComptimeField = struct { @@ -4537,14 +4532,11 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key { .addr = .{ .decl = info.decl }, } }; }, - .ptr_mut_decl => { - const info = ip.extraData(PtrMutDecl, data); + .ptr_comptime_alloc => { + const info = ip.extraData(PtrComptimeAlloc, data); return .{ .ptr = .{ .ty = info.ty, - .addr = .{ .mut_decl = .{ - .decl = info.decl, - .runtime_index = info.runtime_index, - } }, + .addr = .{ .comptime_alloc = info.index }, } }; }, .ptr_anon_decl => { @@ -5186,12 +5178,11 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { .decl = decl, }), }), - .mut_decl => |mut_decl| ip.items.appendAssumeCapacity(.{ - .tag = .ptr_mut_decl, - .data = try ip.addExtra(gpa, PtrMutDecl{ + .comptime_alloc => |alloc_index| ip.items.appendAssumeCapacity(.{ + .tag = .ptr_comptime_alloc, + .data = try ip.addExtra(gpa, PtrComptimeAlloc{ .ty = ptr.ty, - .decl = mut_decl.decl, - .runtime_index = mut_decl.runtime_index, + .index = alloc_index, }), }), .anon_decl => |anon_decl| ip.items.appendAssumeCapacity( @@ -7265,6 +7256,7 @@ fn addExtraAssumeCapacity(ip: *InternPool, extra: anytype) u32 { Tag.TypePointer.VectorIndex, TrackedInst.Index, TrackedInst.Index.Optional, + ComptimeAllocIndex, => @intFromEnum(@field(extra, field.name)), u32, @@ -7342,6 +7334,7 @@ fn extraDataTrail(ip: *const InternPool, comptime T: type, index: usize) struct Tag.TypePointer.VectorIndex, TrackedInst.Index, TrackedInst.Index.Optional, + ComptimeAllocIndex, => @enumFromInt(int32), u32, @@ -8144,7 +8137,7 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void { .simple_type => 0, .simple_value => 0, .ptr_decl => @sizeOf(PtrDecl), - .ptr_mut_decl => @sizeOf(PtrMutDecl), + .ptr_comptime_alloc => @sizeOf(PtrComptimeAlloc), .ptr_anon_decl => @sizeOf(PtrAnonDecl), .ptr_anon_decl_aligned => @sizeOf(PtrAnonDeclAligned), .ptr_comptime_field => @sizeOf(PtrComptimeField), @@ -8275,7 +8268,7 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void { .type_function, .undef, .ptr_decl, - .ptr_mut_decl, + .ptr_comptime_alloc, .ptr_anon_decl, .ptr_anon_decl_aligned, .ptr_comptime_field, @@ -8690,7 +8683,7 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index { .simple_value => unreachable, // handled via Index above inline .ptr_decl, - .ptr_mut_decl, + .ptr_comptime_alloc, .ptr_anon_decl, .ptr_anon_decl_aligned, .ptr_comptime_field, @@ -8822,10 +8815,8 @@ pub fn getBackingDecl(ip: *const InternPool, val: Index) OptionalDeclIndex { var base = @intFromEnum(val); while (true) { switch (ip.items.items(.tag)[base]) { - inline .ptr_decl, - .ptr_mut_decl, - => |tag| return @enumFromInt(ip.extra.items[ - ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "decl").? + .ptr_decl => return @enumFromInt(ip.extra.items[ + ip.items.items(.data)[base] + std.meta.fieldIndex(PtrDecl, "decl").? ]), inline .ptr_eu_payload, .ptr_opt_payload, @@ -8834,8 +8825,8 @@ pub fn getBackingDecl(ip: *const InternPool, val: Index) OptionalDeclIndex { => |tag| base = ip.extra.items[ ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "base").? ], - inline .ptr_slice => |tag| base = ip.extra.items[ - ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "ptr").? + .ptr_slice => base = ip.extra.items[ + ip.items.items(.data)[base] + std.meta.fieldIndex(PtrSlice, "ptr").? ], else => return .none, } @@ -8847,7 +8838,7 @@ pub fn getBackingAddrTag(ip: *const InternPool, val: Index) ?Key.Ptr.Addr.Tag { while (true) { switch (ip.items.items(.tag)[base]) { .ptr_decl => return .decl, - .ptr_mut_decl => return .mut_decl, + .ptr_comptime_alloc => return .comptime_alloc, .ptr_anon_decl, .ptr_anon_decl_aligned => return .anon_decl, .ptr_comptime_field => return .comptime_field, .ptr_int => return .int, @@ -9023,7 +9014,7 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois .undef, .simple_value, .ptr_decl, - .ptr_mut_decl, + .ptr_comptime_alloc, .ptr_anon_decl, .ptr_anon_decl_aligned, .ptr_comptime_field, |
