verifier: cache structural properties and nominal size queries
This commit is contained in:
1 parent
41f3afbf05
commit
48b9b42fed
31 files changed
+1399
-166
No files matched your search
+72
-10
@@ -5,6 +5,7 @@
|
||||
|
||||
const std = @import("std");
|
||||
const air = @import("../air.zig");
|
||||
const air_module = @import("../air/module.zig");
|
||||
const verifier = @import("../verifier.zig");
|
||||
|
||||
pub const Limits = struct {
|
||||
@@ -24,30 +25,63 @@ pub const Limits = struct {
|
||||
/// reported later (E1003) and count as 0 here; `void`, `never` and storage
|
||||
/// types are not materializable and count as 0.
|
||||
pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
// Read-only cache lookup never allocates; the uncached path remains an oracle.
|
||||
return sizeQuery(m, t, depth, null) catch unreachable;
|
||||
}
|
||||
|
||||
pub const SizeMemo = struct {
|
||||
arena: std.mem.Allocator,
|
||||
values: air_module.SizeCache = .{},
|
||||
evaluations: usize = 0,
|
||||
|
||||
// Preserve the legacy depth cutoff exactly: at most V * (V + 2)
|
||||
// nominal/depth states, rather than exponentially many shared paths.
|
||||
|
||||
pub fn size(cache: *SizeMemo, m: air.Module, t: air.Type, depth: usize) std.mem.Allocator.Error!?u64 {
|
||||
return sizeQuery(m, t, depth, cache);
|
||||
}
|
||||
};
|
||||
|
||||
fn sizeQuery(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem.Allocator.Error!?u64 {
|
||||
if (depth > m.types.len + 1) return 0;
|
||||
const key: air_module.SizeKey = .{ .index = if (t == .named) t.named else 0, .depth = depth };
|
||||
if (t == .named) {
|
||||
if (memo) |cache| {
|
||||
if (cache.values.get(key)) |value| return value;
|
||||
cache.evaluations += 1;
|
||||
} else if (m.accounted_sizes) |cache| {
|
||||
if (cache.get(key)) |value| return value;
|
||||
}
|
||||
}
|
||||
const result = try sizeValue(m, t, depth, memo);
|
||||
if (t == .named) if (memo) |cache| try cache.values.put(cache.arena, key, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn sizeValue(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem.Allocator.Error!?u64 {
|
||||
return switch (t) {
|
||||
.int => |it| it.bits() / 8,
|
||||
.bool => 1,
|
||||
.ref, .mut, .cap, .resource, .own, .region, .scope, .task, .shield, .sender, .receiver, .guard, .island, .frozen, .arc => 8,
|
||||
.iref => 16,
|
||||
.atomic => |inner| accountedSize(m, inner.*, depth + 1),
|
||||
.mutex => |inner| std.math.add(u64, 8, accountedSize(m, inner.*, depth + 1) orelse return null) catch null,
|
||||
.atomic => |inner| (try sizeQuery(m, inner.*, depth + 1, memo)),
|
||||
.mutex => |inner| std.math.add(u64, 8, (try sizeQuery(m, inner.*, depth + 1, memo)) orelse return null) catch null,
|
||||
.own_array => 16,
|
||||
.buffer => 24,
|
||||
.slice, .slice_mut => 16,
|
||||
.builtin => |b| tagSize(b.variants().len),
|
||||
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
|
||||
.array => |a| std.math.mul(u64, a.len, (try sizeQuery(m, a.elem, depth + 1, memo)) orelse return null) catch null,
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| blk: {
|
||||
var sum: u64 = 0;
|
||||
for (fs) |f| sum = std.math.add(u64, sum, accountedSize(m, f.ty, depth + 1) orelse return null) catch return null;
|
||||
for (fs) |f| sum = std.math.add(u64, sum, (try sizeQuery(m, f.ty, depth + 1, memo)) orelse return null) catch return null;
|
||||
break :blk sum;
|
||||
},
|
||||
.@"enum" => |vs| blk: {
|
||||
var max: u64 = 0;
|
||||
for (vs) |v| {
|
||||
var sum: u64 = 0;
|
||||
for (v.payload) |p| sum = std.math.add(u64, sum, accountedSize(m, p, depth + 1) orelse return null) catch return null;
|
||||
for (v.payload) |p| sum = std.math.add(u64, sum, (try sizeQuery(m, p, depth + 1, memo)) orelse return null) catch return null;
|
||||
max = @max(max, sum);
|
||||
}
|
||||
break :blk std.math.add(u64, tagSize(vs.len), max) catch null;
|
||||
@@ -66,7 +100,9 @@ fn tagSize(variants: usize) u64 {
|
||||
}
|
||||
|
||||
pub fn check(c: *verifier.Context) !void {
|
||||
try checkValueSizes(c);
|
||||
c.module.accounted_sizes = null;
|
||||
var memo: SizeMemo = .{ .arena = c.arena };
|
||||
try checkValueSizes(c, &memo);
|
||||
const m = c.module;
|
||||
const origin: air.Span = .{ .line = 1, .col = 1 };
|
||||
try limit(c, origin, "functions per module", Limits.functions, m.functions.len);
|
||||
@@ -97,11 +133,14 @@ pub fn check(c: *verifier.Context) !void {
|
||||
try limit(c, f.span, "instructions per function", Limits.insts, n);
|
||||
var frame: ?u64 = 0;
|
||||
for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .local_alloc) {
|
||||
const size = accountedSize(m, inst.op.local_alloc, 0);
|
||||
const size = try memo.size(m, inst.op.local_alloc, 0);
|
||||
frame = if (frame != null and size != null) std.math.add(u64, frame.?, size.?) catch null else null;
|
||||
};
|
||||
try limit(c, f.span, "local frame bytes", Limits.local_frame, frame);
|
||||
}
|
||||
const cached = try c.arena.create(air_module.SizeCache);
|
||||
cached.* = memo.values;
|
||||
c.module.accounted_sizes = cached;
|
||||
}
|
||||
|
||||
/// `found == null` means the size computation overflowed.
|
||||
@@ -119,12 +158,13 @@ fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found
|
||||
/// first occurrence.
|
||||
const ValueSizes = struct {
|
||||
c: *verifier.Context,
|
||||
memo: *SizeMemo,
|
||||
reported: std.StringHashMapUnmanaged(void) = .empty,
|
||||
|
||||
fn visit(v: *ValueSizes, span: air.Span, t: air.Type) !void {
|
||||
const m = v.c.module;
|
||||
if (t != .local and t != .void and t != .never) {
|
||||
const size = accountedSize(m, t, 0);
|
||||
const size = try v.memo.size(m, t, 0);
|
||||
if (size == null or size.? > Limits.value_size) {
|
||||
const name = try v.c.typeName(t);
|
||||
const gop = try v.reported.getOrPut(v.c.arena, name);
|
||||
@@ -141,8 +181,8 @@ const ValueSizes = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn checkValueSizes(c: *verifier.Context) !void {
|
||||
var v: ValueSizes = .{ .c = c };
|
||||
fn checkValueSizes(c: *verifier.Context, memo: *SizeMemo) !void {
|
||||
var v: ValueSizes = .{ .c = c, .memo = memo };
|
||||
const m = c.module;
|
||||
for (m.types, 0..) |d, i| try v.visit(d.span, .{ .named = @intCast(i) });
|
||||
for (m.functions) |f| {
|
||||
@@ -168,6 +208,28 @@ test "too many parameters" {
|
||||
try verifier.expectCodes(src.items, &.{"E5101"});
|
||||
}
|
||||
|
||||
test "accounted sizes memoize shared diamonds and cyclic depth states" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const ds = try a.alloc(air.TypeDecl, 65);
|
||||
for (ds, 0..) |*d, i| d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = if (i == 64) &.{} else try a.dupe(air_module.Field, &.{ .{ .name = "a", .ty = .{ .named = @intCast(i + 1) } }, .{ .name = "b", .ty = .{ .named = @intCast(i + 1) } } }) } };
|
||||
var m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), try memo.size(m, .{ .named = @intCast(i) }, 0));
|
||||
try std.testing.expect(memo.evaluations <= 65 * 67);
|
||||
const before = memo.evaluations;
|
||||
m.accounted_sizes = &memo.values;
|
||||
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), accountedSize(m, .{ .named = @intCast(i) }, 0));
|
||||
try std.testing.expectEqual(before, memo.evaluations);
|
||||
// A cyclic, branching size query keeps the legacy depth cutoff exactly.
|
||||
ds[64].kind = .{ .@"struct" = try a.dupe(air_module.Field, &.{ .{ .name = "a", .ty = .{ .named = 0 } }, .{ .name = "b", .ty = .{ .named = 0 } } }) };
|
||||
m.accounted_sizes = null;
|
||||
var cyclic: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, 0), try cyclic.size(m, .{ .named = 0 }, 0));
|
||||
try std.testing.expect(cyclic.evaluations <= 65 * 67);
|
||||
}
|
||||
|
||||
test "local frame limit" {
|
||||
const h = "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\n";
|
||||
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<u8, 1048576>\n ret\n", &.{});
|
||||
|
||||
+8
-108
@@ -12,6 +12,7 @@ const Context = verifier.Context;
|
||||
const Type = air.Type;
|
||||
|
||||
pub fn check(c: *Context) !void {
|
||||
c.module = try air.properties.prepare(c.arena, c.module, null);
|
||||
try checkDeclarations(c);
|
||||
if (c.diags.items.len != 0) return;
|
||||
const all = try c.arena.alloc([]const Type, c.module.functions.len);
|
||||
@@ -157,19 +158,7 @@ fn argumentLengths(c: *Context, span: air.Span, t: Type) !void {
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
fn zeroable(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| zeroable(m, a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!zeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
const zeroable = air.properties.isZeroable;
|
||||
|
||||
// ---------------------------------------------------------------- functions
|
||||
|
||||
@@ -379,66 +368,16 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
|
||||
}
|
||||
|
||||
/// Structural Copy query for source constraints; borrows remain second-class.
|
||||
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| aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
pub const isCopy = air.properties.isCopy;
|
||||
|
||||
/// Local is context-bound state, not the complement of Send (spec 15 §15).
|
||||
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| 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 const isLocalType = air.properties.isLocalType;
|
||||
|
||||
/// `Send` (spec 04 §35): may move to another task.
|
||||
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| aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => false,
|
||||
};
|
||||
}
|
||||
pub const isSend = air.properties.isSend;
|
||||
|
||||
/// `Share` (spec 04 §35): may be observed concurrently through shared borrows.
|
||||
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| aggregateAll(m, i, isShare),
|
||||
};
|
||||
}
|
||||
pub const isShare = air.properties.isShare;
|
||||
|
||||
/// A borrow whose pointee is an atomic cell: reached only atomically.
|
||||
fn atomicPointee(t: Type) bool {
|
||||
@@ -479,26 +418,7 @@ pub fn isAtomicScalar(t: Type) bool {
|
||||
|
||||
/// `IslandSafe` (spec 02 §6): plain data and `iref` handles, at any depth.
|
||||
/// `ArcSafe` is the same set in format 0.
|
||||
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| aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
return switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
pub const isIslandSafe = air.properties.isIslandSafe;
|
||||
|
||||
/// `task.spawn` (spec 04 §33.2, §35): arguments like a call, Send/Share
|
||||
/// rules per argument, and a borrow-free, Send result.
|
||||
@@ -558,27 +478,7 @@ fn checkElement(fc: *FnCtx, span: air.Span, t: Type) !void {
|
||||
|
||||
/// Whether `t` transitively contains an endpoint, or a borrow or
|
||||
/// function-bound value.
|
||||
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| 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,
|
||||
};
|
||||
}
|
||||
const contains = air.properties.contains;
|
||||
|
||||
/// `select` (spec 04 §39): each arm is a channel operation; each target
|
||||
/// takes its own parameters, then the send values not sent on its edge.
|
||||
|
||||
Reference in new issue
Block a user