cli: emit source SSA zero iteratively
This commit is contained in:
1 parent
c1bf99af83
commit
f5e2af15e8
4 files changed
+194
-11
No files matched your search
+178
-9
@@ -2361,16 +2361,35 @@ const Lowerer = struct {
|
||||
return st;
|
||||
}
|
||||
|
||||
fn zeroValue(l: *Lowerer, ty: Ty, base: []const u8, span: Span) Error!Value {
|
||||
return switch (ty) {
|
||||
.int => |n| l.def(base, ty, try l.fmt("const.{s} 0", .{n}), span),
|
||||
.bool => l.def(base, ty, "const.bool false", span),
|
||||
.array => |a| blk: {
|
||||
const z = try l.zeroValue(a.elem, base, span);
|
||||
break :blk l.def(base, ty, try l.fmt("array.splat {s} %{s}", .{ try l.tyText(ty), z.name }), span);
|
||||
},
|
||||
else => l.invalid(.zero_not_zeroable, span, "`zero` needs a zeroable type (integers, booleans, arrays of them)", &.{}),
|
||||
fn zeroValue(l: *Lowerer, annotation: Ty, base: []const u8, span: Span) Error!Value {
|
||||
var leaf = annotation;
|
||||
var count: usize = 0;
|
||||
while (leaf == .array) {
|
||||
count += 1;
|
||||
leaf = leaf.array.elem;
|
||||
}
|
||||
// An unsupported leaf rejects before allocating traversal metadata,
|
||||
// just as the recursive descent did before emitting any instruction.
|
||||
if (leaf != .int and leaf != .bool) return l.invalid(.zero_not_zeroable, span, "`zero` needs a zeroable type (integers, booleans, arrays of them)", &.{});
|
||||
const parents = try l.arena.alloc(*const Ty.Arr, count);
|
||||
var t = annotation;
|
||||
for (parents) |*parent| {
|
||||
parent.* = t.array;
|
||||
t = t.array.elem;
|
||||
}
|
||||
var value = switch (leaf) {
|
||||
.int => |n| try l.def(base, leaf, try l.fmt("const.{s} 0", .{n}), span),
|
||||
.bool => try l.def(base, leaf, "const.bool false", span),
|
||||
else => unreachable,
|
||||
};
|
||||
// Original leaf-first order: one constant followed by each splat,
|
||||
// retaining the source type nodes, names and source spans.
|
||||
while (count != 0) {
|
||||
count -= 1;
|
||||
const ty: Ty = .{ .array = parents[count] };
|
||||
value = try l.def(base, ty, try l.fmt("array.splat {s} %{s}", .{ try l.tyText(ty), value.name }), span);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/// A byte string in frame storage: `[u8; N]`, one store per byte (no heap).
|
||||
@@ -5048,3 +5067,153 @@ test "source linear queries walk deep arrays and module-sized feature guards" {
|
||||
try std.testing.expect(!l.needsLinearType(borrowed, 33));
|
||||
try std.testing.expect(!l.needsLinearType(.{ .named = 1 }, count + 33));
|
||||
}
|
||||
|
||||
// Independent original SSA zero lowering, used only on shallow test types.
|
||||
const ReferenceSourceZero = struct {
|
||||
fn zeroValue(l: *Lowerer, ty: Ty, base: []const u8, span: Span) Error!Value {
|
||||
return switch (ty) {
|
||||
.int => |n| l.def(base, ty, try l.fmt("const.{s} 0", .{n}), span),
|
||||
.bool => l.def(base, ty, "const.bool false", span),
|
||||
.array => |a| blk: {
|
||||
const z = try zeroValue(l, a.elem, base, span);
|
||||
break :blk l.def(base, ty, try l.fmt("array.splat {s} %{s}", .{ try l.tyText(ty), z.name }), span);
|
||||
},
|
||||
else => l.invalid(.zero_not_zeroable, span, "`zero` needs a zeroable type (integers, booleans, arrays of them)", &.{}),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
fn expectSourceZeroMatches(a: Allocator, ty: Ty) !void {
|
||||
const m: ast.Module = .{ .name = "zero", .types = &.{}, .functions = &.{} };
|
||||
const span: Span = .{ .start = .{ .line = 7, .col = 9 }, .end = .{ .line = 7, .col = 26 } };
|
||||
const boolean: ast.Type = .{ .span = span, .kind = .{ .name = "bool" } };
|
||||
const params = [_]ast.Param{ .{ .name = "z_1", .ty = boolean, .span = span }, .{ .name = "z_3", .ty = boolean, .span = span } };
|
||||
var old: Lowerer = .{ .arena = a, .m = m, .params = ¶ms, .in_constant = 12 };
|
||||
var current: Lowerer = .{ .arena = a, .m = m, .params = ¶ms, .in_constant = 12 };
|
||||
old.cur = try old.newBlock("entry", span);
|
||||
current.cur = try current.newBlock("entry", span);
|
||||
const before = ReferenceSourceZero.zeroValue(&old, ty, "z", span);
|
||||
const after = current.zeroValue(ty, "z", span);
|
||||
if (before) |value| {
|
||||
const result = try after;
|
||||
try std.testing.expectEqualStrings(value.name, result.name);
|
||||
try std.testing.expect(value.ty.eql(result.ty));
|
||||
if (ty == .array) try std.testing.expect(ty.array == result.ty.array);
|
||||
} else |err| try std.testing.expectError(err, after);
|
||||
try std.testing.expectEqualDeep(old.problem, current.problem);
|
||||
try std.testing.expectEqualDeep(old.blocks.items[0].insts.items, current.blocks.items[0].insts.items);
|
||||
try std.testing.expectEqual(old.counter, current.counter);
|
||||
try std.testing.expectEqualDeep(old.uses, current.uses);
|
||||
}
|
||||
|
||||
test "source SSA zero retains shallow instructions, names, types and errors" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
var l: Lowerer = .{ .arena = a, .m = .{ .name = "zero", .types = &.{}, .functions = &.{} } };
|
||||
var leaves: std.ArrayList(Ty) = .empty;
|
||||
for (int_types) |name| try leaves.append(a, .{ .int = name });
|
||||
try leaves.appendSlice(a, &.{ .bool, .void, .never, .region, .scope, .shield, .{ .named = 0 }, .{ .named = 123 } });
|
||||
for (authority.kinds) |kind| try leaves.append(a, .{ .cap = kind.name });
|
||||
inline for (@typeInfo(authority.Builtin).@"enum".field_names) |name| try leaves.append(a, .{ .builtin = @field(authority.Builtin, name) });
|
||||
inline for (@typeInfo(authority.Resource).@"enum".field_names) |name| try leaves.append(a, .{ .resource = @field(authority.Resource, name) });
|
||||
inline for (@typeInfo(Ty).@"union".field_names, @typeInfo(Ty).@"union".field_types) |name, FieldType| if (comptime FieldType == *const Ty) try leaves.append(a, try l.wrap(@field(std.meta.Tag(Ty), name), .bool));
|
||||
for (leaves.items) |leaf| {
|
||||
try expectSourceZeroMatches(a, leaf);
|
||||
for ([_]u64{ 0, 1, @as(u64, 1) << 32, std.math.maxInt(u64) }) |length| {
|
||||
try expectSourceZeroMatches(a, try l.arrayOf(leaf, length));
|
||||
try expectSourceZeroMatches(a, try l.arrayOf(try l.arrayOf(leaf, length), 7));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "source SSA zero rejects deep unsupported leaves without traversal allocation" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const m: ast.Module = .{ .name = "zero", .types = &.{}, .functions = &.{} };
|
||||
var builder: Lowerer = .{ .arena = a, .m = m };
|
||||
var ty: Ty = .{ .named = 0 };
|
||||
for (0..65_536) |_| ty = try builder.arrayOf(ty, 1);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
var l: Lowerer = .{ .arena = failing.allocator(), .m = m };
|
||||
const span: Span = .{ .start = .{ .line = 3, .col = 31 }, .end = .{ .line = 3, .col = 100 } };
|
||||
try std.testing.expectError(error.Lower, l.zeroValue(ty, "z", span));
|
||||
try std.testing.expectEqualStrings("E7006", l.problem.?.code);
|
||||
try std.testing.expectEqual(InvalidReason.zero_not_zeroable, l.problem.?.reason.?);
|
||||
try std.testing.expectEqual(span, l.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.problem.?.fields.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.counter);
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
try std.testing.expectEqual(@as(usize, 0), failing.allocated_bytes);
|
||||
}
|
||||
|
||||
test "source SSA zero emits every supported array in inner-to-outer order" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
var l: Lowerer = .{ .arena = a, .m = .{ .name = "zero", .types = &.{}, .functions = &.{} } };
|
||||
const span: Span = .{ .start = .{ .line = 2, .col = 3 }, .end = .{ .line = 2, .col = 9 } };
|
||||
var ty: Ty = .{ .int = "u8" };
|
||||
const count = 1024;
|
||||
for (0..count) |i| ty = try l.arrayOf(ty, i + 1);
|
||||
l.cur = try l.newBlock("entry", span);
|
||||
const value = try l.zeroValue(ty, "z", span);
|
||||
try std.testing.expect(value.ty.array == ty.array);
|
||||
try std.testing.expectEqualStrings("z_1025", value.name);
|
||||
const instructions = l.blocks.items[0].insts.items;
|
||||
try std.testing.expectEqual(@as(usize, count + 1), instructions.len);
|
||||
try std.testing.expectEqualStrings("%z_1 = const.u8 0", instructions[0].text);
|
||||
var expected_type: []const u8 = "u8";
|
||||
for (instructions[1..], 0..) |inst, i| {
|
||||
expected_type = try std.fmt.allocPrint(a, "array<{s}, {d}>", .{ expected_type, i + 1 });
|
||||
const expected = try std.fmt.allocPrint(a, "%z_{d} = array.splat {s} %z_{d}", .{ i + 2, expected_type, i + 1 });
|
||||
try std.testing.expectEqualStrings(expected, inst.text);
|
||||
}
|
||||
for (instructions) |inst| {
|
||||
try std.testing.expectEqual(@as(?Span, span), inst.span);
|
||||
try std.testing.expectEqual(@as(u32, 0), inst.constant);
|
||||
}
|
||||
}
|
||||
|
||||
test "source SSA zero OOM propagates with complete instruction prefixes and arena cleanup" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const m: ast.Module = .{ .name = "zero", .types = &.{}, .functions = &.{} };
|
||||
const span: Span = .{ .start = .{ .line = 2, .col = 3 }, .end = .{ .line = 2, .col = 9 } };
|
||||
var builder: Lowerer = .{ .arena = a, .m = m };
|
||||
var ty: Ty = .bool;
|
||||
for (0..64) |i| ty = try builder.arrayOf(ty, i + 1);
|
||||
builder.cur = try builder.newBlock("entry", span);
|
||||
_ = try ReferenceSourceZero.zeroValue(&builder, ty, "z", span);
|
||||
const expected = builder.blocks.items[0].insts.items;
|
||||
var fail_index: usize = 0;
|
||||
while (true) : (fail_index += 1) {
|
||||
try std.testing.expect(fail_index < 100);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
|
||||
var arena: std.heap.ArenaAllocator = .init(failing.allocator());
|
||||
var l: Lowerer = .{ .arena = arena.allocator(), .m = m };
|
||||
const result: Error!Value = blk: {
|
||||
l.cur = l.newBlock("entry", span) catch |err| break :blk err;
|
||||
break :blk l.zeroValue(ty, "z", span);
|
||||
};
|
||||
if (failing.has_induced_failure) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
try std.testing.expect(l.problem == null);
|
||||
} else {
|
||||
try std.testing.expect((try result).ty.eql(ty));
|
||||
try std.testing.expectEqual(@as(usize, 65), l.blocks.items[0].insts.items.len);
|
||||
}
|
||||
if (l.cur) |index| {
|
||||
const actual = l.blocks.items[index].insts.items;
|
||||
try std.testing.expect(actual.len <= expected.len);
|
||||
try std.testing.expectEqualDeep(expected[0..actual.len], actual);
|
||||
}
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
arena.deinit();
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user