Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: implement analysis-local comptime-mutable memory #19414

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/std/Target.zig
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
3 changes: 2 additions & 1 deletion lib/std/enums.zig
Expand Up @@ -41,7 +41,8 @@ pub inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumFi
for (&result, fields) |*r, f| {
r.* = @enumFromInt(f.value);
}
return &result;
const final = result;
return &final;
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/std/fmt.zig
Expand Up @@ -1829,7 +1829,8 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
var buf: [count(fmt, args):0]u8 = undefined;
_ = bufPrint(&buf, fmt, args) catch unreachable;
buf[buf.len] = 0;
return &buf;
const final = buf;
return &final;
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/std/meta.zig
Expand Up @@ -465,7 +465,8 @@ pub fn fieldNames(comptime T: type) *const [fields(T).len][:0]const u8 {
var names: [fieldInfos.len][:0]const u8 = undefined;
// This concat can be removed with the next zig1 update.
for (&names, fieldInfos) |*name, field| name.* = field.name ++ "";
break :blk &names;
const final = names;
break :blk &final;
};
}

Expand Down Expand Up @@ -506,7 +507,8 @@ pub fn tags(comptime T: type) *const [fields(T).len]T {
for (fieldInfos, 0..) |field, i| {
res[i] = @field(T, field.name);
}
break :blk &res;
const final = res;
break :blk &final;
};
}

Expand Down
3 changes: 2 additions & 1 deletion lib/std/unicode.zig
Expand Up @@ -1358,7 +1358,8 @@ pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16Le
var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
assert(len == utf16le_len);
break :blk &utf16le;
const final = utf16le;
break :blk &final;
};
}

Expand Down
30 changes: 19 additions & 11 deletions lib/std/zig/AstGen.zig
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
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
4 changes: 3 additions & 1 deletion src/Air.zig
Expand Up @@ -1084,9 +1084,11 @@ 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 {
Expand Down
1 change: 0 additions & 1 deletion src/Compilation.zig
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