Recompiler: simplify direct DXIL binary16, D16, and buffer store lowering #3650
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3650"
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 ivall/xcl. Originally posted by Kalmarv on 2026-12-02T03:43:32Z.
Compacts emitted direct-DXIL IR for binary16/float/D16 conversions, formatted buffer stores, and buffer address/bounds lowering across 4 files (
+447/-242, 2 commits), without changing RDNA2 rounding, NaN, or bounds semantics.What is in it
lower_wide.rs,prelude.rs,lower_mem.rs,core_tests.rs):f32_to_f16_rnesubnormal magnitudes with a single native RNE add at0.5(2^-24binary32 ulp) and round normal significands with a retained parity bit (51 → 21SSA ops;-72,820 Bacross 329 validated shaders).f32_to_f16_rmrounding fromf32_to_f16_rneby comparing widened unsigned magnitude bits (-13,340 B).IsNaNchecks incvt_u32(fcmp ule <= 0.0) andf16_rtz(orderedfcmp ogtis already false for NaN and zero halves, also dropping the zero-magnitude guard) (-51,888 Bcombined; builds on @ramon-victor's #2289, which is currently conflicting onmain).-8,544 B).lower_mem.rs):-63,872 B), and rotate dynamic-format four-component stores through one numeric encoder loop (-146,160 B).-114,308 B) and emit runtime byte-count fragmented stores as a bounded word loop while keeping straight-line stores for constant counts (-77,400 Bmain,-72,780 Bauxiliary dispatch pieces).-10,640 B), check the carried scalar flat-read window validity once (-2,992 B), and reuse the 32-bit lane-stride product in buffer window calculations whenIDXENis clear (-11,248 B).~146.6 KBfrom Commit 1 +~426.6 KBmain /72.8 KBauxiliary from Commit 2) across the individual before/after measurements on 329 validated shaders.4f39cd191vsmain 1c8b55147):1267vs1387frames shown (91.3%),60 60 61presents per 2 s, peak commit15112 MBvs17480 MB(-2368 MB/-13.5%),0device removals,0crashes.Tested
rustc 1.99.0 (b940084d7 2026-09-28), DXC1.9.2609.5(debug + release).scripts/test.ps1(cargo build --workspace,cargo bench -p xcl --bench '*' -- --test,cargo test --workspace -- --test-threads=4): 2921 passed, 0 failed, 51 ignored across all 43 test targets (24 SKIPPED GPU tests on this adapter: 22no wave64, 2no heap windows).gpu::dxil::core_testsandgpu::dxil::lower_mem::stage_guard_tests(all passing with DXC1.9.2609.5validation).cargo build --release -p xcl: clean (39 warnings, 0 new vsmain).Regression run
Baseline (
main):PR head:
Not tested
XCL_HEAP_WINDOWS=1GPU tests (2 skipped under defaultscripts/test.ps1).main+ this PR alone (individual 329-shader corpus byte savings were recorded during the offline validation sweep before cherry-picking).AI use
GPT-6) wrote the individual binary16/float/D16 and buffer/store lowering improvements and unit tests during the offline DXIL size sweep.Gemini 4 Neon) audited and cherry-picked themain-independent subset ontomain, adapted the directed binary16 unit test for inlinef32_to_f16_rne, rancargo build --release,scripts/test.ps1, and the 60 s smoke run, and drafted this description.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T08:59:53Z.
Review (head
4f39cd19, diff read, not a merge-check):f32_to_f16_rne(lower_wide.rs~708) is the same instruction sequence as thexcl.f16.rnebody in #3654 (module.rs~845). Both PRs changefmt_encodeto take the width (lower_mem.rs~1042), but they remove its four copies in different ways: this PR runs it in a loop (encode_dynamic_components~1136), and #3654 makes it an outlinedxcl.fmt.encodecall. Both also rework the carried-window check inscalar_flat_read(~1244). The two PRs conflict incore_tests.rs,lower_mem.rsandlower_wide.rs. One of them has to drop these parts. This PR is the smaller place to keep them.cvt_u32/f16_rtzchange inprelude.rs~20/~62 is the change from #2289, plus the removal of the zero-magnitude guard. #2289 comes with a GPU value test, and this PR does not. One of the two should be closed, and the test should be kept.core_tests.rstests and most of thelower_mem.rstests count instructions in the disassembly (onefadd … 0.5, onecreateHandleFromHeap, onelegacyF32ToF16). The only value test of the rewritten RNE and directed conversions is the existing 8-case GPU test (core_tests.rs~915). It has no NaN payload, ±inf, -0, min-normal boundary, subnormal tie or +inf/-inf mode case. A GPU parity test against the unchanged HLSLf32_to_f16_rm(recomp/emit.rs~7003) over those cases, in all four modes, would pin the behaviour down. Also,a_fragmented_store_selects_its_heap_window_once(~2934) never callsvalidate, and it is the only test of the new single heap-window wrapper inst_bytes(~548).Fine: By reading, the new RNE sequence matches what main computes. The subnormal rounding through the 0.5 add relies on f32
faddrounding to nearest even, whichround_tests.rsalready assumes. Ties, 65520 → inf, the NaN payload and quiet bit, and the sign all match. The directed modes step from the nearest result correctly: overflow gives 0x7BFF or inf, the smallest subnormal is used when rounding away from zero, and NaN and inf are left alone. The D16 UNORM/SNORM 32-bit quotient matches main's 64-bit division for widths 1–16, including y = 0 (FirstbitHi of 0 is -1, so the shift is capped at 24) and y = dmax (the carry gives 0x3C00). The shared byte limit gives the same result for each dword as before, including when the offset wraps.pack_componentsis tested against every BUF_FORMATS layout, and only the format's bytes are stored. The straight-line and loop forms of the byte store keep the word and spill order and the 16-byte cap. The 32-bit lane × stride product cannot overflow. D16 store packing is unchanged.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T10:25:32Z.
merge-check
base:
1c8b551475head:
4f39cd1918merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2921 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3650#issuecomment-7340218502 and its findings stand. Most serious: two changes in one PR; its f16 rounding and
fmt_encodework is redone differently in #3654 and repeats open #2289.performance: 60 s smoke of a retail title, main
1c8b5514vs this merge (4f39cd19): 1652 -> 1424 frames shown (86 %; last presents per 2 s: 61 60 60), peak commit 18228 -> 15835 MB, 0 device removals, 0 crashes. This run is lower than most today: the PC was in use at the time (a browser and Discord running), and main's own runs on this machine today ranged from 1370 to 1652 frames, so the difference isn't clearly from this PR.verdict: safe to merge as code (no bug found, the suite passes, the smoke run has no crash), but it is two changes in one PR and its f16 rounding and
fmt_encodework overlaps open #2289 and #3654, so only one of those should landhardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by vasanex on 2026-12-02T10:52:50Z.
merge-check
base:
1c8b551475head:
4f39cd1918merge: clean (
git diff --checkclean)build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2921 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same tests as on the base (2909 passed there); failing: none. The twelve new tests ran and passed.
review: whole diff read (4 files, +447/-242). No correctness finding. Checked by reading: (1) the new
f32_to_f16_rnegives the old results for zero, the tiny range (tie at 2^-25 to zero), subnormals up to the smallest normal, normals with a tie to even and a carry into the exponent, overflow to infinity from 0x477ff000 on, and the NaN payload; (2) the directed modes step the nearest result by one towards or away from zero from an unsigned compare of the magnitudes, which also turns an overflow to infinity into 0x7bff when rounding towards zero and leaves infinities and NaN alone; (3) the normalised D16 quotient: for a code y below dmax = 2^we - 1 the quotient stays inside the binade the shift picks, soexp + sigis right with sig up to 2048, y = dmax gives 0x3c00, y = 0 gives 0 through the capped shift, and adding dmax / 2 before the division rounds to nearest without a tie; (4)FULeand the bareFOGtincvt_u32/f16_rtzequal the old guarded forms; (5) the word loop infor_store_wordsstores the same words, masks and spills as the four unrolled copies, and the rotating encoder returns X, Y, Z, W after four turns; (6)pack_componentsrelies on absent components having width 0, which the all-formats test confirms for the table. One thing a reviewer should know: the old round-to-nearest conversion was integer-only, the new one takes the subnormal significand from one hostfaddat 0.5, so it depends on the host adding with round to nearest even; the existing GPU value test passes on this card, and there is no wider value test (as the review above says). The overlap with #3654 and #2289 named there stands; I did not test the combinations. The wave64 paths did not run here (wave32 card).performance: the default path gets a loop for runtime-sized byte stores and runtime formats in place of four unrolled copies. 60 s smoke, release, same title and settings, machine otherwise idle: base 1608 frames, peak commit 19911 MB; merged tree 1698 frames, 17980 MB (90 %), presents 61 60 60, 0 device removals, 0 crashes. So the 86 % of the first check does not show here. Not measured: translation time and shader size.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0 / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T13:52:25Z.
Thanks @Foniass and @vasanex for the review and merge-checks! Leaving
4f39cd191untouched whileready-to-mergeis set; we will drop the overlappingf16RNE /fmt_encode/scalar_flat_readhunks from #3654 when rebasing #3654 ontomainso only one PR carries them (and note #2289's GPU value test can be kept as a follow-up or folded in if preferred).Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:17:29Z.
Code review location:
crates/xcl/src/gpu/dxil/prelude.rs; line 21Medium: Not certain about this one, since it depends on how drivers treat float compares in these modules:
cvt_u32drops the explicitdx.op.isSpecialFloatIsNaN check and relies on the unorderedfcmp ulebeing true for NaN, andf16_rtz(prelude.rs:69) drops itsisnan(f)guard and relies on the orderedfcmp ogtbeing false for NaN. These modules don't set DisableMathRefactoring by default. If a driver treats the compares as NaN-free,v_cvt_u32_f32on NaN returns ftoui(NaN) (driver-defined) instead of 0, andv_cvt_pkrtz_f16_f32or a 16-bit FLOAT store turns a NaN encoded as 0x7C01/0xFC01 into ±infinity. The HLSL path still has bothisnanguards, so the two paths can disagree on NaN inputs. Keeping the explicit IsNaN checks costs a few instructions and removes the dependence.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:17:29Z.
Review: COMMENTED
Review of
4f39cd19: 1 finding(s) (1 medium), most severe first.crates/xcl/src/gpu/dxil/prelude.rs:21: Not certain about this one, since it depends on how drivers treat float compares in these modules:cvt_u32drops the explicitdx.op.isSpecialFloatIsNaN check and relies on the unorderedfcmp ulebeing true for NaN, andf16_rtz(.... (inline)These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T18:17:31Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T18:26:29Z.
Thanks @bmjask for checking this — leaving
4f39cd191untouched (2safe to mergemerge-checks +ready-to-mergeset):Op::Cmp(crates/xcl/src/gpu/dxil/module.rsABBREV_CMP2) emits strict IEEEfcmpwith no fast-math flags (nnan/fastare never emitted onfcmp).fcmppredicates withoutDisableMathRefactoringfor all 16f32v_cmp_*instructions (lower_valu.rs::float_compareusesPred::FOGt,Pred::FULe,Pred::FOrd,Pred::FUno, etc.). The HLSL prelude (recomp/emit.rs) keeps explicitisnanbecause HLSL source has no unordered<=operator and DXC emits fast-math flags by default.cvt_u32(Pred::FULe) +f16_rtz(Pred::FOGt) simplification was also verified on GPU across±0.0,±Inf,qNaN, andsNaNin #2289 (gpu_direct_cvt_u32_and_pkrtz_drop_isnan_and_match_isa); happy to bring that GPU value test over as a follow-up once this lands.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.