cli: query SSA liveness with iterative continuations
This commit is contained in:
7 files changed
+1427
-339
No files matched your search
@@ -0,0 +1,407 @@
|
||||
//! Pruned-SSA source queries (spec 15 §4). Borrowed AST cursors preserve
|
||||
//! short-circuit order without native AST recursion. Scratch is reused across
|
||||
//! queries; host OOM is an error, never a fabricated read/assignment fact.
|
||||
const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Span = ast.Span;
|
||||
|
||||
pub const Loop = struct { cond: ?ast.Expr, body: ast.Block, step: ?[]const u8 };
|
||||
pub const Region = union(enum) {
|
||||
loop: Loop,
|
||||
@"if": ast.If,
|
||||
match: ast.Match,
|
||||
lock: struct { ast.Block, ast.Block },
|
||||
select: ast.Select,
|
||||
pub fn assigns(r: Region, a: Allocator, workspace: *Workspace, name: []const u8) Allocator.Error!bool {
|
||||
return workspace.assigns(a, .{ .region = r }, name);
|
||||
}
|
||||
};
|
||||
pub const Use = enum { read, kill, none };
|
||||
pub const Root = union(enum) { expr: ast.Expr, stmt: ast.Stmt, block: ast.Block, loop: Loop, region: Region };
|
||||
pub const After = struct {
|
||||
name: []const u8,
|
||||
construct: Span,
|
||||
seen: bool = false,
|
||||
pub fn expr(w: *After, a: Allocator, workspace: *Workspace, e: ast.Expr) Allocator.Error!bool {
|
||||
return workspace.query(a, .after, .{ .expr = e }, w.name, null, w);
|
||||
}
|
||||
pub fn stmt(w: *After, a: Allocator, workspace: *Workspace, st: ast.Stmt) Allocator.Error!bool {
|
||||
return workspace.query(a, .after, .{ .stmt = st }, w.name, null, w);
|
||||
}
|
||||
};
|
||||
|
||||
fn spanEq(a: Span, b: Span) bool {
|
||||
// Preserve the former position-only comparison, including across files.
|
||||
return a.start.line == b.start.line and a.start.col == b.start.col and a.end.line == b.end.line and a.end.col == b.end.col;
|
||||
}
|
||||
fn eq(a: []const u8, b: []const u8) bool {
|
||||
return std.mem.eql(u8, a, b);
|
||||
}
|
||||
|
||||
/// A workspace belongs to one allocator; every query and deinit uses that
|
||||
/// same allocator. Lowerer owns one workspace for its entire arena lifetime.
|
||||
pub const Workspace = struct {
|
||||
const Mode = enum { read, assign, after };
|
||||
const ArmCursor = struct { arms: []const ast.Arm, cut_on_seen: bool = false };
|
||||
const SelectCursor = struct { sel: ast.Select, at: usize = 0, body: usize = 0, cut_on_seen: bool = false };
|
||||
const Entry = union(enum) {
|
||||
expr: ast.Expr,
|
||||
stmt: ast.Stmt,
|
||||
block: ast.Block,
|
||||
loop: Loop,
|
||||
region: Region,
|
||||
exprs: []const ast.Expr,
|
||||
stmts: []const ast.Stmt,
|
||||
fields: []const ast.FieldInit,
|
||||
conditional: ast.If,
|
||||
if_then: ast.If,
|
||||
if_else: struct { branch: ?ast.Else, cut_on_seen: bool },
|
||||
match: ast.Match,
|
||||
match_arms: []const ast.Arm,
|
||||
arms: ArmCursor,
|
||||
select: ast.Select,
|
||||
select_headers: SelectCursor,
|
||||
select_reads: SelectCursor,
|
||||
select_bodies: SelectCursor,
|
||||
loop_back: struct { loop: Loop, was_seen: bool },
|
||||
};
|
||||
const Work = struct { mode: Mode, entry: Entry };
|
||||
work: std.ArrayList(Work) = .empty,
|
||||
|
||||
/// Same allocator as queries. Arena-backed buffers retain arena lifetime.
|
||||
pub fn deinit(self: *Workspace, a: Allocator) void {
|
||||
self.work.deinit(a);
|
||||
}
|
||||
pub fn reads(self: *Workspace, a: Allocator, root: Root, name: []const u8, skip: ?Span) Allocator.Error!bool {
|
||||
return self.query(a, .read, root, name, skip, null);
|
||||
}
|
||||
pub fn assigns(self: *Workspace, a: Allocator, root: Root, name: []const u8) Allocator.Error!bool {
|
||||
return self.query(a, .assign, root, name, null, null);
|
||||
}
|
||||
/// Only a straight-line whole-variable assignment kills the old value.
|
||||
pub fn firstUse(self: *Workspace, a: Allocator, stmts: []const ast.Stmt, tail: ?*const ast.Expr, name: []const u8) Allocator.Error!Use {
|
||||
for (stmts) |st| {
|
||||
if (st.kind == .assign and st.kind.assign.place.kind == .name and eq(st.kind.assign.place.kind.name, name)) {
|
||||
return if (try self.reads(a, .{ .expr = st.kind.assign.e }, name, null)) .read else .kill;
|
||||
}
|
||||
if (try self.reads(a, .{ .stmt = st }, name, null)) return .read;
|
||||
}
|
||||
if (tail) |t| if (try self.reads(a, .{ .expr = t.* }, name, null)) return .read;
|
||||
return .none;
|
||||
}
|
||||
fn push(self: *Workspace, a: Allocator, mode: Mode, entry: Entry) Allocator.Error!void {
|
||||
try self.work.append(a, .{ .mode = mode, .entry = entry });
|
||||
}
|
||||
fn query(self: *Workspace, a: Allocator, mode: Mode, root: Root, name: []const u8, skip: ?Span, after: ?*After) Allocator.Error!bool {
|
||||
std.debug.assert(self.work.items.len == 0);
|
||||
defer self.work.items.len = 0;
|
||||
var current: Work = .{ .mode = mode, .entry = switch (root) {
|
||||
.expr => |x| .{ .expr = x },
|
||||
.stmt => |x| .{ .stmt = x },
|
||||
.block => |x| .{ .block = x },
|
||||
.loop => |x| .{ .loop = x },
|
||||
.region => |x| .{ .region = x },
|
||||
} };
|
||||
scan: while (true) {
|
||||
const m = current.mode;
|
||||
switch (current.entry) {
|
||||
.expr => |e| {
|
||||
if (m == .read) {
|
||||
if (skip) |sk| if (spanEq(e.span, sk)) {
|
||||
current = self.work.pop() orelse return false;
|
||||
continue :scan;
|
||||
};
|
||||
}
|
||||
if (m == .after and !after.?.seen and spanEq(e.span, after.?.construct)) {
|
||||
after.?.seen = true;
|
||||
current = self.work.pop() orelse return false;
|
||||
continue :scan;
|
||||
}
|
||||
switch (e.kind) {
|
||||
.name, .move => |n| if (m != .assign and (m == .read or after.?.seen) and eq(n, name)) return true,
|
||||
.int, .boolean, .bytes, .variant, .specialize => {},
|
||||
.neg, .not, .deref, .or_trap => |x| {
|
||||
current.entry = .{ .expr = x.* };
|
||||
continue :scan;
|
||||
},
|
||||
.borrow => |x| if (m != .assign) {
|
||||
current.entry = .{ .expr = x.place.* };
|
||||
continue :scan;
|
||||
},
|
||||
.binary => |x| {
|
||||
try self.push(a, m, .{ .expr = x.r.* });
|
||||
current.entry = .{ .expr = x.l.* };
|
||||
continue :scan;
|
||||
},
|
||||
.cast => |x| {
|
||||
current.entry = .{ .expr = x.e.* };
|
||||
continue :scan;
|
||||
},
|
||||
.call => |x| {
|
||||
if (m != .assign and x.args.len != 0) try self.push(a, m, .{ .exprs = x.args });
|
||||
current.entry = if (m == .assign) .{ .exprs = x.args } else .{ .expr = x.callee.* };
|
||||
continue :scan;
|
||||
},
|
||||
.field => |x| {
|
||||
current.entry = .{ .expr = x.e.* };
|
||||
continue :scan;
|
||||
},
|
||||
.index => |x| {
|
||||
if (x.hi) |h| try self.push(a, m, .{ .expr = h.* });
|
||||
try self.push(a, m, .{ .expr = x.lo.* });
|
||||
current.entry = .{ .expr = x.e.* };
|
||||
continue :scan;
|
||||
},
|
||||
.struct_lit => |x| {
|
||||
current.entry = .{ .fields = x.fields };
|
||||
continue :scan;
|
||||
},
|
||||
.array_rep => |x| {
|
||||
current.entry = .{ .expr = x.e.* };
|
||||
continue :scan;
|
||||
},
|
||||
.array_list => |xs| {
|
||||
current.entry = .{ .exprs = xs };
|
||||
continue :scan;
|
||||
},
|
||||
.if_expr => |x| {
|
||||
current.entry = .{ .conditional = x.* };
|
||||
continue :scan;
|
||||
},
|
||||
.match_expr => |x| {
|
||||
current.entry = .{ .match = x.* };
|
||||
continue :scan;
|
||||
},
|
||||
.block => |b| {
|
||||
current.entry = .{ .block = b.* };
|
||||
continue :scan;
|
||||
},
|
||||
}
|
||||
},
|
||||
.stmt => |st| {
|
||||
if (m == .after and !after.?.seen and spanEq(st.span, after.?.construct)) {
|
||||
after.?.seen = true;
|
||||
current = self.work.pop() orelse return false;
|
||||
continue :scan;
|
||||
}
|
||||
switch (st.kind) {
|
||||
.let => |x| {
|
||||
current.entry = .{ .expr = x.e };
|
||||
continue :scan;
|
||||
},
|
||||
.@"var" => |x| if (x.init) |e| {
|
||||
current.entry = .{ .expr = e };
|
||||
continue :scan;
|
||||
},
|
||||
.assign => |x| {
|
||||
if (m == .assign and x.place.kind == .name and eq(x.place.kind.name, name)) return true;
|
||||
if (m != .assign and x.place.kind != .name) try self.push(a, m, .{ .expr = x.place });
|
||||
current.entry = .{ .expr = x.e };
|
||||
continue :scan;
|
||||
},
|
||||
.borrow => |x| {
|
||||
current.entry = .{ .expr = x.place };
|
||||
continue :scan;
|
||||
},
|
||||
.@"return" => |e| if (e) |x| {
|
||||
current.entry = .{ .expr = x };
|
||||
continue :scan;
|
||||
},
|
||||
.expr => |e| {
|
||||
current.entry = .{ .expr = e };
|
||||
continue :scan;
|
||||
},
|
||||
.@"while" => |x| {
|
||||
current.entry = .{ .loop = .{ .cond = x.cond, .body = x.body, .step = null } };
|
||||
continue :scan;
|
||||
},
|
||||
.@"for" => |x| {
|
||||
if (m == .assign) {
|
||||
if (eq(x.name, name)) return true;
|
||||
current.entry = .{ .block = x.body };
|
||||
} else {
|
||||
try self.push(a, m, if (m == .after) .{ .loop = .{ .cond = null, .body = x.body, .step = x.name } } else .{ .block = x.body });
|
||||
try self.push(a, m, .{ .expr = x.hi });
|
||||
current.entry = .{ .expr = x.lo };
|
||||
}
|
||||
continue :scan;
|
||||
},
|
||||
.loop => |b| {
|
||||
current.entry = .{ .loop = .{ .cond = null, .body = b, .step = null } };
|
||||
continue :scan;
|
||||
},
|
||||
.shield => |b| {
|
||||
current.entry = .{ .block = b };
|
||||
continue :scan;
|
||||
},
|
||||
.scope => |x| {
|
||||
current.entry = .{ .block = x.body };
|
||||
continue :scan;
|
||||
},
|
||||
.lock => |x| {
|
||||
try self.push(a, m, .{ .block = x.cancelled });
|
||||
if (m == .assign) current.entry = .{ .block = x.body } else {
|
||||
try self.push(a, m, .{ .block = x.body });
|
||||
current.entry = .{ .expr = x.place };
|
||||
}
|
||||
continue :scan;
|
||||
},
|
||||
.select => |x| {
|
||||
current.entry = .{ .select = x };
|
||||
continue :scan;
|
||||
},
|
||||
.@"break", .@"continue", .trap => {},
|
||||
}
|
||||
},
|
||||
.block => |b| {
|
||||
if (b.stmts.len == 0) {
|
||||
if (b.tail) |t| {
|
||||
current.entry = .{ .expr = t.* };
|
||||
continue :scan;
|
||||
}
|
||||
} else {
|
||||
if (b.tail) |t| try self.push(a, m, .{ .expr = t.* });
|
||||
current.entry = .{ .stmts = b.stmts };
|
||||
continue :scan;
|
||||
}
|
||||
},
|
||||
.exprs => |xs| if (xs.len != 0) {
|
||||
if (xs.len > 1) try self.push(a, m, .{ .exprs = xs[1..] });
|
||||
current.entry = .{ .expr = xs[0] };
|
||||
continue :scan;
|
||||
},
|
||||
.stmts => |xs| if (xs.len != 0) {
|
||||
if (xs.len > 1) try self.push(a, m, .{ .stmts = xs[1..] });
|
||||
current.entry = .{ .stmt = xs[0] };
|
||||
continue :scan;
|
||||
},
|
||||
.fields => |xs| if (xs.len != 0) {
|
||||
if (xs.len > 1) try self.push(a, m, .{ .fields = xs[1..] });
|
||||
current.entry = .{ .expr = xs[0].e };
|
||||
continue :scan;
|
||||
},
|
||||
.conditional => |x| {
|
||||
try self.push(a, m, .{ .if_then = x });
|
||||
current.entry = .{ .expr = x.cond };
|
||||
continue :scan;
|
||||
},
|
||||
.if_then => |x| {
|
||||
try self.push(a, m, .{ .if_else = .{ .branch = x.@"else", .cut_on_seen = m == .after and !after.?.seen } });
|
||||
current.entry = .{ .block = x.then };
|
||||
continue :scan;
|
||||
},
|
||||
.if_else => |x| if (!(x.cut_on_seen and after.?.seen)) {
|
||||
if (x.branch) |els| {
|
||||
current.entry = switch (els) {
|
||||
.block => |b| .{ .block = b },
|
||||
.@"if" => |inner| .{ .conditional = inner.* },
|
||||
};
|
||||
continue :scan;
|
||||
}
|
||||
},
|
||||
.match => |x| {
|
||||
try self.push(a, m, .{ .match_arms = x.arms });
|
||||
current.entry = .{ .expr = x.scrutinee };
|
||||
continue :scan;
|
||||
},
|
||||
.match_arms => |arms| {
|
||||
current.entry = .{ .arms = .{ .arms = arms, .cut_on_seen = m == .after and !after.?.seen } };
|
||||
continue :scan;
|
||||
},
|
||||
.arms => |cursor| if (cursor.arms.len != 0 and !(cursor.cut_on_seen and after.?.seen)) {
|
||||
const arm = cursor.arms[0];
|
||||
if (m == .assign and arm.pattern.kind == .ctor) for (arm.pattern.kind.ctor.binds) |bn| {
|
||||
if (eq(bn, name)) return true;
|
||||
};
|
||||
if (cursor.arms.len > 1) try self.push(a, m, .{ .arms = .{ .arms = cursor.arms[1..], .cut_on_seen = cursor.cut_on_seen } });
|
||||
current.entry = .{ .expr = arm.body };
|
||||
continue :scan;
|
||||
},
|
||||
.select => |sel| {
|
||||
current.entry = if (m == .read) .{ .select_reads = .{ .sel = sel } } else if (m == .assign) .{ .select_bodies = .{ .sel = sel } } else .{ .select_headers = .{ .sel = sel } };
|
||||
continue :scan;
|
||||
},
|
||||
.select_reads => |cursor| {
|
||||
if (cursor.at < cursor.sel.arms.len) {
|
||||
const arm = cursor.sel.arms[cursor.at];
|
||||
if (eq(arm.endpoint, name)) return true;
|
||||
try self.push(a, m, .{ .select_reads = .{ .sel = cursor.sel, .at = cursor.at + 1 } });
|
||||
if (arm.value) |v| {
|
||||
if (arm.arms.len != 0) try self.push(a, m, .{ .arms = .{ .arms = arm.arms } });
|
||||
current.entry = .{ .expr = v };
|
||||
} else current.entry = .{ .arms = .{ .arms = arm.arms } };
|
||||
} else current.entry = .{ .expr = cursor.sel.cancelled };
|
||||
continue :scan;
|
||||
},
|
||||
.select_headers => |cursor| {
|
||||
if (cursor.at < cursor.sel.arms.len) {
|
||||
const arm = cursor.sel.arms[cursor.at];
|
||||
if (after.?.seen and eq(arm.endpoint, name)) return true;
|
||||
current.entry = .{ .select_headers = .{ .sel = cursor.sel, .at = cursor.at + 1 } };
|
||||
if (arm.value) |v| {
|
||||
try self.push(a, m, current.entry);
|
||||
current.entry = .{ .expr = v };
|
||||
}
|
||||
} else current.entry = .{ .select_bodies = .{ .sel = cursor.sel, .cut_on_seen = !after.?.seen } };
|
||||
continue :scan;
|
||||
},
|
||||
.select_bodies => |cursor| if (!(cursor.cut_on_seen and after.?.seen)) {
|
||||
if (cursor.at == cursor.sel.arms.len) {
|
||||
if (m == .assign) for (cursor.sel.cancelled_binds) |bn| {
|
||||
if (eq(bn, name)) return true;
|
||||
};
|
||||
current.entry = .{ .expr = cursor.sel.cancelled };
|
||||
continue :scan;
|
||||
}
|
||||
const arms = cursor.sel.arms[cursor.at].arms;
|
||||
if (cursor.body == arms.len) {
|
||||
var next = cursor;
|
||||
next.at += 1;
|
||||
next.body = 0;
|
||||
current.entry = .{ .select_bodies = next };
|
||||
continue :scan;
|
||||
}
|
||||
const arm = arms[cursor.body];
|
||||
if (m == .assign and arm.pattern.kind == .ctor) for (arm.pattern.kind.ctor.binds) |bn| {
|
||||
if (eq(bn, name)) return true;
|
||||
};
|
||||
var next = cursor;
|
||||
next.body += 1;
|
||||
try self.push(a, m, .{ .select_bodies = next });
|
||||
current.entry = .{ .expr = arm.body };
|
||||
continue :scan;
|
||||
},
|
||||
.loop => |lp| {
|
||||
if (m != .after) {
|
||||
if (lp.step) |step| if (eq(step, name)) return true;
|
||||
} else try self.push(a, m, .{ .loop_back = .{ .loop = lp, .was_seen = after.?.seen } });
|
||||
if (lp.cond) |cond| {
|
||||
try self.push(a, m, .{ .block = lp.body });
|
||||
current.entry = .{ .expr = cond };
|
||||
} else current.entry = .{ .block = lp.body };
|
||||
continue :scan;
|
||||
},
|
||||
.loop_back => |frame| if (!frame.was_seen and after.?.seen) {
|
||||
// Resume the old full-loop read query in this same driver.
|
||||
current = .{ .mode = .read, .entry = .{ .loop = frame.loop } };
|
||||
continue :scan;
|
||||
},
|
||||
.region => |region| {
|
||||
current.entry = switch (region) {
|
||||
.loop => |lp| .{ .loop = lp },
|
||||
.@"if" => |x| .{ .conditional = x },
|
||||
.match => |x| .{ .match = x },
|
||||
.select => |x| .{ .select = x },
|
||||
.lock => |b| blk: {
|
||||
try self.push(a, m, .{ .block = b[1] });
|
||||
break :blk .{ .block = b[0] };
|
||||
},
|
||||
};
|
||||
continue :scan;
|
||||
},
|
||||
}
|
||||
current = self.work.pop() orelse return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
+863
-338
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user