Skip to content
Open
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/cross-module-inline-option.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Allow selected functions to opt into cross-module inlining with `@inline(crossModule)` and inline common `Option` helpers in application code.
1 change: 1 addition & 0 deletions compiler/core/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ let get_result mid_val =
(Const_js_null | Const_js_undefined _ | Const_js_true | Const_js_false))
| None ->
mid_val
| Some (Lfunction f) when f.attr.inline = Cross_module_inline -> mid_val
| Some _ ->
if !((Js_config.current ()).cross_module_inline) then mid_val
else {mid_val with persistent_closed_lambda = None}
Expand Down
5 changes: 2 additions & 3 deletions compiler/core/js_cmj_format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ val single_na : arity

val from_file : string -> t

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

type cmj_load_info = {cmj_table: t}
2 changes: 1 addition & 1 deletion compiler/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ let destruct_pattern (body : Lambda.t) params args =
(** Hints to inlining *)
let ok_to_inline_fun_when_app (m : Lambda.lfunction) (args : Lambda.t list) =
match m.attr.inline with
| Always_inline -> true
| Always_inline | Cross_module_inline -> true
| Never_inline -> false
| Default_inline -> (
match m with
Expand Down
6 changes: 5 additions & 1 deletion compiler/core/lam_closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t)
iter sink_pos e1;
local_add_list vars;
iter sink_pos e2
| Ltrywith (e1, _, e2) | Ltryfinally (e1, e2) ->
| Ltrywith (e1, exn, e2) ->
iter top e1;
local_add exn;
iter sink_pos e2
| Ltryfinally (e1, e2) ->
iter top e1;
iter sink_pos e2
| Lifthenelse (e1, e2, e3) ->
Expand Down
13 changes: 13 additions & 0 deletions compiler/core/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
| Const_js_false ))
| None ->
optlam
| Some
(Lfunction
({attr = {inline = Cross_module_inline}; loc} as lfunction) as
lambda) ->
if not (Lambda.lfunction_can_be_inlined lfunction) then
Location.raise_errorf ~loc
"@inline(crossModule) cannot be used on an async or directed \
function"
else if not (Lam_closure.is_closed lambda) then
Location.raise_errorf ~loc
"@inline(crossModule) requires a function without captured local \
values"
Comment thread
cknitt marked this conversation as resolved.
Resolved
else optlam
| Some lambda ->
if not !((Js_config.current ()).cross_module_inline) then None
else if
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ type inline_attribute =
| Always_inline (* [@inline] or [@inline always] *)
| Never_inline (* [@inline never] *)
| Default_inline (* no [@inline] attribute *)
| Cross_module_inline (* [@inline(crossModule)] *)

type let_kind = Strict | Alias | StrictOpt | Variable

Expand Down
1 change: 1 addition & 0 deletions compiler/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ type inline_attribute =
| Always_inline (* [@inline] or [@inline always] *)
| Never_inline (* [@inline never] *)
| Default_inline (* no [@inline] attribute *)
| Cross_module_inline (* [@inline(crossModule)] *)

