Skip to content

[compiler] Instruction reordering #29863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -265,6 +265,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