Skip to content

Shaders: lower v_cvt_f32_f64 - #2348

Open
Calvix2k wants to merge 3 commits into
Marvisak:mainfrom
Calvix2k:codex/shader-cvt-f32-f64
Open

Calvix2k wants to merge 3 commits into
Marvisak:mainfrom
Calvix2k:codex/shader-cvt-f32-f64

Conversation

@Calvix2k

Copy link
Copy Markdown
Contributor

Three of the compute shaders this title's frame runs once per frame — 5dc3b259…, 6f225b64…, 926c46ba…, the shape of full-screen post-processing passes — do not translate, and the reason is v_cvt_f32_f64:

victory_gpu::pipeline: shader does not translate; its pipelines' draws are dropped
  stage="compute" hash=5dc3b25986622949
  error=converter shader.dxil v1: GFX10 code at 0x7e8: v_cvt_f32_f64 is not supported

What the fix does

The VGPR pair is read as 64 bits (src64), bitcast to a double, and truncated with the host's fptrunc — round to nearest even, which is MODE's default for 64-bit ops. MODE's toward-zero mode has no DXIL form and is refused by name rather than mis-rounded.

The IR had no 64-bit float type, so it gains double (bitcode type 4) and the fptrunc cast (LLVM opcode 7); a module that uses it declares the doubles feature — SFI0 0x1 and the module feature-info bit 2. Both are needed: without the feature-info bit dxil.dll says Flags must match usage, and without the SFI0 bit the container no longer matches the module.

Verification

  • a_64_bit_value_converts_to_f32_round_to_nearest: ten cases run on the card (Windows 10 / RX 7800 XT, SM 6.6 through the Agility runtime for the test binaries) and checked against Rust's as f32 — 1.0, −2.5, 0.1, a tie that rounds to even, the two smallest subnormals (→ 0), f64 max (→ inf), ±inf, and a NaN (a NaN out with a payload). WARP here tops out at SM 6.2, so hardware is the device exercised; the DXIL is validated by dxil.dll.
  • cargo test -p victory-shader --lib: 248 passed, 0 failed, 3 ignored; clippy -D warnings clean.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Clean lowering of v_cvt_f32_f64 via double bitcast + FpTrunc with flags::FLOAT64 and PSV feature bit 0x1, refusing toward-zero mode (mode16 == Some(3)).
  • cargo test -p victory-shader a_64_bit_value_converts_to_f32_round_to_nearest: 1/1 passed on RTX 5090, 1/1 passed on WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

@DrDarkDK

Copy link
Copy Markdown

I checked the f32 output underflow boundary on 91fa9dd with an RTX 4070 Ti (621.01) and WARP. All 16 synthetic inputs matched the round-to-nearest-even reference bit for bit on both adapters.

The smallest and largest f32 subnormals survived with both signs. At the positive zero-to-subnormal boundary, 2^-150 rounded to 0x00000000, and the next binary64 value above it rounded to 0x00000001. The negative cases preserved the sign bit.

This extends the existing underflow-to-zero cases to inputs that must produce a nonzero subnormal. Default rounding only. No game data used.

AI-assisted with Codex.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — v_cvt_f32_f64 lowered.

@Xvania7 Xvania7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review and local verification of 91fa9dd:

  • Clean lowering of v_cvt_f32_f64 to dx.op.unary(80, ...) / fptrunc to float
  • emit::lower::tests:: (21 passed)
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings
  • cargo xtask policy: 182 files ok
  • CI hardware test run is verified green on GitHub Actions Windows CI.

Approving.

@Calvix2k

Copy link
Copy Markdown
Contributor Author

Third hardware vendor, and the boundary cases are the ones I wanted checked: 2^-150 to zero and the next value up to the smallest subnormal (0x00000001) is exactly where a naive fptrunc-free conversion would be wrong, and the sign-preserving negatives close it.

For the record, the AMD result on the same head (91fa9dd) matches: the ten synthetic cases (including the two smallest subnormals, f64 max at ±inf bounds and a NaN) are bit-for-bit against Rust's as f32 on the RX 7800 XT, and WARP tops out at SM 6.2 here so it skips. Your WARP run is the one that covers the software path on a device that can take SM 6.6.

@tyr0nsur

Copy link
Copy Markdown

Two source-authored cases exposed operand and rounding issues while integrating this revision. On an RTX 4060, a literal operand of 0xC0020000 represents the high word of binary64 0xC002000000000000 (−2.25). The current path returned f32 bits 0x00000000. Placing the literal in bits 63:32 and zeroing the low word returned the expected 0xC0100000. The register-pair control already worked. AMD RDNA 2 ISA §6.2.1 describes the literal convention.

The mode16 == Some(3) guard also admits positive-infinity rounding, negative-infinity rounding and a dynamically written mode, although the subsequent fptrunc still uses nearest-even. We now accept only Some(0) for this lowering. Our regression sets the two DP rounding bits with s_setreg_imm32_b32 for each directed mode and uses s_setreg_b32 for an unknown value. Both tests are independent of the underflow boundary cases covered above.

@Calvix2k

Copy link
Copy Markdown
Contributor Author

Both findings hold, and both are fixed in 6f8acac:

The literal source. You are right that a literal in a 64-bit operand is the high word of the binary64: src64 reads a literal as the sign-extended integer its bits are, which is correct for 64-bit integer operands and wrong here. The lowering handles the literal itself now — zero-extend the literal, shift it into bits 63:32, low word zero — and a_64_bit_literal_source_is_the_high_word runs exactly your case (0xC0020000 → 0xC0100000) on the card, so the convention is pinned by a test rather than by the comment.

One detail that fell out of it: the literal form builds the 64 bits with integer shifts, so the module has to declare the 64-bit integer feature as well (dxil.dll: "Flags must match usage" otherwise). That is set with the float feature now.

The mode check. Tightened to mode16 == Some(0): directed (1, 2), toward-zero (3) and a mode written at run time (None) are refused by name. Thank you for the s_setreg_imm32_b32 / s_setreg_b32 regression — that is the case my Some(3) check would have let through as "nearest even", which is exactly the silent mis-rounding this PR exists to avoid.

Lib suite after both: 249 passed, 0 failed, 3 ignored (the new literal case included); clippy clean. If your two regressions are on top of 6f8acac they should now pass unchanged; if either still fails I would like the case, since it would mean a third convention.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/lower.rs and emit/valu_tests.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: refuse or apply VOP3 abs/neg on the new literal and inline-constant arms of v_cvt_f32_f64 (they bypass src64), ideally with a test.


Generated by Claude Code

Calvix2k added a commit to Calvix2k/VIctory that referenced this pull request Nov 25, 2026
Review feedback on Marvisak#2348: `v_cvt_f32_f64` lowers an inline float constant
(operand codes 240-248) through `src64`'s fallback, which zero-extends the
**f32** bit pattern — `1.0` became 0x000000003F800000, a subnormal that
converts to 0, where the hardware uses the binary64 of the value (RDNA 2 ISA
6.2.1).

The instruction now maps 240-248 to the same values as binary64 (including
1/(2π)) and builds the bits with an integer constant; register pairs and
literals keep their paths. `a_64_bit_inline_constant_source_is_the_same_value`
converts code 242 (1.0) and expects 1.0; before this it got 0.
@Calvix2k
Calvix2k force-pushed the codex/shader-cvt-f32-f64 branch from 6f8acac to f956a66 Compare November 25, 2026 16:05
@Calvix2k

Copy link
Copy Markdown
Contributor Author

