Skip to content

shader: lower v_ldexp_f16 in the direct compute path - #3123

Merged
pigalot merged 3 commits into
R0Y00:mainfrom
R0D4X:feat/native-ldexp-f16
Nov 28, 2026
Merged

pigalot merged 3 commits into
R0Y00:mainfrom
R0D4X:feat/native-ldexp-f16

Conversation

@R0D4X

@R0D4X R0D4X commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #3122. Refs #2935.

What this changes

  • Lowers v_ldexp_f16 (VOP2 0x3b / VOP3A 0x13b) in src/gfx1013_direct_compute.inl using exact 32-bit integer IEEE-754 binary16 scaling while preserving D[31:16].
  • Sign-extends S1[15:0] and clamps the exponent delta to [-64, 64], normalizes denormal inputs without flushing, rounds subnormal underflows to nearest-even, preserves ±0.0h, ±Inf, and NaN, supports VOP3A abs/neg on src0 and clamp, and refuses unsupported modifiers (omod, neg/abs on src1, op_sel, unused src2).

Tests & docs added

  • tests/native_ldexp_f16_check.py & tests/cmake/native_ldexp_f16.cmake:
    • native_ldexp_f16_controls: validates emitted integer DXIL (shl i32 / lshr i32), refuses invalid modifiers, and verifies that the flush_denormals, truncate_subnormal, no_clamp, and zero_high negative controls each differ.
    • native_ldexp_f16_hardware: dispatches 8 binary16 scaling cases (normals, negative i16 exponent, denormal promotion, subnormal round-up and even/odd ties, overflow to +Inf/clamp) across VOP2 and VOP3A with pre-seeded D[31:16], matching 2048 result words and 16 sentinel guards.
  • docs/experiments/native-ldexp-f16.md: experiment note and reproduction commands.

Verification

  • Commit: 520ac7d1
  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2124).
  • ctest --preset dev --no-tests=error -R '^native_ldexp_' -V: 4/4 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_ldexp_': 4/4 passed (0 skipped).
  • tests/docs_links_check.py & tests/bidi_controls_check.py: passed.

Scope: covers synthetic direct-DXIL compute fixtures for v_ldexp_f16 only; does not establish full-game execution.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 0594e43 (the change from 520ac7d, updated with main). Comment only.

The scaling, the denormal normalization, the round-to-nearest-even subnormal path and the overflow to Inf read correctly to me, and the CPU reference is independent of the lowering.

One thing should change before merge: inline float constants. operand32(i, i.src[0]) returns the f32 bit pattern for sources 240-248, and the lowering then keeps only the low 16 bits. For 0.5, 1.0, 2.0, 4.0 and their negatives the low half of the f32 pattern is 0, so v_ldexp_f16 v4, 1.0, v3 scales +0 instead of 1.0h; 1/(2*pi) gives an unrelated value. The v_add_f16 block in the same file maps these sources through halves[] (binary16 roundings). Could you do the same here, or refuse 240-248, and add one inline-constant source to the fixture? The same pattern was raised on #3135, and #3120 (already merged) has it too, so one follow-up could cover all of them.

Windows Server 2025 (Build 26100, MSVC 14.44.35207, D3D12 WARP) at 0594e433, for information:

  • dev: ctest --preset dev -R <native sweep> after building compile_dxil native_store_replay: 100% tests passed out of 134
  • release: same with --preset release: 100% tests passed out of 134
  • <native sweep> is every native_* test except native_startup_request_cache_hardware (fails on WARP-only hosts with or without this PR). The _hardware legs skip on WARP.

AI-assisted; commands listed were executed locally.

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

Correct lowering of v_ldexp_f16 using exact integer-based IEEE-754 binary16 scaling. The denormal normalization, exponent clamping to [-64, 64], round-to-nearest-even subnormal underflow, and overflow handling are all precise. VOP3A abs/neg/clamp support is correct and unsupported modifiers (omod, neg/abs on src1, op_sel) are properly refused. Test suite covers denormal promotion, negative exponents, tie cases, and negative controls. Approved.

@KelboLive

Copy link
Copy Markdown
Contributor

