Skip to content

Commit b25abe4

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 b25abe4

28 files changed

+886
-557
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/enums.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pub inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumFi
4141
for (&result, fields) |*r, f| {
4242
r.* = @enumFromInt(f.value);
4343
}
44-
return &result;
44+
const final = result;
45+
return &final;
4546
}
4647
}
4748

lib/std/fmt.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,8 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
18291829
var buf: [count(fmt, args):0]u8 = undefined;
18301830
_ = bufPrint(&buf, fmt, args) catch unreachable;
18311831
buf[buf.len] = 0;
1832-
return &buf;
1832+
const final = buf;
1833+
return &final;
18331834
}
18341835
}
18351836

lib/std/meta.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ pub fn fieldNames(comptime T: type) *const [fields(T).len][:0]const u8 {
465465
var names: [fieldInfos.len][:0]const u8 = undefined;
466466
// This concat can be removed with the next zig1 update.
467467
for (&names, fieldInfos) |*name, field| name.* = field.name ++ "";
468-
break :blk &names;
468+
const final = names;
469+
break :blk &final;
469470
};
470471
}
471472

@@ -506,7 +507,8 @@ pub fn tags(comptime T: type) *const [fields(T).len]T {
506507
for (fieldInfos, 0..) |field, i| {
507508
res[i] = @field(T, field.name);
508509
}
509-
break :blk &res;
510+
const final = res;
511+
break :blk &final;
510512
};
511513
}
512514

lib/std/unicode.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,8 @@ pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16Le
13581358
var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
13591359
const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
13601360
assert(len == utf16le_len);
1361-
break :blk &utf16le;
1361+
const final = utf16le;
1362+
break :blk &final;
13621363
};
13631364
}
13641365

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,11 @@ 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

10921094
pub const InferredAlloc = struct {

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
};

0 commit comments

Comments
 (0)