Skip to content

borealis: speed up M5 acceleration-structure builds and refits - #1482

Merged
DjDeveloperr merged 7 commits into
aurora-wipfrom
djdev/m5-asb-fast-refit-batch
Dec 1, 2026
Merged

DjDeveloperr merged 7 commits into
aurora-wipfrom
djdev/m5-asb-fast-refit-batch

Conversation

@DjDeveloperr

Copy link
Copy Markdown

What: Speed up the M5 native acceleration-structure builder with H-PLOC, phased Morton sorting, cached resource IDs, and batched planning and encoding. Refit eligible triangle BLASes directly and align replacement BLAS addresses when relinking TLAS instances.

Tested: Apple M5 Pro J714s (G17S B1) at ced9f46842c001972717c6eb14501dbefa1f63b7. Four-TLAS build median fell from 1.1920 to 0.3320 ms (−72.15%) and nine-BLAS in-place refit from 0.0775 to 0.0640 ms (−17.42%). The out-of-place refit ranges overlap. The ray-query AS-builder CTS list passed 35/35, and 7,864,320 refit-versus-rebuild rays matched.

Risk: Two-way door for Macs using the M5 native acceleration-structure path. Direct refit reuses retained native planning metadata. Other updates use the batched refit path.

Not yet: Hardware evidence covers G17S B1. The original mixed CTS list includes 12 pipeline cases marked NotSupported because the standalone driver leaves VK_KHR_ray_tracing_pipeline disabled on G17S.

Details
  • Use H-PLOC for builds that selected PLOC, with a single-workgroup shuffle path up to 4096 leaves. Dispatch Morton sort passes separately so their progress does not depend on other workgroups in the same dispatch.
  • Cache the native resource-table ID on each Vulkan acceleration structure. Batch independent planning phases before their barriers, fuse planning for small builds, then write disjoint node, envelope and retained-plan ranges.
  • Refit eligible in-place triangle BLASes from application vertex buffers using the retained native plan. Load task fields at their use sites to shorten live ranges. Preserve retained data through copy, compaction and serialization.
  • Align a replacement BLAS address to its native address before writing the TLAS instance and looking up its resource ID.

The H-PLOC and Morton-sort work overlaps #1440 and #1453. The TLAS relink fix overlaps #1422.

Evidence

Testing

Native AArch64 Arch Linux ARM on one J714s Apple M5 Pro, G17S B1, kernel 7.3.0-m5-pr67 (#171 SMP PREEMPT), GPU firmware 27.0 (26A428), SLC enabled on 16 planes. GPU fault lines were zero before and after. The machine had one pre-existing failed service and a kernel built without SELinux. The failed-service count was unchanged.

Arm Mesa revision libvulkan_asahi.so SHA-256
Baseline, aurora-wip at test time and the series merge base c5bf0a743d3c0c0b31cbfd53c5d3ca10e5c0a15b 1aa324f78350c203af44f2e0190d58bf726f3f11dae75c428a400f6ca9cffafd
Proposed ced9f46842c001972717c6eb14501dbefa1f63b7 bf8c5a65e5018603a84920743f4a684fa4dc5a699797b3d29885af3b389221d8

At publication, aurora-wip had advanced to 456457aa93d16441ecfabdaf7179c18200ee32fd with a DRM primary-node reporting fix outside the series' changed files. The tested head merges cleanly with that revision. The table uses the tested merge base.

Both Mesa 26.3.0-devel ICDs were built with identical native release options using GCC 16.1.1, Meson 1.12.1, Ninja 1.13.2 and glslang 16.4.0. The build command was ninja -j 12 -C "$BUILD_DIR" src/asahi/vulkan/libvulkan_asahi.so. The runner pinned each ICD with VK_DRIVER_FILES, used LC_ALL=C and a shared shader cache directory, and left trace and compiler debug flags unset for timings.

Benchmark

rqbatch2 and its TLAS-update variant use the pinned C sources, scene data and SPIR-V in the benchmark kit and raw outputs. The recorded rqbatch2 binary SHA-256 is 6fdf03d807a6b924a4d785d5177cf2c438491ff1625fdcf66c45390eb6bf8585. The refit batch used nine BLASes with 13,874 triangles total. Each TLAS batch had four structures with 512 instances each. The TLAS-update variant builds updateable structures before timing their in-place updates.

The following are the timing commands for each pinned ICD, with the inputs staged in the working directory.

timeout -k 2 20 ./rqbatch2 --spv ./rq.spv --k 9 --dim 64 --arena 1 --mis 512 --time 32
timeout -k 2 20 ./rqbatch2 --spv ./rq.spv --k 9 --dim 64 --arena 1 --mis 512 --into 1 --time 32
timeout -k 2 20 ./rqbatch2 --spv ./rq.spv --k 9 --dim 64 --arena 1 --mis 512 --time-build 32 --tlas-n 512 --tlas-calls 4
timeout -k 2 20 ./rqbatch2-tlas --spv ./rq.spv --k 9 --dim 64 --arena 1 --mis 512 --tlas-update 1 --time-build 32 --tlas-n 512 --tlas-calls 4

Four independent processes per arm in ABBA ABBA order. Each process averaged 32 GPU timestamp measurements after two warmups. Cells show min / median / max across process means in ms per batch. Changes use the medians.

Workload Baseline (ms) Proposed (ms) Change
Build 9 triangle BLASes 0.780 / 0.7805 / 0.781 0.674 / 0.6755 / 0.689 −13.45%
Refit 9 triangle BLASes in place 0.075 / 0.0775 / 0.123 0.062 / 0.0640 / 0.073 −17.42%
Refit 9 triangle BLASes out of place 0.116 / 0.1185 / 0.120 0.113 / 0.1130 / 0.124 −4.64%
Build 4 TLASes 1.191 / 1.1920 / 1.193 0.332 / 0.3320 / 0.333 −72.15%
Update 4 TLASes 0.043 / 0.0470 / 0.050 0.040 / 0.0400 / 0.042 −14.89%

The out-of-place refit ranges overlap and the slowest proposed process was slower than the slowest baseline process. One in-place baseline process averaged 0.123 ms and contained a 0.332 ms iteration. All samples are included.

Path verification and correctness

  • Separate live traces with the shader cache disabled showed six baseline compute dispatches and two proposed dispatches per in-place refit. The live direct_refit shader (c733e7e4) used 207 GPRs, 512 resident threads, zero spills/fills and 0 B scratch.
  • In a 256-instance diagnostic build, live shader dumps matched baseline PLOC and proposed H-PLOC, H-PLOC shuffle and fused planner SPIR-V by BLAKE3. The 512-instance build diagnostic recorded 69 → 14 flushes for the BLAS batch and 29 → 10 for the TLAS batch.
  • Five triangle refit-versus-rebuild probes matched all 7,864,320 rays with zero hit/miss, distance or primitive/geometry mismatches. The AABB update and point/collinear triangle update probes also matched their rebuilds.
  • Vulkan CTS 1.4.7.1 (0d18b347943e5e843529a05c95220e27fe30c538) recorded 23 Pass, 12 NotSupported, 0 Fail on the original 35-case list. The 12 cases require VK_KHR_ray_tracing_pipeline. The separate ray-query-only list retained the 23 applicable cases and added 12 AABB cases for 35 Pass, 0 NotSupported, 0 Fail. These CTS rows were run on the proposed ICD.
  • The mesa:asahi suite passed all 286 runnable tests, with one skip and zero failures, in both default and serialized modes. All eight M5 SPIR-V variants passed spirv-val --target-env vulkan1.2 --scalar-block-layout.

Benchmark source, runner, build settings, raw outputs from both arms, path traces and CTS verdicts

Build M5 native acceleration structures with H-PLOC and use its
single-workgroup shuffle path for up to 4096 leaves. Dispatch each pass
of the Morton sort separately to avoid relying on forward progress
between workgroups.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Store the native table index on the Vulkan object at registration. Use
it directly when building, updating, copying or destroying the object
to avoid scanning the resource table for every lookup.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Run the same planning phase for each independent build before issuing a
compute barrier. Add a single-workgroup planner for small builds, with
workgroup barriers between phases. Leave indirect command visibility
to the barrier after the final planning phase.

Derive the instance table offsets from the completed plan so node
encoding, envelope emission and retained-plan saves can write their
disjoint ranges without intervening barriers.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Read updated vertices from the application buffers and write native
leaf clusters without generating a temporary triangle IR. Reuse the
retained native plan to propagate bounds using acquire-release arrival
counters, and reload task and job fields at their use sites to shorten
live ranges. Keep the batched refit path for other updates.

Write the retained IR and plan span after the native image, including
empty images, and preserve it through copies, compaction and
serialization. Share envelope scaling with the ordinary build path
so finite degenerate bounds remain representable.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Convert the replacement Vulkan address to the aligned native BLAS
address before writing the instance record and looking up its resource
ID. The resource table keys entries by native address.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 1, 2026 12:02
@github-actions github-actions Bot added area:borealis Area from CODEOWNERS (bot) area:compiler Area from CODEOWNERS (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Merge readiness at eff6ed7a: not ready yet

Check Detail
CI at head running running: request-pr-gcp, policy, pr-text / pr-text, policy, selftest, build-test
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok the PR body at ced9f468; the changed files are byte-identical at eff6ed7a
Conflicts ok none
PR text ok Risk: two-way · for Macs using the M5 native acceleration-structure path. Direct refit reuses retained native planning metadata. Other updates use the batched refit path.

Areas: area:borealis, area:compiler

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Dec 1, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at ced9f46
Checked: Read all six commits against aurora-wip c5bf0a7, focusing on the host side (hk_m5_acceleration_structure.c, hk_agx3_as_layout.h, m5_as_metadata.h, vk_acceleration_structure.h), copy.cl, and the new agx3_m5_legacy_direct_refit.comp / refit / envelope shaders.
Checked: Direct refit eligibility (hk_m5_direct_triangle_update): only when src == dst, BOTTOM_LEVEL, TRIANGLES, leaf_node_count > 0, updateable, no quads and not indirect. Everything else drops to the batched fallback, and order/bottom are recomputed for the remaining updates. hk_encode_dispatch emits a compute barrier after the PREPARE dispatch (which zeroes the per-internal-node arrival counters) and before hk_update_batched. Leaf tri_id maps back through a first_id binary search over the non-empty geometries, using the same prefix and geometry index j as the build-time vk_fill_geometry_data. Transforms and NaN-to-origin match leaf.h for ALWAYS_ACTIVE. The climb spin-wait waits only for a counter that its own 8-lane group has already completed (the prev+1 == child_count winner), so it never depends on another workgroup making progress.
Checked: Scratch sizing: update scratch shrinks to scratch.size + lbvh + (node_bound - leaves)*4. Both refit shaders now index arrivals by parent - leaf_bound, and internal_bound = node_bound - leaf_bound, so the new size covers them. Retained span and metadata: as_bytes adds the 16-byte trailer for M5, the envelope publishes the trailer for every build including empty ones, and the direct path writes it in write_envelope. Copy, compact, serialize and deserialize copy image+trailer from the native VA and the retained span at base+retained_offset. retained_offset is at least the worst-case as_bytes, which already includes the 1023-byte alignment slack, so a compacted destination with a different 1024 phase cannot overlap the image and the retained span. Copy thread counts cover as->size - padding, which is >= retained_bytes.
Checked: TLAS relink: the resource table key is ALIGN_POT(va, 1024) and hk_as_native_va in the M5 file aligns the same way, so aligning the replacement address before the lookup is correct. Aligning 0 stays 0.
Checked: Built the head in the B1 x86 container with tests (glslang compiled the new direct-refit and fused-plan shaders, libagx copy.cl compiled): BUILD-OK, meson test 266/266 OK.
Checked: No AI attribution in commits or PR body. No Apple-derived tables or values in the diff.
Follow-ups:

  • Size: +1804/-331 over 14 files. Next time, please split along the commit lines (H-PLOC/Morton, resource-ID cache, batched planning, direct refit, TLAS relink fix). Not a hold.
  • Overlap with #1488: both branch from c5bf0a7. f47dd44 here and 79e8773 there are the same H-PLOC/phased-Morton change, and git merge-tree pr1482 pr1488 conflicts in bvh/meson.build and hk_m5_acceleration_structure.c (this PR replaces the single-build encode path that #1488 edits). Merge this one first; #1488 then rebases, drops its duplicate commit and moves pairs into the batched stages.
  • The body says the H-PLOC/Morton work overlaps #1440/#1453 and the relink fix overlaps #1422. Close or rebase those once this lands.
  • The selftest check is red. It is the repo-wide agent-policy-test.sh failure ("push to the default branch heals it"), the same on all four PRs, and unrelated to this diff.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Dec 1, 2026
@DjDeveloperr
DjDeveloperr merged commit 179d997 into aurora-wip Dec 1, 2026
14 checks passed
@DjDeveloperr
DjDeveloperr deleted the djdev/m5-asb-fast-refit-batch branch December 1, 2026 22:48
@DjDeveloperr

Copy link
Copy Markdown
Author

A same-boot, interleaved G17C A/B on stock Linux isolates a standalone BLAS refit regression to the #1482 merge (7c053c71609f → 179d997578f9). The runner alternated isolated ICDs for three rounds, five 250 ms samples per visit, with zero GPU faults.

Standalone fast-trace refit Before After GPU time Time per submit
65K 509.5 Mtri/s 339.7 Mtri/s 127.6 → 191.4 µs 259.3 → 297.5 µs
1M 686.2 Mtri/s 528.8 Mtri/s 1525.2 → 1979.0 µs 1856.2 → 2159.6 µs

CPU recording improved (about 14 → 2 µs and 168 → 2–6 µs). The first follow-up is to compare the current direct triangle refit with the batched fallback on the same build and workload. Keep the original nine-BLAS row and ray-query correctness in that comparison so a fix preserves the gains this PR shipped.

@DjDeveloperr

Copy link
Copy Markdown
Author

Disabling only the direct in-place triangle path on the merged tree (179d997578f9) recovered the standalone refit regression on G17C. The runner interleaved the two Borealis ICDs for three rounds on the same stock Linux boot. All 12 runs verified and reported zero GPU or IOMMU faults.

Triangles Direct Mtri/s Batched Mtri/s Direct GPU time Batched GPU time
65,024 347.6 577.8 187.0 µs 112.5 µs
1,046,528 528.5 859.3 1,980.2 µs 1,217.8 µs

The batched path also beats the pre-merge throughput by 13.4% and 25.2%. CPU recording took 13.42 µs and 151.5 µs with batching, versus 1.71 µs and 2.17 µs with direct refit. Compare both paths on the current tip with single and batched updates, including CPU recording and total submit time, before choosing the default. The driver’s acceleration-structure layout and copy/serialization changes stay in the comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:borealis Area from CODEOWNERS (bot) area:compiler Area from CODEOWNERS (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant