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
@@ -49,7 +49,7 @@ pub const kinds = [_]Kind{
|
||||
.{ .name = "fs.write", .scope = .path_prefix, .forms = &.{"prefix"} },
|
||||
.{ .name = "io.read", .scope = .none, .forms = &.{} },
|
||||
.{ .name = "io.write", .scope = .none, .forms = &.{} },
|
||||
.{ .name = "mem.alloc", .scope = .byte_budget, .forms = &.{} },
|
||||
.{ .name = "mem.alloc", .scope = .byte_budget, .forms = &.{"budget"} },
|
||||
.{ .name = "mem.map", .scope = .none, .forms = &.{} },
|
||||
.{ .name = "net.connect", .scope = .none, .forms = &.{} },
|
||||
.{ .name = "net.listen", .scope = .none, .forms = &.{} },
|
||||
@@ -62,11 +62,13 @@ pub const kinds = [_]Kind{
|
||||
|
||||
/// Built-in enums: spelled without `@`, fixed variants, no payloads.
|
||||
pub const Builtin = enum {
|
||||
alloc_error,
|
||||
cap_error,
|
||||
io_error,
|
||||
|
||||
pub fn variants(b: Builtin) []const []const u8 {
|
||||
return switch (b) {
|
||||
.alloc_error => &.{ "budget_exceeded", "out_of_memory" },
|
||||
.cap_error => &.{ "invalid_restriction", "not_subset", "unsupported_restriction" },
|
||||
.io_error => &.{ "not_found", "permission_denied", "already_exists", "invalid_input", "no_space", "broken_pipe", "connection_refused", "connection_reset", "address_in_use", "address_unavailable", "timed_out", "other" },
|
||||
};
|
||||
@@ -75,6 +77,7 @@ pub const Builtin = enum {
|
||||
/// Feature a built-in enum belongs to.
|
||||
pub fn feature(b: Builtin) []const u8 {
|
||||
return switch (b) {
|
||||
.alloc_error => "heap",
|
||||
.cap_error => "capabilities",
|
||||
.io_error => "resources",
|
||||
};
|
||||
|
||||
@@ -28,11 +28,11 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "capabilities", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "channels", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
||||
.{ .name = "effects", .depends = &.{}, .status = .published, .phase = 2 },
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .reserved, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "ownership", "regions" }, .status = .reserved, .phase = 4 },
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "regions", "resources" }, .status = .reserved, .phase = 4 },
|
||||
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
||||
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .reserved, .phase = 2 },
|
||||
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "resource_joins", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "resources", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "sync", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
||||
@@ -40,7 +40,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arrays", "capabilities", "effects", "ownership", "resources" };
|
||||
pub const supported = [_][]const u8{ "arrays", "capabilities", "effects", "heap", "ownership", "regions", "resources" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+60
-3
@@ -79,6 +79,10 @@ pub const Type = union(enum) {
|
||||
builtin: authority.Builtin,
|
||||
// Feature `resources` (spec 06 §6).
|
||||
resource: authority.Resource,
|
||||
// Features `heap` and `regions` (spec 03 §18–§20).
|
||||
own: *const Type,
|
||||
own_array: *const Type,
|
||||
region,
|
||||
|
||||
pub const Array = struct { elem: Type, len: u64 };
|
||||
|
||||
@@ -87,7 +91,8 @@ pub const Type = union(enum) {
|
||||
return switch (a) {
|
||||
.int => |x| b.int == x,
|
||||
.named => |x| b.named == x,
|
||||
.local, .ref, .mut, .slice, .slice_mut => |x| x.eql(b.inner().?),
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array => |x| x.eql(b.inner().?),
|
||||
.region => true,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.cap => |k| b.cap == k,
|
||||
.builtin => |e| b.builtin == e,
|
||||
@@ -99,7 +104,7 @@ pub const Type = union(enum) {
|
||||
/// Element or pointee type of storage, borrow and slice types.
|
||||
pub fn inner(t: Type) ?Type {
|
||||
return switch (t) {
|
||||
.local, .ref, .mut, .slice, .slice_mut => |x| x.*,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array => |x| x.*,
|
||||
.array => |a| a.elem,
|
||||
else => null,
|
||||
};
|
||||
@@ -113,6 +118,14 @@ pub const Type = union(enum) {
|
||||
};
|
||||
}
|
||||
|
||||
/// Linear values: consumed exactly once (spec 06 §6.2–§6.3).
|
||||
pub fn isLinear(t: Type) bool {
|
||||
return switch (t) {
|
||||
.resource, .own, .own_array, .region => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isMutBorrow(t: Type) bool {
|
||||
return t == .mut or t == .slice_mut;
|
||||
}
|
||||
@@ -126,7 +139,7 @@ pub const Type = union(enum) {
|
||||
/// Linear resources are neither values nor borrows: they are moved.
|
||||
pub fn isValue(t: Type) bool {
|
||||
return switch (t) {
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource => false,
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource, .own, .own_array, .region => false,
|
||||
else => true,
|
||||
};
|
||||
}
|
||||
@@ -240,6 +253,17 @@ pub const Op = union(enum) {
|
||||
/// Intrinsic call (spec 05 §11). With `ok`/`err` it is the fallible
|
||||
/// terminator form; without, the infallible form.
|
||||
intrinsic: struct { index: u16, args: []const ValueId, ok: ?u32 = null, err: ?u32 = null },
|
||||
// Features `heap` and `regions` (spec 03 §19–§20). Allocations are
|
||||
// fallible terminators delivering to `ok`/`err` block parameters.
|
||||
own_new: struct { value: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
own_array: struct { n: ValueId, value: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
own_take: ValueId,
|
||||
own_free: ValueId,
|
||||
region_create: struct { cap: ValueId, ok: u32, err: u32 },
|
||||
region_new: struct { region: ValueId, value: ValueId, ok: u32, err: u32 },
|
||||
region_array: struct { region: ValueId, n: ValueId, value: ValueId, ok: u32, err: u32 },
|
||||
region_reset: ValueId,
|
||||
region_destroy: ValueId,
|
||||
ret: ?ValueId,
|
||||
trap,
|
||||
|
||||
@@ -249,6 +273,7 @@ pub const Op = union(enum) {
|
||||
pub fn isTerminator(op: Op) bool {
|
||||
return switch (op) {
|
||||
.br, .cbr, .@"switch", .enum_switch, .cap_narrow, .ret, .trap => true,
|
||||
.own_new, .own_array, .region_create, .region_new, .region_array => true,
|
||||
.intrinsic => |x| x.ok != null,
|
||||
else => false,
|
||||
};
|
||||
@@ -376,6 +401,26 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
for (x.args) |v| f(ctx, v);
|
||||
},
|
||||
.intrinsic => |x| for (x.args) |v| f(ctx, v),
|
||||
.own_new => |x| {
|
||||
f(ctx, x.value);
|
||||
f(ctx, x.cap);
|
||||
},
|
||||
.own_array => |x| {
|
||||
f(ctx, x.n);
|
||||
f(ctx, x.value);
|
||||
f(ctx, x.cap);
|
||||
},
|
||||
.own_take, .own_free, .region_reset, .region_destroy => |v| f(ctx, v),
|
||||
.region_create => |x| f(ctx, x.cap),
|
||||
.region_new => |x| {
|
||||
f(ctx, x.region);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.region_array => |x| {
|
||||
f(ctx, x.region);
|
||||
f(ctx, x.n);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.ret => |v| if (v) |x| f(ctx, x),
|
||||
.local_alloc => {},
|
||||
.local_store => |x| {
|
||||
@@ -410,6 +455,14 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
}
|
||||
|
||||
/// Successor block indices of a terminator, in source order (may repeat).
|
||||
/// `ok`/`err` targets of a fallible allocation terminator.
|
||||
pub fn allocTargets(op: Op) ?struct { ok: u32, err: u32 } {
|
||||
return switch (op) {
|
||||
inline .own_new, .own_array, .region_create, .region_new, .region_array => |x| .{ .ok = x.ok, .err = x.err },
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn successors(op: Op, buf: *std.ArrayList(u32), gpa: std.mem.Allocator) !void {
|
||||
switch (op) {
|
||||
.br => |t| try buf.append(gpa, t.block),
|
||||
@@ -433,6 +486,10 @@ pub fn successors(op: Op, buf: *std.ArrayList(u32), gpa: std.mem.Allocator) !voi
|
||||
try buf.append(gpa, ok);
|
||||
try buf.append(gpa, x.err.?);
|
||||
},
|
||||
inline .own_new, .own_array, .region_create, .region_new, .region_array => |x| {
|
||||
try buf.append(gpa, x.ok);
|
||||
try buf.append(gpa, x.err);
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
+75
-6
@@ -485,6 +485,10 @@ const Parser = struct {
|
||||
try p.requireFeature(b.feature(), t.span, t.text);
|
||||
return .{ .builtin = b };
|
||||
}
|
||||
if (eql(t.text, "region")) {
|
||||
try p.requireFeature("regions", t.span, t.text);
|
||||
return .region;
|
||||
}
|
||||
if (eql(t.text, "listener") or eql(t.text, "stream")) {
|
||||
try p.requireFeature("resources", t.span, t.text);
|
||||
return .{ .resource = if (eql(t.text, "listener")) .listener else .stream };
|
||||
@@ -517,6 +521,18 @@ const Parser = struct {
|
||||
.append => .file_append,
|
||||
} };
|
||||
}
|
||||
if (eql(t.text, "own") or eql(t.text, "own_array")) {
|
||||
try p.requireFeature("heap", t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Nested linear values need `linear_aggregates` (spec 03 §19.2).
|
||||
try p.gateLinearMember(inner, inner_span);
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return if (eql(t.text, "own")) .{ .own = boxed } else .{ .own_array = boxed };
|
||||
}
|
||||
if (eql(t.text, "cap")) {
|
||||
try p.requireFeature("capabilities", t.span, "cap");
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
@@ -540,8 +556,10 @@ const Parser = struct {
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Storage and arrays of linear values need `linear_aggregates`; a
|
||||
// borrow of a linear value (`mut<file<read>>`) is fine.
|
||||
if (kind != .ref and kind != .mut) try p.gateLinearMember(inner, inner_span);
|
||||
if (kind == .array) {
|
||||
try p.gateLinearMember(inner, inner_span);
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const n_tok = try p.expect(.number, "array length");
|
||||
const n = std.fmt.parseInt(u64, n_tok.text, 0) catch 0;
|
||||
@@ -568,7 +586,7 @@ const Parser = struct {
|
||||
|
||||
/// Resources inside aggregates need the reserved feature `linear_aggregates`.
|
||||
fn gateLinearMember(p: *Parser, t: air.Type, span: Span) Error!void {
|
||||
if (t == .resource) try p.requireFeature("linear_aggregates", span, "resource as a member");
|
||||
if (t.isLinear()) try p.requireFeature("linear_aggregates", span, "linear value as a member");
|
||||
}
|
||||
|
||||
fn declares(p: *Parser, feature: []const u8) bool {
|
||||
@@ -724,11 +742,7 @@ const Parser = struct {
|
||||
while (p.peek().tag != .r_paren) {
|
||||
const v = try p.expect(.local, "block parameter");
|
||||
_ = try p.expect(.colon, "`:`");
|
||||
const ty_span = p.peek().span;
|
||||
const ty = try p.parseType();
|
||||
// Borrow-typed block parameters belong to the reserved feature
|
||||
// `borrow_joins` (spec 08 §11.6).
|
||||
if (ty.isBorrow()) try p.requireFeature("borrow_joins", ty_span, "borrow-typed block parameter");
|
||||
const id = try p.defineValue(v, .{ .block_param = .{ .block = index, .index = @intCast(params.items.len) } });
|
||||
try params.append(p.arena, .{ .value = id, .ty = ty });
|
||||
if (p.peek().tag != .comma) break;
|
||||
@@ -915,6 +929,7 @@ const Parser = struct {
|
||||
const err = try p.blockRef();
|
||||
break :op .{ .cap_narrow = .{ .form = parts[2], .cap = c, .args = args.items, .ok = ok, .err = err } };
|
||||
}
|
||||
if (try p.parseAllocOp(op_tok)) |aop| break :op aop;
|
||||
if (eql(op_tok.text, "intrinsic")) {
|
||||
try p.requireFeature("capabilities", op_tok.span, "intrinsic");
|
||||
const name = try p.expect(.word, "intrinsic name");
|
||||
@@ -941,6 +956,7 @@ const Parser = struct {
|
||||
_ = try p.expect(.nl, "end of line");
|
||||
|
||||
const produces_value = switch (op) {
|
||||
.own_take => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
@@ -961,6 +977,59 @@ const Parser = struct {
|
||||
return .{ .result = result, .result2 = result2, .op = op, .span = if (result_tok) |rt| rt.span else op_tok.span };
|
||||
}
|
||||
|
||||
fn okErr(p: *Parser) Error!struct { u32, u32 } {
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
_ = try p.expectWord("ok");
|
||||
_ = try p.expect(.arrow, "`->`");
|
||||
const ok = try p.blockRef();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
_ = try p.expectWord("err");
|
||||
_ = try p.expect(.arrow, "`->`");
|
||||
return .{ ok, try p.blockRef() };
|
||||
}
|
||||
|
||||
/// Instructions of the features `heap` and `regions` (spec 03 §19–§20).
|
||||
fn parseAllocOp(p: *Parser, op_tok: Token) Error!?air.Op {
|
||||
const Name = enum { @"own.new", @"own.array", @"own.take", @"own.free", @"region.create", @"region.new", @"region.array", @"region.reset", @"region.destroy" };
|
||||
const name = std.meta.stringToEnum(Name, op_tok.text) orelse return null;
|
||||
try p.requireFeature(if (std.mem.startsWith(u8, op_tok.text, "own.")) "heap" else "regions", op_tok.span, op_tok.text);
|
||||
switch (name) {
|
||||
.@"own.new" => {
|
||||
const v, const c = try p.twoOperands();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .own_new = .{ .value = v, .cap = c, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"own.array" => {
|
||||
const n, const v = try p.twoOperands();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const c = try p.useValue();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .own_array = .{ .n = n, .value = v, .cap = c, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"own.take" => return .{ .own_take = try p.useValue() },
|
||||
.@"own.free" => return .{ .own_free = try p.useValue() },
|
||||
.@"region.create" => {
|
||||
const c = try p.useValue();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .region_create = .{ .cap = c, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"region.new" => {
|
||||
const r, const v = try p.twoOperands();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .region_new = .{ .region = r, .value = v, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"region.array" => {
|
||||
const r, const n = try p.twoOperands();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const v = try p.useValue();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .region_array = .{ .region = r, .n = n, .value = v, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"region.reset" => return .{ .region_reset = try p.useValue() },
|
||||
.@"region.destroy" => return .{ .region_destroy = try p.useValue() },
|
||||
}
|
||||
}
|
||||
|
||||
/// Instructions of the features `ownership` and `arrays` (spec 07a).
|
||||
fn parseMemoryOp(p: *Parser, op_tok: Token) Error!?air.Op {
|
||||
const Name = enum {
|
||||
|
||||
@@ -105,6 +105,12 @@ pub fn printType(m: air.Module, t: air.Type, w: *Writer) Writer.Error!void {
|
||||
.cap => |k| try w.print("cap<{s}>", .{air.authority.kinds[k].name}),
|
||||
.builtin => |b| try w.writeAll(@tagName(b)),
|
||||
.resource => |r| try w.writeAll(r.text()),
|
||||
.own, .own_array => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
},
|
||||
.region => try w.writeAll("region"),
|
||||
.bool, .void, .never => try w.writeAll(@tagName(t)),
|
||||
}
|
||||
}
|
||||
@@ -286,6 +292,34 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
try values(f, x.args, w);
|
||||
if (x.ok) |ok| try w.print(", ok -> @{s}, err -> @{s}", .{ f.blocks[ok].name, f.blocks[x.err.?].name });
|
||||
},
|
||||
.own_new => |x| {
|
||||
try two(f, w, "own.new", x.value, x.cap);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.own_array => |x| {
|
||||
try two(f, w, "own.array", x.n, x.value);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.cap, w);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.own_take => |v| try one(f, w, "own.take", v),
|
||||
.own_free => |v| try one(f, w, "own.free", v),
|
||||
.region_create => |x| {
|
||||
try one(f, w, "region.create", x.cap);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.region_new => |x| {
|
||||
try two(f, w, "region.new", x.region, x.value);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.region_array => |x| {
|
||||
try two(f, w, "region.array", x.region, x.n);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.value, w);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.region_reset => |v| try one(f, w, "region.reset", v),
|
||||
.region_destroy => |v| try one(f, w, "region.destroy", v),
|
||||
.cap_narrow => |x| {
|
||||
try w.print("cap.narrow.{s} ", .{x.form});
|
||||
try value(f, x.cap, w);
|
||||
@@ -298,6 +332,10 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
}
|
||||
}
|
||||
|
||||
fn okErr(f: air.Function, w: *Writer, ok: u32, err: u32) Writer.Error!void {
|
||||
try w.print(", ok -> @{s}, err -> @{s}", .{ f.blocks[ok].name, f.blocks[err].name });
|
||||
}
|
||||
|
||||
fn one(f: air.Function, w: *Writer, op: []const u8, a: air.ValueId) Writer.Error!void {
|
||||
try w.print("{s} ", .{op});
|
||||
try value(f, a, w);
|
||||
|
||||
Reference in new issue
Block a user