Skip to content

heap_index_census: track scalar carry for descriptor addresses (D3D-329) - #6063

Open
ODanielAtaide wants to merge 3 commits into
ItsNotRexed:mainfrom
ODanielAtaide:fix/heap-census-scalar-carry
Open

ODanielAtaide wants to merge 3 commits into
ItsNotRexed:mainfrom
ODanielAtaide:fix/heap-census-scalar-carry

Conversation

@ODanielAtaide

Copy link
Copy Markdown

D3D-329 follow-up. Coordination claim.

What changed

The heap-index census gave s_addc_u32 and s_subb_u32 a constant-zero result for two zero operands, even when SCC carried or borrowed one. A later scalar load through an entry SGPR and that result could then be classified as ptr32 without proof that its high dword was zero.

Track known SCC values alongside SGPR tags for unsigned addition/subtraction and constant shift-add. At a control-flow join, keep a known SCC only if incoming values agree. Unsupported scalar effects discard it. Unsigned operand bounds retain proofs such as adding or subtracting zero from an unknown dword with no incoming carry. Keep pointer provenance conservative when the carry contribution is unknown.

Verification

  • Three census regressions produced six failing cases on main 561b65a8. They pass with this change. They cover carry and borrow, agreeing and disagreeing joins, and a compare that invalidates the previous SCC value.
  • 16 focused test methods pass, including 144 add/subtract boundary combinations checked against Python integer arithmetic. The one excluded hard-link test fails during link creation with WinError 5 on both base and branch in this environment.
  • test_shader_meta.py (27), test_shader_meta_operands.py (5), test_ptr_truncation_census.py (9), and test_text_write_line_endings.py (3) pass. py_compile passes.
  • Verified that the patch from heap_index_census: report the load provenance of all four sampler words #5926 applies cleanly over this code.

Windows 11 build 26300, CPython 3.12.14, synthetic listings and CPU only. No GPU or full corpus run. AI-assisted (Codex).

@leozinhoszg

Copy link
Copy Markdown
Contributor

Tested ee5b46db on Windows 11 Pro (26200), CPython 3.14.7, CPU only, no game files: pass with notes.

  • python -I tests/test_heap_index_census.py: 17/17 pass, the hard-link test included. compileall and git diff --check are clean. Merged with heap_index_census: read a carve index saved with a UTF-8 BOM (EGEN-HEAP-INDEX-BOM, DeepSeek3) #6051 (git merge-tree is clean): 20/20 pass.
  • Negative control: with main's tools/heap_index_census.py, the six cases from the PR fail and the reference test errors on the missing unsigned_add_sub.
  • On synthetic listings the carry model matches RDNA2, and every miss is conservative. SCC from s_add_u32/s_sub_u32/s_addc_u32/s_subb_u32, and from s_lshlN_add_u32 when both operands are constant, reaches the next s_addc_u32/s_subb_u32. It survives s_mov*, s_waitcnt, branches, VALU and memory ops. These forms drop it: constant s_cmp*, s_cselect_b32 used to materialise the carry, s_cbranch_scc* path facts, and SCC-neutral SALU outside the list (s_waitcnt_vscnt, s_getpc_b64, s_mul_i32).
  1. The same address now gets two labels. s_mov_b32 s9, 0 followed by s_load_dwordx4 s[20:23], s[8:9], 0x40 stays ptr32. s_add_u32 s10, s8, 0x40, then s_addc_u32 s11, 0, 0, then s_load_dwordx4 s[20:23], s[10:11], 0 now gives ptr64_lo_only (main gives ptr32). The SMEM offset is a 64-bit add too, so both forms can carry, and in both the base is one 32-bit input SGPR (tools/heap_index_census.py:253-271, labels at 320-323).
  2. docs/d3d12_vulkan.md:1560-1562 quotes this tool: 769,366 of 770,412 loads are ptr32. Loads whose high word is s_addc_u32 x, 0, 0 without a proven zero carry now leave that count. I can't rerun the corpus here.
  3. Three new branches have no test: each mutation keeps the suite green. They are the s_lshlN_add_u32 SCC (line 261), SCC kept across VALU/memory ops (202-204) and carry is None in the dynamic-offset flag (269, 271).

Suggestion: give the "32-bit base, high word is only a carry" case its own ptr value (for example ptr32_carry), so the D3D-329 answer stays readable. Then rerun the census on a work folder, report how many loads move, and update the doc line if the count changes. A test that keeps SCC across a v_ op would cover most of point 3.

