Skip to content

Loader: add O(1) envelope fast-reject, packed RangeEntry lookup, and synchronized registration in modules - #2673

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-modules-range-table-fast-reject-and-sync
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-modules-range-table-fast-reject-and-sync

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Improves lock-free module address lookup performance, fixes high-address overflow bugs, and eliminates a registration/unregistration race in crates/xcl/src/modules.rs:

  • O(1) envelope fast-reject and packed RangeEntry table (RangeTable): Replaced split RANGE_BASE / RANGE_SIZE / RANGE_COUNT statics with a self-contained RangeTable packing #[repr(C)] struct RangeEntry { base: AtomicUsize, size: AtomicUsize } per slot and maintaining a monotonic [min_addr, max_inclusive] envelope. Out-of-envelope probes (such as PC D3D12/driver callers in 0x7ff... during guarded COM calls or VEH dispatch) reject in O(1) without scanning all registered slots, and in-envelope scans only load size when b != 0 && addr >= b.
  • Synchronized registration/unregistration and one-time saturation warning: register_mapped and unregister_mapped now mutate RANGES strictly while holding MAPPED.write(), preventing concurrent unregister_mapped calls from missing an in-flight register_mapped slot and leaving zombie entries. When MAX_RANGES (512) is reached, RangeTable::insert clamps count and signals a one-time xlog_warn! logged strictly after dropping MAPPED.write().
  • Overflow-safe containment checks: Replaced addr < self.base + self.size in MappedModule::contains with addr.checked_sub(self.base).is_some_and(|off| off < self.size), and replaced addr < b + s in RangeTable::lookup with addr - b < s (after addr >= b) and base.saturating_add(size - 1) for max_inclusive, avoiding debug overflow panics and release wraparound near usize::MAX.
  • Leaves describe_address, MAIN, and existing tests untouched.

Part of #1833

Tested

  • Failing-first debug verification on unmodified origin/main (b49be183, cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 2 passed; 1 failed; 0 ignored — contains_handles_base_plus_size_overflow panicked at modules.rs:34:37 (attempt to add with overflow).
  • Post-fix debug WARP targeted tests (XCL_ADAPTER=warp, XCL_WARP=1, cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.00s, zero compiler warnings in modules.rs).
  • Post-fix release WARP targeted tests (XCL_ADAPTER=warp, XCL_WARP=1, cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.00s).
    • Release micro-benchmark (200,000 out-of-envelope 0x7FF9_1234_5678 lookups against 32 registered modules on an isolated RangeTable): 14.11 ns -> 0.73 ns (19.3x faster, WARP run) / 16.02 ns -> 0.71 ns (22.6x faster, RTX run).
  • Clippy check (cargo clippy --all-targets): 0 new warnings in crates/xcl/src/modules.rs (only 1 pre-existing unusual_byte_groupings lint on untouched test literal 0x1000_0000_4).
  • Post-fix release RTX 4060 targeted tests (cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.01s).

Regression run

  • Full release workspace on RTX 4060 (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture): 37 test binaries, 1437 passed; 0 failed; 47 ignored, 29 pre-existing environment-gated SKIPPED lines:

    • 21 skipped: no wave64 (RTX 4060 is wave32)
    • 3 skipped: wave32 per-lane form: refused by driver (f16 arithmetic, f16 packed, f32 conversions)
    • 3 skipped: no D3D12 debug layer (d3d12SDKLayers.dll unavailable on host: debug-layer dred: false/true and uploads_start_from_each_subresources_state)
    • 1 skipped: pso_measurement_compiles_an_exact_synthetic_record (cannot disable driver cache on Windows)
    • 1 skipped: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)
  • Paired 60 s RTX 4060 smoke capture (scripts/smoke.ps1):

Base (origin/main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1552 (PresentX 1552)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13970 MB

PR (8660a4593d51dfbf26abf9a41580d3e896ef1bb2):

xcl-smoke v1
commit: 8660a4593d51dfbf26abf9a41580d3e896ef1bb2
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1538 (PresentX 1538)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13980 MB

Not tested

  • Live retail scenarios loading more than 512 modules (saturation behavior is verified on an isolated RangeTable::new() instance to avoid polluting the process-wide table).
  • The 29 host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows).

AI use

Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, micro-benchmarks, and paired 60 s GPU smoke captures were executed directly in this workspace.

…synchronized registration in modules

Replace split RANGE_BASE/RANGE_SIZE/RANGE_COUNT arrays with a packed
RangeTable maintaining a monotonic [min_addr, max_inclusive] envelope for
O(1) lock-free fast-reject of out-of-range callers (e.g. PC D3D12/driver
addresses), synchronize RangeTable insert/remove under MAPPED.write() so
unregister_mapped cannot leave zombie slots, warn once outside the lock on
MAX_RANGES saturation, and make MappedModule::contains and RangeTable
lookup overflow-safe against high base + size wraparound.

Part of xcl-dev#1833
Assisted-by: Google Antigravity (Gemini 4 Neon)

@luckyteagirl luckyteagirl 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.

