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

Open
Calvix2k wants to merge 1 commit from upstream-pr/1668 into main
Calvix2k commented 2026-11-24 20:36:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-24T20:36:14Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6590910021 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-24T20:36:14Z. --- Addresses `VIC-BT-021` from [audit #1242](https://github.com/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).
adamdotpy commented 2026-11-24 21:03:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T20:56:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144254034 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7144254034). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T20:56:10Z. --- 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.
catminou4 commented 2026-11-24 22:01:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:42:41Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6047880552 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#pullrequestreview-6047880552). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T21:42:41Z. **Review:** APPROVED --- 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.
Calvix2k commented 2026-11-24 23:05:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-24T22:17:04Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146038320 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7146038320). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-24T22:17:04Z. --- 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.
Marvisak commented 2026-11-25 04:06:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:32:50Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152015542 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7152015542). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T03:32:50Z. --- <!-- 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)_
Calvix2k commented 2026-11-25 09:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T08:38:39Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157292141 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7157292141). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T08:38:39Z. --- 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 commented 2026-11-25 11:05:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T10:15:27Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159446468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1668#issuecomment-7159446468). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T10:15:27Z. --- 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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1668:upstream-pr/1668'
git switch 'upstream-pr/1668'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/1668'
git switch 'upstream-pr/1668'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1668'
git switch 'upstream-pr/1668'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1668'
git switch 'main'
git merge --squash 'upstream-pr/1668'
git switch 'main'
git merge --ff-only 'upstream-pr/1668'
git switch 'main'
git merge 'upstream-pr/1668'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1667
No description provided.