Skip to content

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

Closed
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-float-cmp-false-true-832
Closed

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-float-cmp-false-true-832

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.
@acetonahard-code

Copy link
Copy Markdown

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.

@alexdeliu

Copy link
Copy Markdown
Contributor

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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

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.

@Marvisak

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shaders: v_cmp_f/v_cmp_tru on floats emit fcmp false/true, which removes the WARP device (fix on a fork)

6 participants