Shaders: inline float constants as f16 in v_fma_mix and v_pack_b32_f16 #78
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/78"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by tedin7 on 2026-11-24T00:16:29Z.
What
v_fma_mix_f32,v_fma_mixlo_f16,v_fma_mixhi_f16(for sources with op_sel_hi set) andv_pack_b32_f16read their f16 sources throughoperand. 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_srcandpacked_srcalready 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 oneinline_f16thathalf_srcandpacked_srcshare (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 failedcargo clippy -j4 -p victory-shader --tests: no new warnings (one existing dead-code warning forconstant_colorinemit/mod.rs)rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs: cleaninline_float_constants_mean_the_same_in_f16: withinline_f16temporarily returning the low half ofresource::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.Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:23:54Z.
Tested
e6faad2stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.cargo test --workspace: 156 passed, 0 failed, 3 ignored; merge with #39 clean.#[test]s for the f16 inline-constant forms, included in that run.AI-assisted (DeepSeek V4.1 Flash coding agent).
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:06:54Z.
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_selon 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.
1.0)v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,1,0]0x40400000(3.0)v_fma_mix_f32 v, v4, 1.0, 0 op_sel:[0,1,0] op_sel_hi:[0,1,0]0x00000000(0.0)v_fma_mix_f32 v, v4, 1.0, 0 op_sel_hi:[0,0,0]0x40400000(3.0)v_fma_mixlo_f16 v, v5, 1.0, 0 op_sel_hi:[1,1,0]0x4000v_pack_b32_f16 v, 1.0, v50x40003c000x3c00v_pack_b32_f16 v, v5, 1.0 op_sel:[0,1]0x00004000v_pk_add_f16 v, v5, 1.0(defaultop_sel_hi:[1,1])0x40004200v_pk_add_f16 v, v5, 1.0 op_sel_hi:[1,0]0x42004200The same holds for all nine constants. 1/(2π) is
0x3118as 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 clearop_sel_hito broadcast a constant into both halves of a packed op.For this PR:
f16_operandputs the f16 bits in both halves, soop_sel/op_sel_hi= 1 on a constant reads the f16 value where the hardware reads 0.half_srcalready does it the hardware's way (h, high bits 0).main'spacked_srchas the same both-halves behaviour (// shortcut: taken to fill both halves). #637 replaces that with anf16_operandthat 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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:15:38Z.
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
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.