Skip to content

Commit 6483117

Browse files
committed
[compiler] Stop using function dependencies in propagateScopeDeps
Recursively visit inner function instructions to extract dependencies instead of using `LoweredFunction.dependencies` directly. This is currently gated by enableFunctionDependencyRewrite, which needs to be removed before we delete `LoweredFunction.dependencies` altogether (#31204). Some nice side effects - optional-chaining deps for inner functions - full DCE and outlining for inner functions (see #31202) - fewer extraneous instructions (see #31204) -
1 parent 125581e commit 6483117

12 files changed

+159
-170
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ const EnvironmentConfigSchema = z.object({
231231
*/
232232
enableUseTypeAnnotations: z.boolean().default(false),
233233

234+
enableFunctionDependencyRewrite: z.boolean().default(true),
235+
234236
/**
235237
* Enables inlining ReactElement object literals in place of JSX
236238
* An alternative to the standard JSX transform which replaces JSX with React's jsxProd() runtime

compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -663,35 +663,54 @@ function collectDependencies(
663663

664664
const scopeTraversal = new ScopeBlockTraversal();
665665

666-
for (const [blockId, block] of fn.body.blocks) {
667-
scopeTraversal.recordScopes(block);
668-
const scopeBlockInfo = scopeTraversal.blockInfos.get(blockId);
669-
if (scopeBlockInfo?.kind === 'begin') {
670-
context.enterScope(scopeBlockInfo.scope);
671-
} else if (scopeBlockInfo?.kind === 'end') {
672-
context.exitScope(scopeBlockInfo.scope, scopeBlockInfo?.pruned);
673-
}
674-
675-
// Record referenced optional chains in phis
676-
for (const phi of block.phis) {
677-
for (const operand of phi.operands) {
678-
const maybeOptionalChain = temporaries.get(operand[1].identifier.id);
679-
if (maybeOptionalChain) {
680-
context.visitDependency(maybeOptionalChain);
666+
const handleFunction = (fn: HIRFunction): void => {
667+
for (const [blockId, block] of fn.body.blocks) {
668+
scopeTraversal.recordScopes(block);
669+
const scopeBlockInfo = scopeTraversal.blockInfos.get(blockId);
670+
if (scopeBlockInfo?.kind === 'begin') {
671+
context.enterScope(scopeBlockInfo.scope);
672+
} else if (scopeBlockInfo?.kind === 'end') {
673+
context.exitScope(scopeBlockInfo.scope, scopeBlockInfo.pruned);
674+
}
675+
// Record referenced optional chains in phis
676+
for (const phi of block.phis) {
677+
for (const operand of phi.operands) {
678+
const maybeOptionalChain = temporaries.get(operand[1].identifier.id);
679+
if (maybeOptionalChain) {
680+
context.visitDependency(maybeOptionalChain);
681+
}
681682
}
682683
}
683-
}
684-
for (const instr of block.instructions) {
685-
if (!processedInstrsInOptional.has(instr)) {
686-
handleInstruction(instr, context);
684+
for (const instr of block.instructions) {
685+
if (
686+
fn.env.config.enableFunctionDependencyRewrite &&
687+
(instr.value.kind === 'FunctionExpression' ||
688+
instr.value.kind === 'ObjectMethod')
689+
) {
690+
context.declare(instr.lvalue.identifier, {
691+
id: instr.id,
692+
scope: context.currentScope,
693+
});
694+
/**
695+
* Recursively visit the inner function to extract dependencies there
696+
*/
697+
const wasInInnerFn = context.inInnerFn;
698+
context.inInnerFn = true;
699+
handleFunction(instr.value.loweredFunc.func);
700+
context.inInnerFn = wasInInnerFn;
701+
} else if (!processedInstrsInOptional.has(instr)) {
702+
handleInstruction(instr, context);
703+
}
687704
}
688-
}
689705

690-
if (!processedInstrsInOptional.has(block.terminal)) {
691-
for (const place of eachTerminalOperand(block.terminal)) {
692-
context.visitOperand(place);
706+
if (!processedInstrsInOptional.has(block.terminal)) {
707+
for (const place of eachTerminalOperand(block.terminal)) {
708+
context.visitOperand(place);
709+
}
693710
}
694711
}
695-
}
712+
};
713+
714+
handleFunction(fn);
696715
return context.deps;
697716
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,20 @@ export const FIXTURE_ENTRYPOINT = {
2626
```javascript
2727
import { c as _c } from "react/compiler-runtime";
2828
function component(a, b) {
29-
const $ = _c(5);
30-
let t0;
31-
if ($[0] !== b) {
32-
t0 = { b };
33-
$[0] = b;
34-
$[1] = t0;
35-
} else {
36-
t0 = $[1];
37-
}
38-
const y = t0;
29+
const $ = _c(2);
30+
const y = { b };
3931
let z;
40-
if ($[2] !== a || $[3] !== y) {
32+
if ($[0] !== a) {
4133
z = { a };
4234
const x = function () {
4335
z.a = 2;
4436
};
4537

4638
x();
47-
$[2] = a;
48-
$[3] = y;
49-
$[4] = z;
39+
$[0] = a;
40+
$[1] = z;
5041
} else {
51-
z = $[4];
42+
z = $[1];
5243
}
5344
return z;
5445
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validatePreserveExistingMemoizationGuarantees
6+
import {useCallback} from 'react';
7+
import {Stringify} from 'shared-runtime';
8+
9+
/**
10+
* TODO: we're currently bailing out because `contextVar` is a context variable
11+
* and not recorded into the PropagateScopeDeps LoadLocal / PropertyLoad
12+
* sidemap. Previously, we were able to avoid this as `BuildHIR` hoisted
13+
* `LoadContext` and `PropertyLoad` instructions into the outer function, which
14+
* we took as eligible dependencies.
15+
*
16+
* One solution is to simply record `LoadContext` identifiers into the
17+
* temporaries sidemap when the instruction occurs *after* the context
18+
* variable's mutable range.
19+
*/
20+
function Foo(props) {
21+
let contextVar;
22+
if (props.cond) {
23+
contextVar = {val: 2};
24+
} else {
25+
contextVar = {};
26+
}
27+
28+
const cb = useCallback(() => [contextVar.val], [contextVar.val]);
29+
30+
return <Stringify cb={cb} shouldInvokeFns={true} />;
31+
}
32+
33+
export const FIXTURE_ENTRYPOINT = {
34+
fn: Foo,
35+
params: [{cond: true}],
36+
};
37+
38+
```
39+
40+
41+
## Error
42+
43+
```
44+
22 | }
45+
23 |
46+
> 24 | const cb = useCallback(() => [contextVar.val], [contextVar.val]);
47+
| ^^^^^^^^^^^^^^^^^^^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (24:24)
48+
25 |
49+
26 | return <Stringify cb={cb} shouldInvokeFns={true} />;
50+
27 | }
51+
```
52+
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @validatePreserveExistingMemoizationGuarantees
2+
import {useCallback} from 'react';
3+
import {Stringify} from 'shared-runtime';
4+
5+
/**
6+
* TODO: we're currently bailing out because `contextVar` is a context variable
7+
* and not recorded into the PropagateScopeDeps LoadLocal / PropertyLoad
8+
* sidemap. Previously, we were able to avoid this as `BuildHIR` hoisted
9+
* `LoadContext` and `PropertyLoad` instructions into the outer function, which
10+
* we took as eligible dependencies.
11+
*
12+
* One solution is to simply record `LoadContext` identifiers into the
13+
* temporaries sidemap when the instruction occurs *after* the context
14+
* variable's mutable range.
15+
*/
16+
function Foo(props) {
17+
let contextVar;
18+
if (props.cond) {
19+
contextVar = {val: 2};
20+
} else {
21+
contextVar = {};
22+
}
23+
24+
const cb = useCallback(() => [contextVar.val], [contextVar.val]);
25+
26+
return <Stringify cb={cb} shouldInvokeFns={true} />;
27+
}
28+
29+
export const FIXTURE_ENTRYPOINT = {
30+
fn: Foo,
31+
params: [{cond: true}],
32+
};

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/error.useCallback-infer-less-specific-conditional-access.expect.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ function Component({propA, propB}) {
4444
| ^^^^^^^^^^^^^^^^^
4545
> 14 | }, [propA?.a, propB.x.y]);
4646
| ^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (6:14)
47-
48-
CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (6:14)
4947
15 | }
5048
16 |
5149
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/useCallback-captures-reassigned-context-property.expect.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/useCallback-captures-reassigned-context-property.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/useCallback-captures-reassigned-context.expect.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,16 @@ function Foo(props) {
4545
} else {
4646
x = $[1];
4747
}
48-
49-
const t0 = x;
50-
let t1;
51-
if ($[2] !== t0) {
52-
t1 = () => [x];
53-
$[2] = t0;
54-
$[3] = t1;
48+
let t0;
49+
if ($[2] !== x) {
50+
t0 = () => [x];
51+
$[2] = x;
52+
$[3] = t0;
5553
} else {
56-
t1 = $[3];
54+
t0 = $[3];
5755
}
5856
x;
59-
const cb = t1;
57+
const cb = t0;
6058
return cb;
6159
}
6260

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/useCallback-extended-contextvar-scope.expect.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,26 @@ function useBar(t0, cond) {
7070
if (cond) {
7171
x = b;
7272
}
73-
74-
const t2 = x;
75-
let t3;
76-
if ($[1] !== a || $[2] !== t2) {
77-
t3 = () => [a, x];
73+
let t2;
74+
if ($[1] !== a || $[2] !== x) {
75+
t2 = () => [a, x];
7876
$[1] = a;
79-
$[2] = t2;
80-
$[3] = t3;
77+
$[2] = x;
78+
$[3] = t2;
8179
} else {
82-
t3 = $[3];
80+
t2 = $[3];
8381
}
8482
x;
85-
const cb = t3;
86-
let t4;
83+
const cb = t2;
84+
let t3;
8785
if ($[4] !== cb) {
88-
t4 = <Stringify cb={cb} shouldInvoke={true} />;
86+
t3 = <Stringify cb={cb} shouldInvoke={true} />;
8987
$[4] = cb;
90-
$[5] = t4;
88+
$[5] = t3;
9189
} else {
92-
t4 = $[5];
90+
t3 = $[5];
9391
}
94-
return t4;
92+
return t3;
9593
}
9694

9795
export const FIXTURE_ENTRYPOINT = {

0 commit comments

Comments
 (0)