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 {...@@ -4759,7 +4759,7 @@ pub const FuncGen = struct {
47594759
4760 inlined: std.ArrayListUnmanaged(struct {4760 inlined: std.ArrayListUnmanaged(struct {
4761 base_line: u32,4761 base_line: u32,
4762 location: Builder.Metadata,4762 location: Builder.DebugLocation,
4763 scope: Builder.Metadata,4763 scope: Builder.Metadata,
4764 }) = .{},4764 }) = .{},
47654765
...@@ -6579,17 +6579,20 @@ pub const FuncGen = struct {...@@ -6579,17 +6579,20 @@ pub const FuncGen = struct {
6579 const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;6579 const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
6580 self.prev_dbg_line = @intCast(self.base_line + dbg_stmt.line + 1);6580 self.prev_dbg_line = @intCast(self.base_line + dbg_stmt.line + 1);
6581 self.prev_dbg_column = @intCast(dbg_stmt.column + 1);6581 self.prev_dbg_column = @intCast(dbg_stmt.column + 1);
6582 const inlined_at = if (self.inlined.items.len > 0)6582
6583 self.inlined.items[self.inlined.items.len - 1].location6583 const inlined_at_location = if (self.inlined.getLastOrNull()) |inlined|
6584 try inlined.location.toMetadata(self.wip.builder)
6584 else6585 else
6585 .none;6586 .none;
65866587
6587 self.wip.current_debug_location = try self.wip.builder.debugLocation(6588 self.wip.debug_location = .{
6588 self.prev_dbg_line,6589 .location = .{
6589 self.prev_dbg_column,6590 .line = self.prev_dbg_line,
6590 self.scope,6591 .column = self.prev_dbg_column,
6591 inlined_at,6592 .scope = self.scope,
6592 );6593 .inlined_at = inlined_at_location,
6594 },
6595 };
65936596
6594 return .none;6597 return .none;
6595 }6598 }
...@@ -6610,7 +6613,7 @@ pub const FuncGen = struct {...@@ -6610,7 +6613,7 @@ pub const FuncGen = struct {
66106613
6611 const line_number = decl.src_line + 1;6614 const line_number = decl.src_line + 1;
6612 try self.inlined.append(self.gpa, .{6615 try self.inlined.append(self.gpa, .{
6613 .location = self.wip.current_debug_location,6616 .location = self.wip.debug_location,
6614 .scope = self.scope,6617 .scope = self.scope,
6615 .base_line = self.base_line,6618 .base_line = self.base_line,
6616 });6619 });
...@@ -6649,13 +6652,15 @@ pub const FuncGen = struct {...@@ -6649,13 +6652,15 @@ pub const FuncGen = struct {
6649 );6652 );
6650 self.scope = lexical_block;6653 self.scope = lexical_block;
6651 self.base_line = decl.src_line;6654 self.base_line = decl.src_line;
6652 const inlined_at = self.wip.current_debug_location;6655 const inlined_at_location = try self.wip.debug_location.toMetadata(&o.builder);
6653 self.wip.current_debug_location = try o.builder.debugLocation(6656 self.wip.debug_location = .{
6654 line_number,6657 .location = .{
6655 0,6658 .line = line_number,
6656 self.scope,6659 .column = 0,
6657 inlined_at,6660 .scope = self.scope,
6658 );6661 .inlined_at = inlined_at_location,
6662 },
6663 };
6659 return .none;6664 return .none;
6660 }6665 }
66616666
...@@ -6672,7 +6677,7 @@ pub const FuncGen = struct {...@@ -6672,7 +6677,7 @@ pub const FuncGen = struct {
6672 const old = self.inlined.pop();6677 const old = self.inlined.pop();
6673 self.scope = old.scope;6678 self.scope = old.scope;
6674 self.base_line = old.base_line;6679 self.base_line = old.base_line;
6675 self.wip.current_debug_location = old.location;6680 self.wip.debug_location = old.location;
6676 return .none;6681 return .none;
6677 }6682 }
66786683
...@@ -8833,13 +8838,15 @@ pub const FuncGen = struct {...@@ -8833,13 +8838,15 @@ pub const FuncGen = struct {
8833 @intCast(self.arg_index),8838 @intCast(self.arg_index),
8834 );8839 );
88358840
8836 const old_location = self.wip.current_debug_location;8841 const old_location = self.wip.debug_location;
8837 self.wip.current_debug_location = try o.builder.debugLocation(8842 self.wip.debug_location = .{
8838 lbrace_line,8843 .location = .{
8839 lbrace_col,8844 .line = lbrace_line,
8840 self.scope,8845 .column = lbrace_col,
8841 .none,8846 .scope = self.scope,
8842 );8847 .inlined_at = .none,
8848 },
8849 };
88438850
8844 const owner_mod = self.dg.ownerModule();8851 const owner_mod = self.dg.ownerModule();
8845 if (isByRef(inst_ty, mod)) {8852 if (isByRef(inst_ty, mod)) {
...@@ -8886,7 +8893,7 @@ pub const FuncGen = struct {...@@ -8886,7 +8893,7 @@ pub const FuncGen = struct {
8886 );8893 );
8887 }8894 }
88888895
8889 self.wip.current_debug_location = old_location;8896 self.wip.debug_location = old_location;
8890 return arg_val;8897 return arg_val;
8891 }8898 }
88928899
...@@ -11732,15 +11739,15 @@ fn buildAllocaInner(...@@ -11732,15 +11739,15 @@ fn buildAllocaInner(
1173211739
11733 const alloca = blk: {11740 const alloca = blk: {
11734 const prev_cursor = wip.cursor;11741 const prev_cursor = wip.cursor;
11735 const prev_debug_location = wip.current_debug_location;11742 const prev_debug_location = wip.debug_location;
11736 defer {11743 defer {
11737 wip.cursor = prev_cursor;11744 wip.cursor = prev_cursor;
11738 if (wip.cursor.block == .entry) wip.cursor.instruction += 1;11745 if (wip.cursor.block == .entry) wip.cursor.instruction += 1;
11739 wip.current_debug_location = prev_debug_location;11746 wip.debug_location = prev_debug_location;
11740 }11747 }
1174111748
11742 wip.cursor = .{ .block = .entry };11749 wip.cursor = .{ .block = .entry };
11743 wip.current_debug_location = .none;11750 wip.debug_location = .no_location;
11744 break :blk try wip.alloca(.normal, llvm_ty, .none, alignment, address_space, "");11751 break :blk try wip.alloca(.normal, llvm_ty, .none, alignment, address_space, "");
11745 };11752 };
1174611753
src/codegen/llvm/Builder.zig+46-24
...@@ -3797,7 +3797,7 @@ pub const Function = struct {...@@ -3797,7 +3797,7 @@ pub const Function = struct {
3797 instructions: std.MultiArrayList(Instruction) = .{},3797 instructions: std.MultiArrayList(Instruction) = .{},
3798 names: [*]const String = &[0]String{},3798 names: [*]const String = &[0]String{},
3799 value_indices: [*]const u32 = &[0]u32{},3799 value_indices: [*]const u32 = &[0]u32{},
3800 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, Metadata) = .{},3800 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{},
3801 debug_values: []const Instruction.Index = &.{},3801 debug_values: []const Instruction.Index = &.{},
3802 extra: []const u32 = &.{},3802 extra: []const u32 = &.{},
38033803
...@@ -4857,16 +4857,40 @@ pub const Function = struct {...@@ -4857,16 +4857,40 @@ pub const Function = struct {
4857 }4857 }
4858};4858};
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
4860pub const WipFunction = struct {4884pub const WipFunction = struct {
4861 builder: *Builder,4885 builder: *Builder,
4862 function: Function.Index,4886 function: Function.Index,
4863 last_debug_location: Metadata,4887 prev_debug_location: DebugLocation,
4864 current_debug_location: Metadata,4888 debug_location: DebugLocation,
4865 cursor: Cursor,4889 cursor: Cursor,
4866 blocks: std.ArrayListUnmanaged(Block),4890 blocks: std.ArrayListUnmanaged(Block),
4867 instructions: std.MultiArrayList(Instruction),4891 instructions: std.MultiArrayList(Instruction),
4868 names: std.ArrayListUnmanaged(String),4892 names: std.ArrayListUnmanaged(String),
4869 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, Metadata),4893 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, DebugLocation),
4870 debug_values: std.AutoArrayHashMapUnmanaged(Instruction.Index, void),4894 debug_values: std.AutoArrayHashMapUnmanaged(Instruction.Index, void),
4871 extra: std.ArrayListUnmanaged(u32),4895 extra: std.ArrayListUnmanaged(u32),
48724896
...@@ -4902,8 +4926,8 @@ pub const WipFunction = struct {...@@ -4902,8 +4926,8 @@ pub const WipFunction = struct {
4902 var self: WipFunction = .{4926 var self: WipFunction = .{
4903 .builder = builder,4927 .builder = builder,
4904 .function = function,4928 .function = function,
4905 .last_debug_location = .none,4929 .prev_debug_location = .no_location,
4906 .current_debug_location = .none,4930 .debug_location = .no_location,
4907 .cursor = undefined,4931 .cursor = undefined,
4908 .blocks = .{},4932 .blocks = .{},
4909 .instructions = .{},4933 .instructions = .{},
...@@ -5850,7 +5874,7 @@ pub const WipFunction = struct {...@@ -5850,7 +5874,7 @@ pub const WipFunction = struct {
5850 const value_indices = try gpa.alloc(u32, final_instructions_len);5874 const value_indices = try gpa.alloc(u32, final_instructions_len);
5851 errdefer gpa.free(value_indices);5875 errdefer gpa.free(value_indices);
58525876
5853 var debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, Metadata) = .{};5877 var debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{};
5854 errdefer debug_locations.deinit(gpa);5878 errdefer debug_locations.deinit(gpa);
5855 try debug_locations.ensureUnusedCapacity(gpa, @intCast(self.debug_locations.count()));5879 try debug_locations.ensureUnusedCapacity(gpa, @intCast(self.debug_locations.count()));
58565880
...@@ -6494,10 +6518,10 @@ pub const WipFunction = struct {...@@ -6494,10 +6518,10 @@ pub const WipFunction = struct {
6494 if (!self.builder.strip) {6518 if (!self.builder.strip) {
6495 self.names.appendAssumeCapacity(final_name);6519 self.names.appendAssumeCapacity(final_name);
6496 if (block_instructions.items.len == 0 or6520 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))
6498 {6522 {
6499 self.debug_locations.putAssumeCapacity(index, self.current_debug_location);6523 self.debug_locations.putAssumeCapacity(index, self.debug_location);
6500 self.last_debug_location = self.current_debug_location;6524 self.prev_debug_location = self.debug_location;
6501 }6525 }
6502 }6526 }
6503 block_instructions.insertAssumeCapacity(self.cursor.instruction, index);6527 block_instructions.insertAssumeCapacity(self.cursor.instruction, index);
...@@ -14866,20 +14890,18 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14866,20 +14890,18 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1486614890
14867 if (!self.strip) {14891 if (!self.strip) {
14868 if (func.debug_locations.get(@enumFromInt(instr_index))) |debug_location| {14892 if (func.debug_locations.get(@enumFromInt(instr_index))) |debug_location| {
14869 if (debug_location != .none) {14893 switch (debug_location) {
14870 const location = self.metadata_items.get(@intFromEnum(debug_location));14894 .no_location => has_location = false,
14871 assert(location.tag == .location);14895 .location => |location| {
14872 const extra = self.metadataExtraData(Metadata.Location, location.data);14896 try function_block.writeAbbrev(FunctionBlock.DebugLoc{
14873 try function_block.writeAbbrev(FunctionBlock.DebugLoc{14897 .line = location.line,
14874 .line = extra.line,14898 .column = location.column,
14875 .column = extra.column,14899 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)),
14876 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(extra.scope)),14900 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)),
14877 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(extra.inlined_at)),14901 .is_implicit = false,
14878 .is_implicit = false,14902 });
14879 });14903 has_location = true;
14880 has_location = true;14904 },
14881 } else {
14882 has_location = false;
14883 }14905 }
14884 } else if (has_location) {14906 } else if (has_location) {
14885 try function_block.writeAbbrev(FunctionBlock.DebugLocAgain{});14907 try function_block.writeAbbrev(FunctionBlock.DebugLocAgain{});