xshader: add stage-aware loop guards #522
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/522"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T19:27:55Z.
Summary
Stage-aware loop guards for translated guest loops (split out from the earlier combined PR alongside #547 and #549 per @brcly's review):
tools/shader-compiler/src/loop_guard.rs): Sets the default round budget to65,536for both graphics (TUXBOX_LOOP_CAP_GRAPHICS) and compute (TUXBOX_LOOP_CAP_COMPUTE) matchingmain, with block-dispatcher pass limits set to16 * cap(stage)(1,048,576default). Keeping the graphics cap configurable lets us lower graphics in a separate PR once live rounds are measured in ProjectScarlett.hlsl/emitter/control_flow.rs):tb_guardfor per-lane loops without group barriers (BackEdge::Lane)tb_sync_guardfor group-synchronized loops (BackEdge::Group, so every lane in a group sees identical uniform exits)tb_copy_guard{i}atdispatch_cap(stage)for packed-root strided copies (BackEdge::Copy), and no translator cap on verified guest-bounded loops (BackEdge::Bounded).hlsl/structured_cfg/{emit,mod}.rs): Keeps per-looptb_g{id}capped atcap(stage)and adds one sharedtb_nested_guardcapped atdispatch_cap(stage)only on loops with a parent (parent.is_some()), preventing nested structured loops from multiplyingcap^2without adding an extra counter to single-loop programs.dispatch any-host cs_6_5 waves=64intools/shader-compiler/src/tests/hlsl/host_profiles.txt(9b890279474ca108 -> 5e127d5c84740f8e) because its nested loop now emitstb_nested_guard.RayQuery::Proceed()templates: Bounds traversal loops viaTB_LOOP_CAPincard_direct_shadow.hlslandradiance_probe.hlsl.TUXBOX_LOOP_GUARD=0: Restores the previous loop-guard behavior; all three env vars are documented indocs/ENVIRONMENT.md.Verification
TUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>(rebased ontomainf482572a, headbc8604953381)10de:1e84), driver620.73.04,GE-Proton11-14758Python tests passed23synthetic DXC programs passed (3refused,0slower,0failed)656lib +3bin +2integration passed120DXBC tests,24+7XG tests (1ignored),22storage codec tests,9ACP HAL tests, Windows typecheck, Windows buildLanes and fixtures on the GPU: passed (lanes-bindless-compute,lanes-compute-root,lanes-compute-root-recovery,lanes-raster-tables,lanes-shader-cache)Shader translator on the GPU: passed (includingtranslated-mesh,wave-guard-*,lockstep, etc.)Evidence: TuxBox check of bc8604953381, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not runlanes-*suites) have not been run on this commit.AI-assistance disclosure
Prepared and verified with AI-assisted coding tools (submitting contributor verified builds, diffs, and test runs).
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T19:48:57Z.
Thanks for this. Could you split it into separate PRs? The three parts are unrelated and would be easier to review and land on their own:
loop_guard.rsand its uses),--capabilities/--stride-report/--stride-hintsoptions,CbDyn/tbuf_uintvector loads.For the loop guards, two things I'd like to see before merging:
tb_guardis shared by every loop in a program, so 4,096 is a budget for the whole program, not per loop: for example, a pixel shader with nested 64 × 64 loops would reach it and stop early. Could you show the cap doesn't cut off legitimate loops in ProjectScarlett's pixel and mesh shaders (for example by counting rounds on a run, or checking the recorded requests' loop bounds)?One heads-up: it conflicts with a commit on #520 (
e1d5b043) that changes the structured loop header from[loop] for (uint tb_gN = 0u; tb_gN < 65536u; ++tb_gN)touint tb_gN = 0u; [loop] while (true) { if (tb_gN == 65536u) break; tb_gN += 1u;, which saves DXC time. Once one of them lands, the other only needs that line updated, using yourloop_capin place of 65536.Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-29T20:08:45Z.
Some data on the loop guards (
824e481f, from reading it and from recorded requests; not built or run):for (uint tb_gN = 0u; tb_gN < cap; …)restarts each time the loop is entered, so two nested loops can run 4,096² rounds in graphics and 65,536² in compute (what I reported on #489). The comment inloop_guard.rsthat nested loops can't multiply the cap holds for the skip form only.tb_guardis shared, so 4,096 covers the invocation. In the structured form it's per loop entry. The graphics dispatcher also goes from 1,000,000 passes to 65,536.Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T21:38:52Z.
@brcly @NickStallman Thanks for the feedback — I've split #522 into three focused PRs and rebased onto
main(updating the loop-guard tests for4198b48c'swhile (true)loop headers):--capabilities,--stride-report, and--stride-hintsCbDynandtbuf_uintvector MUBUF loads9/9host checks green onorigin/main)Next before asking for re-review, I'll follow up on #522 with:
4,096graphics round cap and nested structured-loop shared counter (@NickStallman's parented-loop suggestion).Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T22:03:00Z.
Heads-up: we rewrote
main's history tonight (one commit per merged PR, to drop files that came in with intermediate commits), and this branch is still based on the oldmain(on the old #512 merge,39463ea5). That's why GitHub now shows it with dozens of commits that aren't yours. Your own commits are the ones after39463ea5; this replays only those onto the newmain:Sorry for the extra step.
Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T22:13:58Z.
@brcly @NickStallman Follow-up on nested structured loops and the DXC compile-time / DXIL-size comparison (
e03223e3onmain97f21fad):1. Nested structured loops (
tb_nested_guard)Per @NickStallman's note (and
#489), I added a shared function-widetb_nested_guardinhlsl/structured_cfg/{emit,mod}.rscapped atdispatch_cap(stage)(65,536graphics /1,048,576compute) only on loops withparent.is_some()(and omitted whenTUXBOX_LOOP_GUARD=0):tb_gN == cap(stage)check; inner/parented loops checktb_gN == cap(stage) || tb_nested_guard == dispatch_cap(stage), bounding total nested iterations instead of multiplyingcap^2.loop_guard.rsthat claimed all forms shared one counter, addednested_structured_loops_share_a_parented_budget, and blessed the one affectedhost_profiles.txtentry (dispatch any-host cs_6_5 waves=64).tuxbox.py check --no-gpu:9/9passed (728Python OK,656+3+2shader compiler).2. Before / after DXC time and DXIL size (
sample.json)Measured sequentially with
tools/stage_baseline.py cpu --jobs 8across the58requests fromtools/reference/stage-baseline/sample.jsonpresent in our local archive (31CS -> 62 translations,12MS,9VS,6PS =89translations total):n32cs,4ps,4ms,1vs)61750.8 -> 61769.7(+0.030%)18103.7 -> 18150.9(+0.261%)253.23 -> 228.73(-9.68%)7.617 -> 6.671(-12.42%)compiled:89/89both)105668.2 -> 105687.0(+0.018%)34958.8 -> 37330.3(+6.784%)*548.94 -> 545.75(-0.58%)7.290 -> 6.252(-14.24%)47/89translations had byte-identical HLSL and identical DXIL SHA-256 (ms/vswere identical DXIL bytes).+2,324 KiBoutlier in "All" is a single computewave32shader (b413657f5e5c) with byte-identical HLSL (7,136,495 Bin both runs) that finished at54.09son baseline and hit the DXC-O0fallback timeout at86.85s(killed=1) on the candidate run.1pixel shader in the sample (6a53b56d6ae8) grew DXIL (+224 B) fromtb_nested_guard.3.
4,096graphics cap & ProjectScarlett statusProjectScarlett has not been run with this PR yet, so I haven't done a live round-counting pass across graphics/mesh shaders to prove
4,096never truncates a legitimate non-structured pixel/mesh loop. Before asking for a merge, would you prefer:65,536default for graphics too (matching main's existing per-loop cap, while still gainingtb_nested_guard,tb_sync_guard, theRayQuery::Proceed()/view-cull guards, andTUXBOX_LOOP_CAP_*) and lowering graphics separately once measured?Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T22:20:16Z.
Thanks for the split and the nested budget. On your question: please keep 65,536 for graphics in this PR, matching
main, and lower it in its own PR once you've counted live rounds in ProjectScarlett. That way this PR only adds bounds, and nothing that runs today gets cut short.On the numbers: thanks for measuring. With 41 changed translations and one identical-HLSL shader landing on the
-O0timeout in only one run, the sample is too noisy to call it faster or slower, so we'll measure it here on the recorded requests when it's ready (DXC time with the runtime's flags, including-fnew-inlining-behavior, and DXIL size), and run the GPU checks on both cards. Tell us when the 65,536 change is in.Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-30T00:05:03Z.
@brcly Updated and rebased onto
main(f482572a, commitbc8604953381):65,536, matchingmain(dispatch_capandtb_nested_guardare1,048,576for both graphics and compute), while keepingTUXBOX_LOOP_CAP_GRAPHICSas a separate override so graphics can be lowered later after measuring live rounds in ProjectScarlett.loop_guard.rsdocs,docs/ENVIRONMENT.md, andtools/shader-compiler/src/tests/loop_guard.rs(keeping4096only as an explicit override test case).Verified on NVIDIA GeForce RTX 2070 SUPER (
10de:1e84, driver620.73.04,GE-Proton11-14) withTUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>(758Python tests,656+3+2xshader, all 5 changedlanes-*suites, and all translator GPU checks passed; AMD GPU checks still needed, ProjectScarlett not run):Evidence: TuxBox check of bc8604953381, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not runImported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T00:49:05Z.
The AMD check you asked for, on
bc860495: AMD iGPU (1002:13c0, RADV, Mesa 26.0.8, GE-Proton11-14, Linux), fullpython tuxbox.py check: 197 pass, 2 fail. The two ared3d12-queuesandd3d12-queries, each stopped by its 30 s limit, with the machine at load 18 (another check and builds were running). Run on their own straight after, at load about 7, both pass in 3 of 3 runs, so I read the timeouts as our load, not the change. It isn't a clean full check. ProjectScarlett wasn't run on it.Evidence: TuxBox check of
bc86049533, no uncommitted changes, GPU 1002:13c0: 13 passed, 1 failed, 0 not runImported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T04:12:22Z.
Heads-up: main just landed a large cleanup (up to
e5d2e913), so this PR needs a rebase.rustfmt.toml).unwrap(), lossyascasts and undocumentedunsafeoutside tests.cargo fmt --check, rustdoc with warnings denied, and Miri on the providers' unit tests.The least painful way is to format your branch first, so the rebase only shows real changes:
Then run
python tuxbox.py check --no-gpu, which now includes the format step.If clippy flags something new:
Fromfor widening,TryFromfor narrowing, and a mask for bit fields.unwrap(): use?, orexpectwith the reason it can't fail.unsafeblock: it needs a// SAFETY:comment.CONTRIBUTING's "Code checks" section lists the commands. Thanks!