authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-26 04:03:40-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-26 04:03:40-08:00
log031f23117dadd84b1e7189ee5b0f712eeb23ca1e
tree38a5e91df49ef36abd5f1579f8d9bef8124ef69f
parent032c2ee9bc1fae507be776c118cd03d28c865edc
parent73a16d440ba7968bab11acddc2129f05e09daa0d
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19083 from antlilja/llvm-blockinfo

LLVM reduce size of emitted bitcode

3 files changed, 67 insertions(+), 28 deletions(-)

src/codegen/llvm/Builder.zig+38-11
...@@ -13114,7 +13114,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13114,7 +13114,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1311413114
13115 // TYPE_BLOCK13115 // TYPE_BLOCK
13116 {13116 {
13117 var type_block = try module_block.enterSubBlock(ir.Type);13117 var type_block = try module_block.enterSubBlock(ir.Type, true);
1311813118
13119 try type_block.writeAbbrev(ir.Type.NumEntry{ .num = @intCast(self.type_items.items.len) });13119 try type_block.writeAbbrev(ir.Type.NumEntry{ .num = @intCast(self.type_items.items.len) });
1312013120
...@@ -13221,7 +13221,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13221,7 +13221,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13221 {13221 {
13222 const ParamattrGroup = ir.ParamattrGroup;13222 const ParamattrGroup = ir.ParamattrGroup;
1322313223
13224 var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup);13224 var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup, true);
1322513225
13226 for (self.function_attributes_set.keys()) |func_attributes| {13226 for (self.function_attributes_set.keys()) |func_attributes| {
13227 for (func_attributes.slice(self), 0..) |attributes, i| {13227 for (func_attributes.slice(self), 0..) |attributes, i| {
...@@ -13424,7 +13424,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13424,7 +13424,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13424 // PARAMATTR_BLOCK13424 // PARAMATTR_BLOCK
13425 {13425 {
13426 const Paramattr = ir.Paramattr;13426 const Paramattr = ir.Paramattr;
13427 var paramattr_block = try module_block.enterSubBlock(Paramattr);13427 var paramattr_block = try module_block.enterSubBlock(Paramattr, true);
1342813428
13429 for (self.function_attributes_set.keys()) |func_attributes| {13429 for (self.function_attributes_set.keys()) |func_attributes| {
13430 const func_attributes_slice = func_attributes.slice(self);13430 const func_attributes_slice = func_attributes.slice(self);
...@@ -13627,7 +13627,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13627,7 +13627,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13627 // CONSTANTS_BLOCK13627 // CONSTANTS_BLOCK
13628 {13628 {
13629 const Constants = ir.Constants;13629 const Constants = ir.Constants;
13630 var constants_block = try module_block.enterSubBlock(Constants);13630 var constants_block = try module_block.enterSubBlock(Constants, true);
1363113631
13632 var current_type: Type = .none;13632 var current_type: Type = .none;
13633 const tags = self.constant_items.items(.tag);13633 const tags = self.constant_items.items(.tag);
...@@ -13953,7 +13953,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13953,7 +13953,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13953 // METADATA_KIND_BLOCK13953 // METADATA_KIND_BLOCK
13954 if (!self.strip) {13954 if (!self.strip) {
13955 const MetadataKindBlock = ir.MetadataKindBlock;13955 const MetadataKindBlock = ir.MetadataKindBlock;
13956 var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock);13956 var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock, true);
1395713957
13958 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {13958 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {
13959 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{13959 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{
...@@ -14006,7 +14006,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14006,7 +14006,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14006 // METADATA_BLOCK14006 // METADATA_BLOCK
14007 if (!self.strip) {14007 if (!self.strip) {
14008 const MetadataBlock = ir.MetadataBlock;14008 const MetadataBlock = ir.MetadataBlock;
14009 var metadata_block = try module_block.enterSubBlock(MetadataBlock);14009 var metadata_block = try module_block.enterSubBlock(MetadataBlock, true);
1401014010
14011 const MetadataBlockWriter = @TypeOf(metadata_block);14011 const MetadataBlockWriter = @TypeOf(metadata_block);
1401214012
...@@ -14411,6 +14411,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14411,6 +14411,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14411 try metadata_block.end();14411 try metadata_block.end();
14412 }14412 }
1441314413
14414 // Block info
14415 {
14416 const BlockInfo = ir.BlockInfo;
14417 var block_info_block = try module_block.enterSubBlock(BlockInfo, true);
14418
14419 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionBlock.id});
14420 inline for (ir.FunctionBlock.abbrevs) |abbrev| {
14421 try block_info_block.defineAbbrev(&abbrev.ops);
14422 }
14423
14424 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionValueSymbolTable.id});
14425 inline for (ir.FunctionValueSymbolTable.abbrevs) |abbrev| {
14426 try block_info_block.defineAbbrev(&abbrev.ops);
14427 }
14428
14429 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionMetadataBlock.id});
14430 inline for (ir.FunctionMetadataBlock.abbrevs) |abbrev| {
14431 try block_info_block.defineAbbrev(&abbrev.ops);
14432 }
14433
14434 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.MetadataAttachmentBlock.id});
14435 inline for (ir.MetadataAttachmentBlock.abbrevs) |abbrev| {
14436 try block_info_block.defineAbbrev(&abbrev.ops);
14437 }
14438
14439 try block_info_block.end();
14440 }
14441
14414 // FUNCTION_BLOCKS14442 // FUNCTION_BLOCKS
14415 {14443 {
14416 const FunctionAdapter = struct {14444 const FunctionAdapter = struct {
...@@ -14500,7 +14528,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14500,7 +14528,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1450014528
14501 if (func.instructions.len == 0) continue;14529 if (func.instructions.len == 0) continue;
1450214530
14503 var function_block = try module_block.enterSubBlock(FunctionBlock);14531 var function_block = try module_block.enterSubBlock(FunctionBlock, false);
1450414532
14505 try function_block.writeAbbrev(FunctionBlock.DeclareBlocks{ .num_blocks = func.blocks.len });14533 try function_block.writeAbbrev(FunctionBlock.DeclareBlocks{ .num_blocks = func.blocks.len });
1450614534
...@@ -14509,7 +14537,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14509,7 +14537,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14509 // Emit function level metadata block14537 // Emit function level metadata block
14510 if (!func.strip and func.debug_values.len > 0) {14538 if (!func.strip and func.debug_values.len > 0) {
14511 const MetadataBlock = ir.FunctionMetadataBlock;14539 const MetadataBlock = ir.FunctionMetadataBlock;
14512 var metadata_block = try function_block.enterSubBlock(MetadataBlock);14540 var metadata_block = try function_block.enterSubBlock(MetadataBlock, false);
1451314541
14514 for (func.debug_values) |value| {14542 for (func.debug_values) |value| {
14515 try metadata_block.writeAbbrev(MetadataBlock.Value{14543 try metadata_block.writeAbbrev(MetadataBlock.Value{
...@@ -14978,7 +15006,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14978,7 +15006,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14978 .column = location.column,15006 .column = location.column,
14979 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)),15007 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)),
14980 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)),15008 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)),
14981 .is_implicit = false,
14982 });15009 });
14983 has_location = true;15010 has_location = true;
14984 },15011 },
...@@ -14995,7 +15022,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14995,7 +15022,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14995 if (!func.strip) {15022 if (!func.strip) {
14996 const ValueSymbolTable = ir.FunctionValueSymbolTable;15023 const ValueSymbolTable = ir.FunctionValueSymbolTable;
1499715024
14998 var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable);15025 var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable, false);
1499915026
15000 for (func.blocks, 0..) |block, block_index| {15027 for (func.blocks, 0..) |block, block_index| {
15001 const name = block.instruction.name(&func);15028 const name = block.instruction.name(&func);
...@@ -15020,7 +15047,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -15020,7 +15047,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
15020 if (dbg == .none) break :blk;15047 if (dbg == .none) break :blk;
1502115048
15022 const MetadataAttachmentBlock = ir.MetadataAttachmentBlock;15049 const MetadataAttachmentBlock = ir.MetadataAttachmentBlock;
15023 var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock);15050 var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock, false);
1502415051
15025 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{15052 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{
15026 .kind = ir.MetadataKind.dbg,15053 .kind = ir.MetadataKind.dbg,
src/codegen/llvm/bitcode_writer.zig+16-11
...@@ -148,7 +148,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -148,7 +148,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
148 }148 }
149149
150 pub fn enterTopBlock(self: *BcWriter, comptime SubBlock: type) Error!BlockWriter(SubBlock) {150 pub fn enterTopBlock(self: *BcWriter, comptime SubBlock: type) Error!BlockWriter(SubBlock) {
151 return BlockWriter(SubBlock).init(self, 2);151 return BlockWriter(SubBlock).init(self, 2, true);
152 }152 }
153153
154 fn BlockWriter(comptime Block: type) type {154 fn BlockWriter(comptime Block: type) type {
...@@ -164,7 +164,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -164,7 +164,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
164 start: usize,164 start: usize,
165 bitcode: *BcWriter,165 bitcode: *BcWriter,
166166
167 pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6) Error!Self {167 pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6, comptime define_abbrevs: bool) Error!Self {
168 try bitcode.writeBits(1, parent_abbrev_len);168 try bitcode.writeBits(1, parent_abbrev_len);
169 try bitcode.writeVBR(Block.id, 8);169 try bitcode.writeVBR(Block.id, 8);
170 try bitcode.writeVBR(abbrev_len, 4);170 try bitcode.writeVBR(abbrev_len, 4);
...@@ -174,19 +174,23 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -174,19 +174,23 @@ pub fn BitcodeWriter(comptime types: []const type) type {
174 const start = bitcode.length();174 const start = bitcode.length();
175 try bitcode.writeBits(0, 32);175 try bitcode.writeBits(0, 32);
176176
177 // Predefine all block abbrevs177 var self = Self{
178 inline for (Block.abbrevs) |Abbrev| {
179 try defineAbbrev(bitcode, &Abbrev.ops);
180 }
181
182 return .{
183 .start = start,178 .start = start,
184 .bitcode = bitcode,179 .bitcode = bitcode,
185 };180 };
181
182 // Predefine all block abbrevs
183 if (define_abbrevs) {
184 inline for (Block.abbrevs) |Abbrev| {
185 try self.defineAbbrev(&Abbrev.ops);
186 }
187 }
188
189 return self;
186 }190 }
187191
188 pub fn enterSubBlock(self: Self, comptime SubBlock: type) Error!BlockWriter(SubBlock) {192 pub fn enterSubBlock(self: Self, comptime SubBlock: type, comptime define_abbrevs: bool) Error!BlockWriter(SubBlock) {
189 return BlockWriter(SubBlock).init(self.bitcode, abbrev_len);193 return BlockWriter(SubBlock).init(self.bitcode, abbrev_len, define_abbrevs);
190 }194 }
191195
192 pub fn end(self: *Self) Error!void {196 pub fn end(self: *Self) Error!void {
...@@ -291,7 +295,8 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -291,7 +295,8 @@ pub fn BitcodeWriter(comptime types: []const type) type {
291 }295 }
292 }296 }
293297
294 fn defineAbbrev(bitcode: *BcWriter, comptime ops: []const AbbrevOp) Error!void {298 pub fn defineAbbrev(self: *Self, comptime ops: []const AbbrevOp) Error!void {
299 const bitcode = self.bitcode;
295 try bitcode.writeBits(2, abbrev_len);300 try bitcode.writeBits(2, abbrev_len);
296301
297 // ops.len is not accurate because arrays are actually two ops302 // ops.len is not accurate because arrays are actually two ops
src/codegen/llvm/ir.zig+13-6
...@@ -186,6 +186,14 @@ pub const Module = struct {...@@ -186,6 +186,14 @@ pub const Module = struct {
186 };186 };
187};187};
188188
189pub const BlockInfo = struct {
190 pub const id = 0;
191
192 pub const set_block_id = 1;
193
194 pub const abbrevs = [_]type{};
195};
196
189pub const Type = struct {197pub const Type = struct {
190 pub const id = 17;198 pub const id = 17;
191199
...@@ -1583,17 +1591,16 @@ pub const FunctionBlock = struct {...@@ -1583,17 +1591,16 @@ pub const FunctionBlock = struct {
1583 pub const DebugLoc = struct {1591 pub const DebugLoc = struct {
1584 pub const ops = [_]AbbrevOp{1592 pub const ops = [_]AbbrevOp{
1585 .{ .literal = 35 },1593 .{ .literal = 35 },
1586 .{ .fixed = 32 },1594 LineAbbrev,
1587 .{ .fixed = 32 },1595 ColumnAbbrev,
1588 .{ .fixed = 32 },1596 MetadataAbbrev,
1589 .{ .fixed = 32 },1597 MetadataAbbrev,
1590 .{ .fixed = 1 },1598 .{ .literal = 0 },
1591 };1599 };
1592 line: u32,1600 line: u32,
1593 column: u32,1601 column: u32,
1594 scope: Builder.Metadata,1602 scope: Builder.Metadata,
1595 inlined_at: Builder.Metadata,1603 inlined_at: Builder.Metadata,
1596 is_implicit: bool,
1597 };1604 };
15981605
1599 pub const DebugLocAgain = struct {1606 pub const DebugLocAgain = struct {