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 {
1294412944
1294512945pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]const u32 {
1294612946 const BitcodeWriter = bitcode_writer.BitcodeWriter(&.{ Type, FunctionAttributes });
12947 var bitcode = BitcodeWriter.init(allocator, &.{
12947 var bitcode = BitcodeWriter.init(allocator, .{
1294812948 std.math.log2_int_ceil(usize, self.type_items.items.len),
1294912949 std.math.log2_int_ceil(usize, 1 + self.function_attributes_set.count()),
1295012950 });
src/codegen/llvm/bitcode_writer.zig+8-12
......@@ -23,17 +23,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {
2323 bit_buffer: u32 = 0,
2424 bit_count: u5 = 0,
2525
26 widths: []const u16,
26 widths: [types.len]u16,
2727
28 pub fn getTypeIndex(comptime ty: type) usize {
29 inline for (types, 0..) |t, i| {
30 if (t == ty) return i;
31 }
32 unreachable;
28 pub fn getTypeWidth(self: BcWriter, comptime Type: type) u16 {
29 return self.widths[comptime std.mem.indexOfScalar(type, types, Type).?];
3330 }
3431
35 pub fn init(allocator: std.mem.Allocator, widths: []const u16) BcWriter {
36 std.debug.assert(widths.len == types.len);
32 pub fn init(allocator: std.mem.Allocator, widths: [types.len]u16) BcWriter {
3733 return .{
3834 .buffer = std.ArrayList(u32).init(allocator),
3935 .widths = widths,
......@@ -250,7 +246,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
250246 .fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len),
251247 .fixed_runtime => |width_ty| try self.bitcode.writeBits(
252248 adapter.get(param, field_name),
253 self.bitcode.widths[getTypeIndex(width_ty)],
249 self.bitcode.getTypeWidth(width_ty),
254250 ),
255251 .vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len),
256252 .char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)),
......@@ -273,7 +269,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
273269 for (param) |x| {
274270 try self.bitcode.writeBits(
275271 adapter.get(x, field_name),
276 self.bitcode.widths[getTypeIndex(width_ty)],
272 self.bitcode.getTypeWidth(width_ty),
277273 );
278274 }
279275 },
......@@ -324,7 +320,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
324320 .fixed_runtime => |width_ty| {
325321 try bitcode.writeBits(0, 1);
326322 try bitcode.writeBits(1, 3);
327 try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5);
323 try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
328324 },
329325 .vbr => |width| {
330326 try bitcode.writeBits(0, 1);
......@@ -357,7 +353,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
357353 // Fixed or VBR op
358354 try bitcode.writeBits(0, 1);
359355 try bitcode.writeBits(1, 3);
360 try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5);
356 try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
361357 },
362358 .array_vbr => |width| {
363359 // Array op