runtime: eliminate per-instruction allocations in spirv_f16_halves #1947

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

@⁠IlyassRS3 on GitHub · 2026-11-25 16:28 UTC · original
IlyassRS3:perf/rt-spirv-f16-halves-flat-scan → main · +327 −152 · 2 files · 1 commits · open

Item ID: RT-SPIRV-F16-HALVES-FLAT-SCAN-PERF (claimed in #1608 comment #7167909660).

Summary

Eliminates per-instruction std::vector<uint32_t> heap allocations in leonida::link_f16_halves() (runtime/src/spirv_f16_halves.h), directly speeding up graphics PSO creation under --f16-interp-fallback (MoltenVK / macOS and GPUs without VK_KHR_fragment_shader_barycentric) while preserving bit-exact SPIR-V output:

  • Single-pass flat framing + located-variable scan (validate_and_collect_located_vars):
    • Previously, link_f16_halves parsed every VS and PS instruction into std::vector<Inst> (each Inst owning a separate heap std::vector<uint32_t>), even on the no-op already_linked fast path where neither module is modified.
    • Now validates instruction framing (using overflow-safe wc > words.size() - pos) and collects Output/Input Location variables directly over the flat std::span<const uint32_t> with zero per-instruction allocations.
  • In-place PS relocation with deferred commit:
    • Previously, relocating PS f16_halves re-serialized the entire ps_mod instruction vector; now records {word_index, new_location} patches from the PS preamble and applies them in place only after VS succeeds (zero full-buffer PS copy, and PS stays untouched on any VS failure).
  • Conditional OpAccessChain scan & single-buffer VS rebuild:
    • Only scans VS function bodies for OpAccessChain/OpInBoundsAccessChain when static half variables are actually being demoted, using binary search over the demoted ID list instead of inserting every access chain into a std::set.
    • Streams rebuilt VS words directly into one pre-reserved flat std::vector<uint32_t> (bulk-copying the entire function section in one insert when no epilogue synthesis is required), avoids ptr_pointee[...] silent insertion, and guards against header[3] ID bound overflow and OpEntryPoint 16-bit word-count overflow.
    • Keeps spirv_detail::Module, Inst, and collect_located_vars intact for existing tests and helpers (also hardening Module::parse with wc > words.size() - pos).
  • runtime/test/spirv_f16_halves_test.cpp:
    • Added regression coverage for OpAccessChain demotion failure and ID bound overflow leaving both VS and PS untouched, and for malformed/truncated instructions inside function bodies still failing on the fast path.

A/B Benchmark (-O2, 8,000-instruction VS + 12,000-instruction PS, 200 iters, 2 runs)

All three scenarios produced bit-exact output FNV-64 hashes between base and head:

Scenario Base (main) Head (this PR) Speedup Output hashes match
Fast path (already_linked) 419.3 / 300.9 µs 58.8 / 26.3 µs 7.1× – 11.4× exact
Relocate + demote (no synth) 484.7 / 480.9 µs 66.0 / 37.2 µs 7.3× – 12.9× exact
Full corpus synth + demote + PS relocate 503.6 / 530.6 µs 101.7 / 76.5 µs 5.0× – 6.9× exact

Verification

  • AI-assisted: yes
  • GPUs tested: none (host-only portable C++ tests on macOS arm64; no game files used)
  • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: spirv_f16_halves_test PASS
  • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror: spirv_f16_halves_test PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror: spirv_f16_halves_test × 3 runs PASS
  • Hygiene: check_no_game_data.py PASS, tests/test_text_write_line_endings.py PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1947) `IlyassRS3:perf/rt-spirv-f16-halves-flat-scan` → `main` · +327 −152 · 2 files · 1 commits · open Item ID: `RT-SPIRV-F16-HALVES-FLAT-SCAN-PERF` (claimed in #1608 comment #7167909660). ### Summary Eliminates per-instruction `std::vector<uint32_t>` heap allocations in `leonida::link_f16_halves()` (`runtime/src/spirv_f16_halves.h`), directly speeding up graphics PSO creation under `--f16-interp-fallback` (MoltenVK / macOS and GPUs without `VK_KHR_fragment_shader_barycentric`) while preserving bit-exact SPIR-V output: - **Single-pass flat framing + located-variable scan (`validate_and_collect_located_vars`)**: - Previously, `link_f16_halves` parsed every VS and PS instruction into `std::vector<Inst>` (each `Inst` owning a separate heap `std::vector<uint32_t>`), even on the no-op `already_linked` fast path where neither module is modified. - Now validates instruction framing (using overflow-safe `wc > words.size() - pos`) and collects `Output`/`Input` `Location` variables directly over the flat `std::span<const uint32_t>` with zero per-instruction allocations. - **In-place PS relocation with deferred commit**: - Previously, relocating PS `f16_halves` re-serialized the entire `ps_mod` instruction vector; now records `{word_index, new_location}` patches from the PS preamble and applies them in place only after VS succeeds (zero full-buffer PS copy, and PS stays untouched on any VS failure). - **Conditional `OpAccessChain` scan & single-buffer VS rebuild**: - Only scans VS function bodies for `OpAccessChain`/`OpInBoundsAccessChain` when static half variables are actually being demoted, using binary search over the demoted ID list instead of inserting every access chain into a `std::set`. - Streams rebuilt VS words directly into one pre-reserved flat `std::vector<uint32_t>` (bulk-copying the entire function section in one `insert` when no epilogue synthesis is required), avoids `ptr_pointee[...]` silent insertion, and guards against `header[3]` ID bound overflow and `OpEntryPoint` 16-bit word-count overflow. - Keeps `spirv_detail::Module`, `Inst`, and `collect_located_vars` intact for existing tests and helpers (also hardening `Module::parse` with `wc > words.size() - pos`). - **`runtime/test/spirv_f16_halves_test.cpp`**: - Added regression coverage for `OpAccessChain` demotion failure and ID bound overflow leaving both VS and PS untouched, and for malformed/truncated instructions inside function bodies still failing on the fast path. ### A/B Benchmark (`-O2`, 8,000-instruction VS + 12,000-instruction PS, 200 iters, 2 runs) All three scenarios produced **bit-exact** output FNV-64 hashes between base and head: | Scenario | Base (`main`) | Head (this PR) | Speedup | Output hashes match | |---|---:|---:|---:|:---:| | Fast path (`already_linked`) | 419.3 / 300.9 µs | **58.8 / 26.3 µs** | **7.1× – 11.4×** | exact | | Relocate + demote (no synth) | 484.7 / 480.9 µs | **66.0 / 37.2 µs** | **7.3× – 12.9×** | exact | | Full corpus synth + demote + PS relocate | 503.6 / 530.6 µs | **101.7 / 76.5 µs** | **5.0× – 6.9×** | exact | ### Verification - AI-assisted: yes - GPUs tested: none (host-only portable C++ tests on macOS arm64; no game files used) - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror`: `spirv_f16_halves_test` PASS - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror`: `spirv_f16_halves_test` PASS - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror`: `spirv_f16_halves_test` × 3 runs PASS - Hygiene: `check_no_game_data.py` PASS, `tests/test_text_write_line_endings.py` PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:21 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:40 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 16:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1947#pullrequestreview-6061346093) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request has changes conflicting with the target branch.
  • runtime/test/spirv_f16_halves_test.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1947'
git switch 'github-bot/gh-1947'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1947'
git switch 'github-bot/gh-1947'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1947'
git switch 'main'
git merge --squash 'github-bot/gh-1947'
git switch 'main'
git merge --ff-only 'github-bot/gh-1947'
git switch 'main'
git merge 'github-bot/gh-1947'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.