From bc013b154f13d62e11b8b71ead0008c41acab470 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 10 Nov 2026 19:01:10 +0000 Subject: [PATCH 1/2] Windows heap: free and realloc do not capture the block, so rc and own 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 --- tslang/lib/TypeScript/ProcessHeapPass.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/ProcessHeapPass.cpp b/tslang/lib/TypeScript/ProcessHeapPass.cpp index 4afe4f5b9..b9b4538f9 100644 --- a/tslang/lib/TypeScript/ProcessHeapPass.cpp +++ b/tslang/lib/TypeScript/ProcessHeapPass.cpp @@ -154,15 +154,23 @@ class ProcessHeapPass : public mlir::PassWrapper LLVM::ModRefInfo::NoModRef, LLVM::ModRefInfo::NoModRef); funcOp.setMemoryEffectsAttr(memoryEffects); - // and no other pointer reaches the block, as `noalias` says of malloc's result + // and no other pointer reaches the block, as `noalias` says of malloc's and realloc's result. + // Without it on realloc, a store into a grown array's elements might have written the + // array's header, so a loop over the elements loaded their pointer again on every + // iteration and was not vectorized. if (!takesBlock && funcOp.getNumResults() > 0) { funcOp.setResultAttr(0, LLVM::LLVMDialect::getNoAliasAttrName(), mlir::UnitAttr::get(context)); } + // A release or a resize does not keep the block it is given, as LLVM declares free's and + // realloc's (`captures(none)`). Without it, an object whose release is on any path of a + // function counts as escaped there, so under rc and own a header that a function may + // release was assumed reachable through its own elements, with the same effect on loops. if (takesBlock && funcOp.getNumArguments() > 0) { funcOp.setArgAttr(0, LLVM::LLVMDialect::getAllocatedPointerAttrName(), mlir::UnitAttr::get(context)); + funcOp.setArgAttr(0, LLVM::LLVMDialect::getNoCaptureAttrName(), mlir::UnitAttr::get(context)); } auto entry = [&](llvm::StringRef key, llvm::StringRef value) { From b170defafae8330eb67c3cd5e2c466b4b25098c5 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 10 Nov 2026 19:01:59 +0000 Subject: [PATCH 2/2] Benchmarks: Windows results of 2026-11-10, before and after the heap's release does not capture Co-Authored-By: Claude Opus 5.5 --- tslang/benchmarks/numeric/README.md | 37 +++++++++++++++++++++++++++- tslang/benchmarks/raytrace/README.md | 27 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/tslang/benchmarks/numeric/README.md b/tslang/benchmarks/numeric/README.md index b0357ef9e..e52f6f6a0 100644 --- a/tslang/benchmarks/numeric/README.md +++ b/tslang/benchmarks/numeric/README.md @@ -34,7 +34,42 @@ TSLANG= bash tslang/benchmarks/numeric/bench.sh With a kernel's name as its argument a program runs only that kernel, which is how `bench.sh` times them one by one. A JIT figure includes the compiler's start-up, about 13 ms here. -## Results 2026-11-10 +## Results 2026-11-10: Windows + +Release, Windows 11 x64; main d64978ed, and the same tree with the heap's release and resize +declared not to capture their block (below). `bench.sh` runs under Git Bash; the lib paths +(`TSLANG_LIB_PATH`, `GC_LIB_PATH`, `LLVM_LIB_PATH`) point at the build tree. Median of 15 runs +after a warm-up, ms; every build prints the same check values. The "after" rows are one run, in +which Rust too was about 10% slower than in the "before" run: compare a row with the Rust row of +its own run. + +| Build | saxpy | triad | stencil | dot | matmul | hash | +|---|---|---|---|---|---|---| +| tslang gc | 87.3 | 112.3 | 97.3 | 96.7 | 57.3 | 66.8 | +| tslang rc | 130.9 | 150.4 | 146.7 | 94.5 | 84.9 | 157.7 | +| tslang none | 88.1 | 112.7 | 92.2 | 94.2 | 60.9 | 67.0 | +| tslang own | 88.7 | 114.2 | 93.3 | 94.7 | 59.4 | 160.0 | +| Rust | 85.9 | 98.4 | 97.1 | 95.0 | 55.5 | 66.8 | +| C++ (clang) | 133.4 | 145.9 | 143.5 | 93.3 | 75.2 | 157.8 | +| tslang rc, release does not capture | 92.3 | 117.8 | 102.2 | 103.6 | 68.5 | 75.1 | +| tslang own, release does not capture | 99.9 | 121.4 | 108.2 | 103.2 | 68.7 | 75.0 | +| Rust, the same run | 93.8 | 106.6 | 107.1 | 109.4 | 64.0 | 76.1 | + +- On Windows, under every memory model but gc, `malloc`, `realloc` and `free` become the process + heap's `__tslang_heap_*` helpers (ProcessHeapPass). LLVM declares the C `free` and `realloc` not + to capture the block they are given; the helpers did not say so. A header that a function may + release on any path then counted as escaped, so a store to the elements might have written it, + and the loop loaded the elements' pointer again on every iteration. The rc executable had 8 + packed floating-point and integer instructions, against 234-282 for gc, none, own and Rust, and + own's `hash` loop was not vectorized either. Linux does not run the pass. Declared `captures(none)`, rc and + own are level with Rust on every kernel. +- `noalias` on the resize's result, which LLVM also says of `realloc`, vectorizes a few more loops + under none, but crashes five array tests (0xC0000409); it is left out. +- The C++ port is about twice as slow here as on Linux (82 packed instructions): clang with the + MSVC standard library does not vectorize most of its loops. That is the port's build, not a + baseline to compare with. + +## Results 2026-11-10: Linux Release, Ubuntu 24.04 x64, a 4-core cloud VM (Xeon 2.1 GHz, AVX2); main d78c735 with the collector's allocations declared `noalias` (below). Median of 9 runs after a warm-up, ms; every build prints diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 60cdd5666..8b5ba56eb 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -26,6 +26,33 @@ pwsh tslang/benchmarks/raytrace/bench.ps1 - Rust: `opt-level=3`, `lto="fat"`, `codegen-units=1`, `panic="abort"` - C++: `clang++ -std=c++20 -O3`, and `cl /std:c++20 /O2 /EHsc /MT` +## Results 2026-11-10: Windows + +Release, Windows 11 x64, the machine of the 10-31 run; main d64978ed, and the same tree with the +heap's release and resize declared not to capture their block (see `../numeric`). `bench.ps1`, +median of 9 runs after a warm-up; every build prints the same checksum. + +| Build | main | release does not capture | vs Rust idiomatic (57 ms) | +|---|---|---|---| +| tslang gc | 74.4 ms | 74.1 ms | 1.3x | +| tslang gc, `--no-stack-promotion` | 159.3 ms | 155.2 ms | | +| tslang rc | 117.5 ms | 114.2 ms | 2.0x | +| tslang none | 78.2 ms | 73.2 ms | 1.3x | +| tslang own | 77.5 ms | 76.4 ms | 1.3x | +| Rust idiomatic | 57.2 ms | 57.2 ms | 1x | +| Rust Rc-boxed | 528.7 ms | 502.6 ms | 8.8x | +| C++ idiomatic (clang) | 52.7 ms | 51.6 ms | 0.9x | +| C++ idiomatic (MSVC) | 71.6 ms | 71.2 ms | 1.2x | +| C++ shared_ptr (clang) | 858.4 ms | 844.0 ms | 14.8x | +| C++ shared_ptr (MSVC) | 911.3 ms | 910.2 ms | 16x | + +- Against 10-31 (gc 71, rc 110, none 70, own 77) the four models are within a few percent; Rust + and C++ also measured 4-5% slower than then. +- gc now depends on stack promotion: without it gc takes twice as long, where on 10-24 the flag + changed it by 2% or less. Under rc and own the flag changes nothing, under none 10 ms. Not + looked into. +- The heap change is for loops over arrays; here it moves the models by no more than the noise. + ## Results 2026-11-10: Linux Release, Ubuntu 24.04 x64, a 4-core cloud VM (Xeon 2.1 GHz); main d78c735 with the collector's