Skip to content

Commit a84a59d

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 281d161 commit a84a59d

15 files changed

+192
-196
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
@@ -230,6 +230,8 @@ const EnvironmentConfigSchema = z.object({
230230
*/
231231
enableUseTypeAnnotations: z.boolean().default(false),
232232

233+
enableFunctionDependencyRewrite: z.boolean().default(true),
234+
233235
/**
234236
* Enables inlining ReactElement object literals in place of JSX
235237
* 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 & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -669,35 +669,55 @@ function collectDependencies(
669669
): boolean => {
670670
return processedInstrsInOptional.get(fn)?.has(id) ?? false;
671671
};
672-
for (const [blockId, block] of fn.body.blocks) {
673-
scopeTraversal.recordScopes(block);
674-
const scopeBlockInfo = scopeTraversal.blockInfos.get(blockId);
675-
if (scopeBlockInfo?.kind === 'begin') {
676-
context.enterScope(scopeBlockInfo.scope);
677-
} else if (scopeBlockInfo?.kind === 'end') {
678-
context.exitScope(scopeBlockInfo.scope, scopeBlockInfo?.pruned);
679-
}
680672

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

696-
if (!shouldSkipInstructionDependencies(fn, block.terminal.id)) {
697-
for (const place of eachTerminalOperand(block.terminal)) {
698-
context.visitOperand(place);
713+
if (!shouldSkipInstructionDependencies(fn, block.terminal.id)) {
714+
for (const place of eachTerminalOperand(block.terminal)) {
715+
context.visitOperand(place);
716+
}
699717
}
700718
}
701-
}
719+
};
720+
721+
handleFunction(fn);
702722
return context.deps;
703723
}

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
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-outlining-child-stored-in-id.expect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Component(arr) {
5353
const $ = _c(3);
5454
const x = useX();
5555
let t0;
56-
if ($[0] !== arr || $[1] !== x) {
56+
if ($[0] !== x || $[1] !== arr) {
5757
t0 = arr.map((i) => {
5858
arr.map((i_0, id) => {
5959
const T0 = _temp;
@@ -63,8 +63,8 @@ function Component(arr) {
6363
return jsx;
6464
});
6565
});
66-
$[0] = arr;
67-
$[1] = x;
66+
$[0] = x;
67+
$[1] = arr;
6868
$[2] = t0;
6969
} else {
7070
t0 = $[2];
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

0 commit comments

Comments
 (0)