Skip to content

Commit e0deeff

Browse files
cknittclaude
andcommitted
Inline functions marked @inline(crossModule) across modules
Why: Calls to small stdlib helpers such as `Option.map` stay calls into `Stdlib_Option` unless the whole build enables cross-module optimization. `@inline(crossModule)` marks a closed function whose body the .cmj exports, so other modules inline its calls as they would a local function. Option's forEach, map, flatMap, filter, mapOr, getOr, orElse, isSome and isNone use it. JS modified (Option helper calls): Example: let bign = BigInt.fromFloat(Number.MAX_VALUE)->Option.getOr(0n) Before (js): let bign = Stdlib_Option.getOr(Stdlib_BigInt.fromFloat(Number.MAX_VALUE), 0n); After (js): let opt = Stdlib_BigInt.fromFloat(Number.MAX_VALUE); let bign = opt !== undefined ? Primitive_option.valFromOption(opt) : 0n; Diagnostic modified (@inline payload): Example: @inline(sometimes) let f = x => x Before (diagnostic): illegal payload for attribute @inline. It must be either empty, 'always' or 'never' After (diagnostic): illegal payload for attribute @inline. It must be empty, 'always', 'never' or 'crossModule' An async or directed function, or one capturing local values, is rejected with an error. The build system recompiles dependents when a module's .cmj changes, not only its .cmi, since an inline body can change without its type. A changed .cmj also updates the .cmi's modification time, so the next build sees the change too. Review: Does every implementation change now recompile dependents? Only those whose .cmj bytes change. Review: Why does Lam_closure.free_variables change? Its `Ltrywith` case never bound the exception, so a function whose `catch` reads it counted as capturing a local value and was rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gKsWykY6KH4QSJ9Wrs Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 91d74a0 commit e0deeff

39 files changed

Lines changed: 558 additions & 81 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Allow selected functions to opt into cross-module inlining with `@inline(crossModule)` and inline common `Option` helpers in application code.

‎compiler/core/js_cmj_format.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ let get_result mid_val =
110110
(Const_js_null | Const_js_undefined _ | Const_js_true | Const_js_false))
111111
| None ->
112112
mid_val
113+
| Some (Lfunction f) when f.attr.inline = Cross_module_inline -> mid_val
113114
| Some _ ->
114115
if !((Js_config.current ()).cross_module_inline) then mid_val
115116
else {mid_val with persistent_closed_lambda = None}

‎compiler/core/js_cmj_format.mli‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ val single_na : arity
8181

8282
val from_file : string -> t
8383

84-
(*
85-
Note writing the file if its content is not changed
86-
*)
84+
(* Write the .cmj. Unchanged files are not rewritten when [check_exists] is
85+
true, so Rewatch can use changes to this file to invalidate callers. *)
8786
val to_file : string -> check_exists:bool -> t -> unit
8887

8988
type cmj_load_info = {cmj_table: t}

‎compiler/core/lam_analysis.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ let destruct_pattern (body : Lambda.t) params args =
328328
(** Hints to inlining *)
329329
let ok_to_inline_fun_when_app (m : Lambda.lfunction) (args : Lambda.t list) =
330330
match m.attr.inline with
331-
| Always_inline -> true
331+
| Always_inline | Cross_module_inline -> true
332332
| Never_inline -> false
333333
| Default_inline -> (
334334
match m with

‎compiler/core/lam_closure.ml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t)
117117
iter sink_pos e1;
118118
local_add_list vars;
119119
iter sink_pos e2
120-
| Ltrywith (e1, _, e2) | Ltryfinally (e1, e2) ->
120+
| Ltrywith (e1, exn, e2) ->
121+
iter top e1;
122+
local_add exn;
123+
iter sink_pos e2
124+
| Ltryfinally (e1, e2) ->
121125
iter top e1;
122126
iter sink_pos e2
123127
| Lifthenelse (e1, e2, e3) ->

