diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig index 70a85093e8..6e850dd169 100644 --- a/src/codegen/llvm/Builder.zig +++ b/src/codegen/llvm/Builder.zig @@ -13114,7 +13114,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // TYPE_BLOCK { - var type_block = try module_block.enterSubBlock(ir.Type); + var type_block = try module_block.enterSubBlock(ir.Type, true); try type_block.writeAbbrev(ir.Type.NumEntry{ .num = @intCast(self.type_items.items.len) }); @@ -13221,7 +13221,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co { const ParamattrGroup = ir.ParamattrGroup; - var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup); + var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup, true); for (self.function_attributes_set.keys()) |func_attributes| { for (func_attributes.slice(self), 0..) |attributes, i| { @@ -13424,7 +13424,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // PARAMATTR_BLOCK { const Paramattr = ir.Paramattr; - var paramattr_block = try module_block.enterSubBlock(Paramattr); + var paramattr_block = try module_block.enterSubBlock(Paramattr, true); for (self.function_attributes_set.keys()) |func_attributes| { const func_attributes_slice = func_attributes.slice(self); @@ -13627,7 +13627,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // CONSTANTS_BLOCK { const Constants = ir.Constants; - var constants_block = try module_block.enterSubBlock(Constants); + var constants_block = try module_block.enterSubBlock(Constants, true); var current_type: Type = .none; const tags = self.constant_items.items(.tag); @@ -13953,7 +13953,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // METADATA_KIND_BLOCK if (!self.strip) { const MetadataKindBlock = ir.MetadataKindBlock; - var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock); + var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock, true); inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| { try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{ @@ -14006,7 +14006,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // METADATA_BLOCK if (!self.strip) { const MetadataBlock = ir.MetadataBlock; - var metadata_block = try module_block.enterSubBlock(MetadataBlock); + var metadata_block = try module_block.enterSubBlock(MetadataBlock, true); const MetadataBlockWriter = @TypeOf(metadata_block); @@ -14411,6 +14411,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co try metadata_block.end(); } + // Block info + { + const BlockInfo = ir.BlockInfo; + var block_info_block = try module_block.enterSubBlock(BlockInfo, true); + + try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionBlock.id}); + inline for (ir.FunctionBlock.abbrevs) |abbrev| { + try block_info_block.defineAbbrev(&abbrev.ops); + } + + try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionValueSymbolTable.id}); + inline for (ir.FunctionValueSymbolTable.abbrevs) |abbrev| { + try block_info_block.defineAbbrev(&abbrev.ops); + } + + try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionMetadataBlock.id}); + inline for (ir.FunctionMetadataBlock.abbrevs) |abbrev| { + try block_info_block.defineAbbrev(&abbrev.ops); + } + + try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.MetadataAttachmentBlock.id}); + inline for (ir.MetadataAttachmentBlock.abbrevs) |abbrev| { + try block_info_block.defineAbbrev(&abbrev.ops); + } + + try block_info_block.end(); + } + // FUNCTION_BLOCKS { const FunctionAdapter = struct { @@ -14500,7 +14528,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co if (func.instructions.len == 0) continue; - var function_block = try module_block.enterSubBlock(FunctionBlock); + var function_block = try module_block.enterSubBlock(FunctionBlock, false); try function_block.writeAbbrev(FunctionBlock.DeclareBlocks{ .num_blocks = func.blocks.len }); @@ -14509,7 +14537,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co // Emit function level metadata block if (!func.strip and func.debug_values.len > 0) { const MetadataBlock = ir.FunctionMetadataBlock; - var metadata_block = try function_block.enterSubBlock(MetadataBlock); + var metadata_block = try function_block.enterSubBlock(MetadataBlock, false); for (func.debug_values) |value| { try metadata_block.writeAbbrev(MetadataBlock.Value{ @@ -14978,7 +15006,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co .column = location.column, .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)), .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)), - .is_implicit = false, }); has_location = true; }, @@ -14995,7 +15022,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co if (!func.strip) { const ValueSymbolTable = ir.FunctionValueSymbolTable; - var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable); + var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable, false); for (func.blocks, 0..) |block, block_index| { const name = block.instruction.name(&func); @@ -15020,7 +15047,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co if (dbg == .none) break :blk; const MetadataAttachmentBlock = ir.MetadataAttachmentBlock; - var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock); + var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock, false); try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{ .kind = ir.MetadataKind.dbg, diff --git a/src/codegen/llvm/bitcode_writer.zig b/src/codegen/llvm/bitcode_writer.zig index d48a92dd40..7140c87929 100644 --- a/src/codegen/llvm/bitcode_writer.zig +++ b/src/codegen/llvm/bitcode_writer.zig @@ -148,7 +148,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { } pub fn enterTopBlock(self: *BcWriter, comptime SubBlock: type) Error!BlockWriter(SubBlock) { - return BlockWriter(SubBlock).init(self, 2); + return BlockWriter(SubBlock).init(self, 2, true); } fn BlockWriter(comptime Block: type) type { @@ -164,7 +164,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { start: usize, bitcode: *BcWriter, - pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6) Error!Self { + pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6, comptime define_abbrevs: bool) Error!Self { try bitcode.writeBits(1, parent_abbrev_len); try bitcode.writeVBR(Block.id, 8); try bitcode.writeVBR(abbrev_len, 4); @@ -174,19 +174,23 @@ pub fn BitcodeWriter(comptime types: []const type) type { const start = bitcode.length(); try bitcode.writeBits(0, 32); - // Predefine all block abbrevs - inline for (Block.abbrevs) |Abbrev| { - try defineAbbrev(bitcode, &Abbrev.ops); - } - - return .{ + var self = Self{ .start = start, .bitcode = bitcode, }; + + // Predefine all block abbrevs + if (define_abbrevs) { + inline for (Block.abbrevs) |Abbrev| { + try self.defineAbbrev(&Abbrev.ops); + } + } + + return self; } - pub fn enterSubBlock(self: Self, comptime SubBlock: type) Error!BlockWriter(SubBlock) { - return BlockWriter(SubBlock).init(self.bitcode, abbrev_len); + pub fn enterSubBlock(self: Self, comptime SubBlock: type, comptime define_abbrevs: bool) Error!BlockWriter(SubBlock) { + return BlockWriter(SubBlock).init(self.bitcode, abbrev_len, define_abbrevs); } pub fn end(self: *Self) Error!void { @@ -291,7 +295,8 @@ pub fn BitcodeWriter(comptime types: []const type) type { } } - fn defineAbbrev(bitcode: *BcWriter, comptime ops: []const AbbrevOp) Error!void { + pub fn defineAbbrev(self: *Self, comptime ops: []const AbbrevOp) Error!void { + const bitcode = self.bitcode; try bitcode.writeBits(2, abbrev_len); // ops.len is not accurate because arrays are actually two ops diff --git a/src/codegen/llvm/ir.zig b/src/codegen/llvm/ir.zig index bde52f7d8b..f5c0e535c2 100644 --- a/src/codegen/llvm/ir.zig +++ b/src/codegen/llvm/ir.zig @@ -186,6 +186,14 @@ pub const Module = struct { }; }; +pub const BlockInfo = struct { + pub const id = 0; + + pub const set_block_id = 1; + + pub const abbrevs = [_]type{}; +}; + pub const Type = struct { pub const id = 17; @@ -1583,17 +1591,16 @@ pub const FunctionBlock = struct { pub const DebugLoc = struct { pub const ops = [_]AbbrevOp{ .{ .literal = 35 }, - .{ .fixed = 32 }, - .{ .fixed = 32 }, - .{ .fixed = 32 }, - .{ .fixed = 32 }, - .{ .fixed = 1 }, + LineAbbrev, + ColumnAbbrev, + MetadataAbbrev, + MetadataAbbrev, + .{ .literal = 0 }, }; line: u32, column: u32, scope: Builder.Metadata, inlined_at: Builder.Metadata, - is_implicit: bool, }; pub const DebugLocAgain = struct {