air: evaluate anonymous type properties iteratively
This commit is contained in:
1 parent
b0cd7d74ff
commit
d0fcc3d473
5 files changed
+304
-118
No files matched your search
+268
-104
@@ -56,10 +56,10 @@ pub fn prepare(a: std.mem.Allocator, input: air.Module, stats: ?*Stats) !air.Mod
|
||||
|
||||
// Include phantom edges for height. This is a dependency superset for the
|
||||
// boolean predicates, whose unchanged tag rules decide which inners matter.
|
||||
fn dependencies(a: std.mem.Allocator, reverse: []std.ArrayList(u32), parent: u32, t: Type) !void {
|
||||
if (t == .named) {
|
||||
try reverse[t.named].append(a, parent);
|
||||
} else if (t.inner()) |inner| try dependencies(a, reverse, parent, inner);
|
||||
fn dependencies(a: std.mem.Allocator, reverse: []std.ArrayList(u32), parent: u32, initial: Type) !void {
|
||||
var t = initial;
|
||||
while (t != .named) t = t.inner() orelse return;
|
||||
try reverse[t.named].append(a, parent);
|
||||
}
|
||||
|
||||
fn accumulate(m: air.Module, out: *air.NominalProperties, t: Type, stats: ?*Stats) void {
|
||||
@@ -79,18 +79,35 @@ fn accumulate(m: air.Module, out: *air.NominalProperties, t: Type, stats: ?*Stat
|
||||
|
||||
/// Match the frontend's existing element edges and depth-32 witness guard.
|
||||
/// A nearest-leaf distance permits pruning without changing field order.
|
||||
pub fn sourceForbiddenDepth(m: air.Module, t: Type) u6 {
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => 0,
|
||||
.array, .own, .own_array, .buffer, .mutex => @intCast(@min(34, @as(u8, sourceForbiddenDepth(m, t.inner().?)) + 1)),
|
||||
.named => |i| m.nominal_properties.?[i].source_forbidden_depth,
|
||||
else => 34,
|
||||
};
|
||||
pub fn sourceForbiddenDepth(m: air.Module, initial: Type) u6 {
|
||||
var t = initial;
|
||||
var edges: u6 = 0;
|
||||
while (true) {
|
||||
const leaf: u6 = switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => 0,
|
||||
.array, .own, .own_array, .buffer, .mutex => {
|
||||
edges += 1;
|
||||
if (edges == 34) return 34;
|
||||
t = t.inner().?;
|
||||
continue;
|
||||
},
|
||||
.named => |i| m.nominal_properties.?[i].source_forbidden_depth,
|
||||
else => 34,
|
||||
};
|
||||
return @intCast(@min(34, @as(u8, edges) + leaf));
|
||||
}
|
||||
}
|
||||
|
||||
fn height(m: air.Module, t: Type) u7 {
|
||||
if (t == .named) return m.nominal_properties.?[t.named].height;
|
||||
return if (t.inner()) |inner| @intCast(@min(65, @as(u8, height(m, inner)) + 1)) else 1;
|
||||
fn height(m: air.Module, initial: Type) u7 {
|
||||
var t = initial;
|
||||
var edges: u7 = 0;
|
||||
while (t != .named) {
|
||||
const inner = t.inner() orelse return @intCast(@min(65, @as(u8, edges) + 1));
|
||||
edges += 1;
|
||||
if (edges == 65) return 65;
|
||||
t = inner;
|
||||
}
|
||||
return @intCast(@min(65, @as(u8, edges) + m.nominal_properties.?[t.named].height));
|
||||
}
|
||||
|
||||
/// The preceding depth guard, with cached nominal height and identical edges.
|
||||
@@ -111,85 +128,127 @@ pub fn bounded(m: air.Module, t: Type, depth: usize) bool {
|
||||
return if (t.inner()) |inner| bounded(m, inner, depth + 1) else true;
|
||||
}
|
||||
|
||||
pub fn isZeroable(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| isZeroable(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].zeroable else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!isZeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isCopy(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| isCopy(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].copy else aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isLocalType(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| isLocalType(m, inner.*),
|
||||
.array => |a| isLocalType(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].local else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
pub fn isZeroable(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].zeroable else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!isZeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isCopy(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].copy else aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isLocalType(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| {
|
||||
t = inner.*;
|
||||
continue;
|
||||
},
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].local else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => true,
|
||||
.own, .own_array, .buffer, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].send else aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => false,
|
||||
};
|
||||
return sendShare(m, t, .send);
|
||||
}
|
||||
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.frozen, .arc, .iref, .atomic => true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| isSend(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].share else aggregateAll(m, i, isShare),
|
||||
};
|
||||
return sendShare(m, t, .share);
|
||||
}
|
||||
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| isIslandSafe(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].island_safe else aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
/// Borrow and mutex edges can change the property being queried. Keep that
|
||||
/// state explicit so alternating ref/mutex chains do not grow the native stack.
|
||||
fn sendShare(m: air.Module, initial: Type, initial_mode: enum { send, share }) bool {
|
||||
var t = initial;
|
||||
var mode = initial_mode;
|
||||
while (true) {
|
||||
if (mode == .send) switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => return true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => return true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => return true,
|
||||
.own, .own_array, .buffer, .mutex, .mut, .slice_mut => |inner| t = inner.*,
|
||||
.ref, .slice => |inner| {
|
||||
t = inner.*;
|
||||
mode = .share;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].send else aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => return false,
|
||||
} else switch (t) {
|
||||
.int, .bool, .builtin, .cap => return true,
|
||||
.frozen, .arc, .iref, .atomic => return true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => return false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => return false,
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| t = inner.*,
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| {
|
||||
t = inner.*;
|
||||
mode = .send;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].share else aggregateAll(m, i, isShare),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isIslandSafe(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].island_safe else aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
@@ -204,26 +263,32 @@ pub fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
// An `iref<T>` holds no `T`: types may recurse through it (spec 03 §22).
|
||||
if (t == .iref) return false;
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) return contains(m, inner, what);
|
||||
return switch (t) {
|
||||
.named => |i| if (m.nominal_properties) |p| @field(p[i], @tagName(what)) else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
pub fn contains(m: air.Module, initial: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
// An `iref<T>` holds no `T`: types may recurse through it (spec 03 §22).
|
||||
if (t == .iref) return false;
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) {
|
||||
t = inner;
|
||||
continue;
|
||||
};
|
||||
return switch (t) {
|
||||
.named => |i| if (m.nominal_properties) |p| @field(p[i], @tagName(what)) else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
test "shared nominal diamonds have counted bounded property queries" {
|
||||
@@ -345,3 +410,102 @@ test "cyclic existential facts reach all predecessors without a false cache" {
|
||||
try std.testing.expect(!isCopy(cached, t) and !isShare(cached, t));
|
||||
}
|
||||
}
|
||||
|
||||
fn deepArrays(a: std.mem.Allocator, leaf: Type) !Type {
|
||||
const rows = try a.alloc(Type.Array, 32_768);
|
||||
var t = leaf;
|
||||
for (rows) |*row| {
|
||||
row.* = .{ .elem = t, .len = 1 };
|
||||
t = .{ .array = row };
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
test "deep anonymous property queries and nominal preparation avoid recursion" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const u: Type = .{ .int = .u8 };
|
||||
const plain = try deepArrays(a, u);
|
||||
const task = try deepArrays(a, .{ .task = &u });
|
||||
const endpoint = try deepArrays(a, .{ .sender = &u });
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{
|
||||
.{ .name = "Plain", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = plain }} } },
|
||||
.{ .name = "Task", .span = .{ .line = 2, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = task }} } },
|
||||
.{ .name = "Endpoint", .span = .{ .line = 3, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = endpoint }} } },
|
||||
}, .functions = &.{} };
|
||||
const cached = try prepare(a, m, null);
|
||||
for ([_]air.Module{ m, cached }) |module| {
|
||||
try std.testing.expect(!module.isLinearType(plain));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(pred(module, plain));
|
||||
try std.testing.expect(!isLocalType(module, plain) and !contains(module, plain, .forbidden) and !contains(module, plain, .endpoint));
|
||||
try std.testing.expect(module.isLinearType(task) and isLocalType(module, task) and contains(module, task, .forbidden));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(!pred(module, task));
|
||||
try std.testing.expect(module.isLinearType(endpoint) and isSend(module, endpoint) and contains(module, endpoint, .endpoint));
|
||||
try std.testing.expect(!isLocalType(module, endpoint) and !isShare(module, endpoint) and !contains(module, endpoint, .forbidden));
|
||||
}
|
||||
for ([_]Type{ plain, task, endpoint }) |t| {
|
||||
try std.testing.expectEqual(@as(u7, 65), height(cached, t));
|
||||
try std.testing.expectEqual(@as(u6, 34), sourceForbiddenDepth(cached, t));
|
||||
}
|
||||
const p = cached.nominal_properties.?;
|
||||
try std.testing.expect(p[0].copy and p[0].zeroable and p[0].send and p[0].share and p[0].island_safe and !p[0].linear);
|
||||
try std.testing.expect(p[1].linear and p[1].local and p[1].forbidden and !p[1].send and !p[1].copy);
|
||||
try std.testing.expect(p[2].endpoint and p[2].linear and p[2].send and !p[2].share);
|
||||
}
|
||||
|
||||
test "deep nominal dependency through phantom anonymous handles is retained" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const nodes = try a.alloc(Type, 32_768);
|
||||
var t: Type = .{ .named = 0 };
|
||||
for (nodes) |*node| {
|
||||
node.* = t;
|
||||
t = .{ .iref = node };
|
||||
}
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Node", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "next", .ty = t }} } }}, .functions = &.{} };
|
||||
const cached = try prepare(a, m, null);
|
||||
const p = cached.nominal_properties.?[0];
|
||||
try std.testing.expect(p.copy and p.send and p.share and p.island_safe and !p.linear and !p.endpoint and !p.forbidden);
|
||||
try std.testing.expectEqual(@as(u7, 65), p.height);
|
||||
}
|
||||
|
||||
test "deep Send Share transitions preserve ref and mutex modes" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
|
||||
for ([_]bool{ false, true }) |outer_ref| {
|
||||
const nodes = try a.alloc(Type, 65_536);
|
||||
var t: Type = .region;
|
||||
for (nodes, 0..) |*node, i| {
|
||||
node.* = t;
|
||||
t = if ((i % 2 == 1) == outer_ref) .{ .ref = node } else .{ .mutex = node };
|
||||
}
|
||||
try std.testing.expectEqual(outer_ref, isSend(m, t));
|
||||
try std.testing.expectEqual(outer_ref, isShare(m, t));
|
||||
}
|
||||
}
|
||||
|
||||
test "height and source witness distance retain saturation boundaries" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const rows = try a.alloc(Type.Array, 66);
|
||||
const properties = [_]air.NominalProperties{.{}};
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{}, .nominal_properties = &properties };
|
||||
var named: Type = .{ .named = 0 };
|
||||
for (rows, 1..) |*row, edges| {
|
||||
row.* = .{ .elem = named, .len = 1 };
|
||||
named = .{ .array = row };
|
||||
try std.testing.expectEqual(@as(u7, @intCast(@min(65, edges + 1))), height(m, named));
|
||||
}
|
||||
const u: Type = .{ .int = .u8 };
|
||||
var forbidden: Type = .{ .ref = &u };
|
||||
for (rows, 1..) |*row, edges| {
|
||||
row.* = .{ .elem = forbidden, .len = 1 };
|
||||
forbidden = .{ .array = row };
|
||||
try std.testing.expectEqual(@as(u6, @intCast(@min(34, edges))), sourceForbiddenDepth(m, forbidden));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user