Skip to content

Shaders: inline float constants as f16 in v_fma_mix and v_pack_b32_f16 - #78

Open
tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-fma-mix-f16
Open

tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-fma-mix-f16

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What

v_fma_mix_f32, v_fma_mixlo_f16, v_fma_mixhi_f16 (for sources with op_sel_hi set) and v_pack_b32_f16 read their f16 sources through operand. For an inline float constant (codes 240..=248: 0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2 pi)) that gives the f32 bits, e.g. 0x3F800000 for 1.0, whose low 16 bits are 0, so the shader saw 0.0 instead of 1.0. half_src and packed_src already used f16 values for these.

This adds f16_operand (the f16 bits in both halves, so op_sel works for either half) and uses it on those paths. The constant table now lives in one inline_f16 that half_src and packed_src share (no behavior change there). op_sel, neg and abs handling is untouched: they act on the value after it is read, and were right.

Why

Mixed-precision FMAs and packs with an inline constant such as v_fma_mix_f32 v0, v1, 1.0, v2 op_sel_hi:[1,1,1] computed with a 0.0 operand.

Tested

Linux x86_64 only, in the worktree:

  • cargo test -j4 -p victory-shader: 52 passed, 0 failed
  • cargo clippy -j4 -p victory-shader --tests: no new warnings (one existing dead-code warning for constant_color in emit/mod.rs)
  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs: clean
  • New test inline_float_constants_mean_the_same_in_f16: with inline_f16 temporarily returning the low half of resource::inline (the old value) it fails (left 0, right 0x3C00); with the fix it passes.

Not verified: lowering a whole shader (there is no unit-test harness for Lower, so the test covers the constant helper, not the call sites), the Windows runtime, and any GPU.

These read f16 sources with the f32 bits of an inline constant, whose low half is 0 (1.0 became 0.0). Use the f16 value, as half_src and packed_src do.
@DaviTostes

Copy link
Copy Markdown

Tested e6faad2 stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

AI-assisted (DeepSeek V4.1 Flash coding agent).

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement: the low half is right (the constant is its f16 value), but the high half of an inline float constant is 0 on the hardware, not the f16 value again. So op_sel on such an operand reads 0.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs each of the nine inline float constants (0.5, -0.5, 1, -1, 2, -2, 4, -4, 1/(2π)) as src1, with v4 = f32 3.0 and v5 = f16 2.0 in both halves. RDNA2 desktop silicon, not the console's. No game data.

Instruction (src1 = 1.0) Result Reading
v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,1,0] 0x40400000 (3.0) low half = f16 1.0
v_fma_mix_f32 v, v4, 1.0, 0 op_sel:[0,1,0] op_sel_hi:[0,1,0] 0x00000000 (0.0) high half = 0
v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,0,0] 0x40400000 (3.0) as f32 1.0
v_fma_mixlo_f16 v, v5, 1.0, 0 op_sel_hi:[1,1,0] 0x4000 2.0 × 1.0
v_pack_b32_f16 v, 1.0, v5 0x40003c00 low half = 0x3c00
v_pack_b32_f16 v, v5, 1.0 op_sel:[0,1] 0x00004000 high half = 0
v_pk_add_f16 v, v5, 1.0 (default op_sel_hi:[1,1]) 0x40004200 low 2+1, high 2+0
v_pk_add_f16 v, v5, 1.0 op_sel_hi:[1,0] 0x42004200 both halves 2+1

The same holds for all nine constants. 1/(2π) is 0x3118 as f16, so the f16 value is used, not a truncation of the f32.

So an inline float constant used as a 16-bit operand is {0x0000, f16}: the f16 value in bits 15:0 and 0 in bits 31:16. This is why compilers clear op_sel_hi to broadcast a constant into both halves of a packed op.

For this PR: f16_operand puts the f16 bits in both halves, so op_sel / op_sel_hi = 1 on a constant reads the f16 value where the hardware reads 0. half_src already does it the hardware's way (h, high bits 0). main's packed_src has the same both-halves behaviour (// shortcut: taken to fill both halves). #637 replaces that with an f16_operand that puts the constant in the low half only, and I measured its three expected values on this hardware too. Building on #637's helper would keep the two in step.

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.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

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

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


Generated by Claude Code

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