From ab2641a57dcb2996a574e216498d09eaba1dc4b9 Mon Sep 17 00:00:00 2001 From: peterino2 Date: Tue, 3 Nov 2026 08:51:20 -0800 Subject: [PATCH] promote: walk lists of allocas, getelementptrs and memory accesses Of promotion's 16 passes over every instruction, 11 look only at allocas, getelementptrs or loads and stores. The first pass now lists those, and the others walk the lists. The hill-climbing set: -2.1% (A/B); output unchanged on the verify corpus. --- src/dxil/promote.zig | 83 +++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/src/dxil/promote.zig b/src/dxil/promote.zig index 1e477ed..a62838b 100644 --- a/src/dxil/promote.zig +++ b/src/dxil/promote.zig @@ -137,15 +137,25 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM // per edge, so a switch's repeated target counts once per case). const block_start = try a.alloc(u32, nb + 1); const block_of = try a.alloc(u32, n); + // The allocas, getelementptrs, and loads and stores, in order: the + // passes over just those walk these, not every instruction. + var allocas: std.ArrayList(u32) = .empty; + var geps: std.ArrayList(u32) = .empty; + var mems: std.ArrayList(u32) = .empty; { var b: u32 = 0; block_start[0] = 0; for (insts, 0..) |inst, i| { if (b >= nb) return; // not the block layout this pass expects block_of[i] = b; - if (isTerminator(inst)) { - b += 1; - block_start[b] = @intCast(i + 1); + switch (inst.op) { + .alloca => try allocas.append(a, @intCast(i)), + .gep => try geps.append(a, @intCast(i)), + .load, .store => try mems.append(a, @intCast(i)), + else => if (isTerminator(inst)) { + b += 1; + block_start[b] = @intCast(i + 1); + }, } } if (b != nb) return; @@ -271,8 +281,9 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM @memset(var_base, none); const elem_ty = try a.alloc(TypeId, n); const direct = try a.alloc(bool, n); - for (insts, 0..) |inst, i| switch (inst.op) { - .alloca => |x| switch (m.types.items[x.elem]) { + for (allocas.items) |i| { + const x = insts[i].op.alloca; + switch (m.types.items[x.elem]) { .array => |arr| if (isScalar(m, arr.elem) and arr.len > 0) { var_base[i] = 0; elem_ty[i] = arr.elem; @@ -283,9 +294,8 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM elem_ty[i] = x.elem; direct[i] = true; }, - }, - else => {}, - }; + } + } // getelementptrs of candidates: the alloca and the element they // address (`none` for a dynamic index, which pins its range). const gep_alloca = try a.alloc(u32, n); @@ -315,8 +325,9 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM }; // (Code no path reaches, such as loops unrolling left behind, does // not count.) - for (insts, 0..) |inst, i| if (rpo_index[block_of[i]] != none) switch (inst.op) { - .gep => |g| if (g.ptr == .inst and var_base[g.ptr.inst] != none and insts[g.ptr.inst].op == .alloca) { + for (geps.items) |i| if (rpo_index[block_of[i]] != none) { + const g = insts[i].op.gep; + if (g.ptr == .inst and var_base[g.ptr.inst] != none and insts[g.ptr.inst].op == .alloca) { const al = g.ptr.inst; if (direct[al]) { var_base[al] = none; @@ -326,20 +337,20 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM if (k) |kk| { gep_alloca[i] = al; gep_k[i] = kk; - } else if (dyn.contains(@intCast(i)) and g.indices.len == 2 and constInt(m, g.indices[0]) == 0) { + } else if (dyn.contains(i) and g.indices.len == 2 and constInt(m, g.indices[0]) == 0) { gep_alloca[i] = al; gep_k[i] = none; } else var_base[al] = none; - }, - else => {}, + } }; { var scan: Scan = .{ .insts = insts, .var_base = var_base, .direct = direct, .gep_alloca = gep_alloca }; for (insts, 0..) |*inst, i| if (rpo_index[block_of[i]] != none) try operands(false, a, inst, &scan); } // Loads and stores must move whole elements of the element type. - for (insts, 0..) |inst, i| { + for (mems.items) |i| { if (rpo_index[block_of[i]] == none) continue; + const inst = insts[i]; const ptr, const ty = switch (inst.op) { .load => |x| .{ x.ptr, inst.ty }, .store => |x| .{ x.ptr, typeOf(m, f, x.val) }, @@ -352,14 +363,16 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM // Variables. var nv: u32 = 0; - for (insts, 0..) |inst, i| { - if (inst.op != .alloca or var_base[i] == none) continue; + for (allocas.items) |i| { + const inst = insts[i]; + if (var_base[i] == none) continue; var_base[i] = nv; nv += if (direct[i]) 1 else @intCast(m.types.items[inst.op.alloca.elem].array.len); } const var_ty = try a.alloc(TypeId, nv); - for (insts, 0..) |inst, i| { - if (inst.op != .alloca or var_base[i] == none) continue; + for (allocas.items) |i| { + const inst = insts[i]; + if (var_base[i] == none) continue; const len: u32 = if (direct[i]) 1 else @intCast(m.types.items[inst.op.alloca.elem].array.len); @memset(var_ty[var_base[i]..][0..len], elem_ty[i]); } @@ -369,12 +382,12 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM @memset(pinned, false); const keep = try a.alloc(bool, n); @memset(keep, false); - for (insts, 0..) |inst, i| { - if (inst.op != .gep or gep_alloca[i] == none or gep_k[i] != none) continue; + for (geps.items) |i| { + if (gep_alloca[i] == none or gep_k[i] != none) continue; const al = gep_alloca[i]; if (var_base[al] == none) continue; const len: u32 = @intCast(m.types.items[insts[al].op.alloca.elem].array.len); - const r = dyn.get(@intCast(i)).?; + const r = dyn.get(i).?; const lo = @min(r[0], len); const hi = @min(r[1], len); @memset(pinned[var_base[al] + lo .. var_base[al] + hi], true); @@ -383,11 +396,11 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM // The variable an instruction loads or stores, or `none`. const access = try a.alloc(u32, n); @memset(access, none); - for (insts, 0..) |inst, i| { - const ptr = switch (inst.op) { + for (mems.items) |i| { + const ptr = switch (insts[i].op) { .load => |x| x.ptr, .store => |x| x.ptr, - else => continue, + else => unreachable, }; if (ptr != .inst) continue; const p = ptr.inst; @@ -404,19 +417,19 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM @memset(first_block, none); const global = try a.alloc(bool, nv); @memset(global, false); - for (insts, 0..) |inst, i| { + for (mems.items) |i| { const v = access[i]; if (v == none) continue; if (first_block[v] == none) { first_block[v] = block_of[i]; - if (inst.op == .load) global[v] = true; + if (insts[i].op == .load) global[v] = true; } else if (first_block[v] != block_of[i]) global[v] = true; } const Def = struct { v: u32, b: u32 }; var defs: std.ArrayList(Def) = .empty; - for (insts, 0..) |inst, i| { + for (mems.items) |i| { const v = access[i]; - if (v == none or inst.op != .store or !global[v] or rpo_index[block_of[i]] == none) continue; + if (v == none or insts[i].op != .store or !global[v] or rpo_index[block_of[i]] == none) continue; try defs.append(a, .{ .v = v, .b = block_of[i] }); } std.sort.pdq(Def, defs.items, {}, struct { @@ -556,8 +569,8 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM filled[j] += 1; } } - for (insts, 0..) |inst, i| { - if (access[i] != none and inst.op == .load and repl[i] == null) + for (mems.items) |i| { + if (access[i] != none and insts[i].op == .load and repl[i] == null) repl[i] = try Zero.get(a, m, &zeros, var_ty[access[i]]); } @@ -612,12 +625,10 @@ pub fn run(a: Allocator, m: *module.Module, func: u32, dyn: *const std.AutoHashM // The instructions that go: promoted allocas, their getelementptrs, // loads and stores. const removed = try a.alloc(bool, n); - for (insts, 0..) |inst, i| removed[i] = switch (inst.op) { - .alloca => var_base[i] != none and !keep[i], - .gep => gep_alloca[i] != none and var_base[gep_alloca[i]] != none and gep_k[i] != none and !pinned[var_base[gep_alloca[i]] + gep_k[i]], - .load, .store => access[i] != none, - else => false, - }; + @memset(removed, false); + for (allocas.items) |i| removed[i] = var_base[i] != none and !keep[i]; + for (geps.items) |i| removed[i] = gep_alloca[i] != none and var_base[gep_alloca[i]] != none and gep_k[i] != none and !pinned[var_base[gep_alloca[i]] + gep_k[i]]; + for (mems.items) |i| removed[i] = access[i] != none; // What stays: instructions with effects, and what they read, // transitively; new phis likewise. (The loads promotion leaves dead,