From 8ea1332c2069da71839032a16dc5e3d01458d469 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:57:03 +0000 Subject: [PATCH 01/10] Ray tracer benchmark without interfaces: rtbench_static.ts and a Rust port with enums rtbench_static.ts is rtbench.ts with no interfaces and no closures: a thing is one @final class with a shape tag and a switch, a surface is an enum, the rest are @value classes and free functions. rust/src/bin/static_dispatch.rs is the Rust program it mirrors (enum Thing, enum Surface, match). bench.ps1 builds both, and bench.sh is the same script for Linux. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- tslang/benchmarks/raytrace/bench.ps1 | 14 +- tslang/benchmarks/raytrace/bench.sh | 74 +++++ tslang/benchmarks/raytrace/rtbench_static.ts | 292 ++++++++++++++++++ .../raytrace/rust/src/bin/static_dispatch.rs | 246 +++++++++++++++ 4 files changed, 624 insertions(+), 2 deletions(-) create mode 100755 tslang/benchmarks/raytrace/bench.sh create mode 100644 tslang/benchmarks/raytrace/rtbench_static.ts create mode 100644 tslang/benchmarks/raytrace/rust/src/bin/static_dispatch.rs diff --git a/tslang/benchmarks/raytrace/bench.ps1 b/tslang/benchmarks/raytrace/bench.ps1 index 88c4da472..98498da4f 100644 --- a/tslang/benchmarks/raytrace/bench.ps1 +++ b/tslang/benchmarks/raytrace/bench.ps1 @@ -1,6 +1,7 @@ # Builds rtbench.ts under every memory model (with and without stack promotion; -mm=own builds its -# copy rtbench_own.ts) and the Rust and C++ ports, checks that they all print the same checksum, -# then times each one: a warm-up run and $Runs timed runs, reporting min/median/max wall time. +# copy rtbench_own.ts), rtbench_static.ts under every memory model and the Rust and C++ ports, +# checks that they all print the same checksum, then times each one: a warm-up run and $Runs timed +# runs, reporting min/median/max wall time. # # pwsh tslang/benchmarks/raytrace/bench.ps1 [-Tslang ] [-Runs 7] param( @@ -26,9 +27,18 @@ foreach ($mm in "gc", "rc", "none", "own") { } } +foreach ($mm in "gc", "rc", "none", "own") { + $name = "tslang $mm, static" + $exe = Join-Path $out "rtbs_$mm.exe" + & $Tslang --emit=exe --opt --opt_level=3 --no-default-lib "--mm=$mm" "$PSScriptRoot\rtbench_static.ts" -o $exe 2>&1 | Out-Null + if ($LASTEXITCODE -ne 0) { "{0,-22} does not compile (exit {1})" -f $name, $LASTEXITCODE; continue } + $bins += @{ n = $name; p = $exe } +} + Push-Location "$PSScriptRoot\rust" try { cargo build --release --quiet; if ($LASTEXITCODE -ne 0) { throw "cargo build failed" } } finally { Pop-Location } $bins += @{ n = "rust idiomatic"; p = "$PSScriptRoot\rust\target\release\idiomatic.exe" } +$bins += @{ n = "rust static"; p = "$PSScriptRoot\rust\target\release\static_dispatch.exe" } $bins += @{ n = "rust Rc-boxed"; p = "$PSScriptRoot\rust\target\release\boxed.exe" } # C++: clang++ -O3 from PATH, and MSVC /O2 through the newest Visual Studio's vcvars64 diff --git a/tslang/benchmarks/raytrace/bench.sh b/tslang/benchmarks/raytrace/bench.sh new file mode 100755 index 000000000..0182acf26 --- /dev/null +++ b/tslang/benchmarks/raytrace/bench.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# bench.ps1 for Linux and macOS: builds rtbench.ts (rtbench_own.ts for -mm=own) and +# rtbench_static.ts under every memory model and the Rust and C++ ports, checks that they all print +# the same checksum, then times them: a warm-up run each, then RUNS rounds in which every build +# runs once, so that the machine's drift falls on every build alike. Reports min/median/max wall +# time in ms. +# +# TSLANG= [RUNS=15] bash bench.sh +# +# tslang finds its libraries through GC_LIB_PATH and TSLANG_LIB_PATH (or beside itself, as a +# release archive has them). +set -u +here=$(cd "$(dirname "$0")" && pwd) +tslang=${TSLANG:-$here/../../../__build/tslang/ninja/release/bin/tslang} +runs=${RUNS:-15} +out=$here/out +mkdir -p "$out" + +names=() +cmds=() +add() { names+=("$1"); cmds+=("$2"); } + +flags="--emit=exe --opt --opt_level=3 --no-default-lib" +for mm in gc rc none own; do + src=rtbench.ts + if [ $mm = own ]; then src=rtbench_own.ts; fi + if "$tslang" $flags --mm=$mm "$here/$src" -o "$out/rtb_$mm" >"$out/rtb_$mm.log" 2>&1; then + add "tslang $mm" "$out/rtb_$mm" + else + echo "tslang $mm does not compile, see $out/rtb_$mm.log" + fi +done +for mm in gc rc none own; do + if "$tslang" $flags --mm=$mm "$here/rtbench_static.ts" -o "$out/rtbs_$mm" >"$out/rtbs_$mm.log" 2>&1; then + add "tslang $mm, static" "$out/rtbs_$mm" + else + echo "tslang $mm, static does not compile, see $out/rtbs_$mm.log" + fi +done + +if command -v cargo >/dev/null; then + if (cd "$here/rust" && cargo build --release --quiet); then + add "rust idiomatic" "$here/rust/target/release/idiomatic" + add "rust static" "$here/rust/target/release/static_dispatch" + add "rust Rc-boxed" "$here/rust/target/release/boxed" + fi +fi +if command -v clang++ >/dev/null; then + for f in idiomatic shared; do + clang++ -std=c++20 -O3 -o "$out/cpp_$f" "$here/cpp/$f.cpp" && add "c++ $f (clang)" "$out/cpp_$f" + done +fi + +now_us() { echo $(($(date +%s%N) / 1000)); } + +checks=() +for i in "${!names[@]}"; do + # the warm-up run gives the checksum + if ${cmds[$i]} | grep -q -e '38837369' -e 'low digits: 369'; then checks+=(ok); else checks+=(MISMATCH); fi + : >"$out/times_$i" +done +for _ in $(seq "$runs"); do + for i in "${!names[@]}"; do + t0=$(now_us); ${cmds[$i]} >/dev/null; t1=$(now_us) + echo $((t1 - t0)) >>"$out/times_$i" + done +done +for i in "${!names[@]}"; do + sort -n "$out/times_$i" | awk -v name="${names[$i]}" -v check="${checks[$i]}" ' + { t[NR] = $1 / 1000 } + END { printf "%-22s min %8.1f ms median %8.1f ms max %8.1f ms checksum %s\n", + name, t[1], t[int((NR + 1) / 2)], t[NR], check }' + rm -f "$out/times_$i" +done diff --git a/tslang/benchmarks/raytrace/rtbench_static.ts b/tslang/benchmarks/raytrace/rtbench_static.ts new file mode 100644 index 000000000..6e5e7a5d3 --- /dev/null +++ b/tslang/benchmarks/raytrace/rtbench_static.ts @@ -0,0 +1,292 @@ +// @strict-null false +// The ray tracer of rtbench.ts written the way rust/src/bin/static_dispatch.rs is: no interfaces +// and no closures, so nothing is called through a vtable or a function value. A thing is one +// @final class with a `shape` tag and a `switch` (Rust's `enum Thing` and `match`), a surface is +// an enum, Vector, Color, Ray, Light and Hit are @value classes, the ray tracer's methods are free +// functions and the lights are added up in a `for` loop. The same file builds under every memory +// model, and the scene, the arithmetic and the checksum (38837369) are rtbench.ts's. +// print() shows the sum with 6 significant digits, so the low digits are printed separately. +declare function sqrt(v: number): number; +declare function floor(v: number): number; +declare function pow(v: number, s: number): number; + +@value +class Vector { + constructor(public readonly x: number, + public readonly y: number, + public readonly z: number) { + } + static times(k: number, v: Vector) { return new Vector(k * v.x, k * v.y, k * v.z); } + static minus(v1: Vector, v2: Vector) { return new Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } + static plus(v1: Vector, v2: Vector) { return new Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } + static dot(v1: Vector, v2: Vector) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } + static mag(v: Vector) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); } + static norm(v: Vector) { + let mag = Vector.mag(v); + let div = (mag === 0) ? Infinity : 1.0 / mag; + return Vector.times(div, v); + } + static cross(v1: Vector, v2: Vector) { + return new Vector(v1.y * v2.z - v1.z * v2.y, + v1.z * v2.x - v1.x * v2.z, + v1.x * v2.y - v1.y * v2.x); + } +} + +@value +class Color { + constructor(public readonly r: number, + public readonly g: number, + public readonly b: number) { + } + static scale(k: number, v: Color) { return new Color(k * v.r, k * v.g, k * v.b); } + static plus(v1: Color, v2: Color) { return new Color(v1.r + v2.r, v1.g + v2.g, v1.b + v2.b); } + static times(v1: Color, v2: Color) { return new Color(v1.r * v2.r, v1.g * v2.g, v1.b * v2.b); } +} + +const white = new Color(1.0, 1.0, 1.0); +const grey = new Color(0.5, 0.5, 0.5); +const black = new Color(0.0, 0.0, 0.0); +const background = black; +const defaultColor = black; + +@value +class Ray { + constructor(public readonly start: Vector, public readonly dir: Vector) { + } +} + +@value +class Light { + constructor(public readonly pos: Vector, public readonly color: Color) { + } +} + +@final +class Camera { + public forward: Vector; + public right: Vector; + public up: Vector; + + constructor(public pos: Vector, lookAt: Vector) { + let down = new Vector(0.0, -1.0, 0.0); + this.forward = Vector.norm(Vector.minus(lookAt, this.pos)); + this.right = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, down))); + this.up = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, this.right))); + } +} + +enum Surface { + Shiny, + Checkerboard +} + +function isWhiteSquare(pos: Vector) { + return (floor(pos.z) + floor(pos.x)) % 2 !== 0; +} + +function diffuse(surface: Surface, pos: Vector): Color { + switch (surface) { + case Surface.Shiny: return white; + default: return isWhiteSquare(pos) ? white : black; + } +} + +function specular(surface: Surface): Color { + switch (surface) { + case Surface.Shiny: return grey; + default: return white; + } +} + +function reflect(surface: Surface, pos: Vector): number { + switch (surface) { + case Surface.Shiny: return 0.7; + default: return isWhiteSquare(pos) ? 0.1 : 0.7; + } +} + +function roughness(surface: Surface): number { + switch (surface) { + case Surface.Shiny: return 250.0; + default: return 150.0; + } +} + +enum Shape { + Sphere, + Plane +} + +// Rust's `enum Thing { Sphere { center, radius2, .. }, Plane { norm, offset, .. } }`: `v` is the +// sphere's center or the plane's normal, `k` the sphere's squared radius or the plane's offset. +@final +class Thing { + constructor(public readonly shape: Shape, public readonly v: Vector, public readonly k: number, + public readonly surface: Surface) { + } + + static sphere(center: Vector, radius: number, surface: Surface) { + return new Thing(Shape.Sphere, center, radius * radius, surface); + } + + static plane(norm: Vector, offset: number, surface: Surface) { + return new Thing(Shape.Plane, norm, offset, surface); + } + + normal(pos: Vector): Vector { + switch (this.shape) { + case Shape.Sphere: return Vector.norm(Vector.minus(pos, this.v)); + default: return this.v; + } + } + + // The distance along the ray to the thing, or Infinity when the ray misses it. + intersect(ray: Ray): number { + switch (this.shape) { + case Shape.Sphere: { + let eo = Vector.minus(this.v, ray.start); + let v = Vector.dot(eo, ray.dir); + let dist = 0.0; + if (v >= 0) { + let disc = this.k - (Vector.dot(eo, eo) - v * v); + if (disc >= 0) { + dist = v - sqrt(disc); + } + } + return dist === 0 ? Infinity : dist; + } + default: { + let denom = Vector.dot(this.v, ray.dir); + if (denom > 0) { + return Infinity; + } + return (Vector.dot(this.v, ray.start) + this.k) / (-denom); + } + } + } +} + +@final +class Scene { + constructor(public things: Thing[], public lights: Light[], public camera: Camera) { + } +} + +// What a ray hit first: the thing and the distance to it. A ray that hit nothing has +// dist === Infinity (and `thing` is then the scene's first thing, not to be used). +@value +class Hit { + constructor(public readonly thing: Thing, public readonly dist: number) { + } +} + +const maxDepth = 5; + +function intersections(ray: Ray, scene: Scene): Hit { + const things = scene.things; + let closest = Infinity; + let closestThing = things[0]; + for (let i = 0; i < things.length; i++) { + const thing = things[i]; + const dist = thing.intersect(ray); + if (dist < closest) { + closestThing = thing; + closest = dist; + } + } + + return new Hit(closestThing, closest); +} + +function traceRay(ray: Ray, scene: Scene, depth: number): Color { + const hit = intersections(ray, scene); + if (hit.dist === Infinity) { + return background; + } + + return shade(hit, ray, scene, depth); +} + +function shade(hit: Hit, ray: Ray, scene: Scene, depth: number): Color { + const thing = hit.thing; + const d = ray.dir; + const pos = Vector.plus(Vector.times(hit.dist, d), ray.start); + const normal = thing.normal(pos); + const reflectDir = Vector.minus(d, Vector.times(2, Vector.times(Vector.dot(normal, d), normal))); + const naturalColor = Color.plus(background, getNaturalColor(thing, pos, normal, reflectDir, scene)); + const reflectedColor = (depth >= maxDepth) ? grey : getReflectionColor(thing, pos, reflectDir, scene, depth); + return Color.plus(naturalColor, reflectedColor); +} + +function getReflectionColor(thing: Thing, pos: Vector, rd: Vector, scene: Scene, depth: number): Color { + return Color.scale(reflect(thing.surface, pos), traceRay(new Ray(pos, rd), scene, depth + 1)); +} + +function getNaturalColor(thing: Thing, pos: Vector, norm: Vector, rd: Vector, scene: Scene): Color { + const lights = scene.lights; + const surface = thing.surface; + let col = defaultColor; + for (let i = 0; i < lights.length; i++) { + const light = lights[i]; + const ldis = Vector.minus(light.pos, pos); + const livec = Vector.norm(ldis); + // in shadow: something is nearer along the ray to the light than the light itself + if (intersections(new Ray(pos, livec), scene).dist <= Vector.mag(ldis)) { + continue; + } + + const illum = Vector.dot(livec, norm); + const lcolor = (illum > 0) ? Color.scale(illum, light.color) : defaultColor; + const spec = Vector.dot(livec, Vector.norm(rd)); + const scolor = (spec > 0) ? Color.scale(pow(spec, roughness(surface)), light.color) : defaultColor; + col = Color.plus(col, Color.plus(Color.times(diffuse(surface, pos), lcolor), + Color.times(specular(surface), scolor))); + } + + return col; +} + +function getPoint(x: number, y: number, camera: Camera, screenWidth: number, screenHeight: number): Vector { + const recenterX = (x - (screenWidth / 2.0)) / 2.0 / screenWidth; + const recenterY = - (y - (screenHeight / 2.0)) / 2.0 / screenHeight; + return Vector.norm(Vector.plus(camera.forward, + Vector.plus(Vector.times(recenterX, camera.right), Vector.times(recenterY, camera.up)))); +} + +function legalize(d: number) { + return d > 1 ? 1 : d; +} + +function render(scene: Scene, screenWidth: number, screenHeight: number): number { + const camera = scene.camera; + let sum = 0; + for (let y = 0; y < screenHeight; y++) { + for (let x = 0; x < screenWidth; x++) { + const color = traceRay(new Ray(camera.pos, getPoint(x, y, camera, screenWidth, screenHeight)), scene, 0); + sum += floor(legalize(color.r) * 255) + floor(legalize(color.g) * 255) + floor(legalize(color.b) * 255); + } + } + + return sum; +} + +function defaultScene(): Scene { + return new Scene( + [Thing.plane(new Vector(0.0, 1.0, 0.0), 0.0, Surface.Checkerboard), + Thing.sphere(new Vector(0.0, 1.0, -0.25), 1.0, Surface.Shiny), + Thing.sphere(new Vector(-6, 3.0, -5.0), 1.0, Surface.Shiny), + Thing.sphere(new Vector(-1.0, 0.5, 1.5), 0.5, Surface.Shiny)], + [new Light(new Vector(-2.0, 2.5, 0.0), new Color(0.49, 0.07, 0.07)), + new Light(new Vector(1.5, 2.5, 1.5), new Color(0.07, 0.07, 0.49)), + new Light(new Vector(1.5, 2.5, -1.5), new Color(0.07, 0.49, 0.071)), + new Light(new Vector(0.0, 3.5, 0.0), new Color(0.21, 0.21, 0.35))], + new Camera(new Vector(3.0, 2.0, 4.0), new Vector(-1.0, 0.5, 0.0))); +} + +function main() { + print("start..."); + const sum = render(defaultScene(), 512, 512); + print("checksum:", sum, "low digits:", sum - 38837000); + print("done."); +} diff --git a/tslang/benchmarks/raytrace/rust/src/bin/static_dispatch.rs b/tslang/benchmarks/raytrace/rust/src/bin/static_dispatch.rs new file mode 100644 index 000000000..d11f72e41 --- /dev/null +++ b/tslang/benchmarks/raytrace/rust/src/bin/static_dispatch.rs @@ -0,0 +1,246 @@ +// The ray tracer of idiomatic.rs with no trait objects and no function pointers: a thing and a +// surface are enums and every call is a `match`, so all of it is static dispatch. This is the +// program rtbench_static.ts mirrors. + +#[derive(Clone, Copy)] +struct Vector { x: f64, y: f64, z: f64 } + +impl Vector { + fn new(x: f64, y: f64, z: f64) -> Self { Vector { x, y, z } } + fn times(k: f64, v: Vector) -> Vector { Vector::new(k * v.x, k * v.y, k * v.z) } + fn minus(a: Vector, b: Vector) -> Vector { Vector::new(a.x - b.x, a.y - b.y, a.z - b.z) } + fn plus(a: Vector, b: Vector) -> Vector { Vector::new(a.x + b.x, a.y + b.y, a.z + b.z) } + fn dot(a: Vector, b: Vector) -> f64 { a.x * b.x + a.y * b.y + a.z * b.z } + fn mag(v: Vector) -> f64 { (v.x * v.x + v.y * v.y + v.z * v.z).sqrt() } + fn norm(v: Vector) -> Vector { + let mag = Vector::mag(v); + let div = if mag == 0.0 { f64::INFINITY } else { 1.0 / mag }; + Vector::times(div, v) + } + fn cross(a: Vector, b: Vector) -> Vector { + Vector::new(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x) + } +} + +#[derive(Clone, Copy)] +struct Color { r: f64, g: f64, b: f64 } + +const WHITE: Color = Color { r: 1.0, g: 1.0, b: 1.0 }; +const GREY: Color = Color { r: 0.5, g: 0.5, b: 0.5 }; +const BLACK: Color = Color { r: 0.0, g: 0.0, b: 0.0 }; +const BACKGROUND: Color = BLACK; +const DEFAULT_COLOR: Color = BLACK; + +impl Color { + fn scale(k: f64, v: Color) -> Color { Color { r: k * v.r, g: k * v.g, b: k * v.b } } + fn plus(a: Color, b: Color) -> Color { Color { r: a.r + b.r, g: a.g + b.g, b: a.b + b.b } } + fn times(a: Color, b: Color) -> Color { Color { r: a.r * b.r, g: a.g * b.g, b: a.b * b.b } } +} + +#[derive(Clone, Copy)] +struct Ray { start: Vector, dir: Vector } + +struct Light { pos: Vector, color: Color } + +struct Camera { pos: Vector, forward: Vector, right: Vector, up: Vector } + +impl Camera { + fn new(pos: Vector, look_at: Vector) -> Self { + let down = Vector::new(0.0, -1.0, 0.0); + let forward = Vector::norm(Vector::minus(look_at, pos)); + let right = Vector::times(1.5, Vector::norm(Vector::cross(forward, down))); + let up = Vector::times(1.5, Vector::norm(Vector::cross(forward, right))); + Camera { pos, forward, right, up } + } +} + +#[derive(Clone, Copy)] +enum Surface { Shiny, Checkerboard } + +fn is_white_square(pos: Vector) -> bool { (pos.z.floor() + pos.x.floor()) % 2.0 != 0.0 } + +impl Surface { + fn diffuse(self, pos: Vector) -> Color { + match self { + Surface::Shiny => WHITE, + Surface::Checkerboard => if is_white_square(pos) { WHITE } else { BLACK }, + } + } + fn specular(self) -> Color { + match self { + Surface::Shiny => GREY, + Surface::Checkerboard => WHITE, + } + } + fn reflect(self, pos: Vector) -> f64 { + match self { + Surface::Shiny => 0.7, + Surface::Checkerboard => if is_white_square(pos) { 0.1 } else { 0.7 }, + } + } + fn roughness(self) -> f64 { + match self { + Surface::Shiny => 250.0, + Surface::Checkerboard => 150.0, + } + } +} + +enum Thing { + Sphere { center: Vector, radius2: f64, surface: Surface }, + Plane { norm: Vector, offset: f64, surface: Surface }, +} + +impl Thing { + fn surface(&self) -> Surface { + match *self { + Thing::Sphere { surface, .. } | Thing::Plane { surface, .. } => surface, + } + } + + fn normal(&self, pos: Vector) -> Vector { + match *self { + Thing::Sphere { center, .. } => Vector::norm(Vector::minus(pos, center)), + Thing::Plane { norm, .. } => norm, + } + } + + // The distance along the ray to the thing, if the ray hits it. + fn intersect(&self, ray: &Ray) -> Option { + match *self { + Thing::Sphere { center, radius2, .. } => { + let eo = Vector::minus(center, ray.start); + let v = Vector::dot(eo, ray.dir); + let mut dist = 0.0; + if v >= 0.0 { + let disc = radius2 - (Vector::dot(eo, eo) - v * v); + if disc >= 0.0 { dist = v - disc.sqrt(); } + } + if dist == 0.0 { None } else { Some(dist) } + } + Thing::Plane { norm, offset, .. } => { + let denom = Vector::dot(norm, ray.dir); + if denom > 0.0 { None } else { Some((Vector::dot(norm, ray.start) + offset) / (-denom)) } + } + } + } +} + +struct Scene { things: Vec, lights: Vec, camera: Camera } + +// What a ray hit first: the thing and the distance to it. +struct Hit<'a> { thing: &'a Thing, dist: f64 } + +const MAX_DEPTH: i32 = 5; + +fn intersections<'a>(ray: &Ray, scene: &'a Scene) -> Option> { + let mut closest: Option> = None; + for thing in &scene.things { + if let Some(dist) = thing.intersect(ray) { + if closest.as_ref().map_or(true, |hit| dist < hit.dist) { + closest = Some(Hit { thing, dist }); + } + } + } + closest +} + +fn trace_ray(ray: &Ray, scene: &Scene, depth: i32) -> Color { + match intersections(ray, scene) { + None => BACKGROUND, + Some(hit) => shade(&hit, ray, scene, depth), + } +} + +fn shade(hit: &Hit, ray: &Ray, scene: &Scene, depth: i32) -> Color { + let thing = hit.thing; + let d = ray.dir; + let pos = Vector::plus(Vector::times(hit.dist, d), ray.start); + let normal = thing.normal(pos); + let reflect_dir = Vector::minus(d, Vector::times(2.0, Vector::times(Vector::dot(normal, d), normal))); + let natural_color = Color::plus(BACKGROUND, get_natural_color(thing, pos, normal, reflect_dir, scene)); + let reflected_color = if depth >= MAX_DEPTH { + GREY + } else { + get_reflection_color(thing, pos, reflect_dir, scene, depth) + }; + Color::plus(natural_color, reflected_color) +} + +fn get_reflection_color(thing: &Thing, pos: Vector, rd: Vector, scene: &Scene, depth: i32) -> Color { + Color::scale(thing.surface().reflect(pos), trace_ray(&Ray { start: pos, dir: rd }, scene, depth + 1)) +} + +fn get_natural_color(thing: &Thing, pos: Vector, norm: Vector, rd: Vector, scene: &Scene) -> Color { + let surface = thing.surface(); + let mut col = DEFAULT_COLOR; + for light in &scene.lights { + let ldis = Vector::minus(light.pos, pos); + let livec = Vector::norm(ldis); + // in shadow: something is nearer along the ray to the light than the light itself + if let Some(hit) = intersections(&Ray { start: pos, dir: livec }, scene) { + if hit.dist <= Vector::mag(ldis) { continue; } + } + + let illum = Vector::dot(livec, norm); + let lcolor = if illum > 0.0 { Color::scale(illum, light.color) } else { DEFAULT_COLOR }; + let spec = Vector::dot(livec, Vector::norm(rd)); + let scolor = if spec > 0.0 { Color::scale(spec.powf(surface.roughness()), light.color) } else { DEFAULT_COLOR }; + col = Color::plus(col, Color::plus(Color::times(surface.diffuse(pos), lcolor), + Color::times(surface.specular(), scolor))); + } + col +} + +fn get_point(x: f64, y: f64, camera: &Camera, screen_width: f64, screen_height: f64) -> Vector { + let recenter_x = (x - (screen_width / 2.0)) / 2.0 / screen_width; + let recenter_y = -(y - (screen_height / 2.0)) / 2.0 / screen_height; + Vector::norm(Vector::plus(camera.forward, + Vector::plus(Vector::times(recenter_x, camera.right), Vector::times(recenter_y, camera.up)))) +} + +fn legalize(d: f64) -> f64 { if d > 1.0 { 1.0 } else { d } } + +fn render(scene: &Scene, screen_width: f64, screen_height: f64) -> f64 { + let camera = &scene.camera; + let mut sum = 0.0; + let mut y = 0.0; + while y < screen_height { + let mut x = 0.0; + while x < screen_width { + let ray = Ray { start: camera.pos, dir: get_point(x, y, camera, screen_width, screen_height) }; + let color = trace_ray(&ray, scene, 0); + sum += (legalize(color.r) * 255.0).floor() + (legalize(color.g) * 255.0).floor() + + (legalize(color.b) * 255.0).floor(); + x += 1.0; + } + y += 1.0; + } + sum +} + +fn default_scene() -> Scene { + Scene { + things: vec![ + Thing::Plane { norm: Vector::new(0.0, 1.0, 0.0), offset: 0.0, surface: Surface::Checkerboard }, + Thing::Sphere { center: Vector::new(0.0, 1.0, -0.25), radius2: 1.0, surface: Surface::Shiny }, + Thing::Sphere { center: Vector::new(-6.0, 3.0, -5.0), radius2: 1.0, surface: Surface::Shiny }, + Thing::Sphere { center: Vector::new(-1.0, 0.5, 1.5), radius2: 0.25, surface: Surface::Shiny }, + ], + lights: vec![ + Light { pos: Vector::new(-2.0, 2.5, 0.0), color: Color { r: 0.49, g: 0.07, b: 0.07 } }, + Light { pos: Vector::new(1.5, 2.5, 1.5), color: Color { r: 0.07, g: 0.07, b: 0.49 } }, + Light { pos: Vector::new(1.5, 2.5, -1.5), color: Color { r: 0.07, g: 0.49, b: 0.071 } }, + Light { pos: Vector::new(0.0, 3.5, 0.0), color: Color { r: 0.21, g: 0.21, b: 0.35 } }, + ], + camera: Camera::new(Vector::new(3.0, 2.0, 4.0), Vector::new(-1.0, 0.5, 0.0)), + } +} + +fn main() { + println!("start..."); + let size: f64 = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(512.0); + let sum = render(&default_scene(), size, size); + println!("checksum: {}", sum); + println!("done."); +} From 0664d0b7a774011350686bd1c6c1cd95f0b5cdc0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:05:17 +0000 Subject: [PATCH 02/10] rtbench_static.ts: builds under every memory model, const locals; Linux results Hit names the thing by its index, which -mm=own accepts; locals that are never reassigned are const. README: what the two new programs are and their timings. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- tslang/benchmarks/raytrace/README.md | 43 ++++++++++++++++++++ tslang/benchmarks/raytrace/rtbench_static.ts | 39 +++++++++--------- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 3c65c59ca..3fe494efd 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -15,10 +15,20 @@ and `Ray` and `Intersection` `@value` interfaces (see "Results 2026-10-24: `@val tslang produces, so the gap between a language's two ports is the cost of allocation alone. `Rc` counts are plain integers; `shared_ptr` counts are atomic. +`rtbench_static.ts` and `rust/src/bin/static_dispatch.rs` are the same ray tracer with static +dispatch only, the TypeScript written the way the Rust is (see "Results 2026-11-11"): + +- Rust: `enum Thing` and `enum Surface` with a `match` in each method, no `dyn Thing` and no + function pointers; free functions, and a `for` loop over the lights. +- tslang: no interfaces and no closures. A thing is one `@final` class with a `shape` tag and a + `switch`, a surface is an enum, `Vector`, `Color`, `Ray`, `Light` and `Hit` are `@value` classes. + One file builds under every memory model. + Run (needs a Release tslang and cargo; clang++ on PATH and Visual Studio are used when present): ``` pwsh tslang/benchmarks/raytrace/bench.ps1 +TSLANG= bash tslang/benchmarks/raytrace/bench.sh ``` - tslang: `--emit=exe --opt --opt_level=3 --no-default-lib --mm=` (`--no-default-lib` @@ -26,6 +36,39 @@ 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-11: static dispatch (Linux) + +Release, Ubuntu 24.04 x64, a 4-core cloud VM (Xeon 2.3 GHz); main 864dab4, built against the +official LLVM 22.1.8 package; Rust 1.97.0, clang 18.1.3. `bench.sh`: a warm-up, then 15 rounds in +which every build runs once; median wall time. Every build prints the same checksum. The machine is +shared, so a few percent either way is noise. + +| Build | `rtbench.ts` | `rtbench_static.ts` | vs Rust static | +|---|---|---|---| +| tslang gc | 87 ms | 60 ms | 1.2x | +| tslang rc | 123 ms | 62 ms | 1.2x | +| tslang none | 89 ms | 61 ms | 1.2x | +| tslang own | 91 ms | 60 ms | 1.2x | +| Rust static (`static_dispatch.rs`) | | 51 ms | 1x | +| Rust idiomatic (`dyn Thing`) | 116 ms | | 2.3x | +| C++ idiomatic (clang) | 70 ms | | 1.4x | + +- Without interfaces and closures the four memory models run alike: nothing is counted or + allocated per ray, so rc loses its extra time (123 to 62 ms). +- tslang is within 1.2x of the Rust port written the same way, and 1.4x faster than its own + interface version. +- Under `-mm=own` a `@value` class cannot hold a borrowed element of an array (`new Hit(thing, + dist)` is "borrows an element and cannot be stored, returned or captured"), so `Hit` names the + thing by its index in `scene.things`. Under none, holding the reference instead measured 2 ms + faster. +- What is left, from callgrind (804 million instructions against Rust's 598 million) and the + disassembly of `intersections`, which has half of them; none of it measured separately: + `intersections` is not inlined into its callers, where Rust inlines everything into `trace_ray`; + the array is tested for null and its length and data pointer are loaded again on every iteration; + and `sqrt` is libm's, whose call for a negative argument makes the function spill the ray's six + numbers on entry. Making `Thing` a `@value` class (the things inline in the array, as in Rust's + `Vec`) or reading `things.length` once before the loop changed nothing. + ## Results 2026-11-10: Windows Release, Windows 11 x64, the machine of the 10-31 run; main d64978ed, and the same tree with the diff --git a/tslang/benchmarks/raytrace/rtbench_static.ts b/tslang/benchmarks/raytrace/rtbench_static.ts index 6e5e7a5d3..239993bc2 100644 --- a/tslang/benchmarks/raytrace/rtbench_static.ts +++ b/tslang/benchmarks/raytrace/rtbench_static.ts @@ -3,8 +3,9 @@ // and no closures, so nothing is called through a vtable or a function value. A thing is one // @final class with a `shape` tag and a `switch` (Rust's `enum Thing` and `match`), a surface is // an enum, Vector, Color, Ray, Light and Hit are @value classes, the ray tracer's methods are free -// functions and the lights are added up in a `for` loop. The same file builds under every memory -// model, and the scene, the arithmetic and the checksum (38837369) are rtbench.ts's. +// functions and the lights are added up in a `for` loop. A miss is the distance Infinity where the +// Rust port has `None`. The same file builds under every memory model, and the scene, the +// arithmetic and the checksum (38837369) are rtbench.ts's. // print() shows the sum with 6 significant digits, so the low digits are printed separately. declare function sqrt(v: number): number; declare function floor(v: number): number; @@ -22,8 +23,8 @@ class Vector { static dot(v1: Vector, v2: Vector) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } static mag(v: Vector) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); } static norm(v: Vector) { - let mag = Vector.mag(v); - let div = (mag === 0) ? Infinity : 1.0 / mag; + const mag = Vector.mag(v); + const div = (mag === 0) ? Infinity : 1.0 / mag; return Vector.times(div, v); } static cross(v1: Vector, v2: Vector) { @@ -69,7 +70,7 @@ class Camera { public up: Vector; constructor(public pos: Vector, lookAt: Vector) { - let down = new Vector(0.0, -1.0, 0.0); + const down = new Vector(0.0, -1.0, 0.0); this.forward = Vector.norm(Vector.minus(lookAt, this.pos)); this.right = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, down))); this.up = Vector.times(1.5, Vector.norm(Vector.cross(this.forward, this.right))); @@ -145,11 +146,11 @@ class Thing { intersect(ray: Ray): number { switch (this.shape) { case Shape.Sphere: { - let eo = Vector.minus(this.v, ray.start); - let v = Vector.dot(eo, ray.dir); + const eo = Vector.minus(this.v, ray.start); + const v = Vector.dot(eo, ray.dir); let dist = 0.0; if (v >= 0) { - let disc = this.k - (Vector.dot(eo, eo) - v * v); + const disc = this.k - (Vector.dot(eo, eo) - v * v); if (disc >= 0) { dist = v - sqrt(disc); } @@ -157,7 +158,7 @@ class Thing { return dist === 0 ? Infinity : dist; } default: { - let denom = Vector.dot(this.v, ray.dir); + const denom = Vector.dot(this.v, ray.dir); if (denom > 0) { return Infinity; } @@ -173,11 +174,12 @@ class Scene { } } -// What a ray hit first: the thing and the distance to it. A ray that hit nothing has -// dist === Infinity (and `thing` is then the scene's first thing, not to be used). +// What a ray hit first: the thing, as its index in the scene's things, and the distance to it. A +// ray that hit nothing has dist === Infinity. (The Rust port holds a `&Thing`; under -mm=own a +// @value class cannot hold a borrowed element of an array, and an index needs no lifetime.) @value class Hit { - constructor(public readonly thing: Thing, public readonly dist: number) { + constructor(public readonly index: number, public readonly dist: number) { } } @@ -186,17 +188,16 @@ const maxDepth = 5; function intersections(ray: Ray, scene: Scene): Hit { const things = scene.things; let closest = Infinity; - let closestThing = things[0]; + let index = 0; for (let i = 0; i < things.length; i++) { - const thing = things[i]; - const dist = thing.intersect(ray); + const dist = things[i].intersect(ray); if (dist < closest) { - closestThing = thing; + index = i; closest = dist; } } - return new Hit(closestThing, closest); + return new Hit(index, closest); } function traceRay(ray: Ray, scene: Scene, depth: number): Color { @@ -209,7 +210,7 @@ function traceRay(ray: Ray, scene: Scene, depth: number): Color { } function shade(hit: Hit, ray: Ray, scene: Scene, depth: number): Color { - const thing = hit.thing; + const thing = scene.things[hit.index]; const d = ray.dir; const pos = Vector.plus(Vector.times(hit.dist, d), ray.start); const normal = thing.normal(pos); @@ -260,7 +261,7 @@ function legalize(d: number) { function render(scene: Scene, screenWidth: number, screenHeight: number): number { const camera = scene.camera; - let sum = 0; + let sum: number = 0; for (let y = 0; y < screenHeight; y++) { for (let x = 0; x < screenWidth; x++) { const color = traceRay(new Ray(camera.pos, getPoint(x, y, camera, screenWidth, screenHeight)), scene, 0); From fc1a9affbe2d7846689db79ef91eb93f378b89d1 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:20:40 +0000 Subject: [PATCH 03/10] Optimizer: array reads without a branch, math calls as intrinsics, internal functions in an executable, inline threshold 325 - An array's length and data are read through a select between the header and a constant empty header, not a branch on null, so a loop hoists both. - A call of a C math function (sqrt, pow, floor, ...) is the LLVM intrinsic: TypeScript has no errno. - With --opt, --emit=exe makes functions that are not exported internal, so the inliner knows every caller. - The inliner's threshold at --opt_level=3 is 325. Test suite still running; benchmark figures follow. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- .../TypeScript/LowerToLLVM/ArrayLayout.h | 45 +++++-- tslang/tslang/transform.cpp | 118 +++++++++++++++++- 2 files changed, 150 insertions(+), 13 deletions(-) diff --git a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h index a1e6ce4bc..adbcd0337 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h +++ b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h @@ -377,22 +377,43 @@ class ArrayLayout : public LLVMCodeHelperBase ArrayRef{0, index}); } + // The header a null array is read through: { null, 0, 0 }, one constant per module. A read + // picks it with a select and then loads the field with no branch. A branch around the load + // ("null ? 0 : header->length") left the load conditional, so a loop over an array could not + // hoist the array's length and data out: both were tested for null and loaded again on every + // iteration. + mlir::Value emptyHeader() + { + StringRef name = "__ts_empty_array"; + + TypeHelper th(rewriter); + auto loc = op->getLoc(); + auto parentModule = op->getParentOfType(); + if (!parentModule.lookupSymbol(name)) + { + OpBuilder::InsertionGuard insertGuard(rewriter); + rewriter.setInsertionPointToStart(parentModule.getBody()); + auto global = rewriter.create(rewriter.getUnknownLoc(), headerType(), /*isConstant=*/true, + LLVM::Linkage::Internal, name, mlir::Attribute{}); + rewriter.setInsertionPointToStart(rewriter.createBlock(&global.getInitializerRegion())); + mlir::Value zero = rewriter.create(rewriter.getUnknownLoc(), headerType()); + rewriter.create(rewriter.getUnknownLoc(), ValueRange{zero}); + } + + return rewriter.create(loc, th.getPtrType(), name); + } + mlir::Value loadOrZero(mlir::Value header, mlir::Type fieldType, int32_t index) { TypeHelper th(rewriter); - CodeLogicHelper clh(op, rewriter); auto loc = op->getLoc(); - auto isSet = rewriter.create(loc, LLVM::ICmpPredicate::ne, header, - rewriter.create(loc, th.getPtrType())); - return clh.conditionalExpressionLowering( - loc, fieldType, isSet, - [&](OpBuilder &, Location) -> mlir::Value { - return rewriter.create( - loc, fieldType, - rewriter.create(loc, th.getPtrType(), headerType(), header, - ArrayRef{0, index})); - }, - [&](OpBuilder &, Location) -> mlir::Value { return rewriter.create(loc, fieldType); }); + auto isNull = rewriter.create(loc, LLVM::ICmpPredicate::eq, header, + rewriter.create(loc, th.getPtrType())); + mlir::Value readHeader = rewriter.create(loc, isNull, emptyHeader(), header); + return rewriter.create( + loc, fieldType, + rewriter.create(loc, th.getPtrType(), headerType(), readHeader, + ArrayRef{0, index})); } }; diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 7adc22a72..da284abaf 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -40,6 +40,8 @@ #endif #include "llvm/Analysis/LoopAnalysisManager.h" +#include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/IR/Intrinsics.h" #include "llvm/Analysis/CGSCCPassManager.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/Operator.h" @@ -79,6 +81,7 @@ extern cl::opt sizeLevel; extern cl::opt disableWarnings; extern cl::opt verifyOwnership; extern cl::opt ownSkipInference; +extern cl::opt mainFuncName; extern cl::opt noStackPromotion; extern cl::opt noBorrowFactsExport; extern cl::opt stackPromotionMaxObject; @@ -342,6 +345,101 @@ static std::optional mapToLevel(unsigned optLevel, unsi return std::nullopt; } +// A C math function has two results: its value, and `errno` when the argument is out of its +// domain. TypeScript has no `errno` (`Math.sqrt(-1)` is NaN and nothing else), so a call of one, +// whether the default library's or a program's own `declare function sqrt(v: number): number`, +// is the LLVM intrinsic, as clang's -fno-math-errno and rustc make it. As a libm call that may +// write `errno` it kept loads from being hoisted over it, counted as a call against inlining the +// function around it, and `sqrt` kept a branch to the library for a negative argument. +static void applyNoMathErrno(llvm::Module &m) +{ + llvm::TargetLibraryInfoImpl tlii(llvm::Triple(m.getTargetTriple())); + llvm::TargetLibraryInfo tli(tlii); + + for (auto &func : m) + { + llvm::LibFunc libFunc; + if (!func.isDeclaration() || func.isIntrinsic() || !tli.getLibFunc(func, libFunc)) + { + continue; + } + + auto intrinsic = llvm::Intrinsic::not_intrinsic; + switch (libFunc) + { + case llvm::LibFunc_sqrt: case llvm::LibFunc_sqrtf: intrinsic = llvm::Intrinsic::sqrt; break; + case llvm::LibFunc_pow: case llvm::LibFunc_powf: intrinsic = llvm::Intrinsic::pow; break; + case llvm::LibFunc_exp: case llvm::LibFunc_expf: intrinsic = llvm::Intrinsic::exp; break; + case llvm::LibFunc_exp2: case llvm::LibFunc_exp2f: intrinsic = llvm::Intrinsic::exp2; break; + case llvm::LibFunc_log: case llvm::LibFunc_logf: intrinsic = llvm::Intrinsic::log; break; + case llvm::LibFunc_log2: case llvm::LibFunc_log2f: intrinsic = llvm::Intrinsic::log2; break; + case llvm::LibFunc_log10: case llvm::LibFunc_log10f: intrinsic = llvm::Intrinsic::log10; break; + case llvm::LibFunc_sin: case llvm::LibFunc_sinf: intrinsic = llvm::Intrinsic::sin; break; + case llvm::LibFunc_cos: case llvm::LibFunc_cosf: intrinsic = llvm::Intrinsic::cos; break; + case llvm::LibFunc_floor: case llvm::LibFunc_floorf: intrinsic = llvm::Intrinsic::floor; break; + case llvm::LibFunc_ceil: case llvm::LibFunc_ceilf: intrinsic = llvm::Intrinsic::ceil; break; + case llvm::LibFunc_trunc: case llvm::LibFunc_truncf: intrinsic = llvm::Intrinsic::trunc; break; + case llvm::LibFunc_round: case llvm::LibFunc_roundf: intrinsic = llvm::Intrinsic::round; break; + case llvm::LibFunc_rint: case llvm::LibFunc_rintf: intrinsic = llvm::Intrinsic::rint; break; + case llvm::LibFunc_nearbyint: case llvm::LibFunc_nearbyintf: intrinsic = llvm::Intrinsic::nearbyint; break; + case llvm::LibFunc_fabs: case llvm::LibFunc_fabsf: intrinsic = llvm::Intrinsic::fabs; break; + default: continue; + } + + // a use that is not a direct call (the function's address is taken) keeps the function + llvm::SmallVector calls; + for (auto *user : func.users()) + { + auto *call = llvm::dyn_cast(user); + if (call && call->getCalledOperand() == &func && call->getFunctionType() == func.getFunctionType() + && !call->isMustTailCall()) + { + calls.push_back(call); + } + } + + if (calls.empty()) + { + continue; + } + + auto *intrinsicFunc = llvm::Intrinsic::getOrInsertDeclaration(&m, intrinsic, {func.getReturnType()}); + for (auto *call : calls) + { + llvm::SmallVector args(call->args()); + auto *newCall = llvm::CallInst::Create(intrinsicFunc, args, "", call->getIterator()); + newCall->setDebugLoc(call->getDebugLoc()); + newCall->setTailCallKind(call->getTailCallKind()); + newCall->copyFastMathFlags(call); + newCall->takeName(call); + call->replaceAllUsesWith(newCall); + call->eraseFromParent(); + } + } +} + +// An executable built with --opt: a function no other module can name (not `export`ed, not the +// entry point) is internal, as a `static` C function and a Rust function that is not `pub` are. +// The inliner then knows every caller: a function with one call left is inlined whatever its +// size and its body dropped, which is how the ray tracer's `shade` and `getNaturalColor` end up +// inside `traceRay`. A library's object, a shared library and JIT modules are loaded beside other +// modules that find functions by name, and stay as they were. +static void internalizeModulePrivateFunctions(llvm::Module &m) +{ + for (auto &func : m) + { + if (func.isDeclaration() || !func.hasExternalLinkage() || func.hasDLLExportStorageClass() + || func.getName() == MAIN_ENTRY_NAME || func.getName() == mainFuncName.getValue() + || func.hasFnAttribute("export") || func.hasFnAttribute(DLL_EXPORT) || func.hasFnAttribute(DLL_NAME) + || func.hasSection()) + { + continue; + } + + func.setLinkage(llvm::GlobalValue::InternalLinkage); + } +} + // --size_level: the pipeline's level alone decides little - the inliner's thresholds, the // vectorizer, the unroller and the backend all ask the function whether it is optimized for // size. clang marks every function `optsize` under -Os and `optsize minsize` under -Oz. @@ -425,6 +523,13 @@ std::function makeCustomPassesWithOptimizingTransfo applyFastMathFlags(*m); } + applyNoMathErrno(*m); + + if (compileOptions.isExecutable && *ol != llvm::OptimizationLevel::O0) + { + internalizeModulePrivateFunctions(*m); + } + applySizeAttributes(*m, *ol); // Without a TargetMachine the -O2/-O3 cost model has no target information: no vector @@ -453,7 +558,18 @@ std::function makeCustomPassesWithOptimizingTransfo llvm::CGSCCAnalysisManager cgam; llvm::ModuleAnalysisManager mam; - llvm::PassBuilder pb(effectiveTargetMachine); + // The inliner's threshold at the highest speed level is the one LLVM keeps for functions + // with an inline hint (325, what clang gives every C++ function defined in its class), not + // the plain 250: a TypeScript program is small methods and functions over small values. + // The ray tracer's `intersections` (cost 285; its Rust twin 245 against rustc's 275) was + // left as a call in each ray's loop, with every live number spilled around it. + llvm::PipelineTuningOptions pto; + if (*ol == llvm::OptimizationLevel::O3) + { + pto.InlinerThreshold = 325; + } + + llvm::PassBuilder pb(effectiveTargetMachine, pto); pb.registerModuleAnalyses(mam); pb.registerCGSCCAnalyses(cgam); From be4585c92ad90f8b3876dd7d3ad97ce64fec886d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:26:22 +0000 Subject: [PATCH 04/10] Ray tracer README: the optimizer changes and their timings Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- tslang/benchmarks/raytrace/README.md | 50 ++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 3fe494efd..2b8670ac9 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -61,13 +61,49 @@ shared, so a few percent either way is noise. dist)` is "borrows an element and cannot be stored, returned or captured"), so `Hit` names the thing by its index in `scene.things`. Under none, holding the reference instead measured 2 ms faster. -- What is left, from callgrind (804 million instructions against Rust's 598 million) and the - disassembly of `intersections`, which has half of them; none of it measured separately: - `intersections` is not inlined into its callers, where Rust inlines everything into `trace_ray`; - the array is tested for null and its length and data pointer are loaded again on every iteration; - and `sqrt` is libm's, whose call for a negative argument makes the function spill the ray's six - numbers on entry. Making `Thing` a `@value` class (the things inline in the array, as in Rust's - `Vec`) or reading `things.length` once before the loop changed nothing. +- Making `Thing` a `@value` class (the things inline in the array, as in Rust's `Vec`) or + reading `things.length` once before the loop changed nothing. + +### The optimizer: array reads, math calls, internal functions, the inliner + +callgrind (804 million instructions against Rust's 598 million), the optimized IR and the +inliner's remarks put the gap in four things, each now changed in the compiler: + +- An array's length and data were read as `header == null ? 0 : header->field`, a branch around + the load, so the loop in `intersections` tested the array for null and loaded both again on every + iteration. They are now loaded through a select between the header and one constant empty + header, with no branch, and a loop hoists them. +- `sqrt` was libm's, which may write `errno`: a call that kept loads from being hoisted over it and + a branch to the library for a negative argument. A call of a C math function (`sqrt`, `pow`, + `floor`, ...) is now the LLVM intrinsic, as under clang's `-fno-math-errno` and in Rust. +- Every function had external linkage. With `--opt`, `--emit=exe` now makes a function that is not + exported internal, so one with a single call left is inlined whatever its size: `shade`, + `getNaturalColor` and `getReflectionColor` end up inside `traceRay`, as in the Rust build. +- `intersections` cost the inliner 285 against a threshold of 250 and stayed a call in each ray's + loop, with every live number spilled around it (its Rust twin costs 245 against rustc's 275). + The threshold at `--opt_level=3` is now 325, the one LLVM keeps for functions with an inline + hint. + +`bench.sh`, 21 rounds, the same machine: + +| Build | before | after | vs Rust static (51 ms) | +|---|---|---|---| +| tslang gc, `rtbench_static.ts` | 60 ms | 55 ms | 1.07x | +| tslang rc, `rtbench_static.ts` | 62 ms | 60 ms | 1.18x | +| tslang none, `rtbench_static.ts` | 61 ms | 55 ms | 1.08x | +| tslang own, `rtbench_static.ts` | 60 ms | 55 ms | 1.08x | +| tslang gc, none, own, `rtbench.ts` | 87, 89, 91 ms | 85, 84, 87 ms | | +| tslang rc, `rtbench.ts` | 123 ms | 126 ms | | + +- One at a time, under none (medians of 21 runs): the array reads, the math calls and internal + functions together took 59 ms to 57; the inliner's threshold on top of them, to 54. +- `../numeric`'s six kernels take the same time with the old and the new compiler (within 1%), and + the test suite passes. +- What is left, read from the disassembly and not fixed: a miss is the distance `Infinity`, which + LLVM computes with selects that a miss still runs through (writing the sphere's test with early + returns in a scratch copy took 1.4 ms off); `let i = 0` is a 32-bit counter, sign-extended on + every iteration beside a 64-bit one; and Rust adds two of a vector's three components at a time + (SLP), which turned off changed its time by less than 1%. ## Results 2026-11-10: Windows From 7a8e146f715d7e0a0471328e9861317fedfd6172 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:46:38 +0000 Subject: [PATCH 05/10] Optimizer: jump threading before the first SimplifyCFG A path that knows its answer (a phi of floating-point constants and a test of it) was made a select by SimplifyCFG, which jump threading does not unfold. At --opt_level 2 and 3 the pipeline now starts with SROA and jump threading. Ray tracer README: the timings, and that the 32-bit counter was not a cost. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- tslang/benchmarks/raytrace/README.md | 34 ++++++++++++++++++++++++---- tslang/tslang/transform.cpp | 21 +++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 2b8670ac9..38a71f7f0 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -99,11 +99,35 @@ inliner's remarks put the gap in four things, each now changed in the compiler: functions together took 59 ms to 57; the inliner's threshold on top of them, to 54. - `../numeric`'s six kernels take the same time with the old and the new compiler (within 1%), and the test suite passes. -- What is left, read from the disassembly and not fixed: a miss is the distance `Infinity`, which - LLVM computes with selects that a miss still runs through (writing the sphere's test with early - returns in a scratch copy took 1.4 ms off); `let i = 0` is a 32-bit counter, sign-extended on - every iteration beside a 64-bit one; and Rust adds two of a vector's three components at a time - (SLP), which turned off changed its time by less than 1%. + +### Jump threading before the first SimplifyCFG + +Counting executed instructions per block (callgrind) against the Rust build put most of what was +left on one path: a sphere the ray misses. `intersect` ends `return dist === 0 ? Infinity : dist` +with `dist` a phi of constants on the miss paths; the pipeline's first SimplifyCFG makes the test a +select, and LLVM's jump threading later unfolds a select only over integer constants. A miss ran 8 +instructions of select and then the caller's comparison, where Rust's goes straight to the next +thing. The pipeline now starts with SROA and jump threading (at `--opt_level` 2 and 3), so such a +path is threaded to its result before the select exists. + +| Build, `rtbench_static.ts` | before | after | vs Rust static (50 ms) | +|---|---|---|---| +| tslang gc | 55 ms | 53 ms | 1.06x | +| tslang rc | 60 ms | 57 ms | 1.14x | +| tslang none | 55 ms | 53 ms | 1.06x | +| tslang own | 55 ms | 53 ms | 1.06x | + +- Instructions executed under none: 679 million to 650 million (Rust 604 million, 647 million with + its SLP vectorizer off, which does not change its time). `rtbench.ts` and `../numeric` take the + same time as before, and the test suite passes. +- The 32-bit loop counter named above as a cost is not one: with `let i: index = 0` the program + executes more instructions (661 million) and takes the same time. +- The Rust port written as this file is, a miss as `f64::INFINITY` and the thing by its index, + takes 73 ms: the form costs Rust more than it costs tslang. +- What is left is spread out, a few instructions in every block: the caller's `minsd` still runs + on a miss; the things are pointers in the array, one load more each; `const light = lights[i]` + loads all six numbers of the light, where Rust reads the color only when the point is lit; and + two of the ray's numbers live on the stack in the shadow loop. None was measured by itself. ## Results 2026-11-10: Windows diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index da284abaf..65d5bdc0a 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -50,6 +50,8 @@ #include "llvm/Target/TargetMachine.h" #include "llvm/Passes/OptimizationLevel.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/Transforms/Scalar/SROA.h" +#include "llvm/Transforms/Scalar/JumpThreading.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" #include "llvm/Support/FormatVariadic.h" @@ -570,6 +572,25 @@ std::function makeCustomPassesWithOptimizingTransfo } llvm::PassBuilder pb(effectiveTargetMachine, pto); + // Jump threading before the pipeline's first SimplifyCFG. `let d = 0; if (..) d = ..; + // return d === 0 ? Infinity : d` reaches that pass as a phi of constants and a test of it; + // SimplifyCFG makes the test a select, and jump threading later unfolds a select only over + // integer constants, so a path that knew its answer still ran the select and what the + // caller does with the result. Threaded first, such a path goes straight to its result + // (the ray tracer's misses to the loop's next thing). SROA first: the values are in + // allocas until then. + pb.registerPipelineStartEPCallback([](llvm::ModulePassManager &pm, llvm::OptimizationLevel level) { + if (level != llvm::OptimizationLevel::O2 && level != llvm::OptimizationLevel::O3) + { + return; + } + + llvm::FunctionPassManager fpm; + fpm.addPass(llvm::SROAPass(llvm::SROAOptions::ModifyCFG)); + fpm.addPass(llvm::JumpThreadingPass()); + pm.addPass(llvm::createModuleToFunctionPassAdaptor(std::move(fpm))); + }); + pb.registerModuleAnalyses(mam); pb.registerCGSCCAnalyses(cgam); From e7c10c451e5b9b9fcb25446f6e5cc44c338c725a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:49:04 +0000 Subject: [PATCH 06/10] README: the ray tracer without interfaces against Rust; docs/raytrace-rust-parity.md: what is left Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- README.md | 27 ++++- docs/raytrace-rust-parity.md | 153 +++++++++++++++++++++++++++ tslang/benchmarks/raytrace/README.md | 2 + 3 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 docs/raytrace-rust-parity.md diff --git a/README.md b/README.md index 926c7f975..130395bce 100644 --- a/README.md +++ b/README.md @@ -767,7 +767,9 @@ what to do about it, and **[docs/own-memory-model.md](docs/own-memory-model.md)* **Compared with Rust:** under `-mm=own`, tslang matches Rust on numeric array loops and runs within about 1.35x of idiomatic Rust on an object-heavy ray tracer, about 6.6x faster than the -same program written with Rust's `Rc` for every object. Put shortly: within 1.1-1.4x of idiomatic +same program written with Rust's `Rc` for every object. Written without interfaces and closures, +as Rust code with enums is, the ray tracer runs within 6% of that Rust code under `-mm=own`, +`-mm=gc` and `-mm=none` (measured on Linux x64). Put shortly: within 1.1-1.4x of idiomatic Rust, and on par for tight array loops. These are two benchmarks of our own, measured on Windows x64; on the ray tracer the remaining gap is in code generation (calls through interfaces, structs returned through memory), not in memory management, since `-mm=own` runs within a few percent of @@ -800,6 +802,29 @@ the earlier results. `-mm=rc` went from 110 to 96 ms when retains and releases t interface began changing the count inline; what it still pays over the other models is counting itself, not thread safety (atomics are only used for blocks shared between threads). +The same ray tracer written without interfaces and closures, the way a Rust program with enums +and `match` is (`rtbench_static.ts`: a thing is one `@final` class with a shape tag and a +`switch`, the rest `@value` classes and free functions), against that Rust program +(`rust/src/bin/static_dispatch.rs`). Ubuntu 24.04 x64, a 4-core cloud VM, release build, median of +21 runs, measured 2026-11-11: + +| build | time | vs Rust (enums) | +| --- | --- | --- | +| `-mm=gc` | 53 ms | 1.06x slower | +| `-mm=none` | 53 ms | 1.06x slower | +| `-mm=own` | 53 ms | 1.06x slower | +| `-mm=rc` | 57 ms | 1.14x slower | +| Rust, enums and `match` | 50 ms | | +| Rust, `dyn Thing` (the port above) | 113 ms | | + +With nothing called through a vtable or a function value the memory models run alike, and tslang +is within 6% of Rust. It was 1.2x (60 ms) before five changes to the optimizer made for this +benchmark: an array's length and data are read without a branch on null, a C math function is the +LLVM intrinsic (TypeScript has no `errno`), `--emit=exe` makes functions that are not exported +internal, the inliner's threshold at `--opt_level=3` is 325, and jump threading runs before the +first SimplifyCFG. What is still between tslang and Rust here, and what each part would take, is in +[docs/raytrace-rust-parity.md](docs/raytrace-rust-parity.md). + Loops over arrays are measured by [tslang/benchmarks/numeric](tslang/benchmarks/numeric/README.md): six kernels over flat arrays of 4096 elements, with Rust and C++ ports. On Windows 11 x64 (release build, median of 15 runs, ms, measured 2026-11-10): diff --git a/docs/raytrace-rust-parity.md b/docs/raytrace-rust-parity.md new file mode 100644 index 000000000..eddf9b284 --- /dev/null +++ b/docs/raytrace-rust-parity.md @@ -0,0 +1,153 @@ +# The ray tracer against Rust: what is left + +`tslang/benchmarks/raytrace/rtbench_static.ts` is the ray tracer written without interfaces and +closures; `rust/src/bin/static_dispatch.rs` beside it is the Rust program it mirrors (`enum Thing`, +`enum Surface`, `match`). This note says where the two stand, what was done to get there, and what +is still between them. The benchmark's [README](../tslang/benchmarks/raytrace/README.md) has every +measurement. + +## Where it stands + +Ubuntu 24.04 x64, a 4-core cloud VM, 2026-11-11, `bench.sh` with 21 rounds, median wall time. A +few percent either way is noise on this machine. + +| Build | Time | vs Rust | +|---|---|---| +| tslang gc, none, own | 53 ms | 1.06x | +| tslang rc | 57 ms | 1.14x | +| Rust, enums and `match` | 50 ms | 1x | + +Instructions executed (callgrind), under `-mm=none`: tslang 650 million, Rust 604 million. Without +the math library, whose `floor` and `fmod` Rust compiles in and tslang calls in libm, the +program's own code is about 540 million in tslang against 480 million in Rust with its SLP +vectorizer off (which does not change Rust's time). + +Not measured: Windows, and any machine but this one. + +## What was done + +Five changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first +(`include/TypeScript/LowerToLLVM/ArrayLayout.h`). Together they took the benchmark from 60 to +53 ms. + +| Change | Why | +|---|---| +| An array's length and data are loaded through a select between the header and a constant empty header | The branch on a null array kept both loads inside the loop | +| A call of a C math function is the LLVM intrinsic | `sqrt` could write `errno`: a call, a branch to libm, and no hoisting over it | +| `--emit=exe` with `--opt` makes functions that are not exported internal | The inliner inlines a function with one call left whatever its size | +| The inliner's threshold at `--opt_level=3` is 325 | `intersections` cost 285 against 250 and stayed a call in every ray's loop | +| SROA and jump threading run before the first SimplifyCFG (levels 2 and 3) | A miss's `dist === 0 ? Infinity : dist` became a select that jump threading does not unfold | + +Open questions about these, none of them checked: + +- The threshold of 325 applies to every program at `--opt_level=3`. Its effect on code size and + compile time of a large program was not measured. With 250 the benchmark takes about 57 ms. +- The early SROA and jump threading run on every function at levels 2 and 3. Compile time was not + measured. +- Internal functions: the rule is "not `export`ed, not the entry point, no section, in an + executable". The test suite passes; a program whose other objects call a function of the main + module by name without `export` would no longer link. + +## What is left + +Counting executed instructions per block against the Rust build (how: below) puts nearly all of +the difference in the loop over the things, which runs 9 million times a frame: 13 instructions a +thing in the shadow loop and 15 in the primary loop, against 8 and 7 in Rust. The rest is a few +instructions per shadow ray (54 against 44) and per shaded point (127 against 113). None of the +parts below was measured by itself, and the estimates of what each is worth are instruction +counts, not times. + +### 1. The caller's minimum still runs on a miss + +`intersections` does `if (dist < closest) { index = i; closest = dist; }`. SimplifyCFG turns it +into selects (`minsd`, and `ucomisd` + `cmova` for the index) before `Thing.intersect` is inlined +into the loop. After inlining, a miss arrives as the constant `Infinity`, for which the test is +false, but jump threading does not thread a constant through a select whose other operand is not +a constant. So a miss still runs the minimum, reloads the constant `Infinity` and copies a +register: 3 to 6 instructions a thing, about 30 million a frame, and `closest` is carried from one +iteration to the next through `minsd`. + +What it would take, one of: + +- keep the caller's `if` a branch until its callee is inlined (SimplifyCFG's folding of a + two-entry phi, off in the pipeline's first run), then let jump threading and the later + SimplifyCFG decide. `--two-entry-phi-node-folding-threshold=0` on the command line did not + change the code, so the select is made somewhere else; which pass makes it was not found; +- a pass of our own, after inlining, that unfolds a select over a phi with a floating-point + constant among its values. + +Writing a miss as `number | undefined` in the benchmark (Rust's `Option`) does not avoid +this: the flag becomes a phi of `true`/`false` feeding the same selects. + +### 2. Things are pointers in the array + +`Thing[]` holds pointers to class instances, so each thing costs one load more than Rust's +`Vec`, and the hit thing is loaded again for shading. Rust with `Vec>` is 3 ms +slower than with `Vec`, which is the size of what is left. + +`@value class Thing` puts the things in the array itself and compiles today, but measured no +faster: each element is then loaded as one aggregate, all six fields, before the shape is tested. +What it would take: an element of an array of `@value` records read field by field where only +fields are used (`things[i].intersect(ray)` with `intersect` inlined), instead of copied whole. + +### 3. `const light = lights[i]` copies the light + +The same copy, seen in the shadow loop: all six numbers of the light are loaded at the top of the +iteration and the three of its color are stored on the stack, where Rust reads the color only for +a point the light reaches. About 9 instructions a shadow ray, 14 million a frame. The fix is the +one of part 2: a `const` bound to an element of a `@value` array that nothing writes can be read +in place. + +### 4. Two of the ray's numbers live on the stack in the shadow loop + +x86-64 has 16 vector registers and none of them survives a call. The shadow loop keeps the point, +the direction, the normal, the reflected direction and the color so far alive, and two numbers end +up on the stack (`subsd 0x10(%rsp), %xmm7` in the sphere test). Rust's SLP vectorizer packs two of +a vector's three components into one register, which leaves it room. In tslang SLP does not fire +on this code at all (no packed instruction in the binary). Why was not looked into; a guess is +that a `@value` argument arrives as separate scalars (`{ double, double, double }` by value) where +Rust's arrives as a pointer whose two first fields load as one vector. + +### 5. `-mm=rc` is 4 ms behind the other models + +Not looked into. The program allocates nothing per ray, so it is counting: probably the things +read out of the array and the scene's arrays. + +### Not a cost: the 32-bit loop counter + +`let i = 0` is an `s32`, sign-extended beside a 64-bit induction variable (2 instructions a +thing). With `let i: index = 0` the program executes more instructions (661 million against 650 +million) and takes the same time. + +## How to measure + +```bash +TSLANG= RUNS=21 bash tslang/benchmarks/raytrace/bench.sh +``` + +Wall time on a shared machine moves by a millisecond between runs, which is a third of what is +left, so compare instruction counts first: + +```bash +valgrind --tool=callgrind --dump-instr=yes --compress-pos=no --callgrind-out-file=cg.out ./rtbs_none +``` + +Everything is inlined into `traceRay` (`trace_ray` in Rust), so the function totals say nothing. +Join the per-address counts of `cg.out` with `objdump -d` and group the instructions by how often +they ran: blocks of the two builds that ran the same number of times are the same block of the +source (6,353,088 is a thing in the shadow loop, 4,764,816 a sphere there, 1,588,272 a shadow ray, +397,068 a shaded point), and the number of instructions in each is what to compare. + +The inliner's decisions: + +```bash +tslang --emit=llvm --opt --opt_level=3 --no-default-lib --mm=none rtbench_static.ts -o s.ll +opt -passes='cgscc(inline)' -pass-remarks-missed=inline -disable-output s.ll +``` + +LLVM's hidden options are accepted on tslang's command line (`--help-hidden`), which is the quick +way to try a threshold. + +One comparison to keep in mind: the Rust port written as `rtbench_static.ts` is, a miss as +`f64::INFINITY` and the thing by its index, takes 73 ms. Part of what is left is the form of the +source, which costs Rust more than it costs tslang. diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 38a71f7f0..cd5b36c36 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -124,6 +124,8 @@ path is threaded to its result before the select exists. executes more instructions (661 million) and takes the same time. - The Rust port written as this file is, a miss as `f64::INFINITY` and the thing by its index, takes 73 ms: the form costs Rust more than it costs tslang. +- [docs/raytrace-rust-parity.md](../../../docs/raytrace-rust-parity.md) lists what is left and what + each part would take. - What is left is spread out, a few instructions in every block: the caller's `minsd` still runs on a miss; the things are pointers in the array, one load more each; `const light = lights[i]` loads all six numbers of the light, where Rust reads the color only when the point is lit; and From 2dd49b884751bb1e311f0015f79e5d0d752b7e6a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:57:55 +0000 Subject: [PATCH 07/10] Optimizer: a select over a phi with a deciding constant is made a branch again; the ray tracer takes Rust's time After a function's callees are inlined, a select whose condition compares a phi of its block and is decided for one of the phi's constants becomes a branch, and jump threading sends those predecessors past it. rtbench_static.ts: 53 to 50 ms under gc, none and own (Rust 50.4 ms); rc 57 to 53 ms. README, the benchmark's README and docs/raytrace-rust-parity.md updated. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- README.md | 29 +++--- docs/raytrace-rust-parity.md | 129 +++++++++++---------------- tslang/benchmarks/raytrace/README.md | 27 ++++++ tslang/tslang/transform.cpp | 128 ++++++++++++++++++++++++++ 4 files changed, 221 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 130395bce..82866ad51 100644 --- a/README.md +++ b/README.md @@ -768,7 +768,7 @@ what to do about it, and **[docs/own-memory-model.md](docs/own-memory-model.md)* **Compared with Rust:** under `-mm=own`, tslang matches Rust on numeric array loops and runs within about 1.35x of idiomatic Rust on an object-heavy ray tracer, about 6.6x faster than the same program written with Rust's `Rc` for every object. Written without interfaces and closures, -as Rust code with enums is, the ray tracer runs within 6% of that Rust code under `-mm=own`, +as Rust code with enums is, the ray tracer takes the same time as that Rust code under `-mm=own`, `-mm=gc` and `-mm=none` (measured on Linux x64). Put shortly: within 1.1-1.4x of idiomatic Rust, and on par for tight array loops. These are two benchmarks of our own, measured on Windows x64; on the ray tracer the remaining gap is in code generation (calls through interfaces, structs @@ -810,19 +810,22 @@ and `match` is (`rtbench_static.ts`: a thing is one `@final` class with a shape | build | time | vs Rust (enums) | | --- | --- | --- | -| `-mm=gc` | 53 ms | 1.06x slower | -| `-mm=none` | 53 ms | 1.06x slower | -| `-mm=own` | 53 ms | 1.06x slower | -| `-mm=rc` | 57 ms | 1.14x slower | +| `-mm=gc` | 50 ms | the same | +| `-mm=none` | 50 ms | the same | +| `-mm=own` | 50 ms | the same | +| `-mm=rc` | 53 ms | 1.05x slower | | Rust, enums and `match` | 50 ms | | -| Rust, `dyn Thing` (the port above) | 113 ms | | - -With nothing called through a vtable or a function value the memory models run alike, and tslang -is within 6% of Rust. It was 1.2x (60 ms) before five changes to the optimizer made for this -benchmark: an array's length and data are read without a branch on null, a C math function is the -LLVM intrinsic (TypeScript has no `errno`), `--emit=exe` makes functions that are not exported -internal, the inliner's threshold at `--opt_level=3` is 325, and jump threading runs before the -first SimplifyCFG. What is still between tslang and Rust here, and what each part would take, is in +| Rust, `dyn Thing` (the port above) | 112 ms | | + +With nothing called through a vtable or a function value the memory models run alike, and under +`-mm=gc`, `-mm=none` and `-mm=own` tslang takes the time of the Rust program and executes as many +instructions (606 million against 604 million). It was 1.2x (60 ms) before six changes to the +optimizer made for this benchmark: an array's length and data are read without a branch on null, a +C math function is the LLVM intrinsic (TypeScript has no `errno`), `--emit=exe` makes functions +that are not exported internal, the inliner's threshold at `--opt_level=3` is 325, jump threading +runs before the first SimplifyCFG, and a select over a phi with a constant is made a branch again +once its function's callees are inlined. `-mm=rc` still retains and releases the array of things +on every ray. The steps and what is left are in [docs/raytrace-rust-parity.md](docs/raytrace-rust-parity.md). Loops over arrays are measured by [tslang/benchmarks/numeric](tslang/benchmarks/numeric/README.md): diff --git a/docs/raytrace-rust-parity.md b/docs/raytrace-rust-parity.md index eddf9b284..3352d62bb 100644 --- a/docs/raytrace-rust-parity.md +++ b/docs/raytrace-rust-parity.md @@ -1,10 +1,9 @@ -# The ray tracer against Rust: what is left +# The ray tracer against Rust: where it stands and what is left `tslang/benchmarks/raytrace/rtbench_static.ts` is the ray tracer written without interfaces and closures; `rust/src/bin/static_dispatch.rs` beside it is the Rust program it mirrors (`enum Thing`, `enum Surface`, `match`). This note says where the two stand, what was done to get there, and what -is still between them. The benchmark's [README](../tslang/benchmarks/raytrace/README.md) has every -measurement. +is left. The benchmark's [README](../tslang/benchmarks/raytrace/README.md) has every measurement. ## Where it stands @@ -13,22 +12,21 @@ few percent either way is noise on this machine. | Build | Time | vs Rust | |---|---|---| -| tslang gc, none, own | 53 ms | 1.06x | -| tslang rc | 57 ms | 1.14x | -| Rust, enums and `match` | 50 ms | 1x | +| tslang gc | 50.4 ms | 1.00x | +| tslang none, own | 50.0 ms | 0.99x | +| tslang rc | 53.0 ms | 1.05x | +| Rust, enums and `match` | 50.4 ms | 1x | -Instructions executed (callgrind), under `-mm=none`: tslang 650 million, Rust 604 million. Without -the math library, whose `floor` and `fmod` Rust compiles in and tslang calls in libm, the -program's own code is about 540 million in tslang against 480 million in Rust with its SLP -vectorizer off (which does not change Rust's time). +Instructions executed (callgrind), under `-mm=none`: tslang 606 million, Rust 604 million. -Not measured: Windows, and any machine but this one. +Under gc, none and own the program takes the time of the Rust one. Under rc it does not (part 1 +below). Not measured: Windows, and any machine but this one. ## What was done -Five changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first +Six changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first (`include/TypeScript/LowerToLLVM/ArrayLayout.h`). Together they took the benchmark from 60 to -53 ms. +50 ms. | Change | Why | |---|---| @@ -37,87 +35,60 @@ Five changes to the optimizer, all in `tslang/tslang/transform.cpp` except the f | `--emit=exe` with `--opt` makes functions that are not exported internal | The inliner inlines a function with one call left whatever its size | | The inliner's threshold at `--opt_level=3` is 325 | `intersections` cost 285 against 250 and stayed a call in every ray's loop | | SROA and jump threading run before the first SimplifyCFG (levels 2 and 3) | A miss's `dist === 0 ? Infinity : dist` became a select that jump threading does not unfold | +| A select whose condition compares a phi and is decided for one of the phi's constants is made a branch, then jump threading (levels 2 and 3, after a function's callees are inlined) | The caller's `if (dist < closest)` was selects before `intersect` was inlined, so a miss still ran the minimum | Open questions about these, none of them checked: - The threshold of 325 applies to every program at `--opt_level=3`. Its effect on code size and - compile time of a large program was not measured. With 250 the benchmark takes about 57 ms. -- The early SROA and jump threading run on every function at levels 2 and 3. Compile time was not - measured. + compile time of a large program was not measured. +- The early SROA and jump threading, and the select pass with its jump threading, run on every + function at levels 2 and 3. Compile time was not measured. - Internal functions: the rule is "not `export`ed, not the entry point, no section, in an executable". The test suite passes; a program whose other objects call a function of the main module by name without `export` would no longer link. +- The select pass makes a branch of a select that the processor may have predicted worse than it + computed the select. It fires only where a predecessor's constant decides the condition, and the + branch that is left for the other predecessors is given back to SimplifyCFG. No program but + these two benchmarks was timed with it. ## What is left -Counting executed instructions per block against the Rust build (how: below) puts nearly all of -the difference in the loop over the things, which runs 9 million times a frame: 13 instructions a -thing in the shadow loop and 15 in the primary loop, against 8 and 7 in Rust. The rest is a few -instructions per shadow ray (54 against 44) and per shaded point (127 against 113). None of the -parts below was measured by itself, and the estimates of what each is worth are instruction -counts, not times. +### 1. `-mm=rc`: the array of things is retained on every ray -### 1. The caller's minimum still runs on a miss +`const things = scene.things` in `intersections` takes a reference: the array's count goes up at +the top of the function and down at the end, through a release routine that is not inlined, 2.2 +million times a frame. That is the 3 ms rc is behind (711 million instructions against 606 +million). Using `scene.things` in the loop instead retains it on every use and is worse (934 +million, 65 ms). -`intersections` does `if (dist < closest) { index = i; closest = dist; }`. SimplifyCFG turns it -into selects (`minsd`, and `ucomisd` + `cmova` for the index) before `Thing.intersect` is inlined -into the loop. After inlining, a miss arrives as the constant `Infinity`, for which the test is -false, but jump threading does not thread a constant through a select whose other operand is not -a constant. So a miss still runs the minimum, reloads the constant `Infinity` and copies a -register: 3 to 6 instructions a thing, about 30 million a frame, and `closest` is carried from one -iteration to the next through `minsd`. +The retain is there because a call in the loop could assign `scene.things` and so free the array +under the alias. Here nothing can: once `Thing.intersect` is inlined the loop is arithmetic. What +it would take: dropping a retain and its release when nothing between them can release or store +(a pass over the LLVM IR after inlining, as Objective-C's ARC optimizer does), or the same proof +in the ownership inference, from the callee's facts ("writes no field"), before lowering. -What it would take, one of: +### 2. Differences that are still there and cost nothing measured -- keep the caller's `if` a branch until its callee is inlined (SimplifyCFG's folding of a - two-entry phi, off in the pipeline's first run), then let jump threading and the later - SimplifyCFG decide. `--two-entry-phi-node-folding-threshold=0` on the command line did not - change the code, so the select is made somewhere else; which pass makes it was not found; -- a pass of our own, after inlining, that unfolds a select over a phi with a floating-point - constant among its values. +With the instruction counts level, these are no longer a gap, but the code still differs from +Rust's in them: -Writing a miss as `number | undefined` in the benchmark (Rust's `Option`) does not avoid -this: the flag becomes a phi of `true`/`false` feeding the same selects. +- `Thing[]` holds pointers, one load more a thing than Rust's `Vec`. `@value class Thing` + puts the things in the array itself and measured no faster: each element is loaded whole, all + six fields, before the shape is tested. +- `const light = lights[i]` loads all six numbers of the light at the top of the iteration, where + Rust reads the color only for a point the light reaches. Sinking the loads with LLVM's sink pass + changed nothing. +- LLVM's SLP vectorizer packs two of a vector's three components in the Rust build and does not + fire in tslang's. Turning it off in Rust does not change Rust's time. +- `let i = 0` is a 32-bit counter beside a 64-bit induction variable. With `let i: index = 0` the + program executes more instructions and takes the same time. -### 2. Things are pointers in the array +### 3. The benchmark with interfaces -`Thing[]` holds pointers to class instances, so each thing costs one load more than Rust's -`Vec`, and the hit thing is loaded again for shading. Rust with `Vec>` is 3 ms -slower than with `Vec`, which is the size of what is left. - -`@value class Thing` puts the things in the array itself and compiles today, but measured no -faster: each element is then loaded as one aggregate, all six fields, before the shape is tested. -What it would take: an element of an array of `@value` records read field by field where only -fields are used (`things[i].intersect(ray)` with `intersect` inlined), instead of copied whole. - -### 3. `const light = lights[i]` copies the light - -The same copy, seen in the shadow loop: all six numbers of the light are loaded at the top of the -iteration and the three of its color are stored on the stack, where Rust reads the color only for -a point the light reaches. About 9 instructions a shadow ray, 14 million a frame. The fix is the -one of part 2: a `const` bound to an element of a `@value` array that nothing writes can be read -in place. - -### 4. Two of the ray's numbers live on the stack in the shadow loop - -x86-64 has 16 vector registers and none of them survives a call. The shadow loop keeps the point, -the direction, the normal, the reflected direction and the color so far alive, and two numbers end -up on the stack (`subsd 0x10(%rsp), %xmm7` in the sphere test). Rust's SLP vectorizer packs two of -a vector's three components into one register, which leaves it room. In tslang SLP does not fire -on this code at all (no packed instruction in the binary). Why was not looked into; a guess is -that a `@value` argument arrives as separate scalars (`{ double, double, double }` by value) where -Rust's arrives as a pointer whose two first fields load as one vector. - -### 5. `-mm=rc` is 4 ms behind the other models - -Not looked into. The program allocates nothing per ray, so it is counting: probably the things -read out of the array and the scene's arrays. - -### Not a cost: the 32-bit loop counter - -`let i = 0` is an `s32`, sign-extended beside a 64-bit induction variable (2 instructions a -thing). With `let i: index = 0` the program executes more instructions (661 million against 650 -million) and takes the same time. +`rtbench.ts` (interfaces, closures) is unchanged by all this at 84 ms under gc, none and own, 1.7x +the static version. Its costs are the calls through `Thing` and `Surface` and the `addLight` +closure; see the benchmark's README. The Rust port written the same way (`dyn Thing`) takes +112 ms here. ## How to measure @@ -149,5 +120,5 @@ LLVM's hidden options are accepted on tslang's command line (`--help-hidden`), w way to try a threshold. One comparison to keep in mind: the Rust port written as `rtbench_static.ts` is, a miss as -`f64::INFINITY` and the thing by its index, takes 73 ms. Part of what is left is the form of the -source, which costs Rust more than it costs tslang. +`f64::INFINITY` and the thing by its index, takes 73 ms: the form costs Rust more than it costs +tslang. diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index cd5b36c36..37957e265 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -131,6 +131,33 @@ path is threaded to its result before the select exists. loads all six numbers of the light, where Rust reads the color only when the point is lit; and two of the ray's numbers live on the stack in the shadow loop. None was measured by itself. +### Selects over a phi made branches again + +Part 1 of [docs/raytrace-rust-parity.md](../../../docs/raytrace-rust-parity.md): `intersections` +does `if (dist < closest) { index = i; closest = dist; }`, which SimplifyCFG makes selects before +`Thing.intersect` is inlined into the loop. A miss then arrives as the constant `Infinity`, for +which the test is false, and still ran the minimum. A pass of our own, after a function's callees +are inlined, turns a select back into a branch when its condition compares a phi of the same block +and is decided for one of the phi's constants; jump threading, run next, sends those predecessors +past it. A miss now goes to the next thing, as in the Rust build. + +`bench.sh`, 21 rounds, the same machine: + +| Build, `rtbench_static.ts` | before | after | vs Rust static (50.4 ms) | +|---|---|---|---| +| tslang gc | 53 ms | 50.4 ms | 1.00x | +| tslang rc | 57 ms | 53.0 ms | 1.05x | +| tslang none | 53 ms | 50.0 ms | 0.99x | +| tslang own | 53 ms | 50.0 ms | 0.99x | + +- Instructions executed under none: 650 million to 606 million; Rust executes 604 million. +- `rtbench.ts` and `../numeric` take the same time as before, and the test suite passes. +- Sinking the loads of `const light = lights[i]` to where the color is used (LLVM's sink pass at + the end of the pipeline) changed neither the instruction count nor the time, and is not in. +- rc: `const things = scene.things` retains the array and releases it at the end of every + `intersections` call, 2.2 million times a frame (711 million instructions against 606 million). + Without the alias it is worse: every `scene.things` in the loop is retained (934 million, 65 ms). + ## Results 2026-11-10: Windows Release, Windows 11 x64, the machine of the 10-31 run; main d64978ed, and the same tree with the diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 65d5bdc0a..73096f43a 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -52,6 +52,8 @@ #include "llvm/Passes/PassBuilder.h" #include "llvm/Transforms/Scalar/SROA.h" #include "llvm/Transforms/Scalar/JumpThreading.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" #include "llvm/Support/FormatVariadic.h" @@ -442,6 +444,122 @@ static void internalizeModulePrivateFunctions(llvm::Module &m) } } +// A select whose condition compares a phi of the same block, when the comparison is decided for +// one of the phi's constant values: made a branch again, which jump threading (run next) threads +// for the predecessors that bring those constants. SimplifyCFG turns `if (d < closest) { closest = +// d; index = i; }` into selects before the function computing `d` is inlined; once it is, a path +// on which `d` is Infinity still runs them, and jump threading unfolds a select only over integer +// constants compared with a constant. +struct UnfoldSelectOverPhiPass : llvm::PassInfoMixin +{ + static llvm::CmpInst *decidedCompare(llvm::SelectInst *select, const llvm::DataLayout &dl) + { + auto *block = select->getParent(); + auto *cmp = llvm::dyn_cast(select->getCondition()); + if (!cmp || cmp->getParent() != block || !select->getType()->isSingleValueType()) + { + return nullptr; + } + + auto *phi = llvm::dyn_cast(cmp->getOperand(0)); + auto phiIsLhs = true; + if (!phi || phi->getParent() != block) + { + phi = llvm::dyn_cast(cmp->getOperand(1)); + phiIsLhs = false; + } + + if (!phi || phi->getParent() != block) + { + return nullptr; + } + + for (unsigned i = 0, e = phi->getNumIncomingValues(); i != e; ++i) + { + auto *constant = llvm::dyn_cast(phi->getIncomingValue(i)); + if (!constant) + { + continue; + } + + auto *other = cmp->getOperand(phiIsLhs ? 1 : 0)->DoPHITranslation(block, phi->getIncomingBlock(i)); + auto *lhs = phiIsLhs ? static_cast(constant) : other; + auto *rhs = phiIsLhs ? other : static_cast(constant); + if (auto *folded = llvm::simplifyCmpInst(cmp->getPredicate(), lhs, rhs, {dl})) + { + if (llvm::isa(folded)) + { + return cmp; + } + } + } + + return nullptr; + } + + llvm::PreservedAnalyses run(llvm::Function &func, llvm::FunctionAnalysisManager &) + { + auto &dl = func.getDataLayout(); + auto changed = false; + llvm::SmallVector candidates; + for (auto &block : func) + { + for (auto &inst : block) + { + auto *select = llvm::dyn_cast(&inst); + if (select && decidedCompare(select, dl)) + { + candidates.push_back(select); + break; + } + } + } + + for (auto *first : candidates) + { + auto *block = first->getParent(); + auto *cmp = llvm::cast(first->getCondition()); + + // every select on that comparison whose operands are computed before the first one + llvm::SmallVector selects; + for (auto it = first->getIterator(), end = block->end(); it != end; ++it) + { + auto *select = llvm::dyn_cast(&*it); + if (!select || select->getCondition() != cmp || !select->getType()->isSingleValueType()) + { + continue; + } + + auto before = [&](llvm::Value *value) { + auto *inst = llvm::dyn_cast(value); + return !inst || inst->getParent() != block || inst->comesBefore(first); + }; + if (before(select->getTrueValue()) && before(select->getFalseValue())) + { + selects.push_back(select); + } + } + + auto *thenTerminator = llvm::SplitBlockAndInsertIfThen(cmp, first->getIterator(), /*Unreachable=*/false); + auto *thenBlock = thenTerminator->getParent(); + auto *tail = first->getParent(); + for (auto *select : selects) + { + auto *phi = llvm::PHINode::Create(select->getType(), 2, "", tail->begin()); + phi->addIncoming(select->getTrueValue(), thenBlock); + phi->addIncoming(select->getFalseValue(), block); + phi->takeName(select); + select->replaceAllUsesWith(phi); + select->eraseFromParent(); + } + + changed = true; + } + + return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all(); + } +}; + // --size_level: the pipeline's level alone decides little - the inliner's thresholds, the // vectorizer, the unroller and the backend all ask the function whether it is optimized for // size. clang marks every function `optsize` under -Os and `optsize minsize` under -Oz. @@ -592,6 +710,16 @@ std::function makeCustomPassesWithOptimizingTransfo }); + // after a function's callees are inlined into it + pb.registerScalarOptimizerLateEPCallback([](llvm::FunctionPassManager &fpm, llvm::OptimizationLevel level) { + if (level != llvm::OptimizationLevel::O2 && level != llvm::OptimizationLevel::O3) + { + return; + } + + fpm.addPass(UnfoldSelectOverPhiPass()); + fpm.addPass(llvm::JumpThreadingPass()); + }); pb.registerModuleAnalyses(mam); pb.registerCGSCCAnalyses(cgam); pb.registerFunctionAnalyses(fam); From 3af2bf0febe34bfd38337136ee87d02655228316 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 12:08:24 +0000 Subject: [PATCH 08/10] rc: a local alias is not counted where nothing can release what it names; the ray tracer takes Rust's time under every model Under -mm=rc, before the inliner, a retain of a local (tsret_(slot)) and its release on every path are removed when the function and everything it calls store only into locals and call nothing that could release. The ray tracer's intersections counted the array of things up and down on every ray: rtbench_static.ts under rc 53 to 50.6 ms (Rust 49.6 ms). Test: 00rc_local_alias_no_count.ts, with a callee that releases the array. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- README.md | 28 +- docs/raytrace-rust-parity.md | 50 +-- tslang/benchmarks/raytrace/README.md | 22 ++ tslang/test/tester/CMakeLists.txt | 3 + .../tester/tests/00rc_local_alias_no_count.ts | 39 +++ tslang/tslang/transform.cpp | 318 +++++++++++++++++- 6 files changed, 423 insertions(+), 37 deletions(-) create mode 100644 tslang/test/tester/tests/00rc_local_alias_no_count.ts diff --git a/README.md b/README.md index 82866ad51..5a2d3b4ef 100644 --- a/README.md +++ b/README.md @@ -768,8 +768,8 @@ what to do about it, and **[docs/own-memory-model.md](docs/own-memory-model.md)* **Compared with Rust:** under `-mm=own`, tslang matches Rust on numeric array loops and runs within about 1.35x of idiomatic Rust on an object-heavy ray tracer, about 6.6x faster than the same program written with Rust's `Rc` for every object. Written without interfaces and closures, -as Rust code with enums is, the ray tracer takes the same time as that Rust code under `-mm=own`, -`-mm=gc` and `-mm=none` (measured on Linux x64). Put shortly: within 1.1-1.4x of idiomatic +as Rust code with enums is, the ray tracer takes the same time as that Rust code under every +memory model (measured on Linux x64). Put shortly: within 1.1-1.4x of idiomatic Rust, and on par for tight array loops. These are two benchmarks of our own, measured on Windows x64; on the ray tracer the remaining gap is in code generation (calls through interfaces, structs returned through memory), not in memory management, since `-mm=own` runs within a few percent of @@ -813,19 +813,19 @@ and `match` is (`rtbench_static.ts`: a thing is one `@final` class with a shape | `-mm=gc` | 50 ms | the same | | `-mm=none` | 50 ms | the same | | `-mm=own` | 50 ms | the same | -| `-mm=rc` | 53 ms | 1.05x slower | +| `-mm=rc` | 51 ms | the same | | Rust, enums and `match` | 50 ms | | -| Rust, `dyn Thing` (the port above) | 112 ms | | - -With nothing called through a vtable or a function value the memory models run alike, and under -`-mm=gc`, `-mm=none` and `-mm=own` tslang takes the time of the Rust program and executes as many -instructions (606 million against 604 million). It was 1.2x (60 ms) before six changes to the -optimizer made for this benchmark: an array's length and data are read without a branch on null, a -C math function is the LLVM intrinsic (TypeScript has no `errno`), `--emit=exe` makes functions -that are not exported internal, the inliner's threshold at `--opt_level=3` is 325, jump threading -runs before the first SimplifyCFG, and a select over a phi with a constant is made a branch again -once its function's callees are inlined. `-mm=rc` still retains and releases the array of things -on every ray. The steps and what is left are in +| Rust, `dyn Thing` (the port above) | 113 ms | | + +With nothing called through a vtable or a function value the four memory models run alike, and +tslang takes the time of the Rust program (under `-mm=none` it executes 606 million instructions +against Rust's 604 million). It was 1.2x (60 ms) before seven changes to the optimizer made for +this benchmark: an array's length and data are read without a branch on null, a C math function +is the LLVM intrinsic (TypeScript has no `errno`), `--emit=exe` makes functions that are not +exported internal, the inliner's threshold at `--opt_level=3` is 325, jump threading runs before +the first SimplifyCFG, a select over a phi with a constant is made a branch again once its +function's callees are inlined, and under `-mm=rc` a local alias is not counted where nothing can +release what it names. The steps, their open questions and what is left are in [docs/raytrace-rust-parity.md](docs/raytrace-rust-parity.md). Loops over arrays are measured by [tslang/benchmarks/numeric](tslang/benchmarks/numeric/README.md): diff --git a/docs/raytrace-rust-parity.md b/docs/raytrace-rust-parity.md index 3352d62bb..9222b642c 100644 --- a/docs/raytrace-rust-parity.md +++ b/docs/raytrace-rust-parity.md @@ -12,19 +12,21 @@ few percent either way is noise on this machine. | Build | Time | vs Rust | |---|---|---| -| tslang gc | 50.4 ms | 1.00x | -| tslang none, own | 50.0 ms | 0.99x | -| tslang rc | 53.0 ms | 1.05x | -| Rust, enums and `match` | 50.4 ms | 1x | +| tslang gc | 50.5 ms | 1.02x | +| tslang rc | 50.6 ms | 1.02x | +| tslang none | 49.6 ms | 1.00x | +| tslang own | 49.5 ms | 1.00x | +| Rust, enums and `match` | 49.6 ms | 1x | -Instructions executed (callgrind), under `-mm=none`: tslang 606 million, Rust 604 million. +Instructions executed (callgrind): tslang 606 million under `-mm=none` and 622 million under +`-mm=rc`, Rust 604 million. -Under gc, none and own the program takes the time of the Rust one. Under rc it does not (part 1 -below). Not measured: Windows, and any machine but this one. +Under every memory model the program takes the time of the Rust one, within this machine's noise. +Not measured: Windows, and any machine but this one. ## What was done -Six changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first +Seven changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first (`include/TypeScript/LowerToLLVM/ArrayLayout.h`). Together they took the benchmark from 60 to 50 ms. @@ -36,6 +38,7 @@ Six changes to the optimizer, all in `tslang/tslang/transform.cpp` except the fi | The inliner's threshold at `--opt_level=3` is 325 | `intersections` cost 285 against 250 and stayed a call in every ray's loop | | SROA and jump threading run before the first SimplifyCFG (levels 2 and 3) | A miss's `dist === 0 ? Infinity : dist` became a select that jump threading does not unfold | | A select whose condition compares a phi and is decided for one of the phi's constants is made a branch, then jump threading (levels 2 and 3, after a function's callees are inlined) | The caller's `if (dist < closest)` was selects before `intersect` was inlined, so a miss still ran the minimum | +| `-mm=rc`: a retain of a local and its release on every path are removed when the function and everything it calls store only into locals and can release nothing (before the inliner) | `const things = scene.things` was counted up and down on every ray | Open questions about these, none of them checked: @@ -51,26 +54,29 @@ Open questions about these, none of them checked: branch that is left for the other predecessors is given back to SimplifyCFG. No program but these two benchmarks was timed with it. +- The rc pass trusts its own reading of what a function can do: a store is local when LLVM's + `getUnderlyingObject` finds an alloca or a parameter, a callee is safe when it is defined in the + module and passes the same test, or is a declaration that only reads memory. It finds the pair + by the names `tsret_` and `tsrel_`. The test suite passes and + `00rc_local_alias_no_count.ts` covers a callee that releases the array; nothing else checks it, + and an object shared between threads and released by another thread while the alias is in use + is not considered (the function's caller holds a reference to the owner in the case measured). + ## What is left -### 1. `-mm=rc`: the array of things is retained on every ray +Nothing in time, on this benchmark and this machine. What follows is what still differs. -`const things = scene.things` in `intersections` takes a reference: the array's count goes up at -the top of the function and down at the end, through a release routine that is not inlined, 2.2 -million times a frame. That is the 3 ms rc is behind (711 million instructions against 606 -million). Using `scene.things` in the loop instead retains it on every use and is worse (934 -million, 65 ms). +### 1. rc counts that the alias pass does not reach -The retain is there because a call in the loop could assign `scene.things` and so free the array -under the alias. Here nothing can: once `Thing.intersect` is inlined the loop is arithmetic. What -it would take: dropping a retain and its release when nothing between them can release or store -(a pass over the LLVM IR after inlining, as Objective-C's ARC optimizer does), or the same proof -in the ownership inference, from the callee's facts ("writes no field"), before lowering. +The pass removes a pair only around code that can release nothing. `rtbench.ts` (interfaces, +closures) under rc is unchanged at 119 ms against 84 ms for the other models: its counts are on +interface values and closure cells, and its loop calls through `Thing`, which the pass cannot see +through. The benchmark's README has what those counts are. ### 2. Differences that are still there and cost nothing measured -With the instruction counts level, these are no longer a gap, but the code still differs from -Rust's in them: +With the instruction counts level, these are no gap, but the code still differs from Rust's in +them: - `Thing[]` holds pointers, one load more a thing than Rust's `Vec`. `@value class Thing` puts the things in the array itself and measured no faster: each element is loaded whole, all @@ -86,7 +92,7 @@ Rust's in them: ### 3. The benchmark with interfaces `rtbench.ts` (interfaces, closures) is unchanged by all this at 84 ms under gc, none and own, 1.7x -the static version. Its costs are the calls through `Thing` and `Surface` and the `addLight` +the static version, and 119 ms under rc. Its costs are the calls through `Thing` and `Surface` and the `addLight` closure; see the benchmark's README. The Rust port written the same way (`dyn Thing`) takes 112 ms here. diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 37957e265..704bfcdc8 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -158,6 +158,28 @@ past it. A miss now goes to the next thing, as in the Rust build. `intersections` call, 2.2 million times a frame (711 million instructions against 606 million). Without the alias it is worse: every `scene.things` in the loop is retained (934 million, 65 ms). +### rc: a local alias that needs no count + +`const things = scene.things` retained the array and released it at the end of every +`intersections` call. The count keeps the array alive should something release it under the alias; +here nothing can. Under `-mm=rc`, before the inliner runs, a retain of a local and its release on +every path are now removed when the function, and every function it calls, stores only into its +own locals (or what a parameter given a local points to) and calls nothing that could release: +`tsret_(slot)` and `tsrel_(slot)` on a slot stored to once. A callee that assigns the +field keeps the pair (`test/tester/tests/00rc_local_alias_no_count.ts`). + +`bench.sh`, 21 rounds, the same machine: + +| Build, `rtbench_static.ts` | before | after | vs Rust static (49.6 ms) | +|---|---|---|---| +| tslang gc | 50.4 ms | 50.5 ms | 1.02x | +| tslang rc | 53.0 ms | 50.6 ms | 1.02x | +| tslang none | 50.0 ms | 49.6 ms | 1.00x | +| tslang own | 50.0 ms | 49.5 ms | 1.00x | + +Instructions executed under rc: 711 million to 622 million. `rtbench.ts` under rc is unchanged +(119 ms): its counts are on interfaces and closures, which this does not reach. + ## Results 2026-11-10: Windows Release, Windows 11 x64, the machine of the 10-31 run; main d64978ed, and the same tree with the diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 0f1393afe..4061dece5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -623,6 +623,7 @@ tslang_add_test(NAME test-compile-00-object-literal-contextual-lambda COMMAND te tslang_add_test(NAME test-compile-own-00-object-literal-contextual-lambda COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-compile-00-interface-field-getter-owned COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-compile-00-rc-interface-count-inline COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_interface_count_inline.ts") +tslang_add_test(NAME test-compile-00-rc-local-alias-no-count COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_local_alias_no_count.ts") tslang_add_test(NAME test-compile-00-conditional-record-literal-temporary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_record_literal_temporary.ts") tslang_add_test(NAME test-compile-00-conditional-nested-record-literal-temporary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_nested_record_literal_temporary.ts") tslang_add_test(NAME test-compile-00-generic-callback-closure-late-class COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_callback_closure_late_class.ts") @@ -1299,6 +1300,7 @@ tslang_add_test(NAME test-jit-00-object-literal-contextual-lambda COMMAND test-r tslang_add_test(NAME test-jit-own-00-object-literal-contextual-lambda COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-jit-00-interface-field-getter-owned COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-jit-00-rc-interface-count-inline COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_interface_count_inline.ts") +tslang_add_test(NAME test-jit-00-rc-local-alias-no-count COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_local_alias_no_count.ts") tslang_add_test(NAME test-jit-00-conditional-record-literal-temporary COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_record_literal_temporary.ts") tslang_add_test(NAME test-jit-00-conditional-nested-record-literal-temporary COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_nested_record_literal_temporary.ts") tslang_add_test(NAME test-jit-00-generic-callback-closure-late-class COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_callback_closure_late_class.ts") @@ -2480,6 +2482,7 @@ set(TSLANG_CORPUS 00object_literal_contextual_lambda.ts 00interface_field_getter_owned.ts 00rc_interface_count_inline.ts + 00rc_local_alias_no_count.ts 00conditional_record_literal_temporary.ts 00conditional_nested_record_literal_temporary.ts 00generic_callback_closure_late_class.ts diff --git a/tslang/test/tester/tests/00rc_local_alias_no_count.ts b/tslang/test/tester/tests/00rc_local_alias_no_count.ts new file mode 100644 index 000000000..71e5499f5 --- /dev/null +++ b/tslang/test/tester/tests/00rc_local_alias_no_count.ts @@ -0,0 +1,39 @@ +// -mm=rc: a local alias of a field keeps the array alive only where something could release it. +class Box { + items: number[] = [1, 2, 3]; +} + +function replace(b: Box) { + b.items = [7]; +} + +// nothing here can release the array: the alias needs no count +function sum(b: Box) { + const items = b.items; + let s: number = 0; + for (let i = 0; i < items.length; i++) { + s += items[i]; + } + + return s; +} + +// `replace` releases the array the alias names: the alias has to hold it +function sumAfterReplace(b: Box) { + const items = b.items; + replace(b); + let s: number = 0; + for (let i = 0; i < items.length; i++) { + s += items[i]; + } + + return s + b.items[0]; +} + +function main() { + const b = new Box(); + assert(sum(b) == 6, "sum"); + assert(sumAfterReplace(b) == 13, "sum after replace"); + assert(sum(b) == 7, "sum of the new array"); + print("done."); +} diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 73096f43a..1686aad4a 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -54,6 +54,10 @@ #include "llvm/Transforms/Scalar/JumpThreading.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Analysis/InstructionSimplify.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/ADT/SmallBitVector.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/CFG.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" #include "llvm/Support/FormatVariadic.h" @@ -560,6 +564,312 @@ struct UnfoldSelectOverPhiPass : llvm::PassInfoMixin } }; +// -mm=rc: `const things = scene.things` retains the array and releases it when the local ends. +// The count protects the alias from a release of what it names, and nothing between the two can +// release anything when the function, and every function it calls, stores only into its own +// locals and calls nothing that could: then the retain and the release go. They are the +// out-of-line routines `tsret_(slot)` and `tsrel_(slot)` at this point, before the +// inliner opens them, and the pair is found by its slot: an alloca stored to once, with one retain +// and, on every path after it, one release. The ray tracer's `intersections` paid this pair on +// every ray. +struct RcPairEliminationPass : llvm::PassInfoMixin +{ + struct Info + { + bool neutral = true; + llvm::SmallBitVector written; + }; + + llvm::DenseMap infos; + + static bool isCountCall(const llvm::Instruction &inst, llvm::StringRef prefix, llvm::StringRef *type = nullptr) + { + auto *call = llvm::dyn_cast(&inst); + auto *callee = call ? call->getCalledFunction() : nullptr; + if (!callee || !callee->getName().starts_with(prefix) || call->arg_size() != 1) + { + return false; + } + + if (type) + { + *type = callee->getName().substr(prefix.size()); + } + + return true; + } + + // a store through `pointer`: into a local (true), into what a parameter points to (true, and + // the parameter is marked), anywhere else (false) + static bool localWrite(const llvm::Value *pointer, Info &info) + { + auto *base = llvm::getUnderlyingObject(pointer); + if (llvm::isa(base)) + { + return true; + } + + if (auto *arg = llvm::dyn_cast(base)) + { + info.written.set(arg->getArgNo()); + return true; + } + + return false; + } + + // whether `func` changes no count and no memory but its locals and what its marked parameters + // point to, leaving out the instructions in `ignored` + Info scan(const llvm::Function &func, const llvm::SmallPtrSetImpl &ignored) + { + Info info; + info.written.resize(func.arg_size()); + for (auto &inst : llvm::instructions(func)) + { + if (ignored.contains(&inst)) + { + continue; + } + + if (auto *store = llvm::dyn_cast(&inst)) + { + if (store->isVolatile() || !localWrite(store->getPointerOperand(), info)) + { + info.neutral = false; + } + } + else if (auto *call = llvm::dyn_cast(&inst)) + { + auto *callee = call->getCalledFunction(); + if (!callee || call->isInlineAsm()) + { + info.neutral = false; + } + else if (auto *transfer = llvm::dyn_cast(call)) + { + if (transfer->isVolatile() || !localWrite(transfer->getRawDest(), info)) + { + info.neutral = false; + } + } + else if (callee->isDeclaration()) + { + if (!callee->onlyReadsMemory() && !(callee->isIntrinsic() && !call->mayWriteToMemory())) + { + info.neutral = false; + } + } + else + { + auto found = infos.find(callee); + if (found == infos.end() || !found->second.neutral || callee->isVarArg() + || callee->arg_size() != call->arg_size()) + { + info.neutral = false; + } + else + { + for (auto index : found->second.written.set_bits()) + { + if (!localWrite(call->getArgOperand(index), info)) + { + info.neutral = false; + } + } + } + } + } + else if (llvm::isa(&inst) + || inst.mayWriteToMemory()) + { + info.neutral = false; + } + + if (!info.neutral) + { + break; + } + } + + return info; + } + + // the retain and the releases of `slot`, when they pair on every path + static bool pairedCalls(llvm::AllocaInst *slot, llvm::SmallVectorImpl &calls) + { + llvm::StoreInst *theStore = nullptr; + llvm::StringRef type; + auto retains = 0; + for (auto *user : slot->users()) + { + auto *inst = llvm::dyn_cast(user); + llvm::StringRef callType; + if (auto *store = llvm::dyn_cast_or_null(inst)) + { + if (theStore || store->getPointerOperand() != slot || store->getValueOperand() == slot) + { + return false; + } + + theStore = store; + } + else if (inst && (isCountCall(*inst, "tsret_", &callType) || isCountCall(*inst, "tsrel_", &callType))) + { + if (!type.empty() && type != callType) + { + return false; + } + + type = callType; + retains += isCountCall(*inst, "tsret_") ? 1 : 0; + calls.push_back(inst); + } + else if (!llvm::isa_and_nonnull(inst)) + { + return false; + } + } + + if (!theStore || retains != 1 || calls.size() < 2) + { + return false; + } + + // per block: whether the reference is held on entry; every predecessor has to agree + auto *func = slot->getFunction(); + llvm::DenseMap heldAt; + llvm::SmallVector worklist; + heldAt[&func->getEntryBlock()] = false; + worklist.push_back(&func->getEntryBlock()); + while (!worklist.empty()) + { + auto *block = worklist.pop_back_val(); + auto held = heldAt[block]; + for (auto &inst : *block) + { + if (&inst == theStore) + { + if (held) + { + return false; + } + } + else if (llvm::is_contained(calls, &inst)) + { + auto retain = isCountCall(inst, "tsret_"); + if (retain == held) + { + return false; + } + + held = retain; + } + else if (llvm::isa(inst) && held) + { + return false; + } + } + + for (auto *successor : llvm::successors(block)) + { + auto inserted = heldAt.try_emplace(successor, held); + if (inserted.second) + { + worklist.push_back(successor); + } + else if (inserted.first->second != held) + { + return false; + } + } + } + + return true; + } + + llvm::PreservedAnalyses run(llvm::Module &m, llvm::ModuleAnalysisManager &) + { + auto erased = false; + for (auto changed = true; changed;) + { + changed = false; + + // from "every function is neutral" down to what holds + infos.clear(); + llvm::SmallPtrSet none; + for (auto &func : m) + { + if (!func.isDeclaration()) + { + infos[&func].written.resize(func.arg_size()); + } + } + + for (auto settled = false; !settled;) + { + settled = true; + for (auto &func : m) + { + if (func.isDeclaration()) + { + continue; + } + + auto &known = infos[&func]; + auto info = known.neutral ? scan(func, none) : known; + if (info.neutral != known.neutral || info.written != known.written) + { + known = info; + settled = false; + } + } + } + + for (auto &func : m) + { + if (func.isDeclaration()) + { + continue; + } + + llvm::SmallVector calls; + for (auto &inst : func.getEntryBlock()) + { + if (auto *slot = llvm::dyn_cast(&inst)) + { + llvm::SmallVector slotCalls; + if (pairedCalls(slot, slotCalls)) + { + calls.append(slotCalls.begin(), slotCalls.end()); + } + } + } + + if (calls.empty()) + { + continue; + } + + llvm::SmallPtrSet ignored(calls.begin(), calls.end()); + if (!scan(func, ignored).neutral) + { + continue; + } + + for (auto *call : calls) + { + call->eraseFromParent(); + } + + changed = erased = true; + } + } + + return erased ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all(); + } +}; + // --size_level: the pipeline's level alone decides little - the inliner's thresholds, the // vectorizer, the unroller and the backend all ask the function whether it is optimized for // size. clang marks every function `optsize` under -Os and `optsize minsize` under -Oz. @@ -697,12 +1007,18 @@ std::function makeCustomPassesWithOptimizingTransfo // caller does with the result. Threaded first, such a path goes straight to its result // (the ray tracer's misses to the loop's next thing). SROA first: the values are in // allocas until then. - pb.registerPipelineStartEPCallback([](llvm::ModulePassManager &pm, llvm::OptimizationLevel level) { + auto refCounted = compileOptions.isRefCounted(); + pb.registerPipelineStartEPCallback([refCounted](llvm::ModulePassManager &pm, llvm::OptimizationLevel level) { if (level != llvm::OptimizationLevel::O2 && level != llvm::OptimizationLevel::O3) { return; } + if (refCounted) + { + pm.addPass(RcPairEliminationPass()); + } + llvm::FunctionPassManager fpm; fpm.addPass(llvm::SROAPass(llvm::SROAOptions::ModifyCFG)); fpm.addPass(llvm::JumpThreadingPass()); From 8bb570e7c581866e6a25aed6dd785d8e1d09335e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 12:26:42 +0000 Subject: [PATCH 09/10] Calls through a function value try the module's few candidates directly; null of a @value type writes only its pointers - --opt_level=3, before the inliner: a call through a function value whose type at most four address-taken functions of the module have tests the pointer against each and calls it directly; the indirect call is kept for anything else. The runtime's own routines are not candidates. - null of a @value type that holds null in a pointer is its pointers null and its numbers undefined, not every field zeroed. rtbench.ts: 84 to 77 ms under gc, 83 to 78 under none, 87 to 82 under own, 119 to 111 under rc (Rust dyn Thing 116 ms, C++ 70 ms on this machine). 00fresh_objects_folded.ts: the callback, now inlined into keep, reads no field. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- README.md | 5 + docs/raytrace-rust-parity.md | 47 +++++++++- tslang/benchmarks/raytrace/README.md | 41 +++++++++ .../TypeScript/LowerToLLVM/CastLogicHelper.h | 44 ++++++++- .../tester/tests/00fresh_objects_folded.ts | 5 +- tslang/tslang/transform.cpp | 91 +++++++++++++++++++ 6 files changed, 226 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5a2d3b4ef..e8dc25137 100644 --- a/README.md +++ b/README.md @@ -817,6 +817,11 @@ and `match` is (`rtbench_static.ts`: a thing is one `@final` class with a shape | Rust, enums and `match` | 50 ms | | | Rust, `dyn Thing` (the port above) | 113 ms | | +On the same machine the version with interfaces above (`rtbench.ts`) takes 77 ms under `-mm=gc`, +78 under `-mm=none`, 82 under `-mm=own` and 111 under `-mm=rc`, against 116 ms for the Rust port +with `dyn Thing` and 70 ms for the C++ port: a call through an interface or a callback now first +tries, as direct calls, the few functions of the module it can be. + With nothing called through a vtable or a function value the four memory models run alike, and tslang takes the time of the Rust program (under `-mm=none` it executes 606 million instructions against Rust's 604 million). It was 1.2x (60 ms) before seven changes to the optimizer made for diff --git a/docs/raytrace-rust-parity.md b/docs/raytrace-rust-parity.md index 9222b642c..00bb098d8 100644 --- a/docs/raytrace-rust-parity.md +++ b/docs/raytrace-rust-parity.md @@ -26,7 +26,7 @@ Not measured: Windows, and any machine but this one. ## What was done -Seven changes to the optimizer, all in `tslang/tslang/transform.cpp` except the first +Seven changes to the optimizer for the static version, all in `tslang/tslang/transform.cpp` except the first (`include/TypeScript/LowerToLLVM/ArrayLayout.h`). Together they took the benchmark from 60 to 50 ms. @@ -91,10 +91,47 @@ them: ### 3. The benchmark with interfaces -`rtbench.ts` (interfaces, closures) is unchanged by all this at 84 ms under gc, none and own, 1.7x -the static version, and 119 ms under rc. Its costs are the calls through `Thing` and `Surface` and the `addLight` -closure; see the benchmark's README. The Rust port written the same way (`dyn Thing`) takes -112 ms here. +`rtbench.ts` (interfaces, closures) against the ports written the same way (`dyn Thing`, +`std::unique_ptr`), the same machine and day: + +| Build | Time | +|---|---| +| tslang gc | 77 ms | +| tslang none | 78 ms | +| tslang own | 82 ms | +| tslang rc | 111 ms | +| Rust idiomatic | 116 ms | +| C++ idiomatic (clang) | 70 ms | + +Two more changes got it there from 84 ms (119 under rc): a call through a function value first +tries, as direct calls, the few functions of the module that have its type +(`SpeculativeDirectCallPass`, `--opt_level=3`), and null of a `@value` type with a pointer writes +only its pointers (`CastLogicHelper::nullOfValueType`). + +Every model is ahead of the Rust port here, but the Rust port is slow on this machine for the +instructions it executes (900 million; the C++ port 828 million in 70 ms; tslang none 1148 +million). On Windows the Rust and C++ ports were level at 57 and 52 ms with tslang at 73 to +77 ms, and nothing in this note was measured there. Take the C++ port as the target: tslang is +1.1x of it under gc and none, 1.6x under rc. + +What is left, from the disassembly of the `addLight` closure, none of it measured apart: + +- `scene.things` is read again on every iteration, through the `Scene` interface: the call kept + for "any other function" may write anywhere, so nothing is hoisted over it. It would take + knowing that no other function can be there: every class that implements `Thing` is in the + module and `Thing` is not exported, the same closed world that already makes a class's method + call direct. Then the kept call is unreachable and the loop is the static version's. +- The pointer is compared with `Thing.null.intersect`, the method of a null `Thing`, as with the + sphere's and the plane's: one compare a thing. +- `intersect` returns `Intersection | null` through memory, 80 bytes with the optional's flag, + and its caller tests it there. +- rc: an `Intersection` holds a `Thing`, counted when it is copied, and the `addLight` closure's + captures are counted. 33 ms over the other models; the alias pass above does not apply (the + loop calls through a pointer). The benchmark's README (2026-11-10, "rc") has the counts. +- Open questions for the direct-call pass: it adds up to four tests and calls at every call + through a pointer of a matching type, so code grows; candidates are chosen by LLVM function + type alone, so an unrelated function of the same type is tried too; the order is by size. No + program but the benchmarks was timed with it, and compile time was not measured. ## How to measure diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index 704bfcdc8..c24b2c452 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -180,6 +180,47 @@ field keeps the pair (`test/tester/tests/00rc_local_alias_no_count.ts`). Instructions executed under rc: 711 million to 622 million. `rtbench.ts` under rc is unchanged (119 ms): its counts are on interfaces and closures, which this does not reach. +### `rtbench.ts`, with interfaces: direct calls tried first, and a null that writes less + +The same day and machine, `bench.sh`, 21 rounds. Two changes for the version with interfaces and +closures: + +- A call through a function value (an interface's method, a callback) whose type at most four + functions of the module have, among those whose address is taken, first tests the pointer + against each and calls it directly; the call through the pointer is kept for anything else + (`--opt_level=3`, before the inliner). `thing.intersect(ray)` becomes "a sphere's? a plane's?" + with both bodies inlined in the loop, and `reduce`'s callback, the surfaces' functions and + `getPoint`'s closures are called directly as well. Nothing is assumed about who else may supply + a pointer. The runtime's own routines (`tsrel...`, `tsret...`) are not candidates. +- null of a `@value` type that holds null in a pointer (`Intersection | null`) is its pointers + null and its numbers undefined, not every field zeroed: a miss in `intersect` stored nine words + where two say it. + +| Build, `rtbench.ts` | before | null writes less | and direct calls | vs Rust idiomatic (116 ms) | vs C++ idiomatic (70 ms) | +|---|---|---|---|---|---| +| tslang gc | 84 ms | | 77 ms | 0.66x | 1.10x | +| tslang rc | 119 ms | 118 ms | 111 ms | 0.96x | 1.59x | +| tslang none | 83 ms | 81 ms | 78 ms | 0.68x | 1.12x | +| tslang own | 87 ms | | 82 ms | 0.71x | 1.17x | + +- Instructions executed under none: 1305 million, 1252 million with the smaller null, 1148 + million with the direct calls; the C++ port executes 828 million and the Rust port 900 million. +- On this machine the Rust port with `dyn Thing` is slow for its instruction count (116 ms; the + C++ port 70 ms), as in the Linux table of 2026-11-10, so "against Rust" is met here by every + model, and the C++ port is the one still ahead. On Windows the two ports are level (57 and + 52 ms on 2026-11-10, tslang 73 to 77 ms); nothing here was run on Windows. +- What the loop over the things still does that the C++ port's does not, read from the + disassembly of the `addLight` closure (23 instructions a thing beside the sphere's arithmetic) + and not measured apart: `scene.things` is read again through the `Scene` interface on every + iteration (the field's offset from the vtable, the array's header and data), because the call + kept for "anything else" may write anywhere; each thing is an interface, two words and a load + of its vtable's slot; the pointer is compared with `Thing`'s method for a null object as well + as the sphere's and the plane's; and `intersect`'s result comes back through memory with the + optional's flag. +- `test/tester/tests/00fresh_objects_folded.ts`: its callback no longer reads `v.x`. The callback + is now inlined into `keep`, where that read was a load the test's check took for one of the + fresh objects' fields. + ## Results 2026-11-10: Windows Release, Windows 11 x64, the machine of the 10-31 run; main d64978ed, and the same tree with the diff --git a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h index fd329d274..e3f03929e 100644 --- a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h @@ -48,6 +48,48 @@ class CastLogicHelper bool external; public: + // null of a @value type that holds null in a pointer of its own: every pointer in it null, and + // its numbers left undefined. Nothing reads a null's numbers (null is asked of the pointer), and + // with every field zeroed a function returning null stored the whole record - the ray tracer's + // `intersect` wrote nine words for a miss where the null takes two. + mlir::Value nullOfValueType(mlir::Type llvmType) + { + mlir::Value value = rewriter.create(loc, llvmType); + llvm::SmallVector path; + std::function clearPointers = [&](mlir::Type type) { + if (auto structType = dyn_cast(type)) + { + for (auto [index, field] : llvm::enumerate(structType.getBody())) + { + path.push_back(index); + clearPointers(field); + path.pop_back(); + } + } + else if (auto arrayType = dyn_cast(type)) + { + for (unsigned index = 0; index < arrayType.getNumElements(); index++) + { + path.push_back(index); + clearPointers(arrayType.getElementType()); + path.pop_back(); + } + } + else if (isa(type)) + { + value = rewriter.create(loc, value, rewriter.create(loc, type), path); + } + }; + + if (!isa(llvmType)) + { + return rewriter.create(loc, llvmType); + } + + clearPointers(llvmType); + return value; + } + CastLogicHelper(Operation *op, PatternRewriter &rewriter, TypeConverterHelper &tch, CompileOptions &compileOptions) : op(op), rewriter(rewriter), tch(tch), th(rewriter, compileOptions), ch(op, rewriter, tch.typeConverter, compileOptions), clh(op, rewriter), loc(op->getLoc()), compileOptions(compileOptions), external(false) @@ -743,7 +785,7 @@ class CastLogicHelper // (MLIRTypeHelper::getValueNullNiche): T with every field zeroed, that pointer too if (isa(inType) && isa(baseType)) { - return rewriter.create(loc, tch.convertType(baseType)); + return nullOfValueType(tch.convertType(baseType)); } return cast(in, inType, tch.convertType(inType), baseType, tch.convertType(baseType)); diff --git a/tslang/test/tester/tests/00fresh_objects_folded.ts b/tslang/test/tester/tests/00fresh_objects_folded.ts index 257595db8..98b3190a9 100644 --- a/tslang/test/tester/tests/00fresh_objects_folded.ts +++ b/tslang/test/tester/tests/00fresh_objects_folded.ts @@ -21,7 +21,10 @@ export function keep(a: number, b: number, sink: (v: V) => void) { } function main() { + // The callback reads no field: with --opt it is called directly from `keep` and inlined there + // (the only function of its type), and a read of `v.x` in it, after its own store to `n`, would + // be a load in `keep` that says nothing about the two fresh objects. let n = 0; - assert(keep(1, 2, (v: V) => { n += v.x; }) == 3 && n == 3, "the fields are the ones stored"); + assert(keep(1, 2, (v: V) => { n += 1; }) == 3 && n == 2, "the fields are the ones stored"); print("done."); } diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 1686aad4a..a56a24a49 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -53,6 +53,7 @@ #include "llvm/Transforms/Scalar/SROA.h" #include "llvm/Transforms/Scalar/JumpThreading.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include "llvm/Transforms/Utils/CallPromotionUtils.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/ADT/SmallBitVector.h" @@ -870,6 +871,91 @@ struct RcPairEliminationPass : llvm::PassInfoMixin } }; +// A call through a function value (an interface's method, a callback) whose type only a few +// functions of the module have, among those whose address is taken: the call tests the pointer +// against each and calls it directly, and the call through the pointer is left for anything else. +// The direct calls are what the inliner and every pass after it can see through; a ray tracer's +// `thing.intersect(ray)` becomes "a sphere? a plane?" with both bodies in the loop, as a `switch` +// over the kind of thing would be. Nothing is assumed about who else may supply a pointer. +struct SpeculativeDirectCallPass : llvm::PassInfoMixin +{ + static constexpr unsigned MaxTargets = 4; + + llvm::PreservedAnalyses run(llvm::Module &m, llvm::ModuleAnalysisManager &) + { + llvm::DenseMap> targetsOf; + for (auto &func : m) + { + // not the runtime's own routines (a type's release, its descriptor's callbacks), whose + // addresses are taken too and whose types can be a callback's + auto name = func.getName(); + if (name.starts_with("tsrel") || name.starts_with("tsret") || name.starts_with("__ts") || name.starts_with("ts_")) + { + continue; + } + + if (!func.isDeclaration() && !func.isVarArg() && func.hasAddressTaken() + && !func.hasFnAttribute(llvm::Attribute::NoInline) && !func.hasFnAttribute(llvm::Attribute::OptimizeNone)) + { + targetsOf[func.getFunctionType()].push_back(&func); + } + } + + // the larger function first: a stub (an interface's method for a null object, an empty + // callback) is the one least often called + for (auto &entry : targetsOf) + { + llvm::stable_sort(entry.second, [](llvm::Function *a, llvm::Function *b) { + return a->getInstructionCount() > b->getInstructionCount(); + }); + } + + llvm::SmallVector calls; + for (auto &func : m) + { + if (func.hasFnAttribute(llvm::Attribute::OptimizeNone)) + { + continue; + } + + for (auto &inst : llvm::instructions(func)) + { + auto *call = llvm::dyn_cast(&inst); + if (call && call->isIndirectCall() && !call->isMustTailCall() && !call->isInlineAsm()) + { + calls.push_back(call); + } + } + } + + auto changed = false; + for (auto *call : calls) + { + auto found = targetsOf.find(call->getFunctionType()); + if (found == targetsOf.end() || found->second.size() > MaxTargets) + { + continue; + } + + llvm::CallBase *indirect = call; + for (auto *target : found->second) + { + if (target->getCallingConv() != indirect->getCallingConv() || !llvm::isLegalToPromote(*indirect, target)) + { + continue; + } + + // the direct call goes into the "then" block; what is returned is it, and the + // call through the pointer stays in the "else" block for the next target + llvm::promoteCallWithIfThenElse(*indirect, target); + changed = true; + } + } + + return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all(); + } +}; + // --size_level: the pipeline's level alone decides little - the inliner's thresholds, the // vectorizer, the unroller and the backend all ask the function whether it is optimized for // size. clang marks every function `optsize` under -Os and `optsize minsize` under -Oz. @@ -1014,6 +1100,11 @@ std::function makeCustomPassesWithOptimizingTransfo return; } + if (level == llvm::OptimizationLevel::O3) + { + pm.addPass(SpeculativeDirectCallPass()); + } + if (refCounted) { pm.addPass(RcPairEliminationPass()); From a2dc1aec2440c535748bfe3542f7530820d9b41b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 12:52:22 +0000 Subject: [PATCH 10/10] @final on an interface: only classes of the module implement it, and its methods are called directly - @final interface I: a value of I is made from a class object or null. A cast from an object literal, another interface's value or any, an interface that extends I, export, and an exported or declared class implementing I are errors. - The read of a method of I and every vtable made for I are marked in the IR; at --opt_level=3 SpeculativeDirectCallPass takes the functions in the method's slot of those vtables (at most four) and calls the last without a test, so no call through a pointer is left. - rtbench.ts and rtbench_own.ts: @final interface Thing. 75.5 to 70.7 ms under gc and none (C++ port 68-73 ms on this machine), 111.6 to 105.5 under rc. Tests: 00final_interface.ts under every model, three error tests. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb --- README.md | 4 +- docs/own-memory-model.md | 7 ++ docs/raytrace-rust-parity.md | 32 ++++-- tslang/benchmarks/raytrace/README.md | 31 ++++++ tslang/benchmarks/raytrace/rtbench.ts | 3 + tslang/benchmarks/raytrace/rtbench_own.ts | 3 + tslang/include/TypeScript/Defines.h | 6 + .../TypeScript/MLIRLogic/MLIRGenStore.h | 6 +- tslang/lib/TypeScript/LowerToLLVM.cpp | 61 +++++++++- tslang/lib/TypeScript/MLIRGenCast.cpp | 17 +++ tslang/lib/TypeScript/MLIRGenClasses.cpp | 18 +++ tslang/lib/TypeScript/MLIRGenImpl.h | 10 ++ tslang/lib/TypeScript/MLIRGenInterfaces.cpp | 33 ++++++ tslang/test/tester/CMakeLists.txt | 16 +++ tslang/test/tester/tests/00final_interface.ts | 87 +++++++++++++++ .../tests/final_interface_export_error.ts | 9 ++ .../tests/final_interface_extends_error.ts | 13 +++ .../tests/final_interface_object_error.ts | 11 ++ tslang/test/tester/verify-ownership.cmake | 3 + tslang/tslang/transform.cpp | 105 ++++++++++++++++++ 20 files changed, 459 insertions(+), 16 deletions(-) create mode 100644 tslang/test/tester/tests/00final_interface.ts create mode 100644 tslang/test/tester/tests/final_interface_export_error.ts create mode 100644 tslang/test/tester/tests/final_interface_extends_error.ts create mode 100644 tslang/test/tester/tests/final_interface_object_error.ts diff --git a/README.md b/README.md index e8dc25137..ff8e93f6c 100644 --- a/README.md +++ b/README.md @@ -820,7 +820,9 @@ and `match` is (`rtbench_static.ts`: a thing is one `@final` class with a shape On the same machine the version with interfaces above (`rtbench.ts`) takes 77 ms under `-mm=gc`, 78 under `-mm=none`, 82 under `-mm=own` and 111 under `-mm=rc`, against 116 ms for the Rust port with `dyn Thing` and 70 ms for the C++ port: a call through an interface or a callback now first -tries, as direct calls, the few functions of the module it can be. +tries, as direct calls, the few functions of the module it can be. With `@final interface Thing` +(only classes of this module implement it) no call through a pointer is left, and it takes 71 ms +under `-mm=gc` and `-mm=none`, the C++ port's time, and 106 ms under `-mm=rc`. With nothing called through a vtable or a function value the four memory models run alike, and tslang takes the time of the Rust program (under `-mm=none` it executes 606 million instructions diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index 2794c1b15..063837047 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -667,6 +667,13 @@ that can run TypeScript code (one that takes a callback). of type `C` is a direct call, with no vtable lookup ([design](../tslang/docs/superpowers/specs/2026-10-19-final-classes-design.md)). +`@final interface I`: only classes of this module implement `I`. A value of type `I` is made from +a class object or null and nothing else: a cast from an object literal, from another interface's +value or from `any` is an error, as are an interface that extends `I`, `export` on `I`, and an +exported or declared class that implements it. In return, with `--opt_level=3` a call of one of +`I`'s methods that at most four functions can be is those functions called directly, with no call +through a pointer left. + Under `-mm=own` it matters more. A method of an exported class that is not `@final` may be overridden in another module, by a method that frees anything; the compiler cannot see that method, so a call of it ends every borrow the caller holds, and its result is not known to be diff --git a/docs/raytrace-rust-parity.md b/docs/raytrace-rust-parity.md index 00bb098d8..4f5596e9a 100644 --- a/docs/raytrace-rust-parity.md +++ b/docs/raytrace-rust-parity.md @@ -111,18 +111,26 @@ only its pointers (`CastLogicHelper::nullOfValueType`). Every model is ahead of the Rust port here, but the Rust port is slow on this machine for the instructions it executes (900 million; the C++ port 828 million in 70 ms; tslang none 1148 million). On Windows the Rust and C++ ports were level at 57 and 52 ms with tslang at 73 to -77 ms, and nothing in this note was measured there. Take the C++ port as the target: tslang is -1.1x of it under gc and none, 1.6x under rc. - -What is left, from the disassembly of the `addLight` closure, none of it measured apart: - -- `scene.things` is read again on every iteration, through the `Scene` interface: the call kept - for "any other function" may write anywhere, so nothing is hoisted over it. It would take - knowing that no other function can be there: every class that implements `Thing` is in the - module and `Thing` is not exported, the same closed world that already makes a class's method - call direct. Then the kept call is unreachable and the loop is the static version's. -- The pointer is compared with `Thing.null.intersect`, the method of a null `Thing`, as with the - sphere's and the plane's: one compare a thing. +77 ms, and nothing in this note was measured there. Take the C++ port as the target: tslang was +1.1x of it under gc and none, 1.6x under rc, with these two changes. + +A third change since: `@final interface Thing`. `@final` on an interface says only classes of +this module implement it; the compiler refuses everything else that could make a value of it (an +object literal, another interface's value, `any`, an extending interface, `export`, an exported +class), and a call of its method is then exactly the functions in the vtables the module made: +the last is called without a test and no call through a pointer is left. `rtbench.ts` takes +70.7 ms under gc and 70.8 under none (from 75.5; the C++ port 68 to 73 ms in a noisy run of the +same hour), 105.5 under rc (from 111.6). gc and none are at the C++ port within this machine's +noise; rc is about 1.5x of it. + +What is left, from the disassembly of the `addLight` closure before `@final`, none of it measured +apart: + +- Done by `@final interface Thing`: the call kept for "any other function", which made + `scene.things` be read again on every iteration, and the compare with the null `Thing`'s method. +- `@final` is the source saying it. The compiler could find the same for an interface that is not + exported, in an executable, with no annotation; that is not done. `Surface` and `Scene`, made + from object literals, cannot be `@final`. - `intersect` returns `Intersection | null` through memory, 80 bytes with the optional's flag, and its caller tests it there. - rc: an `Intersection` holds a `Thing`, counted when it is copied, and the `addLight` closure's diff --git a/tslang/benchmarks/raytrace/README.md b/tslang/benchmarks/raytrace/README.md index c24b2c452..488c0d48b 100644 --- a/tslang/benchmarks/raytrace/README.md +++ b/tslang/benchmarks/raytrace/README.md @@ -180,6 +180,37 @@ field keeps the pair (`test/tester/tests/00rc_local_alias_no_count.ts`). Instructions executed under rc: 711 million to 622 million. `rtbench.ts` under rc is unchanged (119 ms): its counts are on interfaces and closures, which this does not reach. +### `rtbench.ts`: `@final interface Thing`, no call through a pointer left + +`@final` on an interface says that only classes of this module implement it. The compiler refuses +what could bring a vtable it did not make (a cast from an object literal, from another interface's +value or from `any`; an interface that extends it; `export`; an exported or declared class that +implements it), so a call of one of its methods is one of the functions in that method's slot of +the vtables the module made for it. With `--opt_level=3`, when those are at most four, the call +tests the pointer against all but the last and calls the last without a test: the call through +the pointer that the section below keeps "for anything else" is gone. In `rtbench.ts` the things +are tested for the sphere's and the plane's `intersect`, and what is left is the null `Thing`'s, +which throws. + +The same day and machine. Two measurements, 21 rounds each, because the second was noisy (every +build's maximum was 1.4 to 1.6 times its minimum): + +| Build, `rtbench.ts` | before | `@final interface Thing`, quiet run | noisy run (`bench.sh`): min, median | +|---|---|---|---| +| tslang gc | 75.6 ms | 70.7 ms | 69.9, 74.9 ms | +| tslang none | 75.4 ms | 70.8 ms | 69.6, 72.0 ms | +| tslang rc | 111.6 ms | 105.5 ms | 98.5, 108.0 ms | +| tslang own | | | 73.9, 88.8 ms | +| C++ idiomatic (clang) | | | 67.9, 73.3 ms | +| Rust idiomatic | | | 110.0, 117.4 ms | + +- gc and none are level with the C++ port within this machine's noise; rc is still about 1.5x of + it (the counts on `Intersection` copies and closure captures, below). own was not in the quiet + run, and its median in the noisy one says little. +- `Surface` and `Scene` are made from object literals in this program, so they cannot be `@final`. +- More than four implementers: nothing is done for the call, not even the tests of the section + below, which has the same limit. Not measured on any other program; nothing run on Windows. + ### `rtbench.ts`, with interfaces: direct calls tried first, and a null that writes less The same day and machine, `bench.sh`, 21 rounds. Two changes for the version with interfaces and diff --git a/tslang/benchmarks/raytrace/rtbench.ts b/tslang/benchmarks/raytrace/rtbench.ts index 5876c87e6..e7cec0487 100644 --- a/tslang/benchmarks/raytrace/rtbench.ts +++ b/tslang/benchmarks/raytrace/rtbench.ts @@ -115,6 +115,9 @@ interface Surface { roughness: number; } +// @final: only classes of this file implement Thing, so `thing.intersect(ray)` is the sphere's or +// the plane's method (or null's), called directly, and no call through a pointer is left +@final interface Thing { intersect(ray: Ray): Intersection | null; normal(pos: Vector): Vector; diff --git a/tslang/benchmarks/raytrace/rtbench_own.ts b/tslang/benchmarks/raytrace/rtbench_own.ts index 7bae0f043..99151e9fb 100644 --- a/tslang/benchmarks/raytrace/rtbench_own.ts +++ b/tslang/benchmarks/raytrace/rtbench_own.ts @@ -116,6 +116,9 @@ interface Surface { roughness: number; } +// @final: only classes of this file implement Thing, so `thing.intersect(ray)` is the sphere's or +// the plane's method (or null's), called directly, and no call through a pointer is left +@final interface Thing { intersect(ray: Ray): Intersection | null; normal(pos: Vector): Vector; diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 973721945..e73aae994 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -212,6 +212,12 @@ // on the global of an interface's null vtable (mlirGenNullInterfaceVTable): a null interface is seen // through it, and nothing implements the interface by it #define NULL_INTERFACE_VTABLE_ATTR_NAME "__null_interface_vtable" +// `@final interface I`: on a vtable made for I and on a read of one of I's methods, the name of I. +// Every vtable an I can have is a global of this module that carries it. +#define FINAL_INTERFACE_ATTR_NAME "__final_interface" +// in LLVM IR: the element type of the GEP of such a read is the struct `ts.final:`, and each +// of I's vtables has a global `ts.final::` holding its address (SpeculativeDirectCallPass) +#define FINAL_INTERFACE_IR_PREFIX "ts.final:" // on the C++ runtime's type_info vtable a `throw`'s exception tables name (`??_7type_info@@6B@`, // `_ZTVN10__cxxabiv1...`): a foreign global, but no program value is ever read out of it or stored into it #define EH_TYPE_INFO_ATTR_NAME "__eh_type_info" diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h index d77e89294..9cbd4d0a9 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h @@ -329,7 +329,11 @@ struct InterfaceInfo // MLIRGenImpl::getValueTypeOfInterfaceIfLaidOut) bool membersResolved; - InterfaceInfo() : hasNew(false), isValue(false), membersResolved(false) + // `@final`: only classes of this module implement it, so every vtable its values can have is + // known when the module is compiled (see FINAL_INTERFACE_ATTR_NAME) + bool isFinal; + + InterfaceInfo() : hasNew(false), isValue(false), membersResolved(false), isFinal(false) { } diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index d3f1b9278..205a82d7e 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -7286,8 +7286,28 @@ struct InterfaceSymbolRefOpLowering : public TsLlvmPattern( - loc, th.getPtrType(), vtable, interfaceSymbolRefOp.getIndex() + INTERFACE_VTABLE_HEADER_SLOTS); + mlir::Value methodOrFieldPtr; + if (auto finalInterface = interfaceSymbolRefOp->getAttrOfType(FINAL_INTERFACE_ATTR_NAME)) + { + // a method of a @final interface: the same read of the slot, the vtable indexed as an + // array of a struct named for the interface, which tells SpeculativeDirectCallPass that + // the vtable is one of that interface's (FINAL_INTERFACE_IR_PREFIX) + auto slotType = LLVM::LLVMStructType::getIdentified( + rewriter.getContext(), (llvm::Twine(FINAL_INTERFACE_IR_PREFIX) + finalInterface.getValue()).str()); + if (!slotType.isInitialized()) + { + (void)slotType.setBody({th.getPtrType()}, false); + } + + auto index = clh.createI32ConstantOf(interfaceSymbolRefOp.getIndex() + INTERFACE_VTABLE_HEADER_SLOTS); + auto slotPtr = rewriter.create(loc, th.getPtrType(), slotType, vtable, ValueRange{index}); + methodOrFieldPtr = rewriter.create(loc, th.getPtrType(), slotPtr); + } + else + { + methodOrFieldPtr = rewriter.create( + loc, th.getPtrType(), vtable, interfaceSymbolRefOp.getIndex() + INTERFACE_VTABLE_HEADER_SLOTS); + } // a write of a function-typed field (`h.f = g`, not optional): the class field a tagged slot's cell // names, or null where nothing can be written - a method's cell (field offset -1), or an untagged @@ -9891,11 +9911,48 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() LLVM_DEBUG(llvm::dbgs() << "\n!! BEFORE DUMP: \n" << m << "\n";); + // the vtables of @final interfaces (FINAL_INTERFACE_ATTR_NAME), noted before their globals are + // replaced + SmallVector> finalInterfaceVTables; + for (auto globalOp : m.getOps()) + { + if (auto finalInterface = globalOp->getAttrOfType(FINAL_INTERFACE_ATTR_NAME)) + { + finalInterfaceVTables.push_back({finalInterface.getValue().str(), globalOp.getSymName().str()}); + } + } + if (failed(applyFullConversion(m, target, std::move(patterns)))) { signalPassFailure(); } + // For each, a global named for the interface and the vtable that holds the vtable's address: + // how SpeculativeDirectCallPass finds every vtable the interface has (FINAL_INTERFACE_IR_PREFIX). + // Nothing else reads it. + for (auto &[interfaceName, vtableName] : finalInterfaceVTables) + { + auto markerName = (llvm::Twine(FINAL_INTERFACE_IR_PREFIX) + interfaceName + ":" + vtableName).str(); + if (m.lookupSymbol(markerName)) + { + continue; + } + + OpBuilder builder(m.getBodyRegion()); + builder.setInsertionPointToEnd(m.getBody()); + auto loc = m.getLoc(); + auto ptrType = LLVM::LLVMPointerType::get(builder.getContext()); + auto marker = builder.create(loc, ptrType, true, LLVM::Linkage::Internal, markerName, mlir::Attribute()); + auto *block = new mlir::Block(); + marker.getInitializerRegion().push_back(block); + builder.setInsertionPointToStart(block); + // null, should the vtable not be a global: the pass then knows it cannot read every vtable + mlir::Value address = m.lookupSymbol(vtableName) + ? mlir::Value(builder.create(loc, ptrType, vtableName)) + : mlir::Value(builder.create(loc, ptrType)); + builder.create(loc, mlir::ValueRange{address}); + } + LLVM_DEBUG(llvm::dbgs() << "\n!! AFTER DUMP - BEFORE CLEANUP: \n" << m << "\n";); cleanupUnrealizedConversionCast(m); diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index a61f9ec63..721e001cf 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -1311,6 +1311,23 @@ namespace mlirgen return V(newInterface); } + // a @final interface is made only from a class object (above) or null: an object + // literal, another interface's value or an `any` would bring a vtable that is not one + // of those the module made for it + if (!genContext.allowPartialResolve + && (isa(valueType) + || (isa(valueType) && valueType != type))) + { + auto interfaceInfo = getInterfaceInfoByFullName(interfaceType.getName().getValue()); + if (interfaceInfo && interfaceInfo->isFinal) + { + emitError(location) << "type '" << to_print(valueType) << "' can't be cast to @" FINAL_DECORATOR + << " interface '" << interfaceInfo->name << "': only a class object can"; + return mlir::failure(); + } + } + // tuple to interface if (auto constTupleType = dyn_cast(valueType)) { diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index 04992feb1..4ae764ee4 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -3244,6 +3244,16 @@ genContext); MLIRCodeLogic mcl(builder, compileOptions); + // a @final interface's vtables are all in this module: a class another module has, or can + // extend, would bring one of its own + if (newInterfacePtr->isFinal && + (newClassPtr->isExport || newClassPtr->isImport || newClassPtr->isDeclaration || newClassPtr->isDynamicImport)) + { + emitError(location) << "class '" << newClassPtr->name << "' cannot implement @" FINAL_DECORATOR " interface '" + << newInterfacePtr->name << "': the class is exported or declared"; + return mlir::failure(); + } + MethodInfo emptyMethod; mlir_ts::FieldInfo emptyFieldInfo; // TODO: ... @@ -4000,6 +4010,14 @@ genContext); }, genContext); + if (registeredType && newInterfacePtr->isFinal) + { + if (auto globalOp = theModule.lookupSymbol(fullClassInterfaceVTableFieldName)) + { + globalOp->setAttr(FINAL_INTERFACE_ATTR_NAME, builder.getStringAttr(newInterfacePtr->fullName)); + } + } + return registeredType ? mlir::success() : mlir::failure(); } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index ebff71989..c6ea806de 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -9892,6 +9892,16 @@ class MLIRGenImpl erasedGenericCallees[interfaceSymbolRefValue.getOperation()] = methodInfo->genericSignature; } + // a @final interface: the method is one of those in the vtables this module made for it + if (auto interfaceType = dyn_cast(interfaceValue.getType())) + { + auto interfaceInfo = getInterfaceInfoByFullName(interfaceType.getName().getValue()); + if (interfaceInfo && interfaceInfo->isFinal) + { + interfaceSymbolRefValue->setAttr(FINAL_INTERFACE_ATTR_NAME, builder.getStringAttr(interfaceInfo->fullName)); + } + } + return interfaceSymbolRefValue; } diff --git a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp index 610a4adcd..496da3317 100644 --- a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp +++ b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp @@ -687,6 +687,10 @@ namespace mlirgen if (auto globalOp = theModule.lookupSymbol(vtableName)) { globalOp->setAttr(NULL_INTERFACE_VTABLE_ATTR_NAME, builder.getUnitAttr()); + if (newInterfacePtr->isFinal) + { + globalOp->setAttr(FINAL_INTERFACE_ATTR_NAME, builder.getStringAttr(newInterfacePtr->fullName)); + } } return resolveFullNameIdentifier(location, vtableName, true, genContext); @@ -1135,6 +1139,11 @@ namespace mlirgen { newInterfacePtr->isValue = true; } + + if (decoratorName == FINAL_DECORATOR) + { + newInterfacePtr->isFinal = true; + } }); } @@ -1204,6 +1213,15 @@ namespace mlirgen baseInterfaceInfo = getInterfaceInfoByFullName(interfaceType.getName().getValue()); } + // a value of the extending interface would be a value of the @final one, with a vtable + // made for the extending one + if (baseInterfaceInfo && baseInterfaceInfo->isFinal) + { + emitError(loc(extendsType)) << "interface '" << newInterfacePtr->name << "' cannot extend '" + << baseInterfaceInfo->name << "': it is @" FINAL_DECORATOR; + return mlir::failure(); + } + // a value has no base layout to widen, and an interface that is not a value is seen // through a vtable: a @value interface extends only another @value interface's fields if (newInterfacePtr->isValue && !(baseInterfaceInfo && baseInterfaceInfo->isValue)) @@ -1396,6 +1414,21 @@ namespace mlirgen newInterfacePtr->membersResolved = true; + // @final says every implementer is in this module: another module can be told nothing by + // a declaration, and a @value interface has no vtable to name an implementer + if (newInterfacePtr->isFinal) + { + StringRef reason = newInterfacePtr->isValue ? "@" VALUE_DECORATOR + : getExportModifier(interfaceDeclarationAST) ? "exported" + : declarationMode || hasModifier(interfaceDeclarationAST, SyntaxKind::DeclareKeyword) ? "a declaration" + : ""; + if (!reason.empty()) + { + emitError(location) << "@" FINAL_DECORATOR " interface '" << newInterfacePtr->name << "' cannot be " << reason; + return mlir::failure(); + } + } + // add to export if any if (auto hasExport = getExportModifier(interfaceDeclarationAST)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 4061dece5..7fb8072be 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -624,6 +624,7 @@ tslang_add_test(NAME test-compile-own-00-object-literal-contextual-lambda COMMAN tslang_add_test(NAME test-compile-00-interface-field-getter-owned COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-compile-00-rc-interface-count-inline COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_interface_count_inline.ts") tslang_add_test(NAME test-compile-00-rc-local-alias-no-count COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_local_alias_no_count.ts") +tslang_add_test(NAME test-compile-00-final-interface COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00final_interface.ts") tslang_add_test(NAME test-compile-00-conditional-record-literal-temporary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_record_literal_temporary.ts") tslang_add_test(NAME test-compile-00-conditional-nested-record-literal-temporary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_nested_record_literal_temporary.ts") tslang_add_test(NAME test-compile-00-generic-callback-closure-late-class COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_callback_closure_late_class.ts") @@ -1301,6 +1302,7 @@ tslang_add_test(NAME test-jit-own-00-object-literal-contextual-lambda COMMAND te tslang_add_test(NAME test-jit-00-interface-field-getter-owned COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-jit-00-rc-interface-count-inline COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_interface_count_inline.ts") tslang_add_test(NAME test-jit-00-rc-local-alias-no-count COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_local_alias_no_count.ts") +tslang_add_test(NAME test-jit-00-final-interface COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00final_interface.ts") tslang_add_test(NAME test-jit-00-conditional-record-literal-temporary COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_record_literal_temporary.ts") tslang_add_test(NAME test-jit-00-conditional-nested-record-literal-temporary COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_nested_record_literal_temporary.ts") tslang_add_test(NAME test-jit-00-generic-callback-closure-late-class COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_callback_closure_late_class.ts") @@ -1810,6 +1812,19 @@ add_test(NAME test-compile-cpp-class-value-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/cpp_class_value_error.ts") set_tests_properties(test-compile-cpp-class-value-error PROPERTIES PASS_REGULAR_EXPRESSION "the @cpp class 'Native' can't be used as a value: its objects are made by C\\+\\+" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +# `@final interface`: what could bring a vtable the module did not make is refused +add_test(NAME test-compile-final-interface-object-error + COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/final_interface_object_error.ts") +set_tests_properties(test-compile-final-interface-object-error PROPERTIES PASS_REGULAR_EXPRESSION "can't be cast to @final interface 'Shape': only a class object can" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +add_test(NAME test-compile-final-interface-extends-error + COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/final_interface_extends_error.ts") +set_tests_properties(test-compile-final-interface-extends-error PROPERTIES PASS_REGULAR_EXPRESSION "interface 'Solid' cannot extend 'Shape': it is @final" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +add_test(NAME test-compile-final-interface-export-error + COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/final_interface_export_error.ts") +set_tests_properties(test-compile-final-interface-export-error PROPERTIES PASS_REGULAR_EXPRESSION "@final interface 'Shape' cannot be exported" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") # ...a class whose constructor takes other arguments than its base's is no value of the base's type add_test(NAME test-compile-class-value-constructor-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/class_value_constructor_error.ts") @@ -2483,6 +2498,7 @@ set(TSLANG_CORPUS 00interface_field_getter_owned.ts 00rc_interface_count_inline.ts 00rc_local_alias_no_count.ts + 00final_interface.ts 00conditional_record_literal_temporary.ts 00conditional_nested_record_literal_temporary.ts 00generic_callback_closure_late_class.ts diff --git a/tslang/test/tester/tests/00final_interface.ts b/tslang/test/tester/tests/00final_interface.ts new file mode 100644 index 000000000..588ef85f4 --- /dev/null +++ b/tslang/test/tester/tests/00final_interface.ts @@ -0,0 +1,87 @@ +// @strict-null false +// `@final interface`: only classes of this module implement it, so a call of one of its methods is +// one of the functions in the vtables the module made for it. With --opt_level=3 the call tests the +// pointer against all but one of them and calls the last without a test (SpeculativeDirectCallPass): +// every implementer has to be among them - a class that declares the interface, a class derived +// from one (its override through its own vtable), a class cast to it without declaring it, and +// null, whose methods throw. +@final +interface Shape { + area(): number; + scaled(k: number): number; + name: string; +} + +class Square implements Shape { + name = "square"; + constructor(public side: number) {} + area(): number { return this.side * this.side; } + scaled(k: number): number { return this.area() * k; } +} + +class Disc implements Shape { + name = "disc"; + constructor(public r: number) {} + area(): number { return 3 * this.r * this.r; } + scaled(k: number): number { return this.area() * k; } +} + +// derived from an implementer: its own area +class Frame extends Square { + constructor(side: number, public hole: number) { super(side); this.name = "frame"; } + area(): number { return this.side * this.side - this.hole * this.hole; } +} + +// has the members and does not declare the interface +class Strip { + name = "strip"; + constructor(public length: number) {} + area(): number { return this.length; } + scaled(k: number): number { return this.length * k; } +} + +@noinline function make(kind: number): Shape { + if (kind == 0) return new Square(2); + if (kind == 1) return new Disc(1); + if (kind == 2) return new Frame(3, 1); + if (kind == 3) return new Strip(5); + return null; +} + +@noinline function total(shapes: Shape[]): number { + let sum = 0.0; + for (const shape of shapes) { + sum += shape.area() + shape.scaled(10); + } + + return sum; +} + +// what a method does: -1 when it throws +@noinline function areaOf(shape: Shape): number { + try { + return shape.area(); + } catch (e) { + return -1; + } +} + +function main() { + const shapes: Shape[] = []; + for (let kind = 0; kind < 4; kind++) { + shapes.push(make(kind)); + } + + // 4 + 3 + 8 + 5 = 20, and ten times that + assert(total(shapes) == 220, "sum over every implementer"); + + assert(areaOf(make(0)) == 4, "a class that declares it"); + assert(areaOf(make(1)) == 3, "another"); + assert(areaOf(make(2)) == 8, "a derived class's override"); + assert(areaOf(make(3)) == 5, "a class that does not declare it"); + assert(areaOf(make(4)) == -1, "null throws"); + + assert(make(2).name == "frame", "a field"); + + print("done."); +} diff --git a/tslang/test/tester/tests/final_interface_export_error.ts b/tslang/test/tester/tests/final_interface_export_error.ts new file mode 100644 index 000000000..8c23d8645 --- /dev/null +++ b/tslang/test/tester/tests/final_interface_export_error.ts @@ -0,0 +1,9 @@ +// A @final interface is not exported: another module could implement it. +@final +export interface Shape { + area(): number; +} + +function main() { + print("done."); +} diff --git a/tslang/test/tester/tests/final_interface_extends_error.ts b/tslang/test/tester/tests/final_interface_extends_error.ts new file mode 100644 index 000000000..c24cb97a7 --- /dev/null +++ b/tslang/test/tester/tests/final_interface_extends_error.ts @@ -0,0 +1,13 @@ +// A @final interface is not extended: a value of the extending one would be a value of it. +@final +interface Shape { + area(): number; +} + +interface Solid extends Shape { + volume(): number; +} + +function main() { + print("done."); +} diff --git a/tslang/test/tester/tests/final_interface_object_error.ts b/tslang/test/tester/tests/final_interface_object_error.ts new file mode 100644 index 000000000..519a59982 --- /dev/null +++ b/tslang/test/tester/tests/final_interface_object_error.ts @@ -0,0 +1,11 @@ +// An object literal is no implementer of a @final interface: it would bring a vtable of its own. +@final +interface Shape { + area(): number; +} + +function main() { + const shape: Shape = { area() { return 1; } }; + print(shape.area()); + print("done."); +} diff --git a/tslang/test/tester/verify-ownership.cmake b/tslang/test/tester/verify-ownership.cmake index 09ea8109c..c93991caa 100644 --- a/tslang/test/tester/verify-ownership.cmake +++ b/tslang/test/tester/verify-ownership.cmake @@ -54,6 +54,9 @@ set(not_compilable_alone class_local_capture_error.ts class_field_capture_error.ts cpp_class_value_error.ts + final_interface_export_error.ts + final_interface_extends_error.ts + final_interface_object_error.ts # an error by design (test-compile-mixin-runtime-base-error) mixin_runtime_base_error.ts # an error by design (test-compile-ref-element-layout-error) diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index a56a24a49..5fc2a8fbc 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -877,10 +877,93 @@ struct RcPairEliminationPass : llvm::PassInfoMixin // The direct calls are what the inliner and every pass after it can see through; a ray tracer's // `thing.intersect(ray)` becomes "a sphere? a plane?" with both bodies in the loop, as a `switch` // over the kind of thing would be. Nothing is assumed about who else may supply a pointer. +// +// A method of a `@final` interface is the exception: the compiler has refused everything but a +// class of this module as its implementer, and marked the read of the method and every vtable +// made for the interface (FINAL_INTERFACE_IR_PREFIX). There the functions tried are exactly those +// in the method's slot of those vtables, and the last of them is called without a test: no call +// through the pointer is left. struct SpeculativeDirectCallPass : llvm::PassInfoMixin { static constexpr unsigned MaxTargets = 4; + // the vtables of each @final interface, from the globals that name them + static llvm::StringMap> finalInterfaceVTables(llvm::Module &m) + { + llvm::StringMap> vtablesOf; + for (auto &global : m.globals()) + { + auto name = global.getName(); + if (!name.consume_front(FINAL_INTERFACE_IR_PREFIX)) + { + continue; + } + + auto interfaceName = name.split(':').first; + auto *vtable = global.hasInitializer() + ? llvm::dyn_cast(global.getInitializer()->stripPointerCasts()) + : nullptr; + // a null entry: the interface has a vtable that cannot be read here + vtablesOf[interfaceName].push_back(vtable); + } + + return vtablesOf; + } + + // The functions a call of a @final interface's method can reach, when `callee` is such a read + // and every vtable of the interface holds a function of the call's type in the slot. + static bool finalInterfaceTargets(llvm::CallBase &call, + const llvm::StringMap> &vtablesOf, + llvm::SmallVectorImpl &targets) + { + // the method is called out of the {method, this} pair it was bound into + auto *callee = call.getCalledOperand(); + if (auto *extract = llvm::dyn_cast(callee)) + { + callee = llvm::FindInsertedValue(extract->getAggregateOperand(), extract->getIndices()); + } + + auto *load = llvm::dyn_cast_or_null(callee); + auto *gep = load ? llvm::dyn_cast(load->getPointerOperand()) : nullptr; + auto *slotType = gep ? llvm::dyn_cast(gep->getSourceElementType()) : nullptr; + if (!slotType || !slotType->hasName() || !slotType->getName().starts_with(FINAL_INTERFACE_IR_PREFIX) + || gep->getNumIndices() != 1) + { + return false; + } + + auto *slot = llvm::dyn_cast(gep->getOperand(1)); + auto found = vtablesOf.find(slotType->getName().drop_front(llvm::StringRef(FINAL_INTERFACE_IR_PREFIX).size())); + if (!slot || found == vtablesOf.end()) + { + return false; + } + + for (auto *vtable : found->second) + { + // a vtable filled when the program starts has no initializer to read + if (!vtable || !vtable->hasDefinitiveInitializer()) + { + return false; + } + + auto *element = vtable->getInitializer()->getAggregateElement((unsigned)slot->getZExtValue()); + auto *target = element ? llvm::dyn_cast(element->stripPointerCasts()) : nullptr; + if (!target || target->getFunctionType() != call.getFunctionType() + || target->getCallingConv() != call.getCallingConv() || !llvm::isLegalToPromote(call, target)) + { + return false; + } + + if (!llvm::is_contained(targets, target)) + { + targets.push_back(target); + } + } + + return !targets.empty() && targets.size() <= MaxTargets; + } + llvm::PreservedAnalyses run(llvm::Module &m, llvm::ModuleAnalysisManager &) { llvm::DenseMap> targetsOf; @@ -928,9 +1011,31 @@ struct SpeculativeDirectCallPass : llvm::PassInfoMixin finalTargets; + if (!vtablesOf.empty() && finalInterfaceTargets(*call, vtablesOf, finalTargets)) + { + // the larger function first, as below; the last one, the least likely, is what is + // left when the pointer is none of the others + llvm::stable_sort(finalTargets, [](llvm::Function *a, llvm::Function *b) { + return a->getInstructionCount() > b->getInstructionCount(); + }); + + llvm::CallBase *indirect = call; + for (auto *target : llvm::ArrayRef(finalTargets).drop_back()) + { + llvm::promoteCallWithIfThenElse(*indirect, target); + } + + llvm::promoteCall(*indirect, finalTargets.back()); + changed = true; + continue; + } + auto found = targetsOf.find(call->getFunctionType()); if (found == targetsOf.end() || found->second.size() > MaxTargets) {