Skip to content
Merged
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
2 changes: 1 addition & 1 deletion changes/inline-assign-rename.fix.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Fix inlining another module's function that assigns a local variable, as in `let r = ref(x); r := r.contents + 1`, with `-bs-cross-module-opt`: the compiler crashed, the generated code assigned an undeclared variable, a value read from the variable before an assignment was read after it instead, or a condition on the variable was decided by the value it started with.
- Fix inlining another module's function that assigns a local variable, as in `let r = ref(x); r := r.contents + 1`: the compiler crashed, the generated code assigned an undeclared variable, a value read from the variable before an assignment was read after it instead, or a condition on the variable was decided by the value it started with.
1 change: 1 addition & 0 deletions changes/remove-cross-module-opt.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Remove the internal `-bs-cross-module-opt` and `-bs-no-cross-module-opt` flags: other modules inline exactly the functions marked `@inline`. With the first flag, small functions and functors used to be inlined into other modules automatically; mark the ones that should be with `@inline`, and drop the flags from `compiler-flags`.
20 changes: 3 additions & 17 deletions compiler/backend/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,23 @@ let key_comp a b = Ext_string.compare a b.name
let not_found key =
{name = key; arity = single_na; persistent_closed_lambda = None}

let get_result mid_val =
match mid_val.persistent_closed_lambda with
| Some
(Lconst
(Const_js_null | Const_js_undefined _ | Const_js_true | Const_js_false))
| None ->
mid_val
(* Stored because the function is [@inline]: inlined with or without
-bs-cross-module-opt. *)
| Some (Lfunction {attr = {inline = Always_inline; _}; _}) -> mid_val
| Some _ ->
if !((Js_config.current ()).cross_module_inline) then mid_val
else {mid_val with persistent_closed_lambda = None}

let rec binary_search_aux arr lo hi (key : string) =
let mid = (lo + hi) / 2 in
let mid_val = Array.unsafe_get arr mid in
let c = key_comp key mid_val in
if c = 0 then get_result mid_val
if c = 0 then mid_val
else if c < 0 then
(* a[lo] =< key < a[mid] <= a[hi] *)
if hi = mid then
let lo_val = Array.unsafe_get arr lo in
if lo_val.name = key then get_result lo_val else not_found key
if lo_val.name = key then lo_val else not_found key
else binary_search_aux arr lo mid key
else if
(* a[lo] =< a[mid] < key <= a[hi] *)
lo = mid
then
let hi_val = Array.unsafe_get arr hi in
if hi_val.name = key then get_result hi_val else not_found key
if hi_val.name = key then hi_val else not_found key
else binary_search_aux arr mid hi key

let binary_search (sorted : keyed_cmj_values) (key : string) : keyed_cmj_value =
Expand Down
12 changes: 0 additions & 12 deletions compiler/backend/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,3 @@ let ok_to_inline_fun_when_app (m : Lambda.lfunction) (args : Lambda.t list) =
s < small_inline_size
|| destruct_pattern body params args
|| (args_all_const args && s < 10 && no_side_effects body))

(* TODO: We can relax this a bit later,
but decide whether to inline it later in the call site
*)
let safe_to_inline (lam : Lambda.t) =
match lam with
| Lfunction _ -> true
| Lconst
( Const_polyvar _ | Const_constructor _ | Const_js_true | Const_js_false
| Const_js_undefined _ ) ->
true
| _ -> false
2 changes: 0 additions & 2 deletions compiler/backend/lam_analysis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ val ok_to_inline_fun_when_app : Lambda.lfunction -> Lambda.t list -> bool
val small_inline_size : int

val exit_inline_size : int

