Shaders: read inline floats as f32 bits in 16-bit integer ops #1668

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

@⁠Calvix2k on GitHub · 2026-11-24 20:36 UTC · original
Calvix2k:codex/shader-inline-16bit-021 → main · +34 −4 · 2 files · 1 commits · open

Addresses VIC-BT-021 from audit #1242.

Summary

half_src decoded the inline float constants (240..=248) as packed f16 patterns for every 16-bit operation. Hardware only does that when the source type is F16: a 16-bit integer operation sees the 32-bit value, so 1.0 is 0x3F800000, its low half is 0 and its high half 0x3F80.

  • Added half_int_src (the 32-bit view) next to half_src (the f16 view), sharing the op_sel/SDWA half selection.
  • Routed v_cvt_f16_u16/v_cvt_f16_i16, the 16-bit arithmetic/shift/min/max ops, and the u16/i16 compares through it.
  • f16 operations (half_fsrc, v_cvt_norm_*_f16, v_cmp_class_f16) keep the f16 decoding.

Test

New hardware test integer_16bit_ops_read_inline_floats_as_f32_bits:

  • v_add_nc_u16 v0, 1.0, v1 with v1 = 5 gives 5 (low half 0), not the old 0x3C05.
  • With op_sel selecting the high half it gives 16261 (5 + 0x3F80), not 5.

Verified on a Radeon RX 7800 XT (Agility SDK 1.616 runtime for SM 6.6): before the change the same test fails with 15365 (0x3C05).

Validation

  • cargo test -p victory-shader --locked -- --test-threads=1 --skip emit::valu_tests --skip emit::waterfall_tests — passed (102 tests).
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings — passed.
  • cargo xtask policy — passed (154 files).
