Shaders: lower v_div_scale_f32, v_div_fmas_f32 and v_div_fixup_f32 #200
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!200
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-200"
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?
@n-romanovich on GitHub · 2026-11-24 01:22 UTC · original
n-romanovich:feat/valu-div-f32→main· +441 −0 · 4 files · 2 commits · openImplements IR tracking, lane-mask structuring, and DXIL lowering for RDNA2's IEEE-754
f32division sequence (v_div_scale_f32,v_div_fmas_f32,v_div_fixup_f32) invictory-shader:ir.rs&structure.rs: marksv_div_fmas_f32(andv_div_fmas_f64) as readingVCC, and resolvesVCCthrough lane-mask joins.v_div_scale_f32: scales tiny/huge numerator or denominator operands by2^64/2^-64when needed (skipping0,Inf, andNaN), writing the uncancelled scale flag tov.carry(lane_bits).v_div_fmas_f32: computesfma(src0, src1, src2)and applies2^64/2^-64scaling based onVCCandsrc2's biased exponent (>= 128).v_div_fixup_f32: handles IEEE-754NaNpropagation (quieting sNaNs),0/0&Inf/Inf(-NaN), division by zero /Inf, and result sign.Testing
emit::tests::valu_div_f32_sequence_executes_on_gpuincrates/victory-shader/src/emit/mod.rs, running the full 9-instruction RDNA2f32division sequence across 6 lanes (7.0 / 3.0,-1.0 / 1e-40,1.0 / 1e35,1e-35 / 1e-36,0.0 / 0.0,-1.0 / 0.0).dxil.dlland executed on D3D12 (NVIDIA GeForce RTX 5060 Ti).cargo fmt -p victory-shader -- --check: passed.cargo clippy -p victory-shader --all-targets -- -D warnings: passed (0 warnings).cargo xtask policy: passed (asset policy: 151 files ok).cargo test -p victory-shader: 70 passed, 0 failed.@DaviTostes on GitHub · 2026-11-24 01:30 UTC · original
Tested
3e8f2d3on Arch Linux x86_64, rustc 1.98.1: pass.cargo test -p victory-shader: 57 passed, 0 failed (2 test binaries), including the {adds} test{'s' if adds != 1 else ''} this PR adds.victory-gpuE0425.No game data used.
@r4tedx on GitHub · 2026-11-24 01:42 UTC · original
A note from checking the open opcode PRs against AMD's ISA reference (I have #163 open, which leaves these three refused): as far as I can read this diff,
v_div_scale_f32takes src1 as the numerator and src2 as the denominator (let (a, num, den) = (f(self, 0)?, f(self, 1)?, f(self, 2)?), and the test program passes them that way too). The RDNA 2 ISA has them the other way round, opcode 365:and its pseudo-code starts
so a zero numerator or denominator gives NaN there. LLVM's
LowerFDIV32emitsDIV_SCALE(RHS, RHS, LHS)for the denominator andDIV_SCALE(LHS, RHS, LHS)for the numerator, which matches (src1 = denominator, src2 = numerator).v_div_fixup_f32is (quotient, denominator, numerator) in the same document.If the roles are swapped in both the lowering and the test, the test would still pass while a real program's division scales the wrong way, so it may be worth a second look. I have not run this on hardware; this is from the documents only. No game data used.
@n-romanovich on GitHub · 2026-11-24 01:47 UTC · original
Thanks for the review, @r4tedx!
Fixed in
db20637:v_div_scale_f32operands to(src0, den, num)per RDNA2 ISA opcode 365 / LLVMLowerFDIV32, and returnedqNaN(0x7FC0_0000) when eithernumordenis zero.valu_div_f32_sequence_executes_on_gputo match the(den, num)operand order (keeping the originalnuminv1forv_div_fixup_f32) and re-verified all 70 tests invictory-shaderon D3D12 (RTX 5060 Ti).@r4tedx on GitHub · 2026-11-24 03:14 UTC · original
One more on this PR, for
v_div_fmas_f32. The lowering scales by 2^64 or 2^-64 depending on the exponent of src2 (0x5F80_0000/0x1F80_0000). Both of AMD's references give a fixed factor:if (VCC[threadId]) D.f = 2**32 * (S0.f * S1.f + S2.f); else D.f = S0.f * S1.f + S2.f;if VCC.u64[laneId] then D0.f = 2.0F ** 32 * fma(S0.f, S1.f, S2.f)(2^64 is the f64 form's factor.) A probe on an RTX 5070, with VCC set and inputs (1, 1, 0), returns
0x1F800000(2^-64) where the reference gives0x4F800000(2^32).For
v_div_fixup_f32, the same document's case list haselse if ((exponent(S2.f) - exponent(S1.f)) < -150) D.f = sign_out ? -underflow : underflowbefore the pass-through of the quotient; a probe with quotient 1, denominator 2^73, numerator 2^-126 returned 1 here.From the documents and synthetic probes only; I have no copy of the title and have not run this on console hardware.
@upipibi on GitHub · 2026-11-24 05:02 UTC · original
Review of
4c9a922(read against the RDNA2 ISA, not run):1.
v_div_scale_f32's VCC depends on which operand S0 is (lower.rs:3744,flag_raw = select(is_den, uncancelled, up_raw)). In the RDNA2 ISA (§12.12, V_DIV_SCALE_F32, opcode 365), VCC is set only by the "exp(S2) − exp(S1) ≥ 96", "1/S1 and S2/S1 denormal" and "S2/S1 denormal" cases, all functions of S1 (denominator) and S2 (numerator). S0 only decides whether this instance's D is scaled, so both instances of one division agree on VCC. LLVM's f32fdivexpansion feedsv_div_fmas_f32the VCC of the numerator instance (v_div_scale_f32 vN, vcc, num, den, num). The GPU test reads the denominator instance's instead (mod.rs:610/:622), which matches this lowering, so it passes. With LLVM's order:1.0 / 1e35: the ISA scales nothing (VCC = 0), giving 1e-35. Here den (biased exponent 243 ≥ 232) is scaled by 2^-64, num isn't, and the numerator instance's VCC is 0, so the quotient comes out about 1.84e-16.-1.0 / 1e-40: the ISA's "≥ 96" case sets VCC = 1 for both instances, giving -inf. Here the numerator instance's VCC isnum_tiny= 0, so the result is about -5.4e20.Swapping
vcc_loands16in the test reproduces it. Suggestion: follow the ISA's case list for VCC and for the scale-both cases, and use S0 only to pick whether this instance's D is scaled.2.
v_div_fixup_f32maps a NaN quotient to ±inf (lower.rs:3816,q_naninmake_inf). With finite, non-zero S1 / S2 the ISA returnssign_out ? -|S0| : |S0|, sofixup(NaN, 2.0, 1.0)should stay NaN; here it gives +inf.Also: this inserts right above the
v_fmac_f32 | v_mac_f32 | v_mac_legacy_f32arm that #284 rewrites, so the two will conflict textually.AI-assisted (Claude Code), read-only review.
@Lyrnc on GitHub · 2026-11-24 05:58 UTC · original
RDNA2 hardware probe of
v_div_fixup_f32: two cases where the lowering here differs from the hardware. The rest of the special cases I tried agree.Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs
v_div_fixup_f32 v10, v4, v5, v6(quotient, denominator, numerator), compared with a Python transcription of this PR'sv_div_fixup_f32arm. Default mode (IEEE on, f32 denormals flushed). RDNA2 desktop silicon, not the console's. No game data.0x7fc00000(NaN)0x3f8000000x000000000x000000000x7f8000000x7fc000000xbf8000000x800000000x000000000x7f8000000x7fc000000x7f8000000x3f8000000x000000000x7f8000000x7fc000000xff8000000x404000000x800000000xff8000000x400000000x7149f2ca(1e30)0x0da24260(1e-30)0x000000000x400000000x400000000x5d800000(2^60)0x12000000(2^-91)0x000000000x400000000x400000000x5d800000(2^60)0x12800000(2^-90)0x400000000x400000000x7fc000000x000000000x3f8000000x7f8000000x7f8000000x7fc000000x3f8000000x7f8000000x7f8000000x7f8000000x7fc000000x3f8000000x3f8000000x7f8000000x7f800000q_nanjoinsmake_inf, andmake_infis selected aftermake_zero. So a NaN quotient turns 0/x and x/inf into an infinity. On the hardware, 0/x and x/inf give a signed zero whatever the quotient is. The quotient's all-ones exponent only matters once the zero and infinity cases have passed: it gives ±inf, which you get right for finite non-zero operands (last row). Selectingq_nanbeforemake_zero(ormake_zerolast) would match.< -150. The lowering returns the quotient there. In a full division sequence the quotient for such operands is probably already 0 under f32 flush, so this may not show up end to end. I only checked the fixup step.The other cases match: 0/0 and inf/inf give
0xffc00000; a NaN numerator or denominator comes back quieted (0x7fa00000→0x7fe00000); x/0 and inf/x give a signed infinity.v_div_scale_f32andv_div_fmas_f32were not probed.AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
@upipibi on GitHub · 2026-11-24 06:06 UTC · original
Correction to point 2 of my review above, after @Lyrnc's RDNA2 probe: I said
v_div_fixup_f32should keep a NaN quotient as NaN when the operands are finite and non-zero. The hardware returns ±inf there (last row of their table: quotient NaN, 1.0 / 1.0 →0x7f800000), which is what this PR does. So that part of the PR was right and my reading of the ISA was wrong. What the hardware does show is the order @Lyrnc describes: 0/x and x/inf give a signed zero whatever the quotient is, somake_zerohas to win over the NaN-quotient case, plus the< -150exponent-difference underflow step. Point 1 (v_div_scale_f32's VCC) is unaffected; it wasn't part of this probe.AI-assisted (Claude Code).
@n-romanovich on GitHub · 2026-11-24 08:45 UTC · original
Thanks for the review! Addressed the feedback and resolved the compilation issue in the latest commit. All tests pass cleanly.
@DeerSilver on GitHub · 2026-11-24 10:51 UTC · original
Tested head
7706273merged ontomainffe310eon NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.Merged onto current
main, the test build fails.6638098("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument toresource::track:There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break
cargo testonmain. The call this PR adds needs the fourth argument.main's own tests passSome(&st);Nonekeeps the old three-argument behaviour.With
, Noneadded locally to that one call, everything passes:valu_div_f32_sequence_executes_on_gpucargo test --workspace --locked: 343 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored onmain.cargo clippy --workspace --all-targets --locked -- -D warnings: clean.AI-assisted (Claude Code).
@alexdeliu on GitHub · 2026-11-24 17:23 UTC · original
Merged onto
main@ffe310eb, this PR (77062732) no longer compiles its tests.crates/victory-shader/src/emit/mod.rs:833callsresource::trackwith 3 arguments. Since6638098cit takes a 4th,lanes: Option<&Structured>; main's tests passSome(&st)withlet st = structure::structure(&p).unwrap();.Checked with CI's steps (both clippy runs with
-D warnings,cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87@n-romanovich on GitHub · 2026-11-24 17:54 UTC · original
Thanks for testing and pointing this out, @alexdeliu!
Merged latest main and updated resource::track in the test to pass Some(&st) per
6638098c. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.@alexdeliu on GitHub · 2026-11-24 17:56 UTC · original
@n-romanovich rechecked
93f052e2merged ontomain@ffe310ebon the RX 9070: both CI clippy runs are clean andcargo test --workspacepasses (343 tests).@upipibi on GitHub · 2026-11-24 10:07 UTC · original · edited 2026-11-25 23:55 UTC
Update, head
03360c6(2^64 / 2^-64 scaling and the three lanes from this comment):cargo test -p victory-shader --lib div_f32 -- --test-threads=1, RTX 3070 (driver 616.56):valu_div_f32_sequence_executes_on_gpupasses 3 of 3, including lanes 10-12 (1e30 / 1e-5,1e-20 / 1e20,0.1 / 1e38), which failed on2d240dc. Note the test's bounds for lanes 11 and 12 are absolute (1e-39, 1e-38), looser than the +0-or-within-2e-45 check in the commit below. Still open from this comment: a denormal S1 andexp(S2) <= 23inv_div_scale_f32.Checked
7706273: both points are addressed. VCC now comes only from the two operands' exponents (case_overflow | case_denorm), so both instances of one division agree. Andv_div_fixup_f32putsmake_zerobefore the NaN quotient and adds the< -150underflow, matching @Lyrnc's measurement.One question, from reading the code (not run): the new
v_div_scale_f32scales only in those two cases. The ISA's other scaling rows (§12.12, opcode 365) are gone: S1 denormal →ldexp(S0, 64)for both instances;1/S1denormal →ldexp(S0, -64); andexp(S2) <= 23→ldexp(S0, 64). With f32 denormals flushed on the host these may never change the final quotient, but a denormal denominator or a numerator near 2^-104 would be the inputs to check. Is dropping them intentional?Added 17:04Z, for the three lanes the triage asks for: a test commit,
708f504on branchev-200-testsof upipibi/VIctory, on top of2d240dc(adds lanes 10-12 tovalu_div_f32_sequence_executes_on_gpu, +26/-4; each lane is checked against the true quotient, and for the two denormal quotients +0 is accepted too). RTX 3070, driver 616.56, Windows 11, PR head2d240dc:1e30 / 1e-51e-20 / 1e200.1 / 1e38All three fail on the head; lanes 0-9 still pass. In a local experiment (not in the commit),
v_div_fmas_f32scaling by 2^64 or 2^-64, chosen by S2's exponent, fixes the first two; the third also needsv_div_scale_f32to scale only the denominator, by 2^-64 (scaling it by 2^64 overflows 1e38 to inf). That is NVIDIA running the translated sequence, not an AMD probe, so the factor rule still wants an RDNA2 measurement.@n-romanovich,
8201c0eisn't on the PR branch:feat/valu-div-f32still points at2d240dc. It sits on an oldermain(42363ab) and still uses a fixed 2^32 inv_div_fmas_f32.AI-assisted (Claude Code, Claude Opus 5.5).
@Marvisak on GitHub · 2026-11-25 00:22 UTC · original · edited 2026-11-26 00:21 UTC
Automated triage for the maintainers.
Merge candidate (unchanged). The head is still
03360c6, the commit the previous merge-candidate verdict covered. It lowersv_div_scale_f32,v_div_fmas_f32andv_div_fixup_f32incrates/victory-shader/src/emit/lower.rs, marksv_div_fmas_*as reading VCC inir.rs, and resolves VCC through lane-mask joins instructure.rs. Thev_div_fixup_f32case order (signed zero before the NaN quotient, the< -150underflow) matches the RDNA2 probe in this thread.valu_div_f32_sequence_executes_on_gpuchecks the full nine-instruction sequence across 13 lanes. The branch still merges cleanly into currentmain.On your reply: thanks for the summary. It matches what is on the branch. The small follow-ups from last time still apply but don't block: the priority comment for row 3 still says "S2 * 2^64", and the
v_div_fmas_f32comment credits the 2^±64 rule to opcode 367, whose text says 2^32. An RDNA2 probe ofv_div_fmas_f32with VCC set would still be the best hardware evidence for the factor.CI: passing.
Generated by Claude Code
@n-romanovich on GitHub · 2026-11-25 00:46 UTC · original · edited 2026-11-25 00:53 UTC
@Marvisak, thanks for the review.
In 8201c0e, I implemented the remaining
v_div_scale_f32cases incrates/victory-shader/src/emit/lower.rs:S1denormal (ldexp(S0, 64)for both operands,VCC = 0), using normal-float scaling(2^-62 + mant * 2^-85) - 2^-62when!case_overflowso FTZ does not flush denormal inputs to0.1/S1andS2/S1both denormal (S1 * 2^-64,S2 * 2^64,VCC = 1).1/S1denormal (|S1| > 2^126:ldexp(S0, -64)for both operands,VCC = 0).exp(S2) <= 23(ldexp(S0, 64)for both operands,VCC = 0).Added the requested GPU test lanes in
crates/victory-shader/src/emit/mod.rs(1e30 / 1e38,1e-40 / 2e-40, and2e-34 / 4.0) and re-rancargo test -p victory-shader --locked -- valu_div_f32_sequence_executes_on_gpuon Windows 11 (NVIDIA GeForce RTX 5060 Ti):1 passed, 0 failed.On CI,
valu_div_f32_sequence_executes_on_gpupasses; the two failures inemit::synthetic_tests(gathers_at_a_level_match_the_hardwareandpermutes_read_the_lane_named_and_zero_from_lanes_left_out) are pre-existingmainfailures on the WARP runner (no hardware GPU / wave-32 support).@n-romanovich on GitHub · 2026-11-25 22:57 UTC · original
@Marvisak Updated
feat/valu-div-f32(03360c6):v_div_fmas_f32to use the exponent-dependent2^64/2^-64scale factor based onexp(S2) > 127instead of a fixed2^32.v_div_scale_f32to scale only the denominator instance.1e30 / 1e-5,1e-20 / 1e20, and0.1 / 1e38lanes tovalu_div_f32_sequence_executes_on_gpu.valu_div_f32_sequence_executes_on_gpuand CIcheckpass.@fuzeofficlal on GitHub · 2026-11-26 00:42 UTC · original
Code review and RTX 5090 + WARP + DXIL verification at head
03360c672be4merged cleanly ontomain(63bcca43c6a0):Two blocking issues found (one reproduced on both RTX 5090 and WARP hardware):
xtask):main(63bcca43),crates/victory-shader/src/emit/lower.rsreaches533,031bytes (8,743B over the524,288-byte limit), causingcargo test -p xtask --lockedto fail (5 passed; 1 failed):POLICY: crates/victory-shader/src/emit/lower.rs: 533031 bytes exceeds the 524288-byte limitv_div_scale_f32/v_div_fmas_f32/v_div_fixup_f32helpers into a submodule (e.g.emit/lower/div.rs) will resolve this cleanly.case_overflow(reproduced on RTX 5090 & WARP):v_div_scale_f32:let a_denorm_keep = self.s.m.bin(c, Bin::And, a_is_denorm, not_overflow);S1is denormal (e_den == 0) and|num| >= 2^-31(exp(num) - exp(den) >= 96),case_overflowclearsnot_overflow, forcinga_denorm_keep = falseand takingFMul(S1, 2^64). Under D3D12 f32 FTZ, denormalS1flushes to0.0, turning finite quotients into±Inf.-1.0 / 1e-40overflows to-Infanyway so it masked this. Probing2^-20 / 2^-132(0x3580_0000 / 0x0002_0000, exact quotient2^112 = 0x7780_0000) returned0x7f800000(+Inf) on both RTX 5090 and WARP.not_overflowgate (let a_denorm_keep = a_is_denorm;) preserves the denormal exponent scaling path cleanly.// 3. 1/S1 and S2/S1 both denormal: S1 * 2^-64, S2 * 2^64, VCC = 1does not match the code (s_num_uponly scalesS2onc5, which is actually the right behavior sincev_div_fmas_f32later scales by2^-64whenVCC=1andexp<=127). Updating the comment avoids confusion.valu_div_f32_sequence_executes_on_gpucurrently runsHost::new(false)only; iteratingfor warp in [false, true]and adding a finite-quotient denormal-denominator case (2^-20 / 2^-132) would lock in both backends.cargo clippy(default +--features diagnostics) 0 warnings; existingvalu_div_f32_sequence_executes_on_gpupassed on RTX 5090 (and WARP when probed);emit::lower::tests33/33;emit::semantics_tests34/34 (1 ignored).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.