Skip to content

shader/opt: the visit-budget test uses 5 phi levels, where the budget decides (D3D12-SHADER-BOUNDED-BUDGET-TEST) - #2969

Open
aznohonza wants to merge 1 commit into
ProjectLeonida:mainfrom
aznohonza:shader/bounded-budget-test
Open

aznohonza wants to merge 1 commit into
ProjectLeonida:mainfrom
aznohonza:shader/bounded-budget-test

Conversation

@aznohonza

Copy link
Copy Markdown
Contributor

Re-created from the original #2959. aznohonza/mandy-contrib was made private at 21:41 UTC on 2026-12-01, which detached the fork and permanently closed its pull requests; GitHub refuses to reopen them (422 state cannot be changed). Same branch, same head 535c140949179bb31da20d0ecf45375fd3227ea7. The original review thread is at #2959.


bounded_budget_caps_wide_phi_dag is named for the 64-visit budget in Module::bounded(), but it built 10 levels of a 4-way phi DAG. At 10 levels the depth limit answers None after 12 visits, before the budget is reached: taking the budget away (let mut budget = u32::MAX) left the test passing, so it did not test what it says.

Five levels needs 2,729 visits without the budget, so there the budget is what answers None. The test now builds 5 levels, and its comment says why the depth limit no longer decides first.

Verification (Linux, no GPU, no game files):

Test-only: no translator output, no switch, no game data.

Claimed in #2570 (D3D12-SHADER-BOUNDED-BUDGET-TEST). AI-assisted.

… decides

`bounded_budget_caps_wide_phi_dag` built 10 levels of a 4-way phi DAG. At 10
levels the depth limit answers None after 12 visits, before the 64-visit
budget is reached, so the test also passed with the budget removed. Five
levels needs 2,729 visits without it, so the test now fails if the budget is
taken away: None with it, Some(0.0) without.

AI-assisted.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identical to #2959 (535c14094), which I reviewed: 5 phi levels cost exactly 2,729 visits, the 64 budget is the limiter at that depth, and the mutations agree (removing the budget or raising it to 2,729 fails). merge-tree is clean. Good to merge; only the optional low-side assertion (budgets 13–2,728 still pass) is open.

@aznohonza

Copy link
Copy Markdown
Contributor Author

Thanks for the mutation check. The optional low-side assertion is real: the test pins the cap, not that a small tree still answers a bound. The 2-level diamond above it does return Some(0.0) at budget 64, so the "budget does not refuse small trees" side is covered there; I will add the 13–2,728 sweep only if it can be done without pinning the implementation's exact capacity.

AI-assisted.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2969 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files

Verdict: PASS. A test-only change that makes an existing visit-budget test actually discriminate. I confirmed the
mechanism directly: with the visit budget deliberately broken, the new 5-level test fails while the old
10-level test passes — so before this pull request the test was passing for the wrong reason (the depth limit, not
the budget). The whole crate suite is identical on the merge base and on the head.

Head 535c1409, base the current main (8422def7), merge base 1fff6912, 1 file, +4/−3:
d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs.

1. What it changes

The test bounded_budget_caps_wide_phi_dag builds a 4-way diamond DAG of Phis and asserts the final value is capped to
None. It looped 0..10 (10 phi levels); it now loops 0..5 (5 phi levels), with the comment rewritten to say why:

// 5-level 4-way diamond DAG with distinct ops per edge: 2,729 visits
// without the budget, more than the 64 it allows, so the budget (not
// the depth limit, which 10 levels would hit first) caps it to None.
for _ in 0..5 {

The call under test is unchanged: assert_eq!(m.bounded(cur, 12), None); — bounded is entered with a
depth of 12 and an internal visit budget of 64 (opt.rs:463-465, let mut budget = 64;). The change is
test-only: no production code is touched.

2. The new test checks the property that matters

The property is: the wide-DAG value is capped to None by the visit budget, not by the depth limit. bounded
returns None from either of two independent guards — depth.checked_sub(1)? or *budget = budget.checked_sub(1)?
(opt.rs:473-474). At 10 phi levels the recursion is ~20 nested calls deep, which trips the depth limit of 12 first,
so None came back even if the budget were completely broken — the test proved nothing about the budget. At 5 phi
levels the recursion is ~10 deep (under 12, so depth is not the binder) but the DAG still needs far more than 64
visits (my recurrence for the diamond gives ≈1,705 recursive calls; see the note on the comment's number in §4), so
the budget is what returns None.

I verified this empirically rather than by argument. I mutated bounded to disable the budget decrement
(// MUTATION: *budget = budget.checked_sub(1)?; commented out) and ran the test both ways:

Test version Budget broken Result
new (5 levels) budget decrement disabled FAILED — assertion left == right failed: left: Some(0.0), right: None
old (10 levels) budget decrement disabled ok (passes) — the depth limit masks the break

So the pull request's test notices a broken budget and the old one did not. That is exactly what a discriminating test
should do, and it is the whole point of the change. I then restored the source (the mutation lived only in throwaway
debug worktrees, now removed).

3. What the tests do here

python3 -I scripts/red_green.py 2969 --crate leonida-shader (own work-dir and target dir):

Side Result
red (base + only this pull request's test hunk) the test-changed opt.rs hunk applied cleanly
green (base + pull request merged) applied cleanly
outcome 252 tests pass on both sides, none fail on either, verdict no red (exit 1)

no red is the expected verdict for a test-only change with no production fix: the test passes on the base too
(because the base's 10-level version is capped by depth), so nothing goes red→green. That is precisely why the brief's
alternative method — break the tool and check the test notices — is the right one here, and §2 is that check.

I also ran the whole crate suite on the merge base and the head on clean target dirs:

Commit cargo test -p leonida-shader
merge base 1fff6912 250 passed; 0 failed; 2 ignored
head 535c1409 (pr2969) 250 passed; 0 failed; 2 ignored

Identical. (The count differs from red_green's 252 because red_green's target had the two source_hash_tests counted
differently — see §4.)

4. Any failure I saw, and why it is not this pull request

  • source_hash_tests failed spuriously during iteration (the_source_hash_leaves_the_test_modules_out,
    every_test_module_is_built_for_tests_only). Cause: build.rs bakes LEONIDA_SHADER_SOURCE_HASH into the test
    binary at compile time while the test recomputes the hash from src/ at run time; sharing one CARGO_TARGET_DIR
    across worktrees left a stale binary whose baked hash did not match the checked-out tree. It reproduced on plain
    main and on the merge base, which rules out this pull request. With a per-commit clean target dir both the
    base and the head pass 250/250 (§3). Harness artefact, not the code under test.
  • The comment's "2,729 visits" is an approximation, not an exact count. My recurrence for the diamond
    (V(L)=1+4·(1+V(L−1))) gives ≈1,705 recursive calls at 5 levels, not 2,729; the exact figure depends on
    traversal order and the duplicate-incoming skip in the Op::Phi arm (opt.rs:507-515). The number is not asserted
    anywhere and does not affect the test, whose validity rests only on "more than 64 visits, fewer than 12 levels
    deep" — which holds. Worth a one-word fix (~1,700) if the author cares, but it is not a correctness problem and I
    would not block on it.

5. What I did not check

  • No GPU and no running title. This change is entirely inside a Rust unit test; there is no GPU, runtime, or shader
    translation involved, and I did not run any.
  • No Windows build and no cargo clippy/cargo fmt gate; cargo test on Linux only.
  • No corpus run. The test's bounded predicate is exercised only by this unit test; I did not measure how often
    bounded hits the budget versus the depth limit over the real shader corpus, so I cannot say how many real programs
    this test now protects that it did not before.
  • The exact visit count. As in §4, I did not instrument bounded to print the true number of visits for the
    5-level DAG; my ≈1,705 is a calculation from the structure, not a measured value, and the comment's 2,729 I likewise
    did not reproduce.
  • Behavioural risk: none claimed. Because the diff touches only test code and one comment, there is no production
    behaviour to regress; the identical base/head suite (§3) is the whole of the evidence needed and I did not look for
    any beyond it.

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.

3 participants