Skip to content

Commit

Permalink
[compiler] Instruction reordering
Browse files Browse the repository at this point in the history
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: c54a806cad7aefba4ac1876c9fd9b25f9177e95a
Pull Request resolved: #29863
  • Loading branch information
josephsavona committed Jun 14, 2024
1 parent ed8dd20 commit 59e73d9
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
deadCodeElimination,
pruneMaybeThrows,
} from "../Optimization";
import { instructionReordering } from "../Optimization/InstructionReordering";
import {
CodegenFunction,
alignObjectMethodScopes,
Expand Down Expand Up @@ -204,6 +205,11 @@ function* runWithEnvironment(
deadCodeElimination(hir);
yield log({ kind: "hir", name: "DeadCodeElimination", value: hir });

if (env.config.enableInstructionReordering) {
instructionReordering(hir);
yield log({ kind: "hir", name: "InstructionReordering", value: hir });
}

pruneMaybeThrows(hir);
yield log({ kind: "hir", name: "PruneMaybeThrows", value: hir });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ const EnvironmentConfigSchema = z.object({

enableEmitHookGuards: ExternalFunctionSchema.nullish(),

/**
* Enable instruction reordering. See InstructionReordering.ts for the details
* of the approach.
*/
enableInstructionReordering: z.boolean().default(false),

/*
* Enables instrumentation codegen. This emits a dev-mode only call to an
* instrumentation function, for components and hooks that Forget compiles.
Expand Down
22 changes: 22 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ export type HIR = {
* statements and not implicit exceptions which may occur.
*/
export type BlockKind = "block" | "value" | "loop" | "sequence" | "catch";

/**
* Returns true for "block" and "catch" block kinds which correspond to statements
* in the source, including BlockStatement, CatchStatement.
*
* Inverse of isExpressionBlockKind()
*/
export function isStatementBlockKind(kind: BlockKind): boolean {
return kind === "block" || kind === "catch";
}

/**
* Returns true for "value", "loop", and "sequence" block kinds which correspond to
* expressions in the source, such as ConditionalExpression, LogicalExpression, loop
* initializer/test/updaters, etc
*
* Inverse of isStatementBlockKind()
*/
export function isExpressionBlockKind(kind: BlockKind): boolean {
return !isStatementBlockKind(kind);
}

export type BasicBlock = {
kind: BlockKind;
id: BlockId;
Expand Down
Loading

0 comments on commit 59e73d9

Please sign in to comment.