authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-01 08:10:45+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:58-07:00
log9b48fc2833be409902f4d3256d2d921f4f924e0d
tree17281521b79b188aba2605b25894bc066d24e065
parentcab79b0877e9e7239045dc55d3b2bcff190016cb

Allocate capture scopes in gpa instead of Decl.value_arena

This eliminates the last major use of value_arena, in preparation to remove it.

2 files changed, 101 insertions(+), 66 deletions(-)

src/Module.zig+46-30
......@@ -277,61 +277,75 @@ pub const Export = struct {
277277};
278278
279279pub const CaptureScope = struct {
280 refs: u32,
280281 parent: ?*CaptureScope,
281282
282283 /// Values from this decl's evaluation that will be closed over in
283 /// child decls. Values stored in the value_arena of the linked decl.
284 /// During sema, this map is backed by the gpa. Once sema completes,
285 /// it is reallocated using the value_arena.
286 captures: std.AutoHashMapUnmanaged(Zir.Inst.Index, TypedValue) = .{},
284 /// child decls. This map is backed by the gpa, and deinited when
285 /// the refcount reaches 0.
286 captures: std.AutoHashMapUnmanaged(Zir.Inst.Index, Capture) = .{},
287287
288 pub fn failed(noalias self: *const @This()) bool {
288 pub const Capture = union(enum) {
289 comptime_val: InternPool.Index, // index of value
290 runtime_val: InternPool.Index, // index of type
291 };
292
293 pub fn failed(noalias self: *const CaptureScope) bool {
289294 return self.captures.available == 0 and self.captures.size == std.math.maxInt(u32);
290295 }
291296
292 pub fn fail(noalias self: *@This()) void {
297 pub fn fail(noalias self: *CaptureScope, gpa: Allocator) void {
298 self.captures.deinit(gpa);
293299 self.captures.available = 0;
294300 self.captures.size = std.math.maxInt(u32);
295301 }
302
303 pub fn incRef(self: *CaptureScope) void {
304 self.refs += 1;
305 }
306
307 pub fn decRef(self: *CaptureScope, gpa: Allocator) void {
308 self.refs -= 1;
309 if (self.refs > 0) return;
310 if (self.parent) |p| p.decRef(gpa);
311 if (!self.failed()) {
312 self.captures.deinit(gpa);
313 }
314 }
296315};
297316
298317pub const WipCaptureScope = struct {
299318 scope: *CaptureScope,
300319 finalized: bool,
301320 gpa: Allocator,
302 perm_arena: Allocator,
303321
304 pub fn init(gpa: Allocator, perm_arena: Allocator, parent: ?*CaptureScope) !@This() {
305 const scope = try perm_arena.create(CaptureScope);
306 scope.* = .{ .parent = parent };
307 return @This(){
322 pub fn init(gpa: Allocator, parent: ?*CaptureScope) !WipCaptureScope {
323 const scope = try gpa.create(CaptureScope);
324 if (parent) |p| p.incRef();
325 scope.* = .{ .refs = 1, .parent = parent };
326 return .{
308327 .scope = scope,
309328 .finalized = false,
310329 .gpa = gpa,
311 .perm_arena = perm_arena,
312330 };
313331 }
314332
315 pub fn finalize(noalias self: *@This()) !void {
316 assert(!self.finalized);
317 // use a temp to avoid unintentional aliasing due to RLS
318 const tmp = try self.scope.captures.clone(self.perm_arena);
319 self.scope.captures.deinit(self.gpa);
320 self.scope.captures = tmp;
333 pub fn finalize(noalias self: *WipCaptureScope) !void {
321334 self.finalized = true;
322335 }
323336
324 pub fn reset(noalias self: *@This(), parent: ?*CaptureScope) !void {
325 if (!self.finalized) try self.finalize();
326 self.scope = try self.perm_arena.create(CaptureScope);
327 self.scope.* = .{ .parent = parent };
328 self.finalized = false;
337 pub fn reset(noalias self: *WipCaptureScope, parent: ?*CaptureScope) !void {
338 self.scope.decRef(self.gpa);
339 self.scope = try self.gpa.create(CaptureScope);
340 if (parent) |p| p.incRef();
341 self.scope.* = .{ .refs = 1, .parent = parent };
329342 }
330343
331 pub fn deinit(noalias self: *@This()) void {
332 if (!self.finalized) {
333 self.scope.captures.deinit(self.gpa);
334 self.scope.fail();
344 pub fn deinit(noalias self: *WipCaptureScope) void {
345 if (self.finalized) {
346 self.scope.decRef(self.gpa);
347 } else {
348 self.scope.fail(self.gpa);
335349 }
336350 self.* = undefined;
337351 }
......@@ -3311,6 +3325,7 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
33113325 mod.destroyNamespace(i);
33123326 }
33133327 }
3328 if (decl.src_scope) |scope| scope.decRef(gpa);
33143329 decl.clearValues(mod);
33153330 decl.dependants.deinit(gpa);
33163331 decl.dependencies.deinit(gpa);
......@@ -4425,7 +4440,7 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
44254440 };
44264441 defer sema.deinit();
44274442
4428 var wip_captures = try WipCaptureScope.init(gpa, new_decl_arena_allocator, null);
4443 var wip_captures = try WipCaptureScope.init(gpa, null);
44294444 defer wip_captures.deinit();
44304445
44314446 if (sema.analyzeStructDecl(new_decl, main_struct_inst, struct_index)) |_| {
......@@ -4538,7 +4553,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
45384553 }
45394554 log.debug("semaDecl {*} ({s})", .{ decl, decl.name });
45404555
4541 var wip_captures = try WipCaptureScope.init(gpa, decl_arena_allocator, decl.src_scope);
4556 var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope);
45424557 defer wip_captures.deinit();
45434558
45444559 var block_scope: Sema.Block = .{
......@@ -5499,7 +5514,7 @@ pub fn analyzeFnBody(mod: *Module, func_index: Fn.Index, arena: Allocator) SemaE
54995514 try sema.air_extra.ensureTotalCapacity(gpa, reserved_count);
55005515 sema.air_extra.items.len += reserved_count;
55015516
5502 var wip_captures = try WipCaptureScope.init(gpa, decl_arena_allocator, decl.src_scope);
5517 var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope);
55035518 defer wip_captures.deinit();
55045519
55055520 var inner_block: Sema.Block = .{
......@@ -5771,6 +5786,7 @@ pub fn allocateNewDecl(
57715786 };
57725787 };
57735788
5789 if (src_scope) |scope| scope.incRef();
57745790 decl_and_index.new_decl.* = .{
57755791 .name = undefined,
57765792 .src_namespace = namespace,
src/Sema.zig+55-36
......@@ -889,17 +889,18 @@ fn analyzeBodyInner(
889889
890890 try sema.inst_map.ensureSpaceForInstructions(sema.gpa, body);
891891
892 // Most of the time, we don't need to construct a new capture scope for a
893 // block. However, successive iterations of comptime loops can capture
894 // different values for the same Zir.Inst.Index, so in those cases, we will
895 // have to create nested capture scopes; see the `.repeat` case below.
892896 const parent_capture_scope = block.wip_capture_scope;
893
894 var wip_captures = WipCaptureScope{
895 .finalized = true,
897 parent_capture_scope.incRef();
898 var wip_captures: WipCaptureScope = .{
896899 .scope = parent_capture_scope,
897 .perm_arena = sema.perm_arena,
898900 .gpa = sema.gpa,
901 .finalized = true, // don't finalize the parent scope
899902 };
900 defer if (wip_captures.scope != parent_capture_scope) {
901 wip_captures.deinit();
902 };
903 defer wip_captures.deinit();
903904
904905 const mod = sema.mod;
905906 const map = &sema.inst_map;
......@@ -1452,6 +1453,11 @@ fn analyzeBodyInner(
14521453 const src = LazySrcLoc.nodeOffset(datas[inst].node);
14531454 try sema.emitBackwardBranch(block, src);
14541455 if (wip_captures.scope.captures.count() != orig_captures) {
1456 // We need to construct new capture scopes for the next loop iteration so it
1457 // can capture values without clobbering the earlier iteration's captures.
1458 // At first, we reused the parent capture scope as an optimization, but for
1459 // successive scopes we have to create new ones as children of the parent
1460 // scope.
14551461 try wip_captures.reset(parent_capture_scope);
14561462 block.wip_capture_scope = wip_captures.scope;
14571463 orig_captures = 0;
......@@ -1467,6 +1473,11 @@ fn analyzeBodyInner(
14671473 const src = LazySrcLoc.nodeOffset(datas[inst].node);
14681474 try sema.emitBackwardBranch(block, src);
14691475 if (wip_captures.scope.captures.count() != orig_captures) {
1476 // We need to construct new capture scopes for the next loop iteration so it
1477 // can capture values without clobbering the earlier iteration's captures.
1478 // At first, we reused the parent capture scope as an optimization, but for
1479 // successive scopes we have to create new ones as children of the parent
1480 // scope.
14701481 try wip_captures.reset(parent_capture_scope);
14711482 block.wip_capture_scope = wip_captures.scope;
14721483 orig_captures = 0;
......@@ -1749,6 +1760,8 @@ fn analyzeBodyInner(
17491760 if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some);
17501761
17511762 if (!wip_captures.finalized) {
1763 // We've updated the capture scope due to a `repeat` instruction where
1764 // the body had a capture; finalize our child scope and reset
17521765 try wip_captures.finalize();
17531766 block.wip_capture_scope = parent_capture_scope;
17541767 }
......@@ -3007,7 +3020,7 @@ fn zirEnumDecl(
30073020 defer sema.func = prev_func;
30083021 defer sema.func_index = prev_func_index;
30093022
3010 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
3023 var wip_captures = try WipCaptureScope.init(gpa, new_decl.src_scope);
30113024 defer wip_captures.deinit();
30123025
30133026 var enum_block: Block = .{
......@@ -6888,7 +6901,7 @@ fn analyzeCall(
68886901 sema.error_return_trace_index_on_fn_entry = block.error_return_trace_index;
68896902 defer sema.error_return_trace_index_on_fn_entry = parent_err_ret_index;
68906903
6891 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, fn_owner_decl.src_scope);
6904 var wip_captures = try WipCaptureScope.init(gpa, fn_owner_decl.src_scope);
68926905 defer wip_captures.deinit();
68936906
68946907 var child_block: Block = .{
......@@ -7751,7 +7764,7 @@ fn resolveGenericInstantiationType(
77517764 };
77527765 defer child_sema.deinit();
77537766
7754 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
7767 var wip_captures = try WipCaptureScope.init(gpa, new_decl.src_scope);
77557768 defer wip_captures.deinit();
77567769
77577770 var child_block: Block = .{
......@@ -11151,7 +11164,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1115111164 const body = sema.code.extra[extra_index..][0..body_len];
1115211165 extra_index += body_len;
1115311166
11154 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope);
11167 var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope);
1115511168 defer wip_captures.deinit();
1115611169
1115711170 case_block.instructions.shrinkRetainingCapacity(0);
......@@ -11396,7 +11409,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1139611409 var cond_body = try case_block.instructions.toOwnedSlice(gpa);
1139711410 defer gpa.free(cond_body);
1139811411
11399 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope);
11412 var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope);
1140011413 defer wip_captures.deinit();
1140111414
1140211415 case_block.instructions.shrinkRetainingCapacity(0);
......@@ -11577,7 +11590,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1157711590 }),
1157811591 };
1157911592
11580 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope);
11593 var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope);
1158111594 defer wip_captures.deinit();
1158211595
1158311596 case_block.instructions.shrinkRetainingCapacity(0);
......@@ -15720,13 +15733,15 @@ fn zirClosureCapture(
1572015733 // ...in which case the closure_capture instruction has access to a runtime
1572115734 // value only. In such case we preserve the type and use a dummy runtime value.
1572215735 const operand = try sema.resolveInst(inst_data.operand);
15723 const val = (try sema.resolveMaybeUndefValAllowVariables(operand)) orelse
15724 Value.@"unreachable";
15725
15726 try block.wip_capture_scope.captures.putNoClobber(sema.gpa, inst, .{
15727 .ty = sema.typeOf(operand),
15728 .val = try val.copy(sema.perm_arena),
15729 });
15736 const ty = sema.typeOf(operand);
15737 const capture: CaptureScope.Capture = blk: {
15738 if (try sema.resolveMaybeUndefValAllowVariables(operand)) |val| {
15739 const ip_index = try val.intern(ty, sema.mod);
15740 break :blk .{ .comptime_val = ip_index };
15741 }
15742 break :blk .{ .runtime_val = ty.toIntern() };
15743 };
15744 try block.wip_capture_scope.captures.putNoClobber(sema.gpa, inst, capture);
1573015745}
1573115746
1573215747fn zirClosureGet(
......@@ -15740,7 +15755,7 @@ fn zirClosureGet(
1574015755 var scope: *CaptureScope = mod.declPtr(block.src_decl).src_scope.?;
1574115756 // Note: The target closure must be in this scope list.
1574215757 // If it's not here, the zir is invalid, or the list is broken.
15743 const tv = while (true) {
15758 const capture = while (true) {
1574415759 // Note: We don't need to add a dependency here, because
1574515760 // decls always depend on their lexical parents.
1574615761
......@@ -15753,13 +15768,13 @@ fn zirClosureGet(
1575315768 }
1575415769 return error.AnalysisFail;
1575515770 }
15756 if (scope.captures.getPtr(inst_data.inst)) |tv| {
15757 break tv;
15771 if (scope.captures.get(inst_data.inst)) |capture| {
15772 break capture;
1575815773 }
1575915774 scope = scope.parent.?;
1576015775 };
1576115776
15762 if (tv.val.toIntern() == .unreachable_value and !block.is_typeof and sema.func_index == .none) {
15777 if (capture == .runtime_val and !block.is_typeof and sema.func_index == .none) {
1576315778 const msg = msg: {
1576415779 const name = name: {
1576515780 const file = sema.owner_decl.getFileScope(mod);
......@@ -15787,7 +15802,7 @@ fn zirClosureGet(
1578715802 return sema.failWithOwnedErrorMsg(msg);
1578815803 }
1578915804
15790 if (tv.val.toIntern() == .unreachable_value and !block.is_typeof and !block.is_comptime and sema.func_index != .none) {
15805 if (capture == .runtime_val and !block.is_typeof and !block.is_comptime and sema.func_index != .none) {
1579115806 const msg = msg: {
1579215807 const name = name: {
1579315808 const file = sema.owner_decl.getFileScope(mod);
......@@ -15817,13 +15832,17 @@ fn zirClosureGet(
1581715832 return sema.failWithOwnedErrorMsg(msg);
1581815833 }
1581915834
15820 if (tv.val.toIntern() == .unreachable_value) {
15821 assert(block.is_typeof);
15822 // We need a dummy runtime instruction with the correct type.
15823 return block.addTy(.alloc, tv.ty);
15835 switch (capture) {
15836 .runtime_val => |ty_ip_index| {
15837 assert(block.is_typeof);
15838 // We need a dummy runtime instruction with the correct type.
15839 return block.addTy(.alloc, ty_ip_index.toType());
15840 },
15841 .comptime_val => |val_ip_index| {
15842 const ty = mod.intern_pool.typeOf(val_ip_index).toType();
15843 return sema.addConstant(ty, val_ip_index.toValue());
15844 },
1582415845 }
15825
15826 return sema.addConstant(tv.ty, tv.val);
1582715846}
1582815847
1582915848fn zirRetAddr(
......@@ -31838,7 +31857,7 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi
3183831857 var sema: Sema = .{ .mod = mod, .gpa = gpa, .arena = analysis_arena.allocator(), .perm_arena = decl_arena_allocator, .code = zir, .owner_decl = decl, .owner_decl_index = decl_index, .func = null, .func_index = .none, .fn_ret_ty = Type.void, .owner_func = null, .owner_func_index = .none };
3183931858 defer sema.deinit();
3184031859
31841 var wip_captures = try WipCaptureScope.init(gpa, decl_arena_allocator, decl.src_scope);
31860 var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope);
3184231861 defer wip_captures.deinit();
3184331862
3184431863 var block: Block = .{
......@@ -32591,7 +32610,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void
3259132610 };
3259232611 defer sema.deinit();
3259332612
32594 var wip_captures = try WipCaptureScope.init(gpa, decl_arena_allocator, decl.src_scope);
32613 var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope);
3259532614 defer wip_captures.deinit();
3259632615
3259732616 var block_scope: Block = .{
......@@ -32933,7 +32952,7 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
3293332952 };
3293432953 defer sema.deinit();
3293532954
32936 var wip_captures = try WipCaptureScope.init(gpa, decl_arena_allocator, decl.src_scope);
32955 var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope);
3293732956 defer wip_captures.deinit();
3293832957
3293932958 var block_scope: Block = .{
......@@ -33360,7 +33379,7 @@ fn generateUnionTagTypeSimple(
3336033379}
3336133380
3336233381fn getBuiltin(sema: *Sema, name: []const u8) CompileError!Air.Inst.Ref {
33363 var wip_captures = try WipCaptureScope.init(sema.gpa, sema.perm_arena, sema.owner_decl.src_scope);
33382 var wip_captures = try WipCaptureScope.init(sema.gpa, sema.owner_decl.src_scope);
3336433383 defer wip_captures.deinit();
3336533384
3336633385 var block: Block = .{
......@@ -33405,7 +33424,7 @@ fn getBuiltin(sema: *Sema, name: []const u8) CompileError!Air.Inst.Ref {
3340533424fn getBuiltinType(sema: *Sema, name: []const u8) CompileError!Type {
3340633425 const ty_inst = try sema.getBuiltin(name);
3340733426
33408 var wip_captures = try WipCaptureScope.init(sema.gpa, sema.perm_arena, sema.owner_decl.src_scope);
33427 var wip_captures = try WipCaptureScope.init(sema.gpa, sema.owner_decl.src_scope);
3340933428 defer wip_captures.deinit();
3341033429
3341133430 var block: Block = .{