Skip to content

Commit 9050b34

Browse files
committed
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.
1 parent e90583f commit 9050b34

21 files changed

+844
-538
lines changed

lib/std/Target.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,8 @@ pub const Cpu = struct {
13171317
for (decls, 0..) |decl, i| {
13181318
array[i] = &@field(cpus, decl.name);
13191319
}
1320-
return &array;
1320+
const finalized = array;
1321+
return &finalized;
13211322
}
13221323
};
13231324

lib/std/zig/AstGen.zig

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8296,22 +8296,27 @@ fn localVarRef(
82968296
});
82978297
}
82988298

8299-
const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
8300-
gz,
8301-
ident,
8302-
num_namespaces_out,
8303-
.{ .ref = local_ptr.ptr },
8304-
.{ .token = local_ptr.token_src },
8305-
) else local_ptr.ptr;
8306-
83078299
switch (ri.rl) {
83088300
.ref, .ref_coerced_ty => {
8301+
const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
8302+
gz,
8303+
ident,
8304+
num_namespaces_out,
8305+
.{ .ref = local_ptr.ptr },
8306+
.{ .token = local_ptr.token_src },
8307+
) else local_ptr.ptr;
83098308
local_ptr.used_as_lvalue = true;
83108309
return ptr_inst;
83118310
},
83128311
else => {
8313-
const loaded = try gz.addUnNode(.load, ptr_inst, ident);
8314-
return rvalueNoCoercePreRef(gz, ri, loaded, ident);
8312+
const val_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
8313+
gz,
8314+
ident,
8315+
num_namespaces_out,
8316+
.{ .ref_load = local_ptr.ptr },
8317+
.{ .token = local_ptr.token_src },
8318+
) else try gz.addUnNode(.load, local_ptr.ptr, ident);
8319+
return rvalueNoCoercePreRef(gz, ri, val_inst, ident);
83158320
},
83168321
}
83178322
}
@@ -8390,6 +8395,7 @@ fn tunnelThroughClosure(
83908395
/// The value being captured.
83918396
value: union(enum) {
83928397
ref: Zir.Inst.Ref,
8398+
ref_load: Zir.Inst.Ref,
83938399
decl_val: Zir.NullTerminatedString,
83948400
decl_ref: Zir.NullTerminatedString,
83958401
},
@@ -8400,7 +8406,8 @@ fn tunnelThroughClosure(
84008406
},
84018407
) !Zir.Inst.Ref {
84028408
switch (value) {
8403-
.ref => |v| if (v.toIndex() == null) return v, // trivia value; do not need tunnel
8409+
.ref => |v| if (v.toIndex() == null) return v, // trivial value; do not need tunnel
8410+
.ref_load => |v| assert(v.toIndex() != null), // there are no constant pointer refs
84048411
.decl_val, .decl_ref => {},
84058412
}
84068413

@@ -8433,6 +8440,7 @@ fn tunnelThroughClosure(
84338440
// captures as required, starting with the outermost namespace.
84348441
const root_capture = Zir.Inst.Capture.wrap(switch (value) {
84358442
.ref => |v| .{ .instruction = v.toIndex().? },
8443+
.ref_load => |v| .{ .instruction_load = v.toIndex().? },
84368444
.decl_val => |str| .{ .decl_val = str },
84378445
.decl_ref => |str| .{ .decl_ref = str },
84388446
});

lib/std/zig/Zir.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,20 +3058,23 @@ pub const Inst = struct {
30583058

30593059
/// Represents a single value being captured in a type declaration's closure.
30603060
pub const Capture = packed struct(u32) {
3061-
tag: enum(u2) {
3061+
tag: enum(u3) {
30623062
/// `data` is a `u16` index into the parent closure.
30633063
nested,
30643064
/// `data` is a `Zir.Inst.Index` to an instruction whose value is being captured.
30653065
instruction,
3066+
/// `data` is a `Zir.Inst.Index` to an instruction representing an alloc whose contents is being captured.
3067+
instruction_load,
30663068
/// `data` is a `NullTerminatedString` to a decl name.
30673069
decl_val,
30683070
/// `data` is a `NullTerminatedString` to a decl name.
30693071
decl_ref,
30703072
},
3071-
data: u30,
3073+
data: u29,
30723074
pub const Unwrapped = union(enum) {
30733075
nested: u16,
30743076
instruction: Zir.Inst.Index,
3077+
instruction_load: Zir.Inst.Index,
30753078
decl_val: NullTerminatedString,
30763079
decl_ref: NullTerminatedString,
30773080
};
@@ -3085,6 +3088,10 @@ pub const Inst = struct {
30853088
.tag = .instruction,
30863089
.data = @intCast(@intFromEnum(inst)),
30873090
},
3091+
.instruction_load => |inst| .{
3092+
.tag = .instruction_load,
3093+
.data = @intCast(@intFromEnum(inst)),
3094+
},
30883095
.decl_val => |str| .{
30893096
.tag = .decl_val,
30903097
.data = @intCast(@intFromEnum(str)),
@@ -3099,6 +3106,7 @@ pub const Inst = struct {
30993106
return switch (cap.tag) {
31003107
.nested => .{ .nested = @intCast(cap.data) },
31013108
.instruction => .{ .instruction = @enumFromInt(cap.data) },
3109+
.instruction_load => .{ .instruction_load = @enumFromInt(cap.data) },
31023110
.decl_val => .{ .decl_val = @enumFromInt(cap.data) },
31033111
.decl_ref => .{ .decl_ref = @enumFromInt(cap.data) },
31043112
};

src/Air.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,14 @@ pub const Inst = struct {
10841084
inferred_alloc: InferredAlloc,
10851085

10861086
pub const InferredAllocComptime = struct {
1087-
decl_index: InternPool.DeclIndex,
10881087
alignment: InternPool.Alignment,
10891088
is_const: bool,
1089+
/// This is `undefined` until we encounter a `store_to_inferred_alloc`,
1090+
/// at which point the pointer is created and stored here.
1091+
ptr: InternPool.Index,
10901092
};
10911093

1092-
pub const InferredAlloc = struct {
1094+
pub const InferredAlloc = packed struct {
10931095
alignment: InternPool.Alignment,
10941096
is_const: bool,
10951097
};

src/Compilation.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
13821382
.global_zir_cache = global_zir_cache,
13831383
.local_zir_cache = local_zir_cache,
13841384
.emit_h = emit_h,
1385-
.tmp_hack_arena = std.heap.ArenaAllocator.init(gpa),
13861385
.error_limit = error_limit,
13871386
.llvm_object = null,
13881387
};

src/InternPool.zig

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ pub const RuntimeIndex = enum(u32) {
389389
}
390390
};
391391

392+
pub const ComptimeAllocIndex = enum(u32) { _ };
393+
392394
pub const DeclIndex = std.zig.DeclIndex;
393395
pub const OptionalDeclIndex = std.zig.OptionalDeclIndex;
394396

@@ -979,7 +981,7 @@ pub const Key = union(enum) {
979981
const Tag = @typeInfo(Addr).Union.tag_type.?;
980982

981983
decl: DeclIndex,
982-
mut_decl: MutDecl,
984+
comptime_alloc: ComptimeAllocIndex,
983985
anon_decl: AnonDecl,
984986
comptime_field: Index,
985987
int: Index,
@@ -1172,20 +1174,14 @@ pub const Key = union(enum) {
11721174
const seed2 = seed + @intFromEnum(addr);
11731175
const common = asBytes(&ptr.ty);
11741176
return switch (ptr.addr) {
1175-
.decl => |x| Hash.hash(seed2, common ++ asBytes(&x)),
1176-
1177-
.mut_decl => |x| Hash.hash(
1178-
seed2,
1179-
common ++ asBytes(&x.decl) ++ asBytes(&x.runtime_index),
1180-
),
1181-
1182-
.anon_decl => |x| Hash.hash(seed2, common ++ asBytes(&x)),
1183-
1177+
inline .decl,
1178+
.comptime_alloc,
1179+
.anon_decl,
11841180
.int,
11851181
.eu_payload,
11861182
.opt_payload,
11871183
.comptime_field,
1188-
=> |int| Hash.hash(seed2, common ++ asBytes(&int)),
1184+
=> |x| Hash.hash(seed2, common ++ asBytes(&x)),
11891185

11901186
.elem, .field => |x| Hash.hash(
11911187
seed2,
@@ -1452,7 +1448,7 @@ pub const Key = union(enum) {
14521448

14531449
return switch (a_info.addr) {
14541450
.decl => |a_decl| a_decl == b_info.addr.decl,
1455-
.mut_decl => |a_mut_decl| std.meta.eql(a_mut_decl, b_info.addr.mut_decl),
1451+
.comptime_alloc => |a_alloc| a_alloc == b_info.addr.comptime_alloc,
14561452
.anon_decl => |ad| ad.val == b_info.addr.anon_decl.val and
14571453
ad.orig_ty == b_info.addr.anon_decl.orig_ty,
14581454
.int => |a_int| a_int == b_info.addr.int,
@@ -2787,7 +2783,7 @@ pub const Index = enum(u32) {
27872783
undef: DataIsIndex,
27882784
simple_value: struct { data: SimpleValue },
27892785
ptr_decl: struct { data: *PtrDecl },
2790-
ptr_mut_decl: struct { data: *PtrMutDecl },
2786+
ptr_comptime_alloc: struct { data: *PtrComptimeAlloc },
27912787
ptr_anon_decl: struct { data: *PtrAnonDecl },
27922788
ptr_anon_decl_aligned: struct { data: *PtrAnonDeclAligned },
27932789
ptr_comptime_field: struct { data: *PtrComptimeField },
@@ -3243,8 +3239,8 @@ pub const Tag = enum(u8) {
32433239
/// data is extra index of `PtrDecl`, which contains the type and address.
32443240
ptr_decl,
32453241
/// A pointer to a decl that can be mutated at comptime.
3246-
/// data is extra index of `PtrMutDecl`, which contains the type and address.
3247-
ptr_mut_decl,
3242+
/// data is extra index of `PtrComptimeAlloc`, which contains the type and address.
3243+
ptr_comptime_alloc,
32483244
/// A pointer to an anonymous decl.
32493245
/// data is extra index of `PtrAnonDecl`, which contains the pointer type and decl value.
32503246
/// The alignment of the anonymous decl is communicated via the pointer type.
@@ -3448,7 +3444,7 @@ pub const Tag = enum(u8) {
34483444
.undef => unreachable,
34493445
.simple_value => unreachable,
34503446
.ptr_decl => PtrDecl,
3451-
.ptr_mut_decl => PtrMutDecl,
3447+
.ptr_comptime_alloc => PtrComptimeAlloc,
34523448
.ptr_anon_decl => PtrAnonDecl,
34533449
.ptr_anon_decl_aligned => PtrAnonDeclAligned,
34543450
.ptr_comptime_field => PtrComptimeField,
@@ -4129,10 +4125,9 @@ pub const PtrAnonDeclAligned = struct {
41294125
orig_ty: Index,
41304126
};
41314127

4132-
pub const PtrMutDecl = struct {
4128+
pub const PtrComptimeAlloc = struct {
41334129
ty: Index,
4134-
decl: DeclIndex,
4135-
runtime_index: RuntimeIndex,
4130+
index: ComptimeAllocIndex,
41364131
};
41374132

41384133
pub const PtrComptimeField = struct {
@@ -4537,14 +4532,11 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
45374532
.addr = .{ .decl = info.decl },
45384533
} };
45394534
},
4540-
.ptr_mut_decl => {
4541-
const info = ip.extraData(PtrMutDecl, data);
4535+
.ptr_comptime_alloc => {
4536+
const info = ip.extraData(PtrComptimeAlloc, data);
45424537
return .{ .ptr = .{
45434538
.ty = info.ty,
4544-
.addr = .{ .mut_decl = .{
4545-
.decl = info.decl,
4546-
.runtime_index = info.runtime_index,
4547-
} },
4539+
.addr = .{ .comptime_alloc = info.index },
45484540
} };
45494541
},
45504542
.ptr_anon_decl => {
@@ -5186,12 +5178,11 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
51865178
.decl = decl,
51875179
}),
51885180
}),
5189-
.mut_decl => |mut_decl| ip.items.appendAssumeCapacity(.{
5190-
.tag = .ptr_mut_decl,
5191-
.data = try ip.addExtra(gpa, PtrMutDecl{
5181+
.comptime_alloc => |alloc_index| ip.items.appendAssumeCapacity(.{
5182+
.tag = .ptr_comptime_alloc,
5183+
.data = try ip.addExtra(gpa, PtrComptimeAlloc{
51925184
.ty = ptr.ty,
5193-
.decl = mut_decl.decl,
5194-
.runtime_index = mut_decl.runtime_index,
5185+
.index = alloc_index,
51955186
}),
51965187
}),
51975188
.anon_decl => |anon_decl| ip.items.appendAssumeCapacity(
@@ -7265,6 +7256,7 @@ fn addExtraAssumeCapacity(ip: *InternPool, extra: anytype) u32 {
72657256
Tag.TypePointer.VectorIndex,
72667257
TrackedInst.Index,
72677258
TrackedInst.Index.Optional,
7259+
ComptimeAllocIndex,
72687260
=> @intFromEnum(@field(extra, field.name)),
72697261

72707262
u32,
@@ -7342,6 +7334,7 @@ fn extraDataTrail(ip: *const InternPool, comptime T: type, index: usize) struct
73427334
Tag.TypePointer.VectorIndex,
73437335
TrackedInst.Index,
73447336
TrackedInst.Index.Optional,
7337+
ComptimeAllocIndex,
73457338
=> @enumFromInt(int32),
73467339

73477340
u32,
@@ -8144,7 +8137,7 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
81448137
.simple_type => 0,
81458138
.simple_value => 0,
81468139
.ptr_decl => @sizeOf(PtrDecl),
8147-
.ptr_mut_decl => @sizeOf(PtrMutDecl),
8140+
.ptr_comptime_alloc => @sizeOf(PtrComptimeAlloc),
81488141
.ptr_anon_decl => @sizeOf(PtrAnonDecl),
81498142
.ptr_anon_decl_aligned => @sizeOf(PtrAnonDeclAligned),
81508143
.ptr_comptime_field => @sizeOf(PtrComptimeField),
@@ -8275,7 +8268,7 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
82758268
.type_function,
82768269
.undef,
82778270
.ptr_decl,
8278-
.ptr_mut_decl,
8271+
.ptr_comptime_alloc,
82798272
.ptr_anon_decl,
82808273
.ptr_anon_decl_aligned,
82818274
.ptr_comptime_field,
@@ -8690,7 +8683,7 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index {
86908683
.simple_value => unreachable, // handled via Index above
86918684

86928685
inline .ptr_decl,
8693-
.ptr_mut_decl,
8686+
.ptr_comptime_alloc,
86948687
.ptr_anon_decl,
86958688
.ptr_anon_decl_aligned,
86968689
.ptr_comptime_field,
@@ -8822,10 +8815,8 @@ pub fn getBackingDecl(ip: *const InternPool, val: Index) OptionalDeclIndex {
88228815
var base = @intFromEnum(val);
88238816
while (true) {
88248817
switch (ip.items.items(.tag)[base]) {
8825-
inline .ptr_decl,
8826-
.ptr_mut_decl,
8827-
=> |tag| return @enumFromInt(ip.extra.items[
8828-
ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "decl").?
8818+
.ptr_decl => return @enumFromInt(ip.extra.items[
8819+
ip.items.items(.data)[base] + std.meta.fieldIndex(PtrDecl, "decl").?
88298820
]),
88308821
inline .ptr_eu_payload,
88318822
.ptr_opt_payload,
@@ -8834,8 +8825,8 @@ pub fn getBackingDecl(ip: *const InternPool, val: Index) OptionalDeclIndex {
88348825
=> |tag| base = ip.extra.items[
88358826
ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "base").?
88368827
],
8837-
inline .ptr_slice => |tag| base = ip.extra.items[
8838-
ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "ptr").?
8828+
.ptr_slice => base = ip.extra.items[
8829+
ip.items.items(.data)[base] + std.meta.fieldIndex(PtrSlice, "ptr").?
88398830
],
88408831
else => return .none,
88418832
}
@@ -8847,7 +8838,7 @@ pub fn getBackingAddrTag(ip: *const InternPool, val: Index) ?Key.Ptr.Addr.Tag {
88478838
while (true) {
88488839
switch (ip.items.items(.tag)[base]) {
88498840
.ptr_decl => return .decl,
8850-
.ptr_mut_decl => return .mut_decl,
8841+
.ptr_comptime_alloc => return .comptime_alloc,
88518842
.ptr_anon_decl, .ptr_anon_decl_aligned => return .anon_decl,
88528843
.ptr_comptime_field => return .comptime_field,
88538844
.ptr_int => return .int,
@@ -9023,7 +9014,7 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois
90239014
.undef,
90249015
.simple_value,
90259016
.ptr_decl,
9026-
.ptr_mut_decl,
9017+
.ptr_comptime_alloc,
90279018
.ptr_anon_decl,
90289019
.ptr_anon_decl_aligned,
90299020
.ptr_comptime_field,

0 commit comments

Comments
 (0)