Skip to content

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

Open
Calvix2k wants to merge 1 commit into
Marvisak:mainfrom
Calvix2k:codex/shader-inline-16bit-021
Open

Calvix2k wants to merge 1 commit into
Marvisak:mainfrom
Calvix2k:codex/shader-inline-16bit-021

Conversation

@Calvix2k

Copy link
Copy Markdown
Contributor

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).

@adamdotpy

Copy link
Copy Markdown

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 Shaders: decode 16-bit integer inline float constants as f32 bits #1295/Shaders: lower v_{min3,max3,med3}_{i16,u16} and v_mad_{u32_u16,i32_i16} #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 Shaders: fix 16-bit VALU SDWA placement/sext and inline float constants #1285/Shaders: decode 16-bit integer inline float constants as f32 bits #1295/Shaders: lower v_{min3,max3,med3}_{i16,u16} and v_mad_{u32_u16,i32_i16} #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 Shaders: decode 16-bit integer inline float constants as f32 bits #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 Shader tests: verify every normalized MTBUF field without Int64ShaderOps #1112) — worth one hardware run there before merge if available.

LGTM.

@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 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

Copy link
Copy Markdown
Contributor Author

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
Calvix2k force-pushed the codex/shader-inline-16bit-021 branch 2 times, most recently from c4d626b to 33101a2 Compare November 25, 2026 02:25

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 (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

@Calvix2k

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

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.

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 Marvisak#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
@Calvix2k
Calvix2k force-pushed the codex/shader-inline-16bit-021 branch from 33101a2 to 305c296 Compare November 26, 2026 13:52
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.

4 participants