verifier: preserve resource obligations across CFG handoff
This commit is contained in:
1 parent
0e98638bc6
commit
09fbf8a5bc
34 files changed
+1267
-67
No files matched your search
+220
-66
@@ -1,17 +1,16 @@
|
||||
//! Borrow verification for frame storage (spec 08 §11).
|
||||
//! Reports E2101, E2102, E2105, E2106, E2107, E2108.
|
||||
//!
|
||||
//! Every borrow value has static provenance fixed at its definition (root,
|
||||
//! path, mode, parent). The only flow-sensitive state is the set of live
|
||||
//! owned borrows. It must be identical on every edge into a block (§11.4),
|
||||
//! so one walk over the reverse postorder decides each function. The walk
|
||||
//! covers blocks reachable from the entry block; other blocks never execute.
|
||||
//! Borrow definitions have static provenance. Live values must agree at joins.
|
||||
//! Resource relations also follow block arguments; a monotone worklist solves
|
||||
//! them before the diagnostic walk, including later predecessors/backedges.
|
||||
|
||||
const std = @import("std");
|
||||
const air = @import("../air.zig");
|
||||
const verifier = @import("../verifier.zig");
|
||||
const ssa = @import("ssa.zig");
|
||||
const diag = @import("../diag.zig");
|
||||
const provenance = @import("provenance.zig");
|
||||
|
||||
const Context = verifier.Context;
|
||||
const ValueId = air.ValueId;
|
||||
@@ -30,16 +29,6 @@ const Seg = union(enum) {
|
||||
|
||||
const Mode = enum { shared, exclusive };
|
||||
|
||||
const Loan = struct {
|
||||
task: ValueId,
|
||||
borrow: ValueId,
|
||||
root: ValueId,
|
||||
path: []const Seg,
|
||||
mode: Mode,
|
||||
/// Diagnostic provenance only; never participates in borrow/liveness state.
|
||||
spawn: air.Span,
|
||||
};
|
||||
|
||||
/// Why a resource is no longer live (spec 06 §6.3).
|
||||
const Dead = enum { none, closed, moved };
|
||||
|
||||
@@ -76,19 +65,22 @@ const Walker = struct {
|
||||
live: []bool,
|
||||
/// How a resource stopped being live, for diagnostics.
|
||||
dead: []Dead,
|
||||
/// Borrows lent to tasks (spec 04 §34). A loan is active while its task
|
||||
/// handle is live, so it is part of the per-path state through `live`.
|
||||
loans: std.ArrayList(Loan) = .empty,
|
||||
/// Scope of each task handle value.
|
||||
task_scope: []?ValueId,
|
||||
/// Number of shields live when each shield began (spec 04 §37.3).
|
||||
shield_depth: []u32,
|
||||
/// The `ref<mutex<T>>` each guard was locked through (spec 04 §40).
|
||||
guard_parent: []?ValueId,
|
||||
facts: provenance.State = .{},
|
||||
/// First walk records definition/consumption events with relational
|
||||
/// checks disabled. Its diagnostics are discarded, not published.
|
||||
collecting: bool = false,
|
||||
events: ?*std.ArrayList(provenance.Event) = null,
|
||||
|
||||
block: u32 = 0,
|
||||
index: u32 = 0,
|
||||
span: air.Span = .{ .line = 0, .col = 0 },
|
||||
|
||||
fn event(w: *Walker, e: provenance.Event) !void {
|
||||
if (w.collecting) {
|
||||
if (w.events) |out| try out.append(w.c.arena, e);
|
||||
} else try w.facts.apply(w.c.arena, e);
|
||||
}
|
||||
|
||||
fn name(w: *Walker, v: ValueId) ![]const u8 {
|
||||
return w.c.valueName(w.f, v);
|
||||
}
|
||||
@@ -161,11 +153,13 @@ const Walker = struct {
|
||||
return;
|
||||
}
|
||||
// Borrows lent to live tasks are accessed concurrently (spec 04 §34).
|
||||
for (w.loans.items) |l| {
|
||||
if (!w.live[l.task] or l.root != root) continue;
|
||||
if (mode == .shared and l.mode == .shared) continue;
|
||||
if (!overlap(path, l.path)) continue;
|
||||
try w.report("E4101", "conflicts with a borrow lent to a live task", who, .{ "task", l.task });
|
||||
for (w.facts.items.items) |l| {
|
||||
if (l.kind != .loan or !w.live[l.a]) continue;
|
||||
const li = w.info[l.b] orelse continue;
|
||||
if (li.root != root) continue;
|
||||
if (mode == .shared and li.mode == .shared) continue;
|
||||
if (!overlap(path, li.path)) continue;
|
||||
try w.report("E4101", "conflicts with a borrow lent to a live task", who, .{ "task", l.a });
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -236,6 +230,7 @@ const Walker = struct {
|
||||
try w.access(v, &.{}, .exclusive, null, v);
|
||||
w.live[v] = false;
|
||||
w.dead[v] = how;
|
||||
try w.event(.{ .kill = v });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -303,7 +298,7 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
|
||||
}
|
||||
}
|
||||
|
||||
fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
fn walker(c: *Context, f: air.Function, types: []const air.Type) !Walker {
|
||||
var w: Walker = .{
|
||||
.c = c,
|
||||
.f = f,
|
||||
@@ -311,13 +306,7 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
.info = try c.arena.alloc(?Info, f.values.len),
|
||||
.live = try c.arena.alloc(bool, f.values.len),
|
||||
.dead = try c.arena.alloc(Dead, f.values.len),
|
||||
.task_scope = try c.arena.alloc(?ValueId, f.values.len),
|
||||
.shield_depth = try c.arena.alloc(u32, f.values.len),
|
||||
.guard_parent = try c.arena.alloc(?ValueId, f.values.len),
|
||||
};
|
||||
@memset(w.task_scope, null);
|
||||
@memset(w.shield_depth, 0);
|
||||
@memset(w.guard_parent, null);
|
||||
@memset(w.info, null);
|
||||
@memset(w.live, false);
|
||||
@memset(w.dead, .none);
|
||||
@@ -333,7 +322,40 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
if (c.module.isLinearType(p.ty)) w.live[p.value] = true;
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
const dom = try ssa.dominators(c.arena, f);
|
||||
// Most functions have no relational resources and retain the single walk.
|
||||
var relational = false;
|
||||
for (types) |t| switch (t) {
|
||||
.task, .scope, .shield, .guard => {
|
||||
relational = true;
|
||||
break;
|
||||
},
|
||||
else => {},
|
||||
};
|
||||
if (!relational) {
|
||||
var w = try walker(c, f, types);
|
||||
return walkFunction(&w, dom, null, null);
|
||||
}
|
||||
const events = try c.arena.alloc(std.ArrayList(provenance.Event), f.blocks.len);
|
||||
for (events) |*list| list.* = .empty;
|
||||
var collecting = c.*;
|
||||
collecting.diags = .empty;
|
||||
var first = try walker(&collecting, f, types);
|
||||
first.collecting = true;
|
||||
try walkFunction(&first, dom, null, events);
|
||||
const facts = try solveProvenance(c.arena, f, types, dom, events, null);
|
||||
var final = try walker(c, f, types);
|
||||
try walkFunction(&final, dom, facts, null);
|
||||
}
|
||||
|
||||
fn walkFunction(w: *Walker, dom: ssa.Dominators, facts: ?[]provenance.State, events: ?[]std.ArrayList(provenance.Event)) !void {
|
||||
const c = w.c;
|
||||
const f = w.f;
|
||||
const types = w.types;
|
||||
const entry_state = try c.arena.alloc(?[]bool, f.blocks.len);
|
||||
@memset(entry_state, null);
|
||||
entry_state[0] = try c.arena.dupe(bool, w.live);
|
||||
@@ -342,6 +364,8 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
|
||||
var out: std.ArrayList(Edge) = .empty;
|
||||
for (dom.rpo) |b| {
|
||||
if (events) |lists| w.events = &lists[b];
|
||||
if (facts) |entries| w.facts = try entries[b].clone(c.arena);
|
||||
@memcpy(w.live, entry_state[b].?);
|
||||
const blk = f.blocks[b];
|
||||
// Linear block parameters own their argument from here on; the
|
||||
@@ -354,17 +378,21 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
for (blk.insts, 0..) |inst, ii| {
|
||||
w.index = @intCast(ii);
|
||||
w.span = inst.span;
|
||||
try step(&w, inst);
|
||||
if (inst.result) |v| if (w.isResource(v)) try w.event(.{ .kill = v });
|
||||
try step(w, inst);
|
||||
}
|
||||
w.events = null;
|
||||
// Propagate along every edge; branch arguments of resource type are
|
||||
// moved on their own edge only (§11.4, spec 06 §6.2).
|
||||
out.clearRetainingCapacity();
|
||||
try edges(blk.insts[blk.insts.len - 1].op, &out, c.arena);
|
||||
const base = try c.arena.dupe(bool, w.live);
|
||||
const base_dead = try c.arena.dupe(Dead, w.dead);
|
||||
const base_facts = try w.facts.clone(c.arena);
|
||||
for (out.items) |e| {
|
||||
@memcpy(w.live, base);
|
||||
@memcpy(w.dead, base_dead);
|
||||
w.facts = try base_facts.clone(c.arena);
|
||||
for (e.args) |a| if (c.module.isLinearType(types[a])) 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
|
||||
@@ -387,10 +415,93 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
};
|
||||
}
|
||||
} else entry_state[e.target] = try c.arena.dupe(bool, w.live);
|
||||
if (facts) |entries| {
|
||||
const incoming = try edgeProvenance(c.arena, base_facts, f, types, dom, e);
|
||||
if (!reported_join[e.target]) if (entries[e.target].differentShield(incoming)) |shield| {
|
||||
reported_join[e.target] = true;
|
||||
try c.report("E3002", f.blocks[e.target].span, "shield order differs between edges into this block", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "block", .value = .{ .str = f.blocks[e.target].name } },
|
||||
.{ .name = "resource", .value = .{ .str = try c.valueName(f, shield) } },
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// All linear arguments move simultaneously, including the scope position
|
||||
/// of facts whose task itself is not passed. Old target-parameter instances
|
||||
/// are overwritten; implicit outcome definitions were recorded as events.
|
||||
fn edgeProvenance(arena: std.mem.Allocator, base: provenance.State, f: air.Function, types: []const air.Type, dom: ssa.Dominators, e: Edge) !provenance.State {
|
||||
var out: provenance.State = .{};
|
||||
for (base.items.items) |old| {
|
||||
var fact = old;
|
||||
fact.a = edgeValue(old.a, f, types, dom, e) orelse continue;
|
||||
fact.b = edgeValue(old.b, f, types, dom, e) orelse continue;
|
||||
_ = try out.add(arena, fact);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
fn edgeValue(v: ValueId, f: air.Function, types: []const air.Type, dom: ssa.Dominators, e: Edge) ?ValueId {
|
||||
for (e.args, 0..) |arg, i| if (arg == v and types[arg].isLinear()) return f.blocks[e.target].params[i].value;
|
||||
// Explicit targets overwrite any older instance of their parameters.
|
||||
if (e.args.len != 0) for (f.blocks[e.target].params) |p| if (p.value == v) return null;
|
||||
return switch (f.values[v].def) {
|
||||
.param => v,
|
||||
.block_param => |d| if (dom.dominates(d.block, e.target)) v else null,
|
||||
.inst => |d| if (dom.dominates(d.block, e.target)) v else null,
|
||||
.none => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
const ProvenanceStats = struct {
|
||||
visits: usize = 0,
|
||||
facts: usize = 0,
|
||||
};
|
||||
|
||||
/// A finite monotone lattice of resource relations and loan origins. Events
|
||||
/// depend only on definitions and the collecting live-value transfer. Final
|
||||
/// error recovery may close extra values; checks filter those dead subjects,
|
||||
/// so collecting is conservative. Sparse sets retain insertion order; union
|
||||
/// does not impose a new equality rule on task/guard origins.
|
||||
fn solveProvenance(arena: std.mem.Allocator, f: air.Function, types: []const air.Type, dom: ssa.Dominators, events: []std.ArrayList(provenance.Event), stats: ?*ProvenanceStats) ![]provenance.State {
|
||||
const entries = try arena.alloc(provenance.State, f.blocks.len);
|
||||
for (entries) |*s| s.* = .{};
|
||||
const visited = try arena.alloc(bool, f.blocks.len);
|
||||
@memset(visited, false);
|
||||
const queued = try arena.alloc(bool, f.blocks.len);
|
||||
@memset(queued, false);
|
||||
var queue: std.ArrayList(u32) = .empty;
|
||||
try queue.append(arena, 0);
|
||||
queued[0] = true;
|
||||
visited[0] = true;
|
||||
var pos: usize = 0;
|
||||
var out: std.ArrayList(Edge) = .empty;
|
||||
while (pos < queue.items.len) : (pos += 1) {
|
||||
const b = queue.items[pos];
|
||||
if (stats) |s| s.visits += 1;
|
||||
queued[b] = false;
|
||||
var state = try entries[b].clone(arena);
|
||||
for (events[b].items) |event| try state.apply(arena, event);
|
||||
out.clearRetainingCapacity();
|
||||
const block = f.blocks[b];
|
||||
try edges(block.insts[block.insts.len - 1].op, &out, arena);
|
||||
for (out.items) |e| {
|
||||
const incoming = try edgeProvenance(arena, state, f, types, dom, e);
|
||||
const changed = try entries[e.target].merge(arena, incoming);
|
||||
if (stats) |s| s.facts += incoming.items.items.len;
|
||||
if ((!visited[e.target] or changed) and !queued[e.target]) {
|
||||
try queue.append(arena, e.target);
|
||||
queued[e.target] = true;
|
||||
}
|
||||
visited[e.target] = true;
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
fn step(w: *Walker, inst: air.Inst) !void {
|
||||
const f = w.f;
|
||||
switch (inst.op) {
|
||||
@@ -432,11 +543,11 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
if (!w.live[v]) return w.report("E2106", "borrow ended twice", v, null);
|
||||
// Ended even when rejected, so one mistake yields one diagnostic.
|
||||
defer w.live[v] = false;
|
||||
for (w.loans.items) |l| if (w.live[l.task] and (l.borrow == v or w.isAncestor(v, l.borrow))) {
|
||||
return w.reportWithOrigin("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.task }, l.spawn);
|
||||
for (w.facts.items.items) |l| if (l.kind == .loan and w.live[l.a] and (l.b == v or w.isAncestor(v, l.b))) {
|
||||
return w.reportWithOrigin("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.a }, l.spawn);
|
||||
};
|
||||
for (w.live, 0..) |is_live, g| if (is_live and w.guard_parent[g] != null and w.isAncestor(v, w.guard_parent[g].?)) {
|
||||
return w.report("E2102", "mutex borrow ended while a guard locked through it is live", v, .{ "guard", @intCast(g) });
|
||||
for (w.facts.items.items) |g| if (g.kind == .guard and w.live[g.a] and w.isAncestor(v, g.b)) {
|
||||
return w.report("E2102", "mutex borrow ended while a guard locked through it is live", v, .{ "guard", g.a });
|
||||
};
|
||||
for (w.live, 0..) |is_live, cv| {
|
||||
if (is_live and w.info[cv] != null and w.info[cv].?.parent == v) {
|
||||
@@ -508,14 +619,14 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
.task_spawn => |x| {
|
||||
try noWaitUnderLock(w, "task.spawn");
|
||||
const t = inst.result.?;
|
||||
w.task_scope[t] = x.scope;
|
||||
try w.event(.{ .add = .{ .kind = .scope, .a = t, .b = x.scope } });
|
||||
for (x.args) |a| {
|
||||
if (w.types[a].isBorrow()) {
|
||||
if (!try w.usable(a)) continue;
|
||||
const ai = w.info[a].?;
|
||||
// The loan must not conflict with live borrows or loans.
|
||||
try w.accessCode(ai.root, ai.path, ai.mode, a, a, "E4101", "lending a borrow that conflicts with a live borrow");
|
||||
try w.loans.append(w.c.arena, .{ .task = t, .borrow = a, .root = ai.root, .path = ai.path, .mode = ai.mode, .spawn = inst.span });
|
||||
try w.event(.{ .add = .{ .kind = .loan, .a = t, .b = a, .spawn = inst.span } });
|
||||
} else if (w.isResource(a)) try w.consume(a, .moved);
|
||||
}
|
||||
w.live[t] = true;
|
||||
@@ -530,11 +641,15 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
},
|
||||
.task_cancel => |t| _ = try w.resourceUsable(t),
|
||||
.task_scope_end => |s| {
|
||||
for (w.live, 0..) |is_live, v| {
|
||||
if (is_live and w.task_scope[v] != null and w.task_scope[v].? == s) {
|
||||
try w.report("E4301", "scope ends while a task spawned from it is not joined", @intCast(v), .{ "scope", s });
|
||||
w.live[v] = false; // reported once, in preference to E3002
|
||||
}
|
||||
// Copy the selected handles before killing their facts.
|
||||
var children: std.ArrayList(ValueId) = .empty;
|
||||
for (w.facts.items.items) |fact| if (fact.kind == .scope and fact.b == s and w.live[fact.a]) {
|
||||
if (std.mem.indexOfScalar(ValueId, children.items, fact.a) == null) try children.append(w.c.arena, fact.a);
|
||||
};
|
||||
for (children.items) |v| {
|
||||
try w.report("E4301", "scope ends while a task spawned from it is not joined", v, .{ "scope", s });
|
||||
w.live[v] = false; // prefer E4301 to E3002
|
||||
try w.event(.{ .kill = v });
|
||||
}
|
||||
try w.consume(s, .closed);
|
||||
},
|
||||
@@ -575,28 +690,20 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
const back = w.f.blocks[x.cancelled].params[0].value;
|
||||
// On `cancelled` the same task comes back: its scope and loans
|
||||
// move to the new handle value (spec 04 §37.3).
|
||||
w.task_scope[back] = w.task_scope[t];
|
||||
const n = w.loans.items.len;
|
||||
for (0..n) |i| {
|
||||
const l = w.loans.items[i];
|
||||
if (l.task == t) {
|
||||
var transferred = l;
|
||||
transferred.task = back;
|
||||
try w.loans.append(w.c.arena, transferred);
|
||||
}
|
||||
}
|
||||
try w.event(.{ .kill = back });
|
||||
try w.event(.{ .rename = .{ .from = t, .to = back } });
|
||||
try w.consume(t, .closed);
|
||||
},
|
||||
.task_shield_begin => {
|
||||
const r = inst.result.?;
|
||||
var depth: u32 = 0;
|
||||
for (w.live, 0..) |is_live, v| depth += @intFromBool(is_live and w.types[v] == .shield);
|
||||
w.shield_depth[r] = depth;
|
||||
for (w.live, 0..) |is_live, v| if (is_live and w.types[v] == .shield) {
|
||||
try w.event(.{ .add = .{ .kind = .inner, .a = r, .b = @intCast(v) } });
|
||||
};
|
||||
w.live[r] = true;
|
||||
},
|
||||
.task_shield_end => |sh| {
|
||||
if (w.live[sh]) for (w.live, 0..) |is_live, v| if (is_live and w.types[v] == .shield and w.shield_depth[v] > w.shield_depth[sh]) {
|
||||
try w.report("E4305", "shield ended while a more inner shield is live", sh, .{ "inner", @intCast(v) });
|
||||
if (w.live[sh]) for (w.facts.items.items) |fact| if (fact.kind == .inner and fact.b == sh and w.live[fact.a]) {
|
||||
try w.report("E4305", "shield ended while a more inner shield is live", sh, .{ "inner", fact.a });
|
||||
break;
|
||||
};
|
||||
try w.consume(sh, .closed);
|
||||
@@ -640,7 +747,9 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
.mutex_lock => |x| {
|
||||
try noWaitUnderLock(w, "mutex.lock");
|
||||
try lendArgs(w, &.{x.mutex});
|
||||
w.guard_parent[w.f.blocks[x.ok].params[0].value] = x.mutex;
|
||||
const g = w.f.blocks[x.ok].params[0].value;
|
||||
try w.event(.{ .kill = g });
|
||||
try w.event(.{ .add = .{ .kind = .guard, .a = g, .b = x.mutex } });
|
||||
},
|
||||
.mutex_borrow => |g| {
|
||||
const r = inst.result.?;
|
||||
@@ -659,6 +768,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
};
|
||||
w.live[g] = false;
|
||||
w.dead[g] = .closed;
|
||||
try w.event(.{ .kill = g });
|
||||
},
|
||||
// Graphs (spec 03 §22–§24). An access is a child borrow of the
|
||||
// container borrow; a `mut` child is exclusive for the whole island.
|
||||
@@ -692,8 +802,8 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
/// Taking a mutex or an atomic cell while a borrow of it is lent to a live
|
||||
/// task races with that task: `E4101` rather than `E2101` (spec 04 §28, §40).
|
||||
fn takeLent(w: *Walker, v: ValueId) !bool {
|
||||
for (w.loans.items) |l| if (w.live[l.task] and l.root == v) {
|
||||
try w.report("E4101", "taken while a borrow of it is lent to a live task", v, .{ "task", l.task });
|
||||
for (w.facts.items.items) |l| if (l.kind == .loan and w.live[l.a] and w.info[l.b] != null and w.info[l.b].?.root == v) {
|
||||
try w.report("E4101", "taken while a borrow of it is lent to a live task", v, .{ "task", l.a });
|
||||
// Consumed all the same, so the mistake is not reported again as a leak.
|
||||
w.live[v] = false;
|
||||
w.dead[v] = .closed;
|
||||
@@ -846,3 +956,47 @@ test "call results keep their source alive; mut arguments do not alias" {
|
||||
const two = "fn @g(%a: mut<u8>, %b: ref<u8>) -> void\neffects []\nentry:\n ret\n";
|
||||
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n" ++ local_x ++ " %m = borrow.mut %x\n %r = borrow.ref %m\n call @g(%m, %r)\n borrow.end %r\n borrow.end %m\n ret\n" ++ two, &.{"E2101"});
|
||||
}
|
||||
|
||||
test "task provenance rotation reaches every header parameter with bounded visits" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const count = 63; // Below the existing 64-parameter limit.
|
||||
var source: std.Io.Writer.Allocating = .init(a);
|
||||
const out = &source.writer;
|
||||
try out.writeAll("module rotation\nair 0\nlang 0.1\nfeatures [cancellation, effects, ownership, tasks]\nfn @main() -> void\neffects [task.spawn]\nentry:\n %x = local.alloc u8\n local.zero %x\n %r = borrow.ref %x\n %s = task.scope.begin\n %t0 = task.spawn %s, @read(%r)\n");
|
||||
for (1..count) |i| try out.print(" %t{d} = task.spawn %s, @zero()\n", .{i});
|
||||
try out.writeAll(" br @head(");
|
||||
for (0..count) |i| try out.print("{s}%t{d}", .{ if (i == 0) "" else ", ", i });
|
||||
try out.writeAll(")\nhead(");
|
||||
for (0..count) |i| try out.print("{s}%p{d}: task<u8>", .{ if (i == 0) "" else ", ", i });
|
||||
try out.writeAll("):\n br @head(");
|
||||
for (0..count) |i| try out.print("{s}%p{d}", .{ if (i == 0) "" else ", ", (i + 1) % count });
|
||||
try out.writeAll(")\nfn @read(%p: ref<u8>) -> u8\neffects []\nentry:\n %v = load %p\n ret %v\nfn @zero() -> u8\neffects []\nentry:\n %v = const.u8 0\n ret %v\n");
|
||||
const parsed = try air.parse.parse(a, "rotation.air", source.written());
|
||||
try std.testing.expectEqual(@as(usize, 0), parsed.diagnostics.len);
|
||||
var c: Context = .{ .arena = a, .file = "rotation.air", .module = parsed.module.? };
|
||||
try verifier.cfg.check(&c);
|
||||
try ssa.check(&c);
|
||||
try verifier.types.check(&c);
|
||||
try std.testing.expectEqual(@as(usize, 0), c.diags.items.len);
|
||||
const f = c.module.functions[0];
|
||||
const types = c.value_types[0];
|
||||
const dom = try ssa.dominators(a, f);
|
||||
const events = try a.alloc(std.ArrayList(provenance.Event), f.blocks.len);
|
||||
for (events) |*e| e.* = .empty;
|
||||
var w = try walker(&c, f, types);
|
||||
w.collecting = true;
|
||||
try walkFunction(&w, dom, null, events);
|
||||
try std.testing.expectEqual(@as(usize, 0), c.diags.items.len);
|
||||
var stats: ProvenanceStats = .{};
|
||||
const entries = try solveProvenance(a, f, types, dom, events, &stats);
|
||||
var loans: usize = 0;
|
||||
for (entries[1].items.items) |fact| if (fact.kind == .loan) {
|
||||
loans += 1;
|
||||
try std.testing.expectEqual(@as(u32, 12), fact.spawn.line);
|
||||
};
|
||||
try std.testing.expectEqual(@as(usize, count), loans);
|
||||
try std.testing.expect(stats.visits <= count + 2);
|
||||
try std.testing.expect(stats.facts <= 2 * count * (count + 2));
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
//! Function-local resource relations, independent of SSA spelling.
|
||||
//! Facts join by union; edge renaming is simultaneous in both positions.
|
||||
//! Shield order additionally has an exact-state join rule (spec 04 §37.3).
|
||||
const std = @import("std");
|
||||
const air = @import("../air.zig");
|
||||
|
||||
pub const Kind = enum { loan, scope, guard, inner };
|
||||
pub const Fact = struct {
|
||||
kind: Kind,
|
||||
a: air.ValueId,
|
||||
b: air.ValueId,
|
||||
// Meaningful only for loans. Never changes their transfer semantics.
|
||||
spawn: air.Span = .{ .line = 0, .col = 0 },
|
||||
};
|
||||
|
||||
pub const Event = union(enum) {
|
||||
add: Fact,
|
||||
kill: air.ValueId,
|
||||
// The cancelled join replaces a task handle without ending its loans.
|
||||
rename: struct { from: air.ValueId, to: air.ValueId },
|
||||
};
|
||||
|
||||
pub const State = struct {
|
||||
items: std.ArrayList(Fact) = .empty,
|
||||
set: std.AutoHashMapUnmanaged(Fact, void) = .empty,
|
||||
|
||||
pub fn add(s: *State, arena: std.mem.Allocator, fact: Fact) !bool {
|
||||
const r = try s.set.getOrPut(arena, fact);
|
||||
if (r.found_existing) return false;
|
||||
try s.items.append(arena, fact);
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn merge(s: *State, arena: std.mem.Allocator, other: State) !bool {
|
||||
var changed = false;
|
||||
for (other.items.items) |fact| changed = (try s.add(arena, fact)) or changed;
|
||||
return changed;
|
||||
}
|
||||
|
||||
pub fn clone(s: State, arena: std.mem.Allocator) !State {
|
||||
var out: State = .{};
|
||||
_ = try out.merge(arena, s);
|
||||
return out;
|
||||
}
|
||||
|
||||
pub fn kill(s: *State, v: air.ValueId) void {
|
||||
var n: usize = 0;
|
||||
for (s.items.items) |fact| {
|
||||
if (fact.a == v or fact.b == v) {
|
||||
_ = s.set.remove(fact);
|
||||
} else {
|
||||
s.items.items[n] = fact;
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
s.items.shrinkRetainingCapacity(n);
|
||||
}
|
||||
|
||||
pub fn apply(s: *State, arena: std.mem.Allocator, event: Event) !void {
|
||||
switch (event) {
|
||||
.add => |f| _ = try s.add(arena, f),
|
||||
.kill => |v| s.kill(v),
|
||||
.rename => |r| {
|
||||
var out: State = .{};
|
||||
for (s.items.items) |old| {
|
||||
var f = old;
|
||||
if (f.a == r.from) f.a = r.to;
|
||||
if (f.b == r.from) f.b = r.to;
|
||||
_ = try out.add(arena, f);
|
||||
}
|
||||
s.* = out;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sameShields(a: State, b: State) bool {
|
||||
return a.differentShield(b) == null;
|
||||
}
|
||||
|
||||
pub fn differentShield(a: State, b: State) ?air.ValueId {
|
||||
for (a.items.items) |f| if (f.kind == .inner and !b.set.contains(f)) return f.a;
|
||||
for (b.items.items) |f| if (f.kind == .inner and !a.set.contains(f)) return f.a;
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
test "relations retain loan origin, join once and remove both endpoints" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
var s: State = .{};
|
||||
const loan: Fact = .{ .kind = .loan, .a = 1, .b = 2, .spawn = .{ .line = 7, .col = 3 } };
|
||||
try std.testing.expect(try s.add(a, loan));
|
||||
try std.testing.expect(!try s.add(a, loan));
|
||||
try s.apply(a, .{ .rename = .{ .from = 1, .to = 4 } });
|
||||
try std.testing.expectEqual(@as(air.ValueId, 4), s.items.items[0].a);
|
||||
try std.testing.expectEqual(loan.spawn, s.items.items[0].spawn);
|
||||
s.kill(2);
|
||||
try std.testing.expectEqual(@as(usize, 0), s.items.items.len);
|
||||
}
|
||||
Reference in new issue
Block a user