jolt/test/integration/inline-sra-test.janet
Dmitri Sotnikov f0293fb4ee
Parallelize the test gate; cache cold-init tests, drop the benchmark from it (#139)
run-tests.janet runs the same file set as `jpm test` across a pool of worker
processes (one `janet FILE` each, ev-based). The full gate goes from ~790s
serial to ~98s here (8x), and more on CI where the heavy files don't thrash on
swap. CI and the docs point at it; `jpm test` still works serially.

Three things dominated the wall:

- Nine integration tests cold-built a compile ctx (~8s each); switch them to
  api/init-cached so they share the prebuilt image. The cache key already
  fingerprints the ctx-shaping env vars, so the direct-link ones share one DL
  image and the rest share the plain one.
- core-bench's main ran on every gate (~35s of benchmark loops that assert
  nothing); gate it behind JOLT_BENCH=1.
- cli-test spawned `janet src/jolt/main.janet` ~20 times at ~8s cold each
  (340s under parallel load, and it was the whole wall); prefer build/jolt
  (~20ms baked ctx) when present, fall back to from-source for an unbuilt tree.

type-check-test stays on cold init: a snapshot-loaded ctx loses the success
checker's op/msg detail (jolt-vley). jolt-pria tracks caching from-source
startup generally, which would let cli-test drop the build/jolt preference.

Co-authored-by: Yogthos <yogthos@gmail.com>
2026-06-16 14:23:02 +00:00

64 lines
3.7 KiB
Text

# Inline + scalar-replacement passes (jolt-87f, Route 1 AOT escape analysis).
# When a unit opts into direct-linking (:inline?, JOLT_DIRECT_LINK=1), the IR
# pipeline inlines small direct-linked fns and then scalar-replaces the now-
# exposed non-escaping map allocations: (:r {:r a ..}) -> a. This pins the
# transform (allocations actually vanish) AND that it stays semantics-preserving.
(import ../../src/jolt/api :as api)
(import ../../src/jolt/backend :as backend)
(import ../../src/jolt/reader :as reader)
(print "Inline + scalar replacement (jolt-87f)...")
# A ctx with inlining ON (independent of the build-time JOLT_DIRECT_LINK).
(def ctx (api/init-cached {:compile? true}))
(put (ctx :env) :direct-linking? true)
(put (ctx :env) :inline? true)
(api/eval-string ctx "(ns rt)")
(each s ["(defn v3 [r g b] {:r r :g g :b b})"
"(defn scale [l n] {:r (* (:r l) n) :g (* (:g l) n) :b (* (:b l) n)})"
"(defn add [l r] {:r (+ (:r l) (:r r)) :g (+ (:g l) (:g r)) :b (+ (:b l) (:b r))})"
"(defn dot [l r] (+ (+ (* (:r l) (:r r)) (* (:g l) (:g r))) (* (:b l) (:b r))))"
"(defn sub [l r] {:r (- (:r l) (:r r)) :g (- (:g l) (:g r)) :b (- (:b l) (:b r))})"
"(defn reflect [v n] (sub v (scale n (* 2.0 (dot v n)))))"
# self-recursive: must NOT be inlined into callers (its body has a free
# local — the fn-name self-reference — that would dangle when spliced).
"(defn countdown [n] (if (< n 1) :done (countdown (- n 1))))"]
(api/eval-string ctx s))
(defn alloc-count [src]
# struct / build-map literal occurrences in the emitted Janet = surviving map
# allocations (jolt builds a struct, falling back to build-map-literal).
(def code (string/format "%p" (backend/emit-ir ctx (backend/analyze-form ctx (reader/parse-string src)))))
[(length (string/find-all "struct " code))
(length (string/find-all "build-map" code))])
# A vec3 chain whose intermediates never escape collapses to ONE result map.
(let [[s b] (alloc-count "(fn [v n] (reflect v n))")]
(assert (= 1 s) (string "reflect keeps exactly one alloc, got " s " struct"))
(assert (= 1 b) (string "reflect keeps exactly one build-map fallback, got " b)))
# A fully consumed chain (result not returned as a map) allocates NOTHING.
(let [[s b] (alloc-count "(fn [v n] (dot (reflect v n) (reflect v n)))")]
(assert (= 0 s) (string "fully-consumed chain allocates no struct, got " s))
(assert (= 0 b) (string "fully-consumed chain has no build-map fallback, got " b)))
# Loop bodies optimize too (recur is not a blanket escape).
(let [[s _] (alloc-count "(fn [k] (loop [i 0 acc 0.0] (if (< i k) (recur (inc i) (+ acc (dot (v3 1.0 2.0 3.0) (v3 0.1 0.2 0.3)))) acc)))")]
(assert (= 0 s) (string "loop body allocates no struct, got " s)))
# Correctness: inlined results match the obvious computation.
(assert (= 32.0 (api/eval-string ctx "(dot (v3 1.0 2.0 3.0) (v3 4.0 5.0 6.0))")) "dot value")
(assert (= 9.0 (api/eval-string ctx "(:r (add (v3 1.0 0.0 0.0) (scale (v3 4.0 0.0 0.0) 2.0)))")) "add+scale value")
# the self-recursive fn still runs (the closed-body guard kept it un-inlined)
(assert (= :done (api/eval-string ctx "(countdown 5)")) "recursive fn still works")
# A redefinable (^:redef) callee must NOT be inlined — it stays a live var call.
(api/eval-string ctx "(defn ^:redef wobble [x] {:v x})")
(let [[s _] (alloc-count "(fn [] (:v (wobble 1)))")]
# wobble is not inlined, so its map isn't visible to scalar replacement: the
# lookup stays a call, and the (:v ...) result is whatever wobble returns.
(assert (= 7 (do (api/eval-string ctx "(defn ^:redef wobble [x] {:v (+ x 6)})")
(api/eval-string ctx "(:v (wobble 1))")))
"redef callee stays live (redefinition is visible)"))
(print "Inline + scalar replacement passed!")