‎compiler/core/lam_stats_export.ml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
5858
| Const_js_false ))
5959
| None ->
6060
optlam
61+
| Some
62+
(Lfunction
63+
({attr = {inline = Cross_module_inline}; loc} as lfunction) as
64+
lambda) ->
65+
if not (Lambda.lfunction_can_be_inlined lfunction) then
66+
Location.raise_errorf ~loc
67+
"@inline(crossModule) cannot be used on an async or directed \
68+
function"
69+
else if not (Lam_closure.is_closed lambda) then
70+
Location.raise_errorf ~loc
71+
"@inline(crossModule) requires a function without captured local \
72+
values"
73+
else optlam
6174
| Some lambda ->
6275
if not !((Js_config.current ()).cross_module_inline) then None
6376
else if

‎compiler/ml/lambda.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ type inline_attribute =
335335
| Always_inline (* [@inline] or [@inline always] *)
336336
| Never_inline (* [@inline never] *)
337337
| Default_inline (* no [@inline] attribute *)
338+
| Cross_module_inline (* [@inline(crossModule)] *)
338339

339340
type let_kind = Strict | Alias | StrictOpt | Variable
340341

‎compiler/ml/lambda.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ type inline_attribute =
336336
| Always_inline (* [@inline] or [@inline always] *)
337337
| Never_inline (* [@inline never] *)
338338
| Default_inline (* no [@inline] attribute *)
339+
| Cross_module_inline (* [@inline(crossModule)] *)
339340

340341
type let_kind = Strict | Alias | StrictOpt | Variable
341342
(* Meaning of kinds for let x = e in e':

‎compiler/ml/printlambda.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ let function_attribute ppf {inline; is_a_functor; return_unit} =
243243
match inline with
244244
| Default_inline -> ()
245245
| Always_inline -> fprintf ppf "always_inline@ "
246+
| Cross_module_inline -> fprintf ppf "cross_module_inline@ "
246247
| Never_inline -> fprintf ppf "never_inline@ "
247248

248249
let rec lam ppf = function

‎compiler/ml/translattribute.ml‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ let parse_inline_attribute (attr : t option) : Lambda.inline_attribute =
3434
| Some ({txt; loc}, payload) -> (
3535
let open Parsetree in
3636
(* the 'inline' attribute can be used as
37-
[@inline], [@inline never] or [@inline always].
37+
[@inline], [@inline never], [@inline always] or
38+
[@inline(crossModule)].
3839
[@inline] is equivalent to [@inline always] *)
3940
let warning txt =
4041
Warnings.Attribute_payload
41-
(txt, "It must be either empty, 'always' or 'never'")
42+
(txt, "It must be empty, 'always', 'never' or 'crossModule'")
4243
in
4344
match payload with
4445
| PStr [] -> Always_inline
4546
| PStr [{pstr_desc = Pstr_eval (expression, [])}] -> (
4647
match (Ast_payload.unwrap_braces expression).pexp_desc with
4748
| Pexp_ident {txt = Longident.Lident "never"} -> Never_inline
4849
| Pexp_ident {txt = Longident.Lident "always"} -> Always_inline
50+
| Pexp_ident {txt = Longident.Lident "crossModule"} -> Cross_module_inline
4951
| _ ->
5052
Location.prerr_warning loc (warning txt);
5153
Default_inline)
@@ -62,14 +64,11 @@ let add_inline_attribute (expr : Lambda.t) loc attributes =
6264
| Lfunction ({attr} as funct), inline ->
6365
(match attr.inline with
6466
| Default_inline -> ()
65-
| Always_inline | Never_inline ->
67+
| Always_inline | Cross_module_inline | Never_inline ->
6668
Location.prerr_warning loc (Warnings.Duplicated_attribute "inline"));
6769
let attr = {attr with inline} in
6870
Lambda.function_ ~loc:funct.loc ~attr ~params:funct.params ~body:funct.body
69-
| expr, Always_inline ->
70-
Location.prerr_warning loc (Warnings.Misplaced_attribute "inline");
71-
expr
72-
| expr, Never_inline ->
71+
| expr, (Always_inline | Cross_module_inline | Never_inline) ->
7372
Location.prerr_warning loc (Warnings.Misplaced_attribute "inline");
7473
expr
7574

0 commit comments

Comments
 (0)