authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-26 01:00:58+01:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-26 01:00:58+01:00
log9754d6d0a0cc69920822b64e35bf73f8775e4eab
treee2c58524959f0757b299e72caaa1a55ee456a9a0
parentb2374c4d75d16ac0eb7b7b5e53411a80e9f4413d

Builder: Use BlockInfo block to reduce size of bitcode


3 files changed, 62 insertions(+), 21 deletions(-)

src/codegen/llvm/Builder.zig+38-10
......@@ -13060,7 +13060,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1306013060
1306113061 // TYPE_BLOCK
1306213062 {
13063 var type_block = try module_block.enterSubBlock(ir.Type);
13063 var type_block = try module_block.enterSubBlock(ir.Type, true);
1306413064
1306513065 try type_block.writeAbbrev(ir.Type.NumEntry{ .num = @intCast(self.type_items.items.len) });
1306613066
......@@ -13167,7 +13167,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1316713167 {
1316813168 const ParamattrGroup = ir.ParamattrGroup;
1316913169
13170 var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup);
13170 var paramattr_group_block = try module_block.enterSubBlock(ParamattrGroup, true);
1317113171
1317213172 for (self.function_attributes_set.keys()) |func_attributes| {
1317313173 for (func_attributes.slice(self), 0..) |attributes, i| {
......@@ -13370,7 +13370,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1337013370 // PARAMATTR_BLOCK
1337113371 {
1337213372 const Paramattr = ir.Paramattr;
13373 var paramattr_block = try module_block.enterSubBlock(Paramattr);
13373 var paramattr_block = try module_block.enterSubBlock(Paramattr, true);
1337413374
1337513375 for (self.function_attributes_set.keys()) |func_attributes| {
1337613376 const func_attributes_slice = func_attributes.slice(self);
......@@ -13573,7 +13573,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1357313573 // CONSTANTS_BLOCK
1357413574 {
1357513575 const Constants = ir.Constants;
13576 var constants_block = try module_block.enterSubBlock(Constants);
13576 var constants_block = try module_block.enterSubBlock(Constants, true);
1357713577
1357813578 var current_type: Type = .none;
1357913579 const tags = self.constant_items.items(.tag);
......@@ -13899,7 +13899,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1389913899 // METADATA_KIND_BLOCK
1390013900 if (!self.strip) {
1390113901 const MetadataKindBlock = ir.MetadataKindBlock;
13902 var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock);
13902 var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock, true);
1390313903
1390413904 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {
1390513905 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{
......@@ -13952,7 +13952,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1395213952 // METADATA_BLOCK
1395313953 if (!self.strip) {
1395413954 const MetadataBlock = ir.MetadataBlock;
13955 var metadata_block = try module_block.enterSubBlock(MetadataBlock);
13955 var metadata_block = try module_block.enterSubBlock(MetadataBlock, true);
1395613956
1395713957 const MetadataBlockWriter = @TypeOf(metadata_block);
1395813958
......@@ -14357,6 +14357,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1435714357 try metadata_block.end();
1435814358 }
1435914359
14360 // Block info
14361 {
14362 const BlockInfo = ir.BlockInfo;
14363 var block_info_block = try module_block.enterSubBlock(BlockInfo, true);
14364
14365 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionBlock.id});
14366 inline for (ir.FunctionBlock.abbrevs) |abbrev| {
14367 try block_info_block.defineAbbrev(&abbrev.ops);
14368 }
14369
14370 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionValueSymbolTable.id});
14371 inline for (ir.FunctionValueSymbolTable.abbrevs) |abbrev| {
14372 try block_info_block.defineAbbrev(&abbrev.ops);
14373 }
14374
14375 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.FunctionMetadataBlock.id});
14376 inline for (ir.FunctionMetadataBlock.abbrevs) |abbrev| {
14377 try block_info_block.defineAbbrev(&abbrev.ops);
14378 }
14379
14380 try block_info_block.writeUnabbrev(BlockInfo.set_block_id, &.{ir.MetadataAttachmentBlock.id});
14381 inline for (ir.MetadataAttachmentBlock.abbrevs) |abbrev| {
14382 try block_info_block.defineAbbrev(&abbrev.ops);
14383 }
14384
14385 try block_info_block.end();
14386 }
14387
1436014388 // FUNCTION_BLOCKS
1436114389 {
1436214390 const FunctionAdapter = struct {
......@@ -14445,7 +14473,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1444514473
1444614474 if (func.instructions.len == 0) continue;
1444714475
14448 var function_block = try module_block.enterSubBlock(FunctionBlock);
14476 var function_block = try module_block.enterSubBlock(FunctionBlock, false);
1444914477
1445014478 try function_block.writeAbbrev(FunctionBlock.DeclareBlocks{ .num_blocks = func.blocks.len });
1445114479
......@@ -14454,7 +14482,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1445414482 // Emit function level metadata block
1445514483 if (!self.strip and func.debug_values.len != 0) {
1445614484 const MetadataBlock = ir.FunctionMetadataBlock;
14457 var metadata_block = try function_block.enterSubBlock(MetadataBlock);
14485 var metadata_block = try function_block.enterSubBlock(MetadataBlock, false);
1445814486
1445914487 for (func.debug_values) |value| {
1446014488 try metadata_block.writeAbbrev(MetadataBlock.Value{
......@@ -14940,7 +14968,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1494014968 if (!self.strip) {
1494114969 const ValueSymbolTable = ir.FunctionValueSymbolTable;
1494214970
14943 var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable);
14971 var value_symtab_block = try function_block.enterSubBlock(ValueSymbolTable, false);
1494414972
1494514973 for (func.blocks, 0..) |block, block_index| {
1494614974 const name = block.instruction.name(&func);
......@@ -14965,7 +14993,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1496514993 if (dbg == .none) break :blk;
1496614994
1496714995 const MetadataAttachmentBlock = ir.MetadataAttachmentBlock;
14968 var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock);
14996 var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock, false);
1496914997
1497014998 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{
1497114999 .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+8
......@@ -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