**@⁠Calvix2k** on GitHub · 2026-11-24 20:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668) `Calvix2k:codex/shader-inline-16bit-021` → `main` · +34 −4 · 2 files · 1 commits · open Addresses `VIC-BT-021` from [audit #1242](https://git.gtahub.st/github/Marvisak--VIctory/issues/1242). ## Summary `half_src` decoded the inline float constants (240..=248) as packed f16 patterns for every 16-bit operation. Hardware only does that when the source type is F16: a 16-bit **integer** operation sees the 32-bit value, so `1.0` is `0x3F800000`, its low half is 0 and its high half `0x3F80`. - Added `half_int_src` (the 32-bit view) next to `half_src` (the f16 view), sharing the op_sel/SDWA half selection. - Routed `v_cvt_f16_u16`/`v_cvt_f16_i16`, the 16-bit arithmetic/shift/min/max ops, and the `u16`/`i16` compares through it. - f16 operations (`half_fsrc`, `v_cvt_norm_*_f16`, `v_cmp_class_f16`) keep the f16 decoding. ## Test New hardware test `integer_16bit_ops_read_inline_floats_as_f32_bits`: - `v_add_nc_u16 v0, 1.0, v1` with `v1 = 5` gives **5** (low half 0), not the old `0x3C05`. - With `op_sel` selecting the high half it gives **16261** (`5 + 0x3F80`), not 5. Verified on a Radeon RX 7800 XT (Agility SDK 1.616 runtime for SM 6.6): before the change the same test fails with `15365` (`0x3C05`). ## Validation - `cargo test -p victory-shader --locked -- --test-threads=1 --skip emit::valu_tests --skip emit::waterfall_tests` — passed (102 tests). - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` — passed. - `cargo xtask policy` — passed (154 files).
half_src decoded the inline constants 240..=248 as packed f16 patterns for
every 16-bit operation. Hardware only uses those patterns when the source
type is F16; an integer operation sees the 32-bit value, so 1.0 is
0x3F800000 and its low half is 0 (the high half 0x3F80). Route
v_cvt_f16_u16/i16, the 16-bit add/sub/mul/shift/min/max ops, and the
u16/i16 compares through the 32-bit view, keeping f16 operations on
f16_operand.

Measured on a Radeon RX 7800 XT: v_add_nc_u16 v0, 1.0, v1 with v1 = 5
yields 5, and with op_sel selecting the high half 16261; before the change
they yielded 0x3C05 and 5.

Addresses VIC-BT-021 from audit #1242.

Validation:
- cargo test -p victory-shader --locked -- --test-threads=1
  --skip emit::valu_tests --skip emit::waterfall_tests
- the new GPU test on the RX 7800 XT (Agility SDK runtime) -- passed
- cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings
- cargo xtask policy
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:40:55 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 20:56 UTC · original

Reviewed head d027c4f locally (isolated worktree, fresh CARGO_TARGET_DIR, this machine's GPU really dispatches).

What it does: half_src is split into half_src_bits(.., f16) with two wrappers — float 16-bit ops keep f16_operand (inline constants packed as f16), while 16-bit integer ops (v_cvt_f16_u16/i16 and the u16/i16 VOP3 bin-op arm) now go through plain operand, i.e. the inline float is read as its raw 32-bit value and the op takes the low or high half: inline 1.0 is 0x3F800000 → low half 0, high half (op_sel) 0x3F80, not the f16-packed 0x3C00.

Verified locally:

  • cargo test -p victory-shader --lib integer_16bit_ops_read_inline_floats_as_f32_bits -- --nocapture — passes in 7.4s on the real GPU backend (no skip), both cases: 5 + low16(1.0) == 5 and 5 + high16(1.0) == 16261
  • cargo test -p victory-shader --lib --locked — 121 passed / 0 failed / 1 ignored
  • cargo clippy -p victory-shader --all-targets --all-features --locked — clean
  • cargo build --release -p victory --locked — ok
  • rustfmt: lower.rs has 2 new hunks vs main (at 4638 and 5398 — exactly the two edited tuple bindings; the 3 pre-existing hunks near 7113 are untouched), valu_tests.rs clean. Cosmetic only; CI has no fmt job.

Findings, ranked:

  1. Correct and consistent with the rule we verified on #1295/#1300 (16-bit integer ops read inline constants as f32 bits): this is the same hardware behavior applied at the half_src chokepoint, so the three PRs compose semantically. Expect textual conflicts in lower.rs's 16-bit arms with #1285/#1295/#1300 — same behavior, either order fine.
  2. The new valu test pins both halves (low = 0, high = 0x3F80) against real hardware, which is exactly the evidence needed; the comment in the test matches the code.
  3. Minor: the two new rustfmt hunks (tuple fits on one line) — worth a rustfmt pass on the branch, not blocking.
  4. Caveat carried from #1295: verified here on AMD (RDNA3) + WARP; if NVIDIA reads these inline constants differently, the valu test would catch it on a Blackwell run (as steven-seagull27's RTX 5080 sweep did for #1112) — worth one hardware run there before merge if available.

LGTM.

**@⁠adamdotpy** on GitHub · 2026-11-24 20:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7144254034) Reviewed head d027c4f locally (isolated worktree, fresh CARGO_TARGET_DIR, this machine's GPU really dispatches). **What it does:** `half_src` is split into `half_src_bits(.., f16)` with two wrappers — float 16-bit ops keep `f16_operand` (inline constants packed as f16), while 16-bit *integer* ops (`v_cvt_f16_u16/i16` and the `u16`/`i16` VOP3 bin-op arm) now go through plain `operand`, i.e. the inline float is read as its raw 32-bit value and the op takes the low or high half: inline `1.0` is `0x3F800000` → low half 0, high half (op_sel) `0x3F80`, not the f16-packed `0x3C00`. **Verified locally:** - `cargo test -p victory-shader --lib integer_16bit_ops_read_inline_floats_as_f32_bits -- --nocapture` — passes in 7.4s on the real GPU backend (no skip), both cases: `5 + low16(1.0) == 5` and `5 + high16(1.0) == 16261` - `cargo test -p victory-shader --lib --locked` — 121 passed / 0 failed / 1 ignored - `cargo clippy -p victory-shader --all-targets --all-features --locked` — clean - `cargo build --release -p victory --locked` — ok - rustfmt: `lower.rs` has 2 new hunks vs main (at 4638 and 5398 — exactly the two edited tuple bindings; the 3 pre-existing hunks near 7113 are untouched), `valu_tests.rs` clean. Cosmetic only; CI has no fmt job. **Findings, ranked:** 1. Correct and consistent with the rule we verified on #1295/#1300 (16-bit integer ops read inline constants as f32 bits): this is the same hardware behavior applied at the `half_src` chokepoint, so the three PRs compose semantically. Expect textual conflicts in `lower.rs`'s 16-bit arms with #1285/#1295/#1300 — same behavior, either order fine. 2. The new valu test pins both halves (low = 0, high = 0x3F80) against real hardware, which is exactly the evidence needed; the comment in the test matches the code. 3. Minor: the two new rustfmt hunks (tuple fits on one line) — worth a `rustfmt` pass on the branch, not blocking. 4. Caveat carried from #1295: verified here on AMD (RDNA3) + WARP; if NVIDIA reads these inline constants differently, the valu test would catch it on a Blackwell run (as steven-seagull27's RTX 5080 sweep did for #1112) — worth one hardware run there before merge if available. LGTM.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-24 22:17 UTC · original

Rebased onto 70564ae. The lowering now keeps upstream's low16_src value path and only changes the fallback: f16 operations use f16_operand, integer operations use the 32-bit operand. The call sites were already split into half_src / half_int_src. Test-module conflict resolved by keeping upstream's new helpers and this PR's regression. cargo check -p victory-shader --tests passes; the hardware test was verified on the RX 7800 XT before the rebase.

**@⁠Calvix2k** on GitHub · 2026-11-24 22:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7146038320) Rebased onto 70564ae. The lowering now keeps upstream's `low16_src` value path and only changes the fallback: f16 operations use `f16_operand`, integer operations use the 32-bit `operand`. The call sites were already split into `half_src` / `half_int_src`. Test-module conflict resolved by keeping upstream's new helpers and this PR's regression. `cargo check -p victory-shader --tests` passes; the hardware test was verified on the RX 7800 XT before the rebase.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:32 UTC · original · edited 2026-11-25 22:17 UTC

Automated triage for the maintainers.

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

From the earlier needs-work note, still open: a source (ISA section, LLVM commit rationale, or the title's own shaders) for the op_sel high-half reading of inline floats in 16-bit integer ops, or limit the PR to the low-half case.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7152015542) · edited 2026-11-25 22:17 UTC <!-- victory-triage v1 head=33101a2f3280f4e7cee39f704f67bb9d5800dee8 verdict=needs-rebase at=2026-11-25T22:20:30Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflict in `crates/victory-shader/src/emit/valu_tests.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: a source (ISA section, LLVM commit rationale, or the title's own shaders) for the op_sel high-half reading of inline floats in 16-bit integer ops, or limit the PR to the low-half case. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 08:38 UTC · original

I looked for the source the triage asks for and found a note that points the other way, so I would rather ask than cite it as support:

  • LLVM's operand syntax says floating-point inline constants cannot be used with 16-bit integer operands; the assembler encodes them as 32-bit literals (AMDGPUOperandSyntax, "Floating-point inline constants cannot be used with 16-bit integer operands").
  • AMDGPUAsmParser.cpp has the truncation rule: "When floating-point immediate is used as operand of type i16, the 32-bit representation of the constant truncated to the 16 LSBs should be used."
  • LLVM PR #76522 ("AMDGPU: Fix packed 16-bit inline constants") treats packed 16-bit operands as 32-bit values, "because that's really what they are".

That last point supports the packed path (packed_src) this change follows, but for the non-packed 16-bit integer ops the documented hardware rule is "low 16 bits of the 32-bit literal", not "the f32 bits as an integer". Those differ: 1.0f is 0x3F800000; its low half is 0, while as an i32 it is 1065353216.

If the measured behaviour on the console really is the f32 bits (as the title's constants would suggest), the place to say so is a hardware row like the ones in the AnyPS5 debt page, not the assembler documentation — and I do not have one. Can you point me at the measurement or the ISA section you had in mind? With that I will put it in the PR and the change is ready; without it I would rather keep the old reading than switch to one that contradicts LLVM's documented encoding.

**@⁠Calvix2k** on GitHub · 2026-11-25 08:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7157292141) I looked for the source the triage asks for and found a note that points the other way, so I would rather ask than cite it as support: - LLVM's operand syntax says floating-point inline constants cannot be used with 16-bit integer operands; the assembler encodes them as 32-bit literals (`AMDGPUOperandSyntax`, "Floating-point inline constants cannot be used with 16-bit integer operands"). - `AMDGPUAsmParser.cpp` has the truncation rule: "When floating-point immediate is used as operand of type i16, the 32-bit representation of the constant truncated to the 16 LSBs should be used." - LLVM PR #76522 ("AMDGPU: Fix packed 16-bit inline constants") treats packed 16-bit operands as 32-bit values, "because that's really what they are". That last point supports the packed path (`packed_src`) this change follows, but for the *non-packed* 16-bit integer ops the documented hardware rule is "low 16 bits of the 32-bit literal", not "the f32 bits as an integer". Those differ: `1.0f` is `0x3F800000`; its low half is 0, while as an i32 it is 1065353216. If the measured behaviour on the console really is the f32 bits (as the title's constants would suggest), the place to say so is a hardware row like the ones in the AnyPS5 debt page, not the assembler documentation — and I do not have one. Can you point me at the measurement or the ISA section you had in mind? With that I will put it in the PR and the change is ready; without it I would rather keep the old reading than switch to one that contradicts LLVM's documented encoding.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 10:15 UTC · original

Hardware run on an RX 7800 XT (RDNA3), Windows 10 19045 with SM 6.6 via the Agility runtime for the test binaries (#486): victory-shader lib suite 248 passed, 0 failed, 3 ignored, including the 16-bit integer ops and inline-constant cases this PR adds. The lowering runs on the card here.

**@⁠Calvix2k** on GitHub · 2026-11-25 10:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7159446468) Hardware run on an **RX 7800 XT (RDNA3), Windows 10 19045** with SM 6.6 via the Agility runtime for the test binaries (#486): `victory-shader` lib suite **248 passed, 0 failed, 3 ignored**, including the 16-bit integer ops and inline-constant cases this PR adds. The lowering runs on the card here.
github-bot force-pushed github-bot/gh-1668 from 33101a2f32 to 305c296c9c 2026-11-26 13:53:24 +00:00 Compare
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 21:42 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 → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows.

16-bit integer ops now read inline floats as f32 bits (half_int_src/half_src_bits): integer_16bit_ops_read_inline_floats_as_f32_bits pins the reinterpretation.

**@⁠catminou4** on GitHub · 2026-11-24 21:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1668#pullrequestreview-6047880552) · **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 → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows. 16-bit integer ops now read inline floats as f32 bits (half_int_src/half_src_bits): integer_16bit_ops_read_inline_floats_as_f32_bits pins the reinterpretation.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1668/head:github-bot/gh-1668'
git switch 'github-bot/gh-1668'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1668'
git switch 'github-bot/gh-1668'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1668'
git switch 'github-bot/gh-1668'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1668'
git switch 'main'
git merge --squash 'github-bot/gh-1668'
git switch 'main'
git merge --ff-only 'github-bot/gh-1668'
git switch 'main'
git merge 'github-bot/gh-1668'
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!1668
No description provided.