authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-24 16:59:00+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-24 17:00:36+01:00
logedb6486b3bf7a1c333d7cc3348f88ab121b72830
tree47469b8cfb262fbf9e6a1ddfc89748fd4fa67916
parent7e9f321f53b53dcefaf3b771720c9c25529c39ef

BitcodeWriter: cleanup type widths


2 files changed, 9 insertions(+), 13 deletions(-)

src/codegen/llvm/Builder.zig+1-1
...@@ -12944,7 +12944,7 @@ fn debugConstantAssumeCapacity(self: *Builder, constant: Constant) Metadata {...@@ -12944,7 +12944,7 @@ fn debugConstantAssumeCapacity(self: *Builder, constant: Constant) Metadata {
1294412944
12945pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]const u32 {12945pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]const u32 {
12946 const BitcodeWriter = bitcode_writer.BitcodeWriter(&.{ Type, FunctionAttributes });12946 const BitcodeWriter = bitcode_writer.BitcodeWriter(&.{ Type, FunctionAttributes });
12947 var bitcode = BitcodeWriter.init(allocator, &.{12947 var bitcode = BitcodeWriter.init(allocator, .{
12948 std.math.log2_int_ceil(usize, self.type_items.items.len),12948 std.math.log2_int_ceil(usize, self.type_items.items.len),
12949 std.math.log2_int_ceil(usize, 1 + self.function_attributes_set.count()),12949 std.math.log2_int_ceil(usize, 1 + self.function_attributes_set.count()),
12950 });12950 });
src/codegen/llvm/bitcode_writer.zig+8-12
...@@ -23,17 +23,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -23,17 +23,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {
23 bit_buffer: u32 = 0,23 bit_buffer: u32 = 0,
24 bit_count: u5 = 0,24 bit_count: u5 = 0,
2525
26 widths: []const u16,26 widths: [types.len]u16,
2727
28 pub fn getTypeIndex(comptime ty: type) usize {28 pub fn getTypeWidth(self: BcWriter, comptime Type: type) u16 {
29 inline for (types, 0..) |t, i| {29 return self.widths[comptime std.mem.indexOfScalar(type, types, Type).?];
30 if (t == ty) return i;
31 }
32 unreachable;
33 }30 }
3431
35 pub fn init(allocator: std.mem.Allocator, widths: []const u16) BcWriter {32 pub fn init(allocator: std.mem.Allocator, widths: [types.len]u16) BcWriter {
36 std.debug.assert(widths.len == types.len);
37 return .{33 return .{
38 .buffer = std.ArrayList(u32).init(allocator),34 .buffer = std.ArrayList(u32).init(allocator),
39 .widths = widths,35 .widths = widths,
...@@ -250,7 +246,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -250,7 +246,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
250 .fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len),246 .fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len),
251 .fixed_runtime => |width_ty| try self.bitcode.writeBits(247 .fixed_runtime => |width_ty| try self.bitcode.writeBits(
252 adapter.get(param, field_name),248 adapter.get(param, field_name),
253 self.bitcode.widths[getTypeIndex(width_ty)],249 self.bitcode.getTypeWidth(width_ty),
254 ),250 ),
255 .vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len),251 .vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len),
256 .char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)),252 .char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)),
...@@ -273,7 +269,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -273,7 +269,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
273 for (param) |x| {269 for (param) |x| {
274 try self.bitcode.writeBits(270 try self.bitcode.writeBits(
275 adapter.get(x, field_name),271 adapter.get(x, field_name),
276 self.bitcode.widths[getTypeIndex(width_ty)],272 self.bitcode.getTypeWidth(width_ty),
277 );273 );
278 }274 }
279 },275 },
...@@ -324,7 +320,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -324,7 +320,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
324 .fixed_runtime => |width_ty| {320 .fixed_runtime => |width_ty| {
325 try bitcode.writeBits(0, 1);321 try bitcode.writeBits(0, 1);
326 try bitcode.writeBits(1, 3);322 try bitcode.writeBits(1, 3);
327 try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5);323 try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
328 },324 },
329 .vbr => |width| {325 .vbr => |width| {
330 try bitcode.writeBits(0, 1);326 try bitcode.writeBits(0, 1);
...@@ -357,7 +353,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -357,7 +353,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
357 // Fixed or VBR op353 // Fixed or VBR op
358 try bitcode.writeBits(0, 1);354 try bitcode.writeBits(0, 1);
359 try bitcode.writeBits(1, 3);355 try bitcode.writeBits(1, 3);
360 try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5);356 try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
361 },357 },
362 .array_vbr => |width| {358 .array_vbr => |width| {
363 // Array op359 // Array op