authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-16 06:27:56-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-16 06:28:41-04:00
log9d1820d20635ade6cb2dbfc36744353715653670
treec4efb8a3cf8a31153af696120b2a6a39202336f9
parentb0fe7eef54dcaab8f7f0c18be042bf1876274ca4

InternPool: reduce max tid width by one bit

@mlugg keeps stealing my bits!

3 files changed, 12 insertions(+), 8 deletions(-)

src/InternPool.zig+7-4
......@@ -10,6 +10,8 @@ shards: []Shard = &.{},
1010global_error_set: GlobalErrorSet = GlobalErrorSet.empty,
1111/// Cached number of active bits in a `tid`.
1212tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,
13/// Cached shift amount to put a `tid` in the top bits of a 30-bit value.
14tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
1315/// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
1416tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
1517/// Cached shift amount to put a `tid` in the top bits of a 32-bit value.
......@@ -4091,8 +4093,8 @@ pub const Index = enum(u32) {
40914093
40924094 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) Index {
40934095 assert(@intFromEnum(unwrapped.tid) <= ip.getTidMask());
4094 assert(unwrapped.index <= ip.getIndexMask(u31));
4095 return @enumFromInt(@as(u32, @intFromEnum(unwrapped.tid)) << ip.tid_shift_31 | unwrapped.index);
4096 assert(unwrapped.index <= ip.getIndexMask(u30));
4097 return @enumFromInt(@as(u32, @intFromEnum(unwrapped.tid)) << ip.tid_shift_30 | unwrapped.index);
40964098 }
40974099
40984100 pub fn getExtra(unwrapped: Unwrapped, ip: *const InternPool) Local.Extra {
......@@ -4131,8 +4133,8 @@ pub const Index = enum(u32) {
41314133 .tid = .main,
41324134 .index = @intFromEnum(index),
41334135 } else .{
4134 .tid = @enumFromInt(@intFromEnum(index) >> ip.tid_shift_31 & ip.getTidMask()),
4135 .index = @intFromEnum(index) & ip.getIndexMask(u31),
4136 .tid = @enumFromInt(@intFromEnum(index) >> ip.tid_shift_30 & ip.getTidMask()),
4137 .index = @intFromEnum(index) & ip.getIndexMask(u30),
41364138 };
41374139 }
41384140
......@@ -5822,6 +5824,7 @@ pub fn init(ip: *InternPool, gpa: Allocator, available_threads: usize) !void {
58225824 });
58235825
58245826 ip.tid_width = @intCast(std.math.log2_int_ceil(usize, used_threads));
5827 ip.tid_shift_30 = if (single_threaded) 0 else 30 - ip.tid_width;
58255828 ip.tid_shift_31 = if (single_threaded) 0 else 31 - ip.tid_width;
58265829 ip.tid_shift_32 = if (single_threaded) 0 else ip.tid_shift_31 +| 1;
58275830 ip.shards = try gpa.alloc(Shard, @as(usize, 1) << ip.tid_width);
src/Zcu/PerThread.zig+2-1
......@@ -3,7 +3,8 @@ zcu: *Zcu,
33/// Dense, per-thread unique index.
44tid: Id,
55
6pub const Id = if (InternPool.single_threaded) enum { main } else enum(u8) { main, _ };
6pub const IdBacking = u7;
7pub const Id = if (InternPool.single_threaded) enum { main } else enum(IdBacking) { main, _ };
78
89pub fn destroyDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) void {
910 const zcu = pt.zcu;
src/main.zig+3-3
......@@ -3110,7 +3110,7 @@ fn buildOutputType(
31103110 var thread_pool: ThreadPool = undefined;
31113111 try thread_pool.init(.{
31123112 .allocator = gpa,
3113 .n_jobs = @min(@max(n_jobs orelse std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(u8)),
3113 .n_jobs = @min(@max(n_jobs orelse std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(Zcu.PerThread.IdBacking)),
31143114 .track_ids = true,
31153115 });
31163116 defer thread_pool.deinit();
......@@ -4961,7 +4961,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49614961 var thread_pool: ThreadPool = undefined;
49624962 try thread_pool.init(.{
49634963 .allocator = gpa,
4964 .n_jobs = @min(@max(n_jobs orelse std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(u8)),
4964 .n_jobs = @min(@max(n_jobs orelse std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(Zcu.PerThread.IdBacking)),
49654965 .track_ids = true,
49664966 });
49674967 defer thread_pool.deinit();
......@@ -5399,7 +5399,7 @@ fn jitCmd(
53995399 var thread_pool: ThreadPool = undefined;
54005400 try thread_pool.init(.{
54015401 .allocator = gpa,
5402 .n_jobs = @min(@max(std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(u8)),
5402 .n_jobs = @min(@max(std.Thread.getCpuCount() catch 1, 1), std.math.maxInt(Zcu.PerThread.IdBacking)),
54035403 .track_ids = true,
54045404 });
54055405 defer thread_pool.deinit();