Skip to content

runtime: sha256_hex hashes in place, unrolled, with the x86 SHA extensions (4.6-7.0x, 1 allocation) - #8358

Closed
TG0000 wants to merge 1 commit into
ProjectLeonida:mainfrom
TG0000:perf/runtime-dxgi-xs
Closed

TG0000 wants to merge 1 commit into
ProjectLeonida:mainfrom
TG0000:perf/runtime-dxgi-xs

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claim: #8186 (comment) (no item ID yet; the claim describes the change)

What

leonida::sha256_hex (runtime/src/shader_library.h) runs on the PSO creation path. Every call to pipeline_key() uses it (compute and graphics, d3d12_vulkan_device.cpp:2784/2834), and so do each stage's FSSM lookup (:3264), program_candidates and every cached-PSO blob (:5763). The old version:

  • copied the whole input into a std::vector, then reallocated and copied it again on the 0x80 push, because the vector's capacity equalled its size;
  • padded it one push_back at a time;
  • built its 64-character result one push_back at a time (several string reallocations);
  • compressed with a scalar loop that moves all eight working variables every round.

Now:

  • Whole blocks are compressed where they lie. Only the tail and its padding (one or two blocks) go through a 128-byte stack buffer, so a hash allocates once (its result).
  • Portable compression: eight rounds per step, with the working variables named in rotated order so no round moves them; a 16-word rolling message schedule; and Ch / Maj written as g ^ (e & (f ^ g)) / (a & b) | (c & (a | b)), which are the same functions with fewer operations.
  • x86-64 with GCC or Clang: a SHA256RNDS2 / SHA256MSG1 / SHA256MSG2 path, used when CPUID reports SHA (7.0:EBX[29]), SSSE3 and SSE4.1. The answer is cached in a constant-initialized std::atomic<int>, not a function-local dynamic static, so no thread-safe-statics guard runs on guest threads (as runtime/shims: remove function-local dynamic statics in d3d12_xs, d3d12_xs_device and xgameruntime_offline #8178 does for the shims). -DLEONIDA_SHA256_X86=0 builds the portable path only; other compilers and architectures get it automatically.

No behaviour change: same hex string for every input. Only the SHA-256 block of shader_library.h changes (plus <atomic> and the guarded <cpuid.h> / <immintrin.h> includes). No open PR touches that block: #7924's nearest hunk ends at line 242, and #8074, #8114, #7962 and #8134 are elsewhere in the file.

Measurements

runtime/test/sha256_hex_test.cpp --bench, clang 23.1.6 (llvm-mingw 20261117) -O2, AMD Ryzen 9 5900X, Windows 11. Each figure is the best of 5 interleaved runs, in ns per call. "old" is a verbatim copy of the previous function.

bytes old portable speedup sha256_hex (SHA ext.) speedup allocs old → new
64 712.4 395.7 1.80x 154.0 4.63x 2 → 1
200 1095.9 741.4 1.48x 227.2 4.82x 2 → 1
400 1700.0 1231.9 1.38x 325.5 5.22x 2 → 1
1024 3791.9 3097.1 1.22x 688.8 5.51x 2 → 1
4096 13182.6 10545.9 1.25x 2255.1 5.85x 2 → 1
16384 50032.4 41039.9 1.22x 8528.8 5.87x 2 → 1
65536 237255.7 164430.9 1.44x 33948.1 6.99x 2 → 1

The old allocation count depends on the standard library: 2 with libc++ at -O2, 4 at -O3, more with libstdc++'s 15-character SSO. The new count is 1 everywhere. The portable fallback alone, for a processor without SHA extensions, is 1.22–1.80x.

Tests

New runtime/test/sha256_hex_test.cpp, registered as the sha256_hex CTest. It checks the portable path, the SHA-extensions path (when the CPU has it) and sha256_hex itself against the verbatim old function:

  • the FIPS 180-4 vectors, including the million-'a' one, and a null empty span;
  • every length from 0 to 1100 bytes, so every tail size 0..63 and both padding cases after 0..17 whole blocks, each at an aligned and an odd address;
  • 300 random lengths up to 70 000 bytes;
  • constant fills (0x00 / 0x80 / 0xff) around the 55/56/63/64/119/120/127/128 boundaries;
  • an allocation count of exactly 1 for the new function (replaced operator new), and more than 1 for the old.

Run here:

  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror on the test, with the SHA path and with -DLEONIDA_SHA256_X86=0: pass. Also -O1 -fsanitize=undefined -fsanitize-trap=undefined: pass.
  • cmake -S runtime -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF with clang 23.1.6, full build, then ctest -j8: 68/68 pass (shader_library_corpus and vk_sync_validation skip as usual), including the existing shader_library test with its SHA-256 vectors.
  • python -I scripts/check_no_game_data.py on the changed files and git diff --check: clean.

Not run here: gta6rt itself. This toolchain's headers lack the Windows SDK 10.0.26100 that CMake requires, so d3d12_vulkan_device.cpp and d3d12_xs_device.cpp, which include the header, are left to CI. GCC was not run either (none is installed locally); its path uses the same __attribute__((target("sha,sse4.1"))) / <cpuid.h> that GCC supports. GPU: AMD Radeon RX 6800, driver 32.0.21045.29003. The GPU isn't involved; this is CPU-only.

No game files were used or added. AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

…sions (4.6-7.0x)

Every pipeline key, each stage's program lookup and every cached-PSO blob in
d3d12_vulkan_device.cpp goes through leonida::sha256_hex. It copied the whole input
into a vector (then reallocated it on the padding push), padded it a byte at a
time, grew its 64-character result a character at a time, and compressed with a
plain scalar loop.

- Whole blocks are compressed where they lie. Only the tail and its padding (one
  or two blocks) go through a 128-byte stack buffer, so a hash allocates only its
  result (1 allocation instead of 2 to 7).
- The portable compression runs eight rounds per step with the working variables
  named in rotated order, keeps a 16-word rolling schedule, and uses the
  fewer-operation forms of Ch and Maj: 1.22-1.80x on its own.
- On x86-64 with GCC/Clang, a SHA256RNDS2/MSG1/MSG2 path runs when CPUID reports
  SHA, SSSE3 and SSE4.1. The answer is cached in a constant-initialized atomic,
  not a function-local static. LEONIDA_SHA256_X86=0 builds the portable path only.

The output is unchanged. runtime/test/sha256_hex_test.cpp checks the portable
path, the SHA-extensions path and sha256_hex against a verbatim copy of the old
function: FIPS 180-4 vectors including the million-'a' one, a null empty span,
every length 0..1100 at aligned and odd addresses, 300 random lengths up to
70 000 bytes, and constant fills around both padding cases. It also counts
allocations. `--bench` prints the timings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matheusfermiano-source

Copy link
Copy Markdown

Independent GCC check of 8bfd48feb8de46b2d4031518fe32ec059cbfea34 on an AMD Ryzen 7 5825U (SHA extensions), CachyOS x86-64, GCC 16.2.1 and libstdc++.

  • The normal CMake RelWithDebInfo build passed both focused CTests, sha256_hex and shader_library. sha256_hex_test explicitly exercised the x86 compression. A separate GCC build with -march=x86-64 -fsanitize=address,undefined -fno-sanitize-recover=all passed the hash test as well.
  • I also hashed anonymous zero-filled mappings of 2^29 - 1, 2^29, and 2^29 + 1 bytes. The accelerated build and the build with -DLEONIDA_SHA256_X86=0 each matched Python hashlib.sha256 on all three inputs. This covers the carry into the upper 32 bits of the padding length field.
  • One GCC-specific test build caveat: -O2 -Wall -Wextra -Werror rejects sha256_hex_test.cpp with -Wmismatched-new-delete when the replacement operator delete inlines its std::free. The paired replacement operator new uses std::malloc, and the normal CMake and sanitizer runs passed. In a scratch copy, putting [[gnu::noinline]] on the test's delete overloads let the forced-portable build pass with the original strict flags. The unmodified forced-portable test also ran successfully with just that warning left non-fatal.

CPU-only, synthetic inputs. AI-assisted (Codex).

@senyurah

Copy link
Copy Markdown

Independent Windows check of 8bfd48feb on a Ryzen 7 5700X3D, Windows 11 (26200), MinGW GCC 15.2. The focused RelWithDebInfo CTests sha256_hex and shader_library both passed.

I also compiled a separate probe with -O2 -Wall -Wextra -Werror, with normal x86 dispatch and with LEONIDA_SHA256_X86=0. The probe put one read-only 4 KiB page between two uncommitted pages. For every length from 0 through 4096, it hashed spans starting at the left boundary and ending at the right boundary. All 8,194 digests per run matched Python hashlib in five fresh processes per build.

Each run also released 16 threads together on the first hash call. All digests matched in both builds. In the normal build the dispatch cache started at 0 and finished at 2, confirming the probe used SHA extensions on this CPU.

AI-assisted verification, CPU-only synthetic inputs.

@philip-roy-jones

Copy link
Copy Markdown

On the unmodified 8bfd48fe head, native Windows x64 MSVC 19.51.36248 (Microsoft STL 145, update 202604L) selected portable SHA-256 by default. The preprocessor reported LEONIDA_SHA256_X86=0, and the registered sha256_hex CTest passed 1/1. Both entry points passed the committed digest vectors and every aligned and one-byte-offset input length from 0 through 1,100.

I also ran the same stock executable with --bench to read its allocation counters. For a 64-byte input the reference allocated 6 times and the new function 1. For 200, 400, 1,024, 4,096, 16,384 and 65,536 bytes the counts were 5 and 1 at every size. Both invocations exited 0.

AI-assisted.

@RAINighty

Copy link
Copy Markdown

Tested 8bfd48feb merged with main 867d922f0 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, MSVC 19.44.35228, RelWithDebInfo (Ninja), CPU-only tests: pass, MSVC build only.

  • sha256_hex and shader_library pass (shader_library_corpus is skipped, no corpus here). The test prints sha256_hex: ok (portable compression): with MSVC LEONIDA_SHA256_X86 is 0 by the header's own condition (GCC or Clang only), so this run covers the portable path and the new padding cases, not the SHA-extensions code. The CPU has SHA extensions, but MSVC does not compile that path. The accelerated path is covered by the two GCC reports above.
  • Negative control: this PR's sha256_hex_test.cpp with main's shader_library.h does not build (sha256_hex_with and sha256_blocks_portable are not members of leonida::detail), so there is no pass/fail to compare.
  • Code not reviewed beyond running it. No GPU or game files.

AI-assisted (Claude Code).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 8bfd48fe on Linux, GCC, CPU only (no GPU, no Windows API), merged onto main 867d922f: pass

  • Built only the sha256_hex test target(s) from the merged tree and ran them through CTest: 100% tests passed out of 1.
  • With the PR's runtime/src files reverted to main and rebuilt: build failed, because the new test does not compile without the change.
    • runtime/test/sha256_hex_test.cpp:133:29: error: ‘sha256_hex_with’ is not a member of ‘leonida::detail’
    • runtime/test/sha256_hex_test.cpp:134:26: error: ‘sha256_blocks_portable’ is not a member of ‘leonida::detail’

So the new test depends on the change.

The proof here is compile-level: the new test names entry points that do not exist without the change. A note from the agent's read, which I did not repeat: the test does not compare the new code path's digest against an independent one, so a logic error on a path this host does not take would not fail it. The agent also built the test under ASan/UBSan and reports it clean; I did not repeat the sanitizer build.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

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.

7 participants