hle/xmem: honour exact 128-512 KiB alignment without rounding up to 2 MiB #4364

Open
github-bot wants to merge 1 commit from github-bot/gh-4363 into main AGit
Owner

@⁠TheOriginalF on GitHub · 2026-11-30 19:21 UTC · original
TheOriginalF:feat/xmem-aligned-alloc-and-zero-memory-x → main · +93 −10 · 1 files · 1 commits · open

What it changes

In crates/victory-hle/src/modules/xmem.rs, virtual_alloc() previously set ALIGN_2M for any XMemAllocDefault alignment larger than 64 KiB, rounding 128 KiB, 256 KiB, and 512 KiB requests up to 2 MiB boundaries and wasting 32-bit virtual address space when ALLOC_32BIT is set.

  • Added requested_alignment(kind, flags, min_alignment) and internal virtual_alloc_aligned(...) so virtual_alloc() passes the exact requested power-of-two alignment (128 / 256 / 512 KiB) to reserve_first_fit() / VirtualAlloc2 without forcing ALIGN_2M (while PAGES_2M and explicit ALIGN_2M still align to 2 MiB).
  • Added unit test alloc_32bit_uses_exact_large_alignment_without_2m_rounding that verifies both requested_alignment() and that 4 simultaneous ALLOC_32BIT allocations at 128 / 256 / 512 KiB stay below 4 GiB, honor the exact alignment, and place at least one block off a 2 MiB boundary (failing on main and passing here).

(Scoped down after review: dropped LastError in favor of #4318, and dropped the speculative XMem* / D3D12ZeroMemoryX stubs.)

How it was tested

  • Test system:
    • CPU: AMD Ryzen 9 5950X (Corsair iCUE H150i RGB Pro XT)
    • GPU: Palit GeForce RTX 4090 GameRock OC (SM 6.8)
    • Motherboard: ASUS ROG Strix X570-E Gaming
    • RAM: Corsair Vengeance RGB Pro 32GB (2x16GB @ 3600MHz)
    • PSU: Corsair RM850x (850W)
    • Storage: Corsair MP600 2TB NVMe Gen4 + Seagate Barracuda 2TB HDD
  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Palit GeForce RTX 4090 GameRock OC (SM 6.8), Series S (--console series-s --execute), no DLSS / no frame generation, default software RT path, cache-pareto
  • Before: main (84bd61e2) rounded 128 / 256 / 512 KiB XMemAllocDefault reservations up to 2 MiB (ALIGN_2M); live baseline run (baseline.log, 503.0 s): 0 errors, peak WS 25,132 MB.
  • After: exact 128 / 256 / 512 KiB alignment honored without 2 MiB rounding, verified by alloc_32bit_uses_exact_large_alignment_without_2m_rounding; combined live run (all-prs.log, 430.5 s): 0 errors, no allocation failures.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A
  • cargo test passed for the crates it touches: yes (cargo test -p victory-hle --lib --locked xmem:: -- --test-threads=1 — 51 passed, 0 failed, 1 ignored; cargo clippy — 0 new warnings; xtask policy --staged — ok)

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #4318 (no longer conflicts now that LastError is removed), #3738, #3787 (different scope in xmem.rs)
**@⁠TheOriginalF** on GitHub · 2026-11-30 19:21 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4363) `TheOriginalF:feat/xmem-aligned-alloc-and-zero-memory-x` → `main` · +93 −10 · 1 files · 1 commits · open ## What it changes In `crates/victory-hle/src/modules/xmem.rs`, `virtual_alloc()` previously set `ALIGN_2M` for any `XMemAllocDefault` alignment larger than `64 KiB`, rounding `128 KiB`, `256 KiB`, and `512 KiB` requests up to `2 MiB` boundaries and wasting 32-bit virtual address space when `ALLOC_32BIT` is set. - Added `requested_alignment(kind, flags, min_alignment)` and internal `virtual_alloc_aligned(...)` so `virtual_alloc()` passes the exact requested power-of-two alignment (`128 / 256 / 512 KiB`) to `reserve_first_fit()` / `VirtualAlloc2` without forcing `ALIGN_2M` (while `PAGES_2M` and explicit `ALIGN_2M` still align to `2 MiB`). - Added unit test `alloc_32bit_uses_exact_large_alignment_without_2m_rounding` that verifies both `requested_alignment()` and that 4 simultaneous `ALLOC_32BIT` allocations at `128 / 256 / 512 KiB` stay below 4 GiB, honor the exact alignment, and place at least one block off a `2 MiB` boundary (failing on `main` and passing here). *(Scoped down after review: dropped `LastError` in favor of #4318, and dropped the speculative `XMem*` / `D3D12ZeroMemoryX` stubs.)* ## How it was tested - Test system: - CPU: AMD Ryzen 9 5950X (Corsair iCUE H150i RGB Pro XT) - GPU: Palit GeForce RTX 4090 GameRock OC (SM 6.8) - Motherboard: ASUS ROG Strix X570-E Gaming - RAM: Corsair Vengeance RGB Pro 32GB (2x16GB @ 3600MHz) - PSU: Corsair RM850x (850W) - Storage: Corsair MP600 2TB NVMe Gen4 + Seagate Barracuda 2TB HDD - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Palit GeForce RTX 4090 GameRock OC (SM 6.8), Series S (`--console series-s --execute`), no DLSS / no frame generation, default software RT path, `cache-pareto` - Before: `main` (`84bd61e2`) rounded `128 / 256 / 512 KiB` `XMemAllocDefault` reservations up to `2 MiB` (`ALIGN_2M`); live baseline run (`baseline.log`, 503.0 s): 0 errors, peak WS 25,132 MB. - After: exact `128 / 256 / 512 KiB` alignment honored without `2 MiB` rounding, verified by `alloc_32bit_uses_exact_large_alignment_without_2m_rounding`; combined live run (`all-prs.log`, 430.5 s): 0 errors, no allocation failures. - For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A - `cargo test` passed for the crates it touches: yes (`cargo test -p victory-hle --lib --locked xmem:: -- --test-threads=1` — 51 passed, 0 failed, 1 ignored; `cargo clippy` — 0 new warnings; `xtask policy --staged` — ok) ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #4318 (no longer conflicts now that `LastError` is removed), #3738, #3787 (different scope in `xmem.rs`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 19:22:42 +00:00
Author
Owner

