Repository navigation
Lower v_cvt_pk_u8_f32 in the startup gfx2hlsl translator - #2101
Conversation
VOP3A opcode 350 of the RDNA 1.0 and RDNA 2 ISA guides:
D = (S2 & ~(0xff << 8 * S1[1:0]))
| ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])
The guides leave flt32_to_uint8 undefined (rounding, bounds, NaN and
negative results are not stated), so the lowering holds only where
every reading agrees: S0 is +0.0 or an integer in [1, 255]. A constant
S0 outside that set is refused, as are modifiers, a wrong operand count
and a non-VGPR destination. A register S0 cannot be checked statically;
the HLSL clamp only keeps the conversion defined. This is the set that
tools/compute_store_aot.py already lowers (R0Y00#1546). S1 may be a register
or a constant; only its bits [1:0] are used.
The fixture is source-built and draws all four byte selectors with
S1 bits above [1:0] set, S2 words of four distinct bytes, and S0 from
0.0 to 255.0. Its negative controls (a lowering that loses S2, a wrong
byte position) are detected, and the check fails when the shift in the
translator is mutated. The D3D12 draw was not run here (Linux): the
controls and the llvm-mc reassembly were.
Refs R0Y00#2023 (item K)
AI-assisted.
Six mutations: four caught, two survivors — and one of them I expect to change the drawn word, so I would like your read before I claim anythingBaseline on this branch, both paths run here (Windows 11 build 26300.9974, DXC 10.0.26100.0):
The clamp survivor is fine, and here is why I am not calling it a gapUnder this lowering's stated contract the source is The mask survivor is the one I would like explained
correct: clear the low byte of Different word, and bit 8 of v5 = ((0x55aa33ccu & ~(0xffu << ((0x00000000u & 3u) << 3u))) | ((uint(clamp(F(0x437f0000u), 0.0, 255.0)) & 0x1ffu) << ((0x00000000u & 3u) << 3u)));so one of three things is true, and I could not settle which in the time I had:
If it is (2), then the draw half is not a control against lowering mutations at all, which would matter beyond this PR. If it is (1), ignore me. The thing this PR gets right that I want to write downRefusing a constant AI-assisted; the six mutations, both check modes and the HLSL dump above were run locally. |
Narrowing my own question: the mutant does reach the compiled pixel shaderI said I could not tell whether the draw half compiles the live lowering. That part is now settled, and it makes the survivor more interesting rather than So For your convenience, the case I computed by hand from your own first draw set: If that arithmetic is right, the mutated shader cannot produce the word I am not claiming your draw half is unfaithful — I am saying I could not explain the result with the evidence I have, and the next reviewer should not AI-assisted. |
Correction: the mask survivor is an equivalent mutant, and my hand computation was wrongI have to withdraw the question in my previous comment. I mutated only the insert side: ((v8 & ~(0xffu << ((v4 & 3u) << 3u))) | ((uint(clamp(F(v0), 0.0, 255.0)) & 0x1ffu) << ((v4 & 3u) << 3u)))
^^^^^^ this changed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ clear side untouchedThe clear mask is still In my previous comment I wrote Three things I got out of this, for the record rather than for this PR:
Sorry for the noise. The rest of the review stands: four of six mutations caught, and the clamp constant is the only thing I would still pin (with a text AI-assisted; the mutant text above is from the run I posted, re-read. Verified the follow-through before posting the correctionI said the mutation that would be a real gap is widening the clear mask, so I ran it: Both halves catch it, which is what a faithful pair of controls should do — and it is further evidence that my earlier survivor was equivalent rather |
|
Windows run of the requested draws for #2101, tested at head Environment
Command Results: 9/9 passed (the
Run by Claude (AI-assisted). |
R0Y00
left a comment
There was a problem hiding this comment.
The four in-domain draws and both mutations passed on WARP and RTX 4060 Laptop. The generic startup path still accepts an arbitrary register for S0 and clamps it, with no proof or explicit profile contract that it contains only +0.0 or integers 1..255. A computed fraction, NaN or negative value can now silently select an invented byte. Please restrict the default path to proven in-domain words, or add an explicitly selected fixture/profile contract with a rejection for unproven operands in ordinary translation. Keep the four selector checks and the dropped-S2 control.
Review of R0Y00#2101: the generic path accepted any register S0 and clamped it, so a computed fraction, NaN or negative value could silently select an invented byte, since the ISA leaves flt32_to_uint8 undefined there. A register S0 is now accepted only when proven +0.0 or an integer in 1..255: written by v_cvt_f32_ubyte0..3 (no modifier, straight-line code, entry exec mask) with no write since. Any other register is refused, unless the caller selects the Pipeline keyword pk_u8_s0_contract, which the fixture draws do; use of the contract is recorded in the notes.
|
Thanks for the review. Addressed in 1afe5a6. Default path: proven S0 only. In ordinary translation, a register S0 is now accepted only when its value is proven to be +0.0 or an integer in 1..255:
Anything else is refused with Explicit contract for the fixture. Kept: the four selector checks and the dropped-S2 control are unchanged. In the New controls: an unproven S0 is refused without the contract (three spellings, exact message), and the same words are accepted with it, with the note. Linux: the check (with AI-assisted. |
|
Thanks. One concern about the register path. In a private executable's pixel programs (decode only, aggregate counts):
So the precondition in the comment ("the program must keep it inside the set") does not hold for these programs: S0 is generally not an integer.
Two options:
|
|
Thanks, @denn0x. Our comments crossed: 1afe5a6 (pushed a few seconds before yours) is your option 1, slightly stricter. A register S0 is accepted without the explicit fixture contract only when it comes straight from On option 2, the only measurement I found is second-hand and not on RDNA. shadPS4 #5194 (shadps4-emu/shadPS4#5194, merged 2026-10-01) reports a hardware test on PS4 (GCN). Its findings:
The PR does not describe the test protocol. Nothing else I checked defines the conversion. LLVM has no semantics comment, no constant folding and no pattern for If that holds on RDNA, the truncating AI-assisted. |
Independent Windows rerun at
|
| Test | Result | Time |
|---|---|---|
startup_gfx2hlsl_cvt_pk_u8_controls |
Passed | 0.20 s |
startup_gfx2hlsl_cvt_pk_u8_warp |
Passed | 0.85 s |
startup_gfx2hlsl_cvt_pk_u8_hardware |
Passed | 2.70 s |
Environment, copied from the draw runs themselves:
WARP:
backend=warp debug_layer=enabled
adapter=Microsoft Basic Render Driver vendor=1414 device=008c
driver=10.0.19041.5794
Hardware:
backend=hardware debug_layer=enabled
adapter=NVIDIA GeForce RTX 3060 vendor=10de device=2503
driver=32.0.15.9636
Host: Windows 10 Pro 22H2, build 19045.6466; MSVC x64 Debug (dev preset); DXC 1.7.2308.16 (Windows SDK 10.0.26100.0).
Notes
- The debug layer was available and enabled on both adapters, and the test reported no debug-layer errors. No capability skip: every leg returned 0, so this is a pass and not a skip.
--no-tests=errormatched exactly the three expected names. Theinterp_f16half of the filter belongs to a different branch and is not registered here, so it selected nothing extra.- Not covered by this run: the
/O2(release) configuration. The workflow skipsbuild-and-test-releaseon pull requests (if: github.event_name != 'pull_request'), so nothing has exercised this change at/O2yet. - What this establishes: the new
v_cvt_pk_u8_f32lowering and its drawn pixel output agree with the fixture's expected words on WARP and on the RTX 3060 at this head. It says nothing about any other form or about title startup.
tedin7
left a comment
There was a problem hiding this comment.
Review of head 1afe5a69e748b408b7f38c84b0879abe85cd2d41. No blocking findings.
Summary: v_cvt_pk_u8_f32 is lowered only where S0 is +0.0 or an integer in 1..255. Constants are checked at translation. Registers must be proven by an unmodified v_cvt_f32_ubyte0..3 in straight-line, full-exec code, or the caller must select pk_u8_s0_contract. The merge expression matches the ISA text. Title and body are neutral and the CI jobs pass.
Checked
- Every VGPR write goes through
set_v(rgfor other emitters found none), so discarding the proof there covers all later writes, including loads and multi-register results. v_cvt_f32_ubyte*yields exactly float(0..255), so the proof is sound. It is skipped with modifiers, with branches, or when exec is not the entry mask.- The contract is off by default and is recorded in the pipeline notes when used.
- The
pipelinekwargs instartup_gfx2hlsl_lane_alu.pyare optional, so other families are unchanged. - The CMake and
windows.ymlentries sit next to the siblingcvt_aluentries.
Commands run locally on Linux (output summarised)
python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py-> all ok, rc=0 (translation controls only; no GPU draw was run locally).python3 -B tests/startup_gfx2hlsl_cvt_alu_check.py-> all ok, rc=0 (shared runner is unaffected).python3 tests/docs_links_check.py-> 18 files, 393 links OK.python3 tests/bidi_controls_check.py-> OK.git diff --check origin/main...HEAD-> clean.- Mutation 1: removed the
byte_float_vdiscard inset_v-> the "float multiply is refused" control fails. - Mutation 2: widened the constant bound from 255 to 256 -> "refused forms stay refused" fails.
- Mutation 3: dropped the
has_branchesguard -> "no proof in a program with branches" fails. - All three mutations were reverted with
git checkout -- ..
P1: none.
P2: none.
Nits
docs/experiments.mdis a long single paragraph. Splitting out the "proven register" rule would make it easier to read.getattr(self, 'byte_float_v', set()).discard(n)runs on everyset_v. Initialising the set in__init__would be simpler. This is not blocking.
Not run: Windows WARP/hardware draw (x2w_pipe.py win2025). This is a re-run of the CI Windows job, which passed on this head. A maintainer who wants a local GPU readback should run startup_gfx2hlsl_cvt_pk_u8_warp.
AI-assisted; commands listed were executed locally.
|
Thanks @rasptum for the RTX 3060 / WARP run, and @tedin7 for the review and the mutations. Nits:
That commit only changes AI-assisted. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head 73769831d758cf260248bfee694549dbe54480e0 (comment only, no approval).
Verdict: logic looks sound; one blocker, the branch conflicts with main.
Findings
- P1: GitHub reports
mergeable: CONFLICTING.git merge-tree --write-tree origin/main HEADreports a content conflict intools/startup/gfx2hlsl.py(windows.yml, CMakeLists.txt and docs/experiments.md auto-merge). Please rebase and re-run the checks below. - P2: none.
- Nit:
getattr(self, 'byte_float_v', set()).discard(n)inset_vallocates a throwaway set on every register write when the attribute is absent. Initialisingbyte_float_vin the Stage constructor would remove thegetattr/setdefaultindirection in both places.
What I checked
- The constant S0 gate (+0.0 or an integer in 1..255) matches what
tools/compute_store_aot.pyaccepts for the same opcode, and the PR text states the undefinedflt32_to_uint8behaviour honestly. - A register S0 is accepted only when it was written by
v_cvt_f32_ubyte0..3with no modifier, with straight-line code and full exec, and with no write since. Every write goes throughset_v, which clears the proof, including the fma_mix path. Otherwise it is refused unless the fixture-onlypk_u8_s0_contractis selected, and that choice is recorded in the pipeline notes. - The byte insert formula matches the ISA text: mask
0xff << 8*S1[1:0], merge into S2. - CI on this head: Linux checks, gfx1013 fixtures and Windows build all pass. The /O2 job and the coverage chart job were skipped. I did not run the Windows WARP job (
win2025), because the PR's own Windows CI job passed and the draw test is in that job.
Commands run locally
python3 tests/startup_gfx2hlsl_cvt_pk_u8_check.py->v_cvt_pk_u8_f32: 8 draw sets: ok,mutation changes draw set 0: okpython3 tests/startup_gfx2hlsl_lane_alu.py->accepted forms translate: ok,refused forms stay refused: ok,Self-test passed.python3 tests/docs_links_check.py->Checked 18 Markdown files and 393 links.python3 tests/bidi_controls_check.py->no bidirectional controls.git diff --check origin/main...HEAD-> clean- Mutation 1: forced the proof check to always fail (
src[1] not in byte_float_vreplaced byFalse). The tests failed withunproven S0 refused without the contract, so the mutation was killed. - Mutation 2: removed the proof-clearing discard in
set_v. The tests failed witha float multiply is refused without the contract, so the mutation was killed. - Both mutations were reverted with
git checkout -- ..
AI-assisted; commands listed were executed locally.
|
Merged Re-run locally on the new head: On the @tedin7, since you offered: could you run the Windows draws on The |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head 2487e8f8888c5f78acf8c06c183856b32aa85bf9 (comment only, no approval).
Verdict: the earlier P1 (merge conflict with main) is resolved. No blocking findings.
Earlier P1 check
- GitHub now reports
mergeable: MERGEABLE. git merge-tree --write-tree origin/main HEADreturns a clean tree, with no conflict intools/startup/gfx2hlsl.py.- The head is a merge of main into the branch. The diff against main is 6 files, +246/-6, the same scope as before.
CI on this head: Linux checks, gfx1013 fixtures and Windows build pass. The /O2 job and the coverage chart job were skipped.
Commands run locally on Linux (output summarised)
python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py-> all ok, rc=0python3 -B tests/startup_gfx2hlsl_cvt_alu_check.py-> all ok, rc=0python3 tests/docs_links_check.py-> 18 files, 405 links OKpython3 tests/bidi_controls_check.py-> OKgit diff --check origin/main...HEAD-> clean- Mutation: widened the constant bound from 255 to 256 in
tools/startup/gfx2hlsl.py. The new check fails. The change was reverted withgit checkout -- ..
P1: none.
P2: none.
Nits (unchanged, not blocking)
getattr(self, 'byte_float_v', set()).discard(n)inset_vallocates a set on every write. Initialisingbyte_float_vin the constructor would remove thegetattr/setdefaultindirection.
Not run: a Windows WARP/hardware draw. The CI Windows job passed on this head. A maintainer who wants a local GPU readback should run startup_gfx2hlsl_cvt_pk_u8_warp.
AI-assisted; commands listed were executed locally.
…pk-u8 # Conflicts: # tools/startup/gfx2hlsl.py
|
Merged |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head 77b36dddbf6b69b67eaba4816311fe9ec840fa00 (a merge of main only; the PR's own diff is unchanged in substance since the previous reviewed head).
Scope: v_cvt_pk_u8_f32 lowering in tools/startup/gfx2hlsl.py, new tests/startup_gfx2hlsl_cvt_pk_u8_check.py, lane_alu fixture hook (pipeline kwargs), CMake and workflow wiring.
Commands executed locally in a clean worktree of this head:
git diff --stat origin/main...HEAD: 6 files, +246/-6.python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py: four lanes translated ok; 8 draw sets ok; mutation changes draw set 0 ok.python3 -B tests/startup_gfx2hlsl_lane_alu.py: accepted forms translate ok; refused forms stay refused ok.python3 tests/docs_links_check.py: 18 files, 407 links, ok.python3 tests/bidi_controls_check.py: 1489 files, no bidi controls.git diff --check origin/main...HEAD: clean.- Mutation 1: disabled the constant-source domain refusal (
if False). The refused-forms check fails, so it is killed. - Mutation 2: changed the byte-select mask
& 3uto& 7u. The translated-text check fails, so it is killed. - Worktree is clean after
git checkout -- .. gh pr checks: Linux checks, gfx1013 fixtures and Windows build/test are green. The /O2 job and the chart publish were skipped. The PR is mergeable.- I did not run the Windows VM myself. The draw tests (WARP and hardware) are covered by the PR's own Windows CI job.
Findings:
- P1: none.
- P2: none.
- Nit:
byte_float_vis read throughgetattr(self, ..., ())and created through__dict__.setdefault. A normal attribute set in__init__would be simpler and would not hide a typo. Optional. - Nit:
set_vdiscards the proof, but any future path that writesself.v[...]withoutset_vwould keep a stale proof. Today I found no such path. A short comment at the proof site would help.
Semantics: the formula D = (S2 & ~(0xff << 8*S1[1:0])) | ((cvt(S0) & 0xff) << 8*S1[1:0]) matches the ISA text. Leaving the undefined flt32_to_uint8 cases out by requiring S0 to be +0.0 or an integer in 1..255 is conservative, and the proof/contract split is clear. Titles and text are neutral and clean-room.
Verdict: no blocking issues from this review.
AI-assisted; commands listed were executed locally.
The proof set was read through getattr and created through __dict__.setdefault, which would hide a misspelt name. An immutable class default, replaced by a real set on the first proof, keeps probes built without __init__ working, as low16_only_v does. The proof site now says that every VGPR write must drop it.
|
Thanks @tedin7. Both nits done in e556be5: |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head e556be5 (changes since 77b36dd: byte_float_v becomes a class default, set_v and the proof/lookup sites no longer use getattr/__dict__.setdefault).
Verdict: no P1, no P2.
What I checked
git diff --stat origin/main...HEAD: 6 files, gfx2hlsl.py + one new check + lane_alu hook + CMake/CI/docs. Title is neutral, PR is MERGEABLE.- CI on this head: Linux Python checks, gfx1013 fixtures and Windows build/test all pass (the /O2 and chart jobs are skipped).
- The only VGPR write path in the emitter is
set_v(singleemit('v%d = ...')at gfx2hlsl.py:630), andset_vis where the proof is dropped. The new comment at the proof site states this contract. Good. - The class default
()is falsy, soset_vskips the discard until the first proof creates a per-instanceset. No shared mutable state between stages. - The proof is only granted with no modifiers, known exec and no branches. A constant S0 outside +0.0 / 1..255 is refused. Any other register needs the explicit
pk_u8_s0_contract, and that choice is recorded inpipe.notes. - The formula matches the ISA text quoted in the comment:
D = (S2 & ~(0xff << 8*S1[1:0])) | ((cvt(S0) & 0xff) << 8*S1[1:0]).
Commands run locally on the head
python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py: all steps ok (four lanes translated, 8 draw sets, mutation changes draw set 0, accepted/refused forms).python3 -B tests/startup_gfx2hlsl_lane_alu.py: Self-test passed.python3 tests/docs_links_check.py: 18 files, 407 links ok.python3 tests/bidi_controls_check.py: no bidirectional controls.git diff --check origin/main...HEAD: clean.
Mutations (each reverted with git checkout -- .)
set_vno longer discards the proof: killed (AssertionError: a float multiply is refused without the contract).- Select mask
& 3uchanged to& 1u: killed (translated text reads 42fe0000 12345676 01fe7f80).
Windows: I did not run the Win2025 pipeline. The WARP/hardware draw tests are registered in CMake and the Windows CI job passed on this head.
Nits (non-blocking)
- nit:
byte_float_vis typed as a tuple at class level but becomes aseton first use. A one-word comment would make the switch explicit.
AI-assisted; commands listed were executed locally.
|
Ran head
Every run printed the same 5 ok lines, and the FXC and DXC mutations each failed as expected, for example AI-assisted. |
R0Y00
left a comment
There was a problem hiding this comment.
The original S0 finding is resolved. Ordinary translation refuses an unproven value, and an explicitly selected profile records its input contract. All four byte selectors and the lost-S2 mutation verified independently on WARP and RTX 4060 Laptop.
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
) * shader: lower v_cvt_pk_u8_f32 for any binary32 source in gfx2hlsl 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 #2023 (item K), follows #2101 * tests: pin the MODE write offset, width and hwreg refusal for cvt_pk_u8 Three mutations of the s_setreg_imm32_b32 MODE handling reported on #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. * Advance the startup shader cache revision to 15 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. * Guard v_cvt_pk_u8_f32 with an integer compare on the raw bits 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. * Decode float-printed immediates in s_setreg_imm32_b32 MODE writes 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. * Add the v_cvt_pk_u8_f32 hardware probe and its stored output 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.
Lower
v_cvt_pk_u8_f32intools/startup/gfx2hlsl.py(item K of #2023).What it does
v_cvt_pk_u8_f32 D, S0, S1, S2(VOP3A opcode 350) inserts a byte into S2:The RDNA 1.0 and RDNA 2 ISA guides do not define
flt32_to_uint8: rounding, bounds, NaN and negative results are not stated. I did not find a source for them that I could cite, so this PR does whattools/compute_store_aot.pyalready does for the same opcode (#1546, #1592): it holds only where every reading agrees, an S0 that is +0.0 or an integer in [1, 255].clamponly keeps the conversion defined; it is not a claim about the hardware.I did not add rounding at x.5 or clamping below 0 and above 255 to the fixture, because the reference would be a guess. If a maintainer can point to the hardware behavior, that is a follow-up.
Test
tests/startup_gfx2hlsl_cvt_pk_u8_check.py, source-built (no retail bytes), on the sharedstartup_gfx2hlsl_lane_alu.pyrunner:llvm-mcreassembly of the pixel bodies.Wired in
CMakeLists.txt(controls, warp, hardware) and.github/workflows/windows.yml.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.pyandgit diff --check. The D3D12/WARP draw does not run on Linux, so thedrawmode is untested; the CTest entries need a Windows run.Refs #2023 (item K)
AI-assisted.