From 35dedc171ebfd5ba544832a5c5b31a3c04140fe7 Mon Sep 17 00:00:00 2001 From: peterino2 Date: Tue, 3 Nov 2026 11:05:35 -0800 Subject: [PATCH] unroll: a loop's blocks in order from its bits, not a sort Each loop's block list was sorted after the walk that found its blocks, for every loop on each of unrollOne's calls (230 calls of about 200 loops on 00828). The body's bitset already holds them in order: they are read out of its words. The hill-climbing set: -8.1% (A/B); output unchanged on the verify corpus. --- src/dxil/unroll.zig | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/dxil/unroll.zig b/src/dxil/unroll.zig index b094d60..587b50b 100644 --- a/src/dxil/unroll.zig +++ b/src/dxil/unroll.zig @@ -373,20 +373,28 @@ fn unrollOne(a: Allocator, m: *module.Module, func: u32, budget: *usize, tried: if (latches.items.len == 0) continue; const bits = try a.alloc(u64, (nb + 63) / 64); @memset(bits, 0); - var blocks: std.ArrayList(u32) = .empty; var body: Body = .{ .bits = bits, .blocks = &.{} }; body.add(h); - try blocks.append(a, h); + var count: usize = 1; var stack: std.ArrayList(u32) = .empty; try stack.appendSlice(a, latches.items); while (stack.pop()) |x| { if (body.has(x)) continue; body.add(x); - try blocks.append(a, x); + count += 1; for (cfg.preds[x]) |p| if (cfg.idom[p] != none) try stack.append(a, p); } - std.sort.pdq(u32, blocks.items, {}, std.sort.asc(u32)); - body.blocks = blocks.items; + // The blocks in order, from the bits. + const blocks = try a.alloc(u32, count); + var k: usize = 0; + for (bits, 0..) |word, wi| { + var w = word; + while (w != 0) : (w &= w - 1) { + blocks[k] = @intCast(wi * 64 + @ctz(w)); + k += 1; + } + } + body.blocks = blocks; try loops.append(a, .{ .h = h, .body = body, .latches = latches.items }); }