From c4d5b0742c44b18695fa79fcfe5136576d5f0331 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 10:56:48 -0700 Subject: [PATCH] switch statements on both backends Lowering builds one block per case label (consecutive labels fall through), with break leaving the switch and continue reaching the enclosing loop. DXIL gains the LLVM switch instruction. Co-Authored-By: Claude Opus 5.5 --- src/dxil/emit.zig | 8 +++++- src/dxil/module.zig | 13 +++++++++ src/ir/lower.zig | 61 ++++++++++++++++++++++++++++++++++++++++-- test/dxil/switch.hlsl | 18 +++++++++++++ test/spirv/switch.hlsl | 19 +++++++++++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 test/dxil/switch.hlsl create mode 100644 test/spirv/switch.hlsl diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 47584a6..c339db9 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -540,6 +540,7 @@ const Emitter = struct { .ret => |*v| if (v.*) |x| { v.* = R.v(map, x); }, + .@"switch" => |*sw| sw.cond = R.v(map, sw.cond), .br => |*b| if (b.cond) |c| { b.cond = R.v(map, c); }, @@ -671,7 +672,12 @@ const Emitter = struct { _ = try e.add(void_ty, .{ .br = .{ .then = then_b, .@"else" = else_b, .cond = cond } }); }, .none, .@"unreachable" => _ = try e.add(void_ty, .@"unreachable"), - .@"switch" => return e.unsupported("switch", .{}), + .@"switch" => |sw| { + const cond = (try e.scalars(frame, sw.value))[0]; + const cases = try e.arena.alloc(@typeInfo(@FieldType(@FieldType(@FieldType(module.Inst, "op"), "switch"), "cases")).pointer.child, sw.cases.len); + for (sw.cases, cases) |c, *o| o.* = .{ .val = try e.i32Const(@as(i32, @truncate(c.value))), .block = try e.blockFor(frame, c.block.index()) }; + _ = try e.add(void_ty, .{ .@"switch" = .{ .cond = cond, .default = try e.blockFor(frame, sw.default.index()), .cases = cases } }); + }, .discard => { // Like SPIR-V's OpKill: the invocation stops here. const i32t = try e.dm.intType(32); diff --git a/src/dxil/module.zig b/src/dxil/module.zig index ddd0c42..1f7a9d5 100644 --- a/src/dxil/module.zig +++ b/src/dxil/module.zig @@ -83,6 +83,8 @@ pub const Inst = struct { op: union(enum) { ret: ?Value, br: struct { then: u32, @"else": ?u32 = null, cond: ?Value = null }, + /// Case values are constants. + @"switch": struct { cond: Value, default: u32, cases: []const struct { val: Value, block: u32 } }, call: struct { callee: u32, args: []const Value }, binop: struct { op: BinOp, lhs: Value, rhs: Value, flags: u32 = 0 }, cast: struct { op: CastOp, val: Value }, @@ -541,6 +543,17 @@ const Encoder = struct { if (v) |val| try e.pushValueAndType(f, inst_id, val); try e.rec(10); }, + .@"switch" => |sw| { + // SWITCH: [opty, cond, default, (case value id, block)...] + try e.push(e.typeOf(f, sw.cond)); + try e.pushValue(f, inst_id, sw.cond); + try e.push(sw.default); + for (sw.cases) |c| { + try e.push(e.valueId(f, c.val)); + try e.push(c.block); + } + try e.rec(12); + }, .br => |b| { try e.push(b.then); if (b.@"else") |el| { diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 235ecbe..657179b 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -244,13 +244,70 @@ const Lowerer = struct { l.b.terminate(.{ .br = target.cont }); }, .discard => l.b.terminate(.discard), - .@"switch", .case, .default => { - l.unsupported(st.loc, "switch statements are not supported yet", .{}); + .@"switch" => |x| try l.switchStmt(x), + .case, .default => { + l.unsupported(st.loc, "case labels outside a switch body", .{}); return error.Unsupported; }, } } + /// `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 { + if (x.cond_decl) |d| try l.localDecl(d); + const t = l.types(); + var cond_ty = x.cond.ty; + if (t.component(cond_ty)) |c| cond_ty = try t.scalar(if (c.isUnsigned()) .uint else .int); + const cond = try l.converted(x.cond, cond_ty); + const header = l.b.cur; + const merge = try l.b.newBlock(); + // Segments: a label and the statements up to the next one. + const Seg = struct { block: BlockId, value: ?i64, stmts: std.ArrayList(*const ast.Stmt) = .empty }; + var segs: std.ArrayList(Seg) = .empty; + const body: []const *ast.Stmt = switch (x.body.kind) { + .compound => |c| c, + else => &.{x.body}, + }; + for (body) |st| { + var cur: *const ast.Stmt = st; + while (true) switch (cur.kind) { + .case => |c| { + const v = l.s.evalInt(c.value) orelse { + l.unsupported(cur.loc, "case values that are not constants", .{}); + return error.Unsupported; + }; + try segs.append(l.arena, .{ .block = try l.b.newBlock(), .value = v }); + cur = c.stmt; + }, + .default => |d| { + try segs.append(l.arena, .{ .block = try l.b.newBlock(), .value = null }); + cur = d; + }, + else => break, + }; + // Statements before the first label never run. + if (segs.items.len > 0) try segs.items[segs.items.len - 1].stmts.append(l.arena, cur); + } + var default: BlockId = merge; + var cases: std.ArrayList(@typeInfo(@FieldType(@FieldType(ir.Terminator, "switch"), "cases")).pointer.child) = .empty; + for (segs.items) |sg| { + 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.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 }); + for (segs.items, 0..) |sg, k| { + l.b.setBlock(sg.block); + for (sg.stmts.items) |st| try l.stmt(st); + // Fall through to the next label, or leave the switch. + l.b.terminate(.{ .br = if (k + 1 < segs.items.len) segs.items[k + 1].block else merge }); + } + _ = l.loops.pop(); + l.b.setBlock(merge); + } + /// header (loop merge) -> cond -> body -> continue (inc) -> header. /// `do` loops test at the end of the continue block. fn loop(l: *Lowerer, cond: ?*ast.Expr, inc: ?*ast.Expr, body: *const ast.Stmt, do_while: bool, attrs: []const ast.Attribute) Error!void { diff --git a/test/dxil/switch.hlsl b/test/dxil/switch.hlsl new file mode 100644 index 0000000..22f1dce --- /dev/null +++ b/test/dxil/switch.hlsl @@ -0,0 +1,18 @@ +// PROFILE: cs_6_0 +// switch: fallthrough, default, break, continue to the enclosing loop. +// ZCHECK: switch i32 {{.*}}, label +RWStructuredBuffer o : register(u0); +[numthreads(4,1,1)] void main(uint i : SV_GroupIndex) { + int r = 0; + for (int k = 0; k < 3; k++) { + switch (i + k) { + case 0: r += 1; break; + case 1: + case 2: r += 2; + case 3: r += 3; break; + default: if (r > 4) continue; r -= 1; + } + } + switch (r) { case 7: o[1] = 7; } + o[i] = r; +} diff --git a/test/spirv/switch.hlsl b/test/spirv/switch.hlsl new file mode 100644 index 0000000..f3f09c8 --- /dev/null +++ b/test/spirv/switch.hlsl @@ -0,0 +1,19 @@ +// PROFILE: cs_6_0 +// switch: fallthrough, default, break, continue to the enclosing loop. +// ZCHECK: OpSelectionMerge +// ZCHECK: OpSwitch +RWStructuredBuffer o : register(u0); +[numthreads(4,1,1)] void main(uint i : SV_GroupIndex) { + int r = 0; + for (int k = 0; k < 3; k++) { + switch (i + k) { + case 0: r += 1; break; + case 1: + case 2: r += 2; + case 3: r += 3; break; + default: if (r > 4) continue; r -= 1; + } + } + switch (r) { case 7: o[1] = 7; } + o[i] = r; +}