Repository navigation
shader: lower bounded per-lane heap record indices in compute waterfalls (#2644 T3) - #2861
Conversation
KelboLive
left a comment
There was a problem hiding this comment.
Comprehensive review of PR for T3 of #2644: bounded per-lane heap record indices in compute waterfalls.
Summary
This PR is a significant and well-designed addition to the compute shader translation pipeline. It enables lowering of bounded per-lane resource-heap record selections in compute waterfalls by:
- Forward CFG VGPR bounds tracking to prove per-lane indices are bounded
- Extended root signature with a descriptor range (space 2) for up to 1024 resheap SRVs
- Runtime pre-bind of all resheap SRVs with strict index validation
- Comprehensive refusal paths rejecting unbounded, modified, computed, or non-resheap accesses
Translator Changes (gfx2hlsl.py, ~350 additions)
vgpr_heap_bounds()
Solid forward CFG dataflow over explicit bounding forms (v_and_b32, v_min_u32, v_bfe_u32, v_lshrrev_b32, pre-shifted v_lshlrev_b32, v_mov_b32). Handles EXEC changes correctly by requiring joins on every path after EXEC modifications. The flag propagates through branches and merges. RESHEAP_RANGE_MAX = 1024 caps bounds conservatively.
Key design notes:
- Work queue (BFS) ensures all successors are visited and merged
- iterates over register ranges from multi-register destinations
- Single-register writes preserve bound info; multiple-register writes lose it (safe)
- Branch targets clear and re-merge states correctly
waterfall_heap_check()
Excellent analysis of the waterfall body between readfirstlane head and s_andn2_wrexec close:
- Validates s_add_u32 + s_add_u32/s_addc_u32 64-bit carry pairs for extended pointer arithmetic
- Tracks local_idx / local_off5 / local_ptrs / local_descs through the body
- Ensures the resource descriptor loaded is the resheap SGPR (not a modified/computed value)
- Rejects if pending carry is unresolved at close (the loop ends with carry pending)
_complete_resheap_carry()
Handles the second half of the 64-bit carry in the scalar lowering pass. The comparison ensures the carry is right after the add. The check for const(0) on the high word confirms it's a proper 64-bit address formation.
Per-lane index tracking
The dict maps SGPRs to from the waterfall head's readfirstlane. This propagates through mark_heap_index when s_lshl_b32 ... 5 is applied to the per-lane index, creating 'per_lane' heap marks.
The symbol chain is correctly established:
per_lane index → s_lshl_b32 → heap_index[d] = ('per_lane', expr, bound)
→ s_load_dwordx4 → _capture_resheap_desc → sym = ('desc', per_lane, ...)
Runtime Changes (C++)
d3d12_compute_dispatch.inl
- field parsed from manifest line
- Validates and
- now builds vector
- Iterates over heap SRVs reading each 16-byte record from pass.title_view_heap
- Validates before reading
- Rejects indirect Dispatch when heap SRVs are present (correct conservative choice)
d3d12_draw.inl
- Tracks from the descriptor heap set by slot 32
- Sets for later validation
translated_sampler_bindings.h
- New constants: ,
- signature extended with parameter
- Descriptor ranges built dynamically with (only includes ranges with non-zero size)
- Space 2 SRV range starts at offset
Refusal Path Safety
The PR establishes clear refusal paths for unsupported cases:
- Unbounded per-lane index: returns None → refusal
- Modified source SGPR: tracked writes discard from local_idx/local_off5
- Computed descriptor (non-resheap SGPR): load_hit fails
- Non-resheap pointer:
- Exit-live scalar: rejected by / checks
- Formatted/idxen stores: explicitly rejected in
- Store through resheap: rejected in
- Indirect Dispatch with heap SRVs: refused before Dispatch
Test Coverage
The new test file (483 lines) is thorough:
- Oracle mode: tests 5 key patterns + active-lane subsets through d3d12_xs.dll
- Dispatch mode: end-to-end with debug layer and raw DXIL verification
- NonUniformResourceIndex metadata checks via DXIL IR parsing
- Short-range control (3-descriptor heap omitting record 3) that refuses cleanly
- 64-bit carry form tested alongside direct offset
10/10 tests passed on WARP and hardware (XBOX2WINDOWS_ADAPTER=7900)
Minor Notes
- The in the test file uses nonUniformIndex only when a bounded per-lane index is detected; scalar/constant indices use direct indexing (correct HLSL behavior)
- The temporary SGPRs are declared but only emitted if non-empty (clean)
- The set ensures matching low/high word pairs for 64-bit extended pointers
- The manifest line emits both count and space, matching the runtime parser
Verdict
This is a well-designed, thoroughly tested implementation. The CFG dataflow analysis is principled and conservative (bounds are only propagated when proven on every path). The refusal paths are comprehensive. The runtime validation provides an additional safety layer. I see no issues that need addressing.
Approved for merge.
KelboLive
left a comment
There was a problem hiding this comment.
Comprehensive review of PR for T3 of #2644.
Summary
This PR implements lowered bounded per-lane heap record indices in compute waterfalls. The approach is sound: CFG dataflow bounds tracking, runtime pre-bind of up to 1024 resheap SRVs in space 2, strict index validation, and comprehensive refusal paths.
Translator (gfx2hlsl.py, ~350 lines added)
- vgpr_heap_bounds(): Forward CFG dataflow over explicit bounding forms (v_and_b32, v_min_u32, v_bfe_u32, v_lshrrev_b32, pre-shifted v_lshlrev_b32, v_mov_b32). EXEC changes handled correctly by requiring joins on every path after EXEC modifications.
- waterfall_heap_check(): Validates the waterfall body between readfirstlane head and s_andn2_wrexec close. Handles 64-bit carry pairs (s_add_u32 + s_addc_u32). Rejects pending unresolved carries.
- _complete_resheap_carry(): Handles the second half of the 64-bit carry in the scalar pass. const(0) check on high word confirms proper 64-bit address formation.
- Per-lane index tracking propagates through mark_heap_index when s_lshl_b32 ... 5 is applied, creating per_lane heap marks.
Runtime (C++)
- heap_srvs parsed from manifest resheap N space line, validated against max_translated_resheap.
- record_translated_dispatch iterates heap SRVs reading each 16-byte record from title_view_heap, validates index < title_view_heap_entries.
- Rejects indirect Dispatch with heap SRVs (correct conservative choice).
- Descriptor ranges built dynamically with range_count (zero-size ranges omitted).
Refusal paths cover: unbounded index, modified SGPR, computed descriptor, non-resheap pointer, exit-live scalar, formatted/idxen, indirect Dispatch with heap SRVs.
Tests: 483 line test file with oracle/direct/DS modes. NonUniformResourceIndex metadata checks via DXIL IR parsing. 10/10 pass on WARP and hardware.
Verdict: Well-designed implementation with thorough refusal paths. No blocking issues.
Approved for merge.
|
Second-site reproduction on a different AMD generation (RX 9070 XT, RDNA 4): all four waterfall-heap entries pass (two WARP, two hardware), and the fixture has no negative control for the pre-loop stride Not a code change; measurements only. Environment
CommandsThree consecutive runs, twelve executions, Observed4 of 4 passed on every run. The fixture's own lines: The so the The fixture has no negative control for the pre-loop strideThe probe below builds the fixture's own programs and only asks for translation; no device is involved. Baseline: The guard is there and sound; nothing tests it in the pre-loop form. One-line mutation of The emitted line is unchanged ( No fixture entry exercises a pre-loop stride other than 5, so relaxing the guard changes no verdict: under the mutation the four entries still pass, 4 of 4, A control of the same shape as the accepted pre-loop case would close it: ('wrong pre-loop shift',
build_program(bound=(MASK_AND3, ('v_lshlrev_b32_e32 v5, 4, v5', '840a0a34')),
body=(*CARRY_ADD_S22, WAIT_LGKM, BODY_LOAD, WAIT_VM)),
'per-lane descriptor selection requires a proven bound heap range'),
Limits
AI-assisted. |
|
Thank you for the second-site RDNA 4 (\AMD Radeon RX 9070 XT) verification and for catching the missing pre-loop stride negative control. Added the \wrong pre-loop shift\ control (\�_lshlrev_b32_e32 v5, 4, v5\ / \840a0a34\ in \ENCODINGS, checked by \check_translator\ and \llvm-mc-15) in commit \ee6edd60:
|
|
Independent re-check of the new control on Not a code change; measurements only. What the commit touches
Does the added control bite?One worktree, same fixture path, The relaxation is What that pair of runs does not sayThe control loop aborts on the first failure (332-336), and the added control is the seventh of the sixteen entries, so under the relaxation the nine entries after it and the trailing What the relaxed guard acceptsSame fixture imported, translation only, no device: The accepted pre-loop case (253) and the new control (297-300) are the same The in-body form carries its own guard, Where this check runs
EnvironmentWindows 11 25H2, build 26200.9974, CPython 3.15.2 from Limits
AI-assisted. |
|
This PR now conflicts with
Could you merge AI-assisted; commands listed were executed locally. ( |
…erfall-heap-record # Conflicts: # tests/startup_stream_consumer.cpp # tools/startup/gfx2hlsl.py
|
@tedin7 Merged Local verification on
|
…erfall-heap-record # Conflicts: # src/startup_stubs/d3d12_compute_dispatch.inl # tools/startup/gfx2hlsl.py
|
Heads-up: #3083 (comparison samplers in the published compute manifest reader) just landed, and this PR now conflicts with Command: AI-assisted; commands listed were executed locally. |
Resolved conflicts: - src/startup_stubs/d3d12_compute_dispatch.inl - tests/startup_stream_consumer.cpp
|
Merged Conflicts and how they were resolved.
What I ran. I built everything in ctest --test-dir out/build/<preset> --no-tests=error -j 8 -R '^(startup_compute_.*|startup_slot67_.*|startup_sampler_comparison)$' -E '_hardware$'
python with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_compute_.*|startup_slot67_.*)_hardware$'
Both runs include this branch's This needs a re-review by someone else; I won't review or merge it myself. Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; SDK 10.0.28000.0 DXC (the one CTest picked up); Python 3.12.10. This reply was AI-assisted. |
pigalot
left a comment
There was a problem hiding this comment.
I reviewed 0656c2bf (c's merge of main) merged over main c827d8ba. That merge was clean (tree e4705363) and git diff --cached --check was clean.
Main has moved since. main 5033d2d3 (#2862) now conflicts with this PR in tests/startup_stream_consumer.cpp, in dispatch_slot67:
- #2862 sizes the allocation with
long_run ? 0x28000 + long_records * sizeof(UINT) : ..., and movesbetweento0x28000for its long-run modes. - This PR sizes it with
heap_waterfall ? 0x60000 : ..., and puts the four heap record buffers at0x20000 + rec * 0x10000.
The modes don't overlap, so I expect the resolution to keep both. But this needs another merge of main, and that merge needs its own review. d3d12_compute_dispatch.inl auto-merges with #2862; I have not tested that combination.
c's earlier resolution. I checked git show --remerge-diff 0656c2bf. In d3d12_compute_dispatch.inl it keeps both sides: main's changes and this PR's resheap manifest line, the count + heap_srvs view vector, and the heap-record loop.
What I traced.
vgpr_heap_boundsproves a per-lane index bound fromv_and/v_bfe/bounded copies. It joins bounds across CFG edges (both sides must be bounded) and drops them under a narrowed EXEC.waterfall_heap_checkaccepts the readfirstlane waterfall only when the heap base SGPR is not written in the body, and only when root flag0x400is set.- The emitted access is
rh[NonUniformResourceIndex(...)]. - At runtime, the manifest
resheap <base> <count>line adds a third descriptor range in space 2. Each record must fall insidetitle_view_heap_entries, or the dispatch is refused.
What I ran. I built dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R 'startup_(compute|gfx2hlsl)' -E '_hardware'
python .agentic\scripts\with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R 'startup_(compute|gfx2hlsl).*_hardware'- Non-hardware: 369/369 pass in both Debug and Release
/O2. Two tests skip with 77, the same as onmain:startup_compute_descriptor_scc_warpandstartup_compute_dispatch_native_warp. - Hardware: 190/190 pass in both presets.
- Translator mode:
tests/startup_compute_waterfall_heap_check.pypasses, both with and without--dxil. ctest doesn't register this mode, and it runs only in the Linux CI job. - Negative control: on
mainbf1bc769, the PR's check fails in translator mode with "per-lane descriptor selection requires a proven bound heap range". The--dispatchmode has noheap-waterfallcase inmain'sstartup_stream_consumer.
Runtime mutants. Each was rebuilt in dev, then I ran startup_compute_waterfall_heap_*:
| Mutant | Result |
|---|---|
heap entry count not checked against title_view_heap_entries |
killed (refused later, with a different reason) |
| last heap record skipped | killed (direct case 0, word 3: got 0, expected 0xfd3) |
| heap views created as UAVs | killed (device removed, DXGI_ERROR_INVALID_CALL) |
| heap range placed in space 0 | killed (pipeline creation fails) |
Translator mutants. I ran each against the translator check, with and without --dxil:
| Mutant | Result |
|---|---|
| bound accepted under a narrowed EXEC | killed |
| CFG join keeps a one-sided bound | killed |
v_bfe bound one low |
killed |
NonUniformResourceIndex dropped |
killed |
root flag 0x400 not required |
killed |
heap base SGPR written in the body accepted (body_writes check removed) |
survives |
v_mov copy of an unbounded VGPR treated as bounded |
survives |
Restored afterwards; git diff --quiet is clean.
Non-blocking.
- No control for a copy of an unbounded VGPR. I probed with
v_mov_b32 v5, v9as the index source and no bound onv9. The PR refuses it, and withv_and_b32 v9, 3, v9first, it accepts it (resheap 4 2). But with the copy rule loosened, nothing in the check fails. A controls entry for the unbounded copy would pin this. - The
body_writesguard looks redundant. With it removed, the same probe (s_mov_b32 s8, 0in the body) is still refused by an earlier check. So the surviving mutant is not a gap in coverage. It may still be worth keeping as defence in depth. - The translator controls are Linux-CI-only. The 17 translator controls run only in
Run platform-independent Python checks on Linux; Windows ctest runs only--oracleand--dispatch. Registering the translator mode in ctest would let local runs catch translator regressions too.
Verdict: approve on 0656c2bf. It can't merge until the conflict with #2862 is resolved, and that merge needs re-review.
Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Windows SDK 10.0.28000.0 DXC, Python 3.12.10, wave32; I did not check the debug-layer status.
CI: Build and test on Windows passes on 0656c2bf.
This reply was AI-assisted.
|
I merged Conflicts and how they were resolved. Both were textual. In each case I kept both sides.
What I ran on ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R '^(startup_|compute_)' -E '(startup_audio|_hardware)'
python .agentic\scripts\with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_|compute_).*_hardware$' -E 'startup_audio'
Because I pushed this merge, I won't review or merge it myself. It needs a re-review by someone else. Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Windows SDK 10.0.28000.0 DXC, Python 3.12.10, wave32; I did not check the debug-layer status. This reply was AI-assisted. |
There was a problem hiding this comment.
Retested 8a133409 (f's merge of main 240e0fb9) integrated over current main 6a1968a1. The merge is clean (tree 1a829b53) and git diff --cached --check is clean.
The main merge in 8a133409. I checked it with git show --remerge-diff 8a133409. All three conflicts keep both sides:
tests/startup_stream_consumer.cpp,dispatch_slot67: the allocation is nowheap_waterfall ? 0x60000 : long_run ? 0x28000 + long_records * sizeof(UINT) : (indirect || root_srv) ? 0x30000 : 0x20000, andbetweenkeeps #2862'slong_run ? 0x28000 : 0x8000. The PR's four heap record descriptors are unchanged.heap_waterfall(dispatch-heap-waterfall*) andlong_run(dispatch-input-snapshot-longorkeep_closed) come from different modes, so the two sizes never meet.tools/startup/gfx2hlsl.py, waterfall detection: this branch'swaterfall_refusal(..., vbounds)call is kept, and thepre_submask-subset dataflow from #2877 follows it unchanged.tools/startup/gfx2hlsl.py, SRV declarations:main'stexcubeentry is kept, followed by this branch'srh[]resource-heap declaration.
Apart from those three hunks, the PR's diff against its merge base is the same as at 0656c2bf. So the earlier review of 0656c2bf, with its traced bounds proof, negative control and runtime mutants, still covers the code.
What I ran.
- Builds: full
devandrelease/O2. - Translator mode:
python -B tests/startup_compute_waterfall_heap_check.pypasses (translator plus 17 refused controls). - CPU:
ctest --test-dir out/build/<preset> --no-tests=error -j 8 -R '(startup_compute_waterfall|startup_compute_dispatch|startup_gfx2hlsl_|compute_check|stream_consumer|startup_flat_param|ps_raw_descriptor)' -E '(_hardware$|startup_audio)'gives 285 tests in each preset: 284 pass, 0 fail and 1 is a capability skip (startup_compute_dispatch_native_warp, which the review of0656c2bfalso saw skip onmain). - Hardware: under the GPU lock,
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^(startup_compute_waterfall_heap_.*|startup_compute_dispatch.*|startup_flat_param)_hardware$'passes 14/14 in each preset, with no skips. That includesstartup_compute_waterfall_heap_oracle_hardware,startup_compute_waterfall_heap_dispatch_hardwareand the 11startup_compute_dispatch_*_hardwaretests that sharedispatch_slot67. - Full suite:
ctest --test-dir out/build/dev --no-tests=error -j 8 -E '(startup_audio|_hardware$)'gives 2042 tests: 1982 pass, 0 fail and 60 are capability skips.
Verdict: approve on 8a133409.
Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088; Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; DXC 1.9.2602.17; Python 3.12.10; wave32; debug layer off.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 8a133409.
This reply was AI-assisted.
Completes T3 of #2644 (per-lane resource-heap record indexing inside a compute
v_readfirstlane_b32waterfall loop). Refs #2644.Summary
tools/startup/gfx2hlsl.py):vgpr_heap_bounds) over explicit bounding forms (v_and_b32,v_min_u32,v_bfe_u32,v_lshrrev_b32, pre-shiftedv_lshlrev_b32 ..., 5, andv_mov_b32) with strictEXEChandling across branches/merges (RESHEAP_RANGE_MAX = 1024).waterfall_refusal/waterfall_heap_checkandStage.heap_record/Stage.scalar(_complete_resheap_carry) to lower boundedresheaprecord loads (s_lshl_b32 ..., 5+s_load_dwordx4 s[...], s[heap:0], sOffand the adjacent 64-bits_add_u32+s_add_u32 ..., 0, src_scc/s_addc_u32carry form) toByteAddressBuffer rh[N] : register(t0, space2);indexed viarh[NonUniformResourceIndex(...)], publishingresheap <N> 2in compute manifests while refusing unbounded indices, modified descriptors, non-resheap pointers, exit-live scalars, and store/formatted/idxenaccesses.src/startup_stubs/translated_sampler_bindings.h,d3d12_compute_dispatch.inl,d3d12_draw.inl,d3d12_xs_device.cpp):create_translated_rootandComputeTranslationto acceptresheap <N> <space>(space 2, up to1024entries), validate every reachable descriptor0..N-1inpass.title_view_heap(also trackingtitle_view_heap_entriesso stride-64 devices cannot accept out-of-range descriptor indices by byte size), and bind allNraw SRVs beforeDispatch(refusing cleanly beforehand on short-range heaps or invalid descriptors).tests/startup_compute_waterfall_heap_check.py,tests/compute_check.cpp,tests/startup_stream_consumer.cpp, CMake/CI fragments):reference,static-switch,first-activeproducing 12/25 mismatches,no-marker) via--compute-waterfall-oracle.cs_6_0DXILnonUniformIndexmetadata verification (NonUniformResourceIndexpresent vs omitted).Dispatchtests for both direct-offset and 64-bit carry forms across all five oracle key patterns and partial active counts, plus a short-range control (3-descriptor heap omitting highest record3) that refuses beforeDispatch.Verification
Commit tested:
c15baa0b(rebased cleanly onmain6a094052).Environment: Windows 11 (
10.0.26200.9974), DXC1.8.2502.11, adaptersMicrosoft Basic Render Driver(WARP) andAMD Radeon RX 7900 XTX(PCI\VEN_1002&DEV_744C, driver32.0.32029.2010), D3D12 debug layer enabled for stand-inDispatchchecks../out/pe-env/Scripts/python.exe -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux tests/startup_compute_readfirstlane_check.py tests/startup_gfx2hlsl_heap_record_check.py tests/startup_gfx2hlsl_heap_word_check.py tests/startup_gfx2hlsl_compute_check.py tests/bidi_controls_check.py tests/run_checks_parallel_check.py-> all 11 checks passed (startup_compute_waterfall_heap: PASS (translator + 16 refused controls),run_checks_fragments_check ok,bidi_controls_check, etc.)./out/pe-env/Scripts/python.exe -B tests/docs_links_check.py->Checked 28 Markdown files and 543 links.passed./out/pe-env/Scripts/python.exe -B tests/startup_compute_waterfall_heap_check.py --dxil dxc.exe->PASS (nonUniformIndex true with intrinsic, false without)XBOX2WINDOWS_ADAPTER=7900, after final edit):ctest --preset dev --no-tests=error -R "^(startup_compute_waterfall_heap_|startup_compute_dispatch_heap|startup_translated_sampler_bindings)" --output-on-failure-> 10/10 passed (includingstartup_compute_waterfall_heap_oracle_warp,startup_compute_waterfall_heap_dispatch_warp,startup_compute_waterfall_heap_oracle_hardware,startup_compute_waterfall_heap_dispatch_hardware, and existingstartup_compute_dispatch_heap*/startup_translated_sampler_bindings*).