Shaders: fold always-false and always-true compares to boolean constants #1326
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1326"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-24T17:28:46Z.
Fixes #832.
Summary
v_cmp_{f,tru}_{f32,f16},v_cmpx_{f,tru}_{f32,f16}, and 64-bit integerv_cmp_{f,t}_{u64,i64}/v_cmpx_{f,t}_{u64,i64}directly to constant lane booleans (self.lane(inst, Some(...))) instead of emitting LLVMfcmp false/fcmp true.The GPU device instance has been suspended) on the first dispatch of a pipeline containingfcmp falseorfcmp true, while matching RDNA2 behavior across finite values,±0.0,±inf,qNaN, andsNaN.Verification
valu_tests::float_compares_that_are_always_false_or_truecovering all 8v_cmp_{f,tru}_{f32,f16}andv_cmpx_{f,tru}_{f32,f16}instructions across normal, signed-zero, infinity, quiet-NaN, and signaling-NaN inputs:dxil.dllvalidation: passed0device suspensions)cargo test -p victory-shader,cargo clippy --workspace --all-targets --all-features, andcargo xtask policy: passed.Imported from Marvisak/VIctory. Originally posted by acetonahard-code on 2026-11-24T19:19:17Z.
Tested this on a GPU with SM 6.6 support (RTX 4070 SUPER, driver 621.14, Windows 11, repo toolchain), merged into current
main(ffe310e).cargo test -p victory-shadermainmain+ this PRmain,--all-featuresmain+ this PR,--all-features--nocapture, and no "no SM 6.6 ... device; skipped" lines were printed, so no GPU test skipped for lack of a device.cargo clippy --all-targets -- -D warnings, with and without--all-features: clean.cargo fmt --check: no new diffs from this PR (the ones it reports are already onmain).I didn't test with game data.
Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.
Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T19:42:16Z.
Extra evidence for this one: on
main@ffe310eb, the shader forv_cmp_f_f32,v_cmp_tru_f32(and the f16 pair) removes the WARP device, which is the device CI runs on. Lowered tofcmp false/fcmp true, the shader passesdxil.dllvalidation, and the first dispatch then returns "The GPU device instance has been suspended". D3D12 hands back that same removed device until every reference is released, so the GPU tests that come after it fail as well. Two AMD GPUs run it fine: an RX 9070 and a gfx1036 iGPU.With this PR (
75d919f3) merged ontoffe310eb, all 12 constant compares (f/t/tru, f16/f32/i32/u32 and so on) ran on the RX 9070, the iGPU and WARP, 16 inputs each. No device was lost and the three devices agree.Found with a cross-device VALU fuzzer (each lowered VOP3 opcode as a one-thread shader on every adapter and WARP).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T20:09:47Z.
Review: APPROVED
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests.Folds always-false/always-true float compares (v_cmp_f/v_cmp_tru) directly to boolean constants instead of emitting fcmp false/true — the comment documents that WARP suspends its device on those, so this is a real portability fix. pred::FALSE/TRUE marked allow(dead_code) with the reason.
Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T20:28:13Z.
Reviewed at head
75d919f(worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:1. This is a correctness fix first, an optimization second — both hold.
v_cmp_f_*/v_cmp_tru_*(and the 64-bitf/tcompares) used to lower tofcmp false/fcmp true, which WARP suspends its device on. The PR lowers them directly to a boolean constant per lane viaself.lane(inst, Some(op == "tru" | "t"))in both the f32/f16 and u64/i64 arms (lower.rs:5333,:5365), so no constant-predicate fcmp is emitted at all;pred::FALSE/pred::TRUEbecome#[allow(dead_code)]. The EXEC-guardedv_cmpx_*path goes through the samelanewrite, so the narrowed-EXEC case is handled (the test's cmpx shader restores exec_lo around a guarded write).2. Hardware evidence on this machine. The new
float_compares_that_are_always_false_or_trueruns on the GPU throughrun_cases: f32 and f16f/truover +0/-0, ±1, ±inf, qNaN/sNaN inputs, plus the cmpx EXEC case — 1 passed / 0 failed (2 s, really dispatched). Fullcargo test -p victory-shader --lib121 passed / 0 failed / 1 ignored.3. Checks: clippy
--all-targets --all-features --locked -D warningsclean;cargo build --release -p victorybuilds; rustfmt on the three files shows only main's 3 pre-existinglower.rshunks (shifted +6).4. Relation to #1646 (emit-time constant folding), no conflict: #1646 folds
cmpwhen both operands are constants; this PR removes the always-false/true predicates before any cmp is emitted, whatever the operands. Complementary — after both, acmp FALSE/TRUEreaches the module only from paths neither covers.bitcode.rschanges here are just the two#[allow(dead_code)], so the merge is clean either order.Nothing found that would block this.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:37:29Z.
Automated triage for the maintainers. Closing because this change is already in
main, ormainhas since replaced the code it patches.Always-false/true compares as constants (WARP removes its device on fcmp false/true). The float forms are in main since today (lower.rs:6999, test constant_compares_run); the 64-bit v_cmp_{f,t} forms left over are the 4-line #1685.
If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
Pull request closed