@⁠babuakhil on GitHub · 2026-11-30 19:50 UTC · original

Checked on Windows 11 (Rust 1.98.1, pinned), applied on current main (e1251c31); cherry-picks cleanly. The d3d12x tests pass (5), as do the xmem tests on one thread (52 passed, 1 ignored), and Clippy's one note in xmem.rs (port_on) is on main too. Not tested in the game.

The "What it changes" bullets are empty. The PR does four separate things (large 32-bit alignment, LastError, three new XMem exports, D3D12ZeroMemoryX), and the first two overlap #4318, so here's how they compare.

LastError: #4318 covers cases this doesn't. It conflicts with #4318 in xmem.rs (cherry-picking one onto the other stops on the same lines). A throwaway test (not part of either PR) made a refused zero-byte commit through XMemVirtualAlloc and read GetLastError() right after:

subscriber main #4318 this PR
none 87 87 87
plain fmt() to stderr, INFO, base given 6 87 6
a sink that sets 88 88 87 87
plain fmt() to stderr, TRACE, null base 6 87 6

Here the error is read at if at.is_null() and restored before the base.is_null() debug line and the rest of the function's logging, so with an ordinary stderr subscriber the title still gets 6 (ERROR_INVALID_HANDLE). #4318 reads each attempt's error when it happens and restores it at the end, and it keeps 87 in all four.

Alignment: main already gives aligned memory. On main, align > ALLOCATION_GRANULARITY sets ALIGN_2M, and a 2 MiB boundary is also a 128, 256 and 512 KiB one. With fields 0x11–0x13 and ALLOC_32BIT, main returned addresses below 4 GiB aligned to each, so alloc_32bit_honours_large_alignment passes on main as well. What this changes is that those blocks stop being rounded up to 2 MiB, which saves 32-bit address space. That's worth having, but it would help to say so, and the test doesn't distinguish the two.

The new exports hand the title made-up values. XMemGetPhysicalMemoryLayout reports 0x10_0000 for both page numbers, XMemGetAllocationStatistics reports zeros, and XMemMakeTag returns 0. Without them a title importing these gets a generated stub, so it would help to say whether a title calls them and why these values are right for it. The same goes for where D3D12ZeroMemoryX's decorated name and range layout come from. Separate PRs for these would also be easier to review than one with four unrelated changes.

**@⁠babuakhil** on GitHub · 2026-11-30 19:50 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4363#issuecomment-7290003967) Checked on Windows 11 (Rust 1.98.1, pinned), applied on current `main` (`e1251c31`); cherry-picks cleanly. The `d3d12x` tests pass (5), as do the `xmem` tests on one thread (52 passed, 1 ignored), and Clippy's one note in `xmem.rs` (`port_on`) is on `main` too. Not tested in the game. The "What it changes" bullets are empty. The PR does four separate things (large 32-bit alignment, `LastError`, three new XMem exports, `D3D12ZeroMemoryX`), and the first two overlap #4318, so here's how they compare. **`LastError`: #4318 covers cases this doesn't.** It conflicts with #4318 in `xmem.rs` (cherry-picking one onto the other stops on the same lines). A throwaway test (not part of either PR) made a refused zero-byte commit through `XMemVirtualAlloc` and read `GetLastError()` right after: | subscriber | `main` | #4318 | this PR | |---|---|---|---| | none | 87 | 87 | 87 | | plain `fmt()` to stderr, INFO, base given | 6 | 87 | **6** | | a sink that sets 88 | 88 | 87 | 87 | | plain `fmt()` to stderr, TRACE, null base | 6 | 87 | **6** | Here the error is read at `if at.is_null()` and restored before the `base.is_null()` debug line and the rest of the function's logging, so with an ordinary stderr subscriber the title still gets 6 (`ERROR_INVALID_HANDLE`). #4318 reads each attempt's error when it happens and restores it at the end, and it keeps 87 in all four. **Alignment: `main` already gives aligned memory.** On `main`, `align > ALLOCATION_GRANULARITY` sets `ALIGN_2M`, and a 2 MiB boundary is also a 128, 256 and 512 KiB one. With fields `0x11`–`0x13` and `ALLOC_32BIT`, `main` returned addresses below 4 GiB aligned to each, so `alloc_32bit_honours_large_alignment` passes on `main` as well. What this changes is that those blocks stop being rounded up to 2 MiB, which saves 32-bit address space. That's worth having, but it would help to say so, and the test doesn't distinguish the two. **The new exports hand the title made-up values.** `XMemGetPhysicalMemoryLayout` reports `0x10_0000` for both page numbers, `XMemGetAllocationStatistics` reports zeros, and `XMemMakeTag` returns 0. Without them a title importing these gets a generated stub, so it would help to say whether a title calls them and why these values are right for it. The same goes for where `D3D12ZeroMemoryX`'s decorated name and range layout come from. Separate PRs for these would also be easier to review than one with four unrelated changes.
github-bot changed title from hle/xmem,d3d12x: honour large 32-bit alignment, preserve LastError, and add XMem/D3D12ZeroMemoryX exports to hle/xmem: honour exact 128-512 KiB alignment without rounding up to 2 MiB 2026-11-30 21:10:46 +00:00
github-bot force-pushed github-bot/gh-4363 from e5446551f2 to d3d2853afa 2026-11-30 21:10:47 +00:00 Compare
Author
Owner

@⁠TheOriginalF on GitHub · 2026-11-30 21:09 UTC · original

Thanks for the thorough review and testing, @⁠babuakhil!

I've updated and narrowed this PR based on every point you raised:

  • Dropped LastError preservation so #4318 can handle it completely (including the stderr tracing cases) with zero merge conflict.
  • Dropped the 3 speculative XMem* stub exports and D3D12ZeroMemoryX since no title currently needs them.
  • Focused this PR strictly on avoiding 2 MiB over-alignment for 128 / 256 / 512 KiB XMemAllocDefault allocations (saving 32-bit address space under ALLOC_32BIT) and updated the description to explain the motivation clearly.
  • Strengthened the unit test (alloc_32bit_uses_exact_large_alignment_without_2m_rounding) so it checks requested_alignment() directly and allocates 4 live blocks simultaneously to assert that at least one lands off a 2 MiB boundary (which fails on main and passes here).
**@⁠TheOriginalF** on GitHub · 2026-11-30 21:09 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4363#issuecomment-7292000121) Thanks for the thorough review and testing, @⁠babuakhil! I've updated and narrowed this PR based on every point you raised: - **Dropped `LastError` preservation** so #4318 can handle it completely (including the `stderr` tracing cases) with zero merge conflict. - **Dropped the 3 speculative `XMem*` stub exports and `D3D12ZeroMemoryX`** since no title currently needs them. - **Focused this PR strictly on avoiding `2 MiB` over-alignment for `128 / 256 / 512 KiB` `XMemAllocDefault` allocations** (saving 32-bit address space under `ALLOC_32BIT`) and updated the description to explain the motivation clearly. - **Strengthened the unit test** (`alloc_32bit_uses_exact_large_alignment_without_2m_rounding`) so it checks `requested_alignment()` directly and allocates 4 live blocks simultaneously to assert that at least one lands off a `2 MiB` boundary (which fails on `main` and passes here).
github-bot force-pushed github-bot/gh-4363 from d3d2853afa to 7bc1d7a087 2026-12-01 01:30:23 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/4364/head:github-bot/gh-4363'
git switch 'github-bot/gh-4363'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-4363'
git switch 'github-bot/gh-4363'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-4363'
git switch 'github-bot/gh-4363'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-4363'
git switch 'main'
git merge --squash 'github-bot/gh-4363'
git switch 'main'
git merge --ff-only 'github-bot/gh-4363'
git switch 'main'
git merge 'github-bot/gh-4363'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4364
No description provided.