SPIR-V: selection controls from [branch] and [flatten]

[branch] and [flatten] on if and switch were dropped: every
OpSelectionMerge had selection control None, leaving the driver to
flatten or keep the branch by its own heuristics. They now become
DontFlatten and Flatten, as in dxc (the Hyperion engine's shaders use
them 21 times). The IR keeps them on the header block; unrolling and
inlining carry them over.
This commit is contained in:
Peterino2 2026-11-02 08:00:54 +00:00
parent d39ee30a4c
commit 97517cc5b1
5 changed files with 28 additions and 5 deletions

View File

@ -283,8 +283,10 @@ fn inlineOne(a: Allocator, f: *ir.Function, bi: u32, pos: usize, args: []const R
try f.blocks.items[cont].insts.appendSlice(a, blk.insts.items[pos + 1 ..]); try f.blocks.items[cont].insts.appendSlice(a, blk.insts.items[pos + 1 ..]);
f.blocks.items[cont].term = blk.term; f.blocks.items[cont].term = blk.term;
f.blocks.items[cont].merge = blk.merge; f.blocks.items[cont].merge = blk.merge;
f.blocks.items[cont].selection_control = blk.selection_control;
blk.insts.shrinkRetainingCapacity(pos); blk.insts.shrinkRetainingCapacity(pos);
blk.merge = .none; blk.merge = .none;
blk.selection_control = .none;
} }
// Copies of the callee's blocks and instructions (parameters become // Copies of the callee's blocks and instructions (parameters become
@ -387,7 +389,7 @@ fn inlineOne(a: Allocator, f: *ir.Function, bi: u32, pos: usize, args: []const R
.selection => |mb| .{ .selection = B.map(bmap, mb) }, .selection => |mb| .{ .selection = B.map(bmap, mb) },
.loop => |l| .{ .loop = .{ .merge = B.map(bmap, l.merge), .@"continue" = B.map(bmap, l.@"continue"), .unroll = l.unroll } }, .loop => |l| .{ .loop = .{ .merge = B.map(bmap, l.merge), .@"continue" = B.map(bmap, l.@"continue"), .unroll = l.unroll } },
}; };
f.blocks.items[bmap[k]] = .{ .insts = insts, .term = term, .merge = merge }; f.blocks.items[bmap[k]] = .{ .insts = insts, .term = term, .merge = merge, .selection_control = src.selection_control };
} }
f.blocks.items[bi].term = .{ .br = @fromBackingInt(@intCast(entry_target)) }; f.blocks.items[bi].term = .{ .br = @fromBackingInt(@intCast(entry_target)) };
return result; return result;

View File

@ -180,8 +180,12 @@ pub const Block = struct {
insts: std.ArrayList(Ref) = .empty, insts: std.ArrayList(Ref) = .empty,
term: Terminator = .none, term: Terminator = .none,
merge: Merge = .none, merge: Merge = .none,
/// `[flatten]` / `[branch]` on the selection this block heads.
selection_control: SelectionControl = .none,
}; };
pub const SelectionControl = enum { none, flatten, dont_flatten };
pub const Param = struct { pub const Param = struct {
name: []const u8, name: []const u8,
ty: TypeId, ty: TypeId,

View File

@ -565,6 +565,7 @@ const Lowerer = struct {
const merge = try l.b.newBlock(); const merge = try l.b.newBlock();
const else_b = if (x.@"else" != null) try l.b.newBlock() else merge; const else_b = if (x.@"else" != null) try l.b.newBlock() else merge;
l.b.f.block(l.b.cur).merge = .{ .selection = merge }; l.b.f.block(l.b.cur).merge = .{ .selection = merge };
l.b.f.block(l.b.cur).selection_control = selectionControl(st.attrs);
l.b.terminate(.{ .cond_br = .{ .cond = cond, .then = then_b, .@"else" = else_b } }); l.b.terminate(.{ .cond_br = .{ .cond = cond, .then = then_b, .@"else" = else_b } });
l.b.setBlock(then_b); l.b.setBlock(then_b);
try l.stmt(x.then); try l.stmt(x.then);
@ -597,7 +598,7 @@ const Lowerer = struct {
l.b.terminate(.{ .br = target.cont }); l.b.terminate(.{ .br = target.cont });
}, },
.discard => l.b.terminate(.discard), .discard => l.b.terminate(.discard),
.@"switch" => |x| try l.switchStmt(x), .@"switch" => |x| try l.switchStmt(x, st.attrs),
.case, .default => { .case, .default => {
l.unsupported(st.loc, "case labels outside a switch body", .{}); l.unsupported(st.loc, "case labels outside a switch body", .{});
return error.Unsupported; return error.Unsupported;
@ -607,7 +608,16 @@ const Lowerer = struct {
/// `switch`: the header branches to one block per label (labels in a /// `switch`: the header branches to one block per label (labels in a
/// row get their own, falling through); `break` goes to the merge. /// row get their own, falling through); `break` goes to the merge.
fn switchStmt(l: *Lowerer, x: @FieldType(ast.Stmt.Kind, "switch")) Error!void { /// `[flatten]` / `[branch]` on an `if` or `switch`.
fn selectionControl(attrs: []const ast.Attribute) ir.SelectionControl {
for (attrs) |a| {
if (std.ascii.eqlIgnoreCase(a.name, "flatten")) return .flatten;
if (std.ascii.eqlIgnoreCase(a.name, "branch")) return .dont_flatten;
}
return .none;
}
fn switchStmt(l: *Lowerer, x: @FieldType(ast.Stmt.Kind, "switch"), attrs: []const ast.Attribute) Error!void {
if (x.cond_decl) |d| try l.localDecl(d); if (x.cond_decl) |d| try l.localDecl(d);
const t = l.types(); const t = l.types();
var cond_ty = x.cond.ty; var cond_ty = x.cond.ty;
@ -648,6 +658,7 @@ const Lowerer = struct {
if (sg.value) |v| try cases.append(l.arena, .{ .value = v, .block = sg.block }) else default = sg.block; if (sg.value) |v| try cases.append(l.arena, .{ .value = v, .block = sg.block }) else default = sg.block;
} }
l.b.f.block(header).merge = .{ .selection = merge }; l.b.f.block(header).merge = .{ .selection = merge };
l.b.f.block(header).selection_control = selectionControl(attrs);
l.b.terminate(.{ .@"switch" = .{ .value = cond, .default = default, .cases = cases.items } }); l.b.terminate(.{ .@"switch" = .{ .value = cond, .default = default, .cases = cases.items } });
const cont: BlockId = if (l.loops.items.len > 0) l.loops.getLast().cont else .none; const cont: BlockId = if (l.loops.items.len > 0) l.loops.getLast().cont else .none;
try l.loops.append(l.arena, .{ .brk = merge, .cont = cont }); try l.loops.append(l.arena, .{ .brk = merge, .cont = cont });

View File

@ -734,7 +734,7 @@ fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const
.selection => |b| .{ .selection = mapBlock(&bmap, b) }, .selection => |b| .{ .selection = mapBlock(&bmap, b) },
.loop => |l| .{ .loop = .{ .merge = mapBlock(&bmap, l.merge), .@"continue" = mapBlock(&bmap, l.@"continue"), .unroll = l.unroll } }, .loop => |l| .{ .loop = .{ .merge = mapBlock(&bmap, l.merge), .@"continue" = mapBlock(&bmap, l.@"continue"), .unroll = l.unroll } },
}; };
f.blocks.items[nb] = .{ .insts = insts, .term = term, .merge = mg }; f.blocks.items[nb] = .{ .insts = insts, .term = term, .merge = mg, .selection_control = src.selection_control };
} }
entries[iter] = bmap.get(body).?; entries[iter] = bmap.get(body).?;
conts[iter] = bmap.get(cont).?; conts[iter] = bmap.get(cont).?;

View File

@ -2199,7 +2199,13 @@ const Emitter = struct {
for (blk.insts.items) |r| try e.emitInst(r); for (blk.insts.items) |r| try e.emitInst(r);
switch (blk.merge) { switch (blk.merge) {
.none => {}, .none => {},
.selection => |mb| try e.b.emit(.functions, .selection_merge, &.{ labels[mb.index()], 0 }), // [flatten] / [branch]: SelectionControl Flatten /
// DontFlatten, as in dxc.
.selection => |mb| try e.b.emit(.functions, .selection_merge, &.{ labels[mb.index()], switch (blk.selection_control) {
.none => 0,
.flatten => 1,
.dont_flatten => 2,
} }),
// [unroll] / [loop] the IR could not act on are the // [unroll] / [loop] the IR could not act on are the
// driver's to honor (LoopControl Unroll / DontUnroll), as // driver's to honor (LoopControl Unroll / DontUnroll), as
// in dxc. // in dxc.