Shaders: fold always-false and always-true compares to boolean constants #1326

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/1326 into main
fuzeofficlal commented 2026-11-24 17:28:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-24T17:28:46Z.


Fixes #832.

Summary

  • Lower v_cmp_{f,tru}_{f32,f16}, v_cmpx_{f,tru}_{f32,f16}, and 64-bit integer v_cmp_{f,t}_{u64,i64} / v_cmpx_{f,t}_{u64,i64} directly to constant lane booleans (self.lane(inst, Some(...))) instead of emitting LLVM fcmp false / fcmp true.
  • Prevents Microsoft D3D12 WARP from suspending and removing its device (The GPU device instance has been suspended) on the first dispatch of a pipeline containing fcmp false or fcmp true, while matching RDNA2 behavior across finite values, ±0.0, ±inf, qNaN, and sNaN.

Verification

  • Added valu_tests::float_compares_that_are_always_false_or_true covering all 8 v_cmp_{f,tru}_{f32,f16} and v_cmpx_{f,tru}_{f32,f16} instructions across normal, signed-zero, infinity, quiet-NaN, and signaling-NaN inputs:
    • dxil.dll validation: passed
    • NVIDIA GeForce RTX 5090: passed
    • Microsoft D3D12 WARP: passed (0 device suspensions)
  • cargo test -p victory-shader, cargo clippy --workspace --all-targets --all-features, and cargo xtask policy: passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6587620365 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-24T17:28:46Z. --- Fixes #832. ### Summary - Lower `v_cmp_{f,tru}_{f32,f16}`, `v_cmpx_{f,tru}_{f32,f16}`, and 64-bit integer `v_cmp_{f,t}_{u64,i64}` / `v_cmpx_{f,t}_{u64,i64}` directly to constant lane booleans (`self.lane(inst, Some(...))`) instead of emitting LLVM `fcmp false` / `fcmp true`. - Prevents Microsoft D3D12 WARP from suspending and removing its device (`The GPU device instance has been suspended`) on the first dispatch of a pipeline containing `fcmp false` or `fcmp true`, while matching RDNA2 behavior across finite values, `±0.0`, `±inf`, `qNaN`, and `sNaN`. ### Verification - Added `valu_tests::float_compares_that_are_always_false_or_true` covering all 8 `v_cmp_{f,tru}_{f32,f16}` and `v_cmpx_{f,tru}_{f32,f16}` instructions across normal, signed-zero, infinity, quiet-NaN, and signaling-NaN inputs: - `dxil.dll` validation: passed - **NVIDIA GeForce RTX 5090**: passed - **Microsoft D3D12 WARP**: passed (`0` device suspensions) - `cargo test -p victory-shader`, `cargo clippy --workspace --all-targets --all-features`, and `cargo xtask policy`: passed.
acetonahard-code commented 2026-11-24 20:03:07 +00:00 (Migrated from github.com)

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-shader passed failed ignored
main 120 0 1
main + this PR 121 0 1
main, --all-features 120 0 1
main + this PR, --all-features 121 0 1
  • The new test passes.
  • Run with --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 on main).

I didn't test with game data.


Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141924043 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7141924043). Originally posted by [acetonahard-code](https://github.com/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-shader` | passed | failed | ignored | |---|---|---|---| | `main` | 120 | 0 | 1 | | `main` + this PR | 121 | 0 | 1 | | `main`, `--all-features` | 120 | 0 | 1 | | `main` + this PR, `--all-features` | 121 | 0 | 1 | - The new test passes. - Run with `--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 on `main`). I didn't test with game data. --- <sub>Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.</sub>
alexdeliu commented 2026-11-24 20:03:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T19:42:16Z.


Extra evidence for this one: on main @ ffe310eb, the shader for v_cmp_f_f32, v_cmp_tru_f32 (and the f16 pair) removes the WARP device, which is the device CI runs on. Lowered to fcmp false / fcmp true, the shader passes dxil.dll validation, 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 onto ffe310eb, 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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142471495 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7142471495). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T19:42:16Z. --- Extra evidence for this one: on `main` @ `ffe310eb`, the shader for `v_cmp_f_f32`, `v_cmp_tru_f32` (and the f16 pair) **removes the WARP device**, which is the device CI runs on. Lowered to `fcmp false` / `fcmp true`, the shader passes `dxil.dll` validation, 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 onto `ffe310eb`, 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).
catminou4 commented 2026-11-24 21:02:05 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6046556975 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326#pullrequestreview-6046556975). Originally posted by [catminou4](https://github.com/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.
adamdotpy commented 2026-11-24 21:03:31 +00:00 (Migrated from github.com)

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-bit f/t compares) used to lower to fcmp false/fcmp true, which WARP suspends its device on. The PR lowers them directly to a boolean constant per lane via self.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::TRUE become #[allow(dead_code)]. The EXEC-guarded v_cmpx_* path goes through the same lane write, 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_true runs on the GPU through run_cases: f32 and f16 f/tru over +0/-0, ±1, ±inf, qNaN/sNaN inputs, plus the cmpx EXEC case — 1 passed / 0 failed (2 s, really dispatched). Full cargo test -p victory-shader --lib 121 passed / 0 failed / 1 ignored.

3. Checks: clippy --all-targets --all-features --locked -D warnings clean; cargo build --release -p victory builds; rustfmt on the three files shows only main's 3 pre-existing lower.rs hunks (shifted +6).

4. Relation to #1646 (emit-time constant folding), no conflict: #1646 folds cmp when both operands are constants; this PR removes the always-false/true predicates before any cmp is emitted, whatever the operands. Complementary — after both, a cmp FALSE/TRUE reaches the module only from paths neither covers. bitcode.rs changes here are just the two #[allow(dead_code)], so the merge is clean either order.

Nothing found that would block this.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143594107 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7143594107). Originally posted by [adamdotpy](https://github.com/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-bit `f`/`t` compares) used to lower to `fcmp false`/`fcmp true`, which WARP suspends its device on. The PR lowers them directly to a boolean constant per lane via `self.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::TRUE` become `#[allow(dead_code)]`. The EXEC-guarded `v_cmpx_*` path goes through the same `lane` write, 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_true` runs on the GPU through `run_cases`: f32 and f16 `f`/`tru` over +0/-0, ±1, ±inf, qNaN/sNaN inputs, plus the cmpx EXEC case — 1 passed / 0 failed (2 s, really dispatched). Full `cargo test -p victory-shader --lib` 121 passed / 0 failed / 1 ignored. **3. Checks:** clippy `--all-targets --all-features --locked -D warnings` clean; `cargo build --release -p victory` builds; rustfmt on the three files shows only main's 3 pre-existing `lower.rs` hunks (shifted +6). **4. Relation to #1646 (emit-time constant folding), no conflict:** #1646 folds `cmp` when *both operands* are constants; this PR removes the always-false/true *predicates* before any cmp is emitted, whatever the operands. Complementary — after both, a `cmp FALSE/TRUE` reaches the module only from paths neither covers. `bitcode.rs` changes here are just the two `#[allow(dead_code)]`, so the merge is clean either order. Nothing found that would block this.
upstream-sync closed this pull request 2026-11-25 01:06:14 +00:00
Marvisak commented 2026-11-25 01:08:09 +00:00 (Migrated from github.com)

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, or main has 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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148706951 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7148706951). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:37:29Z. --- Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!1326
No description provided.