Unroll small loops only, and SCCP in reverse postorder
The Unreal work unrolled far more than dxc does: the IR unrolled any
small constant-count loop, and the DXIL unroller any loop of up to 256
trips. On Glide's GlimmerProbeTrace and GlimmerSHUpdate compiles took
10-17x longer and DXIL grew to 470 KB (dxc: 155 KB), so Glide's
replayed compiles took 2.7x as long as with main.
- the IR's automatic unrolling is gone again (it did little for Unreal)
- DXIL unrolls a loop only when unrolled it stays within 300
instructions, as dxc unrolls only small loops without [unroll]
- SCCP visits blocks in reverse postorder, so it settles in a few passes
Glide, against main (bb942b0): 24 threads 1.36 s vs 1.25 s, serial sum
13.2 s vs 11.8 s, DXIL 58.1 MB vs 61.4 MB. Unreal's corpus still
compiles, validates, and reflects no UAV dxc leaves out.
This commit is contained in:
parent
a80a77c0b8
commit
72f1760844
|
|
@ -510,7 +510,6 @@ fn run(ctx: *Ctx, arena: std.mem.Allocator) !void {
|
|||
if (opts.parse or opts.sema) return;
|
||||
if (profile == null) return ctx.fail("missing -T <profile>", .{});
|
||||
if (module) |*m| {
|
||||
m.auto_unroll = !opts.spirv and !opts.no_opt and opts.opt_level != .od;
|
||||
try zdxc.ir.opt.run(arena, m);
|
||||
if (opts.spirv) {
|
||||
zdxc.ir.split_resources.run(arena, m, &diags) catch |err| switch (err) {
|
||||
|
|
|
|||
|
|
@ -79,6 +79,26 @@ fn isTerminator(inst: Inst) bool {
|
|||
};
|
||||
}
|
||||
|
||||
/// A terminator's successor blocks.
|
||||
const Succs = struct {
|
||||
br: [2]u32 = undefined,
|
||||
sw: ?@FieldType(@FieldType(Inst, "op"), "switch") = null,
|
||||
len: usize = 0,
|
||||
|
||||
fn get(s: Succs, k: usize) u32 {
|
||||
if (s.sw) |sw| return if (k == 0) sw.default else sw.cases[k - 1].block;
|
||||
return s.br[k];
|
||||
}
|
||||
};
|
||||
|
||||
fn successors(term: Inst) Succs {
|
||||
return switch (term.op) {
|
||||
.br => |br| if (br.@"else") |e| .{ .br = .{ br.then, e }, .len = 2 } else .{ .br = .{ br.then, 0 }, .len = 1 },
|
||||
.@"switch" => |sw| .{ .sw = sw, .len = 1 + sw.cases.len },
|
||||
else => .{},
|
||||
};
|
||||
}
|
||||
|
||||
/// Runs the propagation on `func`; true if it changed anything.
|
||||
pub fn run(a: Allocator, m: *module.Module, func: u32) Allocator.Error!bool {
|
||||
const f = &m.functions.items[func];
|
||||
|
|
@ -217,15 +237,45 @@ pub fn run(a: Allocator, m: *module.Module, func: u32) Allocator.Error!bool {
|
|||
};
|
||||
const ev: Eval = .{ .m = m, .f = f, .lat = lat };
|
||||
|
||||
// Iterated to the fixed point in block order (the lattice is three
|
||||
// levels deep, so this ends quickly).
|
||||
// Blocks in reverse postorder: definitions before uses except round
|
||||
// loops, so the fixed point takes a few passes (the lattice is three
|
||||
// levels deep).
|
||||
var rpo: std.ArrayList(u32) = .empty;
|
||||
{
|
||||
var post: std.ArrayList(u32) = .empty;
|
||||
const seen = try a.alloc(bool, nb);
|
||||
@memset(seen, false);
|
||||
const Fr = struct { b: u32, next: u32 };
|
||||
var stack: std.ArrayList(Fr) = .empty;
|
||||
try stack.append(a, .{ .b = 0, .next = 0 });
|
||||
seen[0] = true;
|
||||
while (stack.items.len > 0) {
|
||||
const top = &stack.items[stack.items.len - 1];
|
||||
const succs = successors(insts[block_start[top.b + 1] - 1]);
|
||||
if (top.next < succs.len) {
|
||||
const t = succs.get(top.next);
|
||||
top.next += 1;
|
||||
if (!seen[t]) {
|
||||
seen[t] = true;
|
||||
try stack.append(a, .{ .b = t, .next = 0 });
|
||||
}
|
||||
} else {
|
||||
try post.append(a, top.b);
|
||||
_ = stack.pop();
|
||||
}
|
||||
}
|
||||
var k = post.items.len;
|
||||
while (k > 0) {
|
||||
k -= 1;
|
||||
try rpo.append(a, post.items[k]);
|
||||
}
|
||||
}
|
||||
var changed = true;
|
||||
var rounds: u32 = 0;
|
||||
while (changed) : (rounds += 1) {
|
||||
if (rounds > 1000) return false;
|
||||
changed = false;
|
||||
for (0..nb) |bb| {
|
||||
const b: u32 = @intCast(bb);
|
||||
for (rpo.items) |b| {
|
||||
if (!exec[b]) continue;
|
||||
for (block_start[b]..block_start[b + 1]) |ii| {
|
||||
const i: u32 = @intCast(ii);
|
||||
|
|
|
|||
|
|
@ -23,9 +23,12 @@ const Edge = @typeInfo(@FieldType(@FieldType(@FieldType(Inst, "op"), "phi"), "in
|
|||
|
||||
const none = std.math.maxInt(u32);
|
||||
|
||||
/// Most iterations unrolled, and most instructions one function may grow
|
||||
/// by.
|
||||
/// Most iterations unrolled, most instructions one loop may grow to
|
||||
/// unrolled (small loops only, as dxc's full unrolling of loops without
|
||||
/// [unroll]: larger ones cost compile time and code size for little), and
|
||||
/// most one function may grow by.
|
||||
const max_trips = 256;
|
||||
const max_loop_growth = 300;
|
||||
const max_growth = 65536;
|
||||
|
||||
/// Unrolls the qualifying loops of `func`, innermost first; true if any.
|
||||
|
|
@ -472,7 +475,7 @@ fn unrollOne(a: Allocator, m: *module.Module, func: u32, budget: *usize, tried:
|
|||
}
|
||||
const trips = states.items.len;
|
||||
const growth = (trips + 1) * size;
|
||||
if (growth > budget.*) continue;
|
||||
if (growth > max_loop_growth or growth > budget.*) continue;
|
||||
budget.* -= growth;
|
||||
try states.append(a, state); // the last pass, up to the exit
|
||||
|
||||
|
|
|
|||
|
|
@ -535,9 +535,6 @@ pub const Module = struct {
|
|||
resource_structs: bool = false,
|
||||
/// -enable-16bit-types: half and 16-bit integers are native types.
|
||||
native_16bit: bool = false,
|
||||
/// Unroll small loops with a constant trip count too (DXIL with the
|
||||
/// optimizer on, as dxc's loop unrolling does).
|
||||
auto_unroll: bool = false,
|
||||
/// -Zpr: matrices that are structured buffer elements themselves
|
||||
/// (not fields, which carry their own orientation) are row_major.
|
||||
/// DXIL only: dxc's SPIR-V keeps those column_major.
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ const Scalar = types.Scalar;
|
|||
|
||||
pub fn run(a: Allocator, m: *ir.Module) Allocator.Error!void {
|
||||
for (m.functions.items) |f| {
|
||||
try unrollLoops(a, m.types, f, m.auto_unroll);
|
||||
try unrollLoops(a, m.types, f);
|
||||
try fold(a, m.types, f);
|
||||
}
|
||||
}
|
||||
|
|
@ -432,9 +432,6 @@ pub fn fold(a: Allocator, t: *TypeTable, f: *ir.Function) Allocator.Error!void {
|
|||
const unroll_limit = 1024;
|
||||
/// Trip limit for loops unrolled only to make texel offsets constant.
|
||||
const auto_unroll_limit = 64;
|
||||
/// Instructions a loop may grow to when unrolled for no other reason
|
||||
/// than its constant trip count (`auto_unroll`).
|
||||
const auto_unroll_budget = 2048;
|
||||
|
||||
/// Evaluates scalar `r` with the induction variable `iv` holding `v`.
|
||||
fn eval(t: *TypeTable, f: *ir.Function, r: Ref, iv: Ref, v: ?Constant, depth: u32) ?Constant {
|
||||
|
|
@ -452,7 +449,7 @@ fn eval(t: *TypeTable, f: *ir.Function, r: Ref, iv: Ref, v: ?Constant, depth: u3
|
|||
};
|
||||
}
|
||||
|
||||
fn unrollLoops(a: Allocator, t: *TypeTable, f: *ir.Function, auto: bool) Allocator.Error!void {
|
||||
fn unrollLoops(a: Allocator, t: *TypeTable, f: *ir.Function) Allocator.Error!void {
|
||||
var kept: std.AutoHashMapUnmanaged(u32, void) = .empty;
|
||||
while (true) {
|
||||
const live = try reachable(a, f);
|
||||
|
|
@ -473,7 +470,7 @@ fn unrollLoops(a: Allocator, t: *TypeTable, f: *ir.Function, auto: bool) Allocat
|
|||
break;
|
||||
}
|
||||
const h = pick orelse return;
|
||||
if (!try unrollOne(a, t, f, h, sets.get(h).?, live, auto)) try kept.put(a, h, {});
|
||||
if (!try unrollOne(a, t, f, h, sets.get(h).?, live)) try kept.put(a, h, {});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -521,7 +518,7 @@ fn hasTexelOffsets(t: *TypeTable, f: *ir.Function, set: []const bool) bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const bool, live: []const bool, auto: bool) Allocator.Error!bool {
|
||||
fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const bool, live: []const bool) Allocator.Error!bool {
|
||||
const hb = f.blocks.items[h];
|
||||
const lp = hb.merge.loop;
|
||||
if (lp.unroll == .dont) return false;
|
||||
|
|
@ -617,7 +614,7 @@ fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const
|
|||
var v = init orelse return false;
|
||||
|
||||
const wanted = lp.unroll == .unroll or hasTexelOffsets(t, f, set);
|
||||
if (!wanted and !auto) return false;
|
||||
if (!wanted) return false;
|
||||
const limit: usize = if (lp.unroll == .unroll) unroll_limit else auto_unroll_limit;
|
||||
const cond = cb.term.cond_br.cond;
|
||||
var values: std.ArrayList(Constant) = .empty;
|
||||
|
|
@ -628,14 +625,6 @@ fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const
|
|||
try values.append(a, v);
|
||||
v = eval(t, f, step, iv, v, 0) orelse return false;
|
||||
}
|
||||
// Otherwise only small loops: their size, unrolled, within a budget.
|
||||
if (!wanted) {
|
||||
var size: usize = 0;
|
||||
for (f.blocks.items, 0..) |blk, bi| if (set[bi]) {
|
||||
size += blk.insts.items.len + 1;
|
||||
};
|
||||
if (values.items.len * size > auto_unroll_budget) return false;
|
||||
}
|
||||
|
||||
// The condition block is dropped: nothing else may use its values.
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue