Skip to content

shader: read inline float sources as binary16 in v_frexp_mant_f16 and v_frexp_exp_i16_f16 - #3147

Merged
tedin7 merged 2 commits into
R0Y00:mainfrom
JohnConnorNPC:fix/frexp-f16-inline-const
Nov 27, 2026
Merged

tedin7 merged 2 commits into
R0Y00:mainfrom
JohnConnorNPC:fix/frexp-f16-inline-const

Conversation

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Refs #3120.

What this changes

v_frexp_mant_f16 and v_frexp_exp_i16_f16 in src/gfx1013_direct_compute.inl read their source with
and(operand32(src0), 65535). For an inline float (encodings 240–248), constant32 returns the f32 bit
pattern (0x3f800000 for 1.0), so the op saw the low half of that pattern, 0x0000. As a result
v_frexp_mant_f16 v3, 1.0 gave 0x0000 instead of 0x3800, and v_frexp_exp_i16_f16 v3, 1.0 gave 0
instead of 1.

The fix makes an inline float read its binary16 value, as v_add_f16 already does in the same file. The
nine-entry table that v_add_f16 kept locally (halves[]) is now a shared member, inline_half_words,
next to inline_float_words, and both ops read it. Register, literal and integer-constant sources are
unchanged, and so is v_add_f16.

Test added

native_frexp_f16_ir (tests/native_frexp_f16_check.py ir, registered in
tests/cmake/native_frexp_f16.cmake) needs no GPU and no DXC. It builds one compute container, dumps the
IR with native_compute_ir_dump, and evaluates lane 0 with interpret() from native_f16_alu_check.py.
It covers 22 results:

  • all nine inline floats with both ops (VOP1);
  • three VOP3A forms: |-2.0| with clamp, -1.0 and -1/(2pi);
  • one VGPR source as a control.

Each result keeps a seeded D[31:16]. Two rows are pinned independently of the lowering:

  • 1.0 gives exponent 1 and mantissa 0x3800;
  • 1/(2pi) (0x3118) gives exponent 0xfffe and mantissa 0x3918.

Verification

Commit a7b31d1f on 429f11d5. The tools were cross-built on Linux with clang-cl 19 and the xwin SDK
(ninja -j1 native_compute_ir_dump, Release). The checks ran with an embeddable Windows Python under Wine
(Proton Experimental):

  • python -B tests/native_frexp_f16_check.py ir native_compute_ir_dump.exe:
    • before the fix (429f11d5): fails, 19 of 22 rows wrong. The first one is row 4,
      v_frexp_mant_f16 v3, 1.0: got beef0000, expected beef3800.
    • with the fix: passes, 22 results matched, inline floats read as binary16.
  • Paired mutation: I restored only the old operand line on the final tree (byte copy, sha256 and mode
    recorded), rebuilt, and the check failed with the same 19 rows. After restoring the file (hash
    matched) and rebuilding, it passed again.
  • python -B tests/native_f16_alu_check.py ir native_compute_ir_dump.exe: passes before and after,
    82 results match the model; 16 refusals carry the named diagnostic. This shows v_add_f16 reads the
    shared table unchanged.
  • ctest -N --no-tests=error -R '^native_frexp_f16_': lists native_frexp_f16_controls,
    native_frexp_f16_hardware and native_frexp_f16_ir.
  • tests/docs_links_check.py and tests/bidi_controls_check.py: pass.

Not run:

  • native_frexp_f16_controls, because it calls dxc.exe.
  • native_frexp_f16_hardware, because no GPU was used.

A Windows reviewer can run all three with:

cmake --build --preset dev --target compile_dxil native_store_replay native_compute_ir_dump
ctest --preset dev --no-tests=error -R '^native_frexp_f16_' -V

CI: the PR job runs the pr presets. Their fixed test allowlist contains no native_* test, and the
pr build preset does not build native_compute_ir_dump. So PR CI checks that the change compiles, not
the new test.

Scope: a synthetic direct-compute IR fixture for the inline-float source of these two ops only. It does
not establish full-game execution.

This PR was AI-assisted.

… v_frexp_exp_i16_f16

The lowering masked the f32 pattern of an inline float (240-248) to its low
16 bits, so v_frexp_mant_f16 v3, 1.0 gave 0x0000 instead of 0x3800. Read the
binary16 table that v_add_f16 already uses, now shared as inline_half_words,
and add native_frexp_f16_ir, which evaluates the emitted IR for all nine
constants without a GPU or DXC.

@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 d8b40a7 (updated with main). Comment only.

This fixes the inline-constant gap I pointed out on #3123 and #3125 for the already merged #3120: v_frexp_mant_f16 / v_frexp_exp_i16_f16 now read sources 240-248 as their binary16 values, through a shared inline_half_words table that v_add_f16 also uses now (same nine values as its old local halves[], so that path is unchanged). The new native_frexp_f16_ir test needs no GPU: it evaluates the emitted IR for all nine inline constants, abs/neg/clamp forms and a VGPR source against frexp_f16, with two pinned rows independent of the lowering, so it runs in CI and on WARP hosts.

The change is in src/gfx1013_direct_compute.inl, which the cpp-migration branch also changes; it merges cleanly there and the native sweep passes on that merge too (comment below).

Windows results are in the comment below.

Commands: git diff origin/main...pull/3147/head (read), git merge-tree --write-tree origin/cpp-migration pull/3147/head, plus the Windows commands listed in the comment.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Windows Server 2025 verification of d8b40a76814668bd39b63cf0da8cd567d3e133b5 (D3D12 WARP, MSVC 14.44.35207):

  • dev: cmake --build --preset dev --target compile_dxil native_store_replay native_compute_ir_dump; ctest --preset dev -R <native sweep>: 100% tests passed out of 133 (77 passed, 56 hardware legs skipped), including native_frexp_f16_ir and native_frexp_f16_controls
  • release: same targets with --preset release; ctest --preset release -R <native sweep>: 100% tests passed out of 133 (77 passed, 56 skipped)
  • The same dev and release sweep on this head merged with cpp-migration (a0f67731): 100% tests passed out of 133 in both presets

<native sweep> = every native_* test except native_startup_request_cache_hardware, which fails on WARP-only hosts with or without this PR.

AI-assisted; commands listed were executed locally.

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.

3 participants