Skip to content

Commit 645e1a9

Browse files
authored
Improve option optimization for constants (#7913)
* Improve option optimization for constants * CHANGELOG
1 parent 74d2492 commit 645e1a9

File tree

6 files changed

+62
-78
lines changed

6 files changed

+62
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- Add (dev-)dependencies to build schema. https://github.com/rescript-lang/rescript/pull/7892
4949
- Dedicated error for dict literal spreads. https://github.com/rescript-lang/rescript/pull/7901
5050
- Dedicated error message for when mixing up `:` and `=` in various positions. https://github.com/rescript-lang/rescript/pull/7900
51+
- Improve option optimization for constants. https://github.com/rescript-lang/rescript/pull/7913
5152

5253
# 12.0.0-beta.11
5354

compiler/core/lam_pass_remove_alias.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ let id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
4141
| None ->
4242
Eval_unknown
4343

44+
let is_const_some (cst : Lam_constant.t) : bool =
45+
match cst with
46+
| Const_some _ -> true
47+
| Const_block (_, (Lambda.Blk_some | Lambda.Blk_some_not_nested), _) -> true
48+
| _ -> false
49+
4450
let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
4551
let rec simpl (lam : Lam.t) : Lam.t =
4652
match lam with
@@ -78,6 +84,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
7884
((Lprim {primitive = Pis_not_none; args = [Lvar id]} as l1), l2, l3)
7985
-> (
8086
match Hash_ident.find_opt meta.ident_tbl id with
87+
| Some (Constant c) when is_const_some c -> simpl l2
8188
| Some (ImmutableBlock _ | MutableBlock _ | Normal_optional _) -> simpl l2
8289
| Some (OptionalBlock (l, Null)) ->
8390
Lam.if_

tests/tests/src/gpr_3980_test.mjs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,33 @@
22

33
import * as Js_math from "@rescript/runtime/lib/es6/Js_math.js";
44

5+
if (1 !== 1) {
6+
throw {
7+
RE_EXN_ID: "Assert_failure",
8+
_1: [
9+
"gpr_3980_test.res",
10+
15,
11+
7
12+
],
13+
Error: new Error()
14+
};
15+
}
16+
517
let match = 1;
618

7-
if (match !== undefined) {
8-
if (match !== 1) {
19+
if (match !== 1) {
20+
if (match !== 2) {
921
throw {
1022
RE_EXN_ID: "Assert_failure",
1123
_1: [
1224
"gpr_3980_test.res",
13-
15,
14-
7
25+
13,
26+
9
1527
],
1628
Error: new Error()
1729
};
1830
}
19-
let match$1 = 1;
20-
if (match$1 !== 1) {
21-
if (match$1 !== 2) {
22-
throw {
23-
RE_EXN_ID: "Assert_failure",
24-
_1: [
25-
"gpr_3980_test.res",
26-
13,
27-
9
28-
],
29-
Error: new Error()
30-
};
31-
}
32-
Js_math.floor(1);
33-
}
34-
35-
} else {
36-
throw {
37-
RE_EXN_ID: "Assert_failure",
38-
_1: [
39-
"gpr_3980_test.res",
40-
15,
41-
7
42-
],
43-
Error: new Error()
44-
};
31+
Js_math.floor(1);
4532
}
4633

4734
/* Not a pure module */

tests/tests/src/option_optimisation.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ function $$null(val1, val2) {
2828
}
2929
}
3030

31+
function constant() {
32+
console.log(42);
33+
}
34+
35+
function param(opt) {
36+
let x = opt;
37+
console.log(x);
38+
}
39+
3140
export {
3241
boolean,
3342
$$null,
43+
constant,
44+
param,
3445
}
3546
/* No side effect */

tests/tests/src/option_optimisation.res

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ let null = (~val1: Nullable.t<int>, ~val2: Nullable.t<int>) => {
1919
| _ => "b"
2020
}
2121
}
22+
23+
let constant = () => {
24+
let opt = Some(42)
25+
switch opt {
26+
| Some(x) => Console.log(x)
27+
| None => ()
28+
}
29+
}
30+
31+
let param = (opt: int) => {
32+
switch Some(opt) {
33+
| Some(x) => Console.log(x)
34+
| None => ()
35+
}
36+
}

tests/tests/src/record_regression.mjs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -99,66 +99,29 @@ pm0 = ir0.TAG === "V0" ? [
9999

100100
let pm1;
101101

102-
if (ir1.TAG === "V0") {
103-
let x1 = "v1";
104-
let x0 = "v0";
105-
pm1 = x1 !== undefined ? [
106-
x0,
107-
x1,
108-
3
109-
] : [
110-
x0,
111-
"n/a",
112-
3
113-
];
114-
} else {
115-
pm1 = [
102+
pm1 = ir1.TAG === "V0" ? [
103+
"v0",
104+
"v1",
105+
3
106+
] : [
116107
"v0",
117108
"n/a",
118109
"v1"
119110
];
120-
}
121111

122112
let pm2;
123113

124-
if (ir2.TAG === "V0") {
125-
let x1$1 = "v1";
126-
let x0$1 = "v0";
127-
if (x1$1 !== undefined) {
128-
let x2 = 2;
129-
pm2 = x2 !== undefined ? [
130-
x0$1,
131-
x1$1,
132-
x2,
133-
3
134-
] : [
135-
x0$1,
136-
x1$1,
137-
0,
138-
3
139-
];
140-
} else {
141-
let x2$1 = 2;
142-
pm2 = x2$1 !== undefined ? [
143-
x0$1,
144-
"n/a",
145-
x2$1,
146-
3
147-
] : [
148-
x0$1,
149-
"n/a",
150-
0,
151-
3
152-
];
153-
}
154-
} else {
155-
pm2 = [
114+
pm2 = ir2.TAG === "V0" ? [
115+
"v0",
116+
"v1",
117+
2,
118+
3
119+
] : [
156120
"v0",
157121
"n/a",
158122
0,
159123
"v1"
160124
];
161-
}
162125

163126
function inlinedRecord(ir) {
164127
if (ir.TAG !== "V0") {

0 commit comments

Comments
 (0)