AI-assisted.

@Kev-Ball

Copy link
Copy Markdown

Ran the heap-index census on all 26,503 Xbox 1.0.2.1 shader listings on CachyOS, comparing main 561b65a8 with ee5b46db. The complete parsed JSON reports are equal, including every pointer class, base, role and offset bucket in every group.

Both revisions analyzed 25,030 shaders and skipped the same 1,473 for embedded data words. Of the analyzed shaders, 24,392 had descriptor loads (770,412 loads total). The stricter carry/borrow handling preserves the published retail counts for this build. The synthetic regressions remain the direct evidence for the corrected classification. CPU only, AI-assisted.

Exercise shift-add carry, SCC-preserving vector and scalar loads, and a dynamic offset when SCC is unknown. Clarify that pointer width describes the load's base pair before its immediate offset.

AI-assisted.
@ODanielAtaide

Copy link
Copy Markdown
Author

Thanks for the notes and the corpus comparison. 3519100c covers the three previously untested paths: SCC from each shift-add, SCC surviving a VALU instruction or an SMEM load, and an unknown carry as the only reason a low-pointer offset is dynamic. Removing each code path makes its new assertion fail, including both operand orders of s_addc_u32.

I clarified that the existing pointer classes describe the base SGPR pair before the load adds its own offset. That leaves the published class names stable. Kev-Ball's full 26,503-listing comparison above shows that the published corpus counts match this rule.

Windows CPU: 19 heap-census tests passed. The existing hard-link case was blocked locally at os.link (WinError 5); the independent check above passed that case. AI-assisted.

@haayden

haayden commented Nov 23, 2026

Copy link
Copy Markdown

Code review of 3519100c (CPU only)

No blocking findings.

  • Ordering in step is correct. carry is read from sym["scc"] before the invalidation block runs. Any op outside the preserving set drops the bit first, and only a known unsigned_add_sub flag or a fully constant shift-add puts it back. So s_cmp_*, the signed s_add_i32/s_sub_i32, and an unsigned add with an unknown flag all leave SCC unknown, never stale.
  • The old cx == 0 and cy == 0 -> ("const", 0) shortcut for s_addc_u32/s_subb_u32 is gone. A zero high word now comes only from unsigned_add_sub with a known carry, so an unknown carry no longer proves ptr32. Shift-add SCC uses the unbounded (cy << N) + cx > U32_MAX, which matches the ISA's 64-bit tmp >= 0x100000000. The join in analyse ({k: v ... if sym.get(k) == v}) treats "scc" like any SGPR tag, so disagreeing paths drop it with no special case.
  • Pointer provenance: ("off", tx, cy is None or carry is None) marks a pointer offset dynamic when only the carry is unknown. carry is 0 for non-carry ops, so s_add_u32/s_add_i32 behave as before.
  • Ran python -I tests/test_heap_index_census.py (Windows 11, CPython 3.14.4). Head: 20/20 OK, including the hard-link test, which passes here. With tools/heap_index_census.py from the merge base 561b65a8, the 144 unsigned_add_sub reference subtests error (no such function) and 12 subtests fail across the five new census tests. tests/test_shader_meta.py (27) and tests/test_ptr_truncation_census.py (9) pass at head. Merges cleanly into upstream/main 867d922f.

Not blocking: SCC_PRESERVING_SALU leaves out several scalar ops that don't touch SCC, such as s_getpc_b64, s_cselect_b32, s_dcache_inv and s_sendmsg. They discard a known carry, which costs some precision but is never wrong. If corpus numbers show lost ptr32 proofs after such an op, they can be added then.

AI-assisted (Claude Code). No game files.

@mralanbourne

Copy link
Copy Markdown

Second check, CPU, Linux (no game files). Run by an agent (Hermes) on mralanbourne's machine; AI-assisted.

Tested 3519100c7 on CachyOS Linux (kernel 7.3.5), CPython 3.14.10, CPU only: pass. heap_index_census: track scalar carry for descriptor addresses (D3D-329).

On this head, from a worktree: tests/test_heap_index_census.py: OK (20 tests). Not a code review; the author's reasoning was not re-derived.

@OlaiFG7

OlaiFG7 commented Nov 23, 2026

Copy link
Copy Markdown

Reviewed 3519100, code read (CPU), AI-assisted: the carry/join/clobber logic reads correct. Ran python -I tests/test_heap_index_census.py: pass (20); with tools/heap_index_census.py reverted to main it fails (12 failures, 144 errors), so the new tests do test the fix. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants