[compiler][be] Clean up nested function context in DCE (#31202)
Now that we rely on function context exclusively, let's clean up `HIRFunction.context` after DCE. This PR is in preparation of #31204, which would otherwise have unnecessary declarations (of context values that become entirely DCE'd) ' --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31202). * __->__ #31202 * #31203 * #31201 * #31200 * #31521
mofeiZ committed
Nov 15, 2024 at 13:06 UTC
0480cdb58c867c62586ff602fdb06a06c1d63f0c
7 files changed
+46
-33
compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts
+8
@@ -58,6 +58,14 @@ export function deadCodeElimination(fn: HIRFunction): void {
58
}
59
}
60
}
61
+
62
+ /**
63
+ * Constant propagation and DCE may have deleted or rewritten instructions
64
+ * that reference context variables.
65
+ */
66
+ retainWhere(fn.context, contextVar =>
67
+ state.isIdOrNameUsed(contextVar.identifier),
68
+ );
69
}
70
71
class State {
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/arrow-expr-directive.expect.md
+4
-1
@@ -28,7 +28,7 @@ function Component() {
28
t0 = () => {
29
"worklet";
30
31
- setCount((count_0) => count_0 + 1);
31
+ setCount(_temp);
32
};
33
$[0] = t0;
34
} else {
@@ -45,6 +45,9 @@ function Component() {
45
}
46
return t1;
47
}
48
+function _temp(count_0) {
49
+ return count_0 + 1;
50
+}
51
52
```
53
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md
+4
-5
@@ -55,11 +55,7 @@ function getNativeLogFunction(level) {
55
if (arguments.length === 1 && typeof arguments[0] === "string") {
56
str = arguments[0];
57
} else {
58
- str = Array.prototype.map
59
- .call(arguments, function (arg) {
60
- return inspect(arg, { depth: 10 });
61
- })
62
- .join(", ");
58
+ str = Array.prototype.map.call(arguments, _temp).join(", ");
59
}
60
61
const firstArg = arguments[0];
@@ -92,6 +88,9 @@ function getNativeLogFunction(level) {
88
}
89
return t0;
90
}
91
+function _temp(arg) {
92
+ return inspect(arg, { depth: 10 });
93
+}
94
95
```
96
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/function-expr-directive.expect.md
+4
-1
@@ -34,7 +34,7 @@ function Component() {
34
t0 = function update() {
35
"worklet";
36
37
- setCount((count_0) => count_0 + 1);
37
+ setCount(_temp);
38
};
39
$[0] = t0;
40
} else {
@@ -51,6 +51,9 @@ function Component() {
51
}
52
return t1;
53
}
54
+function _temp(count_0) {
55
+ return count_0 + 1;
56
+}
57
58
export const FIXTURE_ENTRYPOINT = {
59
fn: Component,
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.expect.md
+4
-1
@@ -32,7 +32,7 @@ function Component() {
32
let t0;
33
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
34
t0 = () => {
35
- setState((s) => s + 1);
35
+ setState(_temp);
36
};
37
$[0] = t0;
38
} else {
@@ -61,6 +61,9 @@ function Component() {
61
}
62
return t2;
63
}
64
+function _temp(s) {
65
+ return s + 1;
66
+}
67
68
```
69
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md
+18
-24
@@ -39,7 +39,7 @@ function Component() {
39
```javascript
40
import { c as _c } from "react/compiler-runtime"; // @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
41
function Component() {
42
- const $ = _c(8);
42
+ const $ = _c(7);
43
const items = useItems();
44
let t0;
45
let t1;
@@ -47,35 +47,25 @@ function Component() {
47
if ($[0] !== items) {
48
t2 = Symbol.for("react.early_return_sentinel");
49
bb0: {
50
- let t3;
51
- if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
52
- t3 = (t4) => {
53
- const [item] = t4;
54
- return item.name != null;
55
- };
56
- $[4] = t3;
57
- } else {
58
- t3 = $[4];
59
- }
60
- t0 = items.filter(t3);
50
+ t0 = items.filter(_temp);
51
const filteredItems = t0;
52
if (filteredItems.length === 0) {
63
- let t4;
64
- if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
65
- t4 = (
53
+ let t3;
54
+ if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
55
+ t3 = (
56
<div>
57
<span />
58
</div>
59
);
70
- $[5] = t4;
60
+ $[4] = t3;
61
} else {
72
- t4 = $[5];
62
+ t3 = $[4];
63
}
74
- t2 = t4;
64
+ t2 = t3;
65
break bb0;
66
}
67
78
- t1 = filteredItems.map(_temp);
68
+ t1 = filteredItems.map(_temp2);
69
}
70
$[0] = items;
71
$[1] = t1;
@@ -90,19 +80,23 @@ function Component() {
80
return t2;
81
}
82
let t3;
93
- if ($[6] !== t1) {
83
+ if ($[5] !== t1) {
84
t3 = <>{t1}</>;
95
- $[6] = t1;
96
- $[7] = t3;
85
+ $[5] = t1;
86
+ $[6] = t3;
87
} else {
98
- t3 = $[7];
88
+ t3 = $[6];
89
}
90
return t3;
91
}
102
-function _temp(t0) {
92
+function _temp2(t0) {
93
const [item_0] = t0;
94
return <Stringify item={item_0} />;
95
}
96
+function _temp(t0) {
97
+ const [item] = t0;
98
+ return item.name != null;
99
+}
100
101
```
102
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/resolve-react-hooks-based-on-import-name.expect.md
+4
-1
@@ -38,7 +38,7 @@ function Component() {
38
let t0;
39
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
40
t0 = () => {
41
- setState((s) => s + 1);
41
+ setState(_temp);
42
};
43
$[0] = t0;
44
} else {
@@ -67,6 +67,9 @@ function Component() {
67
}
68
return t2;
69
}
70
+function _temp(s) {
71
+ return s + 1;
72
+}
73
74
export const FIXTURE_ENTRYPOINT = {
75
fn: Component,