Numeric return-type hints: ^double/^long on a defn (round 3)

A ^double/^long return hint on a fn's name now (a) coerces the fn's value on the
way out — exact->inexact / jolt->fx, like a JVM primitive return — and (b) types a
call to it, so an accumulator over the result specializes:

  (defn ^double work [^double x ^double y] (+ (* x x) (* y y)))
  (loop [acc 0.0] (recur (+ acc (work a b))))   ; (+ acc (work ..)) -> fl+

The analyzer pushes the name's numeric tag onto each arity (:ret-nhint) for the
back-end coercion, and resolve-global surfaces the callee's declared return
(:num-ret, read from var meta) onto the :var node so jolt.passes.numeric types the
call. defn carries the name hint through.

This unblocks the accumulator-over-fn-result pattern that round 2 had to demote.
The win is bounded by call overhead in an open/dispatched build (~1.15x on a hot
loop whose body is a helper call); it compounds with direct-linking and, later,
inlining. A numeric return hint is a contract, like ^long — redefining the var to
return another type in an open build breaks it.

Not yet: per-arity arglist return hints, (defn f (^double [x] ..)). Gate:
test/chez/numeric-test.ss 39/39; full make test green, 0 new corpus divergences.
This commit is contained in:
Yogthos 2026-06-23 17:21:53 -04:00
parent 0db417a491
commit 5a9acd3cf4
6 changed files with 227 additions and 164 deletions

View file

@ -373,8 +373,15 @@
pbind (map (fn [o p] (str "(" p " " (nhint-init nh o p) ")")) orig params)
binds (if restp
(concat pbind [(str "(" restp " (list->cseq " restp "))")])
pbind)]
[paramlist (str "(let " label " (" (str/join " " binds) ") " body ")")]))
pbind)
lett (str "(let " label " (" (str/join " " binds) ") " body ")")
;; a ^double/^long return hint coerces the arity's value on the way out
;; (exact->inexact / jolt->fx), like a JVM primitive return — so a caller's
;; arithmetic over the result is sound.
ret (:ret-nhint a)]
[paramlist (cond (= ret :double) (str "(exact->inexact " lett ")")
(= ret :long) (str "(jolt->fx " lett ")")
:else lett)]))
(defn- emit-fn [node]
(let [arities (:arities node)