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
1311413114
1311513115 // TYPE_BLOCK
1311613116 {
13117 var type_block = try module_block.enterSubBlock(ir.Type);
13117 var type_block = try module_block.enterSubBlock(ir.Type, true);
1311813118
1311913119 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
1322113221 {
1322213222 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
1322613226 for (self.function_attributes_set.keys()) |func_attributes| {
1322713227 for (func_attributes.slice(self), 0..) |attributes, i| {
......@@ -13424,7 +13424,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1342413424 // PARAMATTR_BLOCK
1342513425 {
1342613426 const Paramattr = ir.Paramattr;
13427 var paramattr_block = try module_block.enterSubBlock(Paramattr);
13427 var paramattr_block = try module_block.enterSubBlock(Paramattr, true);
1342813428
1342913429 for (self.function_attributes_set.keys()) |func_attributes| {
1343013430 const func_attributes_slice = func_attributes.slice(self);
......@@ -13627,7 +13627,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1362713627 // CONSTANTS_BLOCK
1362813628 {
1362913629 const Constants = ir.Constants;
13630 var constants_block = try module_block.enterSubBlock(Constants);
13630 var constants_block = try module_block.enterSubBlock(Constants, true);
1363113631
1363213632 var current_type: Type = .none;
1363313633 const tags = self.constant_items.items(.tag);
......@@ -13953,7 +13953,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1395313953 // METADATA_KIND_BLOCK
1395413954 if (!self.strip) {
1395513955 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
1395813958 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {
1395913959 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{
......@@ -14006,7 +14006,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1400614006 // METADATA_BLOCK
1400714007 if (!self.strip) {
1400814008 const MetadataBlock = ir.MetadataBlock;
14009 var metadata_block = try module_block.enterSubBlock(MetadataBlock);
14009 var metadata_block = try module_block.enterSubBlock(MetadataBlock, true);
1401014010
1401114011 const MetadataBlockWriter = @TypeOf(metadata_block);
1401214012
......@@ -14411,6 +14411,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1441114411 try metadata_block.end();
1441214412 }
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
1441414442 // FUNCTION_BLOCKS
1441514443 {
1441614444 const FunctionAdapter = struct {
......@@ -14500,7 +14528,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1450014528
1450114529 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
1450514533 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
1450914537 // Emit function level metadata block
1451014538 if (!func.strip and func.debug_values.len > 0) {
1451114539 const MetadataBlock = ir.FunctionMetadataBlock;
14512 var metadata_block = try function_block.enterSubBlock(MetadataBlock);
14540 var metadata_block = try function_block.enterSubBlock(MetadataBlock, false);
1451314541
1451414542 for (func.debug_values) |value| {
1451514543 try metadata_block.writeAbbrev(MetadataBlock.Value{
......@@ -14978,7 +15006,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1497815006 .column = location.column,
1497915007 .scope = @enumFromInt(metadata_adapter.getMetadataIndex(location.scope)),
1498015008 .inlined_at = @enumFromInt(metadata_adapter.getMetadataIndex(location.inlined_at)),
14981 .is_implicit = false,
1498215009 });
1498315010 has_location = true;
1498415011 },
......@@ -14995,7 +15022,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1499515022 if (!func.strip) {
1499615023 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
1500015027 for (func.blocks, 0..) |block, block_index| {
1500115028 const name = block.instruction.name(&func);
......@@ -15020,7 +15047,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1502015047 if (dbg == .none) break :blk;
1502115048
1502215049 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
1502515052 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{
1502615053 .kind = ir.MetadataKind.dbg,
src/codegen/llvm/bitcode_writer.zig+16-11
......@@ -148,7 +148,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
148148 }
149149
150150 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);
152152 }
153153
154154 fn BlockWriter(comptime Block: type) type {
......@@ -164,7 +164,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
164164 start: usize,
165165 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 {
168168 try bitcode.writeBits(1, parent_abbrev_len);
169169 try bitcode.writeVBR(Block.id, 8);
170170 try bitcode.writeVBR(abbrev_len, 4);
......@@ -174,19 +174,23 @@ pub fn BitcodeWriter(comptime types: []const type) type {
174174 const start = bitcode.length();
175175 try bitcode.writeBits(0, 32);
176176
177 // Predefine all block abbrevs
178 inline for (Block.abbrevs) |Abbrev| {
179 try defineAbbrev(bitcode, &Abbrev.ops);
180 }
181
182 return .{
177 var self = Self{
183178 .start = start,
184179 .bitcode = bitcode,
185180 };
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;
186190 }
187191
188 pub fn enterSubBlock(self: Self, comptime SubBlock: type) Error!BlockWriter(SubBlock) {
189 return BlockWriter(SubBlock).init(self.bitcode, abbrev_len);
192 pub fn enterSubBlock(self: Self, comptime SubBlock: type, comptime define_abbrevs: bool) Error!BlockWriter(SubBlock) {
193 return BlockWriter(SubBlock).init(self.bitcode, abbrev_len, define_abbrevs);
190194 }
191195
192196 pub fn end(self: *Self) Error!void {
......@@ -291,7 +295,8 @@ pub fn BitcodeWriter(comptime types: []const type) type {
291295 }
292296 }
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;
295300 try bitcode.writeBits(2, abbrev_len);
296301
297302 // 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 {
186186 };
187187};
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
189197pub const Type = struct {
190198 pub const id = 17;
191199
......@@ -1583,17 +1591,16 @@ pub const FunctionBlock = struct {
15831591 pub const DebugLoc = struct {
15841592 pub const ops = [_]AbbrevOp{
15851593 .{ .literal = 35 },
1586 .{ .fixed = 32 },
1587 .{ .fixed = 32 },
1588 .{ .fixed = 32 },
1589 .{ .fixed = 32 },
1590 .{ .fixed = 1 },
1594 LineAbbrev,
1595 ColumnAbbrev,
1596 MetadataAbbrev,
1597 MetadataAbbrev,
1598 .{ .literal = 0 },
15911599 };
15921600 line: u32,
15931601 column: u32,
15941602 scope: Builder.Metadata,
15951603 inlined_at: Builder.Metadata,
1596 is_implicit: bool,
15971604 };
15981605
15991606 pub const DebugLocAgain = struct {