-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] General-purpose function outlining
Implements general-purpose function outlining. Specifically, anonymous function expressions which have no dependencies/context variables are extracted into named top-level functions. The original function expression is replaced with a `LoadGlobal` of the generated name. Note that the architecture is designed to allow very general purpose forms of outlining, though we currently are very conservative in what we outline. Specifically, the outlining allows annotating functions with an optional ReactiveFunctionType, which if set will cause the outlined function to get compiled as that type. So we could for example outline a helper hook or helper component, set the type, and then have the hook/component get memoized as well. For now though we just outline with no type set, and generate the function as-is without running it through compilation. ghstack-source-id: 8d7acc1c5cfd8c26b6ce85b27fbe8bbe8865d579 Pull Request resolved: #30331
- Loading branch information
1 parent
f3229a1
commit 12cf32c
Showing
72 changed files
with
745 additions
and
750 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineFunctions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { CompilerError } from ".."; | ||
import { HIRFunction } from "../HIR"; | ||
|
||
export function outlineFunctions(fn: HIRFunction): void { | ||
for (const [, block] of fn.body.blocks) { | ||
for (const instr of block.instructions) { | ||
const { value } = instr; | ||
|
||
if ( | ||
value.kind === "FunctionExpression" || | ||
value.kind === "ObjectMethod" | ||
) { | ||
// Recurse in case there are inner functions which can be outlined | ||
outlineFunctions(value.loweredFunc.func); | ||
} | ||
|
||
if ( | ||
value.kind === "FunctionExpression" && | ||
value.loweredFunc.dependencies.length === 0 && | ||
value.loweredFunc.func.context.length === 0 && | ||
// TODO: handle outlining named functions | ||
value.loweredFunc.func.id === null | ||
) { | ||
const loweredFunc = value.loweredFunc.func; | ||
|
||
const id = fn.env.generateGloballyUniqueIdentifierName(loweredFunc.id); | ||
loweredFunc.id = id.value; | ||
|
||
fn.env.outlineFunction(loweredFunc, null); | ||
instr.value = { | ||
kind: "LoadGlobal", | ||
binding: { | ||
kind: "Global", | ||
name: id.value, | ||
}, | ||
loc: value.loc, | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.