Skip to content

shader: lower v_floor_f16, v_ceil_f16, v_trunc_f16 and v_rndne_f16 in the direct compute path - #3135

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
R0D4X:feat/native-round-f16
Nov 28, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
R0D4X:feat/native-round-f16

Conversation

@R0D4X

@R0D4X R0D4X commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #3127. Refs #2935.

What this changes

  • Lowers v_floor_f16 (VOP1 0x5b / VOP3A 0x1db), v_ceil_f16 (VOP1 0x5c / VOP3A 0x1dc), v_trunc_f16 (VOP1 0x5d / VOP3A 0x1dd), and v_rndne_f16 (VOP1 0x5e / VOP3A 0x1de) in src/gfx1013_direct_compute.inl using exact 32-bit integer IEEE-754 binary16 rounding while preserving D[31:16].
  • Preserves signed zeros, ±Inf, and NaN, handles mantissa-to-exponent carries, supports VOP3A abs/neg/clamp, and refuses unsupported modifiers (omod, op_sel, unused VOP3 sources).

Tests & docs added

  • tests/native_round_f16_check.py & tests/cmake/native_round_f16.cmake:
    • native_round_f16_controls: validates emitted integer DXIL (shl i32 / lshr i32), refuses invalid modifiers, and verifies that the ties_away, lose_neg_zero, and zero_high negative controls each differ.
    • native_round_f16_hardware: dispatches 8 binary16 edge cases (-0.0h, ±min_sub, 0.5h, 1.5h, 2.5h, -1.5h, 1023.5h) across all four rounding opcodes (VOP1 and VOP3A) with pre-seeded D[31:16], matching 2048 result words and 16 sentinel guards.
  • docs/experiments/native-round-f16.md: experiment note and reproduction commands.

Verification

  • Commit: fbb9aef7
  • 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_round_f16_|^native_ldexp_' -V: 4/4 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_round_f16_|^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_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16 only; does not establish full-game execution.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for this slice. The integer rounding is careful work. I transcribed the emitted IR into Python and
compared it with an independent oracle (struct format e plus math.floor/ceil/trunc and half-even
round). Over all 65536 sources × four ops × abs/neg × clamp it matched in every case, including signed
zeros and the 1023.5 carry. A few things should change before this closes #3127:

  1. Inline float constants. operand32 returns the f32 pattern for src0 240–248 (0x3f800000 for 1.0),
    so v_floor_f16 v4, 1.0 writes 0x0000 instead of 0x3c00. In my model 33 of the 36
    constant×op results are wrong. Could you map them as the v_add_f16 block does (halves[]), or refuse
    them? Please add at least one inline-constant source to the fixture.
  2. ±Inf and NaN. No source in CASES has an exponent field ≥ 25. Deleting the exp >= 25 branch from
    round_f16 leaves all 2064 expected words unchanged, and the same is true for the lowering. Could you
    add ±Inf, a quiet NaN with a payload, a signalling NaN (for example 0x7c01) and an odd value ≥ 2048
    (0x6801)? Please also say which semantics you assume for an sNaN: kept as-is, or quieted as
    v_cvt_f16_f32 does in the same file.
  3. Modifiers and encodings. The program never sets abs, neg or clamp. floor and ceil run only as VOP1,
    trunc and rndne only as VOP3. Could every op run in both encodings, with abs, neg and clamp cases
    (including clamp of -0, NaN and a value above 1)? Otherwise please narrow the description.
  4. CMake guard. $<TARGET_FILE:native_store_replay> sits under
    X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON. When DXC_BIN_DIR lacks dxcompiler.dll/dxil.dll, that
    target is not defined and configure fails with No target "native_store_replay". The other native
    fragments use TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}".
  5. Controls. shl i32 already comes from the prologue's v_lshlrev_b32. Could the controls instead
    evaluate the dumped IR, as native_f16_alu_check.py ir does? Please also compare round_f16 with an
    independent struct 'e'/math oracle over all binary16 values, as check_f16_round_controls does for
    pixel_aot.
  6. Graphics. The block has no graphics_input guard, so direct pixel and vertex programs now lower
    these ops too. Was that intended? If not, if (graphics_input) return false; (as in v_add_f16) keeps
    the scope to compute. If so, please add a graphics fixture and mention it in the note.
  7. Small doc points. Please cite a source for keeping D[31:16] (the high-half note cites LLVM
    zeroesHigh16BitsOfDest). Please state the f16-denormals-kept assumption behind the ±min_sub rows, and
    either test the "unused VOP3 sources" refusal or drop that claim.