The gap is fixed in f956a66 (rebased onto main 67e8ae1): v_cvt_f32_f64 now maps inline float constants 240-248 to the binary64 of the same value (0.5, -0.5, 1.0, -1.0, 2.0, -2.0, 4.0, -4.0, 1/(2π)) and builds the bits with an integer constant, instead of zero-extending the f32 pattern through src64. a_64_bit_inline_constant_source_is_the_same_value converts code 242 (1.0) and expects 1.0; on the old code it got 0.

One note on verification: this Windows 10 machine's test harness reaches SM 6.5 on hardware and 6.2 on WARP (no Agility opt-in in the test binary), so the GPU-backed cases skip locally and CI runs them; local cargo check --all-targets and clippy are clean.

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Measured offline on the title's shaders (the method in #1247): head f956a66 merged onto main 63bcca4. It conflicts in two places, both "keep both": your v_cvt_f32_f64 arm next to main's new v_cvt_f32_u32 arm in lower.rs, and the tests at the end of valu_tests.rs.

The result is 0 gained, 0 lost. Exactly 5 programs change, all compute shaders, and they are the only ones on main that refuse with emit: v_cvt_f32_f64. With this PR they get past the conversion, then stop at:

emit: a whole-wave section step the simulation does not follow

I added a local diagnostic at that refusal, and it says why. The values the conversion needs come out of whole-wave sections that are 64-bit float wave reductions: v_add_f64 steps over register pairs, fed by the usual DPP / v_permlanex16 / v_readlane moves on both halves. emit/wave.rs only models 32-bit (and 16-bit) operations, so it leaves every v_add_f64 result unknown. The same programs also have wave64 f32 reductions whose results come out unknown, possibly only downstream of those.

So on this title, the conversion counts only once the whole-wave simulation can follow an f64 add over lane pairs, giving a Red over a 64-bit value that lowers to a double WaveActiveSum/prefix. That's the next step for these 5 programs, whichever of #2348 / #416 / #1262 lands. Offline counts only; no program names or data.

@Anzymanz

Copy link
Copy Markdown

From a tester working with Claude (AI-assisted, Claude Code): #2978 stacks on this PR. In this title the five programs that stop at v_cvt_f32_f64 convert a double that comes out of a whole-wave section (a WaveActiveSum of a double). With this PR alone they stop one step later, at that section. #2978 follows the section, so one of the five translates and the other four move on to a lane-mask join. In case it saves you time: on main 63bcca4 this PR conflicts in lower.rs (main split v_cvt_f32_u32 into its own arm; keep main's arm and add yours before it) and in valu_tests.rs (two tests added at the same place; keep both). #2978's branch has it resolved that way: https://github.com/Anzymanz/VIctory/commits/wave-f64-sums

@Anzymanz

Copy link
Copy Markdown

Passing on a review finding from #2978 (which stacks on this PR), since it's in this PR's code: @fuzeofficlal notes that in v_cvt_f32_f64 the Operand::Const(240..=248) and Operand::Literal(_) arms return before self.src64(...), so v.abs/v.neg (and sdwa/dpp) aren't checked for those sources. A VOP3 v_cvt_f32_f64_e64 v0, -1.0 would drop the neg silently. Refusing modifiers in those two arms (as src64_dwords does) or applying them would cover it. Details in their review on #2978. (AI-assisted, Claude Code.)

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

An addition to my note above: I missed that #2978 (stacked on this PR, linked by Anzymanz above) already models exactly that step, the binary64 sums in whole-wave sections. I measured it on main 63bcca4. #2978 alone gives +1, 0 lost. #2978 + #385 + #1065 gives +766, 0 lost, and all 5 programs that refuse with emit: v_cvt_f32_f64 on main validate. Details, including a keep-both conflict with #385 and a test that fails only on the built-in WARP, are on #2978.

@steven-seagull27

Copy link
Copy Markdown