val safe_to_inline : Lambda.t -> bool
27 changes: 3 additions & 24 deletions compiler/backend/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
} -> (
(* An [@inline] function or functor: other modules inline its
body too, when they can. It must be closed, since the body is
copied without its environment. *)
copied without its environment. Other values keep only the
constants above. *)
let not_stored =
if async then Some "it is async"
else if Option.is_some directive then Some "it has a directive"
Expand Down Expand Up @@ -113,29 +114,7 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
(Warnings.Inline_not_stored
((if is_a_functor then "functor" else "function"), reason)));
None)
| _ when calls_relative_external lambda -> None
| _ when not !((Js_config.current ()).cross_module_inline) -> None
| _ when not (Lam_analysis.safe_to_inline lambda) ->
(* when inlining a non function, we have to be very careful,
only truly immutable values can be inlined *)
None
| Lfunction {attr = {is_a_functor = true}} ->
if Lam_closure.is_closed lambda then optlam else None
| _ ->
let lam_size =
Lam_analysis.size_upto ~limit:Lam_analysis.small_inline_size
lambda
in
let free_variables =
Lam_closure.free_variables Set_ident.empty Map_ident.empty lambda
in
if
lam_size < Lam_analysis.small_inline_size
&& Map_ident.is_empty free_variables
then (
Ir_log.dwarn ~__POS__ "%s recorded for inlining @." x.name;
optlam)
else None)
| _ -> None)
in
match (arity, persistent_closed_lambda) with
| Single Arity_na, (None | Some (Lconst Const_module_alias)) -> acc
Expand Down
6 changes: 0 additions & 6 deletions compiler/driver/rescript_compiler_driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,6 @@ let command_line_flags () : (string * Bsc_args.spec * string) array =
( "-bs-no-builtin-ppx",
set (Js_config.current ()).no_builtin_ppx,
"*internal* Disable built-in ppx" );
( "-bs-cross-module-opt",
set (Js_config.current ()).cross_module_inline,
"*internal* Enable cross module inlining(experimental), default(false)" );
( "-bs-no-cross-module-opt",
clear (Js_config.current ()).cross_module_inline,
"*internal* Disable cross module inlining(experimental)" );
( "-debug-ir",
set (Js_config.current ()).debug_ir,
"*internal* Dump compiler IR and enable Lambda invariant checks" );
Expand Down
2 changes: 0 additions & 2 deletions compiler/typing/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ let tool_name = "ReScript"
type t = {
no_version_header: bool ref;
directives: string list ref;
cross_module_inline: bool ref;
debug_ir: bool ref;
check_lam: bool ref;
no_builtin_ppx: bool ref;
Expand Down Expand Up @@ -60,7 +59,6 @@ let create () =
{
no_version_header = ref false;
directives = ref [];
cross_module_inline = ref false;
debug_ir = ref false;
check_lam = ref false;
no_builtin_ppx = ref false;
Expand Down
1 change: 0 additions & 1 deletion compiler/typing/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type t = {
no_version_header: bool ref; (** Suppress the version header. *)
directives: string list ref;
(** Directives printed verbatim just after the version header. *)
cross_module_inline: bool ref; (** Cross-module inlining option. *)
debug_ir: bool ref;
(** Dump intermediate representations and related diagnostics. *)
check_lam: bool ref;
Expand Down
1 change: 0 additions & 1 deletion packages/@rescript/belt/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"-no-alias-deps",
"-bs-no-version-header",
"-bs-no-check-div-by-zero",
"-bs-cross-module-opt",
"-w -3+50",
"-warn-error A"
]
Expand Down
2 changes: 0 additions & 2 deletions packages/@rescript/runtime/Primitive_hash.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ module Int = Stdlib_Int
module Obj = Primitive_object_extern
module String = Primitive_string_extern

// Note: this only works as intended as long as the runtime is compiled
// with -bs-cross-module-opt.
let typeof = Primitive_js_extern.typeof

@send external charCodeAt: (string, int) => int = "charCodeAt"
Expand Down
2 changes: 0 additions & 2 deletions packages/@rescript/runtime/Primitive_object.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module Array = Primitive_array_extern

type t = Primitive_object_extern.t

// Note: this only works as intended as long as the runtime is compiled
// with -bs-cross-module-opt.
let repr = Primitive_object_extern.repr
let magic = Primitive_object_extern.magic
let tag = Primitive_object_extern.tag
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Primitive_option.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Obj = Primitive_object_extern
type nested = {@as("BS_PRIVATE_NESTED_SOME_NONE") depth: int}

/* INPUT: [x] should not be nullable */
@inline
let isNested = (x: Obj.t): bool => {
Obj.repr((Obj.magic(x): nested).depth) !== Obj.repr(Primitive_js_extern.undefined)
}
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_JsError.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module URIError = {
let throwWithMessage = s => make(s)->throw
}

@inline
let panic = msg => make(`Panic! ${msg}`)->throw

external toJsExn: t => Stdlib_JsExn.t = "%identity"
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_TypedArray.res
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ typed array module includes them, so `Int8Array.get(view, 0)` works on an
`Int8Array.t`. A typed array reaches the functions above with a coercion,
such as `(view :> TypedArray.t<int>)`.
*/
@inline
module Impl = (
T: {
type t
Expand Down
2 changes: 1 addition & 1 deletion packages/@rescript/runtime/lib/es6/Primitive_hash.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function unsafe_pop(q) {
RE_EXN_ID: "Assert_failure",
_1: [
"Primitive_hash.res",
59,
57,
12
],
Error: new Error()
Expand Down
2 changes: 1 addition & 1 deletion packages/@rescript/runtime/lib/js/Primitive_hash.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function unsafe_pop(q) {
RE_EXN_ID: "Assert_failure",
_1: [
"Primitive_hash.res",
59,
57,
12
],
Error: new Error()
Expand Down
1 change: 0 additions & 1 deletion packages/@rescript/runtime/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"-no-alias-deps",
"-bs-no-version-header",
"-bs-no-check-div-by-zero",
"-bs-cross-module-opt",
"-w -3+50",
"-warn-error A"
]
Expand Down
4 changes: 2 additions & 2 deletions tests/belt_tests/src/belt_mapint_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Mocha.describe("Belt_mapint_test", () => {
m = Belt_MapInt.set(m, i, i);
}
for (let i$1 = 0; i$1 <= 999999; ++i$1) {
Test_utils.ok("File \"belt_mapint_test.res\", line 17, characters 9-16", Belt_MapInt.get(m, i$1) !== undefined);
Test_utils.ok("File \"belt_mapint_test.res\", line 15, characters 9-16", Belt_MapInt.get(m, i$1) !== undefined);
}
for (let i$2 = 0; i$2 <= 999999; ++i$2) {
m = Belt_MapInt.remove(m, i$2);
}
Test_utils.ok("File \"belt_mapint_test.res\", line 23, characters 7-14", Belt_MapInt.isEmpty(m));
Test_utils.ok("File \"belt_mapint_test.res\", line 21, characters 7-14", Belt_MapInt.isEmpty(m));
});
});

Expand Down
2 changes: 0 additions & 2 deletions tests/belt_tests/src/belt_mapint_test.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@@config({flags: ["-bs-no-cross-module-opt"]})

open Mocha
open Test_utils

Expand Down
9 changes: 0 additions & 9 deletions tests/belt_tests/src/rec_module_opt.res
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
@@config({
flags: [
/* "-drawlambda"; */
/* "-dlambda"; */
/* "-dtypedtree"; */
"-bs-no-cross-module-opt",
],
})

module rec A: {
type t = Leaf(string) | Node(AComparable.t)
let cmp: (t, t) => int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

// With -bs-cross-module-opt, a body that calls an external with a path
// An @inline body that calls an external with a path
// relative to its own file must not be inlined into a module in another
// directory, where that path would point elsewhere.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"in-source": true
},
"suffix": ".mjs",
"compiler-flags": ["-bs-cross-module-opt"],
"warnings": { "error": "A-113" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"sources": { "dir": "src" },
"dependencies": ["p"],
"package-specs": { "module": "esmodule", "in-source": true },
"suffix": ".mjs",
"compiler-flags": ["-bs-cross-module-opt"]
"suffix": ".mjs"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

// With -bs-cross-module-opt, a stored body that names a module of a package
// A stored @inline body that names a module of a package
// the caller does not depend on cannot be inlined there: that module is not
// on the caller's load path. The caller calls the value instead.

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

// Without -bs-cross-module-opt, B inlines A's @inline function, and is
// B inlines A's @inline function, and is
// compiled again when that body changes although A's interface does not.

import * as assert from "node:assert";
Expand Down
4 changes: 2 additions & 2 deletions tests/docstring_tests/DocTest.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/tests/src/inline_arg_order_cross_module_lib.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@@config({flags: ["-bs-cross-module-opt"]})

// Inlined by inline_arg_order_cross_module_test.res
@inline
let minus3 = (_x: int, a: int, b: int) => b - a

@inline
let callNext = (next: unit => int => int, x: int) => next()(x)
6 changes: 3 additions & 3 deletions tests/tests/src/inline_arg_order_cross_module_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function readThenCall() {
}

Mocha.describe("Inline_arg_order_cross_module_test", () => {
Mocha.test("a read of a local ref before an argument that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 38, characters 7-14", 1, readThenBump(0)));
Mocha.test("a read of a local ref after an argument that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 42, characters 7-14", 9, bumpThenRead(0)));
Mocha.test("a read of a local ref before a callee that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 46, characters 7-14", 0, readThenCall()));
Mocha.test("a read of a local ref before an argument that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 36, characters 7-14", 1, readThenBump(0)));
Mocha.test("a read of a local ref after an argument that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 40, characters 7-14", 9, bumpThenRead(0)));
Mocha.test("a read of a local ref before a callee that assigns it", () => Test_utils.eq("File \"inline_arg_order_cross_module_test.res\", line 44, characters 7-14", 0, readThenCall()));
});

export {
Expand Down
2 changes: 0 additions & 2 deletions tests/tests/src/inline_arg_order_cross_module_test.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@@config({flags: ["-bs-cross-module-opt"]})

// A call to another module's function that takes a parameter of the caller is
// inlined while compiling to JavaScript, after a local ref has become a
// variable. An argument that reads the variable used to be substituted into
Expand Down
2 changes: 0 additions & 2 deletions tests/tests/src/inline_assign_cross_module_lib.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@@config({flags: ["-bs-cross-module-opt"]})

// Inlined by inline_assign_cross_module_test.res
@inline
let bumpTwice = (x: int) => {
Expand Down
Loading
Loading