dxil: share getelementptr index slices, cache elementPtr's pointer type

Each element access allocated its { 0, k } index pair and looked its
pointer type up; constant pairs are now shared (by the index constant)
and the last pointer type kept. loadPtr also skips the forwarding lookup
when nothing is forwarded.

The hill-climbing set: -7.8% (lowering -12.7%, its allocation calls
halved); output unchanged on the verify corpus.
This commit is contained in:
peterino2 2026-11-03 08:19:43 -08:00
parent d3538e1db3
commit c423c0c1a0
1 changed files with 22 additions and 3 deletions

View File

@ -318,6 +318,10 @@ const Emitter = struct {
struct_types: std.AutoHashMapUnmanaged(TypeId, DType) = .empty, struct_types: std.AutoHashMapUnmanaged(TypeId, DType) = .empty,
/// Per type: see structComps, mixedSizes and mixedStorage. /// Per type: see structComps, mixedSizes and mixedStorage.
struct_comps: std.AutoHashMapUnmanaged(TypeId, []const TypeId) = .empty, struct_comps: std.AutoHashMapUnmanaged(TypeId, []const TypeId) = .empty,
/// elementPtr's last pointer type, and its shared index slices (by the
/// index constant).
elem_ptr_ty: struct { elem: DType = std.math.maxInt(DType), space: u32 = 0, ty: DType = 0 } = .{},
zero_indexed: std.ArrayList([]const Value) = .empty,
mixed_sizes: std.AutoHashMapUnmanaged(TypeId, bool) = .empty, mixed_sizes: std.AutoHashMapUnmanaged(TypeId, bool) = .empty,
mixed_storage: std.AutoHashMapUnmanaged(TypeId, bool) = .empty, mixed_storage: std.AutoHashMapUnmanaged(TypeId, bool) = .empty,
/// Prologue instructions dropped again if unused (resource handles). /// Prologue instructions dropped again if unused (resource handles).
@ -2826,7 +2830,7 @@ const Emitter = struct {
const n = try e.flatCount(ty); const n = try e.flatCount(ty);
const out = try e.arena.alloc(Value, n); const out = try e.arena.alloc(Value, n);
for (out, 0..) |*o, c| { for (out, 0..) |*o, c| {
if (ptr.offset_dyn == null) if (e.forwarded.get(.{ ptr.base, ptr.offset_const + @as(u32, @intCast(c)) })) |fw| if (fw.block == e.cur) { if (ptr.offset_dyn == null and e.forwarded.count() > 0) if (e.forwarded.get(.{ ptr.base, ptr.offset_const + @as(u32, @intCast(c)) })) |fw| if (fw.block == e.cur) {
o.* = fw.v; o.* = fw.v;
continue; continue;
}; };
@ -2878,12 +2882,27 @@ const Emitter = struct {
fn elementPtr(e: *Emitter, ptr: Ptr, c: u32) Error!Value { fn elementPtr(e: *Emitter, ptr: Ptr, c: u32) Error!Value {
var idx = try e.i32Const(ptr.offset_const + c); var idx = try e.i32Const(ptr.offset_const + c);
if (ptr.offset_dyn) |d| idx = try e.add(try e.dm.intType(32), .{ .binop = .{ .op = .add, .lhs = d, .rhs = idx } }); if (ptr.offset_dyn) |d| idx = try e.add(try e.dm.intType(32), .{ .binop = .{ .op = .add, .lhs = d, .rhs = idx } });
const pt = try e.dm.getType(.{ .pointer = .{ .elem = ptr.scalar_ty, .addr_space = ptr.addr_space } }); if (e.elem_ptr_ty.elem != ptr.scalar_ty or e.elem_ptr_ty.space != ptr.addr_space) e.elem_ptr_ty = .{
const gep = try e.add(pt, .{ .gep = .{ .elem = ptr.array_ty, .ptr = ptr.base, .indices = try e.arena.dupe(Value, &.{ try e.i32Const(0), idx }) } }); .elem = ptr.scalar_ty,
.space = ptr.addr_space,
.ty = try e.dm.getType(.{ .pointer = .{ .elem = ptr.scalar_ty, .addr_space = ptr.addr_space } }),
};
const zero = try e.i32Const(0);
const gep = try e.add(e.elem_ptr_ty.ty, .{ .gep = .{ .elem = ptr.array_ty, .ptr = ptr.base, .indices = try e.zeroIndexed(zero, idx) } });
if (ptr.offset_dyn != null) if (ptr.dyn_range) |r| try e.dyn_ranges.put(e.arena, gep.inst, r); if (ptr.offset_dyn != null) if (ptr.dyn_range) |r| try e.dyn_ranges.put(e.arena, gep.inst, r);
return gep; return gep;
} }
/// The indices `{ 0, idx }`, shared between getelementptrs with the
/// same constant `idx` (no pass writes operand slices).
fn zeroIndexed(e: *Emitter, zero: Value, idx: Value) Error![]const Value {
if (idx != .constant) return e.arena.dupe(Value, &.{ zero, idx });
const k = idx.constant;
if (k >= e.zero_indexed.items.len) try e.zero_indexed.appendNTimes(e.arena, &.{}, k + 1 - e.zero_indexed.items.len);
if (e.zero_indexed.items[k].len == 0) e.zero_indexed.items[k] = try e.arena.dupe(Value, &.{ zero, idx });
return e.zero_indexed.items[k];
}
fn convertScalar(e: *Emitter, v: Value, from: Scalar, to: Scalar) Error!Value { fn convertScalar(e: *Emitter, v: Value, from: Scalar, to: Scalar) Error!Value {
if (from == to) return v; if (from == to) return v;
const tt = try e.scalarTy(to); const tt = try e.scalarTy(to);