Skip to content

Commit a1668a6

Browse files
committed
[compiler] Optimize emission in normal (non-value) blocks
In #29863 I tried to find a clean way to share code for emitting instructions between value blocks and regular blocks. The catch is that value blocks have special meaning for their final instruction — that's the value of the block — so reordering can't change the last instruction. However, in finding a clean way to share code for these two categories of code, i also inadvertently reduced the effectiveness of the optimization. This PR updates to use different strategies for these two kinds of blocks: value blocks use the code from #29863 where we first emit all non-reorderable instructions in their original order, _then_ try to emit reorderable values. The reason this is suboptimal, though, is that we want to move instructions closer to their dependencies so that they can invalidate (merge) together. Emitting the reorderable values last prevents this. So for normal blocks, we now emit terminal operands first. This will invariably cause _some_ of the non-reorderable instructions to be emitted, but it will intersperse reoderable instructions in between, right after their dependencies. This maximizes our ability to merge scopes. I think the complexity cost of two strategies is worth the benefit, though i still need to double-check all the output changes. ghstack-source-id: 567b2299fb7d7b22e7ae913041079b5c2ea87bd9 Pull Request resolved: #29883
1 parent f8cb40b commit a1668a6

File tree

151 files changed

+1387
-1208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+1387
-1208
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ const EnvironmentConfigSchema = z.object({
281281
* Enable instruction reordering. See InstructionReordering.ts for the details
282282
* of the approach.
283283
*/
284-
enableInstructionReordering: z.boolean().default(false),
284+
enableInstructionReordering: z.boolean().default(true),
285285

286286
/*
287287
* Enables instrumentation codegen. This emits a dev-mode only call to an

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

+116-53
Original file line numberDiff line numberDiff line change
@@ -243,67 +243,131 @@ function reorderBlock(
243243

244244
DEBUG && console.log(`bb${block.id}`);
245245

246-
// First emit everything that can't be reordered
247-
if (previous !== null) {
248-
DEBUG && console.log(`(last non-reorderable instruction)`);
249-
DEBUG && print(env, locals, shared, seen, previous);
250-
emit(env, locals, shared, nextInstructions, previous);
251-
}
252-
/*
253-
* For "value" blocks the final instruction represents its value, so we have to be
254-
* careful to not change the ordering. Emit the last instruction explicitly.
255-
* Any non-reorderable instructions will get emitted first, and any unused
256-
* reorderable instructions can be deferred to the shared node list.
246+
/**
247+
* The ideal order for emitting instructions may change the final instruction,
248+
* but value blocks have special semantics for the final instruction of a block -
249+
* that's the expression's value!. So we choose between a less optimal strategy
250+
* for value blocks which preserves the final instruction order OR a more optimal
251+
* ordering for statement-y blocks.
257252
*/
258-
if (isExpressionBlockKind(block.kind) && block.instructions.length !== 0) {
259-
DEBUG && console.log(`(block value)`);
260-
DEBUG &&
261-
print(
253+
if (isExpressionBlockKind(block.kind)) {
254+
// First emit everything that can't be reordered
255+
if (previous !== null) {
256+
DEBUG && console.log(`(last non-reorderable instruction)`);
257+
DEBUG && print(env, locals, shared, seen, previous);
258+
emit(env, locals, shared, nextInstructions, previous);
259+
}
260+
/*
261+
* For "value" blocks the final instruction represents its value, so we have to be
262+
* careful to not change the ordering. Emit the last instruction explicitly.
263+
* Any non-reorderable instructions will get emitted first, and any unused
264+
* reorderable instructions can be deferred to the shared node list.
265+
*/
266+
if (block.instructions.length !== 0) {
267+
DEBUG && console.log(`(block value)`);
268+
DEBUG &&
269+
print(
270+
env,
271+
locals,
272+
shared,
273+
seen,
274+
block.instructions.at(-1)!.lvalue.identifier.id
275+
);
276+
emit(
262277
env,
263278
locals,
264279
shared,
265-
seen,
280+
nextInstructions,
266281
block.instructions.at(-1)!.lvalue.identifier.id
267282
);
268-
emit(
269-
env,
270-
locals,
271-
shared,
272-
nextInstructions,
273-
block.instructions.at(-1)!.lvalue.identifier.id
274-
);
275-
}
276-
/*
277-
* Then emit the dependencies of the terminal operand. In many cases they will have
278-
* already been emitted in the previous step and this is a no-op.
279-
* TODO: sort the dependencies based on weight, like we do for other nodes. Not a big
280-
* deal though since most terminals have a single operand
281-
*/
282-
for (const operand of eachTerminalOperand(block.terminal)) {
283-
DEBUG && console.log(`(terminal operand)`);
284-
DEBUG && print(env, locals, shared, seen, operand.identifier.id);
285-
emit(env, locals, shared, nextInstructions, operand.identifier.id);
286-
}
287-
// Anything not emitted yet is globally reorderable
288-
for (const [id, node] of locals) {
289-
if (node.instruction == null) {
290-
continue;
291283
}
292-
CompilerError.invariant(
293-
node.instruction != null &&
284+
/*
285+
* Then emit the dependencies of the terminal operand. In many cases they will have
286+
* already been emitted in the previous step and this is a no-op.
287+
* TODO: sort the dependencies based on weight, like we do for other nodes. Not a big
288+
* deal though since most terminals have a single operand
289+
*/
290+
for (const operand of eachTerminalOperand(block.terminal)) {
291+
DEBUG && console.log(`(terminal operand)`);
292+
DEBUG && print(env, locals, shared, seen, operand.identifier.id);
293+
emit(env, locals, shared, nextInstructions, operand.identifier.id);
294+
}
295+
// Anything not emitted yet is globally reorderable
296+
for (const [id, node] of locals) {
297+
if (node.instruction == null) {
298+
continue;
299+
}
300+
CompilerError.invariant(
301+
node.instruction != null &&
302+
getReoderability(node.instruction, references) ===
303+
Reorderability.Reorderable,
304+
{
305+
reason: `Expected all remaining instructions to be reorderable`,
306+
loc: node.instruction?.loc ?? block.terminal.loc,
307+
description:
308+
node.instruction != null
309+
? `Instruction [${node.instruction.id}] was not emitted yet but is not reorderable`
310+
: `Lvalue $${id} was not emitted yet but is not reorderable`,
311+
}
312+
);
313+
314+
DEBUG && console.log(`save shared: $${id}`);
315+
shared.set(id, node);
316+
}
317+
} else {
318+
/**
319+
* If this is not a value block, then the order within the block doesn't matter
320+
* and we can optimize more. The observation is that blocks often have instructions
321+
* such as:
322+
*
323+
* ```
324+
* t$0 = nonreorderable
325+
* t$1 = nonreorderable <-- this gets in the way of merging t$0 and t$2
326+
* t$2 = reorderable deps[ t$0 ]
327+
* return t$2
328+
* ```
329+
*
330+
* Ie where there is some pair of nonreorderable+reorderable values, with some intervening
331+
* also non-reorderable instruction. If we emit all non-reorderable instructions first,
332+
* then we'll keep the original order. But reordering instructions don't just mean moving
333+
* them later: we can also move then _earlier_. By starting from terminal operands, we
334+
* end up emitting:
335+
*
336+
* ```
337+
* t$0 = nonreorderable // dep of t$2
338+
* t$2 = reorderable deps[ t$0 ]
339+
* t$1 = nonreorderable <-- not in the way of merging anymore!
340+
* return t$2
341+
* ```
342+
*
343+
* Ie all nonreorderable transitive deps of the terminal operands will get emitted first,
344+
* but we'll be able to intersperse the depending reorderable instructions in between
345+
* them in a way that works better with scope merging.
346+
*/
347+
for (const operand of eachTerminalOperand(block.terminal)) {
348+
DEBUG && console.log(`(terminal operand)`);
349+
DEBUG && print(env, locals, shared, seen, operand.identifier.id);
350+
emit(env, locals, shared, nextInstructions, operand.identifier.id);
351+
}
352+
// Anything not emitted yet is globally reorderable
353+
for (const id of Array.from(locals.keys()).reverse()) {
354+
const node = locals.get(id);
355+
if (node === undefined) {
356+
continue;
357+
}
358+
if (
359+
node.instruction !== null &&
294360
getReoderability(node.instruction, references) ===
295-
Reorderability.Reorderable,
296-
{
297-
reason: `Expected all remaining instructions to be reorderable`,
298-
loc: node.instruction?.loc ?? block.terminal.loc,
299-
description:
300-
node.instruction != null
301-
? `Instruction [${node.instruction.id}] was not emitted yet but is not reorderable`
302-
: `Lvalue $${id} was not emitted yet but is not reorderable`,
361+
Reorderability.Reorderable
362+
) {
363+
DEBUG && console.log(`save shared: $${id}`);
364+
shared.set(id, node);
365+
} else {
366+
DEBUG && console.log("leftover");
367+
DEBUG && print(env, locals, shared, seen, id);
368+
emit(env, locals, shared, nextInstructions, id);
303369
}
304-
);
305-
DEBUG && console.log(`save shared: $${id}`);
306-
shared.set(id, node);
370+
}
307371
}
308372

309373
block.instructions = nextInstructions;
@@ -432,7 +496,6 @@ function getReoderability(
432496
range !== undefined &&
433497
range.end === range.start // this LoadLocal is used exactly once
434498
) {
435-
console.log(`reorderable: ${name.value}`);
436499
return Reorderability.Reorderable;
437500
}
438501
}

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-computed-load.expect.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ function component(a) {
1919
import { c as _c } from "react/compiler-runtime";
2020
function component(a) {
2121
const $ = _c(2);
22-
let x;
22+
let t0;
2323
if ($[0] !== a) {
24-
x = { a };
24+
const x = { a };
2525
const y = {};
2626

27+
t0 = x;
2728
y.x = x.a;
2829
mutate(y);
2930
$[0] = a;
30-
$[1] = x;
31+
$[1] = t0;
3132
} else {
32-
x = $[1];
33+
t0 = $[1];
3334
}
34-
return x;
35+
return t0;
3536
}
3637

3738
```

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-nested-member-path-mutate.expect.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ function component() {
2020
import { c as _c } from "react/compiler-runtime";
2121
function component() {
2222
const $ = _c(1);
23-
let x;
23+
let t0;
2424
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
2525
const z = [];
2626
const y = {};
2727
y.z = z;
28-
x = {};
28+
const x = {};
2929
x.y = y;
30+
31+
t0 = x;
3032
mutate(x.y.z);
31-
$[0] = x;
33+
$[0] = t0;
3234
} else {
33-
x = $[0];
35+
t0 = $[0];
3436
}
35-
return x;
37+
return t0;
3638
}
3739

3840
```

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ export const FIXTURE_ENTRYPOINT = {
2525
import { c as _c } from "react/compiler-runtime";
2626
function component() {
2727
const $ = _c(1);
28-
let x;
28+
let t0;
2929
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
3030
const z = [];
3131
const y = {};
3232
y.z = z;
33-
x = {};
33+
const x = {};
34+
35+
t0 = x;
3436
x.y = y;
35-
$[0] = x;
37+
$[0] = t0;
3638
} else {
37-
x = $[0];
39+
t0 = $[0];
3840
}
39-
return x;
41+
return t0;
4042
}
4143

4244
export const FIXTURE_ENTRYPOINT = {

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ import { Stringify, identity, makeArray, mutate } from "shared-runtime";
5252
* handles this correctly.
5353
*/
5454
function Foo(t0) {
55-
const $ = _c(4);
55+
const $ = _c(3);
5656
const { cond1, cond2 } = t0;
57-
const arr = makeArray({ a: 2 }, 2, []);
5857
let t1;
59-
if ($[0] !== cond1 || $[1] !== cond2 || $[2] !== arr) {
58+
if ($[0] !== cond1 || $[1] !== cond2) {
59+
const arr = makeArray({ a: 2 }, 2, []);
60+
6061
t1 = cond1 ? (
6162
<>
6263
<div>{identity("foo")}</div>
@@ -65,10 +66,9 @@ function Foo(t0) {
6566
) : null;
6667
$[0] = cond1;
6768
$[1] = cond2;
68-
$[2] = arr;
69-
$[3] = t1;
69+
$[2] = t1;
7070
} else {
71-
t1 = $[3];
71+
t1 = $[2];
7272
}
7373
return t1;
7474
}

Diff for: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect.expect.md

+23-29
Original file line numberDiff line numberDiff line change
@@ -39,57 +39,51 @@ import { useEffect, useState } from "react";
3939
let someGlobal = {};
4040

4141
function Component() {
42-
const $ = _c(7);
42+
const $ = _c(6);
4343
const [state, setState] = useState(someGlobal);
4444
let t0;
45+
let t1;
4546
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
46-
t0 = () => {
47+
const setGlobal = () => {
4748
someGlobal.value = true;
4849
};
49-
$[0] = t0;
50-
} else {
51-
t0 = $[0];
52-
}
53-
const setGlobal = t0;
54-
let t1;
55-
let t2;
56-
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
57-
t1 = () => {
50+
51+
t0 = () => {
5852
setGlobal();
5953
};
60-
t2 = [];
54+
t1 = [];
55+
$[0] = t0;
6156
$[1] = t1;
62-
$[2] = t2;
6357
} else {
58+
t0 = $[0];
6459
t1 = $[1];
65-
t2 = $[2];
6660
}
67-
useEffect(t1, t2);
61+
useEffect(t0, t1);
62+
let t2;
6863
let t3;
69-
let t4;
70-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
71-
t3 = () => {
64+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
65+
t2 = () => {
7266
setState(someGlobal.value);
7367
};
74-
t4 = [someGlobal];
68+
t3 = [someGlobal];
69+
$[2] = t2;
7570
$[3] = t3;
76-
$[4] = t4;
7771
} else {
72+
t2 = $[2];
7873
t3 = $[3];
79-
t4 = $[4];
8074
}
81-
useEffect(t3, t4);
75+
useEffect(t2, t3);
8276

83-
const t5 = String(state);
84-
let t6;
85-
if ($[5] !== t5) {
86-
t6 = <div>{t5}</div>;
77+
const t4 = String(state);
78+
let t5;
79+
if ($[4] !== t4) {
80+
t5 = <div>{t4}</div>;
81+
$[4] = t4;
8782
$[5] = t5;
88-
$[6] = t6;
8983
} else {
90-
t6 = $[6];
84+
t5 = $[5];
9185
}
92-
return t6;
86+
return t5;
9387
}
9488

9589
export const FIXTURE_ENTRYPOINT = {

0 commit comments

Comments
 (0)