Skip to content

Commit 59e73d9

Browse files
committed
[compiler] Instruction reordering
Adds a pass just after DCE to reorder safely reorderable instructions (jsx, primitives, globals) closer to where they are used, to allow other optimization passes to be more effective. Notably, the reordering allows scope merging to be more effective, since that pass relies on two scopes not having intervening instructions — in many cases we can now reorder such instructions out of the way and unlock merging, as demonstrated in the changed fixtures. The algorithm itself is described in the docblock. note: This is a cleaned up version of #29579 that is ready for review. ghstack-source-id: c54a806 Pull Request resolved: #29863
1 parent ed8dd20 commit 59e73d9

File tree

10 files changed

+543
-2
lines changed

10 files changed

+543
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
deadCodeElimination,
4242
pruneMaybeThrows,
4343
} from "../Optimization";
44+
import { instructionReordering } from "../Optimization/InstructionReordering";
4445
import {
4546
CodegenFunction,
4647
alignObjectMethodScopes,
@@ -204,6 +205,11 @@ function* runWithEnvironment(
204205
deadCodeElimination(hir);
205206
yield log({ kind: "hir", name: "DeadCodeElimination", value: hir });
206207

208+
if (env.config.enableInstructionReordering) {
209+
instructionReordering(hir);
210+
yield log({ kind: "hir", name: "InstructionReordering", value: hir });
211+
}
212+
207213
pruneMaybeThrows(hir);
208214
yield log({ kind: "hir", name: "PruneMaybeThrows", value: hir });
209215

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ const EnvironmentConfigSchema = z.object({
277277

278278
enableEmitHookGuards: ExternalFunctionSchema.nullish(),
279279

280+
/**
281+
* Enable instruction reordering. See InstructionReordering.ts for the details
282+
* of the approach.
283+
*/
284+
enableInstructionReordering: z.boolean().default(false),
285+
280286
/*
281287
* Enables instrumentation codegen. This emits a dev-mode only call to an
282288
* instrumentation function, for components and hooks that Forget compiles.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,28 @@ export type HIR = {
335335
* statements and not implicit exceptions which may occur.
336336
*/
337337
export type BlockKind = "block" | "value" | "loop" | "sequence" | "catch";
338+
339+
/**
340+
* Returns true for "block" and "catch" block kinds which correspond to statements
341+
* in the source, including BlockStatement, CatchStatement.
342+
*
343+
* Inverse of isExpressionBlockKind()
344+
*/
345+
export function isStatementBlockKind(kind: BlockKind): boolean {
346+
return kind === "block" || kind === "catch";
347+
}
348+
349+
/**
350+
* Returns true for "value", "loop", and "sequence" block kinds which correspond to
351+
* expressions in the source, such as ConditionalExpression, LogicalExpression, loop
352+
* initializer/test/updaters, etc
353+
*
354+
* Inverse of isStatementBlockKind()
355+
*/
356+
export function isExpressionBlockKind(kind: BlockKind): boolean {
357+
return !isStatementBlockKind(kind);
358+
}
359+
338360
export type BasicBlock = {
339361
kind: BlockKind;
340362
id: BlockId;

0 commit comments

Comments
 (0)