Repository navigation
Windows heap allocator attributes and inline rc interface counts: rc/own loops vectorized, rc ray tracer 13% faster - #806
Merged
Conversation
…n array loops are vectorized ProcessHeapPass renames malloc/realloc/free to the __tslang_heap_* helpers on Windows. LLVM declares the C functions' block argument captures(none); the helpers lacked it, so under rc and own an array header released on any path of a function counted as escaped, a store to the elements might have written the header, and every loop reloaded the elements' pointer and was not vectorized (numeric benchmark: rc 8 packed instructions against ~270, own's hash 2.4x slower). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s release does not capture Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows,
ProcessHeapPassrenamesmalloc/realloc/freeto the__tslang_heap_*helpers. LLVM declares the C functions' block argumentcaptures(none); the helpers hadallocptrbut not that. Under rc and own, an array header that a function may release on any path counted as escaped, so a store to the array's elements might have written the header: every loop reloaded the elements' pointer on each iteration ("cannot identify array bounds") and was not vectorized. Linux does not run the pass, so it was not affected.Found with
tslang/benchmarks/numericon Windows: the rc executable had 8 packed SIMD instructions against ~270 for gc/none/own/Rust, and own'shashkernel took 160 ms against 67.Change
llvm.nocaptureon argument 0 of__tslang_heap_freeand__tslang_heap_realloc.noaliason realloc's result too, as LLVM says ofrealloc, and itsallockindis"realloc"alone: it was"realloc,uninitialized", which claims the grown block's contents are undefined. With the result fresh, LLVM then read a grown array's old elements as undefined (00array_splice:b[0]after a splice), and that broke five array tests until the flag was dropped. LLVM declaresreallocthe same way.Results (Windows 11 x64, Release, median of 15, ms)
The machine ran ~10% slower during the "this PR" run (Rust moved by the same amount); rc and own are now level with Rust on every kernel. Raytrace (median of 9): rc 117.5 → 114.2, none 78.2 → 73.2, own 77.5 → 76.4, gc 74.4 → 74.1; all checksums match.
Later, quieter run with both changes (ms): gc 87.8/107.9/92.5/91.6/53.8/64.6, rc 86.2/114.4/93.9/94.8/56.7/65.5, none 88.1/112.6/95.1/92.6/53.4/62.7, own 85.6/111.8/94.1/91.2/53.8/62.3, Rust 77.0/93.5/94.7/93.5/52.0/65.4 (saxpy/triad/stencil/dot/matmul/hash). Raytrace: gc 71.0, rc 110.1, none 70.7, own 72.8, Rust 54.3.
rc: an interface's count changed inline
A sampling profile of the rc ray tracer put most of rc's extra time against none in retains and releases of interfaces: an interface value is counted through the type descriptor its vtable names (vtable, tag, descriptor, indirect call), which LLVM can neither inline nor pair.
Sphere.intersectretained itsIntersection'sthingthat way on every hit and spilled the 80-byte result to the stack.Every type whose value references a plainly counted block (class/object instances, strings,
anyboxes,Shared<T>) now shares one retain routine (__tslang_retain_block_slot, same body as before). Under rc, a retain through an interface whose descriptor names that routine increments inline; a release decrements inline unless it is the last reference or the block is shared/immortal, and otherwise calls the descriptor's routine with a slot of its own.--atomic-rckeeps the call for releases. gc, none and own are untouched.Ray tracer, rc: 110-114 ms -> 95.9 ms (gc 73.1, none 73.3, own 75.8, Rust 56.3).
New test
00rc_interface_count_inline.ts(gc/rc/none, exe/jit): copies of interface values in@valuerecords, last references dropped through the interface, object literals, immortal globals. With the inline release taking off two references instead of one it segfaults (checked).The mm differential fuzzer (1220 programs) found one rc crash; it reproduces with the fast path disabled too (an assignment to a global's field inside a call argument, in try/finally: the known arg-assign shape), so it is not from this change.
Testing
ctest -C Release: 100% passed, 6943 tests.🤖 Generated with Claude Code