runtime: eliminate per-instruction allocations in spirv_f16_halves #1947

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-spirv-f16-halves-flat-scan into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 16:28:17 +00:00

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)
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)
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 16:40:14 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
MoonEyes commented 2026-11-28 02:51:23 +00:00

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/test/spirv_f16_halves_test.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/test/spirv_f16_halves_test.cpp`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-11-28 21:56:11 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 2bf5c6ceb
  • Test: not run (merge conflict with main)
  • Red on main: not run
  • Green with the PR: not run
  • Verdict: CONFLICT (runtime/test/spirv_f16_halves_test.cpp conflicts with main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `2bf5c6ceb` - Test: not run (merge conflict with main) - Red on main: not run - Green with the PR: not run - Verdict: CONFLICT (runtime/test/spirv_f16_halves_test.cpp conflicts with main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:39:53 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.