Shaders: programs without FMad disable math refactoring #1856
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1856
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1856"
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?
@rminzao on GitHub · 2026-11-24 23:30 UTC · original
rminzao:shaders-unfused-mul-add→main· +57 −0 · 2 files · 3 commits · openClaimed in #1633 (claim).
Problem
On the Intel UHD (the test host's default adapter on this laptop; same failures as #1829), the host fuses a multiply and an add the console rounds apart. The lowering emits plain
fmul+faddforv_mac_f32,v_mad_f32and the BVH intersection arithmetic, and the shader flags leave math refactoring allowed. The driver then contracts them into an FMA. Onmainat70564ae, on an Intel UHD Graphics (driver 31.0.101.4146), four tests fail:emit::bvh_tests::intersections_on_the_gpu_equal_the_reference,..._with_64_bit_pointers_...and..._on_nodes_in_title_memory_...: the triangle'stis 1 ULP off the reference, withdetand the barycentrics equal.emit::semantics_tests::arithmetic_matches_the_hardware:v_mac_f32of (2^-126, -2^-126, +0) gives -0 instead of +0, on 102 inputs. A fused tiny product flushes with its sign; an unfused one doesn't.Fix
The shader flags get
DisableMathRefactoring(bit 1) when the program emits noFMad. DXIL has no fused f32 op, sov_fma_f32/v_fmac_f32/v_fma_mix*(and the f16 shortcuts) rely on the host fusingFMad. Turning refactoring off everywhere stops that: with the flag on every program,Host::fuses_fmadreportsfalseon NVIDIA and the FMA probes then mismatch like WARP. Programs that useFMadtherefore keep today's behaviour, and every other program now rounds its multiplies and adds apart, as the console does.Programs with both fused and unfused multiply-adds are still left to the host (the existing
multiply-adds are fused only where the host fusesdefect).Tested
Intel UHD Graphics (driver 31.0.101.4146) and RTX 4050 Laptop (driver 595.97), Windows 11. The first version of this description credited the RTX 4050 with the Intel results; see the correction comment.
victory-shader200 → 205 passed). The remaining failures (victory-shader: 5,victory-gpu: 3) fail the same way onmain. They're the 1,000,000 view-heap cap, castable formats, wave-32 permute pipelines, 64-bit image atomics and the FSPX dot test, each with its own open PRs.main. With the PR, nothing changes (207 passed, the same 3 failures asmain).hosts_that_do_not_fuse_are_named: both adapters still fuseFMad(true).cargo clippy -p victory-shader --all-targets -- -D warnings: clean.rustfmt --check: no new diffs.Not run on AMD.
fuses_fmadnotes that the RX 6700 XT fusesFMadtoo, so the same split should apply there, but I couldn't check it. Not measured either: the cost of the lost fusion in programs withoutFMad.AI-assisted (Claude Code).
🤖 Generated with Claude Code
@welisonzika14-jpg on GitHub · 2026-11-24 23:38 UTC · original
Blackwell data point. RTX 5070 (driver 621.24, Windows 11, Rust 1.98.1 MSVC),
70564ae, heade3a1704,cargo test -p victory-gpu -p victory-shader --lib, no game files.mainthe four tests from the description pass here: the threebvh_tests::intersections_on_the_gpu_equal_the_reference*tests andsemantics_tests::arithmetic_matches_the_hardware. So this card does not show the 1 ULP / -0 problem that the RTX 4050 (Ada) shows. I cannot say why; it may be the GPU generation or the driver.hosts_that_do_not_fuse_are_namedpasses, and the failures are exactly the five thatmainalready has here (2 invictory-gpu, 3 invictory-shader, see #1744). Nothing new.So no regression on Blackwell, but this card cannot confirm the fix either, because it does not hit the bug on
main. I only ran the unit tests, not the title.Written with AI help (Claude/Kiro). The numbers are from runs on my machine.
@dpalacios69 on GitHub · 2026-11-24 23:39 UTC · original
Checked
e3a1704on top ofmain70564aeon Linux:cargo test -p victory-shadergives 118 passed, 0 failed, 1 ignored; clippy reports only an existing unused field (rovs,lower.rs:61). The diff matches the title (fusedis set only inLower::fmad,NO_REFACTORINGis raised otherwise) but adds no test: a synthetic one asserting the1 << 1bit with and without an FMad would pin it. Not tested: validation withdxil.dllof the new flag, and any GPU run. AI-assisted analysis.@upipibi on GitHub · 2026-11-24 23:43 UTC · original
Ampere counterpart to the RTX 4050 run: RTX 3070 8 GB, driver 616.56, plus WARP, Windows 11, debug,
e3a1704(onmain70564ae).cargo test -p victory-shader --locked: 206 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA failures onmain, so there are no new ones.AI-assisted (Claude Code). Synthetic tests only.
@rminzao on GitHub · 2026-11-24 23:47 UTC · original
@dpalacios69 thanks. Added
only_programs_with_fmad_let_the_host_refactorin 23e3b5a: it lowers av_fma_f32program and av_mul_f32+v_add_f32one, and asserts the first keeps refactoring and the second setsDisableMathRefactoring(bit 1). The decision moved toShader::settle_flags, so the test reads it the wayfinish()does. Checked that the test fails when the flag is set unconditionally. Ondxil.dll: the GPU tests on the RTX 4050 create their pipelines with the flag set, so the runtime accepted the containers; I did not run the standalone validator.@welisonzika14-jpg thanks for the Blackwell run. Good to know the RTX 5070 does not show it on
main. On the RTX 4050 Laptop (Ada, driver 595.97) all four tests fail onmainon every run, so it looks generation- or driver-specific; the change keeps the same results where the host does not fuse them.victory-shaderon the 4050 at 23e3b5a: 205 passed, 5 failed (the same 5 asmain), clippy clean.AI-assisted (Claude Code).
@rminzao on GitHub · 2026-11-25 01:06 UTC · original
Correction: the four failures are on the Intel iGPU, not the RTX 4050.
The laptop has an RTX 4050 Laptop GPU and an Intel UHD Graphics.
Host::new(false)creates the device on the default adapter (D3D12CreateDevice(None, ...)), and here that is the Intel UHD. I checked by printing the device's adapter (GetAdapterLuid→EnumAdapterByLuid). So every "hardware" result in the description came from the Intel UHD (driver 31.0.101.4146), which matches the Intel report in #1829. It also explains @welisonzika14-jpg's result: NVIDIA doesn't show the bug.I re-ran everything with a local, uncommitted switch that creates the test device on the high-performance adapter (
EnumAdapterByGpuPreference):victory-shader --libmain70564aefuses FMadarithmetic_matches_the_hardwareand the three BVH intersection tests).main. The PR changes nothing there andFMadstill fuses. The 3 failures are the view-heap cap and structured-view ones that other PRs cover.I've updated the description. Sorry for the wrong attribution.
AI-assisted (Claude Code).
@rminzao on GitHub · 2026-11-25 03:31 UTC · original
@dpalacios69 thanks. The test came in the next commit,
23e3b5a:emit::lower::tests::only_programs_with_fmad_let_the_host_refactorlowers av_fma_f32program and av_mul_f32+v_add_f32one, and asserts that only the first leaves refactoring allowed, i.e.NO_REFACTORING(bit 1) is set on the second. It checks the flag, not the GPU result; the device behaviour is what the BVH andarithmetic_matches_the_hardwaretests cover on the Intel UHD.AI-assisted (Claude Code).
@Marvisak on GitHub · 2026-11-25 07:25 UTC · original · edited 2026-11-26 02:17 UTC
Automated triage for the maintainers.
Merge candidate (recommending a look, not an approval). Reviewed at
60bc6a1, which merges cleanly intomain.Shader::settle_flags(crates/victory-shader/src/emit/shader.rs) sets shader-flag bit 1 (NO_REFACTORING, DXIL's disable-math-refactoring) unless the program emitted anFMad.Lower::fmadinemit/lower.rsis the only place that emitsFMad(tertiary.f32, op 46) and now marksfused, so programs that need the host to fuse keep today's behaviour, and the others keep their multiplies and adds apart, as the console rounds them. The new testonly_programs_with_fmad_let_the_host_refactorchecks the flag for both cases. The evidence ismain's own hardware tests (arithmetic_matches_the_hardwareand the three BVH intersection tests), which fail on Intel UHD and pass with this change. The patch is the same as the head that was a merge candidate before the conflict. CI:checkpassing.On your reply: the rebase keeps
main'suses_lane_indexandhas_half_wave_masknext torefactoring, as you describe. Thanks.Generated by Claude Code
@sinan-cmyk on GitHub · 2026-11-25 12:06 UTC · original
Offline measurement on a retail dump
Result: no count change against
main.I merged head 0489d107 onto
main940d32f(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 23,918 onmain, 23,918 with this PR.No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.
No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
@rminzao on GitHub · 2026-11-26 01:43 UTC · original
Rebased onto
main(63bcca4) and pushed60bc6a1. The only conflict was inLowered's test-only fields:mainaddeduses_lane_indexandhas_half_wave_maskwhere this PR addsrefactoring, so it keeps all three.cargo test -p victory-shader --libon the Intel UHD (31.0.101.4146): 296 passed, 7 failed.maingives 291 passed, 11 failed. The four this PR fixes still pass (arithmetic_matches_the_hardwareand the three BVH intersection tests), and the 7 left fail the same way onmain. Clippy is clean.AI-assisted (Claude Code).
@catminou4 on GitHub · 2026-11-24 23:52 UTC · original · approved these changes
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
70564ae), cargo test -p victory-shader --locked → 119 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows.Programs without FMad disable math refactoring: feature-gated optimization — conservative and correct.
@fuzeofficlal on GitHub · 2026-11-26 16:38 UTC · original · approved these changes
Reviewed
60bc6a153cee9386495e0fd0fc4aa2e5c3c9c445(merged cleanly onto63bcca43c6a00a758510398fa43096ce7e4f7df0) and verified on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP +dxil.dll).Raising
flags::NO_REFACTORING(1 << 1) inShader::settle_flagswhen a shader emits noFMadcleanly prevents host drivers from fusing separately-roundedfmul+faddsequences while keepingdx.entryPointsshader flags valid (dxil.dllclean).A couple of non-blocking follow-up points worth keeping in mind:
self.s.fusedis set whenLower::fmadis emitted rather than checking livedx.op.tertiary.f32opcode 46 calls remaining afterShader::finish()dead-code pruning, so a dead/unusedFMadstill leaves refactoring enabled.v_cvt_f32_u32currently routes throughself.fmad(hi, 65536.0, lo)even thoughhi * 65536.0is exact inf32, which also clearsNO_REFACTORINGfor shaders that otherwise have noFMad.emit/lower.rsreaches522,169 B(+1,606 B,2,119 Bunder the524,288 Bcap), so stacking with#379(+2,815 B) exceeds the cap by696 B.Verification
cargo run -p xtask -- policy(191 files ok),cargo clippy(default &--all-features) clean, changed linesrustfmt-clean.emit::lower::(34/34),emit::shader::(1/1), andemit::semantics_tests(34 passed, 1 ignored) on both RTX 5090 and WARP withdxil.dllactive.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.