tombox-xshader: read scalar NULL operands as zero #308

Closed
github-bot wants to merge 2 commits from gh-pr/308/head into gh-pr/308/base
Owner

@⁠BaggyALPHA on GitHub · 2026-11-23 17:31 UTC · original
BaggyALPHA:fix/scalar-null-zero → main · +60 −3 · 2 files · 2 commits · closed without merging

NULL reads can reuse values written to the translator's internal HLSL discard variable. For example, s_mov_b32 null, 1 makes a subsequent move from NULL return 1. Reads encoded in SOPK's destination/source field can also leak discarded values: after writing 0x7fffffff to NULL, s_addk_i32 null, 1 incorrectly sets signed-overflow SCC; s_setreg_b32 can copy a discarded value into MODE.

Resolve explicit Opnd::Null source expressions to 0u and route the encoded source reads in s_addk_i32, s_mulk_i32, and s_setreg_b32 through the same operand-expression helper. Retain the internal discard destination so instructions such as s_not_b32 null, null still derive SCC from their computed result.

Library / area: tombox-xshader, scalar operand lowering and synthetic execution tests.

Exports changed (exports.txt rows): None.

Source of the behaviour: AMD RDNA 2 ISA: section 5.2, Table 10 defines NULL reads/writes; section 12.2 defines add-immediate overflow and hardware-register writes; section 13.1.2, Table 65 includes NULL in the SOPK SDST field, which can supply an input operand.

Validation:

  • Original six-case regression: five fail on upstream main. All six pass with the fix.

  • Additional 24-case SOPK regression: seven fail on the initial PR commit dc6c49b; all 24 pass after the follow-up. Coverage includes NULL add-immediate overflow flags, NULL MODE-register writes, ordinary-SGPR overflow controls, and ordinary-SGPR MODE writes.

  • Both regression tests (30 cases) pass on WARP and the hardware D3D12 path (TOMBOX_EXEC_HW=1).

  • Full cargo test --locked -p tombox-xshader --no-fail-fast: exit 0, 347 passed, zero failed, 18 ignored. The execution target passed 130 tests with two ignored.

  • WARP ISA sweep: 544 passing mnemonics, zero unexpected failures, 11 known WARP deviations and 28 skipped mnemonics. Deviations/skips are not claimed as bit-exact passes.

  • cargo check --locked -p tombox-xshader --all-targets: passed.

  • Contribution gate: two files checked, zero problems. Whitespace and patch application checks passed.

  • Full default crate test command run; limitations below remain.

  • No game or console files and no binaries included.

  • No fake success; no export status changes.

Related to #187's request for synthetic instruction edge-case coverage. Fixture-dependent tests may be conditionally excluded or return early without title/console inputs. No console DLL oracle, native RDNA2 silicon execution, or title compatibility validation is claimed; this does not establish completion of a shader stage.

Required upstream CI results remain separate from local validation.

**@⁠BaggyALPHA** on GitHub · 2026-11-23 17:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/308) `BaggyALPHA:fix/scalar-null-zero` → `main` · +60 −3 · 2 files · 2 commits · closed without merging NULL reads can reuse values written to the translator's internal HLSL discard variable. For example, `s_mov_b32 null, 1` makes a subsequent move from NULL return 1. Reads encoded in SOPK's destination/source field can also leak discarded values: after writing `0x7fffffff` to NULL, `s_addk_i32 null, 1` incorrectly sets signed-overflow SCC; `s_setreg_b32` can copy a discarded value into MODE. Resolve explicit `Opnd::Null` source expressions to `0u` and route the encoded source reads in `s_addk_i32`, `s_mulk_i32`, and `s_setreg_b32` through the same operand-expression helper. Retain the internal discard destination so instructions such as `s_not_b32 null, null` still derive SCC from their computed result. **Library / area:** `tombox-xshader`, scalar operand lowering and synthetic execution tests. **Exports changed (exports.txt rows):** None. **Source of the behaviour:** [AMD RDNA 2 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf): section 5.2, Table 10 defines NULL reads/writes; section 12.2 defines add-immediate overflow and hardware-register writes; section 13.1.2, Table 65 includes NULL in the SOPK SDST field, which can supply an input operand. **Validation:** - Original six-case regression: five fail on upstream main. All six pass with the fix. - Additional 24-case SOPK regression: seven fail on the initial PR commit `dc6c49b`; all 24 pass after the follow-up. Coverage includes NULL add-immediate overflow flags, NULL MODE-register writes, ordinary-SGPR overflow controls, and ordinary-SGPR MODE writes. - Both regression tests (30 cases) pass on WARP and the hardware D3D12 path (`TOMBOX_EXEC_HW=1`). - Full `cargo test --locked -p tombox-xshader --no-fail-fast`: exit 0, 347 passed, zero failed, 18 ignored. The execution target passed 130 tests with two ignored. - WARP ISA sweep: 544 passing mnemonics, zero unexpected failures, 11 known WARP deviations and 28 skipped mnemonics. Deviations/skips are not claimed as bit-exact passes. - `cargo check --locked -p tombox-xshader --all-targets`: passed. - Contribution gate: two files checked, zero problems. Whitespace and patch application checks passed. - [x] Full default crate test command run; limitations below remain. - [x] No game or console files and no binaries included. - [x] No fake success; no export status changes. Related to #187's request for synthetic instruction edge-case coverage. Fixture-dependent tests may be conditionally excluded or return early without title/console inputs. No console DLL oracle, native RDNA2 silicon execution, or title compatibility validation is claimed; this does not establish completion of a shader stage. Required upstream CI results remain separate from local validation.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:18:06 +00:00
github-bot closed this pull request 2026-11-26 06:18:06 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:49 UTC · original

Applied on main in 15d9930 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/308#issuecomment-7164336518) Applied on main in 15d9930 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 14:34 UTC · original · approved these changes

Update: I've now also read the full diff. The Null => "0u" arm is in r() only, so writes to NULL still go to the discard variable, and the SCC of s_not_b32 null, null still comes from the computed result. The s_setreg_b32, s_addk_i32 and s_mulk_i32 changes read the field as a source, which is right. Scope: CI hasn't run on this PR yet; the workflows are waiting for maintainer approval. My approval covers the semantics and the code, not the build or the test run.

I checked this on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels, so RDNA2, not gfx1013. Each kernel ran twice with identical results, and I compared against the semantics this PR implements (my own port; I didn't run the PR's code).

The NULL rules hold on hardware:

  • After s_mov_b32 null, X (4,096 random X), s_mov_b32 sN, null reads 0.
  • s_add_u32 sN, null, Y gives Y with SCC 0.
  • s_addk_i32 null, 1 after writing 0x7fffffff to NULL leaves SCC 0, so the add reads 0, not the discarded value.
  • s_not_b32 null, null sets SCC 1, which is the computed result's SCC, as you describe.

I didn't check the s_setreg_b32 case.

(Measured with an AI assistant, Claude.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 14:34 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/308#pullrequestreview-6041178506) · **approved these changes** **Update:** I've now also read the full diff. The `Null => "0u"` arm is in `r()` only, so writes to NULL still go to the discard variable, and the SCC of `s_not_b32 null, null` still comes from the computed result. The `s_setreg_b32`, `s_addk_i32` and `s_mulk_i32` changes read the field as a source, which is right. **Scope:** CI hasn't run on this PR yet; the workflows are waiting for maintainer approval. My approval covers the semantics and the code, not the build or the test run. I checked this on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels, so RDNA2, not gfx1013. Each kernel ran twice with identical results, and I compared against the semantics this PR implements (my own port; I didn't run the PR's code). The NULL rules hold on hardware: - After `s_mov_b32 null, X` (4,096 random X), `s_mov_b32 sN, null` reads 0. - `s_add_u32 sN, null, Y` gives Y with SCC 0. - `s_addk_i32 null, 1` after writing 0x7fffffff to NULL leaves SCC 0, so the add reads 0, not the discarded value. - `s_not_b32 null, null` sets SCC 1, which is the computed result's SCC, as you describe. I didn't check the `s_setreg_b32` case. (Measured with an AI assistant, Claude.)

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.