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...@@ -13060,7 +13060,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1306013060
13061 // TYPE_BLOCK13061 // TYPE_BLOCK
13062 {13062 {
13063 var type_block = try module_block.enterSubBlock(ir.Type);13063 var type_block = try module_block.enterSubBlock(ir.Type, true);
1306413064
13065 try type_block.writeAbbrev(ir.Type.NumEntry{ .num = @intCast(self.type_items.items.len) });13065 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...@@ -13167,7 +13167,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13167 {13167 {
13168 const ParamattrGroup = ir.ParamattrGroup;13168 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
13172 for (self.function_attributes_set.keys()) |func_attributes| {13172 for (self.function_attributes_set.keys()) |func_attributes| {
13173 for (func_attributes.slice(self), 0..) |attributes, i| {13173 for (func_attributes.slice(self), 0..) |attributes, i| {
...@@ -13370,7 +13370,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13370,7 +13370,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13370 // PARAMATTR_BLOCK13370 // PARAMATTR_BLOCK
13371 {13371 {
13372 const Paramattr = ir.Paramattr;13372 const Paramattr = ir.Paramattr;
13373 var paramattr_block = try module_block.enterSubBlock(Paramattr);13373 var paramattr_block = try module_block.enterSubBlock(Paramattr, true);
1337413374
13375 for (self.function_attributes_set.keys()) |func_attributes| {13375 for (self.function_attributes_set.keys()) |func_attributes| {
13376 const func_attributes_slice = func_attributes.slice(self);13376 const func_attributes_slice = func_attributes.slice(self);
...@@ -13573,7 +13573,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13573,7 +13573,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13573 // CONSTANTS_BLOCK13573 // CONSTANTS_BLOCK
13574 {13574 {
13575 const Constants = ir.Constants;13575 const Constants = ir.Constants;
13576 var constants_block = try module_block.enterSubBlock(Constants);13576 var constants_block = try module_block.enterSubBlock(Constants, true);
1357713577
13578 var current_type: Type = .none;13578 var current_type: Type = .none;
13579 const tags = self.constant_items.items(.tag);13579 const tags = self.constant_items.items(.tag);
...@@ -13899,7 +13899,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13899,7 +13899,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13899 // METADATA_KIND_BLOCK13899 // METADATA_KIND_BLOCK
13900 if (!self.strip) {13900 if (!self.strip) {
13901 const MetadataKindBlock = ir.MetadataKindBlock;13901 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
13904 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {13904 inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {
13905 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{13905 try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{
...@@ -13952,7 +13952,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13952,7 +13952,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13952 // METADATA_BLOCK13952 // METADATA_BLOCK
13953 if (!self.strip) {13953 if (!self.strip) {
13954 const MetadataBlock = ir.MetadataBlock;13954 const MetadataBlock = ir.MetadataBlock;
13955 var metadata_block = try module_block.enterSubBlock(MetadataBlock);13955 var metadata_block = try module_block.enterSubBlock(MetadataBlock, true);
1395613956
13957 const MetadataBlockWriter = @TypeOf(metadata_block);13957 const MetadataBlockWriter = @TypeOf(metadata_block);
1395813958
...@@ -14357,6 +14357,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14357,6 +14357,34 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14357 try metadata_block.end();14357 try metadata_block.end();
14358 }14358 }
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
14360 // FUNCTION_BLOCKS14388 // FUNCTION_BLOCKS
14361 {14389 {
14362 const FunctionAdapter = struct {14390 const FunctionAdapter = struct {
...@@ -14445,7 +14473,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14445,7 +14473,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1444514473
14446 if (func.instructions.len == 0) continue;14474 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
14450 try function_block.writeAbbrev(FunctionBlock.DeclareBlocks{ .num_blocks = func.blocks.len });14478 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...@@ -14454,7 +14482,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14454 // Emit function level metadata block14482 // Emit function level metadata block
14455 if (!self.strip and func.debug_values.len != 0) {14483 if (!self.strip and func.debug_values.len != 0) {
14456 const MetadataBlock = ir.FunctionMetadataBlock;14484 const MetadataBlock = ir.FunctionMetadataBlock;
14457 var metadata_block = try function_block.enterSubBlock(MetadataBlock);14485 var metadata_block = try function_block.enterSubBlock(MetadataBlock, false);
1445814486
14459 for (func.debug_values) |value| {14487 for (func.debug_values) |value| {
14460 try metadata_block.writeAbbrev(MetadataBlock.Value{14488 try metadata_block.writeAbbrev(MetadataBlock.Value{
...@@ -14940,7 +14968,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14940,7 +14968,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14940 if (!self.strip) {14968 if (!self.strip) {
14941 const ValueSymbolTable = ir.FunctionValueSymbolTable;14969 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
14945 for (func.blocks, 0..) |block, block_index| {14973 for (func.blocks, 0..) |block, block_index| {
14946 const name = block.instruction.name(&func);14974 const name = block.instruction.name(&func);
...@@ -14965,7 +14993,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14965,7 +14993,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14965 if (dbg == .none) break :blk;14993 if (dbg == .none) break :blk;
1496614994
14967 const MetadataAttachmentBlock = ir.MetadataAttachmentBlock;14995 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
14970 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{14998 try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{
14971 .kind = ir.MetadataKind.dbg,14999 .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+8
...@@ -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