shader: lower v_cvt_pk_u8_f32 for any binary32 source in gfx2hlsl #2459
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2467"
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 Ybucaille on 2026-11-25T17:28:46Z.
Follow-up of #2101 (item K of #2023). #2101 accepted
v_cvt_pk_u8_f32only where S0 is +0.0 or an integer in 1..255, because the RDNA ISA guides leaveflt32_to_uint8undefined. That refused the 208v_mul_f32→v_cvt_pk_u8_f32sequences @denn0x found (S0 =x * 255.0). The conversion is now measured on RDNA 2 hardware, so this PR lowers it for any S0.Measurement
A small HIP probe runs
v_cvt_pk_u8_f32 vD, vIn, vSel, vS2(S2 =0xa5c3e17b) on about 270 binary32 inputs under 8 byte selects (5,432 rows): zeros, integers, ties and their ±1 ulp neighbours, negatives, values above 255, infinities, NaN payloads, subnormals andx * 255.0values. It compares the readback with four models. Two RDNA 2 parts were tested, both under Windows with ROCm 7.2:Both returned the same 5,432 words in every mode:
s_round_mode 0xf(toward zero)s_denorm_mode 0Negative inputs, -0.5 included, give 0, and +inf gives 255. The byte select and the merge with S2 matched the ISA formula on every row. This is desktop RDNA 2, not the console. Thanks to the two testers from the Discord who ran it; I can post the probe and the raw JSON if a maintainer wants them.
Change
tools/startup/gfx2hlsl.py: the inserted byte is(F(S0) > 0.0 ? uint(round(min(F(S0), 255.0))) : 0u).> 0.0is false for NaN, negatives and both zeros, so they take0uwithout relying on howmin/maxtreat a NaN operand. HLSLroundis DXILRound_ne. The merge and the byte select are unchanged. S0 may be a VGPR, an SGPR or a constant. Modifiers, a wrong operand count, a non-VGPR destination and a register range stay refused.s_round_mode,s_denorm_modeands_setreg_b32were already refused.s_setreg_imm32_b32was silently accepted. It is now refused when its immediate puts a non-zero value in MODE[1:0] (HW_REG_MODEat offset 0 or 1, whole-register writes included), and still accepted when it writes 0 there or touches other fields.byte_float_vproof and thepk_u8_s0_contractfixture switch existed only for the restricted domain and are removed.docs/experiments.md: the startup paragraph now gives the measurement and the MODE rule.tools/compute_store_aot.pykeeps its restricted set, and generalising it is a separate follow-up.Test
tests/startup_gfx2hlsl_cvt_pk_u8_check.py:> 0.0guard removed (NaN not mapped to 0, negatives wrapped), a wrong byte shift, S2 dropped.s_round_mode,s_setreg_b32,s_setreg_imm32_b32with a non-zero rounding field. A MODE write with round bits 00 is accepted.roundwithtruncin the lowering fails the check.Verified here (Linux)
The new check (no arguments and
binarywith llvm-mc), everytests/startup_gfx2hlsl_*_check.py,tests/startup_translator_check.py,tests/docs_links_check.py,tests/bidi_controls_check.pyandgit diff --check. The D3D12/WARPdrawmode does not run on Linux, so the HLSL form has not been compiled by dxc here; the CTest entries need a Windows run.Refs #2023 (item K), follows #2101
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T17:46:36Z.
Six mutations: two caught, one provably equivalent, three I could not reach
Baseline on this branch:
tests/startup_gfx2hlsl_cvt_pk_u8_check.pyexit 0 (mutation changes draw set 0,accepted forms translate,refused forms stay refused), and all 63tests/startup_gfx2hlsl_*_check.pypass.s_setreg_imm32_b32MODE handling& 3→& 0hwreg((HW_REG_MODE|1)…)match narrowed toHW_REG_MODEgroup(2) < 2→< 3<< offsetdropped from the bits testgroup(3) or 32→or 1if False:)The offset one is equivalent, and the reason is worth a comment
< 2→< 3letsoffset == 2through. That cannot change the outcome: the test is((value & mask) << offset) & 3, so at offset 2 the masked value is shifted past bits 1:0 and the test is always zero — the write cannot reach MODE[1:0] whatever the immediate is. So the guard is stricter than it needs to be, and that is fine; but a reader will wonder why< 2and not<= 2, and the answer is one sentence.The three I could not reach
<< offset: atoffset == 1this starts testing bits 0:1 of the unshifted immediate instead of bits 1:2, which changes which writes are refused. My inputs did not reach it — the fixture'shwreg(...)forms presumably use offset 0 or omit the field.32→1: with the field omitted the mask collapses to one bit, sovaluewith bit 1 set would be accepted where it should be refused. Same reachability problem.s_setreg_imm32_b32with a non-hwreg(...)first operand returnsNone(accepted, does nothing) instead of refusing. That is the one I would pin first, because it is the one a title's own disassembly could plausibly hit — a hardware-register write the model does not understand would be silently dropped rather than counted as a refusal in the census.I am reporting these as unexercised rather than as gaps: two of the three need an immediate whose MODE-field bits are set at a non-zero offset, and I did not construct one.
What the two caught rows establish
& 3→& 0is the difference between "the write leaves MODE[1:0] at nearest-even" and "the write touches the rounding mode at all", so the fixture is checking the actual hazard rather than the shape of the code. And acceptinghwreg(1, …)as well asHW_REG_MODEmatters because both spellings appear in disassembly; narrowing the match is caught, which means a refused form exists for it.The
docs/experiments.mdline change (one line) is what makes this reproducible for the next reader: the rounding-mode assumption is exactly the kind of thing that gets silently violated by a later conversion lowering.AI-assisted; the six mutations were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by Ybucaille on 2026-11-25T18:12:24Z.
Thanks @ChazY11, useful mutations. All three survivors are now pinned in
52be6eb7, plus the comment you suggested:<< offsetdropped: killed by the acceptedhwreg(HW_REG_MODE, 1, 2), 2. Its value lands on bits 2:1 and leaves bit 0 clear, so it passes; the unshifted value would put 2 in bits 1:0 and refuse it. The refusedhwreg(HW_REG_MODE, 1, 1), 1(sets bit 1) and the acceptedhwreg(HW_REG_MODE, 1, 1), 0cover offset 1 in both directions.hwreg(HW_REG_MODE), 2.hwreg(...)" refusal disabled: killed by the refused raw SIMM160x1801, 3.< 2→< 3: as you say, equivalent. The code now says the bound is a shortcut, since an offset of 2 or more cannot reach MODE[1:0].Each mutation was applied, run and reverted here: the three fail the check, the equivalent one still passes. The check passes with no arguments and in
binarymode (the new spellings assemble with llvm-mc), and alltests/startup_gfx2hlsl_*_check.pypass.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T19:44:08Z.
Review: CHANGES_REQUESTED
Review of head
52be6eb7.Verdict: the lowering matches the stated model on FXC and DXC, on WARP and on an AMD adapter, and every semantic mutant I tried was killed. Two things block merge: a conflict with
main, and a missing cache-revision bump (P2). One more P2 is about reproducibility.Findings
main.git merge-tree --write-tree --name-onlyreports a content conflict intools/startup/gfx2hlsl.py, at thes_setreg_imm32_b32hunk.main(#2445) addeds_waitcnt_depctrand an LLVM comment to the no-op tuple that still listss_setreg_imm32_b32.main's comment ands_waitcnt_depctr, keep your branch in front of the tuple, and drops_setreg_imm32_b32from the tuple.docs/experiments.mdauto-merges.mainalready accepts.main,v_cvt_pk_u8_f32 v1, 1.0, v3, v4emitsuint(clamp(F(0x3f800000u), 0.0, 255.0)). At this head it emits(F(0x3f800000u) > 0.0 ? uint(round(min(F(0x3f800000u), 255.0))) : 0u). The0constant and av_cvt_f32_ubyte0-proven register change the same way.s_setreg_imm32_b32 hwreg(HW_REG_MODE, 0, 2), 3also moves from accepted to refused.x2w-startup-shader-14and14cs.pending_keysskips keys that already have a manifest and bytecode, and skips older.failfiles whenskip_failedis set. So an existing cache keeps translations made under the old clamp and MODE assumption, and keeps the earlier refusals of computedS0. Thes_brev_b32fold and the mad lowering bumped the revision for HLSL-only changes like this.x2w-startup-shader-NandNcstogether, since compute shares the handler. That meanspipeline_key/compute_keyingfx2hlsl.py,src/startup_stubs/d3d12_draw.inl, the tests that hash the string (tests/startup_compute_request_offline_check.py,tests/startup_gfx2hlsl_buffer_store_check.py,tests/startup_indirect_draw_indexed.cpp) anddocs/startup-runtime.md.docs/experiments.mdnames one RX 6900 XT while the description says two devices matched.PGM_RSRC1.FLOAT_MODE, which the translator doesn't decode. Only explicits_setreg_imm32_b32writes are checked.x > 0.0tofcmp fast ogt, so NaN → 0 on the DXC path relies on the driver honouring the ordered compare despitefast.lt l(0.0), v, which is false for NaN, so the FXC path is robust in the bytecode.((asuint(S0) - 1u) < 0x7f800000u), is true exactly from +subnormal to +inf and false for ±0, every negative and every NaN, so fast-math can't fold it.evaluate()rewrites the?:with a regex, so any textual change to the guard,roundorminfails as aSyntaxErrorrather than as a mismatch against a measured row. The semantic catches come from the draws.disasm_rdna2prints some literals as floats (1.0,0.5). A MODE write with such a literal is refused as "writes MODE[1:0]" even when bits 1:0 are clear. That refuses in the safe direction; only the message is wrong.What I checked
ps_5_1) and DXC (ps_6_0) on WARP and on the RX 9070 XT.S0is newly accepted and not drawn, so I compiled the emitted expression directly. FXC and DXC fold NaN, −0.0, −inf, 0.5, min-subnormal, 254.5, 255.5, 2.5 and 0.5+ulp to the model's bytes. DXC keeps +inf as a run-timeFMin, which gives 255.hwreg(HW_REG_MODE, 1, 2), 2,hwreg(HW_REG_MODE, 1, 1), 0/1,hwreg(HW_REG_MODE), 2and0x1801, 3forms. My own MODE-refusal mutant is killed.Commands executed (Windows 11 build 26300; AMD Radeon RX 9070 XT 1002:7550, driver 32.0.32029.2010, debug layer enabled; WARP 10.0.26100.9818; DXC/FXC from SDK 10.0.26100; llvm-mc 15.0.7)
binaryfixture words match llvm-mc)_warp_hardware(RX 9070 XT)startup_gfx2hlsl_*checks,startup_translator_checkThere were no capability skips. RDNA 4 is not the console GPU: these draws show that the desktop HLSL computes the stated model, not what the console instruction does.
Negative control
Each mutation is one line of the lowering, reverted after its run:
round→trunca1b201d4readsa1b200d4)0f1eff3creads0f1e003c)x > 0.0→!(x <= 0.0))8900cdefreads89ffcdef)clamp(x, 0, 255)refused forms stay refused)Without
/Gis, FXC folds the NaN mutant into the correct code, so the DXC draw is the one that pins the NaN guard. That's one more reason to consider the integer guard.Once
mainis merged and the revision is bumped, I'll rerun the draws on the new head.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ybucaille on 2026-11-25T19:59:32Z.
Thanks @MasterDomino, thorough review. Everything is addressed at
c93b94c5:mainmerged in (b0437dc2, then againc93b94c5). The tuple keepsmain's comment ands_waitcnt_depctr, the branch handless_setreg_imm32_b32in front of it, and the tuple no longer listss_setreg_imm32_b32.x2w-startup-shader-15/15cs(3089b4ad). The bump coverspipeline_key/compute_key,d3d12_draw.inl,startup_indirect_draw_indexed.cpp,startup_resource_retire_title.cpp,startup_gfx2hlsl_buffer_store_check.pyandstartup_compute_request_offline_check.py(precomputedKEYrecomputed), plusdocs/startup-runtime.md.tools/rdna_cvt_pk_u8.py(893b79fc). The raw output is intests/data/rdna_cvt_pk_u8_measured.tsv, one row per input and select (5,432 rows, 312 KB). Its header names both devices, gfx1030 (RX 6900 XT, Adrenalin 26.11.2, ROCm 7.2) and gfx1036 (integrated GPU, ROCm 7.2, driver not reported), both on Windows. They returned identical rows, which was checked before the file was written. A rerun writes the same format underout/and diffs it against the stored file. The cvt_pk_u8 check now replays all stored rows without a GPU:defaultagainst the nearest-even model,rtzagainst truncation, and the translated HLSL againstdefault. Negative controls are checked too: truncation againstdefaultgives 1,512 mismatches, and half-away, wrap and no-select also mismatch.docs/experiments.mdgives the rerun command and requirements. I have no AMD GPU here, so the GPU path of the in-repo version has not been run;--helpand--self-testhave.docs/experiments.mdkeeps "console result unmeasured" and now names both assumptions: the probe ran as compute with HSA defaults (IEEE mode on), and the initial rounding mode comes fromPGM_RSRC1.FLOAT_MODE, which the translator does not decode. It also names both devices.0d487446):((bits - 1u) < 0x7f800000u) ? uint(round(min(F(bits), 255.0))) : 0u, applied to the raw word for VGPR, SGPR and constant sources. The test checks the open bounds (0x1,0x7fffff, +inf) and the closed ones (±0, negatives, NaNs).evaluate()now fails with "lowering text changed, evaluator cannot parse" instead of a bareSyntaxError. The semantic catches still come from the draws, so that part is unchanged.39e0657d). Sohwreg(HW_REG_MODE), 1.0andhwreg(HW_REG_MODE, 0, 2), 0.5are accepted, while1.0e-45(bit 0 set) is refused as a MODE[1:0] write. An immediate that can't be read is refused as "immediate not decoded".Linux, at
c93b94c5: alltests/startup_gfx2hlsl_*_check.py,startup_compute_request_offline_check.py,docs_links_check.pyandbidi_controls_check.pypass (73 files, 0 failures), andgit diff --check origin/main...HEADis clean. Draws are left to the Windows CI and to your rerun.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T20:15:04Z.
Review: APPROVED
Approved at head
c93b94c5. This replaces my change request from52be6eb7; every item is addressed, and I re-verified them at this head.c93b94c5mainmain's comment ands_waitcnt_depctr, ands_setreg_imm32_b32is handled in front of it. The head merges cleanly with currentmain.x2w-startup-shader-15/15csinpipeline_key/compute_key,d3d12_draw.inland the hashing tests.tests/startup_compute_request_offline_check.pyandtests/startup_gfx2hlsl_buffer_store_check.pypass.tools/rdna_cvt_pk_u8.py, using only the standard library plusctypes/HIP; its onlysubprocesscall runs local clang/llvm-mc/ld.lld, with no network. The raw rows are intests/data/rdna_cvt_pk_u8_measured.tsv, whose header names both devices.--self-testpasses here, and the check replays every stored row without a GPU.docs/experiments.mdkeeps "console result unmeasured" and names both assumptions (compute with HSA defaults, andPGM_RSRC1.FLOAT_MODEnot decoded)((bits - 1u) < 0x7f800000u)on the raw wordRe-verification (Windows 11 build 26300; AMD Radeon RX 9070 XT 1002:7550, driver 32.0.32029.2010, debug layer enabled; WARP 10.0.26100.9818; DXC/FXC from SDK 10.0.26100)
16 draws matched, 2 mutated draws read their mutated words, with FXCps_5_1and DXCps_6_0. There were no skips.Negative control for the new guard
I widened the integer guard to
((bits - 1u) < 0xffffffffu), so NaN and negative words reachround(min(…)). The draw then fails on FXC:expected=55aa33ff,0f1eff3c,8900cdef,00ffee11 actual=55aa33ff,0f1eff3c,89ffcdef,ffffee11, so the NaN lanes read 255. With the old float guard, the equivalent NaN mutant survived on FXC because FXC folded it into identical bytecode. The integer form closes that. After reverting, all 3 pass.Notes (not blocking)
15is also the revision #2010 (v_fract_f32) moves to, and #2095 (v_med3_f32) moves to16/16cs. Whichever of those lands after this one needs to take the next free number. I'll mention it on both.tools/rdna_cvt_pk_u8.py.Merge checks: required CI passed at this head. The
/O2and coverage skips come from explicit workflow conditions. There are no unresolved review threads. I read @ChazY11's mutation report and the author's replies in full.AI-assisted; commands listed were executed locally.