Shaders: programs without FMad disable math refactoring (port of upstream VIctory #1856) #961

Open
github-bot wants to merge 2 commits from github-bot/gh-961 into main AGit
Owner

@⁠Moon0755 on GitHub · 2026-11-25 13:51 UTC · original
Moon0755:fix/disable-math-refactoring-without-fmad → main · +90 −5 · 3 files · 2 commits · open

Ports upstream Marvisak/VIctory#1856 (rminzao).

Problem

On some PC GPUs/drivers (such as Intel UHD Graphics and NVIDIA Ada / RTX 4050), the host driver contracts separate fmul + fadd operations (including v_mad_f32, v_mac_f32, and BVH ray-triangle intersection arithmetic) into a fused FMA when math refactoring is allowed in DXIL entry properties. Because Xbox Series X (RDNA2) rounds unfused operations separately, contracting them produces 1 ULP / -0 mismatches against console semantics.

What changed

  • crates/victory-shader/src/emit/shader.rs:
    • Add flags::NO_REFACTORING (1 << 1, DXIL DisableMathRefactoring).
    • Track Shader::fused: bool (initialized false), and add Shader::settle_flags(&mut self) which sets flags::NO_REFACTORING when !self.fused, called in Shader::finish before writing DXIL entry properties.
  • crates/victory-shader/src/emit/lower.rs:
    • Set self.s.fused = true in Lower::fmad, so only shaders that emit DXIL FMad leave math refactoring enabled for the host driver.
    • Expose Lowered::refactoring under #[cfg(test)] and add only_programs_with_fmad_let_the_host_refactor, verifying v_fma_f32 keeps refactoring allowed while v_mul_f32 + v_add_f32 sets NO_REFACTORING.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45627210689
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::lower::tests::only_programs_with_fmad_let_the_host_refactor ... ok, and the full DXIL-validated + WARP-executed shader suite passed with NO_REFACTORING set on non-FMad programs); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 13:51 UTC · [original](https://github.com/fxceless/victory-fork/pull/961) `Moon0755:fix/disable-math-refactoring-without-fmad` → `main` · +90 −5 · 3 files · 2 commits · open Ports upstream [Marvisak/VIctory#1856](https://github.com/Marvisak/VIctory/pull/1856) (`rminzao`). ## Problem On some PC GPUs/drivers (such as Intel UHD Graphics and NVIDIA Ada / RTX 4050), the host driver contracts separate `fmul` + `fadd` operations (including `v_mad_f32`, `v_mac_f32`, and BVH ray-triangle intersection arithmetic) into a fused FMA when math refactoring is allowed in DXIL entry properties. Because Xbox Series X (RDNA2) rounds unfused operations separately, contracting them produces 1 ULP / `-0` mismatches against console semantics. ## What changed - **`crates/victory-shader/src/emit/shader.rs`**: - Add `flags::NO_REFACTORING` (`1 << 1`, DXIL `DisableMathRefactoring`). - Track `Shader::fused: bool` (initialized `false`), and add `Shader::settle_flags(&mut self)` which sets `flags::NO_REFACTORING` when `!self.fused`, called in `Shader::finish` before writing DXIL entry properties. - **`crates/victory-shader/src/emit/lower.rs`**: - Set `self.s.fused = true` in `Lower::fmad`, so only shaders that emit DXIL `FMad` leave math refactoring enabled for the host driver. - Expose `Lowered::refactoring` under `#[cfg(test)]` and add `only_programs_with_fmad_let_the_host_refactor`, verifying `v_fma_f32` keeps refactoring allowed while `v_mul_f32` + `v_add_f32` sets `NO_REFACTORING`. ## Verification Verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP) via a temporary scoped CI branch on the fork: - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45627210689 - `cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings`: passed - `cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p victory-shader -p xtask --locked -- --nocapture`: `victory-shader` `216 passed; 0 failed; 3 ignored` (`test emit::lower::tests::only_programs_with_fmad_let_the_host_refactor ... ok`, and the full DXIL-validated + WARP-executed shader suite passed with `NO_REFACTORING` set on non-FMad programs); `xtask` `4 passed; 0 failed` (including `repository_passes_policy`). Note: CI checks were scoped to `-p victory-shader -p xtask` because `upstream/main` (`fxceless/victory-fork`) currently has unrelated pre-existing Clippy errors in `crates/victory-gpu`. — Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:54:42 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-25 16:01 UTC · original

I compared six original synthetic compute shaders at base 0b0074e and head 76f01ed. The emitted DXIL sets DisableMathRefactoring for separate fmul/fadd, unfused v_mad_f32, and an integer-only control. In those three cases, the flag was the only change in the disassembled IR and the other container chunks matched. The live v_fma_f32 and mixed fused/unfused controls were byte-identical across revisions.

There is one gap when lowering emits an FMad and dead-code elimination removes it. I put an unused v_fma_f32 v9, v0, v0, v0 before v_mul_f32 v12, v0, v0 and v_add_f32 v12, v12, v0, then stored v12 to a UAV. The ALU words are D54B0009 04020100 10180100 0618010C. On the PR head, the final DXIL has separate fmul and fadd and no FMad, but ShaderFlags remains 0x40000000. The same multiply/add with the unused guest instruction omitted gets 0x40000002. With VICTORY_SHADER_OPT=0 the FMad is in the output, and with VICTORY_SHADER_OPT=1 it disappears while the flag stays clear. The flag currently reflects lowering before dead-code elimination. The behavior to cover is the flag on the final DXIL when its last FMad has been removed.

The exact Linux shader library suites passed 171 tests at base and 172 at head, with two ignored in each. Windows MSVC cross-target cargo check --locked -p victory-shader --all-targets --target x86_64-pc-windows-msvc passed at head. This check establishes the emitted metadata and instructions. Native Windows execution, Microsoft DXIL validation, and driver contraction for the dead-FMA case remain unverified.

**@⁠avt0m4t48** on GitHub · 2026-11-25 16:01 UTC · [original](https://github.com/fxceless/victory-fork/pull/961#issuecomment-7167522197) I compared six original synthetic compute shaders at base `0b0074e` and head `76f01ed`. The emitted DXIL sets `DisableMathRefactoring` for separate `fmul`/`fadd`, unfused `v_mad_f32`, and an integer-only control. In those three cases, the flag was the only change in the disassembled IR and the other container chunks matched. The live `v_fma_f32` and mixed fused/unfused controls were byte-identical across revisions. There is one gap when lowering emits an FMad and dead-code elimination removes it. I put an unused `v_fma_f32 v9, v0, v0, v0` before `v_mul_f32 v12, v0, v0` and `v_add_f32 v12, v12, v0`, then stored `v12` to a UAV. The ALU words are `D54B0009 04020100 10180100 0618010C`. On the PR head, the final DXIL has separate `fmul` and `fadd` and no FMad, but `ShaderFlags` remains `0x40000000`. The same multiply/add with the unused guest instruction omitted gets `0x40000002`. With `VICTORY_SHADER_OPT=0` the FMad is in the output, and with `VICTORY_SHADER_OPT=1` it disappears while the flag stays clear. The flag currently reflects lowering before dead-code elimination. The behavior to cover is the flag on the final DXIL when its last FMad has been removed. The exact Linux shader library suites passed 171 tests at base and 172 at head, with two ignored in each. Windows MSVC cross-target `cargo check --locked -p victory-shader --all-targets --target x86_64-pc-windows-msvc` passed at head. This check establishes the emitted metadata and instructions. Native Windows execution, Microsoft DXIL validation, and driver contraction for the dead-FMA case remain unverified.
Author
Owner

@⁠Moon0755 on GitHub · 2026-11-25 16:15 UTC · original

Thank you @⁠avt0m4t48 for catching that post-DCE gap and providing the exact dead-v_fma_f32 reproducer!

Fixed in 0a67218:

  • crates/victory-shader/src/emit/bitcode/opt.rs (Usage::fmad): Scan the post-optimization module in Module::usage() for live dx.op.tertiary.* calls with opcode 46 (FMad).
  • crates/victory-shader/src/emit/shader.rs (Shader::finish_with_flags): Update self.fused = used.fmad right after self.m.optimise(passes) (when VICTORY_SHADER_OPT != 0), before self.settle_flags() writes ShaderFlags — so a dead FMad removed by DCE now sets DisableMathRefactoring, while VICTORY_SHADER_OPT=0 still keeps self.fused from lowering.
  • crates/victory-shader/src/emit/lower.rs: Compute Lowered::refactoring from the settled post-DCE flags and add your exact dead-v_fma_f32 (D54B0009 04020100) + live v_mul_f32/v_add_f32 case to only_programs_with_fmad_let_the_host_refactor.

Re-verified on Windows (windows-latest + dxil.dll + D3D12 WARP):

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45651700909
  • Both Clippy passes (-p victory-shader -p xtask, default and --all-features, -D warnings) and cargo test -p victory-shader -p xtask --locked -- --nocapture passed (including only_programs_with_fmad_let_the_host_refactor and repository_passes_policy).

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 16:15 UTC · [original](https://github.com/fxceless/victory-fork/pull/961#issuecomment-7167724782) Thank you @⁠avt0m4t48 for catching that post-DCE gap and providing the exact dead-`v_fma_f32` reproducer! Fixed in `0a67218`: - **`crates/victory-shader/src/emit/bitcode/opt.rs` (`Usage::fmad`)**: Scan the post-optimization module in `Module::usage()` for live `dx.op.tertiary.*` calls with opcode `46` (`FMad`). - **`crates/victory-shader/src/emit/shader.rs` (`Shader::finish_with_flags`)**: Update `self.fused = used.fmad` right after `self.m.optimise(passes)` (when `VICTORY_SHADER_OPT != 0`), before `self.settle_flags()` writes `ShaderFlags` — so a dead `FMad` removed by DCE now sets `DisableMathRefactoring`, while `VICTORY_SHADER_OPT=0` still keeps `self.fused` from lowering. - **`crates/victory-shader/src/emit/lower.rs`**: Compute `Lowered::refactoring` from the settled post-DCE flags and add your exact dead-`v_fma_f32` (`D54B0009 04020100`) + live `v_mul_f32`/`v_add_f32` case to `only_programs_with_fmad_let_the_host_refactor`. Re-verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP): - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45651700909 - Both Clippy passes (`-p victory-shader -p xtask`, default and `--all-features`, `-D warnings`) and `cargo test -p victory-shader -p xtask --locked -- --nocapture` passed (including `only_programs_with_fmad_let_the_host_refactor` and `repository_passes_policy`). — Neon
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/961/head:github-bot/gh-961'
git switch 'github-bot/gh-961'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-961'
git switch 'github-bot/gh-961'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-961'
git switch 'github-bot/gh-961'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-961'
git switch 'main'
git merge --squash 'github-bot/gh-961'
git switch 'main'
git merge --ff-only 'github-bot/gh-961'
git switch 'main'
git merge 'github-bot/gh-961'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.