Skip to content

Commit 2cb5cdd

Browse files
committed
Update on "[compiler] Instruction reordering"
Adds a pass just after DCE to reorder safely reorderable instructions (jsx, primitives, globals) closer to where they are used, to allow other optimization passes to be more effective. Notably, the reordering allows scope merging to be more effective, since that pass relies on two scopes not having intervening instructions — in many cases we can now reorder such instructions out of the way and unlock merging, as demonstrated in the changed fixtures. The algorithm itself is described in the docblock. note: This is a cleaned up version of #29579 that is ready for review. [ghstack-poisoned]
1 parent a6ca8bb commit 2cb5cdd

File tree

43 files changed

+466
-712
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+466
-712
lines changed

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ import { Stringify, identity, makeArray, mutate } from "shared-runtime";
5252
* handles this correctly.
5353
*/
5454
function Foo(t0) {
55-
const $ = _c(4);
55+
const $ = _c(3);
5656
const { cond1, cond2 } = t0;
57-
const arr = makeArray({ a: 2 }, 2, []);
5857
let t1;
59-
if ($[0] !== cond1 || $[1] !== cond2 || $[2] !== arr) {
58+
if ($[0] !== cond1 || $[1] !== cond2) {
59+
const arr = makeArray({ a: 2 }, 2, []);
60+
6061
t1 = cond1 ? (
6162
<>
6263
<div>{identity("foo")}</div>
@@ -65,10 +66,9 @@ function Foo(t0) {
6566
) : null;
6667
$[0] = cond1;
6768
$[1] = cond2;
68-
$[2] = arr;
69-
$[3] = t1;
69+
$[2] = t1;
7070
} else {
71-
t1 = $[3];
71+
t1 = $[2];
7272
}
7373
return t1;
7474
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,16 @@ function Component() {
6767
}
6868
useEffect(t1, t2);
6969
let t3;
70+
let t4;
7071
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
7172
t3 = () => {
7273
setState(someGlobal.value);
7374
};
74-
$[3] = t3;
75-
} else {
76-
t3 = $[3];
77-
}
78-
let t4;
79-
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
8075
t4 = [someGlobal];
76+
$[3] = t3;
8177
$[4] = t4;
8278
} else {
79+
t3 = $[3];
8380
t4 = $[4];
8481
}
8582
useEffect(t3, t4);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect.expect.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,51 @@ import { useEffect, useState } from "react";
3939
let someGlobal = {};
4040

4141
function Component() {
42-
const $ = _c(7);
42+
const $ = _c(6);
4343
const [state, setState] = useState(someGlobal);
4444
let t0;
45+
let t1;
4546
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
46-
t0 = () => {
47+
const setGlobal = () => {
4748
someGlobal.value = true;
4849
};
49-
$[0] = t0;
50-
} else {
51-
t0 = $[0];
52-
}
53-
const setGlobal = t0;
54-
let t1;
55-
let t2;
56-
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
57-
t1 = () => {
50+
51+
t0 = () => {
5852
setGlobal();
5953
};
60-
t2 = [];
54+
t1 = [];
55+
$[0] = t0;
6156
$[1] = t1;
62-
$[2] = t2;
6357
} else {
58+
t0 = $[0];
6459
t1 = $[1];
65-
t2 = $[2];
6660
}
67-
useEffect(t1, t2);
61+
useEffect(t0, t1);
62+
let t2;
6863
let t3;
69-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
70-
t3 = () => {
64+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
65+
t2 = () => {
7166
setState(someGlobal.value);
7267
};
68+
t3 = [someGlobal];
69+
$[2] = t2;
7370
$[3] = t3;
7471
} else {
72+
t2 = $[2];
7573
t3 = $[3];
7674
}
77-
let t4;
78-
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
79-
t4 = [someGlobal];
80-
$[4] = t4;
81-
} else {
82-
t4 = $[4];
83-
}
84-
useEffect(t3, t4);
75+
useEffect(t2, t3);
8576

86-
const t5 = String(state);
87-
let t6;
88-
if ($[5] !== t5) {
89-
t6 = <div>{t5}</div>;
77+
const t4 = String(state);
78+
let t5;
79+
if ($[4] !== t4) {
80+
t5 = <div>{t4}</div>;
81+
$[4] = t4;
9082
$[5] = t5;
91-
$[6] = t6;
9283
} else {
93-
t6 = $[6];
84+
t5 = $[5];
9485
}
95-
return t6;
86+
return t5;
9687
}
9788

9889
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-reassignment-in-effect-indirect.expect.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,51 @@ import { useEffect, useState } from "react";
3939
let someGlobal = false;
4040

4141
function Component() {
42-
const $ = _c(7);
42+
const $ = _c(6);
4343
const [state, setState] = useState(someGlobal);
4444
let t0;
45+
let t1;
4546
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
46-
t0 = () => {
47+
const setGlobal = () => {
4748
someGlobal = true;
4849
};
49-
$[0] = t0;
50-
} else {
51-
t0 = $[0];
52-
}
53-
const setGlobal = t0;
54-
let t1;
55-
let t2;
56-
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
57-
t1 = () => {
50+
51+
t0 = () => {
5852
setGlobal();
5953
};
60-
t2 = [];
54+
t1 = [];
55+
$[0] = t0;
6156
$[1] = t1;
62-
$[2] = t2;
6357
} else {
58+
t0 = $[0];
6459
t1 = $[1];
65-
t2 = $[2];
6660
}
67-
useEffect(t1, t2);
61+
useEffect(t0, t1);
62+
let t2;
6863
let t3;
69-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
70-
t3 = () => {
64+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
65+
t2 = () => {
7166
setState(someGlobal);
7267
};
68+
t3 = [someGlobal];
69+
$[2] = t2;
7370
$[3] = t3;
7471
} else {
72+
t2 = $[2];
7573
t3 = $[3];
7674
}
77-
let t4;
78-
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
79-
t4 = [someGlobal];
80-
$[4] = t4;
81-
} else {
82-
t4 = $[4];
83-
}
84-
useEffect(t3, t4);
75+
useEffect(t2, t3);
8576

86-
const t5 = String(state);
87-
let t6;
88-
if ($[5] !== t5) {
89-
t6 = <div>{t5}</div>;
77+
const t4 = String(state);
78+
let t5;
79+
if ($[4] !== t4) {
80+
t5 = <div>{t4}</div>;
81+
$[4] = t4;
9082
$[5] = t5;
91-
$[6] = t6;
9283
} else {
93-
t6 = $[6];
84+
t5 = $[5];
9485
}
95-
return t6;
86+
return t5;
9687
}
9788

9889
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-reassignment-in-effect.expect.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,16 @@ function Component() {
5454
}
5555
useEffect(t0, t1);
5656
let t2;
57+
let t3;
5758
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
5859
t2 = () => {
5960
setState(someGlobal);
6061
};
61-
$[2] = t2;
62-
} else {
63-
t2 = $[2];
64-
}
65-
let t3;
66-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
6762
t3 = [someGlobal];
63+
$[2] = t2;
6864
$[3] = t3;
6965
} else {
66+
t2 = $[2];
7067
t3 = $[3];
7168
}
7269
useEffect(t2, t3);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-mutate-global-in-effect-fixpoint.expect.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,16 @@ function Component() {
7070
}
7171
useEffect(t0, t1);
7272
let t2;
73+
let t3;
7374
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
7475
t2 = () => {
7576
setState(someGlobal.value);
7677
};
77-
$[2] = t2;
78-
} else {
79-
t2 = $[2];
80-
}
81-
let t3;
82-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
8378
t3 = [someGlobal];
79+
$[2] = t2;
8480
$[3] = t3;
8581
} else {
82+
t2 = $[2];
8683
t3 = $[3];
8784
}
8885
useEffect(t2, t3);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.expect.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@ function component() {
1515
```javascript
1616
import { c as _c } from "react/compiler-runtime";
1717
function component() {
18-
const $ = _c(2);
18+
const $ = _c(1);
1919
const [x, setX] = useState(0);
2020
let t0;
2121
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
22-
t0 = (v) => setX(v);
22+
const handler = (v) => setX(v);
23+
t0 = <Foo handler={handler} />;
2324
$[0] = t0;
2425
} else {
2526
t0 = $[0];
2627
}
27-
const handler = t0;
28-
let t1;
29-
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
30-
t1 = <Foo handler={handler} />;
31-
$[1] = t1;
32-
} else {
33-
t1 = $[1];
34-
}
35-
return t1;
28+
return t0;
3629
}
3730

3831
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.expect.md

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,45 @@ function mayMutate() {}
3636
```javascript
3737
import { c as _c } from "react/compiler-runtime";
3838
function ComponentA(props) {
39-
const $ = _c(6);
40-
let a;
41-
let b;
39+
const $ = _c(2);
40+
let t0;
4241
if ($[0] !== props) {
43-
a = [];
44-
b = [];
42+
const a = [];
43+
const b = [];
4544
if (b) {
4645
a.push(props.p0);
4746
}
4847
if (props.p1) {
4948
b.push(props.p2);
5049
}
51-
$[0] = props;
52-
$[1] = a;
53-
$[2] = b;
54-
} else {
55-
a = $[1];
56-
b = $[2];
57-
}
58-
let t0;
59-
if ($[3] !== a || $[4] !== b) {
50+
6051
t0 = <Foo a={a} b={b} />;
61-
$[3] = a;
62-
$[4] = b;
63-
$[5] = t0;
52+
$[0] = props;
53+
$[1] = t0;
6454
} else {
65-
t0 = $[5];
55+
t0 = $[1];
6656
}
6757
return t0;
6858
}
6959

7060
function ComponentB(props) {
71-
const $ = _c(6);
72-
let a;
73-
let b;
61+
const $ = _c(2);
62+
let t0;
7463
if ($[0] !== props) {
75-
a = [];
76-
b = [];
64+
const a = [];
65+
const b = [];
7766
if (mayMutate(b)) {
7867
a.push(props.p0);
7968
}
8069
if (props.p1) {
8170
b.push(props.p2);
8271
}
83-
$[0] = props;
84-
$[1] = a;
85-
$[2] = b;
86-
} else {
87-
a = $[1];
88-
b = $[2];
89-
}
90-
let t0;
91-
if ($[3] !== a || $[4] !== b) {
72+
9273
t0 = <Foo a={a} b={b} />;
93-
$[3] = a;
94-
$[4] = b;
95-
$[5] = t0;
74+
$[0] = props;
75+
$[1] = t0;
9676
} else {
97-
t0 = $[5];
77+
t0 = $[1];
9878
}
9979
return t0;
10080
}

0 commit comments

Comments
 (0)