Repository navigation
runtime: sha256_hex hashes in place, unrolled, with the x86 SHA extensions (4.6-7.0x, 1 allocation) - #8358
runtime: sha256_hex hashes in place, unrolled, with the x86 SHA extensions (4.6-7.0x, 1 allocation)#8358TG0000 wants to merge 1 commit into
Conversation
…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>
|
Independent GCC check of
CPU-only, synthetic inputs. AI-assisted (Codex). |
|
Independent Windows check of I also compiled a separate probe with 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. |
|
On the unmodified I also ran the same stock executable with AI-assisted. |
|
Tested
AI-assisted (Claude Code). |
|
Tested
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. |
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 topipeline_key()uses it (compute and graphics,d3d12_vulkan_device.cpp:2784/2834), and so do each stage's FSSM lookup (:3264),program_candidatesand every cached-PSO blob (:5763). The old version:std::vector, then reallocated and copied it again on the0x80push, because the vector's capacity equalled its size;push_backat a time;push_backat a time (several string reallocations);Now:
g ^ (e & (f ^ g))/(a & b) | (c & (a | b)), which are the same functions with fewer operations.SHA256RNDS2/SHA256MSG1/SHA256MSG2path, used when CPUID reports SHA (7.0:EBX[29]), SSSE3 and SSE4.1. The answer is cached in a constant-initializedstd::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=0builds 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.hchanges (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.sha256_hex(SHA ext.)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 thesha256_hexCTest. It checks the portable path, the SHA-extensions path (when the CPU has it) andsha256_hexitself against the verbatim old function:'a'one, and a null empty span;operator new), and more than 1 for the old.Run here:
clang++ -std=c++20 -O2 -Wall -Wextra -Werroron 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=OFFwith clang 23.1.6, full build, thenctest -j8: 68/68 pass (shader_library_corpusandvk_sync_validationskip as usual), including the existingshader_librarytest with its SHA-256 vectors.python -I scripts/check_no_game_data.pyon the changed files andgit diff --check: clean.Not run here:
gta6rtitself. This toolchain's headers lack the Windows SDK 10.0.26100 that CMake requires, sod3d12_vulkan_device.cppandd3d12_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