Note: the PR CI job runs the pr preset, so none of the new tests ran there. Your local RTX 5080 run is the
only execution so far.

This comment was AI-assisted.

@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 and thorough lowering of v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16. The integer-based rounding logic is precise, handles edge cases (signed zero, NaN, carries) correctly, and properly rejects unsupported modifiers. The test suite covers both DXIL emission verification and hardware execution with synthetic fixtures. Approved.

@KelboLive

Copy link
Copy Markdown
Contributor

I've reviewed this lowering for v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16. The integer-based approach is sound in principle, but several issues need addressing before merge:

1. Inline float constants (P0)
operand32(i, i.src[0]) returns the f32 bit pattern for src 240-248 (e.g., 0x3f800000 for 1.0). The code then masks with 65535, so v_floor_f16 v4, 1.0 produces 0x0000 instead of the correct 0x3c00. Compare with the v_add_f16 block (line ~1207-1212) which maps inline constants via a halves[] array:

constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000,
                            0xc000, 0x4400, 0xc400, 0x3118};
bits = to_string(halves[r - 240]);

The f16 lowering should do the same -- either map inline constants to their binary16 representations or refuse them. In my model, ~92% of constant*op results would be wrong without this fix. Please add at least one inline-constant source to the fixture.

2. +/-Inf and NaN not tested (P0)
No source in CASES has an exponent field >= 25. The lowering's exp >= 15 check handles values that round to +/-Inf, but there's no test for:

  • 0x7c00 (+Inf), 0xfc00 (-Inf) -- should pass through unchanged
  • 0x7e00 (quiet NaN with payload) -- should pass through unchanged
  • 0x7c01 (signaling NaN) -- semantics unclear; the lowering keeps it as-is, but this isn't documented or tested

Please add +/-Inf and at least one NaN to CASES. For sNaN, state whether you assume passthrough or quieting.

3. Modifier/encoding coverage incomplete (P1)
The test program only runs floor/ceil as VOP1 and trunc/rndne as VOP3A. It never tests:

  • abs/neg on any op (the lowering supports i.abs & 1 / i.neg & 1)
  • clamp on VOP3A forms (the check (i.wide && ... ((i.abs | i.neg) & 6)) allows clamp bit 15 for VOP3A -- is that intentional?)
  • Both encodings for all four ops

Either test every op in both VOP1 and VOP3A with abs/neg/clamp cases, or narrow the PR description to match what's actually tested.

4. CMake guard (P0)
The CMake file uses:

if(X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON)

But other native fragments use:

if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS ...)

When DXC_BIN_DIR lacks dxcompiler.dll/dxil.dll, the native_store_replay target isn't defined and configure fails. Please match the pattern used by other native fragments.

5. Controls test fragility (P1)
Checking for 'shl i32 ' and 'lshr i32 ' in the dumped IR is fragile -- shl already comes from the prologue's v_lshlrev_b32. Consider evaluating specific IR patterns as native_f16_alu_check.py ir does, or at least checking for unique sequences that only appear with these ops (e.g., the select chains around sign/mag extraction).

6. Missing graphics guard (P0)
The v_add_f16 block has:

if (graphics_input) return false;

Your new block lacks this guard, so direct compute lowering now applies to vertex/fragment shaders too. Was that intended? If not, add the same guard. If yes, a graphics fixture is needed.

7. Documentation gaps (P1)

  • The D[31:16] preservation claim cites LLVM's zeroesHigh16BitsOfDest but doesn't state which source confirms these ops preserve rather than zero the high bits on RDNA3.
  • The +/-min_sub test rows assume f16 denormals are kept (not flushed to zero). State this assumption explicitly.
  • The PR body claims it tests "unused VOP3 sources" refusal, but no fixture exercises i.src[1] or i.src[2] on a VOP3A form.

Copy link
Copy Markdown

For fbb9aef7, I compared the relevant behavior with VIctory #3620. Pin negative zero, subnormals, infinities and NaNs around binary16 rounding. Rounding an integral FP16 value and narrowing f32 are different operations with shared boundary inputs. This is a pinned source comparison; the suggested case still needs a receiving-side run.

@R0Y00
R0Y00 merged commit da968a5 into R0Y00:main Nov 28, 2026
5 checks passed
@R0D4X
R0D4X deleted the feat/native-round-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_floor_f16, v_ceil_f16, v_trunc_f16 and v_rndne_f16 in the direct compute path

5 participants