Skip to content

Number unboxing #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features/Changes
* Compiler: exit-loop-early in more cases (#2077)
* Compiler/wasm: omit code pointer from closures when not used (#2059)

# 6.1.1 (2025-07-07) - Lille

Expand Down
63 changes: 63 additions & 0 deletions compiler/lib-wasm/call_graph_analysis.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
open! Stdlib
open Code

let debug = Debug.find "call-graph"

let times = Debug.find "times"

let block_deps ~info ~non_escaping ~ambiguous ~blocks pc =
let block = Addr.Map.find pc blocks in
List.iter block.body ~f:(fun i ->
match i with
| Let (_, Apply { f; _ }) -> (
try
match Var.Tbl.get info.Global_flow.info_approximation f with
| Top -> ()
| Values { known; others } ->
if others || Var.Set.cardinal known > 1
then Var.Set.iter (fun x -> Var.Hashtbl.replace ambiguous x ()) known;
if debug ()
then
Format.eprintf
"CALL others:%b known:%d@."
others
(Var.Set.cardinal known)
with Invalid_argument _ -> ())
| Let (x, Closure _) -> (
match Var.Tbl.get info.Global_flow.info_approximation x with
| Top -> ()
| Values { known; others } ->
if Var.Set.cardinal known = 1 && (not others) && Var.Set.mem x known
then (
let may_escape = Var.ISet.mem info.Global_flow.info_may_escape x in
if debug () then Format.eprintf "CLOSURE may-escape:%b@." may_escape;
if not may_escape then Var.Hashtbl.replace non_escaping x ()))
| Let (_, (Prim _ | Block _ | Constant _ | Field _ | Special _))
| Event _ | Assign _ | Set_field _ | Offset_ref _ | Array_set _ -> ())

type t = { unambiguous_non_escaping : unit Var.Hashtbl.t }

let direct_calls_only info f =
Config.Flag.optcall () && Var.Hashtbl.mem info.unambiguous_non_escaping f

let f p info =
let t = Timer.make () in
let non_escaping = Var.Hashtbl.create 128 in
let ambiguous = Var.Hashtbl.create 128 in
fold_closures
p
(fun _ _ (pc, _) _ () ->
traverse
{ fold = Code.fold_children }
(fun pc () -> block_deps ~info ~non_escaping ~ambiguous ~blocks:p.blocks pc)
pc
p.blocks
())
();
if debug ()
then Format.eprintf "SUMMARY non-escaping:%d" (Var.Hashtbl.length non_escaping);
Var.Hashtbl.iter (fun x () -> Var.Hashtbl.remove non_escaping x) ambiguous;
if debug ()
then Format.eprintf " unambiguous-non-escaping:%d@." (Var.Hashtbl.length non_escaping);
if times () then Format.eprintf " call graph analysis: %a@." Timer.print t;
{ unambiguous_non_escaping = non_escaping }
5 changes: 5 additions & 0 deletions compiler/lib-wasm/call_graph_analysis.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type t

val direct_calls_only : t -> Code.Var.t -> bool

val f : Code.program -> Global_flow.info -> t
9 changes: 1 addition & 8 deletions compiler/lib-wasm/curry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,7 @@ module Make (Target : Target_sig.S) = struct
(fun ~typ closure ->
let* l = expression_list load l in
call ?typ ~cps:true ~arity closure l)
(let* args =
(* We don't need the deadcode sentinal when the tag is 0 *)
Memory.allocate
~tag:0
~deadcode_sentinal:(Code.Var.fresh ())
~load
(List.map ~f:(fun x -> `Var x) (List.tl l))
in
(let* args = Memory.allocate ~tag:0 (expression_list load (List.tl l)) in
let* make_iterator =
register_import ~name:"caml_apply_continuation" (Fun (Type.primitive_type 1))
in
Expand Down
Loading
Loading