This PR shares the same inline float constant bug that tedin7 already flagged on this PR: operand32(i, i.src[0]) returns f32 bit patterns for src 240-248 (e.g., 0x3f800000 for 1.0), then the lowering keeps only the low 16 bits (& 65535). For 0.5, 1.0, 2.0, 4.0 and their negatives that low half is 0, so v_ldexp_f16 v4, 1.0, v3 scales +0 instead of 1.0h. The fix: map sources 240-248 through a halves[] array like the v_add_f16 block does (line ~1207-1212), or refuse them. Please add an inline-constant case to the fixture.

Missing graphics guard. The v_add_f16 block has if (graphics_input) return false;. Your new block lacks this, so direct compute lowering now applies to vertex/fragment shaders too. Add the same guard unless that's intentional.

CMake guard. Same issue as #3125/#3135: uses X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON instead of TARGET compile_dxil AND TARGET native_store_replay AND EXISTS .... Please match the pattern used by other native fragments.

NaN/Inf test coverage. The reference correctly handles NaN and Inf (exp == 0x1F or (bits & 0x7FFF) == 0). But no source in CASES has exponent=0x1F with zero mantissa (+Inf/-Inf), which should pass through unchanged. Please add ±Inf to the fixture.

VOP2 abs/neg not tested. The lowering supports i.abs & 1 and i.neg & 1, but the test program only runs VOP3A forms (with neg/abs on src0). Either test VOP2 with abs/neg or narrow the description.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the ldexp lowering. I reviewed 0594e433, the current head, and the earlier review points aren't on the branch yet. One thing needs fixing before this can merge, and three more changes would make the tests pin what the description claims:

  1. Inline float constants read as zero. The block uses operand32(i, i.src[0]) & 65535, but for sources 240-248 constant32 returns the f32 pattern (inline_float_words, 0x3f800000 for 1.0). So v_ldexp_f16 v4, 1.0, v3 scales +0h, and 1/(2π) scales 0xf983. Main's v_fma_f16 block maps these through halves[] (0x3800, 0xb800, 0x3c00, ...). Either that or refusing 240-248 works, plus one inline-constant source (every src0 is now v2, set by v_mov_lit).
  2. FP16 MODE isn't checked. The file's contract says "Binary16 arithmetic is lowered only while MODE stays at the contract this path implements", and main's rounding f16 lowerings refuse on !half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu) (7 sites). ldexp rounds subnormal results to nearest-even and keeps denormals, so a program that sets f16 round-toward-zero or denormal flush would silently get different results. The same refusal, plus adding the op to the half_binary scan, would close this.
  3. The pass-through and the delta clamp have no cases. CASES has no ±0, ±Inf or NaN source, and the largest |delta| is 10. Dropping result = select(keep, bits, result); would turn a NaN input into ±Inf or a finite value, and every case would still pass, the hardware leg included. Adding ±0, ±Inf, a qNaN and deltas such as ±100 and 0x8000 would pin what the description says the lowering preserves.
  4. controls doesn't check results. It asserts only that 'shl i32 ' and 'lshr i32 ' appear in the dump, and the reference() != reference(...) lines test only the Python model. Values are checked only by the hardware leg, which skips on WARP and isn't in CI's pr preset (69 tests, none of them native_ldexp).

Minor, and consistent with some existing blocks: there's no graphics_input guard, and the CMake guard doesn't test TARGET compile_dxil. The VOP2 abs/neg request doesn't apply, since VOP2 has no abs/neg fields and DPP and SDWA are refused.

