Repository navigation
shader: lower v_cvt_pk_u8_f32 for any binary32 source in gfx2hlsl - #2467
Conversation
flt32_to_uint8 is undefined in the RDNA ISA guides, so the startup translator only accepted +0.0 or an integer in 1..255. It was measured on two RDNA 2 parts (RX 6900 XT gfx1030 and a gfx1036 iGPU, Windows, ROCm 7.2): round to nearest even, saturate to 0..255, NaN and negatives to 0, with no difference under s_denorm_mode 0. Round toward zero in MODE makes it truncate, so s_setreg_imm32_b32 that puts a non-zero rounding mode in MODE[1:0] is now refused instead of ignored. The byte_float_v proof and the pk_u8_s0_contract fixture switch only existed for the restricted domain and are removed. Refs R0Y00#2023 (item K), follows R0Y00#2101
Six mutations: two caught, one provably equivalent, three I could not reachBaseline on this branch:
The offset one is equivalent, and the reason is worth a comment
The three I could not reach
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
The AI-assisted; the six mutations were run locally on Windows 11 build 26300.9974. |
Three mutations of the s_setreg_imm32_b32 MODE handling reported on R0Y00#2467 survived: dropping the offset shift, defaulting the width to 1 and skipping the non-hwreg refusal. Add accepted and refused forms that kill each one, and note that the offset < 2 bound is a shortcut.
|
Thanks @ChazY11, useful mutations. All three survivors are now pinned in
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 AI-assisted. |
MasterDomino
left a comment
There was a problem hiding this comment.
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
- Blocking: conflict with
main.git merge-tree --write-tree --name-onlyreports a content conflict intools/startup/gfx2hlsl.py, at thes_setreg_imm32_b32hunk.- Cause:
main(#2445) addeds_waitcnt_depctrand an LLVM comment to the no-op tuple that still listss_setreg_imm32_b32. - Resolution: keep
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.
- Cause:
- P2: the startup cache revision is not bumped, although the emitted HLSL changes for forms
mainalready accepts.- The change: I translated the same encodings with both trees. On
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. - Why it matters: both trees still hash
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. - What would resolve it: advance both
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.
- The change: I translated the same encodings with both trees. On
- P2: the measurement can't be reproduced from the PR.
- The source: the test docstring cites the probe as part of another repository, without a link, and the raw JSON isn't published.
- Missing details: the second device has no driver version, and
docs/experiments.mdnames one RX 6900 XT while the description says two devices matched. - The request: could you attach the probe source and the raw output here, or add the probe as a source-built tool, so another contributor with an RDNA 2 adapter can rerun it?
- P3: what the measurement covers.
- Devices and mode: gfx1030 and gfx1036 are GFX10.3 parts, and the probe ran as compute with HSA launch defaults (IEEE mode on). Pixel shaders normally run with IEEE mode off.
- Rounding mode: the initial rounding mode comes from
PGM_RSRC1.FLOAT_MODE, which the translator doesn't decode. Only explicits_setreg_imm32_b32writes are checked. - Request: the docs already say the console result is unmeasured. Please keep that, and perhaps name these two assumptions.
- P3: the DXC guard depends on the driver.
- DXC: compiles
x > 0.0tofcmp fast ogt, so NaN → 0 on the DXC path relies on the driver honouring the ordered compare despitefast. - FXC: emits
lt l(0.0), v, which is false for NaN, so the FXC path is robust in the bytecode. - Optional fix: an integer guard,
((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.
- DXC: compiles
- P3: test clarity.
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. - Nit:
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
- The byte select and merge are unchanged from #2101, and the insert follows the PR's model.
- The draw sets cover ties and their ±1 ulp neighbours, negatives, −0.0, 256 and 255.5±ulp, ±inf, qNaN, sNaN, an all-ones NaN, and positive subnormals. All 32 sets matched with FXC (
ps_5_1) and DXC (ps_6_0) on WARP and on the RX 9070 XT. - Constant
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. - @ChazY11's three survivors are pinned at this head with the
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)
python -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py
python -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py binary <llvm-mc 15.0.7>
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cvt_pk_u8_(controls|warp|hardware)$' -V
python -B tests/startup_gfx2hlsl_{cvt_alu,handler_arity,operand_count,image_arity}_check.py| Test | Result |
|---|---|
controls, binary |
pass (fixture words match llvm-mc) |
_warp |
pass: 16 draws, 2 mutated draws, 0 debug errors |
_hardware (RX 9070 XT) |
pass: the same |
all 62 startup_gfx2hlsl_* checks, startup_translator_check |
pass |
There 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:
| Mutant | FXC draw | DXC draw |
|---|---|---|
round → trunc |
killed (a1b201d4 reads a1b200d4) |
killed |
| saturate at 256.0 | killed (0f1eff3c reads 0f1e003c) |
killed |
NaN → 255 (x > 0.0 → !(x <= 0.0)) |
survives: FXC emits identical bytecode | killed (8900cdef reads 89ffcdef) |
| insert into the wrong byte | killed | killed |
guard removed, clamp(x, 0, 255) |
survives | survives (equivalent on D3D, where min/max return the non-NaN operand) |
| MODE[1:0] refusal disabled | n/a | n/a (controls fail: 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 main is merged and the revision is bumped, I'll rerun the draws on the new head.
AI-assisted; commands listed were executed locally.
The v_cvt_pk_u8_f32 lowering changes the HLSL emitted for forms that main already accepted (constant and proven-register S0, MODE writes), so a cache built under revision 14 would keep the old clamp lowering and the earlier refusals. Draw and compute revisions move together.
DXC compiles the float guard to fcmp fast ogt, so the NaN result relied on the driver honouring the ordered compare. (bits - 1u) < 0x7f800000u is true exactly from the smallest positive subnormal to +inf, so fast-math cannot fold it. The test evaluator now fails with a clear message when the lowering text no longer matches. The docs name both devices that matched (gfx1030, gfx1036) and the two open assumptions: IEEE mode and the initial rounding mode from PGM_RSRC1.FLOAT_MODE.
disasm prints some inline constants as floats (1.0, 0.5). They were refused as writing MODE[1:0] even when bits 1:0 are clear. They now read as their binary32 bits, and an immediate that cannot be decoded is refused with its own reason. Refusal stays the safe direction.
The measurement behind the lowering could not be rerun: the probe lived outside the repository and the raw words were not attached. Add the probe as tools/rdna_cvt_pk_u8.py (builds its kernels with clang, runs through HIP, writes a compact TSV and compares it with the stored one) and store the output in tests/data/rdna_cvt_pk_u8_measured.tsv. The gfx1030 and gfx1036 runs gave identical rows, so one set is kept. The check now validates every stored row without a GPU: nearest-even for the default word, truncation for the rtz word, with negative controls from the other models. The docstring points at the in-repo probe.
|
Thanks @MasterDomino, thorough review. Everything is addressed at
Linux, at AI-assisted (Claude Code). |
MasterDomino
left a comment
There was a problem hiding this comment.
Approved at head c93b94c5. This replaces my change request from 52be6eb7; every item is addressed, and I re-verified them at this head.
| item from my review | at c93b94c5 |
|---|---|
conflict with main |
merged; the no-op tuple keeps main's comment and s_waitcnt_depctr, and s_setreg_imm32_b32 is handled in front of it. The head merges cleanly with current main. |
| P2: cache revision | x2w-startup-shader-15 / 15cs in pipeline_key/compute_key, d3d12_draw.inl and the hashing tests. tests/startup_compute_request_offline_check.py and tests/startup_gfx2hlsl_buffer_store_check.py pass. |
| P2: reproducibility | the probe is now tools/rdna_cvt_pk_u8.py, using only the standard library plus ctypes/HIP; its only subprocess call runs local clang/llvm-mc/ld.lld, with no network. The raw rows are in tests/data/rdna_cvt_pk_u8_measured.tsv, whose header names both devices. --self-test passes here, and the check replays every stored row without a GPU. |
| P3: scope | docs/experiments.md keeps "console result unmeasured" and names both assumptions (compute with HSA defaults, and PGM_RSRC1.FLOAT_MODE not decoded) |
| P3: DXC guard | integer guard ((bits - 1u) < 0x7f800000u) on the raw word |
| P3 and nit | clearer evaluator error; float-printed MODE immediates read as their binary32 bits |
Re-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)
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cvt_pk_u8_(controls|warp|hardware)$' -V
python -B tests/startup_compute_request_offline_check.py
python -B tests/startup_gfx2hlsl_buffer_store_check.py
python -B tools/rdna_cvt_pk_u8.py --self-test- CTest: 3/3 pass. On both adapters:
16 draws matched, 2 mutated draws read their mutated words, with FXCps_5_1and DXCps_6_0. There were no skips. - The other three commands pass.
Negative control for the new guard
I widened the integer guard to ((bits - 1u) < 0xffffffffu), so NaN and negative words reach round(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)
- Revision collision:
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. - Probe GPU path: nobody has run the in-repo probe's GPU path yet. The author has no AMD GPU, and this host has no HIP SDK. The stored rows come from the earlier run of the same probe. A contributor with RDNA 2 and ROCm could confirm it with
tools/rdna_cvt_pk_u8.py.
Merge checks: required CI passed at this head. The /O2 and 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.
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.