authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-04-08 21:21:11-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-20 20:28:04+01:00
log6f210b74eeff0557fffb034386f4e5bb992daba0
tree66a7a523605bdcafc111c7810812ed10c299ec43
parent8258530c39c347ca83fdbe7575460712960a83f3
signature Commit is signed but in an unrecognized format.

print_air: allow dumping air without liveness

This is useful for debug printing air when liveness is broken.

1 files changed, 30 insertions(+), 20 deletions(-)

src/print_air.zig+30-20
......@@ -8,16 +8,16 @@ const Type = @import("type.zig").Type;
88const Air = @import("Air.zig");
99const Liveness = @import("Liveness.zig");
1010
11pub fn write(stream: anytype, module: *Module, air: Air, liveness: Liveness) void {
11pub fn write(stream: anytype, module: *Module, air: Air, liveness: ?Liveness) void {
1212 const instruction_bytes = air.instructions.len *
1313 // Here we don't use @sizeOf(Air.Inst.Data) because it would include
1414 // the debug safety tag but we want to measure release size.
1515 (@sizeOf(Air.Inst.Tag) + 8);
1616 const extra_bytes = air.extra.len * @sizeOf(u32);
1717 const values_bytes = air.values.len * @sizeOf(Value);
18 const tomb_bytes = liveness.tomb_bits.len * @sizeOf(usize);
19 const liveness_extra_bytes = liveness.extra.len * @sizeOf(u32);
20 const liveness_special_bytes = liveness.special.count() * 8;
18 const tomb_bytes = if (liveness) |l| l.tomb_bits.len * @sizeOf(usize) else 0;
19 const liveness_extra_bytes = if (liveness) |l| l.extra.len * @sizeOf(u32) else 0;
20 const liveness_special_bytes = if (liveness) |l| l.special.count() * 8 else 0;
2121 const total_bytes = @sizeOf(Air) + instruction_bytes + extra_bytes +
2222 values_bytes + @sizeOf(Liveness) + liveness_extra_bytes +
2323 liveness_special_bytes + tomb_bytes;
......@@ -38,8 +38,8 @@ pub fn write(stream: anytype, module: *Module, air: Air, liveness: Liveness) voi
3838 air.extra.len, fmtIntSizeBin(extra_bytes),
3939 air.values.len, fmtIntSizeBin(values_bytes),
4040 fmtIntSizeBin(tomb_bytes),
41 liveness.extra.len, fmtIntSizeBin(liveness_extra_bytes),
42 liveness.special.count(), fmtIntSizeBin(liveness_special_bytes),
41 if (liveness) |l| l.extra.len else 0, fmtIntSizeBin(liveness_extra_bytes),
42 if (liveness) |l| l.special.count() else 0, fmtIntSizeBin(liveness_special_bytes),
4343 }) catch return;
4444 // zig fmt: on
4545
......@@ -61,7 +61,7 @@ pub fn writeInst(
6161 inst: Air.Inst.Index,
6262 module: *Module,
6363 air: Air,
64 liveness: Liveness,
64 liveness: ?Liveness,
6565) void {
6666 var writer: Writer = .{
6767 .module = module,
......@@ -74,11 +74,11 @@ pub fn writeInst(
7474 writer.writeInst(stream, inst) catch return;
7575}
7676
77pub fn dump(module: *Module, air: Air, liveness: Liveness) void {
77pub fn dump(module: *Module, air: Air, liveness: ?Liveness) void {
7878 write(std.io.getStdErr().writer(), module, air, liveness);
7979}
8080
81pub fn dumpInst(inst: Air.Inst.Index, module: *Module, air: Air, liveness: Liveness) void {
81pub fn dumpInst(inst: Air.Inst.Index, module: *Module, air: Air, liveness: ?Liveness) void {
8282 writeInst(std.io.getStdErr().writer(), inst, module, air, liveness);
8383}
8484
......@@ -86,7 +86,7 @@ const Writer = struct {
8686 module: *Module,
8787 gpa: Allocator,
8888 air: Air,
89 liveness: Liveness,
89 liveness: ?Liveness,
9090 indent: usize,
9191 skip_body: bool,
9292
......@@ -109,7 +109,7 @@ const Writer = struct {
109109 try s.writeByteNTimes(' ', w.indent);
110110 try s.print("%{d}{c}= {s}(", .{
111111 inst,
112 @as(u8, if (w.liveness.isUnused(inst)) '!' else ' '),
112 @as(u8, if (if (w.liveness) |liveness| liveness.isUnused(inst) else false) '!' else ' '),
113113 @tagName(tag),
114114 });
115115 switch (tag) {
......@@ -773,7 +773,10 @@ const Writer = struct {
773773 const extra = w.air.extraData(Air.CondBr, pl_op.payload);
774774 const then_body = w.air.extra[extra.end..][0..extra.data.then_body_len];
775775 const else_body = w.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
776 const liveness_condbr = w.liveness.getCondBr(inst);
776 const liveness_condbr = if (w.liveness) |liveness|
777 liveness.getCondBr(inst)
778 else
779 Liveness.CondBrSlices{ .then_deaths = &.{}, .else_deaths = &.{} };
777780
778781 try w.writeOperand(s, inst, 0, pl_op.operand);
779782 if (w.skip_body) return s.writeAll(", ...");
......@@ -813,8 +816,15 @@ const Writer = struct {
813816 fn writeSwitchBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
814817 const pl_op = w.air.instructions.items(.data)[inst].pl_op;
815818 const switch_br = w.air.extraData(Air.SwitchBr, pl_op.payload);
816 const liveness = w.liveness.getSwitchBr(w.gpa, inst, switch_br.data.cases_len + 1) catch
817 @panic("out of memory");
819 const liveness = if (w.liveness) |liveness|
820 liveness.getSwitchBr(w.gpa, inst, switch_br.data.cases_len + 1) catch
821 @panic("out of memory")
822 else blk: {
823 const slice = w.gpa.alloc([]const Air.Inst.Index, switch_br.data.cases_len + 1) catch
824 @panic("out of memory");
825 std.mem.set([]const Air.Inst.Index, slice, &.{});
826 break :blk Liveness.SwitchBrTable{ .deaths = slice };
827 };
818828 defer w.gpa.free(liveness.deaths);
819829 var extra_index: usize = switch_br.end;
820830 var case_i: u32 = 0;
......@@ -904,13 +914,13 @@ const Writer = struct {
904914 operand: Air.Inst.Ref,
905915 ) @TypeOf(s).Error!void {
906916 const small_tomb_bits = Liveness.bpi - 1;
907 const dies = if (op_index < small_tomb_bits)
908 w.liveness.operandDies(inst, @intCast(Liveness.OperandInt, op_index))
909 else blk: {
910 var extra_index = w.liveness.special.get(inst).?;
917 const dies = if (w.liveness) |liveness| blk: {
918 if (op_index < small_tomb_bits)
919 break :blk liveness.operandDies(inst, @intCast(Liveness.OperandInt, op_index));
920 var extra_index = liveness.special.get(inst).?;
911921 var tomb_op_index: usize = small_tomb_bits;
912922 while (true) {
913 const bits = w.liveness.extra[extra_index];
923 const bits = liveness.extra[extra_index];
914924 if (op_index < tomb_op_index + 31) {
915925 break :blk @truncate(u1, bits >> @intCast(u5, op_index - tomb_op_index)) != 0;
916926 }
......@@ -918,7 +928,7 @@ const Writer = struct {
918928 extra_index += 1;
919929 tomb_op_index += 31;
920930 }
921 };
931 } else false;
922932 return w.writeInstRef(s, operand, dies);
923933 }
924934