//! Initialization of frame storage (spec 08 ยง13). Reports E1010. //! //! Must-initialized dataflow over reachable blocks: a local is initialized //! at a point if `local.store` or `local.zero` executed on every path to it. const std = @import("std"); const air = @import("../air.zig"); const verifier = @import("../verifier.zig"); const ssa = @import("ssa.zig"); const Context = verifier.Context; pub fn check(c: *Context) !void { for (c.module.functions) |f| try checkFunction(c, f); } fn checkFunction(c: *Context, f: air.Function) !void { // Number the locals of the function. const slot = try c.arena.alloc(?u32, f.values.len); @memset(slot, null); var n: u32 = 0; for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .local_alloc) { slot[inst.result.?] = n; n += 1; }; if (n == 0) return; const dom = try ssa.dominators(c.arena, f); const preds = try predecessors(c.arena, f); // in[b]: locals initialized at entry of b; null until computed. const in = try c.arena.alloc(?[]bool, f.blocks.len); @memset(in, null); const out = try c.arena.alloc([]bool, f.blocks.len); for (out) |*o| { o.* = try c.arena.alloc(bool, n); @memset(o.*, true); // top element for the intersection } // Iterate to a fixpoint; the lattice is finite and transfer is monotone. var changed = true; while (changed) { changed = false; for (dom.rpo) |b| { const state = try c.arena.alloc(bool, n); if (b == 0) { @memset(state, false); } else { @memset(state, true); for (preds[b]) |p| if (dom.reachable(p)) { for (state, out[p]) |*s, o| s.* = s.* and o; }; } in[b] = state; const new = try c.arena.dupe(bool, state); for (f.blocks[b].insts) |inst| transfer(inst.op, slot, new); if (!std.mem.eql(bool, new, out[b])) { out[b] = new; changed = true; } } } // Report reads of locals that are not definitely initialized. for (dom.rpo) |b| { const state = try c.arena.dupe(bool, in[b].?); for (f.blocks[b].insts, 0..) |inst, ii| { const read: ?air.ValueId = switch (inst.op) { .local_load, .borrow_ref, .borrow_mut => |v| v, else => null, }; if (read) |v| if (slot[v]) |s| if (!state[s]) { try c.report("E1010", inst.span, "local read or borrowed before it is initialized on every path", &.{ .{ .name = "function", .value = .{ .str = f.name } }, .{ .name = "block", .value = .{ .str = f.blocks[b].name } }, .{ .name = "inst", .value = .{ .int = @intCast(ii) } }, .{ .name = "local", .value = .{ .str = try c.valueName(f, v) } }, }); }; transfer(inst.op, slot, state); } } } fn transfer(op: air.Op, slot: []const ?u32, state: []bool) void { switch (op) { .local_store => |x| if (slot[x.local]) |s| { state[s] = true; }, .local_zero => |v| if (slot[v]) |s| { state[s] = true; }, else => {}, } } pub fn predecessors(arena: std.mem.Allocator, f: air.Function) ![]const []const u32 { var lists = try arena.alloc(std.ArrayList(u32), f.blocks.len); for (lists) |*l| l.* = .empty; var succ: std.ArrayList(u32) = .empty; for (f.blocks, 0..) |b, i| { succ.clearRetainingCapacity(); try air.successors(b.insts[b.insts.len - 1].op, &succ, arena); for (succ.items) |s| { // A block reached twice from the same terminator has one predecessor. if (std.mem.indexOfScalar(u32, lists[s].items, @intCast(i)) == null) try lists[s].append(arena, @intCast(i)); } } const out = try arena.alloc([]const u32, f.blocks.len); for (lists, out) |l, *o| o.* = l.items; return out; } const h = "module t\nair 0\nlang 0.1\nfeatures [ownership]\n"; test "initialization" { try verifier.expectCodes(h ++ "fn @f() -> u8\neffects []\nentry:\n %x = local.alloc u8\n %v = local.load %x\n ret %v\n", &.{"E1010"}); try verifier.expectCodes(h ++ "fn @f() -> u8\neffects []\nentry:\n %x = local.alloc u8\n local.zero %x\n %v = local.load %x\n ret %v\n", &.{}); // Initialized on one path only. try verifier.expectCodes(h ++ \\fn @f(%c: bool) -> u8 \\effects [] \\entry: \\ %x = local.alloc u8 \\ cbr %c, @a(), @b() \\a: \\ %one = const.u8 1 \\ local.store %x, %one \\ br @join() \\b: \\ br @join() \\join: \\ %v = local.load %x \\ ret %v \\ , &.{"E1010"}); // Initialized on both paths. try verifier.expectCodes(h ++ \\fn @f(%c: bool) -> u8 \\effects [] \\entry: \\ %x = local.alloc u8 \\ cbr %c, @a(), @b() \\a: \\ %one = const.u8 1 \\ local.store %x, %one \\ br @join() \\b: \\ local.zero %x \\ br @join() \\join: \\ %r = borrow.ref %x \\ %v = load %r \\ borrow.end %r \\ ret %v \\ , &.{}); // Initialization inside a loop body does not reach the loop header. try verifier.expectCodes(h ++ \\fn @f(%c: bool) -> u8 \\effects [] \\entry: \\ %x = local.alloc u8 \\ br @loop() \\loop: \\ %v = local.load %x \\ local.zero %x \\ cbr %c, @loop(), @out() \\out: \\ ret %v \\ , &.{"E1010"}); }