Skip to content

Commit 0480cdb

Browse files
authored
[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
1 parent 0f3c62b commit 0480cdb

7 files changed

+46
-33
lines changed

Diff for: compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export function deadCodeElimination(fn: HIRFunction): void {
5858
}
5959
}
6060
}
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+
);
6169
}
6270

6371
class State {

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/arrow-expr-directive.expect.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Component() {
2828
t0 = () => {
2929
"worklet";
3030

31-
setCount((count_0) => count_0 + 1);
31+
setCount(_temp);
3232
};
3333
$[0] = t0;
3434
} else {
@@ -45,6 +45,9 @@ function Component() {
4545
}
4646
return t1;
4747
}
48+
function _temp(count_0) {
49+
return count_0 + 1;
50+
}
4851

4952
```
5053

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ function getNativeLogFunction(level) {
5555
if (arguments.length === 1 && typeof arguments[0] === "string") {
5656
str = arguments[0];
5757
} 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(", ");
6359
}
6460

6561
const firstArg = arguments[0];
@@ -92,6 +88,9 @@ function getNativeLogFunction(level) {
9288
}
9389
return t0;
9490
}
91+
function _temp(arg) {
92+
return inspect(arg, { depth: 10 });
93+
}
9594

9695
```
9796

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/function-expr-directive.expect.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Component() {
3434
t0 = function update() {
3535
"worklet";
3636

37-
setCount((count_0) => count_0 + 1);
37+
setCount(_temp);
3838
};
3939
$[0] = t0;
4040
} else {
@@ -51,6 +51,9 @@ function Component() {
5151
}
5252
return t1;
5353
}
54+
function _temp(count_0) {
55+
return count_0 + 1;
56+
}
5457

5558
export const FIXTURE_ENTRYPOINT = {
5659
fn: Component,

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.expect.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Component() {
3232
let t0;
3333
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
3434
t0 = () => {
35-
setState((s) => s + 1);
35+
setState(_temp);
3636
};
3737
$[0] = t0;
3838
} else {
@@ -61,6 +61,9 @@ function Component() {
6161
}
6262
return t2;
6363
}
64+
function _temp(s) {
65+
return s + 1;
66+
}
6467

6568
```
6669

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md

+18-24
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,33 @@ function Component() {
3939
```javascript
4040
import { c as _c } from "react/compiler-runtime"; // @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
4141
function Component() {
42-
const $ = _c(8);
42+
const $ = _c(7);
4343
const items = useItems();
4444
let t0;
4545
let t1;
4646
let t2;
4747
if ($[0] !== items) {
4848
t2 = Symbol.for("react.early_return_sentinel");
4949
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);
6151
const filteredItems = t0;
6252
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 = (
6656
<div>
6757
<span />
6858
</div>
6959
);
70-
$[5] = t4;
60+
$[4] = t3;
7161
} else {
72-
t4 = $[5];
62+
t3 = $[4];
7363
}
74-
t2 = t4;
64+
t2 = t3;
7565
break bb0;
7666
}
7767

78-
t1 = filteredItems.map(_temp);
68+
t1 = filteredItems.map(_temp2);
7969
}
8070
$[0] = items;
8171
$[1] = t1;
@@ -90,19 +80,23 @@ function Component() {
9080
return t2;
9181
}
9282
let t3;
93-
if ($[6] !== t1) {
83+
if ($[5] !== t1) {
9484
t3 = <>{t1}</>;
95-
$[6] = t1;
96-
$[7] = t3;
85+
$[5] = t1;
86+
$[6] = t3;
9787
} else {
98-
t3 = $[7];
88+
t3 = $[6];
9989
}
10090
return t3;
10191
}
102-
function _temp(t0) {
92+
function _temp2(t0) {
10393
const [item_0] = t0;
10494
return <Stringify item={item_0} />;
10595
}
96+
function _temp(t0) {
97+
const [item] = t0;
98+
return item.name != null;
99+
}
106100

107101
```
108102

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/resolve-react-hooks-based-on-import-name.expect.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function Component() {
3838
let t0;
3939
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
4040
t0 = () => {
41-
setState((s) => s + 1);
41+
setState(_temp);
4242
};
4343
$[0] = t0;
4444
} else {
@@ -67,6 +67,9 @@ function Component() {
6767
}
6868
return t2;
6969
}
70+
function _temp(s) {
71+
return s + 1;
72+
}
7073

7174
export const FIXTURE_ENTRYPOINT = {
7275
fn: Component,

0 commit comments

Comments
 (0)