Confirmed the source-modifier issue reported by @fuzeofficlal and @Anzymanz on f956a66. On RTX 5080 and Windows inbox WARP 10.0.26100.10068, abs of the high-word literal 0x80000000 returned f32 −0 instead of +0, and neg of inline +0.5 returned +0.5. Of 96 synthetic input/modifier combinations, 45 reached dispatch on the PR and 14 of those returned incorrect bits on each adapter. Nine modified shader forms (51 inputs across VGPR, SGPR and inline integer zero) stopped during lowering. Original review finding

Cherry-pick 5b841a2 on this PR head applies ABS then NEG to the binary64 sign bit before truncation. All 96/96 matched on each adapter, covering all four modifier combinations, both signed zeros, infinities, finite rounding boundaries, inline constants, high-word literals and register pairs. The three existing conversion tests passed too. SDK DXIL validation and cargo clippy -p victory-shader --all-targets -- -D warnings passed.

The same commit cherry-picks cleanly onto the #2978 stack. Its three wave-sum readback tests, including the full-width sparse-mask regression, passed on RTX 5080 and app-local WARP 1.0.22. The combined stack also passed xtask policy. Full-width wave test

For the outstanding main conflict, fast-forward 1790222 adds the fix and merges 63bcca43 while keeping your original commits. Its tree matches #2978’s rebased parent plus the modifier fix. I reran the modifier regression and all three existing conversion tests on this version as well.

The console's 64-bit to 32-bit conversion: the VGPR pair is bitcast to a
`double` and truncated with the host's `fptrunc`, round to nearest even,
which is MODE's default. The toward-zero 64-bit mode has no DXIL form and is
refused by name rather than mis-rounded.

The IR had no 64-bit float type: it gains `double` (bitcode type 4) and the
`fptrunc` cast (LLVM opcode 7). A module that uses it declares the doubles
feature — SFI0 0x1 and the module's feature-info bit 2 — which dxil.dll
checks against the module.

Ten cases run on the card, checked against Rust's `as f32`: 1.0, -2.5, 0.1,
a tie that rounds to even, the two smallest subnormals (0), f64 max (inf),
±inf and a NaN (payload kept). WARP tops out at SM 6.2 here, so the hardware
device is the one exercised; the DXIL is validated by dxil.dll.
…nverts

Two findings from the review (tyr0nsur):

- A literal source of `v_cvt_f32_f64` is the **high** word of the binary64
  (RDNA 2 ISA 6.2.1), the low word zero: 0xC0020000 is -2.25. `src64` reads a
  literal as the sign-extended integer its bits are, so the value became
  garbage there. The literal is handled in the lowering now, and
  `a_64_bit_literal_source_is_the_high_word` runs it on the card.
- The round-mode check admitted every mode but toward-zero: the directed
  modes (1, 2) and a mode written at run time (`None`) have no DXIL form, and
  `fptrunc` rounds nearest-even regardless. Only MODE's default (0) converts
  now; anything else is refused by name.

Lib suite 249 passed, 0 failed, 3 ignored; clippy `-D warnings` clean.
Review feedback on Marvisak#2348: `v_cvt_f32_f64` lowers an inline float constant
(operand codes 240-248) through `src64`'s fallback, which zero-extends the
**f32** bit pattern — `1.0` became 0x000000003F800000, a subnormal that
converts to 0, where the hardware uses the binary64 of the value (RDNA 2 ISA
6.2.1).

The instruction now maps 240-248 to the same values as binary64 (including
1/(2π)) and builds the bits with an integer constant; register pairs and
literals keep their paths. `a_64_bit_inline_constant_source_is_the_same_value`
converts code 242 (1.0) and expects 1.0; before this it got 0.
@Calvix2k
Calvix2k force-pushed the codex/shader-cvt-f32-f64 branch from f956a66 to fd01035 Compare November 26, 2026 13:52
@Calvix2k

Copy link
Copy Markdown
Contributor Author

Rebased on today's main. Upstream's dedicated v_cvt_f32_u32 arm is kept, u32 is dropped from the shared arm, and the v_cvt_f32_f64 arm now sits before it. Both tests are kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants