[PR #1947] runtime: eliminate per-instruction allocations in spirv_f16_halves #1954

Open
opened 2026-11-26 21:19:31 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1947
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-spirv-f16-halves-flat-scan


📝 Commits (1)

  • 2bf5c6c runtime: eliminate per-instruction allocations in spirv_f16_halves

📊 Changes

2 files changed (+327 additions, -152 deletions)

View changed files

📝 runtime/src/spirv_f16_halves.h (+265 -152)
📝 runtime/test/spirv_f16_halves_test.cpp (+62 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1947 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-spirv-f16-halves-flat-scan` --- ### 📝 Commits (1) - [`2bf5c6c`](https://github.com/ubiquitarianism/leonida/commit/2bf5c6ceba73748c2a07adb612708a45cfee703d) runtime: eliminate per-instruction allocations in spirv_f16_halves ### 📊 Changes **2 files changed** (+327 additions, -152 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/spirv_f16_halves.h` (+265 -152) 📝 `runtime/test/spirv_f16_halves_test.cpp` (+62 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1954
No description provided.