startup: lower v_mad_i32_i16 in gfx2hlsl #3041

Merged
R0D4X merged 1 commit from upstream-pr/3041 into main 2026-11-27 11:47:21 +00:00
R0D4X commented 2026-11-27 10:23:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-11-27T10:23:02Z.


What this changes

  • Lowers gfx1013 VOP3A v_mad_i32_i16 (0x375) in tools/startup/gfx2hlsl.py as asuint(asint({0} << 16) >> 16) * asuint(asint({1} << 16) >> 16) + {2} (signed 16-bit low halves multiplied and added to the 32-bit addend, mod 2^32).
  • Rejects neg/abs encoding bits on v_mad_i32_i16 in check_encoding(), allows low16_only_v on S0/S1 while keeping S2 as a full 32-bit read, and registers the opcode in valu_align4.

Tests added

  • tests/startup_gfx2hlsl_v_mad_i32_i16_check.py: synthetic VOP3A decode via G.decode(), 216-case evaluation against the emitted HLSL expression, EXEC masking, low16_only_v accept (S0/S1) and refusal (S2), encoding & Stage refusals (neg/abs/clamp/mul:2/op_sel/arity), unlowered controls, and FXC ps_5_1 + DXC ps_6_0 offscreen readback draws with negative controls.
  • Registered in tests/cmake/startup_gfx2hlsl_v_mad_i32_i16.cmake and tests/checks/python-linux/startup_gfx2hlsl_v_mad_i32_i16.txt.

Verification

Commit: 9c6c38a1 on Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK 10.0.26100.0, FXC + DXC 1.9.2602.17, D3D12 debug layer enabled (debug_errors=0 debug_warnings=0):

  • ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_v_mad_i32_i16|^startup_tools_lint$': 4/4 passed, 0 skipped
    • WARP (Microsoft Basic Render Driver, 1414:008c, driver 10.0.26100.9818): pass
    • Hardware (NVIDIA GeForce RTX 5080, 10de:2c02, driver 32.0.16.2124): pass
  • ctest --preset release --no-tests=error -R '^startup_gfx2hlsl_v_mad_i32_i16': 3/3 passed, 0 skipped
  • Repo/neighbor checks (docs_links_check.py, bidi_controls_check.py, startup_gfx2hlsl_handler_arity_check.py, startup_gfx2hlsl_refusals_check.py, startup_gfx2hlsl_mad_check.py, git diff --check): all passed.

Scope: Covers synthetic v_mad_i32_i16 fixtures in the offline gfx2hlsl translator and offscreen D3D12 draw harness; does not establish full-title startup or runtime execution.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5525004571 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3041). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-11-27T10:23:02Z. --- ## What this changes - Lowers gfx1013 VOP3A `v_mad_i32_i16` (`0x375`) in `tools/startup/gfx2hlsl.py` as `asuint(asint({0} << 16) >> 16) * asuint(asint({1} << 16) >> 16) + {2}` (signed 16-bit low halves multiplied and added to the 32-bit addend, mod 2^32). - Rejects `neg`/`abs` encoding bits on `v_mad_i32_i16` in `check_encoding()`, allows `low16_only_v` on S0/S1 while keeping S2 as a full 32-bit read, and registers the opcode in `valu_align4`. ## Tests added - `tests/startup_gfx2hlsl_v_mad_i32_i16_check.py`: synthetic VOP3A decode via `G.decode()`, 216-case evaluation against the emitted HLSL expression, `EXEC` masking, `low16_only_v` accept (S0/S1) and refusal (S2), encoding & `Stage` refusals (`neg`/`abs`/`clamp`/`mul:2`/`op_sel`/arity), unlowered controls, and FXC `ps_5_1` + DXC `ps_6_0` offscreen readback draws with negative controls. - Registered in `tests/cmake/startup_gfx2hlsl_v_mad_i32_i16.cmake` and `tests/checks/python-linux/startup_gfx2hlsl_v_mad_i32_i16.txt`. ## Verification Commit: `9c6c38a1` on Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK `10.0.26100.0`, FXC + DXC `1.9.2602.17`, D3D12 debug layer enabled (`debug_errors=0 debug_warnings=0`): - `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_v_mad_i32_i16|^startup_tools_lint$'`: 4/4 passed, 0 skipped - WARP (`Microsoft Basic Render Driver`, `1414:008c`, driver `10.0.26100.9818`): pass - Hardware (`NVIDIA GeForce RTX 5080`, `10de:2c02`, driver `32.0.16.2124`): pass - `ctest --preset release --no-tests=error -R '^startup_gfx2hlsl_v_mad_i32_i16'`: 3/3 passed, 0 skipped - Repo/neighbor checks (`docs_links_check.py`, `bidi_controls_check.py`, `startup_gfx2hlsl_handler_arity_check.py`, `startup_gfx2hlsl_refusals_check.py`, `startup_gfx2hlsl_mad_check.py`, `git diff --check`): all passed. *Scope:* Covers synthetic `v_mad_i32_i16` fixtures in the offline `gfx2hlsl` translator and offscreen D3D12 draw harness; does not establish full-title startup or runtime execution.
thepatrickworld commented 2026-11-27 10:43:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-27T10:43:05Z.


Independent check of the controls added here, head 9c6c38a1 — mutation matrix, no GPU

Not a code change; measurements only.

Environment

  • Head 9c6c38a1f7685efdc3ff3326969a7237877e4984, base 8a901eb2, tracked tree clean (git status --porcelain --untracked-files=no empty).
  • Linux 7.0.0 x86_64, Python 3.11.16. The no-argument path is pure Python, so the whole controls block runs here without a D3D12 device.
  • tools/startup/gfx2hlsl.py sha256 5549ae98d3a21d1c00a8fcc0544f07377c5f13c219226c65ec3ad9cf5d09a738, fixture sha256 29ea6a086c6b03bbe889367699cb0d96405a28c7aad02ba2c3c24a09d84af1c1.
  • No adapter was involved: neither draw entry was run. Nothing below speaks to DXC/FXC output or to adapter behaviour.

What I ran

python3 -B tests/startup_gfx2hlsl_v_mad_i32_i16_check.py     # 19 controls ok

plus a mutation matrix on tools/startup/gfx2hlsl.py: each mutant is written to a copy outside the tree, injected as sys.modules['gfx2hlsl'], and the fixture and its two helpers are re-imported against it. A must-break mutant (low 16 bits read unsigned) is part of the set and is killed, so the injection is live. Baseline passes before and after; the source is restored to the sha256 above.

The controls do discriminate

13 mutants applied, 9 killed, 4 survivors.

mutant killed by
({0} & 0xffffu) * ({1} & 0xffffu) (unsigned low halves) v_mad_i32_i16 grid (216 cases)
logical shift instead of arithmetic, S0 v_mad_i32_i16 grid (216 cases)
<< 15) >> 15 instead of << 16) >> 16, S0 v_mad_i32_i16 grid (216 cases)
ABS term of the new guard removed (word0 & 0x700) encoding guard refuses abs0
NEG term of the new guard removed (word1 & 0xe0000000) encoding guard refuses neg2
whole if name == 'v_mad_i32_i16': block removed (lines 509-512) encoding guard refuses abs0
low16_only=True dropped on S0, then on S1, separately uncaught Unsupported, before the assert
elif base == 'v_mad_u32_u16' (shared low16 path dropped) uncaught Unsupported, before the assert
guard widened to clamp (0x700 → 0x8700) nothing
guard widened to op_sel (0x700 → 0x7f00) nothing
v_mad_i32_i16 removed from the valu_align4 tuple nothing
+ {2} → + asuint({2}) nothing (equivalent: u() returns a uint expression, so asuint is identity)

Six kills land on a named check; three are an uncaught Unsupported escaping p_lo.one(...) before check('low16_only_v accepted on S0 and S1', ...) is reached. A kill is a kill, but those three do not tell you which assertion would have caught them.

What the table establishes: the 216-case grid is not self-referential — it evaluates the emitted expression text with its own AST evaluator against the fixture's separate ref_mad_i32_i16, and all three expression mutants die there. And each term of the new encoding guard is pinned by its own case: dropping the ABS term and dropping the NEG term fail different checks.

Three of the six source-modifier bits are not covered

