authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-25 01:27:21+01:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-25 03:16:52+01:00
log928914e25afce48f5b4abef7150f2c9242f29ba5
tree5f0b3c11a76204c21e1baa35b63317c5b807c3f1
parent119b2030f70de9ed900ce3594201de6f869b71c8

Builder: Improve debug location system

Debug locations are no longer emitted twice every time

2 files changed, 82 insertions(+), 53 deletions(-)

src/codegen/llvm.zig+36-29
......@@ -4759,7 +4759,7 @@ pub const FuncGen = struct {
47594759
47604760 inlined: std.ArrayListUnmanaged(struct {
47614761 base_line: u32,
4762 location: Builder.Metadata,
4762 location: Builder.DebugLocation,
47634763 scope: Builder.Metadata,
47644764 }) = .{},
47654765
......@@ -6579,17 +6579,20 @@ pub const FuncGen = struct {
65796579 const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
65806580 self.prev_dbg_line = @intCast(self.base_line + dbg_stmt.line + 1);
65816581 self.prev_dbg_column = @intCast(dbg_stmt.column + 1);
6582 const inlined_at = if (self.inlined.items.len > 0)
6583 self.inlined.items[self.inlined.items.len - 1].location
6582
6583 const inlined_at_location = if (self.inlined.getLastOrNull()) |inlined|
6584 try inlined.location.toMetadata(self.wip.builder)
65846585 else
65856586 .none;
65866587
6587 self.wip.current_debug_location = try self.wip.builder.debugLocation(
6588 self.prev_dbg_line,
6589 self.prev_dbg_column,
6590 self.scope,
6591 inlined_at,
6592 );
6588 self.wip.debug_location = .{
6589 .location = .{
6590 .line = self.prev_dbg_line,
6591 .column = self.prev_dbg_column,
6592 .scope = self.scope,
6593 .inlined_at = inlined_at_location,
6594 },
6595 };
65936596
65946597 return .none;
65956598 }
......@@ -6610,7 +6613,7 @@ pub const FuncGen = struct {
66106613
66116614 const line_number = decl.src_line + 1;
66126615 try self.inlined.append(self.gpa, .{
6613 .location = self.wip.current_debug_location,
6616 .location = self.wip.debug_location,
66146617 .scope = self.scope,
66156618 .base_line = self.base_line,
66166619 });
......@@ -6649,13 +6652,15 @@ pub const FuncGen = struct {
66496652 );
66506653 self.scope = lexical_block;
66516654 self.base_line = decl.src_line;
6652 const inlined_at = self.wip.current_debug_location;
6653 self.wip.current_debug_location = try o.builder.debugLocation(
6654 line_number,
6655 0,
6656 self.scope,
6657 inlined_at,
6658 );
6655 const inlined_at_location = try self.wip.debug_location.toMetadata(&o.builder);
6656 self.wip.debug_location = .{
6657 .location = .{
6658 .line = line_number,
6659 .column = 0,
6660 .scope = self.scope,
6661 .inlined_at = inlined_at_location,
6662 },
6663 };
66596664 return .none;
66606665 }
66616666
......@@ -6672,7 +6677,7 @@ pub const FuncGen = struct {
66726677 const old = self.inlined.pop();
66736678 self.scope = old.scope;
66746679 self.base_line = old.base_line;
6675 self.wip.current_debug_location = old.location;
6680 self.wip.debug_location = old.location;
66766681 return .none;
66776682 }
66786683
......@@ -8833,13 +8838,15 @@ pub const FuncGen = struct {
88338838 @intCast(self.arg_index),
88348839 );
88358840
8836 const old_location = self.wip.current_debug_location;
8837 self.wip.current_debug_location = try o.builder.debugLocation(
8838 lbrace_line,
8839 lbrace_col,
8840 self.scope,
8841 .none,
8842 );
8841 const old_location = self.wip.debug_location;
8842 self.wip.debug_location = .{
8843 .location = .{
8844 .line = lbrace_line,
8845 .column = lbrace_col,
8846 .scope = self.scope,
8847 .inlined_at = .none,
8848 },
8849 };
88438850
88448851 const owner_mod = self.dg.ownerModule();
88458852 if (isByRef(inst_ty, mod)) {
......@@ -8886,7 +8893,7 @@ pub const FuncGen = struct {
88868893 );
88878894 }
88888895
8889 self.wip.current_debug_location = old_location;
8896 self.wip.debug_location = old_location;
88908897 return arg_val;
88918898 }
88928899
......@@ -11732,15 +11739,15 @@ fn buildAllocaInner(
1173211739
1173311740 const alloca = blk: {
1173411741 const prev_cursor = wip.cursor;
11735 const prev_debug_location = wip.current_debug_location;
11742 const prev_debug_location = wip.debug_location;
1173611743 defer {
1173711744 wip.cursor = prev_cursor;
1173811745 if (wip.cursor.block == .entry) wip.cursor.instruction += 1;
11739 wip.current_debug_location = prev_debug_location;
11746 wip.debug_location = prev_debug_location;
1174011747 }
1174111748
1174211749 wip.cursor = .{ .block = .entry };
11743 wip.current_debug_location = .none;
11750 wip.debug_location = .no_location;
1174411751 break :blk try wip.alloca(.normal, llvm_ty, .none, alignment, address_space, "");
1174511752 };
1174611753
src/codegen/llvm/Builder.zig+46-24
......@@ -3797,7 +3797,7 @@ pub const Function = struct {
37973797 instructions: std.MultiArrayList(Instruction) = .{},
37983798 names: [*]const String = &[0]String{},
37993799 value_indices: [*]const u32 = &[0]u32{},
3800 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, Metadata) = .{},
3800 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{},
38013801 debug_values: []const Instruction.Index = &.{},
38023802 extra: []const u32 = &.{},
38033803
......@@ -4857,16 +4857,40 @@ pub const Function = struct {
48574857 }
48584858};
48594859
4860pub const DebugLocation = union(enum) {
4861 no_location: void,
4862 location: Location,
4863
4864 pub const Location = struct {
4865 line: u32,
4866 column: u32,
4867 scope: Builder.Metadata,
4868 inlined_at: Builder.Metadata,
4869 };
4870
4871 pub fn toMetadata(self: DebugLocation, builder: *Builder) Allocator.Error!Metadata {
4872 return switch (self) {
4873 .no_location => .none,
4874 .location => |location| try builder.debugLocation(
4875 location.line,
4876 location.column,
4877 location.scope,
4878 location.inlined_at,
4879 ),
4880 };
4881 }
4882};
4883
48604884pub const WipFunction = struct {
48614885 builder: *Builder,
48624886 function: Function.Index,
4863 last_debug_location: Metadata,
4864 current_debug_location: Metadata,
4887 prev_debug_location: DebugLocation,
4888 debug_location: DebugLocation,
48654889 cursor: Cursor,
48664890 blocks: std.ArrayListUnmanaged(Block),
48674891 instructions: std.MultiArrayList(Instruction),
48684892 names: std.ArrayListUnmanaged(String),
4869 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, Metadata),
4893 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, DebugLocation),
48704894 debug_values: std.AutoArrayHashMapUnmanaged(Instruction.Index, void),
48714895 extra: std.ArrayListUnmanaged(u32),
48724896
......@@ -4902,8 +4926,8 @@ pub const WipFunction = struct {
49024926 var self: WipFunction = .{
49034927 .builder = builder,
49044928 .function = function,
4905 .last_debug_location = .none,
4906 .current_debug_location = .none,
4929 .prev_debug_location = .no_location,
4930 .debug_location = .no_location,
49074931 .cursor = undefined,
49084932 .blocks = .{},
49094933 .instructions = .{},
......@@ -5850,7 +5874,7 @@ pub const WipFunction = struct {
58505874 const value_indices = try gpa.alloc(u32, final_instructions_len);
58515875 errdefer gpa.free(value_indices);
58525876
5853 var debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, Metadata) = .{};
5877 var debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{};
58545878 errdefer debug_locations.deinit(gpa);
58555879 try debug_locations.ensureUnusedCapacity(gpa, @intCast(self.debug_locations.count()));
58565880
......@@ -6494,10 +6518,10 @@ pub const WipFunction = struct {
64946518 if (!self.builder.strip) {
64956519 self.names.appendAssumeCapacity(final_name);
64966520 if (block_instructions.items.len == 0 or
6497 self.current_debug_location != self.last_debug_location)
6521 !std.meta.eql(self.debug_location, self.prev_debug_location))
64986522 {
6499 self.debug_locations.putAssumeCapacity(index, self.current_debug_location);
6500 self.last_debug_location = self.current_debug_location;
6523 self.debug_locations.putAssumeCapacity(index, self.debug_location);
6524 self.prev_debug_location = self.debug_location;
65016525 }
65026526 }
65036527 block_instructions.insertAssumeCapacity(self.cursor.instruction, index);
......@@ -14866,20 +14890,18 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1486614890
1486714891 if (!self.strip) {
1486814892 if (func.debug_locations.get(@enumFromInt(instr_index))) |debug_location| {
14869 if (debug_location != .none) {
14870 const location = self.metadata_items.get(@intFromEnum(debug_location));
14871 assert(location.tag == .location);
14872 const extra = self.metadataExtraData(Metadata.Location, location.data);
14873 try function_block.writeAbbrev(FunctionBlock.DebugLoc{
14874 .line = extra.line,
14875 .column = extra.column,
14876 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(extra.scope)),
14877 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(extra.inlined_at)),
14878 .is_implicit = false,
14879 });
14880 has_location = true;
14881 } else {
14882 has_location = false;
14893 switch (debug_location) {
14894 .no_location => has_location = false,
14895 .location => |location| {
14896 try function_block.writeAbbrev(FunctionBlock.DebugLoc{
14897 .line = location.line,
14898 .column = location.column,
14899 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)),
14900 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)),
14901 .is_implicit = false,
14902 });
14903 has_location = true;
14904 },
1488314905 }
1488414906 } else if (has_location) {
1488514907 try function_block.writeAbbrev(FunctionBlock.DebugLocAgain{});