Phase 2: heap, regions and allocation budgets (RFC 0008)
- air: own<T>, own_array<T>, region, alloc_error; own.new/array/take/ free, region.create/new/array/reset/destroy; mem.alloc budget form; linear contents of owners gated by linear_aggregates - verifier: allocation types and capability kind (E1001, E3201), mem.alloc effect (E3101), ok/err counts; borrow-typed block params checked in the CFG stage with the region-allocation exception; linear owners and regions (E3002, E3001, E2001, E2002, E2101); region allocations as disjoint exclusive children of the shared region borrow; E2104 when reset/destroy would invalidate a live reference - interp: live-byte budgets charged along the narrowing chain, shared by copies, mandatory in the deterministic profile; heap cells; regions return quota only on reset/destroy - conformance: 25 heap/region tests including a 64 KiB region file copy; 4000 mutants without a crash
This commit is contained in:
1 parent
b8d4ab4d4c
commit
7f4ce71d44
38 files changed
+1341
-48
No files matched your search
+62
-17
@@ -22,6 +22,9 @@ const Seg = union(enum) {
|
||||
split: struct { key: ValueId, side: u1 },
|
||||
/// An element or sub-range; overlaps every sibling.
|
||||
elem,
|
||||
/// A region allocation, keyed by its `ok` parameter; disjoint from
|
||||
/// every other allocation (spec 03 §20.2).
|
||||
alloc: ValueId,
|
||||
};
|
||||
|
||||
const Mode = enum { shared, exclusive };
|
||||
@@ -110,6 +113,10 @@ const Walker = struct {
|
||||
/// `through` (a borrow whose ancestors do not conflict) or directly on a
|
||||
/// local (`through == null`). Reports the first conflict (E2101).
|
||||
fn access(w: *Walker, root: ValueId, path: []const Seg, mode: Mode, via: ?ValueId, who: ValueId) !void {
|
||||
return w.accessCode(root, path, mode, via, who, "E2101", "conflicting borrow or access");
|
||||
}
|
||||
|
||||
fn accessCode(w: *Walker, root: ValueId, path: []const Seg, mode: Mode, via: ?ValueId, who: ValueId, code: []const u8, message: []const u8) !void {
|
||||
for (w.live, 0..) |is_live, cv| {
|
||||
if (!is_live) continue;
|
||||
const ci = w.info[cv] orelse continue; // live resources are not borrows
|
||||
@@ -117,7 +124,7 @@ const Walker = struct {
|
||||
if (via) |t| if (w.isAncestor(@intCast(cv), t)) continue;
|
||||
if (mode == .shared and ci.mode == .shared) continue;
|
||||
if (!overlap(path, ci.path)) continue;
|
||||
try w.report("E2101", "conflicting borrow or access", who, .{ "conflicts_with", @intCast(cv) });
|
||||
try w.report(code, message, who, .{ "conflicts_with", @intCast(cv) });
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -163,17 +170,18 @@ const Walker = struct {
|
||||
return w.types[v] == .local;
|
||||
}
|
||||
|
||||
/// Linear values: resources, owners and regions (spec 06 §6, spec 03 §19–§20).
|
||||
fn isResource(w: *Walker, v: ValueId) bool {
|
||||
return w.types[v] == .resource;
|
||||
return w.types[v].isLinear();
|
||||
}
|
||||
|
||||
/// A non-consuming use of resource `v` (a borrow of it).
|
||||
fn resourceUsable(w: *Walker, v: ValueId) !bool {
|
||||
if (w.live[v]) return true;
|
||||
switch (w.dead[v]) {
|
||||
.closed => try w.report("E3001", "resource used after it was closed", v, null),
|
||||
.moved => try w.report("E2001", "resource used after its ownership was transferred", v, null),
|
||||
.none => try w.report("E3001", "resource used before it is owned", v, null),
|
||||
.closed => try w.report("E3001", "linear value used after it was closed, freed or destroyed", v, null),
|
||||
.moved => try w.report("E2001", "linear value used after its ownership was transferred", v, null),
|
||||
.none => try w.report("E3001", "linear value used before it is owned", v, null),
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -181,7 +189,7 @@ const Walker = struct {
|
||||
/// Consumes resource `v` by closing or by transfer (spec 06 §6.2).
|
||||
fn consume(w: *Walker, v: ValueId, how: Dead) !void {
|
||||
if (!w.live[v]) {
|
||||
return w.report("E2002", "resource consumed twice", v, null);
|
||||
return w.report("E2002", "linear value consumed twice", v, null);
|
||||
}
|
||||
// Consumption is an exclusive access to the whole resource.
|
||||
try w.access(v, &.{}, .exclusive, null, v);
|
||||
@@ -201,6 +209,10 @@ pub fn overlap(a: []const Seg, b: []const Seg) bool {
|
||||
if (sx.side != y.split.side) return false;
|
||||
continue;
|
||||
},
|
||||
.alloc => |ka| if (y == .alloc) {
|
||||
if (ka != y.alloc) return false;
|
||||
continue;
|
||||
},
|
||||
.elem => {},
|
||||
}
|
||||
return true;
|
||||
@@ -233,6 +245,11 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
|
||||
try out.append(arena, .{ .target = ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = x.err.?, .args = &.{} });
|
||||
},
|
||||
.own_new, .own_array, .region_create, .region_new, .region_array => {
|
||||
const t = air.allocTargets(op).?;
|
||||
try out.append(arena, .{ .target = t.ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = t.err, .args = &.{} });
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -257,8 +274,8 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
.path = &.{},
|
||||
.parent = null,
|
||||
};
|
||||
// Resource parameters were moved in: the function owns them (spec 06 §6.3).
|
||||
if (p.ty == .resource) w.live[p.value] = true;
|
||||
// Linear parameters were moved in: the function owns them (spec 06 §6.3).
|
||||
if (p.ty.isLinear()) w.live[p.value] = true;
|
||||
}
|
||||
|
||||
const dom = try ssa.dominators(c.arena, f);
|
||||
@@ -272,8 +289,9 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
for (dom.rpo) |b| {
|
||||
@memcpy(w.live, entry_state[b].?);
|
||||
const blk = f.blocks[b];
|
||||
// Resource block parameters own their argument from here on.
|
||||
for (blk.params) |p| if (p.ty == .resource) {
|
||||
// Linear block parameters own their argument from here on; the
|
||||
// parameter of a region allocation's `ok` target is a live borrow.
|
||||
for (blk.params) |p| if (p.ty.isLinear() or (p.ty.isBorrow() and w.info[p.value] != null)) {
|
||||
w.live[p.value] = true;
|
||||
w.dead[p.value] = .none;
|
||||
};
|
||||
@@ -292,18 +310,19 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
for (out.items) |e| {
|
||||
@memcpy(w.live, base);
|
||||
@memcpy(w.dead, base_dead);
|
||||
for (e.args) |a| if (types[a] == .resource) try w.consume(a, .moved);
|
||||
// A resource parameter of the target that is still live on this
|
||||
// edge would be overwritten: that instance leaks.
|
||||
for (f.blocks[e.target].params) |p| if (p.ty == .resource) {
|
||||
if (w.live[p.value]) try w.report("E3002", "resource not consumed before it is redefined", p.value, null);
|
||||
for (e.args) |a| if (types[a].isLinear()) try w.consume(a, .moved);
|
||||
// A linear parameter of the target that is still live on this
|
||||
// edge would be overwritten: that instance leaks. Parameters are
|
||||
// not part of the incoming state; they come alive in the block.
|
||||
for (f.blocks[e.target].params) |p| {
|
||||
if (p.ty.isLinear() and w.live[p.value]) try w.report("E3002", "linear value not consumed before it is redefined", p.value, null);
|
||||
w.live[p.value] = false;
|
||||
};
|
||||
}
|
||||
if (entry_state[e.target]) |expected| {
|
||||
if (!std.mem.eql(bool, expected, w.live) and !reported_join[e.target]) {
|
||||
reported_join[e.target] = true;
|
||||
for (expected, w.live, 0..) |a, x, v| if (a != x) {
|
||||
const is_res = types[v] == .resource;
|
||||
const is_res = types[v].isLinear();
|
||||
try c.report(if (is_res) "E3002" else "E2105", f.blocks[e.target].span, if (is_res) "resource state differs between edges into this block" else "borrow state differs between edges into this block", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "block", .value = .{ .str = f.blocks[e.target].name } },
|
||||
@@ -377,6 +396,32 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
w.live[r] = true;
|
||||
};
|
||||
},
|
||||
.own_free, .own_take => |v| try w.consume(v, .closed),
|
||||
.region_reset => |v| {
|
||||
if (!try w.resourceUsable(v)) return;
|
||||
// Reset invalidates the generation: no reference may be live.
|
||||
try w.accessCode(v, &.{}, .exclusive, null, v, "E2104", "region reset would invalidate a live reference into the region");
|
||||
},
|
||||
.region_destroy => |v| {
|
||||
if (w.live[v]) try w.accessCode(v, &.{}, .exclusive, null, v, "E2104", "region destroy would invalidate a live reference into the region");
|
||||
if (!w.live[v]) return w.report("E2002", "linear value consumed twice", v, null);
|
||||
w.live[v] = false;
|
||||
w.dead[v] = .closed;
|
||||
},
|
||||
.region_new, .region_array => {
|
||||
const rb = switch (inst.op) {
|
||||
.region_new => |x| x.region,
|
||||
.region_array => |x| x.region,
|
||||
else => unreachable,
|
||||
};
|
||||
if (!try w.usable(rb)) return;
|
||||
// The allocation is a fresh exclusive child of the shared region
|
||||
// borrow; it comes alive on the `ok` edge (spec 03 §20.2).
|
||||
const ok = air.allocTargets(inst.op).?.ok;
|
||||
const p = w.f.blocks[ok].params[0].value;
|
||||
const ri = w.info[rb].?;
|
||||
w.info[p] = .{ .owned = true, .mode = .exclusive, .root = ri.root, .path = try w.extend(ri.path, .{ .alloc = p }), .parent = rb };
|
||||
},
|
||||
.intrinsic => |x| {
|
||||
try lendArgs(w, x.args);
|
||||
// Closing intrinsics consume on both edges, so before propagation.
|
||||
|
||||
Reference in new issue
Block a user