On the conflict: git merge-tree against main bbf4687b reports one file and one hunk. Main replaced the if (n == "v_add_f16" || ...) line that follows your block with if (binary16_alu(n) || binary16_unary(n)) {. Your block still goes in unchanged right before it, giving +46/−0 against main (and +75/−0 together with #3125's block), so the rebase should be mechanical.

This was a static review: code reading plus git merge-tree. I didn't build or run anything.

This comment was AI-assisted.

@R0D4X

R0D4X commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main (3f780716) and addressed all review points:

  • Mapped inline float constants 240..248 for src0 through packed_f16_inline_constant (halves[]) instead of taking the low 16 bits of the f32 word, and added a VOP2 inline 1.0 (src0=242) test case.
  • Added if (graphics_input) return false;, the FP16 MODE guard (!half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu)), v_ldexp_f16 in the half_binary scan, and a s_round_mode refusal test.
  • Added a second 8-case batch covering ±0.0h, ±Inf, qNaN, ±100, and INT16_MIN (0x8000) deltas, plus drop_keep and inline_as_zero negative controls.
  • Tightened the CMake guard to TARGET compile_dxil AND TARGET native_store_replay AND EXISTS ${INTAKE_PYTHON}.

Verified on NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Windows 11 (10.0.26300.9974), MSVC 19.44.35230, SDK DXC 10.0.26100.0:

  • ctest --preset dev --no-tests=error -R '^native_ldexp_|^native_f16_alu': 6/6 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_ldexp_|^native_f16_alu': 6/6 passed (0 skipped).

Ready for re-review!

Commit: 7a971b3

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retested 7a971b36 integrated over main 32dc3d7c (tree 7b0ce75a). The merge is clean and git diff --check is clean.

Earlier points. tedin7's inline-constant finding is addressed: src0 240..248 now goes through packed_f16_inline_constant, and batch 1 covers it. The graphics_input guard, the MODE gate, the half_binary scan entry and the CMake guard are in. The delta cases cover ±0, ±Inf, qNaN, ±100 and INT16_MIN.

What I traced. I read the new v_ldexp_f16 block against the RDNA ISA description:

  • delta is the sign-extended low 16 bits of src1, clamped to ±64;
  • denormal inputs are normalised before scaling;
  • subnormal results round to nearest even;
  • e > 30 overflows to Inf;
  • Inf, NaN and ±0 pass through;
  • clamp sends NaN and negative results to 0 and caps at 1.0;
  • the high half of D is kept.

The wide form refuses op_sel, src1/src2 modifiers and src2, and the block refuses subword, DPP, permuted and scaled forms.

What I ran. I built in dev and release with the pinned SPIR-V tools set, then ran:

ctest --test-dir out/build/<preset> --no-tests=error -R '^(native_ldexp_f16_controls|native_f16_alu_.*|native_.*_(controls|ir))$' -E '_hardware$'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^(native_ldexp_f16_hardware|native_f16_alu_hardware)$'

The _hardware run went through the GPU lock. Results in Debug and Release /O2, with no skips:

  • 97 non-hardware tests pass.
  • Both hardware tests pass on the RTX 5090. native_ldexp_f16_hardware prints batch=0/1: 2048 result words (high halves kept) and 16 guards matched.

Control and mutants. Each mutant changes src/gfx1013_direct_compute.inl, then runs native_ldexp_f16_controls and native_ldexp_f16_hardware:

Change Result
source back to main (control) killed: controls test, "direct DXIL lowering for v_ldexp_f16" missing
inline src0 read as the low half of the f32 word killed: batch 1 word 9, beef0000 vs beef5000
drop the Inf/NaN/zero pass-through killed: batch 1 word 8
subnormal ties round up killed: batch 0 word 28
denormal exponent off by one killed: batch 0 word 16
delta clamp ±20 instead of ±64 killed: batch 1 word 32
high half of D zeroed killed: batch 0 word 8
clamp lets NaN through killed: batch 1 word 27
drop the MODE gate inside the ldexp block survived

Restored afterwards; the worktree matches the trial merge.

Non-blocking.

  • Dropping the MODE gate inside the new block doesn't fail anything. The bad_mode refusal (s_round_mode 0xC) in the controls test still passes, so something earlier refuses that program first. I didn't trace which. Either the in-block gate is redundant, or the control needs a case that only that gate catches.
  • There is no _ir mode. The value semantics are pinned only by the hardware test, so they run only on wave32 GPUs. CI's controls test checks the DXIL for shl, lshr and 15360 only.
  • The "negative controls" in tests/native_ldexp_f16_check.py compare the Python reference with itself. They don't exercise the lowering.
  • No signalling-NaN input. A qNaN case is present.

Verdict: approve on 7a971b36.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; SDK 10.0.28000.0 DXC; wave32; Python 3.12.10.
CI: Build and test on Windows passes on 7a971b36.

This reply was AI-assisted.

@pigalot
pigalot merged commit 3926a28 into R0Y00:main Nov 28, 2026
5 checks passed
@R0D4X
R0D4X deleted the feat/native-ldexp-f16 branch November 28, 2026 10:12
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.

shader: lower v_ldexp_f16 in the direct compute path

5 participants