shader: lower binary16 arithmetic and integer conversions in gfx2hlsl (Part of #1723) #2066
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2073"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:01:19Z.
Part of #1723
What this unblocks: shaders that use 16-bit vector arithmetic (add, sub, mul, min, max, fma forms and i16/u16 conversions) now translate in gfx2hlsl instead of being refused.
Verification:
python3 -B tests/startup_gfx2hlsl_f16_alu_check.pyon origin/main: fails withUnsupported: v_add_f16.python3 -B tests/startup_gfx2hlsl_f16_alu_check.pyon this branch: passes (value checks as bits, clamp, and refusals for op_sel, omod and wrong operand counts).python3 -B tests/startup_gfx2hlsl_{opsel,op_refusals,operand_count,cvt_alu,refusals}_check.py: all exit 0.startup_gfx2hlsl_f16_aluin CMakeLists.txt.Notes:
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:09:16Z.
Review: COMMENTED
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:
6653af27c5ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:01:27Z.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
6653af2ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:09:16Z.
Code review location:
tools/startup/gfx2hlsl.py; line 722For a native
v_cvt_f16_i16orv_cvt_f16_u16using inline source encoding 242, the shared disassembler prints1.0, butself.u()supplies the binary32 word0x3f800000instead of the binary16 word0x3c00. Masking its low half therefore converts zero rather than integer 15360. For example, synthetic encodingf2a2067eproduces0x0000instead of0x7380. Decode inline constants according to the 16-bit source width before interpreting them as signed or unsigned integers, or refuse these encodings.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:09:16Z.
Code review location:
tools/startup/gfx2hlsl.py; line 725For an e64 half instruction with NEG set on a hexadecimal literal, the shared decoder emits a token such as
-0x3c01. This condition does not recognize the modifier because the token starts with-0, soself.u()instead performs two's-complement integer negation. A decodedv_add_f16_e64 v3, neg(0x3c01), v1withv1 = 0consequently produces0xc3ff(-3.998046875) rather than0xbc01(-1.0009765625). Preserve the encoded NEG as a floating sign flip, or reject the encoding instead of silently changing the operand.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:09:16Z.
Code review location:
tools/startup/gfx2hlsl.py; line 730For inline source encoding 248 (
0.15915494), emitting the binary32 value fromFLOATSdoes not reproduce the native binary16 operand, which is0x3118(0.1591796875), as already recorded inpixel_aot.INLINE_F16. For example,v_mul_f16_e32 v3, 0.15915494, v1withv1 = 0x3002should produce0x251b, but this lowering produces0x251a. Widen the binary16 inline-constant value before applying arithmetic; rounding only the final result is not equivalent.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:11Z.
Code review location:
tools/startup/gfx2hlsl.py; line 722Fixed in
098a0c49(rebased on main; pending host push): inline float constants at i16/u16 width are now binary16 words, a negated hex literal is a float sign flip, and 0.15915494 widens from binary16 before the arithmetic. Covered by new cases in tests/startup_gfx2hlsl_f16_alu_check.py (ran locally, passes).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:13Z.
Code review location:
tools/startup/gfx2hlsl.py; line 725Fixed in
098a0c49(rebased on main; pending host push): inline float constants at i16/u16 width are now binary16 words, a negated hex literal is a float sign flip, and 0.15915494 widens from binary16 before the arithmetic. Covered by new cases in tests/startup_gfx2hlsl_f16_alu_check.py (ran locally, passes).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:14Z.
Code review location:
tools/startup/gfx2hlsl.py; line 730Fixed in
098a0c49(rebased on main; pending host push): inline float constants at i16/u16 width are now binary16 words, a negated hex literal is a float sign flip, and 0.15915494 widens from binary16 before the arithmetic. Covered by new cases in tests/startup_gfx2hlsl_f16_alu_check.py (ran locally, passes).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:11Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:13Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:14Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T01:53:44Z.
Independent run on AMD, and one divergence the fixture cannot reach
Their draw passes here:
startup_gfx2hlsl_f16_alu_draw_check.py draw … hardware …exits 0 on the AMD Radeon RX 9070 XT (1002:7550, driver32.0.32029.2010), with FXC and DXC, head61447651. So the positive half holds on a third adapter, integer conversions included — that is where I expected trouble and there was none.v_fmac_f16is emitted asf16rn(a*b + c). The product of two binary16 values is exact in binary32, but the sum is not: it can need more than 24 bits, so the binary32 result is rounded and then rounded again into binary16. The guide describesV_FMAC_F16as fused, 0.5 ULP precision, which is one rounding. Where the two disagree, the translation is one binary16 unit off.Measured on the card and on WARP, under FXC and DXC, all four lanes:
4a541ef44e2d0x4e330x4e3251eabf3e1d5f0xd55b0xd55a2bd5be8345280x450f0x450e3915387a4b710x4b9f0x4b9eThe expected column is this PR's own reference, the exact rational model, so the disagreement is between the model and the lowering rather than between two opinions of mine. A one-bit corruption of the expected word is rejected by the same harness, so it does discriminate.
The triples are not exotic: nine in 400 000 random binary16 triples, about 2e-5. The fixture does not reach them because
ARITHandACConly combine values where the exact sum and the binary32 sum agree (1*2+1,100*1000+100, …), so the exact model and the lowering never conflict on any input it draws.v_fmamk_f16andv_fmaak_f16share the same template andV_FMA_F16is described the same way; I did not measure those two.What could be done is a design choice, so I am only listing it. A compensated sum in binary32 (TwoSum over
a*bandc) recovers the exact sum and lets the single rounding be done explicitly; the alternative is to state the 1-ULP divergence as a known limit of the lowering instead of claiming the fused accuracy. Either way a control carrying a triple from this class would save the next reader the trouble of finding it again.Limits. AMD and WARP only, no NVIDIA here; and the native
v_fmac_f16cannot be executed from a desktop shader, so the "one rounding" side rests on the guide's wording rather than on a run, exactly as the reference model does.AI-assisted; runs executed on the machine above.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T02:10:41Z.
Measured the new head
08c5844fwith the same four triples: they now agree.f16rn(a*b + c)gave4e32, d55a, 450e, 4b9ehere; withfma16othe same four lanes come backwhich is the exact model's value, under FXC and DXC, on hardware and on WARP. The one-bit corruption control still fails, so the harness is still discriminating — the change is in the value, not in the check.
TwoSum plus round-to-odd is the right shape for this:
pis exact, so the SumErr ofs = p + crecovers what binary32 dropped, and moving an inexact even result to its odd neighbour toward the true value means thef16rnthat follows is the only rounding left. Theprecisequalifiers matter as much as the algebra — without them a compiler is free to reassociate the compensation away, and FXC is allowed to contract.One suggestion. The fixture still does not contain a triple of this class:
ARITH/ACConly combine values where the exact sum and the binary32 sum agree, so revertingfma16otof16rn(a*b + c)would leave every draw in this PR green. The four triples above are free to add and they are the only thing in the file that would notice.v_fmamk_f16andv_fmaak_f16now share the helper, which covers them by construction; they are still unmeasured on hardware, since the fixture drivesv_fmac_f16only.Limits. AMD and WARP, no NVIDIA here; and
v_fmac_f16itself is never executed by these draws, so what passes is the value the translated shader produces.AI-assisted; runs executed on the machine above.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:42:39Z.
Thank you for the independent AMD run and the four measured triples. You were right.
Fixed in
08c5844f:v_fmac_f16,v_fmamk_f16andv_fmaak_f16now go through a small helper. It takes the exact binary32 product, recovers the sum error with TwoSum, and moves an inexact even result to its odd neighbour (round to odd). The singlef16rnthat follows is then the only rounding that can change the result.Counterexamples added (
4a54*1ef4+4e2d,51ea*bf3e+1d5f,2bd5*be83+4528,3915*387a+4b71, expected4e33,d55b,450f,4b9f):tests/startup_gfx2hlsl_f16_alu_check.py: the four triples run through the emitted statement with a model of the helper.tests/startup_gfx2hlsl_f16_alu_draw_check.py: they replace four of the eightv_fmac_f16lanes in the real draw, plus model and lane-coverage controls.Verification on Win2025 (WARP, FXC and DXC):
x2w_pipe.py win2025 2073 --force --targets offscreen_check --regex 'startup_gfx2hlsl_f16_alu_draw_(controls|warp)|startup_translator': dev 3/3 passed, release 3/3 passed.startup_gfx2hlsl_f16_alu_check.py,startup_gfx2hlsl_f16_alu_draw_check.py,startup_translator_check.py,docs_links_check.pyall pass.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T12:02:56Z.
NVIDIA run at
37b610ca:startup_gfx2hlsl_f16_alu_draw_hardwarefails on one draw,fxc v_fmac_f16 set 0I ran the current head on an NVIDIA GeForce RTX 3070 Ti. This is the first NVIDIA run in this thread. @thepatrickworld's run of the
fma16oform (AMD and WARP, 02:10Z) was on08c5844f; thefma16ohelper text is identical at08c5844fand37b610ca.Two runs gave the same result. Only
fxc v_fmac_f16 set 0fails:Every other draw passes in both runs: 26 per run (13 ops, 2 sets each) for FXC and 26 for DXC, minus this one. That includes
v_fmamk_f16andv_fmaak_f16, sets 0 and 1, with FXC and with DXC. They call the same helper; I did not check whether their inputs reach the correction.debug_errors=0 debug_warnings=0on all 52 draws in each run.Controls
dxc v_fmac_f16 set 0: 00004e33,0000d55b,0000450f,00004b9f.fxc v_fmac_f16 set 0: 00004e33,0000d55b,0000450f,00004b9f(startup_gfx2hlsl_f16_alu_draw_warp). The check uses the same HLSL and the same fxc command line forwarpandhardware.startup_gfx2hlsl_f16_alu,startup_gfx2hlsl_f16_alu_draw_controls,startup_gfx2hlsl_f16_alu_draw_warp: 3/3).What FXC emits
fxc /nologo /T ps_5_1 /E mainkeeps the seven float steps of thefma16ohelper separate, each marked[precise]:I also compiled the same shader with
/Gisand drew it on the RTX. I drew the default build the same way. Both give00004e32,0000d55a,0000450e,00004b9e, so/Gismakes no difference here. The seven steps are[precise]in both builds.Thread
The failing words match what thepatrickworld reported for the previous form: "
f16rn(a*b + c)gave4e32, d55a, 450e, 4b9ehere" (02:10Z comment).Hypothesis, not measured: on this card the correction term does not seem to have any effect. I have not isolated the cause and I do not propose a change.
Environment. NVIDIA GeForce RTX 3070 Ti (
10de:2482), driver 32.0.16.2101, Windows 11 build 26300.9974, Debug build, fxc 10.0.26100.9457, dxc 1.9.2602.17, D3D12 debug layer enabled. Both ctest runs were serial on an idle machine. 0 capability skips (77). One card and one driver; AMD and Intel are not measured by me at this head.Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T12:15:35Z.
Follow-up to my NVIDIA report: where the
fxc v_fmac_f16 set 0difference appears to startI drew the same
v_fmac_f16set 0 inputs with a probe that exports one intermediate of thefma16ohelper instead of the final binary16 word. The probe replaces the four linesv12..v15(vN = f16rn(fma16o(...))) withvN = asuint(probe16(...)).probe16has the same fourpreciselines asfma16oand returnsp,soreby variant:Compiled as in my previous comment:
fxc /nologo /T ps_5_1 /E main(no/Gis). In theevariant fxc keeps the fourpreciselines as separate[precise]mul/addinstructions (seven in total, as forfma16o).Words exported for set 0, lanes 0 to 3. "WARP read" equals the CPU value in each case:
3db00040,c2ab52b0,bdcbfffc,3eb6001041c65000,c2ab5000,40a1d000,4173d00035000000,b6800000,33000000,3500000000000000,00000000,00000000,00000000With
e == 0the helper returnss. Eachsword is exactly halfway between two consecutive binary16 values, and round-to-nearest-even on those fourswords gives4e32,d55a,450e,4b9e, which are exactly the words the test read on the RTX. The expected4e33,d55b,450f,4b9fneed the correctioneto movesoff the tie.My hypothesis, not a diagnosis: on this card the error term seems to be simplified to zero in this shader. I am not claiming a cause or proposing a change.
Limits: one draw per variant on the RTX; the RTX draws were on an idle machine. Only FXC without
/Giswas probed. DXC was not probed. I did not measuret, nor the two halves ofeseparately. The WARP column is one draw per variant, withdebug_errors=0. Each variant is a separate shader, sopandswere not read from the shader that readse. Environment as in the linked report (RTX 3070 Ti, driver 32.0.16.2101, head37b610ca);debug_errors=0in every RTX probe draw.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:58:18Z.
Thanks for the two measurements. They pin it down: on the RTX the error term
eof the float TwoSum comes back 0, so the helper returnss, which sits on a binary16 tie and rounds to even (4e32,d55a,450e,4b9e). WARP and DXC keep the term. So the float TwoSum infma16ois not safe on a driver that rewrites floating-point algebra, even withpreciseon every line.Change at
b87b5ae6fd:fma16ono longer uses floats for the correction. For finite operands it calls the existing integerfma16_rne(the helperpixel_aot.pyalready uses for packed fma), which does a*b+c with a single round-to-nearest-even on the binary16 encodings in 32-bit integer arithmetic. Infinity and NaN operands keep the binary32 path. The call sites (v_fmac_f16,v_fmamk_f16,v_fmaak_f16) and thestartup_gfx2hlsl_f16_alu_draw_hardware/_warpreadback tests are unchanged.What I could not do: Linux has no GPU, so I did not reproduce the NVIDIA mismatch here, and I have not run the draw tests. The hardware run on your card at
b87b5ae6fdis the check that decides this. The Python model instartup_gfx2hlsl_f16_alu_check.pynow models the contract (one rounding, ties to even, exact rational reference) and still holds the four #2073 words4e33,d55b,450f,4b9f.Commands run locally:
python3 -I tests/startup_gfx2hlsl_f16_alu_check.py(ok)python3 -I tests/startup_gfx2hlsl_f16_alu_draw_check.py(ok)python3 -I tests/startup_gfx2hlsl_cmp_class_f16_check.py(ok)python3 -I tests/startup_gfx2hlsl_compute_check.py(ok)AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T13:24:00Z.
RTX 3070 Ti run at
b87b5ae6, as you asked:startup_gfx2hlsl_f16_alu_draw_hardwarepasses. Thefxc v_fmac_f16 set 0mismatch from my run at37b610cadoes not reproduce in this run:The log has 26
fxcand 26dxcresult lines (13 ops x 2 sets), includingv_fmamk_f16andv_fmaak_f16; nodebug_errors=line has a nonzero value. CPU, control and WARP tests (ctest --preset dev --no-tests=error -j1 -R 'f16_alu' -E '_hardware$' -V):100% tests passed, 0 tests failed out of 3.The C++ binaries were built at
37b610ca; between37b610caandb87b5ae6onlytools/startup/gfx2hlsl.pyandtests/startup_gfx2hlsl_f16_alu_check.pychange, so I did not rebuild. One hardware run, one card and one driver. The drawn inputs have no Inf or NaN operand, so the binary32 path of the newfma16owas not drawn.Environment: NVIDIA GeForce RTX 3070 Ti (
10de:2482), driver 32.0.16.2101, Windows 11 build 26300.9974, Debug build, fxc 10.0.26100.9457, dxc 1.9.2602.17, D3D12 debug layer enabled; the hardware run was serial on an idle machine; 0 capability skips (77).Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:45:13Z.
Thanks for the RTX 3070 Ti run.
Result: at
b87b5ae6(which is the current head)startup_gfx2hlsl_f16_alu_draw_hardwarepasses. Thefxc v_fmac_f16 set 0mismatch seen at37b610cais gone: fxc and dxc both read4e33,d55b,450f,4b9f. All 26 fxc and 26 dxc lines are present,debug_errorsis 0, and the CPU/control/WARP tests pass 3 of 3.What this means for the PR: the integer
fma16_rnecorrection infma16ofixes the NVIDIA case. No further code change is needed from this comment.Still not covered, as you noted: one card, one driver, and no Inf/NaN operand drawn, so the binary32 path of
fma16ois not exercised on hardware. Your point about not rebuilding the C++ binaries is fine, since onlygfx2hlsl.pyand the Python check changed between the two heads.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:50:54Z.
Closing this PR for now to keep the Windows test queue short. Nothing is wrong with it and the branch stays as it is.
We will reopen it once the queue is clear, re-run the Win2025 WARP checks at its head, and post the result here.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T22:58:07Z.
Post-merge audit of
f03cf16f: seven mutations across the binary16 operand path, five caughtThree fixtures, one of them a draw-level check that the conversion really produces four lane results, so this PR is not
only text comparison.
half_operand/ theVEC_Hdispatchi16sign extension replaced by zero extensionu16mask dropped (float(src)instead offloat(src & 0xffffu))VEC_Hdispatch removed (half ops stop going throughhalf_alu)neginitialisedTrue(every operand treated as negated)f16tof32(src & 0xffffu)abs(…)wrapping droppedreturn '(-%s)' % text if neg else text→return text)The five catches are the ones that change numbers: sign extension, the low-half mask, the dispatch, the negation
predicate, and reading the half through
f16tof32rather than reinterpreting the bits asfloat.Why the two survivors are worth naming
They are the two halves of the same modifier handling, and each is only reachable through a specific operand spelling:
abs—absolute = tok.startswith('|') and tok.endswith('|'). No fixture passes an operand spelled|s0|, sowrapping in
abs(…)is unconstrained. Dropping it means an absolute-value modifier is silently ignored, and the resultis wrong by a sign with nothing to show for it.
neg = Trueis caught (the predicate), but applying it is not. So the fixture never has anoperand that is both non-inline and negated: for an inline constant the code goes down the
tok in FLOATSbranch andreturns before line 3240, and for a plain register the fixture apparently never writes
-s[N].Together those two mean the modifier application at the end of
half_operand—absthen-— has no case whereeither actually runs on a register operand. One case with
-|s0|(negated absolute, exercising both in the documentedorder) would pin both at once.
Note on my own anchors
Four of my first attempts missed on indentation: I copied leading spaces out of a
git diffexcerpt, where the+prefixshifts the column, and once I quoted a line I had already mutated in the previous pass. Anchors taken as
$l[N-1]fromGet-Content— the actual file line — all hit. In a 6 000-line file with nested expressions that is the only reliable way.AI-assisted; pure Python, Windows 11 build 26300, no DXC or GPU for the rows above.