Skip to content

startup: draw the gfx2hlsl binary32 rounding operations - #1741

Open
Asimmimx wants to merge 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-float-round-check
Open

Asimmimx wants to merge 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-float-round-check

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

Refs #1149.

Change

No check ran the rounding operations of the VEC_F table in tools/startup/gfx2hlsl.py: v_floor_f32, v_ceil_f32, v_trunc_f32, v_fract_f32 and v_rndne_f32. They lower to HLSL floor, ceil, trunc, frac and round.

This PR adds a check only. The translator does not change.

tests/startup_gfx2hlsl_float_round_check.py draws them with the per-lane runner tests/startup_gfx2hlsl_lane_alu.py. The runner is byte-identical to the copies in #1703, #1706, #1708 and #1712. In each draw:

  • one operation runs per lane on flat inputs, alternating _e32 and _e64;
  • the four result words are compared exactly;
  • the recorded llvm-mc bodies are checked by a binary mode.

The references follow the AMD RDNA 2 ISA reference:

  • Signed zero: a zero result keeps the sign of the source. ceil(-0.75), trunc(-0.75) and rndne(-2^-30) give -0 (0x80000000).
  • Ties: v_rndne_f32 rounds halves to even. 2.5 → 2, 3.5 → 4, -2.5 → -2, 1000000.5 → 1000000, 8388607.5 → 8388608.
  • Fract cap: v_fract_f32 is x - floor(x), capped at the largest binary32 below 1.0. For -2^-30 the exact difference rounds to 1.0, and the instruction gives 0x3f7fffff. The compute translator lowers fract with an explicit cap because HLSL frac can return 1.0 there on some adapters; see the v_fract_f32 note in docs/experiments.md.

The inputs also include the binary32 just below 0.5 (0x3effffff).

Each operation has one mutated lowering, and the first draw with it must read the mutation's own reference words:

  • floor and ceil exchanged;
  • floor for trunc;
  • x - trunc(x) for frac;
  • trunc for round.

Results

Commit 8ff3dddf on origin/main. Windows 11 26300.9974, DXC 1.8.2502.11, FXC 10.0.26100, debug layer on.

Adapter Driver Draws (FXC ps_5_1 + DXC ps_6_0) Mutated draws
WARP 10.0.26100.9818 20/20 match 10/10 read the mutated words
AMD Radeon RX 6600 (RDNA 2) 32.0.21045.29003 20/20 match 10/10 read the mutated words

There were no skips. binary with llvm-mc 19.1.1 gives fixture words match llvm-mc.

frac(-2^-30). On both adapters, through both compilers, the plain HLSL frac read 0x3f7fffff. The gfx2hlsl lowering, which has no explicit cap, therefore matches the ISA on these two adapters. An adapter whose frac returns 1.0 there would fail this draw. That is intended: the check would then show that gfx2hlsl needs the same cap as compute_store_aot.

AI-assisted (Claude Code).

The VEC_F table lowers v_floor_f32, v_ceil_f32, v_trunc_f32, v_fract_f32
and v_rndne_f32 to HLSL floor, ceil, trunc, frac and round, and no check
ran any of them.

tests/startup_gfx2hlsl_float_round_check.py draws them with the shared
per-lane runner (byte-identical copy of tests/startup_gfx2hlsl_lane_alu.py)
against references written from the RDNA 2 ISA: signed zero results, round
half to even, and fract capped at 0x3f7fffff, on exact halves and
quarters, 1000000.5, the binary32 just below 0.5, 8388607.5 and -2^-30.
Each operation has a mutated lowering whose first draw must read its own
words.

All 20 draws match on WARP and an RX 6600 through FXC ps_5_1 and DXC
ps_6_0, the 10 mutated draws read their mutated words, and frac(-2^-30)
reads 0x3f7fffff on both adapters.

@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 head 8ff3dddf38854a2b59e036c89274442f38c7925f. Tests, CMake, CI list and docs only; tools/startup/gfx2hlsl.py is unchanged. Verdict: 0 P1, 1 P2 (design note, not blocking), plus coverage nits.

Linux limits. Linux x86_64, llvm-mc-19 (the new CI entry pins llvm-mc-15), Python 3.14. No Windows, no DXC/FXC, no WARP or GPU: I could NOT run the draw mode, so the WARP/RX 6600 results in the docs (20 draws, 10 mutated draws, frac(-2^-30) = 0x3f7fffff) are not verified by me. Nothing was run through HLSL; the lowerings are only read as text.

What I ran (worktree at that head)

  • python3 -B tests/startup_gfx2hlsl_float_round_check.py (controls): passes. ... binary llvm-mc-19: fixture words match llvm-mc.
  • git diff --check origin/main...HEAD, docs_links_check.py, bidi_controls_check.py: clean. The diff has one non-ASCII line (the -> arrows in the docs).
  • References, read against the ISA wording: zero results keep the source sign (ceil(-0.75), trunc(-0.75), rndne(-2^-30) give -0), v_rndne_f32 is half-to-even (Python round on the double is also half-to-even; 2.5 -> 2, 3.5 -> 4, -2.5 -> -2, 1000000.5 -> 1000000), v_fract_f32 is x - floor(x) capped at 0x3f7fffff. 0xb0800000 is -2^-30 and 0x3effffff is the binary32 below 0.5. The VEC_F text is plain floor/ceil/trunc/frac/round.
  • Mutations (restored with git checkout -- .):
    • production v_fract_f32 frac -> trunc: controls FAILED (AssertionError: ('v_fract_f32', 0)); this happens because the control's mutation regex no longer matches the changed text.
    • test body word of v_ceil_f32 opcode 0x45 -> 0x44: FAILED in binary (assembled words differ).
    • production floor({0}) -> floor({0} + 0.0000001): controls and binary PASSED, not caught on Linux.
    • production round({0}) -> round(-{0}): controls and binary PASSED, not caught on Linux.
    • test reference of v_rndne_f32 changed to ties-up: controls and binary PASSED (only the draw reads it).
      So a wrong value that keeps the shape of the text is caught only by the draws on a Windows adapter; please make sure they ran for this head.

P2 (design note). The cap of v_fract_f32 at 0x3f7fffff is part of the reference, but the translator emits a bare frac(x); the docs say both adapters returned 0x3f7fffff "without an explicit comparison". For -2^-30 the exact difference rounds to 1.0, so on an adapter or compiler path that computes frac as x - floor(x) in binary32 the result would be 0x3f800000. The new test will then fail on that adapter rather than the lowering being fixed; a min(..., 0x3f7fffff) in the lowering, or a note that the cap is adapter-dependent, would settle it.

Coverage nits

  • No NaN, infinity or -0.0 source in any set: floor(NaN), rndne(-0.0), ceil(-0.0), trunc(+/-inf) are unchecked, although the task of the check is the sign-of-zero rule.
  • binary32 values at and above 2^23 (no fractional bits) appear only as 8388607.5 and 1000000.5.

Not approving (comment only).

AI-assisted; commands listed were executed locally.

@KainanP

KainanP commented Nov 24, 2026

Copy link
Copy Markdown

A heads-up for v_rndne_f32 -> round({0}), since the draws ran on WARP and an RX 6600 only.

In LooperSalty/AnyXboxX#160, HLSL round() (DXIL dx.op.unary 26, Round_ne) returned round-half-away-from-zero on an RTX 5070 (Blackwell), driver 32.0.16.2140. It did so only when the driver could propagate a constant into it. The repro kept a 256-entry register array, stored 2.5 into one entry under an EXEC test, then called round(asfloat(v[15])). That gave 0.5 → 1.0, 1.5 → 2.0, 2.5 → 3.0, -0.5 → -1.0, -2.5 → -3.0, where WARP gives 0.0, 2.0, 2.0, -0.0, -2.0. The DXIL from dxc was correct. When the value came from memory, round was correct on the same card, and floor/ceil/trunc/frac were unaffected: LooperSalty/AnyXboxX#160

It did not reproduce on an RTX 4060 Laptop (Ada), driver 32.0.16.1047: LooperSalty/AnyXboxX#160 (comment). So it is either Blackwell-specific or new in recent drivers.

What this means here:

  • The draws in this PR read flat (runtime) inputs, so I would expect them to pass on that card too. The case at risk is a translated title shader where a v_rndne_f32 source turns into a constant after inlining, for example a literal or an s_mov that reaches it.
  • AnyXboxX avoided the problem by lowering v_rndne_f32 without round(): floor, then a tie-to-even correction on an exact .5 fraction, the value passed through unchanged from 2^23 up, and the sign kept for a zero result.
  • A draw on a Blackwell card with a recent driver, using a constant-fed variant of the ties row (2.5, -2.5, 0.5), would show whether gfx2hlsl is affected. This was measured on the DXIL path only, not on FXC ps_5_1.

AI-assisted (Claude Code). The measurements are from the linked AnyXboxX threads, not from runs of this PR.

@thepatrickworld

Copy link
Copy Markdown
Contributor

Draw mode on Windows, and one thing to fix in the CMake block

tedin7's review says it could not run the draw mode, so the independent confirmation was missing. It is here now — with one caveat about the test registration, below.

2026-11-24, head 8ff3dddf, merged onto main 330df7f0 (my merge adfee36b, tree 90a3df7f, 1117 tests). The merge conflicts only in docs/experiments.md: main added the #1728 non-fused multiply-add section at the same place as your rounding section, so I kept both and committed that.

Result, with the FXC path passed explicitly:

warp:     20 draws matched, 10 mutated draws read their mutated words
hardware: 20 draws matched, 10 mutated draws read their mutated words

Same counts as the table in the PR body, on a different architecture: AMD Radeon RX 9070 XT 1002:7550 driver 32.0.32029.2010 (RDNA 4, gfx1201) rather than the RX 6600, with DXC 1.9.2602.17 rather than 1.8.2502.11 and FXC 10.0.26100.

frac(-2^-30). On this adapter, through both compilers, it reads 0x3f7fffff, the capped value — the same word your two adapters give. So the failure mode you documented does not appear here, and the lowering keeps the cap without an explicit comparison on a third adapter. That is a result for this adapter and driver, not a reason to drop the cap: an adapter whose frac returns 1.0 there would still fail the draw, which is what you intended. The draw prints it directly for the eighth input set:

fxc  v_fract_f32 [49742408, 3effffff, b0800000, 4affffff]: 3f000000,3effffff,3f7fffff,3f000000
dxc  v_fract_f32 [49742408, 3effffff, b0800000, 4affffff]: 3f000000,3effffff,3f7fffff,3f000000

v_rndne_f32 against KainanP's heads-up

The comment above reports round() returning round-half-away-from-zero on an RTX 5070 when the driver propagated a constant, which is exactly what v_rndne_f32 -> round({0}) lowers to. On this adapter, through both compilers, the draw reads half-to-even:

fxc/dxc v_rndne_f32 [40200000, bf400000, 40600000, c0200000]: 40000000,bf800000,40800000,c0000000
        2.5 -> 2.0        -0.75 -> -1.0       3.5 -> 4.0       -2.5 -> -2.0
fxc/dxc v_rndne_f32 [49742408, 3effffff, b0800000, 4affffff]: 49742400,00000000,80000000,4b000000
        1000000.5 -> 1000000.0     0.4999999 -> 0.0     -2^-30 -> -0.0     8388607.5 -> 8388608.0

So the away-from-zero result does not reproduce here, on AMD hardware or on WARP, with FXC or DXC. That says nothing about the RTX 5070 case — I have no NVIDIA hardware — but it does mean the lowering is not wrong per se, and that a driver can differ on it.

The registration drops the FXC argument

Running the test through ctest gives 10 draws and 5 mutated draws, not 20 and 10 — one compiler instead of two:

349: Test command: ... float_round_check.py draw ".../dxc.exe" ".../offscreen_check.exe" hardware ""

The fifth argument is empty, so check_draw receives no fxc and builds compilers with DXC only. Passing the path by hand gives the 20/10 counts above, on the same build and the same tree.

Cause: ${STARTUP_FXC} is expanded at configure time, and find_program(STARTUP_FXC fxc HINTS "${DXC_BIN_DIR}") sits at line 1665, after the add_test block at lines 1648–1653 in the same function scope. The variable is therefore empty when those add_test calls run, while the cache still ends up with STARTUP_FXC:FILEPATH=.../10.0.26100.0/x64/fxc.exe. Re-running cmake --preset dev does not help — I checked, the command still carries "", because the ordering is the same on every configure.

Consequence: on a fresh build, every reader of this test gets the DXC half only, and the test still passes, so nothing signals the gap. Moving the find_program above the new block restores the 20/10 counts under ctest without touching the fixture — the sampler-heap section further down already does it that way, with its find_program above its own tests.

Environment: Windows 11 25H2 26200.9974, DXC 1.9.2602.17, FXC 10.0.26100, dev preset, debug_layer=unavailable.

Limits: one adapter, one driver, no debug layer. The inputs are exact, so this checks which function and which base each operation lowers to, not any adapter's approximation error.

AI-assisted; runs executed on the machine above.

@Makto94

Makto94 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent run of 8ff3dddf on an NVIDIA GeForce RTX 3070 Ti (desktop), Debug build. startup_gfx2hlsl_float_round_hardware fails on this adapter; _controls and _warp pass.

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_float_round_' -j1 -V

Environment: driver 32.0.16.2101 (621.01); Windows 11 Pro 10.0.26300.9974; DXC 1.9.2602.17; debug_layer=enabled, debug_errors=0 debug_warnings=0.

Hardware, second v_fract_f32 draw, third lane (input b0800000, -2^-30). The test alone fails the same way:

adapter=NVIDIA GeForce RTX 3070 Ti vendor=10de device=2482
startup_flat_param mismatch expected=3f000000,3effffff,3f7fffff,3f000000 actual=3f000000,3effffff,3f800000,3f000000

WARP, same draw, same build:

dxc v_fract_f32 [('49742408',), ('3effffff',), ('b0800000',), ('4affffff',)]: 3f000000,3effffff,3f7fffff,3f000000

tools/startup/gfx2hlsl.py:1057 (not changed by this PR) emits frac({0}); the reference in tests/startup_gfx2hlsl_float_round_check.py:41-43 caps the result at 0x3f7fffff. 0x3f800000 is the value tedin7's review note gives for -2^-30.

The test stops at that draw. A local wrapper that records a failed draw and continues (not the PR command) shows the other 9 unmutated hardware draws matching and the 5 mutated draws reading their mutated words, including:

DRAW v_rndne_f32  in=['40200000', 'bf400000', '40600000', 'c0200000'] want=40000000,bf800000,40800000,c0000000 got=ok
DRAW v_rndne_f32  in=['49742408', '3effffff', 'b0800000', '4affffff'] want=49742400,00000000,80000000,4b000000 got=ok

Not covered by this run: the FXC path (the registered command passes "" for it), constant inputs, a Blackwell adapter, and the inputs 0.5, 1.5 and -0.5.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Hardware run of head 8ff3ddd. Windows 11 Pro 26100.9168, DXC/FXC from Windows SDK 10.0.26100, dev preset, debug layer unavailable here. Adapter selected with XBOX2WINDOWS_ADAPTER: WARP 10.0.26100.8972, NVIDIA Quadro M1000M (Maxwell, 10de:13b1) 32.0.15.7371, Intel HD Graphics 530 (Gen9, 8086:191b) 31.0.101.2111.

Test Result
startup_gfx2hlsl_float_round_controls Passed
startup_gfx2hlsl_float_round_warp Passed
startup_gfx2hlsl_float_round_hardware on the HD 530 Passed
startup_gfx2hlsl_float_round_hardware on the Quadro M1000M Failed at v_fract_f32 (FXC)

The floor, ceil and trunc rows pass on the Quadro too. The failing draw:

AssertionError: fxc v_fract_f32 inputs [(1232348168, 0, 0), (1056964607, 0, 0), (2961178624, 0, 0), (1258291199, 0, 0)]
startup_flat_param mismatch expected=3f000000,3effffff,3f7fffff,3f000000 actual=3f000000,3effffff,3f800000,3f000000

Lane 2 is 0xb0800000, a tiny negative value.

  • What the fixture expects: the ISA result 0x3f7fffff, the largest value below 1.0.
  • What the Quadro returns: 0x3f800000, which is 1.0.

This looks like a real difference in the lowering, not in the fixture. v_fract_f32 is frac({0}) in gfx2hlsl, and D3D's frac has no clamp below 1.0. pixel_aot documents exactly this ("D3D frac has no clamp and differs between adapters") and lowers it through fract32, which clamps to 0x3f7fffff. WARP and the HD 530 happen to return the clamped value; Maxwell does not. I have not tested a fix.

No changes made.

@thepatrickworld

Copy link
Copy Markdown
Contributor

@Asimmimx the two NVIDIA results in this thread come from the lowering, not from your draw: gfx2hlsl emits a bare frac({0}) for v_fract_f32, while tools/pixel_aot.py has always capped the same instruction at 0x3f7fffff through fract32, with the comment that D3D frac has no clamp and differs between adapters. On 0xb0800000 the uncapped form rounds up to 1.0 on Maxwell and Turing.

I have opened #2010, which puts that capped helper in the gfx2hlsl preamble, points the table entry at it, and adds a check that pins the emitted text. It targets main, so your branch does not have to carry it.

Two things worth knowing. There is no NVIDIA adapter on my machine — RX 9070 XT and WARP only — so I cannot confirm that the Maxwell and Turing rows turn green; the reasoning above comes from your two reports and from the rule this repository had already written, not from a measurement. And your branch does not carry the ftz32/mad32 helpers that main has today, so rebasing it conflicts in that same preamble; the v_fract_f32 row should have nothing left to disagree about once both land.

AI-assisted.

@thepatrickworld

Copy link
Copy Markdown
Contributor

@Asimmimx one change your branch will need when it lands, so that it does not break quietly.

tests/startup_gfx2hlsl_float_round_check.py pins the v_fract_f32 lowering by mutating the emitted text, and the pattern it matches is U(frac(. The lowering is now fract32({0}) (#2010), so that pattern will no longer match: the mutation will leave the shader untouched and the control will pass without testing anything. Both Makto94 and legacy-code-archaeologist ran into this and changed the pattern to U(fract32( locally. It is worth making the control assert that the mutation actually changed the text, so the next lowering change cannot silence it the same way.

The rest is good news for your draw. The form that survives FXC is to compare the encoding rather than frac(value) >= 1.0f, which FXC folds to false and compiles to a bare frc; #2010 now carries asuint(f) == 0x3f800000u ? asfloat(0x3f7fffffu) : f. On that head the same four inputs read 3f000000,3effffff,3f7fffff,3f000000 on WARP, on the Quadro M1000M that was failing and on an Intel HD 530, under fxc and dxc — so the row your draw is missing should go green once the two are combined.

Your branch also predates #1728, so a rebase conflicts in the preamble where ftz32 and mad32 live. Keep both sides there; nothing in your change depends on them.

AI-assisted.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@Asimmimx could you rebase this branch onto current main and force-push?

GitHub reports head 8ff3dddf as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (cf97bee1):

  • CMakeLists.txt
  • docs/experiments.md
  • tests/startup_gfx2hlsl_lane_alu.py

Commands run locally:

git fetch origin main pull/1741/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 1741 --json mergeable   # CONFLICTING

Once it is rebased and CI is green it will be re-checked right away.

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.

6 participants