shader: compute s_add_i32 overflow SCC from the result sign #1005

Manually merged
github merged 1 commit from gh-pr/1005/head into gh-pr/1005/base 2026-11-26 22:34:32 +00:00
Owner

@⁠mumba17 on GitHub · 2026-11-23 20:53 UTC · original
mumba17:shader/add-i32-overflow-form → main · +4 −2 · 3 files · 1 commits · merged 2026-11-23 21:10 UTC

Summary

pixel_aot lowers the s_add_i32 SCC (signed overflow) as (~(a ^ b) & (a ^ r)) >> 31 != 0u. On an NVIDIA GeForce RTX 3090 the compiled shader returned SCC 0 for 0x7fffffff + 5, so pixel_aot_blend_scalar_hardware fails on that adapter while WARP passes.

This PR switches to the equivalent form ((a ^ r) & (b ^ r)) >> 31 != 0u: overflow exactly when the result's sign differs from both operands. The HLSL is uint throughout and DXC 1.7.2308 emits plain add i32 (no nsw/nuw) for the scalar fixture, so the old form is well defined; the difference is in the driver's compiled code, not in the DXIL.

s_sub_i32 keeps its form. Its overflowing case (set 0, 0x80000000 - 0x20) passed on the same adapter.

Observation (before)

main at aabbedd and again at 3174eb3, serial run:

pixel_aot_blend_scalar_hardware ... ***Failed
AssertionError: Expected ['pass', 'pass'], got ['pass', 'mismatch']
case=1 status=mismatch
pixel_mismatch x=0 y=0
channel=0 expected=1.25586 actual=1.25195 expected_bits=3fa0c000 actual_bits=3fa04000
debug_errors=0 debug_warnings=0
pixel_aot_blend_scalar_warp ... Passed

Channel 0 carries SCC flag bits 0-9 shifted left by 13, so the single differing bit (0x8000) is flag bit 2, which is s_add_i32 s62, s46, s45 with SCALAR_WORDS[1] (0x7fffffff + 5, expected SCC 1).

After

ctest --preset dev --no-tests=error -R '^pixel_aot_blend_scalar_(hardware|warp)$' -V
pixel_aot_blend_scalar_hardware ... Passed
pixel_aot_blend_scalar_warp ....... Passed

Broader check on the branch: ctest --preset dev --no-tests=error -j 4 -R '^(pixel_aot_.*_warp|pixel_aot_blend_scalar_hardware|pixel_aot_hardware|pixel_aot_controls.*|form_coverage.*)$' reports 55/55 passed. tests/form_coverage_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py pass.

Equivalence: both formulas match the Python reference (not -2**31 <= a + b < 2**31) on 144 edge pairs (0, 1, 2, 5, 0x20, 0x7ffffffe, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffff0, 0xfffffffe, 0xffffffff squared) and 2,000,000 random pairs.

Negative control: the unmodified lowering is the failing run above; the existing SCALAR_MUTATIONS and the second constant set still exercise both SCC arms.

Changes

  • tools/pixel_aot.py: add_i32 SCC expression.
  • tests/pixel_aot_check.py: the scalar control that matches the emitted HLSL text.
  • docs/experiments.md: one paragraph after the scalar ALU description.

Environment

  • NVIDIA GeForce RTX 3090 (10de:2204), driver 32.0.16.1664, D3D12 debug layer enabled
  • Windows 11 Pro build 26300
  • MSVC 19.44.35207 (VS 2022 Build Tools), Windows SDK 10.0.26100, DXC 1.7.2308.16, Python 3.13.1, dev preset

Not tested: other NVIDIA drivers or architectures, AMD and Intel hardware. A full ctest on this machine had no other deterministic failures; parallel -j 8 runs timed out several _hardware tests that pass serially (the timing issue tracked in #448).

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠mumba17** on GitHub · 2026-11-23 20:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1005) `mumba17:shader/add-i32-overflow-form` → `main` · +4 −2 · 3 files · 1 commits · merged 2026-11-23 21:10 UTC ## Summary `pixel_aot` lowers the `s_add_i32` SCC (signed overflow) as `(~(a ^ b) & (a ^ r)) >> 31 != 0u`. On an NVIDIA GeForce RTX 3090 the compiled shader returned SCC 0 for `0x7fffffff + 5`, so `pixel_aot_blend_scalar_hardware` fails on that adapter while WARP passes. This PR switches to the equivalent form `((a ^ r) & (b ^ r)) >> 31 != 0u`: overflow exactly when the result's sign differs from both operands. The HLSL is `uint` throughout and DXC 1.7.2308 emits plain `add i32` (no `nsw`/`nuw`) for the scalar fixture, so the old form is well defined; the difference is in the driver's compiled code, not in the DXIL. `s_sub_i32` keeps its form. Its overflowing case (set 0, `0x80000000 - 0x20`) passed on the same adapter. ## Observation (before) `main` at `aabbedd` and again at `3174eb3`, serial run: ``` pixel_aot_blend_scalar_hardware ... ***Failed AssertionError: Expected ['pass', 'pass'], got ['pass', 'mismatch'] case=1 status=mismatch pixel_mismatch x=0 y=0 channel=0 expected=1.25586 actual=1.25195 expected_bits=3fa0c000 actual_bits=3fa04000 debug_errors=0 debug_warnings=0 pixel_aot_blend_scalar_warp ... Passed ``` Channel 0 carries SCC flag bits 0-9 shifted left by 13, so the single differing bit (`0x8000`) is flag bit 2, which is `s_add_i32 s62, s46, s45` with `SCALAR_WORDS[1]` (`0x7fffffff + 5`, expected SCC 1). ## After ``` ctest --preset dev --no-tests=error -R '^pixel_aot_blend_scalar_(hardware|warp)$' -V pixel_aot_blend_scalar_hardware ... Passed pixel_aot_blend_scalar_warp ....... Passed ``` Broader check on the branch: `ctest --preset dev --no-tests=error -j 4 -R '^(pixel_aot_.*_warp|pixel_aot_blend_scalar_hardware|pixel_aot_hardware|pixel_aot_controls.*|form_coverage.*)$'` reports 55/55 passed. `tests/form_coverage_check.py`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. Equivalence: both formulas match the Python reference (`not -2**31 <= a + b < 2**31`) on 144 edge pairs (0, 1, 2, 5, `0x20`, `0x7ffffffe`, `0x7fffffff`, `0x80000000`, `0x80000001`, `0xfffffff0`, `0xfffffffe`, `0xffffffff` squared) and 2,000,000 random pairs. Negative control: the unmodified lowering is the failing run above; the existing `SCALAR_MUTATIONS` and the second constant set still exercise both SCC arms. ## Changes - `tools/pixel_aot.py`: `add_i32` SCC expression. - `tests/pixel_aot_check.py`: the scalar control that matches the emitted HLSL text. - `docs/experiments.md`: one paragraph after the scalar ALU description. ## Environment - NVIDIA GeForce RTX 3090 (`10de:2204`), driver 32.0.16.1664, D3D12 debug layer enabled - Windows 11 Pro build 26300 - MSVC 19.44.35207 (VS 2022 Build Tools), Windows SDK 10.0.26100, DXC 1.7.2308.16, Python 3.13.1, `dev` preset Not tested: other NVIDIA drivers or architectures, AMD and Intel hardware. A full `ctest` on this machine had no other deterministic failures; parallel `-j 8` runs timed out several `_hardware` tests that pass serially (the timing issue tracked in #448). AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
pixel_aot lowered the s_add_i32 SCC (signed overflow) as
(~(a ^ b) & (a ^ r)) >> 31. On an NVIDIA GeForce RTX 3090 (driver
32.0.16.1664) that shader returned SCC 0 for 0x7fffffff + 5, so
pixel_aot_blend_scalar_hardware reported pixel_mismatch on SCC flag
bit 2 while WARP passed. DXC 1.7.2308 emits plain add i32 without nsw,
so the HLSL and DXIL are well defined.

Use the equivalent form ((a ^ r) & (b ^ r)) >> 31: overflow exactly
when the result sign differs from both operands. Both forms agree with
the Python reference on 144 edge pairs and 2,000,000 random pairs.
Update the scalar control that matches the emitted HLSL and record the
observation in docs/experiments.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:34:30 +00:00
github manually merged commit eddd3e174f into gh-pr/1005/base 2026-11-26 22:34:32 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.