victory-shader: make the test dispatches cheap - an event wait and reused command objects (emit suite -33% wall, -48% CPU) #1734
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1734
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1734"
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?
@pes7 on GitHub · 2026-11-24 21:31 UTC · original
pes7:test/fence-event-wait→main· +140 −33 · 2 files · 3 commits · openThe shader test suite spent most of its time on the harness, not on the shaders. Two things in
emit::host:drawanddispatchwaited for their fence by spinning, callingGetDeviceRemovedReason()on every iteration;emit::valu_testsruns hundreds of dispatches for one shader (every f16 pattern, for two opcodes, on two backends), so both costs multiply.1. Wait on an event, not in a spin
One
waithelper for both call sites: create an event,SetEventOnCompletion, thenWaitForSingleObject(100 ms)in a loop. The timeout is what keeps the device-removal check - a removed device never signals, so anINFINITEwait would hang the run instead of reporting it - andWAIT_FAILEDreturns an error rather than looping.Same shape as the submission wait in
victory-gpu/src/host.rs, which usesINFINITEbecause the runtime owns the device; here the check has to stay.Interleaved A/B, 9 runs each,
emit::with--test-threads=8:CPU -20%, wall unchanged: the GPU work was the critical path and the machine had spare cores. Every event run stayed under 4.66 s of CPU; eight of the nine spin runs were at 4.88 s or above.
2. Make the command objects once per host
Timing the five creations alone (200 iterations, release, hardware device) gives 4.19 ms, against 8.35 ms for a whole dispatch of
emit::valu_tests- half of a dispatch was spent on objects that do not depend on the dispatch's inputs.Hostnow keeps them in aReusedstruct (queue,allocator,list,fence,event, last fence value) and each dispatch bumps the fence value andResets the list with its own PSO. That is safe here because the GPU tests already run one at a time (ONE_AT_A_TIME) and every dispatch waits for its fence before returning, so the allocator is never in use when it is reset.The event is closed by
Drop for Reused;drawstill makes its own, since it is not on the hot path.v_cvt_norm_i16_and_u16_f16_round_the_exact_product(576 dispatches)emit::suite, wallemit::suite, CPUBoth, against the original
emit::wallemit::CPUWhat this does not claim
That it fixes the flaky
v_cvt_norm_i16_and_u16_f16_round_the_exact_product(#1633). I tried to make the flake show up by running two full suites in parallel, four rounds per variant: 0/16 failures in both, so the link is unproven and I am not claiming it.Scope
drawanddispatchare#[cfg(test)], so nothing in the game changes: this is the shader test suite's time.crates/victory-shader/Cargo.tomlgainsWin32_SecurityandWin32_System_Threadingfor the optionalwindowsdependency -CreateEventWsits behind both, andvictory-gpualready enables them.Checks
cargo clippy --workspace --all-targets --locked -- -D warnings, the same with--all-features,cargo test --workspace --locked,cargo xtask policy.To reproduce: build the test binary with and without the change (
target/release/deps/victory_shader-*.exe) and run them alternately,TotalProcessorTimefor CPU and the reported time for the 576-dispatch test.What it gives (to decide the merge): the shader test host waits for its fence on an event instead of spinning, and makes its queue, allocator, list and fence once per host: the
victory-shadersuite took 199 s before the rebase and 115 s after it (302 tests), with the same device-removal reporting.State: MERGEABLE, CI
checkgreen, rebased onto today'smain(f3578e3).Risk: test-only code; a wedged device is still reported instead of waited for.
`draw` and `dispatch` poll `fence.GetCompletedValue()` and call `GetDeviceRemovedReason` on every iteration while a thread spins. One event per submission, `SetEventOnCompletion`, and `WaitForSingleObject` in 100 ms slices: the same device-removal report, no CPU burned while the GPU works, and no device call in a hot loop. The 100 ms timeout is what keeps that check: a removed device never signals, so an INFINITE wait would hang the run instead of reporting it. `WAIT_FAILED` returns an error rather than looping. Interleaved A/B, 9 runs each, `emit::` with --test-threads=8: wall 9 080 ms -> 9 084 ms CPU (median) 5 562 ms -> 4 438 ms -20% Every event run stayed under 4.66 s of CPU; eight of the nine spin runs were at 4.88 s or above. `draw` and `dispatch` are `#[cfg(test)]`, so nothing in the game changes. Cargo.toml gains Win32_Security and Win32_System_Threading for the optional windows dependency (CreateEventW sits behind both); victory-gpu already enables them. cargo test --workspace --locked; both clippy configurations with -D warnings; cargo xtask policy.A dispatch made a new queue, allocator, list, fence and event every call. Making those five costs 4.19 ms of an 8.35 ms dispatch (200 iterations, release, hardware device), and emit::valu_tests runs hundreds of dispatches for one shader: every f16 pattern, two opcodes, two backends. Keep them in `Host`, bump the fence value per dispatch, and reset the list with the dispatch's own PSO. That is safe here because the GPU tests run one at a time (ONE_AT_A_TIME) and every dispatch waits for its fence, so the allocator is never in use when it is reset. `Drop for Reused` closes the event; `draw` still makes its own, since it is not on this path. v_cvt_norm_i16_and_u16_f16_round_the_exact_product 4.81 s -> 2.17 s emit:: suite, wall 9 084 -> 6 053 ms emit:: suite, CPU 4 438 -> 2 891 ms Together with the event wait, against the original code: emit:: wall 9 080 -> 6 053 ms and CPU 5 562 -> 2 891 ms. cargo test --workspace --locked; both clippy configurations with -D warnings; cargo xtask policy.@pes7 on GitHub · 2026-11-24 22:03 UTC · original
@catminou4 - thank you, and one thing changed after your run at
21:41: there is a second commit now (head90ebae3, and the PR title says "reused command objects"). Your approval covered the event wait only, so this is what is new, in case you want to look again.A dispatch also made a new command queue, allocator, list, fence and event on every call. Making those five costs 4.19 ms of an 8.35 ms dispatch (200 iterations, release, hardware device), and
emit::valu_testsruns hundreds of dispatches for one shader -v_cvt_norm_i16_and_u16_f16_round_the_exact_productalone is 576.Hostkeeps them in aReusedstruct now, bumps the fence value per dispatch andResets the list with its own PSO. Safe here because the GPU tests already run one at a time (ONE_AT_A_TIME) and every dispatch waits for its fence before returning, so the allocator is never in use when it is reset.Drop for Reusedcloses the event;drawstill makes its own, since it is not on that path.emit::suite, wallemit::suite, CPUAgainst the original code that is wall 9 080 -> 6 053 ms and CPU 5 562 -> 2 891 ms.
cargo test --workspacereports 4.98 s for this crate's lib tests instead of 8.69 s.Nothing else changed, and it is still
#[cfg(test)]only. Both clippy configurations with-D warnings,cargo test --workspace --lockedandcargo xtask policypass on the head.@upipibi on GitHub · 2026-11-24 23:05 UTC · original
NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug,
0d4ea57(onmain70564ae).cargo test -p victory-shader --locked: 206 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA heap-cap failures onmain, not this PR. Wall time for that run: 24 s. A tree without this change (#1757 on the same base, same machine, run right after) took 53 s for 208 tests, consistent with the speed-up described here.AI-assisted (Claude Code). Synthetic tests only.
@Marvisak on GitHub · 2026-11-25 02:16 UTC · original · edited 2026-11-26 00:22 UTC
Automated triage for the maintainers.
Merge candidate. Still a test-only speed-up of the GPU test host in
crates/victory-shader/src/emit/host.rs. The fence wait uses an event (wait) and still checksGetDeviceRemovedReasonon each 100 ms timeout and after the wait.Host::dispatchkeeps its queue, allocator, list, fence and event inReused, resets the allocator beforelist.Reset, and drops the objects if a previous recording never reached its fence. All of the new code is#[cfg(test)]. TheCargo.tomlchange only adds two features ofwindows, which is already a dependency.On your reply: the new head (
7d6cff9) is a rebase onto today'smain. I checked that all three constructors (newon its WARP and hardware branches, andfrom_device) initialisereused, and that main'screate_device/WARP setup is kept unchanged. The rest matches what was reviewed at34965a8.CI: passing (
check).Generated by Claude Code
@Damokles2 on GitHub · 2026-11-25 12:18 UTC · original
Hi @pes7,
While investigating D3D12 device-loss handling, I noticed that
fuses_fmad()still has a similar gap on both upstream main and your current PR head.If
GetCompletedValue()returnsUINT64_MAXafter device removal, the polling loop can exit and reach readback without explicitly checking the removal reason.Since
fuses_fmad()also runs during production adapter initialization, it appears to be outside the current test-only scope of #1734.Would you prefer to include this small post-wait check in your PR, or would you be comfortable with me investigating and preparing a separate follow-up?
The finding is based on source inspection and Microsoft's documented fence behavior. I haven't reproduced native device removal yet.
AI-assisted investigation using Codex.
@pes7 on GitHub · 2026-11-25 12:46 UTC · original
Both fixed in
34965a8- thank you, the first one was a real leak.The allocator is reset now, before the list:
r.alloc.Reset().map_err(err)?inside the sameunsafeblock, after the previous dispatch's fence and beforelist.Reset. Measured cost, in case anyone wonders: four interleaved pairs of theemitsuite, middle two averaged - 10.5 s of CPU with the reset against 11.1 s without, i.e. nothing, and the memory of hundreds of recordings pervalu_testscase no longer accumulates.A failed recording is dropped instead of reused.
Reusedcarriesrecording: Cell<bool>, set just beforelist.Resetand cleared only oncewaithas returned, so "open list / allocator in use" is exactly the state that skips reuse:A case that fails now fails once, and the next dispatch starts from a fresh queue/allocator/list/fence/event.
@Damokles2 - on
fuses_fmad(): a separate PR, please. It runs during adapter initialisation, which is outside everything this one touches (all of it is#[cfg(test)]), and it is your finding with the reasoning behind it - better as its own change with its own reviewer than folded in here. Your reading matches whatwaitdoes in this PR, for what it is worth: it does not consultGetCompletedValue()at all, it waits on an event that a removed device never signals and checksGetDeviceRemovedReasonon each timeout, exactly because a removed fence reportsUINT64_MAXand would let a value comparison pass.@Damokles2 on GitHub · 2026-11-25 12:53 UTC · original
Thanks for confirming the finding and clarifying the scope! I'll prepare a separate PR for the
fuses_fmad()device-loss handling, keeping the change focused on the probe and its error propagation.@pes7 on GitHub · 2026-11-25 22:22 UTC · original
Rebased onto today's
main(f3578e3). Three hunks needed a hand, all from #181's WARP work that landed meanwhile:Hostnow had awarpfield, so the reused objects sit beside it (reused: Mutex<Option<Reused>>), and every constructor (new's WARP branch, its hardware branch,from_device) initialises them;create_device/static WARP, so this branch's own device setup is gone (main's is kept) and only the reused objects remain of it;mod testsblock from #181 is kept as it is.All three commits are there (
cceb602,5b1b9cf,7d6cff9), andcargo test --release -p victory-shaderpasses: 302 tests, 3 ignored, in 115 s (the same suite took 199 s before the rebase, so the win this PR describes still holds on today'smain).@catminou4 on GitHub · 2026-11-24 21:41 UTC · original · approved these changes
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Windows-gated execution/hardware tests only run on Windows.Emit test harness: wait() on the fence now uses SetEventOnCompletion + WaitForSingleObject(100ms) with a GetDeviceRemovedReason check per timeout instead of spinning — a removed device is reported rather than hung, and the event handle is closed on every path except the wedged one (deliberately leaked, one handle, run is ending anyway). Windows-only test code; macOS compile unaffected.
@fuzeofficlal on GitHub · 2026-11-26 17:23 UTC · original · approved these changes
Verified head
7d6cff93e0804f983f389686744fc304213853fdmerged ontomain(63bcca43) on Windows 11 (Ryzen 9 9950X, RTX 5090 + WARP, Rust 1.98.1 MSVC, and D3D12 Debug Layer).main(and clean co-merge with#1493inemit/host.rs).cargo xtask policy(191 files ok),cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features), andemit::rect::/v_med3_/v_fract_tests (6/6, covering bothHost::drawandHost::dispatch) all pass.v_cvt_norm_i16_and_u16_f16_round_the_exact_product(576 dispatches, debug build, hardware + WARP):33.05 sonmain(33,181 mswall) vs1.41 swith this PR (1,773 mswall).ID3D12InfoQueue) on both RTX 5090 and WARP:Hostproduced zero new warnings/errors (onlymain's pre-existingID=1328onView::RawUav).CreateCommittedResourceerror afterlist.Reset/ beforelist.Close, leavingr.recording == true, then running a valid dispatch on the sameHost) cleanly dropped and recreatedReused, returned the expected output, and produced zero Debug Layer warnings/errors on both adapters.GetProcessHandleCount, without Debug Layer):+0handles across 60 reused dispatches on both RTX 5090 (358 -> 358) and WARP (304 -> 304), and+0across 20 fullHost::new->dispatch->dropcycles on WARP (299 -> 299), confirmingDrop for Reusedcloses the event handle cleanly.Minor non-blocking nit: in
Host::draw(emit/host.rs:1355-1358), ifqueue.Signal(&fence, 1)fails,eventis returned pastCloseHandle(event); creatingeventafterqueue.Signalavoids leaking the handle on that error path.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.