From 5f05a99010ab28046b5b4a4aeaa1cd050d2c7578 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Sat, 13 Jun 2026 09:28:16 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20Phase=202=20vector-op=20specialization?= =?UTF-8?q?=20=E2=80=94=20count/nth=20on=20inferred=20vectors=20(jolt-d6u)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The inference now tags a :local it proved to be a vector with :hint :vector, and the back end specializes (count v) -> pv-count (skipping core-count's dispatch chain) and the 3-arg (nth v i default) -> pv-nth. The 2-arg nth is deliberately NOT specialized: pv-nth returns nil out-of-bounds where Clojure nth throws. Sound, conformance 335/335 x3 and full jpm test pass; type-infer-phase2-test pins the specialization and the 2-arg exclusion. --- jolt-core/jolt/passes.clj | 6 ++++- src/jolt/backend.janet | 20 ++++++++++++++++ test/integration/type-infer-phase2-test.janet | 24 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/integration/type-infer-phase2-test.janet diff --git a/jolt-core/jolt/passes.clj b/jolt-core/jolt/passes.clj index 037f3ab..54e8ab3 100644 --- a/jolt-core/jolt/passes.clj +++ b/jolt-core/jolt/passes.clj @@ -817,7 +817,11 @@ [(let [v (get node :val)] (if (or (nil? v) (= false v)) :any :truthy)) node] (= op :local) (let [t (get tenv (get node :name))] - [(if t t :any) (if (struct-safe? t) (assoc node :hint :struct) node)]) + [(if t t :any) + (cond + (struct-safe? t) (assoc node :hint :struct) + (vec-type? t) (assoc node :hint :vector) + :else node)]) (= op :map) (let [res (mapv (fn [pr] (let [kr (infer (nth pr 0) tenv) diff --git a/src/jolt/backend.janet b/src/jolt/backend.janet index 13c33aa..957c547 100644 --- a/src/jolt/backend.janet +++ b/src/jolt/backend.janet @@ -13,6 +13,7 @@ (use ./evaluator) (import ./reader :as r) (import ./phm :as phm) +(import ./pv :as pv) # The IR is portable data; reading its representation is a host-layer concern. # Most nodes are Janet structs (raw-readable), but a node carrying a nil-valued @@ -320,6 +321,16 @@ :host (= "get" (fnode :name)) false)) +# Is fnode a reference to clojure.core/ (or host )? +(defn- core-head? [fnode name] + (case (fnode :op) + :var (and (= "clojure.core" (fnode :ns)) (= name (fnode :name))) + :host (= name (fnode :name)) + false)) + +# Is this IR node a :local the inference proved to be a vector ({:vec ...})? +(defn- vec-hinted? [n] (and (= :local (n :op)) (= :vector (n :hint)))) + # Shared emit for a constant-keyword map lookup — both (:kw m [d]) and # (get m :kw [d]). subj-node is the subject's IR node (carries the type hint), # m-expr its emitted form, k the keyword, d-expr the emitted default or nil. @@ -394,6 +405,15 @@ (emit-kw-lookup (norm-node (in argnodes 0)) (in args 0) ((norm-node (in argnodes 1)) :val) (when (= 3 (length args)) (in args 2))) + # (count v) on an inferred vector -> pv-count, skipping core-count's dispatch + # chain (jolt-d6u, Phase 2). Sound: a {:vec ...}-typed value is a pvec. + (and (core-head? fnode "count") (= 1 (length args)) (vec-hinted? (norm-node (in argnodes 0)))) + (tuple pv/pv-count (in args 0)) + # (nth v i default) on an inferred vector -> pv-nth. Only the 3-ARG form: the + # 2-arg nth ERRORS on out-of-bounds where pv-nth returns nil, so specializing + # it would change semantics; the 3-arg default matches pv-nth exactly. + (and (core-head? fnode "nth") (= 3 (length args)) (vec-hinted? (norm-node (in argnodes 0)))) + (tuple pv/pv-nth (in args 0) (in args 1) (in args 2)) (direct-call? ctx fnode) (tuple (emit ctx fnode) ;args) # Local callee (closure param, let-bound fn, defn self-name): inline the # function check so the overwhelmingly-common function case is a direct diff --git a/test/integration/type-infer-phase2-test.janet b/test/integration/type-infer-phase2-test.janet new file mode 100644 index 0000000..94c8aeb --- /dev/null +++ b/test/integration/type-infer-phase2-test.janet @@ -0,0 +1,24 @@ +# Vector op specialization, Phase 2 (jolt-d6u): a value the inference proved to +# be a vector ({:vec ...}) gets count -> pv-count (skip core-count's dispatch) +# and 3-arg nth -> pv-nth. 2-arg nth is NOT specialized: it errors on +# out-of-bounds where pv-nth returns nil. +(import ../../src/jolt/api :as api) +(import ../../src/jolt/backend :as backend) +(import ../../src/jolt/types :as types) +(import ../../src/jolt/reader :as reader) +(print "Type inference Phase 2 (vector ops)...") +(os/setenv "JOLT_DIRECT_LINK" "1") +(def ctx (api/init {:compile? true})) +(api/eval-string ctx "(ns p2)") +(def reinfer (types/var-get (types/ns-find (types/ctx-find-ns ctx "jolt.passes") "reinfer-def"))) +(defn estr [src ptmap] + (string/format "%p" (backend/emit-ir ctx (reinfer (backend/analyze-form ctx (reader/parse-string src)) ptmap)))) +(assert (string/find "pv-count" (estr "(defn f [v] (count v))" @{"v" {:vec :any}})) "count on vector -> pv-count") +(assert (not (string/find "pv-count" (estr "(defn f [v] (count v))" @{}))) "count on unknown not specialized") +(assert (string/find "pv-nth" (estr "(defn f [v i] (nth v i 0))" @{"v" {:vec :any}})) "3-arg nth on vector -> pv-nth") +(assert (not (string/find "pv-nth" (estr "(defn f [v i] (nth v i))" @{"v" {:vec :any}}))) "2-arg nth NOT specialized") +# correctness +(assert (= 3 (api/eval-string ctx "(count [1 2 3])")) "count value") +(assert (= 2 (api/eval-string ctx "(nth [1 2 3] 1 9)")) "nth 3-arg in-bounds") +(assert (= 9 (api/eval-string ctx "(nth [1 2 3] 5 9)")) "nth 3-arg default") +(print "Type inference Phase 2 passed!")