ABS is w0[10:8] and NEG is w1[31:29], one bit per source. I took the bit-to-source order from tools/disasm_rdna2.py, not from the fixture's encoder: leaving one bit unguarded at a time and decoding prints bit 8 → |v0|, bit 9 → |v4|, bit 10 → |v8|, bit 29 → -v0, bit 30 → -v4, bit 31 → -v8. The fixture sets ABS on S0, NEG on S0 and NEG on S2, so ABS on S1 (bit 9), ABS on S2 (bit 10) and NEG on S1 (bit 30) are never produced. Narrowing the mask asks which bits those three cases can distinguish:

guard mask narrowed to verdict
ABS S0 only (0x100) survives, all 19
ABS S0+S1 (0x300) survives, all 19
ABS S0+S2 (0x500) survives, all 19
NEG S0+S2, S1 dropped (0xa0000000) survives, all 19
NEG S0 only (0x20000000) killed — encoding guard refuses neg2
NEG S1+S2, S0 dropped (0xc0000000) killed — encoding guard refuses neg_inline0

A guard that silently stops covering those three bits still passes everything.

To be clear about what that costs today, I chased the three unguarded bits through four operand kinds each (VGPR, inline 1, inline -1, literal dword). None reached an emitted line: with the bit open the decoder prints |v4|, |v8| or -v4, and VGPR sources are then refused in u() (operand '|v4|', tools/startup/gfx2hlsl.py:2545), while an inline-integer or literal source with NEG is refused earlier by the pre-existing check at lines 549-575 (NEG on an integer or literal source 1: the printed sign would read as inline -N). So this is unmeasured guard scope, not a miscompile path — and three entries in the existing tuple would close it: abs_bits=2, abs_bits=4, neg=2.

The sibling's clear-on-destination control is not ported

tests/startup_gfx2hlsl_mad_u32_u16_check.py:240-244 marks its destination low16_only_v and asserts that a write clears the marking, that neither source ends in whole_read_v, and that S2's source is recorded as a whole-word read. The new fixture marks only the sources ({0, 4}) and writes v12, so clear_low16 (line 2807) is never reached through this op — the low16 contract is only pinned on the "accepted as source" side. One mutant (else: self.clear_low16(n) → else: pass) separates the two fixtures:

fixture mutant verdict
..._mad_u32_u16_check.py clear removed killed — low16_only allowed on S0/S1 and cleared on dst
..._v_mad_i32_i16_check.py clear removed survives, all 19

The sibling control ports as is.

Two harness features of the sibling are absent

  • No binary <llvm-mc> mode: the synthetic encoder is never cross-checked against an assembler. The abs/neg words are produced by the fixture's own vop3a() and read back by G.decode, with no external oracle on the bit positions; the sibling reassembles its own sources (check_binary).
  • No byte-level refusal cases (refused_bytes in the sibling): the clamp, op_sel and mul:2 refusals here are assembly text and go through Stage.lower, so they never exercise check_encoding. That is why widening the encoding guard to clamp or op_sel changes nothing observable — the encoding layer's scope on those fields is untested in both directions.

Limits

  • One machine, no adapter, no debug layer; draw was not run, so nothing here covers the compiled HLSL or either compiler.
  • The mutants are my selection, not an exhaustive enumeration; the counts are sensitive to it.
  • The grid pins the emitted expression, not the operand plumbing — that part is pinned by the two low16_only mutants and the shared-path one, all three of which die by exception.
  • The valu_align4 entry is not covered by any control here, and the sibling fixture does not pin its own entry either.
  • CI, for context only: controls reaches pull requests through tests/checks/python-linux/*.txt and the python-linux job, and _warp is outside the pr preset regex while _hardware is excluded by -E '_hardware$' — the same shape as the rest of the family.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7216220790 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3041#issuecomment-7216220790). Originally posted by [thepatrickworld](https://github.com/thepatrickworld) on 2026-11-27T10:43:05Z. --- **Independent check of the controls added here, head `9c6c38a1` — mutation matrix, no GPU** *Not a code change; measurements only.* ### Environment - Head `9c6c38a1f7685efdc3ff3326969a7237877e4984`, base `8a901eb2`, tracked tree clean (`git status --porcelain --untracked-files=no` empty). - Linux 7.0.0 x86_64, Python 3.11.16. The no-argument path is pure Python, so the whole controls block runs here without a D3D12 device. - `tools/startup/gfx2hlsl.py` sha256 `5549ae98d3a21d1c00a8fcc0544f07377c5f13c219226c65ec3ad9cf5d09a738`, fixture sha256 `29ea6a086c6b03bbe889367699cb0d96405a28c7aad02ba2c3c24a09d84af1c1`. - **No adapter was involved**: neither `draw` entry was run. Nothing below speaks to DXC/FXC output or to adapter behaviour. ### What I ran ``` python3 -B tests/startup_gfx2hlsl_v_mad_i32_i16_check.py # 19 controls ok ``` plus a mutation matrix on `tools/startup/gfx2hlsl.py`: each mutant is written to a copy outside the tree, injected as `sys.modules['gfx2hlsl']`, and the fixture and its two helpers are re-imported against it. A must-break mutant (low 16 bits read unsigned) is part of the set and is killed, so the injection is live. Baseline passes before and after; the source is restored to the sha256 above. ### The controls do discriminate 13 mutants applied, 9 killed, 4 survivors. | mutant | killed by | | --- | --- | | `({0} & 0xffffu) * ({1} & 0xffffu)` (unsigned low halves) | `v_mad_i32_i16 grid (216 cases)` | | logical shift instead of arithmetic, S0 | `v_mad_i32_i16 grid (216 cases)` | | `<< 15) >> 15` instead of `<< 16) >> 16`, S0 | `v_mad_i32_i16 grid (216 cases)` | | ABS term of the new guard removed (`word0 & 0x700`) | `encoding guard refuses abs0` | | NEG term of the new guard removed (`word1 & 0xe0000000`) | `encoding guard refuses neg2` | | whole `if name == 'v_mad_i32_i16':` block removed (lines 509-512) | `encoding guard refuses abs0` | | `low16_only=True` dropped on S0, then on S1, separately | uncaught `Unsupported`, before the assert | | `elif base == 'v_mad_u32_u16'` (shared low16 path dropped) | uncaught `Unsupported`, before the assert | | guard widened to `clamp` (`0x700` → `0x8700`) | nothing | | guard widened to `op_sel` (`0x700` → `0x7f00`) | nothing | | `v_mad_i32_i16` removed from the `valu_align4` tuple | nothing | | `+ {2}` → `+ asuint({2})` | nothing (equivalent: `u()` returns a uint expression, so `asuint` is identity) | Six kills land on a named check; three are an uncaught `Unsupported` escaping `p_lo.one(...)` before `check('low16_only_v accepted on S0 and S1', ...)` is reached. A kill is a kill, but those three do not tell you which assertion would have caught them. What the table establishes: the 216-case grid is not self-referential — it evaluates the emitted expression text with its own AST evaluator against the fixture's separate `ref_mad_i32_i16`, and all three expression mutants die there. And each term of the new encoding guard is pinned **by its own case**: dropping the ABS term and dropping the NEG term fail different checks. ### Three of the six source-modifier bits are not covered ABS is `w0[10:8]` and NEG is `w1[31:29]`, one bit per source. I took the bit-to-source order from `tools/disasm_rdna2.py`, not from the fixture's encoder: leaving one bit unguarded at a time and decoding prints bit 8 → `|v0|`, bit 9 → `|v4|`, bit 10 → `|v8|`, bit 29 → `-v0`, bit 30 → `-v4`, bit 31 → `-v8`. The fixture sets ABS on S0, NEG on S0 and NEG on S2, so ABS on S1 (bit 9), ABS on S2 (bit 10) and NEG on S1 (bit 30) are never produced. Narrowing the mask asks which bits those three cases can distinguish: | guard mask narrowed to | verdict | | --- | --- | | ABS S0 only (`0x100`) | survives, all 19 | | ABS S0+S1 (`0x300`) | survives, all 19 | | ABS S0+S2 (`0x500`) | survives, all 19 | | NEG S0+S2, S1 dropped (`0xa0000000`) | survives, all 19 | | NEG S0 only (`0x20000000`) | killed — `encoding guard refuses neg2` | | NEG S1+S2, S0 dropped (`0xc0000000`) | killed — `encoding guard refuses neg_inline0` | A guard that silently stops covering those three bits still passes everything. To be clear about what that costs today, I chased the three unguarded bits through four operand kinds each (VGPR, inline `1`, inline `-1`, literal dword). None reached an emitted line: with the bit open the decoder prints `|v4|`, `|v8|` or `-v4`, and VGPR sources are then refused in `u()` (`operand '|v4|'`, `tools/startup/gfx2hlsl.py:2545`), while an inline-integer or literal source with NEG is refused earlier by the pre-existing check at lines 549-575 (`NEG on an integer or literal source 1: the printed sign would read as inline -N`). So this is unmeasured guard scope, not a miscompile path — and three entries in the existing tuple would close it: `abs_bits=2`, `abs_bits=4`, `neg=2`. ### The sibling's clear-on-destination control is not ported `tests/startup_gfx2hlsl_mad_u32_u16_check.py:240-244` marks its destination `low16_only_v` and asserts that a write clears the marking, that neither source ends in `whole_read_v`, and that S2's source is recorded as a whole-word read. The new fixture marks only the sources (`{0, 4}`) and writes `v12`, so `clear_low16` (line 2807) is never reached through this op — the low16 contract is only pinned on the "accepted as source" side. One mutant (`else: self.clear_low16(n)` → `else: pass`) separates the two fixtures: | fixture | mutant | verdict | | --- | --- | --- | | `..._mad_u32_u16_check.py` | clear removed | killed — `low16_only allowed on S0/S1 and cleared on dst` | | `..._v_mad_i32_i16_check.py` | clear removed | survives, all 19 | The sibling control ports as is. ### Two harness features of the sibling are absent - No `binary <llvm-mc>` mode: the synthetic encoder is never cross-checked against an assembler. The abs/neg words are produced by the fixture's own `vop3a()` and read back by `G.decode`, with no external oracle on the bit positions; the sibling reassembles its own sources (`check_binary`). - No byte-level refusal cases (`refused_bytes` in the sibling): the `clamp`, `op_sel` and `mul:2` refusals here are assembly text and go through `Stage.lower`, so they never exercise `check_encoding`. That is why widening the encoding guard to `clamp` or `op_sel` changes nothing observable — the encoding layer's scope on those fields is untested in both directions. ### Limits - One machine, no adapter, no debug layer; `draw` was not run, so nothing here covers the compiled HLSL or either compiler. - The mutants are my selection, not an exhaustive enumeration; the counts are sensitive to it. - The grid pins the emitted expression, not the operand plumbing — that part is pinned by the two `low16_only` mutants and the shared-path one, all three of which die by exception. - The `valu_align4` entry is not covered by any control here, and the sibling fixture does not pin its own entry either. - CI, for context only: `controls` reaches pull requests through `tests/checks/python-linux/*.txt` and the `python-linux` job, and `_warp` is outside the `pr` preset regex while `_hardware` is excluded by `-E '_hardware$'` — the same shape as the rest of the family. _AI-assisted._
R0Y00 commented 2026-11-27 11:46:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T11:46:47Z.

Review: APPROVED


Reviewed 9c6c38a1 in integration tree 49adb1b3 on main 1c879506 with the preceding packed-integer PRs in the tested merge order where applicable. python -B tests/startup_gfx2hlsl_v_mad_i32_i16_check.py passed. Its draw <dxc> <offscreen_check> <warp|hardware> <fxc> invocation passed on WARP and RTX with both Debug and Release /O2 renderers, reusing builds of the unchanged renderer sources. Signed low halves and the full-width addend matched, including the alignment and lane-mask controls. Windows 11 26200.9974, SDK 10.0.26100, WARP 10.0.26100.9818, RTX 4060 Laptop driver 32.0.16.1656, D3D12 debug layer enabled. Hosted Windows, Linux Python and gfx1013 checks passed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6087649736 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3041#pullrequestreview-6087649736). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T11:46:47Z. **Review:** APPROVED --- Reviewed `9c6c38a1` in integration tree `49adb1b3` on `main` `1c879506` with the preceding packed-integer PRs in the tested merge order where applicable. `python -B tests/startup_gfx2hlsl_v_mad_i32_i16_check.py` passed. Its `draw <dxc> <offscreen_check> <warp|hardware> <fxc>` invocation passed on WARP and RTX with both Debug and Release /O2 renderers, reusing builds of the unchanged renderer sources. Signed low halves and the full-width addend matched, including the alignment and lane-mask controls. Windows 11 26200.9974, SDK 10.0.26100, WARP 10.0.26100.9818, RTX 4060 Laptop driver 32.0.16.1656, D3D12 debug layer enabled. Hosted Windows, Linux Python and gfx1013 checks passed.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3041
No description provided.