dxil: a by-value parameter's copy can be the argument's values
An inlined function's by-value parameter is copied into a local, and a struct parameter copied whole: every component a getelementptr and a store (and a bitcast or zext where storage mixes types), then a load per read, all of which promotion deletes again. Unreal passes its view state (thousands of components) by value: 00195 inlined a 6-instruction function 256 times and lowered 3.3 million instructions for it. A copy that is never written and only indexed by constants (regsLocals) is now the argument's values themselves: no alloca, stores or loads; reads take the components at their offset. - The hill-climbing set: 1,095 ms to 773 ms (-29%); 00195 -59%, Unreal's other shaders 10-28%, Glide's 1-4.5%. - Output: 2,853 of the 19,624 verify compiles change. Those checked differ only in the type table, or lose zext / icmp ne round trips of bools through i32 storage (the same code, smaller). - Validator 1.8: 11,488 Unreal compiles, 0 invalid; Glide's 8,772 the same 12 invalid as before this change.
This commit is contained in:
parent
9b2fc76c56
commit
e4d5bb5ddc
|
|
@ -77,6 +77,10 @@ const Lowered = union(enum) {
|
||||||
/// per resource, each an alias of what is stored into it.
|
/// per resource, each an alias of what is stored into it.
|
||||||
agg_ptr: struct { data: ?*const Lowered, slots: []const u32, ty: TypeId },
|
agg_ptr: struct { data: ?*const Lowered, slots: []const u32, ty: TypeId },
|
||||||
ptr: Ptr,
|
ptr: Ptr,
|
||||||
|
/// A by-value parameter's copy that is never written and only indexed
|
||||||
|
/// by constants (Emitter.regsLocals): the argument's components, from
|
||||||
|
/// `offset` on, with no memory behind them.
|
||||||
|
regs: struct { vals: []const Value, offset: u32 = 0 },
|
||||||
/// A resource handle (index into Emitter.resources).
|
/// A resource handle (index into Emitter.resources).
|
||||||
handle: u32,
|
handle: u32,
|
||||||
/// A local holding a resource (index into Emitter.res_locals): an
|
/// A local holding a resource (index into Emitter.res_locals): an
|
||||||
|
|
@ -169,6 +173,8 @@ const Resource = struct {
|
||||||
|
|
||||||
const Frame = struct {
|
const Frame = struct {
|
||||||
f: *const ir.Function,
|
f: *const ir.Function,
|
||||||
|
/// `f`'s index (inlined calls; see Emitter.regsLocals).
|
||||||
|
fi: u32 = 0,
|
||||||
vals: []Lowered,
|
vals: []Lowered,
|
||||||
params: []const Lowered,
|
params: []const Lowered,
|
||||||
blocks: []?u32,
|
blocks: []?u32,
|
||||||
|
|
@ -206,7 +212,7 @@ fn ValueMap(comptime V: type) type {
|
||||||
|
|
||||||
const ValueContext = struct {
|
const ValueContext = struct {
|
||||||
fn word(v: Value) u64 {
|
fn word(v: Value) u64 {
|
||||||
return @as(u64, @intFromEnum(v)) << 32 | switch (v) {
|
return @as(u64, @backingInt(v)) << 32 | switch (v) {
|
||||||
inline else => |x| x,
|
inline else => |x| x,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -331,6 +337,7 @@ const Emitter = struct {
|
||||||
writes: u32 = 0,
|
writes: u32 = 0,
|
||||||
/// paramCopies per IR function.
|
/// paramCopies per IR function.
|
||||||
param_copies: std.AutoHashMapUnmanaged(u32, []const u32) = .empty,
|
param_copies: std.AutoHashMapUnmanaged(u32, []const u32) = .empty,
|
||||||
|
regs_locals: std.AutoHashMapUnmanaged(u32, []const bool) = .empty,
|
||||||
uses_barycentrics: bool = false,
|
uses_barycentrics: bool = false,
|
||||||
uses_sampler_heap: bool = false,
|
uses_sampler_heap: bool = false,
|
||||||
/// The shader's reflection (reflect.zig), made with the object; owned
|
/// The shader's reflection (reflect.zig), made with the object; owned
|
||||||
|
|
@ -1774,6 +1781,7 @@ const Emitter = struct {
|
||||||
const f = e.m.functions.items[fi];
|
const f = e.m.functions.items[fi];
|
||||||
var frame: Frame = .{
|
var frame: Frame = .{
|
||||||
.f = f,
|
.f = f,
|
||||||
|
.fi = fi,
|
||||||
.vals = try e.arena.alloc(Lowered, f.insts.items.len),
|
.vals = try e.arena.alloc(Lowered, f.insts.items.len),
|
||||||
.params = args,
|
.params = args,
|
||||||
.blocks = try e.arena.alloc(?u32, f.blocks.items.len),
|
.blocks = try e.arena.alloc(?u32, f.blocks.items.len),
|
||||||
|
|
@ -1867,6 +1875,28 @@ const Emitter = struct {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For each instruction of IR function `fi`: whether it is a local
|
||||||
|
/// paramCopies gives a parameter for, indexed only by constants (so it
|
||||||
|
/// can be the argument's values; see Lowered.regs).
|
||||||
|
fn regsLocals(e: *Emitter, fi: u32) Error![]const bool {
|
||||||
|
const gop = try e.regs_locals.getOrPut(e.arena, fi);
|
||||||
|
if (gop.found_existing) return gop.value_ptr.*;
|
||||||
|
const f = e.m.functions.items[fi];
|
||||||
|
const copies = try e.paramCopies(fi);
|
||||||
|
const out = try e.arena.alloc(bool, copies.len);
|
||||||
|
for (out, copies) |*o, c| o.* = c != std.math.maxInt(u32);
|
||||||
|
for (f.insts.items) |inst| if (inst.op == .access) {
|
||||||
|
var root = inst.op.access.base;
|
||||||
|
while (f.inst(root).op == .access) root = f.inst(root).op.access.base;
|
||||||
|
if (f.inst(root).op != .local_var) continue;
|
||||||
|
for (inst.op.access.indices) |ix| if (f.inst(ix).op != .constant or f.inst(ix).op.constant != .int) {
|
||||||
|
out[root.index()] = false;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
gop.value_ptr.* = out;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/// For each instruction of IR function `fi`: the parameter a
|
/// For each instruction of IR function `fi`: the parameter a
|
||||||
/// `local_var` only ever holds a copy of (stored once, from that
|
/// `local_var` only ever holds a copy of (stored once, from that
|
||||||
/// by-value parameter, and otherwise only read), else maxInt.
|
/// by-value parameter, and otherwise only read), else maxInt.
|
||||||
|
|
@ -2209,6 +2239,13 @@ const Emitter = struct {
|
||||||
const k = frame.param_copies[r.index()];
|
const k = frame.param_copies[r.index()];
|
||||||
if (k != std.math.maxInt(u32)) if (frame.param_srcs[k]) |alias| return alias;
|
if (k != std.math.maxInt(u32)) if (frame.param_srcs[k]) |alias| return alias;
|
||||||
}
|
}
|
||||||
|
// A copy of a parameter passed as values, never written:
|
||||||
|
// those values (no alloca, no stores, no loads).
|
||||||
|
if (frame.copies.len > 0) {
|
||||||
|
const k = frame.copies[r.index()];
|
||||||
|
if (k != std.math.maxInt(u32) and frame.params[k] == .scalars and (try e.regsLocals(frame.fi))[r.index()])
|
||||||
|
return .{ .regs = .{ .vals = frame.params[k].scalars } };
|
||||||
|
}
|
||||||
if (e.payload_local) |pl| if (pl.func == f and pl.inst == r.index()) {
|
if (e.payload_local) |pl| if (pl.func == f and pl.inst == r.index()) {
|
||||||
const dty = try e.memoryType(i.ty);
|
const dty = try e.memoryType(i.ty);
|
||||||
const base = try e.addPrologue(try e.dm.ptrType(dty), .{ .alloca = .{ .elem = dty, .size = try e.i32Const(1) } });
|
const base = try e.addPrologue(try e.dm.ptrType(dty), .{ .alloca = .{ .elem = dty, .size = try e.i32Const(1) } });
|
||||||
|
|
@ -2249,6 +2286,11 @@ const Emitter = struct {
|
||||||
return frame.params[frame.copies[p.index()]];
|
return frame.params[frame.copies[p.index()]];
|
||||||
} else switch (frame.vals[p.index()]) {
|
} else switch (frame.vals[p.index()]) {
|
||||||
.handle => |h| return .{ .handle = h },
|
.handle => |h| return .{ .handle = h },
|
||||||
|
.regs => |rg| {
|
||||||
|
const n = try e.flatCount(i.ty);
|
||||||
|
if (rg.offset + n > rg.vals.len) return .{ .scalars = try e.undefValues(i.ty) };
|
||||||
|
return .{ .scalars = rg.vals[rg.offset..][0..n] };
|
||||||
|
},
|
||||||
.agg_ptr => |ap| {
|
.agg_ptr => |ap| {
|
||||||
const vals: []const Value = if (ap.data) |d| switch (d.*) {
|
const vals: []const Value = if (ap.data) |d| switch (d.*) {
|
||||||
.ptr => |ptr| (try e.loadPtr(ptr, ap.ty)).scalars,
|
.ptr => |ptr| (try e.loadPtr(ptr, ap.ty)).scalars,
|
||||||
|
|
@ -2316,6 +2358,8 @@ const Emitter = struct {
|
||||||
else => |v| return e.unsupported("internal: load from a non-pointer ({s} from %{d} '{s}' in '{s}')", .{ @tagName(v), p.index(), @tagName(frame.f.inst(p).op), frame.f.name }),
|
else => |v| return e.unsupported("internal: load from a non-pointer ({s} from %{d} '{s}' in '{s}')", .{ @tagName(v), p.index(), @tagName(frame.f.inst(p).op), frame.f.name }),
|
||||||
},
|
},
|
||||||
.store => |s| {
|
.store => |s| {
|
||||||
|
// (The copy into a parameter's copy kept as its values.)
|
||||||
|
if (frame.vals[s.ptr.index()] == .regs) return .none;
|
||||||
if (frame.param_copies.len > 0) if (f.inst(s.value).op == .param) {
|
if (frame.param_copies.len > 0) if (f.inst(s.value).op == .param) {
|
||||||
const k = f.inst(s.value).op.param;
|
const k = f.inst(s.value).op.param;
|
||||||
if (frame.param_copies[s.ptr.index()] == k and frame.param_srcs[k] != null) return .none;
|
if (frame.param_copies[s.ptr.index()] == k and frame.param_srcs[k] != null) return .none;
|
||||||
|
|
@ -2400,6 +2444,31 @@ const Emitter = struct {
|
||||||
return .{ .typed = tp };
|
return .{ .typed = tp };
|
||||||
}
|
}
|
||||||
if (frame.vals[a.base.index()] == .agg_ptr) return e.aggAccess(frame, frame.vals[a.base.index()].agg_ptr, a.indices);
|
if (frame.vals[a.base.index()] == .agg_ptr) return e.aggAccess(frame, frame.vals[a.base.index()].agg_ptr, a.indices);
|
||||||
|
if (frame.vals[a.base.index()] == .regs) {
|
||||||
|
var rg = frame.vals[a.base.index()].regs;
|
||||||
|
var cur = f.inst(a.base).ty;
|
||||||
|
for (a.indices) |x| {
|
||||||
|
const k: u32 = @intCast(@max(f.inst(x).op.constant.int, 0));
|
||||||
|
switch (e.t.get(cur)) {
|
||||||
|
.@"struct" => |st| {
|
||||||
|
if (k >= st.fields.items.len) return e.unsupported("struct member index out of range", .{});
|
||||||
|
for (st.fields.items[0..k]) |fld| rg.offset += try e.flatCount(fld.type);
|
||||||
|
cur = st.fields.items[k].type;
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
const elem: TypeId = switch (e.t.get(cur)) {
|
||||||
|
.array => |arr| arr.elem,
|
||||||
|
.vector => |v| try e.t.scalar(v.elem),
|
||||||
|
.matrix => |mt| try e.t.vector(mt.elem, mt.cols),
|
||||||
|
else => return e.unsupported("indexing this type is not supported yet", .{}),
|
||||||
|
};
|
||||||
|
rg.offset += k * try e.flatCount(elem);
|
||||||
|
cur = elem;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return .{ .regs = rg };
|
||||||
|
}
|
||||||
// An element of a local array of resources: of the array
|
// An element of a local array of resources: of the array
|
||||||
// it aliases.
|
// it aliases.
|
||||||
if (frame.vals[a.base.index()] == .res_local) {
|
if (frame.vals[a.base.index()] == .res_local) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue