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

Closed
github-bot wants to merge 1 commit from gh-pr/1326/head into gh-pr/1326/base
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 17:28 UTC · original
fuzeofficlal:fix/shader-float-cmp-false-true-832 → main · +79 −2 · 3 files · 1 commits · closed without merging

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-24 17:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1326) `fuzeofficlal:fix/shader-float-cmp-false-true-832` → `main` · +79 −2 · 3 files · 1 commits · closed without merging 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.
Lowering v_cmp_f_{f32,f16} and v_cmp_tru_{f32,f16} (plus v_cmpx_* forms) via LLVM fcmp false/true causes Microsoft D3D12 WARP to suspend and remove its device on the first dispatch. Fold constant float and 64-bit integer compares directly to boolean lane constants before reading operands, matching RDNA2 semantics across all inputs including qNaN and sNaN.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:27:39 +00:00
github-bot closed this pull request 2026-11-26 04:27:39 +00:00
Author
Owner

@⁠acetonahard-code on GitHub · 2026-11-24 19:19 UTC · original

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.

**@⁠acetonahard-code** on GitHub · 2026-11-24 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7141924043) 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>
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 19:42 UTC · original

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).

**@⁠alexdeliu** on GitHub · 2026-11-24 19:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7142471495) 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).
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 20:28 UTC · original

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.

**@⁠adamdotpy** on GitHub · 2026-11-24 20:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7143594107) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:37 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1326#issuecomment-7148706951) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1326
No description provided.