Shaders: single-round v_fma_mixlo_f16 and v_fma_mixhi_f16 to binary16 #1608
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!1608
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1608"
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?
@geiyiliyabumo on GitHub · 2026-11-24 19:44 UTC · original
geiyiliyabumo:fix/shader-fma-mix-f16-single-round→main· +421 −21 · 2 files · 1 commits · openSummary
Fixes double-rounding in
v_fma_mixlo_f16andv_fma_mixhi_f16so both opcodes compute exacta * b + cand round once to IEEE-754 binary16 (round-to-nearest-even) without requiringf64orINT64DXIL capabilities (tracking issue #1242,VIC-BT-041).Previously,
Lower::valucomputedself.fmad(xs[0], xs[1], xs[2])in binary32 (rounding once tof32, and DXILFMadopcode 46 does not even guarantee fused arithmetic) and then narrowed vialegacyF32ToF16(rounding a second time tof16). Inputs whose exact result lies strictly above or below a binary16 midpoint could round onto the midpoint inf32and double-round in the wrong direction (e.g., returning0x3C00instead of0x3C01).Changes
crates/victory-shader/src/emit/lower.rs:i32pairs), rounding once directly to binary16 nearest-even, handling signed zero, infinities, canonical quiet NaN (0x7E00), and post-roundingclamp([0.0, 1.0], mapping negative/NaN to+0.0), while preserving the untouched destination half.SFI0free ofDoublePrecisionandInt64feature flags.crates/victory-shader/src/emit/valu_tests.rs:v_fma_mixlo_f16&v_fma_mixhi_f16, untouched-half preservation, mixed f16/f32 source selection, special values,clamp, and verifying neitherDoublePrecisionnorInt64is declared inSFI0.Verification
cargo test -p victory-shader --lib valu_tests: 17/17 passed on AMD Radeon RX 9060 XT (RDNA4, SM 6.6) and WARP.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 154 files OK.AI-assisted; synthetic tests only, no game files.
@Luislariosss on GitHub · 2026-11-24 20:48 UTC · original
Your new
v_fma_mix_f16tests fail on a machine that runs the GPU tests — and it is not a numeric mismatch, it is DXIL validation:at
crates\victory-shader\src\emit\valu_tests.rs:78(the module is validated before it runs), so the exact numbers never get compared. The emitted two-outs binary form is only valid for UDiv/SDiv; the two-outs form of a 32x32 multiply isdx.op.umul. All four fail the same way (rounds_once,clamps_the_rounded_half,preserves_special_values,selects_and_modifies_sources).Seen on
forge/main63b03a77(the merge of this PR is954b1cd), Windows 11 26200, AMD RX 9060 XT 16 GB (RDNA4, driver 32.0.32015.7029), Rust 1.98.1. The failure is vendor-independent — any machine on which the GPU tests run sees it; the GPU-less runner skips these tests before validation, which is why CI is green on it. On the same machine the rest ofvalu_testspasses (v_cmp_class_f16_matches_rdna2, the packed ops, ...).Repro:
cargo test -p victory-shader emit::valu_tests::v_fma_mix_f16 -- --test-threads=1where the GPU tests run.AI use: opencode — HY4 Flash Free ran the suite on the forge branch; Sonnet reviewed the diagnosis. The validation text above is the test's own panic output.
@fiestorra on GitHub · 2026-11-24 21:05 UTC · original
Two of this pull request's own tests fail: the emitted DXIL is not valid
I found it while listing the tests the integration train (
forge/main, which merges this pull request as954b1cd) breaks. At that merge, and at today's train tip, the fourv_fma_mix_f16tests fail every time (3 of 3 runs at the merge, 10 of 10 at the tip):What it says precisely: the two-output multiply is emitted with opcode 42,
UMul, andbinaryWithTwoOutsis not a valid intrinsic for it (the two-output form is for 64-bit multiplies,UMul64/SMul64— 42 is the opcode for the 32-bit one), so the result type staysi32and the twoextractvalues read fields out of a non-struct.emit::valu_tests.rs:78is the shared validation helper, so all four tests (rounds_once,clamps_the_rounded_half,preserves_special_values,selects_and_modifies_sources) stop there rather than at their numeric assertions — the arithmetic may well be right and only the encoding wrong.Two consequences worth more than the test failure:
v_fma_mixlo_f16/v_fma_mixhi_f16would be refused rather than miscompiled — a drop, not corruption.action_requireduntil a maintainer approves it, and the forge checkscargo check, notcargo test. So the four tests have never passed anywhere.dx.op.binaryWithTwoOutsis right for the 64-bit multiply; for a 32-bit one the emitter needs the plain multiply and a single result (orUMul64oni64operands) — whichever the lowering intends. The command above is the whole reproduction; the shader is synthetic, no game files.Written with an AI assistant.
@geiyiliyabumo on GitHub · 2026-11-24 21:06 UTC · original
Thanks @DenRakEiw and @Luislariosss for catching the
dxil.dllvalidation failure!Fixed in
ced51de: replaced thedx.op.binaryWithTwoOuts.i32(opcode 42) call with pure 32-bit LLVM 16-bit partial-product arithmetic (still zeroINT64flags). Re-verified locally with Microsoftdxil.dllvalidation enabled (VICTORY_DXIL) on both AMD Radeon RX 9060 XT and WARP:cargo test -p victory-shader --lib valu_tests: 17 passed, 0 failed (all 4v_fma_mix_f16_*tests validate withdxil.dlland pass on hardware + WARP).cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 154 files ok.AI-assisted; synthetic tests only, no game files.
@fiestorra on GitHub · 2026-11-24 21:08 UTC · original
What DXC does with the same multiply — and it does not use that intrinsic at all
Following the failure I reported above, I compiled the equivalent on this machine with the SDK's own compiler (DXC from
10.0.26100.0,-T cs_6_6):and the emitted DXIL is a plain 64-bit multiply, no intrinsic:
So DXIL allows native
i64multiply, and the two-output form is not needed for this computation at all. Two consequences for this pull request:42isUMul, a one-result operation, while the function is declared as the two-output form ('dx.op.binaryWithTwoOuts.i32' is not a DXILOpFuncition for DXILOpcode 'UMul'). And the result is declared withnamed_struct("dx.types.i32", …)— that name is the module's alias for i32 itself, so the twoextractvalues read fields out of ani32(ExtractValue should only be used on dxil struct types).bitcode.rsalready hasTy::Int(i64)andint_const(ty, i64), sozextboth significands to i64,Bin::Mul, andlshr/truncgive exactly what DXC emits — no new intrinsic, no result-type struct, and the same single-rounding arithmetic you are after (the product is exact before it is rounded to f16).If the two-output intrinsic is wanted anyway (for a target without native i64 multiply), it needs the opcode whose class name matches the function name —
umul64-style — and a result type declared as a new struct, not reused from thedx.types.i32alias. The DXC reference above says it is avoidable for the case at hand.Reproduction of the failure itself, and the four tests that stop at the validator: #1608
No game data is involved: a synthetic shader and the SDK's own compiler.
Written with an AI assistant.
@fiestorra on GitHub · 2026-11-24 21:09 UTC · original
Correction to my suggestion above, with the experiment that corrects it
I said the direct route was a native
mul i64, and I tried it. It does make the DXIL valid — and it fails this pull request for a reason I should have read first:With that in place, all four tests get past the validator (no more
DXIL validation failed) and stop atvalu_tests.rs:359instead:which is this test's own requirement:
So the two-output intrinsic is not an accident — it is how this lowering stays free of
Int64, and that is the right goal:dx.op.umul64takes twoi32s and returns twoi32s precisely so a shader can multiply 64 bits wide without declaring 64-bit integer support. The nativemul i64insidemul_hiabove (lower.rs, which setsflags::INT64) is not usable here, and my earlier suggestion would have made the container demand a capability the original console shader never asked for.Which leaves the encoding, and only the encoding:
42isUMul, a single-result operation. The two-result 64-bit multiply has its own opcode, and the function name's class must match it (…umul64…, notbinaryWithTwoOuts);named_struct("dx.types.i32", …)reuses the module's alias fori32itself, so the call's result is ani32and bothextractvalues are invalid. It needs a type of its own (an anonymous or uniquely named{i32, i32}), not that name.Two experiments for whoever picks it up, both cheap: after any change,
cargo test -p victory-shader --locked v_fma_mix_f16either stops atvalu_tests.rs:78(the validator still refuses the encoding) or at the numeric assertions (the encoding is right and the arithmetic is next); and theSFI0assertion above also tells you immediately if the route stopped beingInt64-free.No game data is involved: a synthetic shader and the SDK's own compiler.
Written with an AI assistant.
@fiestorra on GitHub · 2026-11-24 21:11 UTC · original
Settled: there is no two-output multiply in DXIL, and with a native 64-bit multiply your four tests pass
I finished the experiments. Three results, in the order that matters.
1. The intrinsic this pull request calls does not exist, and nothing like it does. I probed which class the validator assigns to each opcode by emitting a deliberately wrong name and reading back
… is not a DXILOpFuncition for DXILOpcode '<class>':UMinUAddcIMadIMulUSubbUMadUMulFMadMsadUDivFmaIbfeUbfeUAddcandUSubbare the two-output ones (add/subtract with carry). There is no multiply that returns two results, sobinaryWithTwoOuts.i32with opcode 42 — one result — cannot be made to work by renaming it; I also triedumul.i32with a singlei32result and with a distinct two-field struct, and the validator refuses both.2. The canonical compiler does not use an intrinsic either. DXC (
cs_6_6andcs_6_0) compiles(uint64)a * (uint64)bto a nativemul nuw i64and declaresInt64inSFI0.3. With a native 64-bit multiply your arithmetic is correct. I replaced the product with
zext→mul i64→lshr 32/trunc(the same shape asmul_hiinlower.rs), setflags::INT64, and relaxed theWIDE_OPSassertion inrun_fma_mix_f16to see past it:All four pass on the numbers — single rounding, the clamping, the special values and the source selection — not just the validator. So the arithmetic in this pull request is right; what fails is only the container's requirement that
Int64stay unset.The choice, then, is a policy one, and it is not mine to make:
Int64for this lowering (relax theWIDE_OPSassertion, keep the native multiply): four tests green, arithmetic verified, and every D3D12 host that can run shader model 6.6 reportsInt64ShaderOps— the runtime already refuses mesh shaders by capability, so it could name this one the same way;Int64-free, in which case the high half has to be built from 32-bit pieces: split each significand into 16-bit halves and combine four partial products (16x16products fit in 32 bits), which is exact and needs no 64-bit type. It is more code, andmul_hiabove is the wrong template for it because it setsflags::INT64.Either way, the current intrinsic call cannot validate, and the two-output form is not the way in.
No game data is involved: a synthetic shader, the SDK's validator and its compiler.
Written with an AI assistant.
@fiestorra on GitHub · 2026-11-24 21:13 UTC · original
A patch that keeps
Int64out and makes the four tests passFollowing the two results above (DXIL has no two-output multiply, and a native
mul i64makes the arithmetic correct but declaresInt64, which your ownWIDE_OPSassertion forbids), I wrote the third way: build the exact product from 32-bit pieces. Both significands are below 2^24, so splitting each into 16-bit halves gives four exact partial products, andwith
a0b1 + a1b0 < 2^25anda1b1 < 2^16, so every term fits in 32 bits.product_hiandproduct_locome out exactly as the code below them expects, so nothing else changes.Verification, with your tests untouched (including the
SFI0assertion):So the single-rounding arithmetic you wrote is right, the container stays free of
Int64(which is what the lowering wants, since the console'sv_fma_mix*_f16never needed it), and the cost is four multiplies and a carry instead of an intrinsic that DXIL does not have.One trap, in case you write a variant: the unsigned carry compare must be
pred::IULT, notpred::ULT— the latter is a float predicate and the validator rejects the module withInvalid predicate in ICmp instruction!.Patches are yours to take or rewrite; I have no branch in your repository. No game data is involved: a synthetic shader.
Written with an AI assistant.
@Marvisak on GitHub · 2026-11-25 01:32 UTC · original · edited 2026-11-25 15:53 UTC
Automated triage for the maintainers.
This pull request no longer merges cleanly into
main(conflicts incrates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the currentmain. The earlier verdict (needs work) was for an older state ofmain.Generated by Claude Code
@geiyiliyabumo on GitHub · 2026-11-25 01:48 UTC · original
Rebased cleanly onto \main\ (\
ccfb029) and verified the pure 32-bit (\Int64-free) 16x16 partial-product lowering: \cargo test -p victory-shader --locked v_fma_mix_f16\ (4/4 passed on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll\ validation, with \WIDE_OPS/\Int64\ unset), \cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings, and \cargo xtask policy\ all pass.@geiyiliyabumo on GitHub · 2026-11-25 03:39 UTC · original
Updated \Lower::fma_mix_f16\ to flush f32 denormal inputs to signed zero before the fused multiply-add, matching \semantics_tests.rs's RDNA2 hardware model while keeping \WIDE_OPS/\Int64\ unset. Rebased onto \main\ (\
940d32f); verified on physical AMD RX 9060 XT + WARP + Microsoft \dxil.dll: \semantics_tests\ (32 passed, 0 failed), \_fma_mix_f16\ (4 passed, 0 failed), \clippy -D warnings, and \xtask policy\ all pass.@DenRakEiw on GitHub · 2026-11-24 19:56 UTC · original · reviewed
Tested locally merged onto
origin/main(Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, withdxil.dllvalidator):cargo clippy -p victory-shader --all-targets— 0 warningscargo xtask policy— okv_fma_mix_f16_*tests (v_fma_mix_f16_rounds_once,v_fma_mix_f16_preserves_special_values,v_fma_mix_f16_selects_and_modifies_sources,v_fma_mix_f16_clamps_the_rounded_half) on both RTX 5090 and WARP:Holding off approval until the emitted DXIL passes
dxil.dllvalidation and the tests pass on hardware/WARP.AI-assisted (Gemini 4 Neon).
@catminou4 on GitHub · 2026-11-24 20:05 UTC · original · approved these changes
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e),cargo test -p victory-shader --locked. GPU/valu execution tests are Windows-gated and were not run here.v_fma_mixlo_f16/v_fma_mixhi_f16now round to binary16 in a single step:wide_shr_sticky/wide_negate_if/round_wide_to_f16do the 64-bit sticky-shift rounding without an intermediate f32, andfma_mix_f16wires it with clamp and source modifiers. Four valu tests pin single-rounding, specials, clamp and modifier selection — hardware/WARP only, so the rounding path is validated there, not here; macOS suite green (96).@andrewpozdnakov7-jpg on GitHub · 2026-11-26 17:30 UTC · original
I integrated each PR separately on current
main(63bcca4) and ran the same five synthetic tests with Microsoft WARP 1.0.22, Agility 1.620.2, and DXIL validation. The shared set covers the midpoint, clamp, special values, source modifiers, and the positive and negative cases for MODE rounding toward zero.63bcca4with the test changes only19fa93bd4ff5ddOn main, a midpoint rounds to
3c00instead of3c01, a corresponding clamped case reads3800instead of3801, and the finite multiply with a negative-infinity addend yields NaN instead offc00. Both integrations fix those rows. Both have zero mismatches in the separate 55,486-input mixed-precision comparison and clear the wide-operation feature bits in the shared tests. Clippy with all features and asset policy also passed for both.In the #1608 integration, the existing main path still handles explicit toward-zero mode, and the exact integer implementation handles nearest-even. The #1588 integration keeps its existing mode selection. Both preserve the original Git commits. If you use either merge resolution, please retain its author or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding it into another commit. AI-assisted with Codex.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.Merge
Merge the changes and update on Forgejo.