type let_kind = Strict | Alias | StrictOpt | Variable
(* Meaning of kinds for let x = e in e':
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ let function_attribute ppf {inline; is_a_functor; return_unit} =
match inline with
| Default_inline -> ()
| Always_inline -> fprintf ppf "always_inline@ "
| Cross_module_inline -> fprintf ppf "cross_module_inline@ "
| Never_inline -> fprintf ppf "never_inline@ "

let rec lam ppf = function
Expand Down
13 changes: 6 additions & 7 deletions compiler/ml/translattribute.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ let parse_inline_attribute (attr : t option) : Lambda.inline_attribute =
| Some ({txt; loc}, payload) -> (
let open Parsetree in
(* the 'inline' attribute can be used as
[@inline], [@inline never] or [@inline always].
[@inline], [@inline never], [@inline always] or
[@inline(crossModule)].
[@inline] is equivalent to [@inline always] *)
let warning txt =
Warnings.Attribute_payload
(txt, "It must be either empty, 'always' or 'never'")
(txt, "It must be empty, 'always', 'never' or 'crossModule'")
in
match payload with
| PStr [] -> Always_inline
| PStr [{pstr_desc = Pstr_eval (expression, [])}] -> (
match (Ast_payload.unwrap_braces expression).pexp_desc with
| Pexp_ident {txt = Longident.Lident "never"} -> Never_inline
| Pexp_ident {txt = Longident.Lident "always"} -> Always_inline
| Pexp_ident {txt = Longident.Lident "crossModule"} -> Cross_module_inline
| _ ->
Location.prerr_warning loc (warning txt);
Default_inline)
Expand All @@ -62,14 +64,11 @@ let add_inline_attribute (expr : Lambda.t) loc attributes =
| Lfunction ({attr} as funct), inline ->
(match attr.inline with
| Default_inline -> ()
| Always_inline | Never_inline ->
| Always_inline | Cross_module_inline | Never_inline ->
Location.prerr_warning loc (Warnings.Duplicated_attribute "inline"));
let attr = {attr with inline} in
Lambda.function_ ~loc:funct.loc ~attr ~params:funct.params ~body:funct.body
| expr, Always_inline ->
Location.prerr_warning loc (Warnings.Misplaced_attribute "inline");
expr
| expr, Never_inline ->
| expr, (Always_inline | Cross_module_inline | Never_inline) ->
Location.prerr_warning loc (Warnings.Misplaced_attribute "inline");
expr

Expand Down
9 changes: 9 additions & 0 deletions packages/@rescript/runtime/Stdlib_Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/
type t<'a> = option<'a> = None | Some('a)

@inline(crossModule)
let filter = (opt, p) =>
switch opt {
| Some(x) as option if p(x) => option
| _ => None
}

@inline(crossModule)
let forEach = (opt, f) =>
switch opt {
| Some(x) => f(x)
Expand All @@ -31,42 +33,49 @@ let getOrThrow = (x, ~message=?) =>

external getUnsafe: option<'a> => 'a = "%identity"

@inline(crossModule)
let mapOr = (opt, default, f) =>
switch opt {
| Some(x) => f(x)
| None => default
}

@inline(crossModule)
let map = (opt, f) =>
switch opt {
| Some(x) => Some(f(x))
| None => None
}

@inline(crossModule)
let flatMap = (opt, f) =>
switch opt {
| Some(x) => f(x)
| None => None
}

@inline(crossModule)
let getOr = (opt, default) =>
switch opt {
| Some(x) => x
| None => default
}

@inline(crossModule)
let orElse = (opt, other) =>
switch opt {
| Some(_) as some => some
| None => other
}

@inline(crossModule)
let isSome = x =>
switch x {
| Some(_) => true
| None => false
}

@inline(crossModule)
let isNone = x => x == None

let equal = (a, b, eq) =>
Expand Down
7 changes: 4 additions & 3 deletions rewatch-ocaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ there directly; dependencies are on its include path as their

- Freshness is timestamp-based: an AST newer than its source is current, a
`.cmt` records the last compile, and a `.cmi` the last change of an
interface.
- ASTs, interfaces and typed trees are held in memory until their request
finished (`Build_file_store.set_held_writes`), so a failed request leaves
interface. A `.cmj` counts like the `.cmi`, since dependents also inline
what it exports, which can change without the interface.
- ASTs, interfaces, `.cmj` files and typed trees are held in memory until
their request finished (`Build_file_store.set_held_writes`), so a failed request leaves
the previous AST and interface on disk. An interface is written only when
it changed, which keeps the modification time that dependents compare
with, and before the typed tree that marks the module compiled, so a build
Expand Down
3 changes: 1 addition & 2 deletions rewatch-ocaml/build_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ let record_published_cmi ?dirty_propagation state ~compile_assets module_ ~path
mark_dependents_compile_dirty ?visited:dirty_propagation state module_);
Compile_assets.refresh_cmi compile_assets ~key:module_.key ~path;
module_.last_compiled_cmi <-
Compile_assets.cmi compile_assets module_.key
|> Option.map (fun entry -> entry.Compile_assets.modified)
Compile_assets.published compile_assets module_.key

let record_successful_compile ~compile_assets module_ ~cmt_path =
Compile_assets.refresh_cmt compile_assets ~key:module_.key ~path:cmt_path;
Expand Down
1 change: 1 addition & 0 deletions rewatch-ocaml/build_state.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type module_ = {
mutable dependents: String_set.t;
mutable compile_dirty: bool;
mutable last_compiled_cmi: float option;
(** See {!Compile_assets.published}. *)
mutable last_compiled_cmt: float option;
}

Expand Down
15 changes: 13 additions & 2 deletions rewatch-ocaml/compile_assets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type t = {
ast_by_source: (string, entry) Hashtbl.t;
cmi_by_module: (string, entry) Hashtbl.t;
cmt_by_module: (string, entry) Hashtbl.t;
cmj_by_module: (string, entry) Hashtbl.t;
}

let ast_header path =
Expand All @@ -24,7 +25,7 @@ let is_managed_basename basename =
|| String.starts_with ~prefix:File_util.atomic_write_prefix basename

let state_extension = function
| ".ast" | ".iast" | ".cmi" | ".cmt" -> true
| ".ast" | ".iast" | ".cmi" | ".cmj" | ".cmt" -> true
| _ -> false

let read_directory directory =
Expand Down Expand Up @@ -66,6 +67,7 @@ let add_module_artifact state (entry, name) =
match Filename.extension name with
| ".cmi" -> Hashtbl.replace state.cmi_by_module (module_key name) entry
| ".cmt" -> Hashtbl.replace state.cmt_by_module (module_key name) entry
| ".cmj" -> Hashtbl.replace state.cmj_by_module (module_key name) entry
| _ -> ()

let create directories =
Expand All @@ -77,6 +79,7 @@ let create directories =
ast_by_source = Hashtbl.create 64;
cmi_by_module = Hashtbl.create 64;
cmt_by_module = Hashtbl.create 64;
cmj_by_module = Hashtbl.create 64;
}
in
let directories =
Expand Down Expand Up @@ -146,14 +149,22 @@ let ast state source = Hashtbl.find_opt state.ast_by_source (source_key source)
let cmi state key = Hashtbl.find_opt state.cmi_by_module key
let cmt state key = Hashtbl.find_opt state.cmt_by_module key

let published state key =
match (cmi state key, Hashtbl.find_opt state.cmj_by_module key) with
| None, _ -> None
| Some cmi, None -> Some cmi.modified
| Some cmi, Some cmj -> Some (Float.max cmi.modified cmj.modified)

let replace_from_path table key path =
try
Hashtbl.replace table key {path; modified = (Unix.stat path).Unix.st_mtime}
with Unix.Unix_error ((Unix.ENOENT | Unix.ENOTDIR), _, _) ->
Hashtbl.remove table key

let refresh_cmi state ~key ~path =
replace_from_path state.cmi_by_module key path
replace_from_path state.cmi_by_module key path;
replace_from_path state.cmj_by_module key
(Filename.remove_extension path ^ ".cmj")

let refresh_cmt state ~key ~path =
replace_from_path state.cmt_by_module key path
Expand Down
8 changes: 8 additions & 0 deletions rewatch-ocaml/compile_assets.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ val ast_dependencies : t -> string -> string list
val ast : t -> string -> entry option
val cmi : t -> string -> entry option
val cmt : t -> string -> entry option

val published : t -> string -> float option
(** When the module's CMI, or its CMJ if newer, was last written; [None] without
a CMI. A CMJ can change without its CMI, for example when an exported inline
body changes, and dependents compiled before it are stale too. *)

val refresh_cmi : t -> key:string -> path:string -> unit
(** Also refreshes the CMJ next to [path]. *)

val refresh_cmt : t -> key:string -> path:string -> unit
val refresh_ast : t -> source:string -> path:string -> unit
Loading
Loading