diff --git a/src/ir/inline.zig b/src/ir/inline.zig index abb381d..6d69561 100644 --- a/src/ir/inline.zig +++ b/src/ir/inline.zig @@ -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 ..]); f.blocks.items[cont].term = blk.term; f.blocks.items[cont].merge = blk.merge; + f.blocks.items[cont].selection_control = blk.selection_control; blk.insts.shrinkRetainingCapacity(pos); blk.merge = .none; + blk.selection_control = .none; } // 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) }, .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)) }; return result; diff --git a/src/ir/ir.zig b/src/ir/ir.zig index c5a50b5..019fc35 100644 --- a/src/ir/ir.zig +++ b/src/ir/ir.zig @@ -180,8 +180,12 @@ pub const Block = struct { insts: std.ArrayList(Ref) = .empty, term: Terminator = .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 { name: []const u8, ty: TypeId, diff --git a/src/ir/lower.zig b/src/ir/lower.zig index bf02060..3711239 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -565,6 +565,7 @@ const Lowerer = struct { const merge = try l.b.newBlock(); 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).selection_control = selectionControl(st.attrs); l.b.terminate(.{ .cond_br = .{ .cond = cond, .then = then_b, .@"else" = else_b } }); l.b.setBlock(then_b); try l.stmt(x.then); @@ -597,7 +598,7 @@ const Lowerer = struct { l.b.terminate(.{ .br = target.cont }); }, .discard => l.b.terminate(.discard), - .@"switch" => |x| try l.switchStmt(x), + .@"switch" => |x| try l.switchStmt(x, st.attrs), .case, .default => { l.unsupported(st.loc, "case labels outside a switch body", .{}); return error.Unsupported; @@ -607,7 +608,16 @@ const Lowerer = struct { /// `switch`: the header branches to one block per label (labels in a /// 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); const t = l.types(); 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; } 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 } }); 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 }); diff --git a/src/ir/opt.zig b/src/ir/opt.zig index 4d71ad8..def9e96 100644 --- a/src/ir/opt.zig +++ b/src/ir/opt.zig @@ -734,7 +734,7 @@ fn unrollOne(a: Allocator, t: *TypeTable, f: *ir.Function, h: u32, set: []const .selection => |b| .{ .selection = mapBlock(&bmap, b) }, .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).?; conts[iter] = bmap.get(cont).?; diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 8d4ab35..68532eb 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -2199,7 +2199,13 @@ const Emitter = struct { for (blk.insts.items) |r| try e.emitInst(r); switch (blk.merge) { .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 // driver's to honor (LoopControl Unroll / DontUnroll), as // in dxc.