Translator: lower six scalar and vector ALU instructions #2165
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2165"
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?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-29T10:04:41Z.
What it changes
Console-compiled compute shaders use a handful of ALU opcodes that the lowering in
emit/lower.rsdid not handle, and any one of them was enough to reject the whole shader. This adds them, using only helpers already inlower.rs; no other file changes.s_sext_i32_i8ands_sext_i32_i16shift the byte or half-word to the top of the register and back arithmetically.s_absdiff_i32computes botha - bandb - aand lets a signed compare pick the non-negative one, wrapping like the hardware, with SCC following the usual "result is not zero" rule.v_mad_u32_u16andv_mad_i32_i16take their 16-bit multiplicands throughhalf_src, soop_selis honoured. The signed form sign-extends both before the multiply, and the 32-bit addend is added in full.v_ldexp_f32has no DXIL equivalent: the exponent is clamped to ±300 with IMax/IMin and applied as twoexp2factors of half the exponent each, so that a small value with a large exponent, or the reverse, does not overflow the intermediate factor.How it was tested
cargo testpassed for the crates it touches: yes.cargo test -p victory-shader --releaseon829188e(2026.11.29.01): 462 passed, 0 failed, 9 ignored.Checked for overlap
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-29T10:25:25Z.
Review: COMMENTED
Tested locally merged onto
main(829188e9).crates/victory-shader/src/emit/lower.rs;cargo test -p victory-shader --lib emit::lower::tests::passed (25 passed; 0 failed; 1 ignored).Two review notes before approval:
v_ldexp_f32exponent split overflows/underflowsf32: Clampingnto[-300, 300]and splitting into two steps (half,rest) yields exponents up to±150. Sincef32overflows to+Infat2^128and becomes subnormal (flushing to0.0under FTZ) below2^-126, cases likea = 2^-140, n = 260computea * exp2(130) = +Infinstead of2^120, anda = 2^120, n = -256can flush to0.0instead of2^-136. Splittingninto three steps (each within[-100, 100]) guarantees every intermediateexp2factor stays strictly normal.s_sext_i32_i8,s_sext_i32_i16,s_absdiff_i32,v_ldexp_f32,v_mad_u32_u16,v_mad_i32_i16) have unit or GPU tests yet — adding a targeted test (especially forv_ldexp_f32corner exponents andv_mad_i32_i16sign-extension/op_sel) would lock down correctness.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-29T11:02:05Z.
Thanks for the review. Both points are addressed in
196d406.v_ldexp_f32: you were right, and on the old code your first case was worse than +Inf. Both halves overflowed, the tiny input flushed, and the result was NaN (0 * inf). The clamped exponent is now applied as three factors, n/3, n/3 and the remainder (at most 101 in size). Each factor is built exactly from its exponent bits rather than through exp2, so it is always a normal f32 and does not depend on exp2 precision. The running product moves monotonically from a towards the result, so it leaves the f32 range only where the result itself does.
While writing the tests I also found that s_absdiff_i32 computed the mathematical |a - b|. The ISA defines it as the wrapped difference, negated when negative, so i32::MIN - 1 gives 0x7fffffff, not 0x80000001. It now follows the ISA.
Tests: one GPU test per instruction in valu_tests.rs, run on the hardware adapter and on WARP:
The ldexp and absdiff tests fail on the previous commit.
cargo test -p victory-shader --release: 466 passed, 0 failed, 9 ignored.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Lyrnc on 2026-11-30T06:11:16Z.
Checked the six lowerings against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.
Setup
mainat41adb551with this PR's head (196d406) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).ldexplanes with a denormal input or result).super::emit(32-lane host),dxil.dllvalidation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon). On main every case below exceptsext_scc(whoses_sextresults are dead) is refused (s_sext_i32_i8/s_absdiff_i32/v_ldexp_f32/v_mad_u32_u16 is not supported).Cases
sext/scc0,sext/scc1:s_sext_i32_i8ands_sext_i32_i16of 8 values (0x7F, 0x80, 0xFF, 0x12345680, 0x8000, 0xFFFF7FFF, 0x7FFF, 0xABCD), SCC set to 0 or 1 before each.sext_scc: SCC read back after them.absdiff:s_absdiff_i32of 8 pairs with overflowing differences ((0x80000000, 1), (1, 0x80000000), (0, 0x80000000), (0x7FFFFFFF, 0xFFFFFFFF), ...), result and SCC. The gfx1036 gives the ISA's wrapped difference, negated when negative, and SCC = result != 0, in all 8 (0x80000000 - 1 gives 0x7FFFFFFF, 0 - 0x80000000 gives 0x80000000).ldexp:v_ldexp_f32of 64 (a, n) pairs: n from -1000 to 1000 (±127..151, 276, 277, 300 among them) and 0x7FFFFFFF, 0x80000000; a normal, denormal, ±0, ±inf, qNaN, sNaN, max. v10 = ldexp(a, n), v13 = ldexp(-a, n), v14 = ldexp(|a|, 3). The gfx1036 result is a times 2^n rounded once (to nearest even, denormals kept) in every finite lane; an sNaN comes back quiet.ldexp_big: 16 pairs with normal a and |n| from 150 to 300, most with a normal result (2^-100 with n = 200, 2^100 with n = -200, 2^-126 with n = 240, 1.5 * 2^-110 with n = 230, -1.25 * 2^90 with n = -210, ...), and ±0 / ±inf with |n| of 200 to 300. v10 = ldexp(a, n), v11 = ldexp(-a, n).mad16:v_mad_u32_u16andv_mad_i32_i16over 64 (a, b, d) combinations, withop_sel[0,0], [1,0], [0,1], [1,1], a literal multiplicand (0xFFFE), and withclamp.mad16_noclamp: the same without the twoclampinstructions. Withclampthe gfx1036 saturates: to 0xFFFFFFFF for the unsigned form, to 0x7FFFFFFF / 0x80000000 for the signed one (8 values in 6 lanes overflow).Results (values that differ from the gfx1036; "refused" = the translator rejects the program)
sext/scc0,sext/scc1(16 outputs)sext_scc(SCC kept)absdiff(result and SCC)mad16_noclamp(10 outputs)mad16(with the twoclampforms)integer output modifiers)ldexpv10, v13 (64 pairs)ldexpv14 (abs(a), n = 3)ldexp_bigv10, v11 (16 pairs, 4 times over)"The flushed denormal": the value the gfx1036 gives for that lane with IEEE off and f32 denormals flushed. Every value that differs on the Radeon is one of these, so the Radeon gives exactly the hardware's FTZ results.
Verdict:
s_sext_i32_i8/_i16,s_absdiff_i32andv_mad_u32_u16/v_mad_i32_i16withoutclampare correct on both cards. Withclampthe program is refused, which is safe (the gfx1036 saturates there: 8 values in 6 lanes overflow).v_ldexp_f32is correct on the Radeon apart from denormals, and wrong on the 5080 for |n| of about 192 and more.On the 5080 (and only there), with this PR:
The Radeon gives the gfx1036 value in every row but the last two: for the denormal input it gives 0 (the hardware's FTZ value), and it passes the sNaN through unquieted (as the hardware does with IEEE off). The same DXIL is dispatched on both, so this is the NVIDIA compiler. The pattern fits it multiplying the two equal
n/3factors together first, a * (f * f) * g: f * f leaves the f32 range once 2 * (n / 3) passes 127 or -126, whatever a is. (2^-100 with n = 150 has three equal factors 2^50 and is exact, so it is not the full product of the factors.) Any order of exact power-of-two multiplies is open to this under fast-math. Adding n to the exponent field in integer arithmetic where a is normal and the result exponent stays in 1..254, and keeping the factors only for the rest, would not depend on the host's reordering.Lanes with a denormal input or result come back flushed on both cards (34 / 38 values in
ldexp, all equal to the gfx1036's FTZ value), as your tests allow. The PR's three-factor product would also round twice there (for example 0x00000005 with n = -3: one rounding gives 0x00000001, the three factors 0), but neither host keeps those denormals, so that does not show.AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by PietroIacomino on 2026-11-30T09:32:41Z.
Thanks for the oracle runs. The diagnosis matches what I see: the factor product is only safe while the host keeps the multiplication order, so I replaced it.
v_ldexp_f32now addsnto the exponent field in integer arithmetic. No float operation is left for a host compiler to reorder:nis clamped to ±300 first.n.a.a's sign once nothing is left). So the double rounding you pointed out (e.g.0x00000005withn = -3) is gone too.a: ±0 and ±inf come back as themselves, and a NaN comes back quieted (payload kept,0x7F800001→0x7FC00001).a: it is made normal by an exact multiply by 2^64 andnis lowered by 64. A host that flushes denormal inputs gives a zero ofa's sign there, which is the hardware's FTZ value.abs/negonsrc0ofv_ldexp_f32are now applied to the sign bit alone. The float negate the other instructions use gave0x7FFFFFFFfor-0x7FC00001on an RTX 4070 Ti, while the hardware only flips the sign bit. Yourv13(ldexp(-a, n)) andv14(ldexp(|a|, 3)) rows go through this path.Test.
ldexp_matches_the_isareplacesldexp_keeps_every_factor_normal:a× 42 values ofn× 3 forms (plain,neg,abs) = 3,150 cases.acovers all the normal inputs from your table (2^-100, 2^-126, 1.5·2^-110, 1.5·2^-126, 2^100, 2^126, -1.25·2^90), the largest finite, 1 + 2^-23 for rounding at every denormal shift, denormals (0x1,0x5,0x7FFFFF,0x80400000), ±0, ±inf, a quiet NaN and a signalling NaN.nruns fromi32::MINand -1000 to 1000 andi32::MAX, including ±125…151, 200, 210, 230, 240, 250, 276, 277 and 300.atimes 2^n computed exactly in f64 and rounded once to f32; a NaN is expected quieted with its payload kept.The old implementation fails this test on the 4070 Ti as well, not only on the 5080.
ldexp(0xBFFFFFFF, -127)gave0x80000000instead of0x80800000: the running product went through the denormal range, was flushed, and the result, which is normal, came back as -0.I could not run it on an RTX 5080 or a Radeon. If you can repeat your oracle runs (
ldexp,ldexp_big) on this head, that would confirm the reorder case on the card where you saw it.v_mad_u32_u16/v_mad_i32_i16with clamp are still refused, as before.Tested:
cargo test -p victory-shader --release: 466 passed, 0 failed, 9 ignored (RTX 4070 Ti, WARP).AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this comment.
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.