Skip to content

Translator: cheaper f16 rounding and a single MODE pass, same bits (SHADER-594) - #3200

Closed
tibzejoker wants to merge 3 commits into
ItsNotRexed:mainfrom
tibzejoker:translator/f16-narrowing-cost
Closed

tibzejoker wants to merge 3 commits into
ItsNotRexed:mainfrom
tibzejoker:translator/f16-narrowing-cost

Conversation

@tibzejoker

@tibzejoker tibzejoker commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-594 (claim)

Follow-up to #2731, which is merged (6e3426d6). Rebased on f847748c: only these three commits are left (only docs/design.md conflicted).

What and why

Makes the integer f16 narrowing (Gen.f16_bits, SHADER-56) cheaper for every f16 result, with the same bits.

  1. Subnormal rounding with a bias. Add half - 1 + the kept low bit, then shift, as the normal range already does, instead of the remainder compares and selects. The underflow select goes: below 2^-25 the shift is 25, which already gives 0.
  2. One pass when the shader writes MODE. Gen.f16_res emitted two full conversions and selected one. It now emits one, where the mode selects the bias and the overflow limit and value.

SPIR-V instructions added per RDNA instruction after spirv-opt -O (hand-written compute probes, inputs from a buffer, 40 copies minus 8):

Instruction main #2731 this PR
v_add_f16 47 31 31
v_add_f16 div:2 48 46 39
v_pk_add_f16 90 58 58
v_mul_f16 47 45 38
v_fma_f16 66 64 57
v_add_f16, run-time MODE 71 55 49
v_add_f16 div:2, run-time MODE 74 73 60
v_pk_add_f16, run-time MODE 140 108 96
v_mul_f16, run-time MODE 56 55 42
v_fma_f16, run-time MODE 75 74 61

For the MODE rows the stored mode was replaced by a user-data word, so the optimizer can't fold it.

Tested

Windows 11, rerun on f847748c + these commits.

  • Exhaustive equivalence with main: the emitted integer sequences, before and after, were turned into C and run on all 2^32 f32 inputs and all 2^32 f16 operand pairs, through both f16_bits and f16_res with a run-time mode (both roundings, sums with and without clamp): 0 differences. Nearest even also equals x86 F16C. The negative control (sum shortcut under div:2) gives 4,196,352 mismatches.

  • python -m unittest discover -s tests/semantic: 7,322 pass, 1 expected failure (same as main).

  • The f16, MODE, d16, FMA, translate, format, buffer, decode and docs test files under pytest: 417 passed, 577 skipped (including tests that need an amdgcn llvm-mc), 0 failed (same as main).

  • The probe modules pass spirv-val.

  • RTX 4090 (621.01), 4.19M threads x 800 f16 add/mul in a hand-written compute shader, timed in VRAM: 5.83 ms on 16282101, 4.35 ms with Translator: smaller f16 narrowing, same bits (SHADER-489) #2731 and this PR (0.075 ms without the ops).

Not run by me: the GPU test suite and the game's shader corpus (so no before/after translated and valid counts). Independent RTX 5090 and Steam Deck runs on the pre-rebase head are in the comments.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 9acdaa1f0edbc1b70b9908cbbad61c16dc8359b8 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent Linux Vulkan check of 9acdaa1f on an RTX 5090 (595.99.02, subgroup 32). I compared the PR translator with the stacked #2731 revision 8d19c775 by reverting only the #3200 translator changes and keeping the same fixtures and harness.

All 18 synthetic compute words matched exact expectations in both runs, across nearest-even, toward zero and a switch back to nearest-even in one dispatch. The cases covered the tie at half of the smallest subnormal, the signed result just above it, the subnormal-to-normal boundary, overflow, and an ordinary rounding case. All 46 fill words stayed intact. The stock ps_outmods_f16 fixture also matched 4,096/4,096 pixels on both revisions. The modules passed Vulkan 1.2 SPIR-V validation, and all 33 focused CPU tests passed on the PR head. Synthetic coverage only. AI-assisted.

AI-assisted verification.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested 9acdaa1 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

Semantic tests:

  • python3 tests/semantic/test_f16_sum_narrowing.py — 5/5 OK

Full GPU suite:

  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 175/189 passed, 14 skipped, 0 failed. Skips are the expected D3D12-owned skips on this Deck.

Note: tested PR head as submitted (PR description lists dependency on #2731).

AI-assisted (OpenCode).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 9acdaa1 merged onto main 1628210 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 143/189 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 194 test shaders translate and pass spirv-val.
  • tests/semantic/test_f16_sum_narrowing.py: ran 5 tests in 13.621s; OK.

AI-assisted (Claude Code).

tibzejoker and others added 2 commits November 22, 2026 21:18
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tibzejoker
tibzejoker force-pushed the translator/f16-narrowing-cost branch from 9acdaa1 to d07396a Compare November 22, 2026 20:42
@Koro4e

Koro4e commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

I took a closer look at the exact_tiny scope in tools/rdna2spirv.py. No correctness issue found; not requesting changes.

The restriction looks right: a sum/sub of two f16 values is a multiple of 2^-24, so below 2^-14 it is already an f16 subnormal and the rounding bias can be skipped. v_pk_mul_f16, v_pk_fma_f16 and scalar mul can leave values off that grid, so excluding them is necessary. v_pk_min_f16/v_pk_max_f16 and scalar min/max could in principle use the same shortcut because the result is one of the operands, but leaving them on the full path is conservative and fine.

I also modelled the old and new integer sequences directly: 2,000,000 random f32 bit patterns plus edge values, and 300,000 f16 operand pairs with their exact f32 sums, comparing nearest-even and toward-zero in both. All matched. The full semantic suite on 9acdaa1f is 4142 tests OK, 3 skipped.

Two non-blocking follow-ups:

  1. Gen.emit_ne0() (tools/rdna2spirv.py:759 in this head) became unused when f13ca4c replaced the compare-based subnormal rounding; it can be dropped in a cleanup commit.
  2. tests/semantic/test_f16_sum_narrowing.py currently exercises only nearest-even. Since cbdf749 makes the MODE path a single pass with is_z-selected bias/limit, a small MODE-toward-zero variant (and a packed-add case with a nonzero high half) would pin those selects in-tree. The existing GPU Tested comments already cover MODE switching, so this is about local regression coverage only.

Thanks - the single-pass narrowing is a nice reduction.

@tibzejoker

Copy link
Copy Markdown
Contributor Author

Merged to main in 0f1bfb3 (the pre-rebase head 9acdaa1; the branch was rebased after the local merge, so GitHub didn't mark it). The docs row update goes with SHADER-673.

@tibzejoker tibzejoker closed this Nov 22, 2026
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
# Conflicts:
#	docs/design.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants