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.
This commit is contained in:
peterino2 2026-11-03 08:51:20 -08:00
parent e05a217cc1
commit ab2641a57d
1 changed files with 47 additions and 36 deletions

View File

@ -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). // per edge, so a switch's repeated target counts once per case).
const block_start = try a.alloc(u32, nb + 1); const block_start = try a.alloc(u32, nb + 1);
const block_of = try a.alloc(u32, n); 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; var b: u32 = 0;
block_start[0] = 0; block_start[0] = 0;
for (insts, 0..) |inst, i| { for (insts, 0..) |inst, i| {
if (b >= nb) return; // not the block layout this pass expects if (b >= nb) return; // not the block layout this pass expects
block_of[i] = b; block_of[i] = b;
if (isTerminator(inst)) { switch (inst.op) {
b += 1; .alloca => try allocas.append(a, @intCast(i)),
block_start[b] = @intCast(i + 1); .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; 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); @memset(var_base, none);
const elem_ty = try a.alloc(TypeId, n); const elem_ty = try a.alloc(TypeId, n);
const direct = try a.alloc(bool, n); const direct = try a.alloc(bool, n);
for (insts, 0..) |inst, i| switch (inst.op) { for (allocas.items) |i| {
.alloca => |x| switch (m.types.items[x.elem]) { const x = insts[i].op.alloca;
switch (m.types.items[x.elem]) {
.array => |arr| if (isScalar(m, arr.elem) and arr.len > 0) { .array => |arr| if (isScalar(m, arr.elem) and arr.len > 0) {
var_base[i] = 0; var_base[i] = 0;
elem_ty[i] = arr.elem; 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; elem_ty[i] = x.elem;
direct[i] = true; direct[i] = true;
}, },
}, }
else => {}, }
};
// getelementptrs of candidates: the alloca and the element they // getelementptrs of candidates: the alloca and the element they
// address (`none` for a dynamic index, which pins its range). // address (`none` for a dynamic index, which pins its range).
const gep_alloca = try a.alloc(u32, n); 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 // (Code no path reaches, such as loops unrolling left behind, does
// not count.) // not count.)
for (insts, 0..) |inst, i| if (rpo_index[block_of[i]] != none) switch (inst.op) { for (geps.items) |i| if (rpo_index[block_of[i]] != none) {
.gep => |g| if (g.ptr == .inst and var_base[g.ptr.inst] != none and insts[g.ptr.inst].op == .alloca) { 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; const al = g.ptr.inst;
if (direct[al]) { if (direct[al]) {
var_base[al] = none; 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| { if (k) |kk| {
gep_alloca[i] = al; gep_alloca[i] = al;
gep_k[i] = kk; 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_alloca[i] = al;
gep_k[i] = none; gep_k[i] = none;
} else var_base[al] = none; } else var_base[al] = none;
}, }
else => {},
}; };
{ {
var scan: Scan = .{ .insts = insts, .var_base = var_base, .direct = direct, .gep_alloca = gep_alloca }; 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); 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. // 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; if (rpo_index[block_of[i]] == none) continue;
const inst = insts[i];
const ptr, const ty = switch (inst.op) { const ptr, const ty = switch (inst.op) {
.load => |x| .{ x.ptr, inst.ty }, .load => |x| .{ x.ptr, inst.ty },
.store => |x| .{ x.ptr, typeOf(m, f, x.val) }, .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. // Variables.
var nv: u32 = 0; var nv: u32 = 0;
for (insts, 0..) |inst, i| { for (allocas.items) |i| {
if (inst.op != .alloca or var_base[i] == none) continue; const inst = insts[i];
if (var_base[i] == none) continue;
var_base[i] = nv; var_base[i] = nv;
nv += if (direct[i]) 1 else @intCast(m.types.items[inst.op.alloca.elem].array.len); nv += if (direct[i]) 1 else @intCast(m.types.items[inst.op.alloca.elem].array.len);
} }
const var_ty = try a.alloc(TypeId, nv); const var_ty = try a.alloc(TypeId, nv);
for (insts, 0..) |inst, i| { for (allocas.items) |i| {
if (inst.op != .alloca or var_base[i] == none) continue; 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); 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]); @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); @memset(pinned, false);
const keep = try a.alloc(bool, n); const keep = try a.alloc(bool, n);
@memset(keep, false); @memset(keep, false);
for (insts, 0..) |inst, i| { for (geps.items) |i| {
if (inst.op != .gep or gep_alloca[i] == none or gep_k[i] != none) continue; if (gep_alloca[i] == none or gep_k[i] != none) continue;
const al = gep_alloca[i]; const al = gep_alloca[i];
if (var_base[al] == none) continue; if (var_base[al] == none) continue;
const len: u32 = @intCast(m.types.items[insts[al].op.alloca.elem].array.len); 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 lo = @min(r[0], len);
const hi = @min(r[1], len); const hi = @min(r[1], len);
@memset(pinned[var_base[al] + lo .. var_base[al] + hi], true); @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`. // The variable an instruction loads or stores, or `none`.
const access = try a.alloc(u32, n); const access = try a.alloc(u32, n);
@memset(access, none); @memset(access, none);
for (insts, 0..) |inst, i| { for (mems.items) |i| {
const ptr = switch (inst.op) { const ptr = switch (insts[i].op) {
.load => |x| x.ptr, .load => |x| x.ptr,
.store => |x| x.ptr, .store => |x| x.ptr,
else => continue, else => unreachable,
}; };
if (ptr != .inst) continue; if (ptr != .inst) continue;
const p = ptr.inst; 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); @memset(first_block, none);
const global = try a.alloc(bool, nv); const global = try a.alloc(bool, nv);
@memset(global, false); @memset(global, false);
for (insts, 0..) |inst, i| { for (mems.items) |i| {
const v = access[i]; const v = access[i];
if (v == none) continue; if (v == none) continue;
if (first_block[v] == none) { if (first_block[v] == none) {
first_block[v] = block_of[i]; 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; } else if (first_block[v] != block_of[i]) global[v] = true;
} }
const Def = struct { v: u32, b: u32 }; const Def = struct { v: u32, b: u32 };
var defs: std.ArrayList(Def) = .empty; var defs: std.ArrayList(Def) = .empty;
for (insts, 0..) |inst, i| { for (mems.items) |i| {
const v = access[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] }); try defs.append(a, .{ .v = v, .b = block_of[i] });
} }
std.sort.pdq(Def, defs.items, {}, struct { 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; filled[j] += 1;
} }
} }
for (insts, 0..) |inst, i| { for (mems.items) |i| {
if (access[i] != none and inst.op == .load and repl[i] == null) 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]]); 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, // The instructions that go: promoted allocas, their getelementptrs,
// loads and stores. // loads and stores.
const removed = try a.alloc(bool, n); const removed = try a.alloc(bool, n);
for (insts, 0..) |inst, i| removed[i] = switch (inst.op) { @memset(removed, false);
.alloca => var_base[i] != none and !keep[i], for (allocas.items) |i| removed[i] = 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]], 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]];
.load, .store => access[i] != none, for (mems.items) |i| removed[i] = access[i] != none;
else => false,
};
// What stays: instructions with effects, and what they read, // What stays: instructions with effects, and what they read,
// transitively; new phis likewise. (The loads promotion leaves dead, // transitively; new phis likewise. (The loads promotion leaves dead,