Skip to content

Commit 3a125c5

Browse files
committed
[compiler] Add lowerContextAccess pass
*This is only for internal profiling, not intended to ship.* This pass is intended to be used with #30407. This pass synthesizes selector functions by collecting immediately destructured context acesses. We bailout for other types of context access. This pass lowers context access to use a selector function by passing the synthesized selector function as the second argument. ghstack-source-id: f01152c Pull Request resolved: #30548
1 parent bae18b4 commit 3a125c5

16 files changed

+600
-0
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import {
9898
} from '../Validation';
9999
import {validateLocalsNotReassignedAfterRender} from '../Validation/ValidateLocalsNotReassignedAfterRender';
100100
import {outlineFunctions} from '../Optimization/OutlineFunctions';
101+
import {lowerContextAccess} from '../Optimization/LowerContextAccess';
101102

102103
export type CompilerPipelineValue =
103104
| {kind: 'ast'; name: string; value: CodegenFunction}
@@ -199,6 +200,10 @@ function* runWithEnvironment(
199200
validateNoCapitalizedCalls(hir);
200201
}
201202

203+
if (env.config.enableLowerContextAccess) {
204+
lowerContextAccess(hir);
205+
}
206+
202207
analyseFunctions(hir);
203208
yield log({kind: 'hir', name: 'AnalyseFunctions', value: hir});
204209

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {
9+
ArrayExpression,
10+
BasicBlock,
11+
CallExpression,
12+
Destructure,
13+
Effect,
14+
Environment,
15+
GeneratedSource,
16+
HIRFunction,
17+
IdentifierId,
18+
Instruction,
19+
LoadLocal,
20+
Place,
21+
PropertyLoad,
22+
isUseContextHookType,
23+
makeBlockId,
24+
makeInstructionId,
25+
makeTemporary,
26+
markInstructionIds,
27+
mergeConsecutiveBlocks,
28+
promoteTemporary,
29+
removeUnnecessaryTryCatch,
30+
reversePostorderBlocks,
31+
} from '../HIR';
32+
import {
33+
createTemporaryPlace,
34+
removeDeadDoWhileStatements,
35+
removeUnreachableForUpdates,
36+
} from '../HIR/HIRBuilder';
37+
import {enterSSA} from '../SSA';
38+
import {inferTypes} from '../TypeInference';
39+
40+
export function lowerContextAccess(fn: HIRFunction): void {
41+
const contextAccess: Map<IdentifierId, CallExpression> = new Map();
42+
const contextKeys: Map<IdentifierId, Array<string>> = new Map();
43+
44+
// collect context access and keys
45+
for (const [, block] of fn.body.blocks) {
46+
for (const instr of block.instructions) {
47+
const {value, lvalue} = instr;
48+
49+
if (
50+
value.kind === 'CallExpression' &&
51+
isUseContextHookType(value.callee.identifier)
52+
) {
53+
contextAccess.set(lvalue.identifier.id, value);
54+
continue;
55+
}
56+
57+
if (value.kind !== 'Destructure') {
58+
continue;
59+
}
60+
61+
const destructureId = value.value.identifier.id;
62+
if (!contextAccess.has(destructureId)) {
63+
continue;
64+
}
65+
66+
const keys = getContextKeys(value);
67+
if (keys === null) {
68+
return;
69+
}
70+
71+
if (contextKeys.has(destructureId)) {
72+
/*
73+
* TODO(gsn): Add support for accessing context over multiple
74+
* statements.
75+
*/
76+
return;
77+
} else {
78+
contextKeys.set(destructureId, keys);
79+
}
80+
}
81+
}
82+
83+
if (contextAccess.size > 0) {
84+
for (const [, block] of fn.body.blocks) {
85+
let nextInstructions: Array<Instruction> | null = null;
86+
87+
for (let i = 0; i < block.instructions.length; i++) {
88+
const instr = block.instructions[i];
89+
const {lvalue, value} = instr;
90+
if (
91+
value.kind === 'CallExpression' &&
92+
isUseContextHookType(value.callee.identifier) &&
93+
contextKeys.has(lvalue.identifier.id)
94+
) {
95+
const keys = contextKeys.get(lvalue.identifier.id)!;
96+
const selectorFnInstr = emitSelectorFn(fn.env, keys);
97+
if (nextInstructions === null) {
98+
nextInstructions = block.instructions.slice(0, i);
99+
}
100+
nextInstructions.push(selectorFnInstr);
101+
102+
const selectorFn = selectorFnInstr.lvalue;
103+
value.args.push(selectorFn);
104+
}
105+
106+
if (nextInstructions) {
107+
nextInstructions.push(instr);
108+
}
109+
}
110+
if (nextInstructions) {
111+
block.instructions = nextInstructions;
112+
}
113+
}
114+
markInstructionIds(fn.body);
115+
}
116+
}
117+
118+
function getContextKeys(value: Destructure): Array<string> | null {
119+
const keys = [];
120+
const pattern = value.lvalue.pattern;
121+
122+
switch (pattern.kind) {
123+
case 'ArrayPattern': {
124+
return null;
125+
}
126+
127+
case 'ObjectPattern': {
128+
for (const place of pattern.properties) {
129+
debugger;
130+
if (
131+
place.kind !== 'ObjectProperty' ||
132+
place.type !== 'property' ||
133+
place.key.kind !== 'identifier' ||
134+
place.place.identifier.name === null ||
135+
place.place.identifier.name.kind !== 'named'
136+
) {
137+
return null;
138+
}
139+
keys.push(place.key.name);
140+
}
141+
return keys;
142+
}
143+
}
144+
}
145+
146+
function emitPropertyLoad(
147+
env: Environment,
148+
obj: Place,
149+
property: string,
150+
): {instructions: Array<Instruction>; element: Place} {
151+
const loadObj: LoadLocal = {
152+
kind: 'LoadLocal',
153+
place: obj,
154+
loc: GeneratedSource,
155+
};
156+
const object: Place = createTemporaryPlace(env, GeneratedSource);
157+
const loadLocalInstr: Instruction = {
158+
lvalue: object,
159+
value: loadObj,
160+
id: makeInstructionId(0),
161+
loc: GeneratedSource,
162+
};
163+
164+
const loadProp: PropertyLoad = {
165+
kind: 'PropertyLoad',
166+
object,
167+
property,
168+
loc: GeneratedSource,
169+
};
170+
const element: Place = createTemporaryPlace(env, GeneratedSource);
171+
const loadPropInstr: Instruction = {
172+
lvalue: element,
173+
value: loadProp,
174+
id: makeInstructionId(0),
175+
loc: GeneratedSource,
176+
};
177+
return {
178+
instructions: [loadLocalInstr, loadPropInstr],
179+
element: element,
180+
};
181+
}
182+
183+
function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
184+
const obj: Place = createTemporaryPlace(env, GeneratedSource);
185+
promoteTemporary(obj.identifier);
186+
const instr: Array<Instruction> = [];
187+
const elements = [];
188+
for (const key of keys) {
189+
const {instructions, element: prop} = emitPropertyLoad(env, obj, key);
190+
instr.push(...instructions);
191+
elements.push(prop);
192+
}
193+
194+
const arrayInstr = emitArrayInstr(elements, env);
195+
instr.push(arrayInstr);
196+
197+
const block: BasicBlock = {
198+
kind: 'block',
199+
id: makeBlockId(0),
200+
instructions: instr,
201+
terminal: {
202+
id: makeInstructionId(0),
203+
kind: 'return',
204+
loc: GeneratedSource,
205+
value: arrayInstr.lvalue,
206+
},
207+
preds: new Set(),
208+
phis: new Set(),
209+
};
210+
211+
const fn: HIRFunction = {
212+
loc: GeneratedSource,
213+
id: null,
214+
fnType: 'Other',
215+
env,
216+
params: [obj],
217+
returnType: null,
218+
context: [],
219+
effects: null,
220+
body: {
221+
entry: block.id,
222+
blocks: new Map([[block.id, block]]),
223+
},
224+
generator: false,
225+
async: false,
226+
directives: [],
227+
};
228+
229+
reversePostorderBlocks(fn.body);
230+
markInstructionIds(fn.body);
231+
enterSSA(fn);
232+
inferTypes(fn);
233+
234+
const fnInstr: Instruction = {
235+
id: makeInstructionId(0),
236+
value: {
237+
kind: 'FunctionExpression',
238+
name: null,
239+
loweredFunc: {
240+
func: fn,
241+
dependencies: [],
242+
},
243+
type: 'ArrowFunctionExpression',
244+
loc: GeneratedSource,
245+
},
246+
lvalue: {
247+
kind: 'Identifier',
248+
identifier: makeTemporary(env.nextIdentifierId, GeneratedSource),
249+
effect: Effect.Unknown,
250+
reactive: false,
251+
loc: GeneratedSource,
252+
},
253+
loc: GeneratedSource,
254+
};
255+
return fnInstr;
256+
}
257+
258+
function emitArrayInstr(elements: Array<Place>, env: Environment): Instruction {
259+
const array: ArrayExpression = {
260+
kind: 'ArrayExpression',
261+
elements,
262+
loc: GeneratedSource,
263+
};
264+
const arrayLvalue: Place = {
265+
kind: 'Identifier',
266+
identifier: makeTemporary(env.nextIdentifierId, GeneratedSource),
267+
effect: Effect.Unknown,
268+
reactive: false,
269+
loc: GeneratedSource,
270+
};
271+
const arrayInstr: Instruction = {
272+
id: makeInstructionId(0),
273+
value: array,
274+
lvalue: arrayLvalue,
275+
loc: GeneratedSource,
276+
};
277+
return arrayInstr;
278+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @enableLowerContextAccess
6+
function App() {
7+
const {foo} = useContext(MyContext);
8+
const {bar} = useContext(MyContext);
9+
return <Bar foo={foo} bar={bar} />;
10+
}
11+
12+
```
13+
14+
## Code
15+
16+
```javascript
17+
import { c as _c } from "react/compiler-runtime"; // @enableLowerContextAccess
18+
function App() {
19+
const $ = _c(3);
20+
const { foo } = useContext(MyContext, _temp);
21+
const { bar } = useContext(MyContext, _temp2);
22+
let t0;
23+
if ($[0] !== foo || $[1] !== bar) {
24+
t0 = <Bar foo={foo} bar={bar} />;
25+
$[0] = foo;
26+
$[1] = bar;
27+
$[2] = t0;
28+
} else {
29+
t0 = $[2];
30+
}
31+
return t0;
32+
}
33+
function _temp2(t0) {
34+
return [t0.bar];
35+
}
36+
function _temp(t0) {
37+
return [t0.foo];
38+
}
39+
40+
```
41+
42+
### Eval output
43+
(kind: exception) Fixture not implemented
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @enableLowerContextAccess
2+
function App() {
3+
const {foo} = useContext(MyContext);
4+
const {bar} = useContext(MyContext);
5+
return <Bar foo={foo} bar={bar} />;
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @enableLowerContextAccess
6+
function App() {
7+
const {foo, bar} = useContext(MyContext);
8+
return <Bar foo={foo} bar={bar} />;
9+
}
10+
11+
```
12+
13+
## Code
14+
15+
```javascript
16+
import { c as _c } from "react/compiler-runtime"; // @enableLowerContextAccess
17+
function App() {
18+
const $ = _c(3);
19+
const { foo, bar } = useContext(MyContext, _temp);
20+
let t0;
21+
if ($[0] !== foo || $[1] !== bar) {
22+
t0 = <Bar foo={foo} bar={bar} />;
23+
$[0] = foo;
24+
$[1] = bar;
25+
$[2] = t0;
26+
} else {
27+
t0 = $[2];
28+
}
29+
return t0;
30+
}
31+
function _temp(t0) {
32+
return [t0.foo, t0.bar];
33+
}
34+
35+
```
36+
37+
### Eval output
38+
(kind: exception) Fixture not implemented
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @enableLowerContextAccess
2+
function App() {
3+
const {foo, bar} = useContext(MyContext);
4+
return <Bar foo={foo} bar={bar} />;
5+
}

0 commit comments

Comments
 (0)