authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-09 17:54:32+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-11 11:02:56+03:00
log0f820d0bdf98b3f8429a9cf2f1b405c2c0a4d958
treed8793ad6cd0258eb704d35fc536636aac3fa45fc
parent002df65b6ec55684d6bc6790ae7b0c0f4abb1375

stage2: improve debugging tools

llvm: dump failed module when -femit-llvm-ir set print_air: * print fully qualified name * use Type.fmt and Value.fmtValue, fmtDebug is useless TypedValue * handle anon structs and tuples * fix bugs

5 files changed, 114 insertions(+), 28 deletions(-)

src/Module.zig+5-2
......@@ -3790,9 +3790,12 @@ pub fn ensureFuncBodyAnalyzed(mod: *Module, func: *Fn) SemaError!void {
37903790 defer liveness.deinit(gpa);
37913791
37923792 if (builtin.mode == .Debug and mod.comp.verbose_air) {
3793 std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});
3793 const fqn = try decl.getFullyQualifiedName(mod);
3794 defer mod.gpa.free(fqn);
3795
3796 std.debug.print("# Begin Function AIR: {s}:\n", .{fqn});
37943797 @import("print_air.zig").dump(mod, air, liveness);
3795 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
3798 std.debug.print("# End Function AIR: {s}\n\n", .{fqn});
37963799 }
37973800
37983801 mod.comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) {
src/TypedValue.zig+64-11
......@@ -144,7 +144,41 @@ pub fn print(
144144 return writer.writeAll(".{ ... }");
145145 }
146146 const vals = val.castTag(.aggregate).?.data;
147 if (ty.zigTypeTag() == .Struct) {
147 if (ty.castTag(.anon_struct)) |anon_struct| {
148 const field_names = anon_struct.data.names;
149 const types = anon_struct.data.types;
150 const max_len = std.math.min(types.len, max_aggregate_items);
151
152 var i: u32 = 0;
153 while (i < max_len) : (i += 1) {
154 if (i != 0) try writer.writeAll(", ");
155 try writer.print(".{s} = ", .{field_names[i]});
156 try print(.{
157 .ty = types[i],
158 .val = vals[i],
159 }, writer, level - 1, mod);
160 }
161 if (types.len > max_aggregate_items) {
162 try writer.writeAll(", ...");
163 }
164 return writer.writeAll(" }");
165 } else if (ty.isTuple()) {
166 const fields = ty.tupleFields();
167 const max_len = std.math.min(fields.types.len, max_aggregate_items);
168
169 var i: u32 = 0;
170 while (i < max_len) : (i += 1) {
171 if (i != 0) try writer.writeAll(", ");
172 try print(.{
173 .ty = fields.types[i],
174 .val = vals[i],
175 }, writer, level - 1, mod);
176 }
177 if (fields.types.len > max_aggregate_items) {
178 try writer.writeAll(", ...");
179 }
180 return writer.writeAll(" }");
181 } else if (ty.zigTypeTag() == .Struct) {
148182 try writer.writeAll(".{ ");
149183 const struct_fields = ty.structFields();
150184 const len = struct_fields.count();
......@@ -194,7 +228,7 @@ pub fn print(
194228 try writer.writeAll(".{ ");
195229
196230 try print(.{
197 .ty = ty.unionTagType().?,
231 .ty = ty.cast(Type.Payload.Union).?.data.tag_ty,
198232 .val = union_val.tag,
199233 }, writer, level - 1, mod);
200234 try writer.writeAll(" = ");
......@@ -278,19 +312,27 @@ pub fn print(
278312 .elem_ptr => {
279313 const elem_ptr = val.castTag(.elem_ptr).?.data;
280314 try writer.writeAll("&");
281 try print(.{
282 .ty = elem_ptr.elem_ty,
283 .val = elem_ptr.array_ptr,
284 }, writer, level - 1, mod);
315 if (level == 0) {
316 try writer.writeAll("(ptr)");
317 } else {
318 try print(.{
319 .ty = elem_ptr.elem_ty,
320 .val = elem_ptr.array_ptr,
321 }, writer, level - 1, mod);
322 }
285323 return writer.print("[{}]", .{elem_ptr.index});
286324 },
287325 .field_ptr => {
288326 const field_ptr = val.castTag(.field_ptr).?.data;
289327 try writer.writeAll("&");
290 try print(.{
291 .ty = field_ptr.container_ty,
292 .val = field_ptr.container_ptr,
293 }, writer, level - 1, mod);
328 if (level == 0) {
329 try writer.writeAll("(ptr)");
330 } else {
331 try print(.{
332 .ty = field_ptr.container_ty,
333 .val = field_ptr.container_ptr,
334 }, writer, level - 1, mod);
335 }
294336
295337 if (field_ptr.container_ty.zigTypeTag() == .Struct) {
296338 const field_name = field_ptr.container_ty.structFields().keys()[field_ptr.field_index];
......@@ -344,6 +386,9 @@ pub fn print(
344386 return writer.writeAll(" }");
345387 },
346388 .slice => {
389 if (level == 0) {
390 return writer.writeAll(".{ ... }");
391 }
347392 const payload = val.castTag(.slice).?.data;
348393 try writer.writeAll(".{ ");
349394 const elem_ty = ty.elemType2();
......@@ -372,17 +417,25 @@ pub fn print(
372417 .@"error" => return writer.print("error.{s}", .{val.castTag(.@"error").?.data.name}),
373418 .eu_payload => {
374419 val = val.castTag(.eu_payload).?.data;
420 ty = ty.errorUnionPayload();
375421 },
376422 .opt_payload => {
377423 val = val.castTag(.opt_payload).?.data;
424 var buf: Type.Payload.ElemType = undefined;
425 ty = ty.optionalChild(&buf);
426 return print(.{ .ty = ty, .val = val }, writer, level, mod);
378427 },
379428 .eu_payload_ptr => {
380429 try writer.writeAll("&");
381430 val = val.castTag(.eu_payload_ptr).?.data.container_ptr;
431 ty = ty.elemType2().errorUnionPayload();
382432 },
383433 .opt_payload_ptr => {
384434 try writer.writeAll("&");
385 val = val.castTag(.opt_payload_ptr).?.data.container_ptr;
435 val = val.castTag(.opt_payload).?.data;
436 var buf: Type.Payload.ElemType = undefined;
437 ty = ty.elemType2().optionalChild(&buf);
438 return print(.{ .ty = ty, .val = val }, writer, level, mod);
386439 },
387440
388441 // TODO these should not appear in this function
src/codegen/llvm.zig+12-7
......@@ -599,6 +599,13 @@ pub const Object = struct {
599599 self.llvm_module.dump();
600600 }
601601
602 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
603 defer arena_allocator.deinit();
604 const arena = arena_allocator.allocator();
605
606 const mod = comp.bin_file.options.module.?;
607 const cache_dir = mod.zig_cache_artifact_directory;
608
602609 if (std.debug.runtime_safety) {
603610 var error_message: [*:0]const u8 = undefined;
604611 // verifyModule always allocs the error_message even if there is no error
......@@ -606,17 +613,15 @@ pub const Object = struct {
606613
607614 if (self.llvm_module.verify(.ReturnStatus, &error_message).toBool()) {
608615 std.debug.print("\n{s}\n", .{error_message});
616
617 if (try locPath(arena, comp.emit_llvm_ir, cache_dir)) |emit_llvm_ir_path| {
618 _ = self.llvm_module.printModuleToFile(emit_llvm_ir_path, &error_message);
619 }
620
609621 @panic("LLVM module verification failed");
610622 }
611623 }
612624
613 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
614 defer arena_allocator.deinit();
615 const arena = arena_allocator.allocator();
616
617 const mod = comp.bin_file.options.module.?;
618 const cache_dir = mod.zig_cache_artifact_directory;
619
620625 var emit_bin_path: ?[*:0]const u8 = if (comp.bin_file.options.emit) |emit|
621626 try emit.basenamePath(arena, try arena.dupeZ(u8, comp.bin_file.intermediary_basename.?))
622627 else
src/codegen/llvm/bindings.zig+3
......@@ -390,6 +390,9 @@ pub const Module = opaque {
390390
391391 pub const setModuleInlineAsm2 = LLVMSetModuleInlineAsm2;
392392 extern fn LLVMSetModuleInlineAsm2(M: *const Module, Asm: [*]const u8, Len: usize) void;
393
394 pub const printModuleToFile = LLVMPrintModuleToFile;
395 extern fn LLVMPrintModuleToFile(M: *const Module, Filename: [*:0]const u8, ErrorMessage: *[*:0]const u8) Bool;
393396};
394397
395398pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
src/print_air.zig+30-8
......@@ -4,6 +4,7 @@ const fmtIntSizeBin = std.fmt.fmtIntSizeBin;
44
55const Module = @import("Module.zig");
66const Value = @import("value.zig").Value;
7const Type = @import("type.zig").Type;
78const Air = @import("Air.zig");
89const Liveness = @import("Liveness.zig");
910
......@@ -304,14 +305,27 @@ const Writer = struct {
304305 // no-op, no argument to write
305306 }
306307
308 fn writeType(w: *Writer, s: anytype, ty: Type) !void {
309 const t = ty.tag();
310 switch (t) {
311 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
312 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
313 .generic_poison => try s.writeAll("(generic_poison)"),
314 .var_args_param => try s.writeAll("(var_args_param)"),
315 .bound_fn => try s.writeAll("(bound_fn)"),
316 else => try ty.print(s, w.module),
317 }
318 }
319
307320 fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
308321 const ty = w.air.instructions.items(.data)[inst].ty;
309 try s.print("{}", .{ty.fmtDebug()});
322 try w.writeType(s, ty);
310323 }
311324
312325 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
313326 const ty_op = w.air.instructions.items(.data)[inst].ty_op;
314 try s.print("{}, ", .{w.air.getRefType(ty_op.ty).fmtDebug()});
327 try w.writeType(s, w.air.getRefType(ty_op.ty));
328 try s.writeAll(", ");
315329 try w.writeOperand(s, inst, 0, ty_op.operand);
316330 }
317331
......@@ -320,7 +334,8 @@ const Writer = struct {
320334 const extra = w.air.extraData(Air.Block, ty_pl.payload);
321335 const body = w.air.extra[extra.end..][0..extra.data.body_len];
322336
323 try s.print("{}, {{\n", .{w.air.getRefType(ty_pl.ty).fmtDebug()});
337 try w.writeType(s, w.air.getRefType(ty_pl.ty));
338 try s.writeAll(", {\n");
324339 const old_indent = w.indent;
325340 w.indent += 2;
326341 try w.writeBody(s, body);
......@@ -335,7 +350,8 @@ const Writer = struct {
335350 const len = @intCast(usize, vector_ty.arrayLen());
336351 const elements = @ptrCast([]const Air.Inst.Ref, w.air.extra[ty_pl.payload..][0..len]);
337352
338 try s.print("{}, [", .{vector_ty.fmtDebug()});
353 try w.writeType(s, vector_ty);
354 try s.writeAll(", [");
339355 for (elements) |elem, i| {
340356 if (i != 0) try s.writeAll(", ");
341357 try w.writeOperand(s, inst, i, elem);
......@@ -408,7 +424,8 @@ const Writer = struct {
408424 const extra = w.air.extraData(Air.Bin, pl_op.payload).data;
409425
410426 const elem_ty = w.air.typeOfIndex(inst).childType();
411 try s.print("{}, ", .{elem_ty.fmtDebug()});
427 try w.writeType(s, elem_ty);
428 try s.writeAll(", ");
412429 try w.writeOperand(s, inst, 0, pl_op.operand);
413430 try s.writeAll(", ");
414431 try w.writeOperand(s, inst, 1, extra.lhs);
......@@ -511,7 +528,9 @@ const Writer = struct {
511528 fn writeConstant(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
512529 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
513530 const val = w.air.values[ty_pl.payload];
514 try s.print("{}, {}", .{ w.air.getRefType(ty_pl.ty).fmtDebug(), val.fmtDebug() });
531 const ty = w.air.getRefType(ty_pl.ty);
532 try w.writeType(s, ty);
533 try s.print(", {}", .{val.fmtValue(ty, w.module)});
515534 }
516535
517536 fn writeAssembly(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
......@@ -523,7 +542,7 @@ const Writer = struct {
523542 var op_index: usize = 0;
524543
525544 const ret_ty = w.air.typeOfIndex(inst);
526 try s.print("{}", .{ret_ty.fmtDebug()});
545 try w.writeType(s, ret_ty);
527546
528547 if (is_volatile) {
529548 try s.writeAll(", volatile");
......@@ -647,7 +666,10 @@ const Writer = struct {
647666 const body = w.air.extra[extra.end..][0..extra.data.body_len];
648667
649668 try w.writeOperand(s, inst, 0, extra.data.ptr);
650 try s.print(", {}, {{\n", .{w.air.getRefType(ty_pl.ty).fmtDebug()});
669
670 try s.writeAll(", ");
671 try w.writeType(s, w.air.getRefType(ty_pl.ty));
672 try s.writeAll(", {\n");
651673 const old_indent = w.indent;
652674 w.indent += 2;
653675 try w.writeBody(s, body);