From 635360da208895bb943b2b0637b4e21807d8af90 Mon Sep 17 00:00:00 2001 From: EatCodeSleep2 <52021568+EatCodeSleep2@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:07:35 -0800 Subject: [PATCH] Shaders: fold conditional branches to their fallthrough (cherry picked from commit 1ed26e50d860f328cd42c640bc12486f65603995) --- crates/victory-shader/src/emit/valu_tests.rs | 20 ++++++++++++++++++++ crates/victory-shader/src/ir.rs | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 8dd2f36..a140708 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -299,6 +299,26 @@ fn is_f16_nan(bits: u16) -> bool { (bits & 0x7FFF) > 0x7C00 } +#[test] +fn a_branch_to_fallthrough_keeps_both_values_at_a_join() { + let shader = build_valu_cs(&[ + 0xBF06_0100, // s_cmp_eq_u32 s0, s1 + 0xBF85_0002, // s_cbranch_scc1 -> the s2 = 1 path + s_mov_b32(2, 128), // s2 = 0 + 0xBF82_0002, // s_branch -> the join + s_mov_b32(2, 129), // s2 = 1 + 0xBF85_0000, // s_cbranch_scc1 -> the following instruction + vop1(0x001, 0, sgpr(2)), // join: v0 = s2 + ]); + run_cases( + &shader, + &[[7, 7, 0, 0], [7, 5, 0, 0]], + |backend, _, inputs, actual| { + assert_eq!(actual, u32::from(inputs[0] == inputs[1]), "{backend}"); + }, + ); +} + fn f16_to_f32(bits: u16) -> f32 { let sign = u32::from(bits >> 15) << 31; let exp = (bits >> 10) & 0x1F; diff --git a/crates/victory-shader/src/ir.rs b/crates/victory-shader/src/ir.rs index 577fbc1..518e72e 100644 --- a/crates/victory-shader/src/ir.rs +++ b/crates/victory-shader/src/ir.rs @@ -1,7 +1,8 @@ //! One-to-one SSA form of a native program (decision 0003, step 1). //! //! Every machine instruction becomes one [`Inst`], except scheduling-only -//! ones (`s_nop`, `s_waitcnt*`, `s_inst_prefetch`...), which are dropped. +//! ones (`s_nop`, `s_waitcnt*`, `s_inst_prefetch`...) and redundant conditional +//! branches. //! Each register dword is a variable: a read names the [`Value`] it sees, a //! write defines a new one, and [`Phi`]s merge values where control flow //! joins. Nothing is raised: an instruction keeps its opcode and decoded @@ -187,6 +188,15 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result