authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-31 11:47:56+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-31 11:47:56+02:00
logaf197d495478f7a5c33a6550d5525e86d797dc89
treef7ef9ac5dd3131eca188e3e4bbcba4dc16fdd802
parentd86685ac9612c08e33f1d94f7f617d9c0da1b7bd
parentbd711dfd255447883d25f422031592e3824ca296
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14130 from Vexu/debug-info

Debug info fixes

13 files changed, 86 insertions(+), 45 deletions(-)

src/Air.zig+7-2
......@@ -31,7 +31,7 @@ pub const Inst = struct {
3131 /// The first N instructions in the main block must be one arg instruction per
3232 /// function parameter. This makes function parameters participate in
3333 /// liveness analysis without any special handling.
34 /// Uses the `ty` field.
34 /// Uses the `arg` field.
3535 arg,
3636 /// Float or integer addition. For integers, wrapping is undefined behavior.
3737 /// Both operands are guaranteed to be the same type, and the result type
......@@ -795,6 +795,10 @@ pub const Inst = struct {
795795 rhs: Ref,
796796 },
797797 ty: Type,
798 arg: struct {
799 ty: Ref,
800 src_index: u32,
801 },
798802 ty_op: struct {
799803 ty: Ref,
800804 operand: Ref,
......@@ -1103,11 +1107,12 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
11031107
11041108 .alloc,
11051109 .ret_ptr,
1106 .arg,
11071110 .err_return_trace,
11081111 .c_va_start,
11091112 => return datas[inst].ty,
11101113
1114 .arg => return air.getRefType(datas[inst].arg.ty),
1115
11111116 .assembly,
11121117 .block,
11131118 .constant,
src/AstGen.zig+8-2
......@@ -6942,7 +6942,13 @@ fn switchExpr(
69426942 // it as the break operand.
69436943 if (body_len < 2)
69446944 break :blk;
6945 const store_inst = payloads.items[end_index - 2];
6945
6946 var store_index = end_index - 2;
6947 while (true) : (store_index -= 1) switch (zir_tags[payloads.items[store_index]]) {
6948 .dbg_block_end, .dbg_block_begin, .dbg_stmt, .dbg_var_val, .dbg_var_ptr => {},
6949 else => break,
6950 };
6951 const store_inst = payloads.items[store_index];
69466952 if (zir_tags[store_inst] != .store_to_block_ptr or
69476953 zir_datas[store_inst].bin.lhs != block_scope.rl_ptr)
69486954 break :blk;
......@@ -12150,7 +12156,7 @@ const GenZir = struct {
1215012156
1215112157 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
1215212158 try gz.astgen.instructions.append(gpa, .{ .tag = .dbg_block_end, .data = undefined });
12153 try gz.instructions.insert(gpa, gz.instructions.items.len - 1, new_index);
12159 try gz.instructions.append(gpa, new_index);
1215412160 }
1215512161};
1215612162
src/Module.zig+5-1
......@@ -5672,11 +5672,15 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
56725672 runtime_param_index += 1;
56735673 continue;
56745674 }
5675 const air_ty = try sema.addType(param_ty);
56755676 const arg_index = @intCast(u32, sema.air_instructions.len);
56765677 inner_block.instructions.appendAssumeCapacity(arg_index);
56775678 sema.air_instructions.appendAssumeCapacity(.{
56785679 .tag = .arg,
5679 .data = .{ .ty = param_ty },
5680 .data = .{ .arg = .{
5681 .ty = air_ty,
5682 .src_index = @intCast(u32, total_param_index),
5683 } },
56805684 });
56815685 sema.inst_map.putAssumeCapacityNoClobber(inst, Air.indexToRef(arg_index));
56825686 total_param_index += 1;
src/Sema.zig+7-8
......@@ -476,13 +476,6 @@ pub const Block = struct {
476476 });
477477 }
478478
479 fn addArg(block: *Block, ty: Type) error{OutOfMemory}!Air.Inst.Ref {
480 return block.addInst(.{
481 .tag = .arg,
482 .data = .{ .ty = ty },
483 });
484 }
485
486479 fn addStructFieldPtr(
487480 block: *Block,
488481 struct_ptr: Air.Inst.Ref,
......@@ -7258,7 +7251,13 @@ fn instantiateGenericCall(
72587251 } else {
72597252 // We insert into the map an instruction which is runtime-known
72607253 // but has the type of the argument.
7261 const child_arg = try child_block.addArg(arg_ty);
7254 const child_arg = try child_block.addInst(.{
7255 .tag = .arg,
7256 .data = .{ .arg = .{
7257 .ty = try child_sema.addType(arg_ty),
7258 .src_index = @intCast(u32, arg_i),
7259 } },
7260 });
72627261 child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
72637262 }
72647263 }
src/arch/aarch64/CodeGen.zig+2-1
......@@ -4158,7 +4158,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
41584158
41594159 const ty = self.air.typeOfIndex(inst);
41604160 const result = self.args[arg_index];
4161 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
4161 const src_index = self.air.instructions.items(.data)[inst].arg.src_index;
4162 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, src_index);
41624163
41634164 const mcv = switch (result) {
41644165 // Copy registers to the stack
src/arch/arm/CodeGen.zig+3-2
......@@ -4037,8 +4037,9 @@ fn genInlineMemsetCode(
40374037
40384038fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, arg_index: u32) error{OutOfMemory}!void {
40394039 const mcv = self.args[arg_index];
4040 const ty = self.air.instructions.items(.data)[inst].ty;
4041 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
4040 const arg = self.air.instructions.items(.data)[inst].arg;
4041 const ty = self.air.getRefType(arg.ty);
4042 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg.src_index);
40424043
40434044 switch (self.debug_output) {
40444045 .dwarf => |dw| {
src/arch/riscv64/CodeGen.zig+5-4
......@@ -1608,9 +1608,10 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void {
16081608 return self.fail("TODO implement codegen airFieldParentPtr", .{});
16091609}
16101610
1611fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void {
1612 const ty = self.air.instructions.items(.data)[inst].ty;
1613 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
1611fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
1612 const arg = self.air.instructions.items(.data)[inst].arg;
1613 const ty = self.air.getRefType(arg.ty);
1614 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg.src_index);
16141615
16151616 switch (self.debug_output) {
16161617 .dwarf => |dw| switch (mcv) {
......@@ -1640,7 +1641,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
16401641 // TODO support stack-only arguments
16411642 // TODO Copy registers to the stack
16421643 const mcv = result;
1643 try self.genArgDbgInfo(inst, mcv, @intCast(u32, arg_index));
1644 try self.genArgDbgInfo(inst, mcv);
16441645
16451646 if (self.liveness.isUnused(inst))
16461647 return self.finishAirBookkeeping();
src/arch/sparc64/CodeGen.zig+5-4
......@@ -1016,7 +1016,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
10161016 }
10171017 };
10181018
1019 try self.genArgDbgInfo(inst, mcv, @intCast(u32, arg_index));
1019 try self.genArgDbgInfo(inst, mcv);
10201020
10211021 if (self.liveness.isUnused(inst))
10221022 return self.finishAirBookkeeping();
......@@ -3407,9 +3407,10 @@ fn finishAir(self: *Self, inst: Air.Inst.Index, result: MCValue, operands: [Live
34073407 self.finishAirBookkeeping();
34083408}
34093409
3410fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void {
3411 const ty = self.air.instructions.items(.data)[inst].ty;
3412 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
3410fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
3411 const arg = self.air.instructions.items(.data)[inst].arg;
3412 const ty = self.air.getRefType(arg.ty);
3413 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg.src_index);
34133414
34143415 switch (self.debug_output) {
34153416 .dwarf => |dw| switch (mcv) {
src/arch/wasm/CodeGen.zig+2-2
......@@ -2474,8 +2474,8 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
24742474
24752475 switch (func.debug_output) {
24762476 .dwarf => |dwarf| {
2477 // TODO: Get the original arg index rather than wasm arg index
2478 const name = func.mod_fn.getParamName(func.bin_file.base.options.module.?, arg_index);
2477 const src_index = func.air.instructions.items(.data)[inst].arg.src_index;
2478 const name = func.mod_fn.getParamName(func.bin_file.base.options.module.?, src_index);
24792479 try dwarf.genArgDbgInfo(name, arg_ty, .wasm, func.mod_fn.owner_decl, .{
24802480 .wasm_local = arg.local.value,
24812481 });
src/arch/x86_64/CodeGen.zig+2-1
......@@ -3799,7 +3799,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
37993799
38003800 const ty = self.air.typeOfIndex(inst);
38013801 const mcv = self.args[arg_index];
3802 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
3802 const src_index = self.air.instructions.items(.data)[inst].arg.src_index;
3803 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, src_index);
38033804
38043805 if (self.liveness.isUnused(inst))
38053806 return self.finishAirBookkeeping();
src/codegen/llvm.zig+31-16
......@@ -1459,7 +1459,8 @@ pub const Object = struct {
14591459 .signed => DW.ATE.signed,
14601460 .unsigned => DW.ATE.unsigned,
14611461 };
1462 const di_type = dib.createBasicType(name, info.bits, dwarf_encoding);
1462 const di_bits = ty.abiSize(target) * 8; // lldb cannot handle non-byte sized types
1463 const di_type = dib.createBasicType(name, di_bits, dwarf_encoding);
14631464 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type);
14641465 return di_type;
14651466 },
......@@ -1550,7 +1551,8 @@ pub const Object = struct {
15501551 return di_type;
15511552 },
15521553 .Bool => {
1553 const di_type = dib.createBasicType("bool", 1, DW.ATE.boolean);
1554 const di_bits = 8; // lldb cannot handle non-byte sized types
1555 const di_type = dib.createBasicType("bool", di_bits, DW.ATE.boolean);
15541556 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type);
15551557 return di_type;
15561558 },
......@@ -1723,10 +1725,31 @@ pub const Object = struct {
17231725 return array_di_ty;
17241726 },
17251727 .Vector => {
1728 const elem_ty = ty.elemType2();
1729 // Vector elements cannot be padded since that would make
1730 // @bitSizOf(elem) * len > @bitSizOf(vec).
1731 // Neither gdb nor lldb seem to be able to display non-byte sized
1732 // vectors properly.
1733 const elem_di_type = switch (elem_ty.zigTypeTag()) {
1734 .Int => blk: {
1735 const info = elem_ty.intInfo(target);
1736 assert(info.bits != 0);
1737 const name = try ty.nameAlloc(gpa, o.module);
1738 defer gpa.free(name);
1739 const dwarf_encoding: c_uint = switch (info.signedness) {
1740 .signed => DW.ATE.signed,
1741 .unsigned => DW.ATE.unsigned,
1742 };
1743 break :blk dib.createBasicType(name, info.bits, dwarf_encoding);
1744 },
1745 .Bool => dib.createBasicType("bool", 1, DW.ATE.boolean),
1746 else => try o.lowerDebugType(ty.childType(), .full),
1747 };
1748
17261749 const vector_di_ty = dib.createVectorType(
17271750 ty.abiSize(target) * 8,
17281751 ty.abiAlignment(target) * 8,
1729 try o.lowerDebugType(ty.childType(), .full),
1752 elem_di_type,
17301753 ty.vectorLen(),
17311754 );
17321755 // The recursive call to `lowerDebugType` means we can't use `gop` anymore.
......@@ -1739,7 +1762,8 @@ pub const Object = struct {
17391762 var buf: Type.Payload.ElemType = undefined;
17401763 const child_ty = ty.optionalChild(&buf);
17411764 if (!child_ty.hasRuntimeBitsIgnoreComptime()) {
1742 const di_ty = dib.createBasicType(name, 1, DW.ATE.boolean);
1765 const di_bits = 8; // lldb cannot handle non-byte sized types
1766 const di_ty = dib.createBasicType(name, di_bits, DW.ATE.boolean);
17431767 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_ty);
17441768 return di_ty;
17451769 }
......@@ -1934,7 +1958,8 @@ pub const Object = struct {
19341958 .signed => DW.ATE.signed,
19351959 .unsigned => DW.ATE.unsigned,
19361960 };
1937 const di_ty = dib.createBasicType(name, info.bits, dwarf_encoding);
1961 const di_bits = ty.abiSize(target) * 8; // lldb cannot handle non-byte sized types
1962 const di_ty = dib.createBasicType(name, di_bits, dwarf_encoding);
19381963 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_ty);
19391964 return di_ty;
19401965 }
......@@ -8062,7 +8087,7 @@ pub const FuncGen = struct {
80628087 return arg_val;
80638088 }
80648089
8065 const src_index = self.getSrcArgIndex(self.arg_index - 1);
8090 const src_index = self.air.instructions.items(.data)[inst].arg.src_index;
80668091 const func = self.dg.decl.getFunction().?;
80678092 const lbrace_line = self.dg.module.declPtr(func.owner_decl).src_line + func.lbrace_line + 1;
80688093 const lbrace_col = func.lbrace_column + 1;
......@@ -8095,16 +8120,6 @@ pub const FuncGen = struct {
80958120 return arg_val;
80968121 }
80978122
8098 fn getSrcArgIndex(self: *FuncGen, runtime_index: u32) u32 {
8099 const fn_info = self.dg.decl.ty.fnInfo();
8100 var i: u32 = 0;
8101 for (fn_info.param_types) |param_ty, src_index| {
8102 if (!param_ty.hasRuntimeBitsIgnoreComptime()) continue;
8103 if (i == runtime_index) return @intCast(u32, src_index);
8104 i += 1;
8105 } else unreachable;
8106 }
8107
81088123 fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value {
81098124 if (self.liveness.isUnused(inst)) return null;
81108125 const ptr_ty = self.air.typeOfIndex(inst);
src/link/SpirV.zig+1-1
......@@ -314,7 +314,7 @@ fn cloneAir(air: Air, gpa: Allocator, air_arena: Allocator) !Air {
314314
315315 for (air_tags) |tag, i| {
316316 switch (tag) {
317 .arg, .alloc, .ret_ptr, .const_ty => air_datas[i].ty = try air_datas[i].ty.copy(air_arena),
317 .alloc, .ret_ptr, .const_ty => air_datas[i].ty = try air_datas[i].ty.copy(air_arena),
318318 else => {},
319319 }
320320 }
src/print_air.zig+8-1
......@@ -204,11 +204,12 @@ const Writer = struct {
204204 .const_ty,
205205 .alloc,
206206 .ret_ptr,
207 .arg,
208207 .err_return_trace,
209208 .c_va_start,
210209 => try w.writeTy(s, inst),
211210
211 .arg => try w.writeArg(s, inst),
212
212213 .not,
213214 .bitcast,
214215 .load,
......@@ -351,6 +352,12 @@ const Writer = struct {
351352 try w.writeType(s, ty);
352353 }
353354
355 fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
356 const arg = w.air.instructions.items(.data)[inst].arg;
357 try w.writeType(s, w.air.getRefType(arg.ty));
358 try s.print(", {d}", .{arg.src_index});
359 }
360
354361 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
355362 const ty_op = w.air.instructions.items(.data)[inst].ty_op;
356363 try w.writeType(s, w.air.getRefType(ty_op.ty));