shader: implement v_cvt_f32_f64 instruction lowering #416

Open
github-bot wants to merge 1 commit from github-bot/gh-416 into main AGit
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 03:08 UTC · original
bitzin-dev:shader/v-cvt-f32-f64 → main · +198 −2 · 4 files · 1 commits · open

This PR implements lowering for the RDNA2 VOP1 opcode v_cvt_f32_f64 (0x00f) converting double-precision 64-bit float registers into 32-bit single-precision floats.

Changes

  1. Bitcode definitions (itcode.rs):
    • Added TypeDef::Double (type code 4 in LLVM bitcode).
    • Added Cast::FPTrunc (= 7) and helper Module::double().
  2. Feature flags (shader.rs):
    • Track lags::DOUBLES (1 << 2) and set SFI0 feature bit x0001 (ShaderFeatureInfo_Doubles).
  3. Lowering (lower.rs):
    • Implemented _cvt_f32_f64: read 64-bit source operand using src64, bitcast i64 -> double, truncate double -> loat using FPTrunc.
  4. Validation & tests ( alu_tests.rs):
    • Added cvt_f32_f64_matches_rdna2 testing normal floats, negative zero, pi, and edge values on host GPU / WARP.
    • Clean verification: cargo test -p victory-shader -- --test-threads=1 (70/70 passing) and cargo xtask policy clean.
**@⁠bitzin-dev** on GitHub · 2026-11-24 03:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/416) `bitzin-dev:shader/v-cvt-f32-f64` → `main` · +198 −2 · 4 files · 1 commits · open This PR implements lowering for the RDNA2 VOP1 opcode `v_cvt_f32_f64` (`0x00f`) converting double-precision 64-bit float registers into 32-bit single-precision floats. ### Changes 1. **Bitcode definitions** (itcode.rs): - Added TypeDef::Double (type code 4 in LLVM bitcode). - Added Cast::FPTrunc (= 7) and helper Module::double(). 2. **Feature flags** (shader.rs): - Track lags::DOUBLES (1 << 2) and set SFI0 feature bit x0001 (ShaderFeatureInfo_Doubles). 3. **Lowering** (lower.rs): - Implemented _cvt_f32_f64: read 64-bit source operand using src64, bitcast i64 -> double, truncate double -> loat using FPTrunc. 4. **Validation & tests** ( alu_tests.rs): - Added cvt_f32_f64_matches_rdna2 testing normal floats, negative zero, pi, and edge values on host GPU / WARP. - Clean verification: cargo test -p victory-shader -- --test-threads=1 (70/70 passing) and cargo xtask policy clean.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:03:02 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:37 UTC · original

Thanks @⁠bitzin-dev for adding v_cvt_f32_f64 lowering. I tested head 7516f947b2e5d92c42b557b90459a0ba3265132b with:

cargo test --locked -p victory-shader --lib -- --test-threads=1

Result: 70 passed, 0 failed, 0 ignored. The new cvt_f32_f64_matches_rdna2 case covers normal and edge f64 inputs, including negative zero and pi; the full suite also exercises bitcode/container generation and shader lowering. This run does not measure console hardware behavior.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7122191904) Thanks @⁠bitzin-dev for adding `v_cvt_f32_f64` lowering. I tested head `7516f947b2e5d92c42b557b90459a0ba3265132b` with: ``` cargo test --locked -p victory-shader --lib -- --test-threads=1 ``` Result: 70 passed, 0 failed, 0 ignored. The new `cvt_f32_f64_matches_rdna2` case covers normal and edge f64 inputs, including negative zero and pi; the full suite also exercises bitcode/container generation and shader lowering. This run does not measure console hardware behavior.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 05:30 UTC · original

