Skip to content

Commit

Permalink
compiler: implement analysis-local comptime-mutable memory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mlugg committed Mar 24, 2024
1 parent e90583f commit 9050b34
Show file tree
Hide file tree
Showing 21 changed files with 844 additions and 538 deletions.
3 changes: 2 additions & 1 deletion lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,8 @@ pub const Cpu = struct {
for (decls, 0..) |decl, i| {
array[i] = &@field(cpus, decl.name);
}
return &array;
const finalized = array;
return &finalized;
}
};

Expand Down
30 changes: 19 additions & 11 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8296,22 +8296,27 @@ fn localVarRef(
});
}

const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
gz,
ident,
num_namespaces_out,
.{ .ref = local_ptr.ptr },
.{ .token = local_ptr.token_src },
) else local_ptr.ptr;

switch (ri.rl) {
.ref, .ref_coerced_ty => {
const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
gz,
ident,
num_namespaces_out,
.{ .ref = local_ptr.ptr },
.{ .token = local_ptr.token_src },
) else local_ptr.ptr;
local_ptr.used_as_lvalue = true;
return ptr_inst;
},
else => {
const loaded = try gz.addUnNode(.load, ptr_inst, ident);
return rvalueNoCoercePreRef(gz, ri, loaded, ident);
const val_inst = if (num_namespaces_out != 0) try tunnelThroughClosure(
gz,
ident,
num_namespaces_out,
.{ .ref_load = local_ptr.ptr },
.{ .token = local_ptr.token_src },
) else try gz.addUnNode(.load, local_ptr.ptr, ident);
return rvalueNoCoercePreRef(gz, ri, val_inst, ident);
},
}
}
Expand Down Expand Up @@ -8390,6 +8395,7 @@ fn tunnelThroughClosure(
/// The value being captured.
value: union(enum) {
ref: Zir.Inst.Ref,
ref_load: Zir.Inst.Ref,
decl_val: Zir.NullTerminatedString,
decl_ref: Zir.NullTerminatedString,
},
Expand All @@ -8400,7 +8406,8 @@ fn tunnelThroughClosure(
},
) !Zir.Inst.Ref {
switch (value) {
.ref => |v| if (v.toIndex() == null) return v, // trivia value; do not need tunnel
.ref => |v| if (v.toIndex() == null) return v, // trivial value; do not need tunnel
.ref_load => |v| assert(v.toIndex() != null), // there are no constant pointer refs
.decl_val, .decl_ref => {},
}

Expand Down Expand Up @@ -8433,6 +8440,7 @@ fn tunnelThroughClosure(
// captures as required, starting with the outermost namespace.
const root_capture = Zir.Inst.Capture.wrap(switch (value) {
.ref => |v| .{ .instruction = v.toIndex().? },
.ref_load => |v| .{ .instruction_load = v.toIndex().? },
.decl_val => |str| .{ .decl_val = str },
.decl_ref => |str| .{ .decl_ref = str },
});
Expand Down
12 changes: 10 additions & 2 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3058,20 +3058,23 @@ pub const Inst = struct {

/// Represents a single value being captured in a type declaration's closure.
pub const Capture = packed struct(u32) {
tag: enum(u2) {
tag: enum(u3) {
/// `data` is a `u16` index into the parent closure.
nested,
/// `data` is a `Zir.Inst.Index` to an instruction whose value is being captured.
instruction,
/// `data` is a `Zir.Inst.Index` to an instruction representing an alloc whose contents is being captured.
instruction_load,
/// `data` is a `NullTerminatedString` to a decl name.
decl_val,
/// `data` is a `NullTerminatedString` to a decl name.
decl_ref,
},
data: u30,
data: u29,
pub const Unwrapped = union(enum) {
nested: u16,
instruction: Zir.Inst.Index,
instruction_load: Zir.Inst.Index,
decl_val: NullTerminatedString,
decl_ref: NullTerminatedString,
};
Expand All @@ -3085,6 +3088,10 @@ pub const Inst = struct {
.tag = .instruction,
.data = @intCast(@intFromEnum(inst)),
},
.instruction_load => |inst| .{
.tag = .instruction_load,
.data = @intCast(@intFromEnum(inst)),
},
.decl_val => |str| .{
.tag = .decl_val,
.data = @intCast(@intFromEnum(str)),
Expand All @@ -3099,6 +3106,7 @@ pub const Inst = struct {
return switch (cap.tag) {
.nested => .{ .nested = @intCast(cap.data) },
.instruction => .{ .instruction = @enumFromInt(cap.data) },
.instruction_load => .{ .instruction_load = @enumFromInt(cap.data) },
.decl_val => .{ .decl_val = @enumFromInt(cap.data) },
.decl_ref => .{ .decl_ref = @enumFromInt(cap.data) },
};
Expand Down
6 changes: 4 additions & 2 deletions src/Air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,14 @@ pub const Inst = struct {
inferred_alloc: InferredAlloc,

pub const InferredAllocComptime = struct {
decl_index: InternPool.DeclIndex,
alignment: InternPool.Alignment,
is_const: bool,
/// This is `undefined` until we encounter a `store_to_inferred_alloc`,
/// at which point the pointer is created and stored here.
ptr: InternPool.Index,
};

pub const InferredAlloc = struct {
pub const InferredAlloc = packed struct {
alignment: InternPool.Alignment,
is_const: bool,
};
Expand Down
1 change: 0 additions & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
.global_zir_cache = global_zir_cache,
.local_zir_cache = local_zir_cache,
.emit_h = emit_h,
.tmp_hack_arena = std.heap.ArenaAllocator.init(gpa),
.error_limit = error_limit,
.llvm_object = null,
};
Expand Down
73 changes: 32 additions & 41 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9050b34

Please sign in to comment.