Could the concurrency test drive overlapping registration and unregistration of the same base? Its writers own disjoint addresses, and each completes register_mapped before its own unregister_mapped, so it cannot exercise the split publication race this patch fixes. On main, a registration can publish RANGE_BASE, a concurrent same-base unregister can clear it before MAPPED.push, and registration can then leave find_mapped_by_base populated while image_base_for_addr returns None. A controlled same-base interleaving with an assertion that both lookups agree after the threads finish would catch that regression. Source-based review at exact head 8660a4593d51dfbf26abf9a41580d3e896ef1bb2; no runtime race test was run. AI-assisted review with Codex.

@SlopTart

SlopTart commented Nov 26, 2026 •

Copy link
Copy Markdown

Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b overrides, official DXC kit, VKD3D_CONFIG=ignore_shared_fence, threads=2), head 8660a459, mergeable against main b49be183:
non-gpu suite (--skip gpu::): 598 passed, 3 failed, 4 ignored, 733 filtered.
The failure set is identical to bare main b49be183 measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo).
Zero delta from this PR; every test it adds passes inside the suite.
Not tested: gpu suite, Windows native.
Correction: an earlier version of this row reported three extra reds; those runs lacked the d3d12,d3d12core=n,b DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

@sage-sage

Copy link
Copy Markdown

Test report: passes on NVIDIA. Exact head 8660a459 (its base is current main b49be18, so head = merged).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4: 1437 passed, 0 failed, 47 ignored; the 2 new tests pass
  • SKIPPED (29 lines, the same as on main on this machine): 21 wave64 tests (NVIDIA is wave32 only), 3 per-lane wave32 f16/f32 forms (refused), 3 D3D12 debug layer (d3d12SDKLayers.dll not installed), 1 no heap windows, 1 pso_measurement_compiles_an_exact_synthetic_record (Developer Mode off)

Not tested

  • No game, so not module registration on a title.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
… registration in modules (port upstream xcl-dev#2673)

Shared modules.rs (lock-free fault/lookup path): MappedModule::contains
no longer wraps for images near the top of the address space
(checked_sub/is_some_and); the range table gains min_addr/max_inclusive
envelope fast-reject (O(1) miss) and packed RangeEntry { base, size } with
base-then-size store order published by count (Release); register_mapped
inserts under the MAPPED write lock so a concurrent unregister_mapped
cannot leave zombie ranges, remove clears by base. Upstream tests
verbatim (overflow/envelope boundaries, concurrent register/unregister).

Linux-only adaptation: image_range_for_addr (no upstream counterpart,
crash-time lock-free (base, size) lookup) now reads through a
RangeTable::lookup_range that shares the ported lookup loop, so lookup
stays the verbatim base-only path. Failing-first: the overflow test
FAILED on the old wrapping contains, green after.
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Pack the lock-free module ranges into a single table with a monotonic address envelope, making the common PC/driver-address rejection O(1). Publish registration and removal under the mapped-module write lock and make all containment arithmetic overflow-safe.

A controlled same-base test holds registration between range publication and vector publication and proves unregister cannot create a zombie entry.

The upstream release microbenchmark reported a 19-23x improvement for out-of-envelope probes against 32 modules. GNU Windows-target cargo check passed here; Windows/MSVC and runtime tests were not run on this host.

Adapted from PR xcl-dev#2673.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Pack the lock-free module ranges into a single table with a monotonic address envelope, making the common PC/driver-address rejection O(1). Publish registration and removal under the mapped-module write lock and make all containment arithmetic overflow-safe.

A controlled same-base test holds registration between range publication and vector publication and proves unregister cannot create a zombie entry.

The upstream release microbenchmark reported a 19-23x improvement for out-of-envelope probes against 32 modules. GNU Windows-target cargo check passed here; Windows/MSVC and runtime tests were not run on this host.

Adapted from PR xcl-dev#2673.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Pack the lock-free module ranges into a single table with a monotonic address envelope, making the common PC/driver-address rejection O(1). Publish registration and removal under the mapped-module write lock and make all containment arithmetic overflow-safe.

A controlled same-base test holds registration between range publication and vector publication and proves unregister cannot create a zombie entry.

The upstream release microbenchmark reported a 19-23x improvement for out-of-envelope probes against 32 modules. GNU Windows-target cargo check passed here; Windows/MSVC and runtime tests were not run on this host.

Adapted from PR xcl-dev#2673.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Pack the lock-free module ranges into a single table with a monotonic address envelope, making the common PC/driver-address rejection O(1). Publish registration and removal under the mapped-module write lock and make all containment arithmetic overflow-safe.

A controlled same-base test holds registration between range publication and vector publication and proves unregister cannot create a zombie entry.

The upstream release microbenchmark reported a 19-23x improvement for out-of-envelope probes against 32 modules. GNU Windows-target cargo check passed here; Windows/MSVC and runtime tests were not run on this host.

Adapted from PR xcl-dev#2673.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)

Assisted-by: Codex (GPT-5.6-sol)
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.

4 participants