One gap in 7516f94, from reading the code (not run): v_cvt_f32_f64 reads its source with src64 (lower.rs:3789), the 64-bit integer reader. Registers are fine, but constants aren't:

  • Float inline constants (240–248) fall through to the _ arm, which zero-extends the 32-bit float bits. For a 64-bit float operand they stand for the double: 1.0 is 0x3FF0000000000000. So v_cvt_f32_f64 v0, 1.0 reads 0x000000003F800000, a denormal double, and gives 0.0 instead of 1.0; 0.5, -2.0 and 1/(2π) likewise.
  • A 32-bit literal for a 64-bit float operand is the high dword (the low dword is 0), so the literal 0x40000000 should be 2.0. Zero-extended it's the low dword, a tiny denormal, and gives 0.0.

Compilers usually fold these, but here they're silently wrong rather than refused. Options: a double source reader that maps 240–248 to the double encodings and puts a literal in the high dword, or refuse float constants and literals for this opcode until one exists. A test with 1.0 and a literal would pin it.

AI-assisted (Claude Code), read-only review.

**@⁠upipibi** on GitHub · 2026-11-24 05:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7123009010) One gap in `7516f94`, from reading the code (not run): `v_cvt_f32_f64` reads its source with `src64` (`lower.rs:3789`), the 64-bit *integer* reader. Registers are fine, but constants aren't: - **Float inline constants (240–248)** fall through to the `_` arm, which zero-extends the 32-bit float bits. For a 64-bit float operand they stand for the double: 1.0 is `0x3FF0000000000000`. So `v_cvt_f32_f64 v0, 1.0` reads `0x000000003F800000`, a denormal double, and gives 0.0 instead of 1.0; `0.5`, `-2.0` and `1/(2π)` likewise. - **A 32-bit literal** for a 64-bit float operand is the *high* dword (the low dword is 0), so the literal `0x40000000` should be 2.0. Zero-extended it's the low dword, a tiny denormal, and gives 0.0. Compilers usually fold these, but here they're silently wrong rather than refused. Options: a double source reader that maps 240–248 to the double encodings and puts a literal in the high dword, or refuse float constants and literals for this opcode until one exists. A test with `1.0` and a literal would pin it. AI-assisted (Claude Code), read-only review.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:26 UTC · original

RDNA2 hardware measurement of v_cvt_f32_f64, as a reference for what fptrunc has to match: round to nearest even, with the f32 denormal mode deciding denormal results and NaNs quieted with their top payload bits.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 32 doubles, each under f32 denormals kept / flushed, f64 denormals kept / flushed, and round-to-nearest / toward-zero.

f64 input f32, nearest, denormals kept f32 denormals flushed toward zero
0x3ff0000010000000 (1 + 2^-24, tie) 0x3f800000 same 0x3f800000
0x3ff0000030000000 (1 + 3·2^-24, tie) 0x3f800002 same 0x3f800001
0x47effffff0000000 (just over FLT_MAX) 0x7f800000 same 0x7f7fffff
0x3800000000000000 (2^-127) 0x00400000 0x00000000 0x00400000
0x36a0000000000000 (2^-149) 0x00000001 0x00000000 0x00000001
0x36a8000000000000 (1.5·2^-149) 0x00000002 0x00000000 0x00000001
0x37a16c262777579c (1e-40) 0x000116c2 0x00000000 0x000116c2
0x7ff0000000000001 (sNaN) 0x7fc00000 same same
0x7ff4000000000000 (sNaN, payload) 0x7fe00000 same same
0x7ff8000020000000 (qNaN, payload) 0x7fc00001 same same
0xfff8000000000000 (-qNaN) 0xffc00000 same same
0x400921fb54442d18 (π) 0x40490fdb same 0x40490fda
  • In the nearest mode, all 32 inputs fit IEEE round-to-nearest-even in every denormal mode, with denormal results flushed to ±0 when the f32 mode flushes and NaN → 0x7fc00000 | sign | payload >> 29.
  • The f64 denormal mode changed nothing for these inputs (an f64 denormal is far below the f32 range anyway).

Things to check against the host's fptrunc: D3D lets the f32 result flush denormals (the first four bold rows, if the console runs with f32 denormals kept), and NaN payloads may not survive. Your test's normal values, ±0 and π are rows where the host and the hardware agree.

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.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7123940772) RDNA2 hardware measurement of `v_cvt_f32_f64`, as a reference for what `fptrunc` has to match: round to nearest even, with the f32 denormal mode deciding denormal results and NaNs quieted with their top payload bits. **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 32 doubles, each under f32 denormals kept / flushed, f64 denormals kept / flushed, and round-to-nearest / toward-zero. | f64 input | f32, nearest, denormals kept | f32 denormals flushed | toward zero | |---|---|---|---| | `0x3ff0000010000000` (1 + 2^-24, tie) | `0x3f800000` | same | `0x3f800000` | | `0x3ff0000030000000` (1 + 3·2^-24, tie) | `0x3f800002` | same | `0x3f800001` | | `0x47effffff0000000` (just over FLT_MAX) | `0x7f800000` | same | `0x7f7fffff` | | `0x3800000000000000` (2^-127) | `0x00400000` | **`0x00000000`** | `0x00400000` | | `0x36a0000000000000` (2^-149) | `0x00000001` | **`0x00000000`** | `0x00000001` | | `0x36a8000000000000` (1.5·2^-149) | `0x00000002` | **`0x00000000`** | `0x00000001` | | `0x37a16c262777579c` (1e-40) | `0x000116c2` | **`0x00000000`** | `0x000116c2` | | `0x7ff0000000000001` (sNaN) | `0x7fc00000` | same | same | | `0x7ff4000000000000` (sNaN, payload) | `0x7fe00000` | same | same | | `0x7ff8000020000000` (qNaN, payload) | `0x7fc00001` | same | same | | `0xfff8000000000000` (-qNaN) | `0xffc00000` | same | same | | `0x400921fb54442d18` (π) | `0x40490fdb` | same | `0x40490fda` | - In the nearest mode, all 32 inputs fit IEEE round-to-nearest-even in every denormal mode, with denormal results flushed to ±0 when the f32 mode flushes and NaN → `0x7fc00000 | sign | payload >> 29`. - The f64 denormal mode changed nothing for these inputs (an f64 denormal is far below the f32 range anyway). Things to check against the host's `fptrunc`: D3D lets the f32 result flush denormals (the first four bold rows, if the console runs with f32 denormals kept), and NaN payloads may not survive. Your test's normal values, ±0 and π are rows where the host and the hardware agree. 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.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, cvt_f32_f64_matches_rdna2 pass on both AMD GPUs.
  • Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
**@⁠slint6614-dev** on GitHub · 2026-11-24 10:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7128558869) AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of `run_cases` (local adapter-selection patch, not part of this PR); `cargo test -p victory-shader --release valu_tests` at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept. - With this PR, `cvt_f32_f64_matches_rdna2` pass on both AMD GPUs. - Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 13:48 UTC · original

On 7516f94, the register-pair cases pass, but I reproduced two operand decoding errors with synthetic shaders on Windows hardware. FP64 inline operand 240 (0.5) and the literal high dword 0x40080000 (3.0) both read back as 0.0.

Follow-up commit uses DXIL MakeDouble with the two source words, decodes the FP64 inline constants and high-word literals per the RDNA 2 ISA, section 6.2.1, and applies e64 source sign modifiers before conversion. Output scaling and clamp use the existing float write path.

All four focused tests passed on the hardware adapter and WARP, with 25 readbacks on each. They cover register pairs, inline constants, literals including signed zero, and e64 modifiers on finite inputs. The victory-shader suite passed all 73 tests.

**@⁠fuzeofficlal** on GitHub · 2026-11-24 13:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7133363693) On `7516f94`, the register-pair cases pass, but I reproduced two operand decoding errors with synthetic shaders on Windows hardware. FP64 inline operand `240` (`0.5`) and the literal high dword `0x40080000` (`3.0`) both read back as `0.0`. [Follow-up commit](https://github.com/fuzeofficlal/VIctory/commit/1b2d2fa7023dd8cca5a94925f76256c4ae9f9446) uses DXIL `MakeDouble` with the two source words, decodes the FP64 inline constants and high-word literals per the [RDNA 2 ISA, section 6.2.1](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content#page=50), and applies e64 source sign modifiers before conversion. Output scaling and clamp use the existing float write path. All four focused tests passed on the hardware adapter and WARP, with 25 readbacks on each. They cover register pairs, inline constants, literals including signed zero, and e64 modifiers on finite inputs. The `victory-shader` suite passed all 73 tests.
Author
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 15:34 UTC · original · edited 2026-11-24 15:39 UTC

Thanks everyone for the extensive testing, hardware traces and reviews! Special shoutout to @⁠upipibi and @⁠fuzeofficlal for pointing out and testing the FP64 inline constant & high-dword literal mappings.

Rebased on upstream main ( fe310e) and updated the PR head (16d2166):

  1. Operand decoding (double_src):
    • Decodes FP64 inline constants (240..=248 mapped to IEEE double values, including AMD's 1/(2*pi) constant ␀x3fc4_5f30_6dc9_c882).
    • Places floating-point literals in the high dword ([31:0] low dword zeroed) per RDNA2 section 6.2.1.
    • Applies e64 input sign modifiers (bs/
      eg) on the high word before conversion.
    • Constructs the double via DXIL's makeDouble.f64 (opcode 101).
  2. Tests:
    • Added cvt_f32_f64_decodes_double_inline_constants, cvt_f32_f64_uses_literal_as_the_high_word, and cvt_f32_f64_applies_e64_source_and_output_modifiers.
    • Re-verified full test suite: all 124 tests pass, zero warnings, cargo xtask policy clean.
**@⁠bitzin-dev** on GitHub · 2026-11-24 15:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7136255192) · edited 2026-11-24 15:39 UTC Thanks everyone for the extensive testing, hardware traces and reviews! Special shoutout to @⁠upipibi and @⁠fuzeofficlal for pointing out and testing the FP64 inline constant & high-dword literal mappings. Rebased on upstream main ( fe310e) and updated the PR head (16d2166): 1. **Operand decoding (double_src)**: - Decodes FP64 inline constants (240..=248 mapped to IEEE double values, including AMD's 1/(2*pi) constant ␀x3fc4_5f30_6dc9_c882). - Places floating-point literals in the high dword ([31:0] low dword zeroed) per RDNA2 section 6.2.1. - Applies e64 input sign modifiers (bs/ eg) on the high word before conversion. - Constructs the double via DXIL's makeDouble.f64 (opcode 101). 2. **Tests**: - Added cvt_f32_f64_decodes_double_inline_constants, cvt_f32_f64_uses_literal_as_the_high_word, and cvt_f32_f64_applies_e64_source_and_output_modifiers. - Re-verified full test suite: all 124 tests pass, zero warnings, cargo xtask policy clean.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:16 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/416#issuecomment-7148339714) <!-- victory-triage v1 head=16d21661cf55da0c29b3e88ea14503c9618bed2b verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-shader/src/emit/valu_tests.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/shader.rs
  • crates/victory-shader/src/emit/valu_tests.rs
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.
git fetch -u origin '+refs/pull/416/head:github-bot/gh-416'
git switch 'github-bot/gh-416'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-416'
git switch 'github-bot/gh-416'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-416'
git switch 'github-bot/gh-416'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-416'
git switch 'main'
git merge --squash 'github-bot/gh-416'
git switch 'main'
git merge --ff-only 'github-bot/gh-416'
git switch 'main'
git merge 'github-bot/gh-416'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!416
No description provided.