diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts index 4a75131ce0..11648a5454 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts @@ -9,15 +9,11 @@ import { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import { HIRFunction, + ReactiveFunction, + assertConsistentIdentifiers, + assertTerminalSuccessorsExist, lower, mergeConsecutiveBlocks, - ReactiveFunction, - validateConsistentIdentifiers, - validateFrozenLambdas, - validateHooksUsage, - validateNoRefAccessInRender, - validateTerminalSuccessors, - validateUnconditionalHooks, } from "../HIR"; import { Environment, EnvironmentConfig } from "../HIR/Environment"; import { findContextIdentifiers } from "../HIR/FindContextIdentifiers"; @@ -45,8 +41,8 @@ import { pruneAllReactiveScopes, pruneNonEscapingScopes, pruneNonReactiveDependencies, - pruneUnusedLabels, pruneUnusedLValues, + pruneUnusedLabels, pruneUnusedScopes, renameVariables, } from "../ReactiveScopes"; @@ -54,6 +50,12 @@ import { eliminateRedundantPhi, enterSSA, leaveSSA } from "../SSA"; import { inferTypes } from "../TypeInference"; import { logHIRFunction, logReactiveFunction } from "../Utils/logger"; import { assertExhaustive } from "../Utils/utils"; +import { + validateFrozenLambdas, + validateHooksUsage, + validateNoRefAccessInRender, + validateUnconditionalHooks, +} from "../Validation"; export type CompilerPipelineValue = | { kind: "ast"; name: string; value: t.FunctionDeclaration } @@ -78,8 +80,8 @@ export function* run( mergeConsecutiveBlocks(hir); yield log({ kind: "hir", name: "MergeConsecutiveBlocks", value: hir }); - validateConsistentIdentifiers(hir); - validateTerminalSuccessors(hir); + assertConsistentIdentifiers(hir); + assertTerminalSuccessorsExist(hir); enterSSA(hir); yield log({ kind: "hir", name: "SSA", value: hir }); @@ -87,7 +89,7 @@ export function* run( eliminateRedundantPhi(hir); yield log({ kind: "hir", name: "EliminateRedundantPhi", value: hir }); - validateConsistentIdentifiers(hir); + assertConsistentIdentifiers(hir); constantPropagation(hir); yield log({ kind: "hir", name: "ConstantPropagation", value: hir }); diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateConsistentIdentifiers.ts b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertConsistentIdentifiers.ts similarity index 97% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateConsistentIdentifiers.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertConsistentIdentifiers.ts index 3b73e9e2e6..d5807f1584 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateConsistentIdentifiers.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertConsistentIdentifiers.ts @@ -24,7 +24,7 @@ import { * Validation pass to check that there is a 1:1 mapping between Identifier objects and IdentifierIds, * ie there can only be one Identifier instance per IdentifierId. */ -export function validateConsistentIdentifiers(fn: HIRFunction): void { +export function assertConsistentIdentifiers(fn: HIRFunction): void { const identifiers: Identifiers = new Map(); const assignments: Set = new Set(); for (const [, block] of fn.body.blocks) { diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateTerminalSuccessors.ts b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertTerminalSuccessorsExist.ts similarity index 92% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateTerminalSuccessors.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertTerminalSuccessorsExist.ts index 56bcd1faa1..92dd4edad9 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateTerminalSuccessors.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertTerminalSuccessorsExist.ts @@ -10,7 +10,7 @@ import { GeneratedSource, HIRFunction } from "./HIR"; import { printTerminal } from "./PrintHIR"; import { mapTerminalSuccessors } from "./visitors"; -export function validateTerminalSuccessors(fn: HIRFunction): void { +export function assertTerminalSuccessorsExist(fn: HIRFunction): void { for (const [, block] of fn.body.blocks) { mapTerminalSuccessors(block.terminal, (successor) => { if (!fn.body.blocks.has(successor)) { diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/index.ts b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/index.ts index 04dcd27e5e..f014f44415 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/index.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/index.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +export { assertConsistentIdentifiers } from "./AssertConsistentIdentifiers"; +export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist"; export { lower } from "./BuildHIR"; export { computeDominatorTree, computePostDominatorTree } from "./Dominator"; export { Environment, Hook } from "./Environment"; @@ -17,9 +19,3 @@ export { } from "./HIRBuilder"; export { mergeConsecutiveBlocks } from "./MergeConsecutiveBlocks"; export { printFunction, printHIR } from "./PrintHIR"; -export { validateConsistentIdentifiers } from "./ValidateConsistentIdentifiers"; -export { validateFrozenLambdas } from "./ValidateFrozenLambdas"; -export { validateHooksUsage } from "./ValidateHooksUsage"; -export { validateNoRefAccessInRender } from "./ValidateNoRefAccesInRender"; -export { validateTerminalSuccessors } from "./ValidateTerminalSuccessors"; -export { validateUnconditionalHooks } from "./ValidateUnconditionalHooks"; diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts index ecd9a548f1..35dad6b529 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts @@ -14,15 +14,15 @@ import { Instruction, InstructionValue, LoadGlobal, + Place, + Primitive, + assertConsistentIdentifiers, + assertTerminalSuccessorsExist, markInstructionIds, markPredecessors, mergeConsecutiveBlocks, - Place, - Primitive, removeUnreachableFallthroughs, reversePostorderBlocks, - validateConsistentIdentifiers, - validateTerminalSuccessors, } from "../HIR"; import { removeDeadDoWhileStatements, @@ -81,8 +81,8 @@ function constantPropagationImpl(fn: HIRFunction, constants: Constants): void { // consecutively mergeConsecutiveBlocks(fn); - validateConsistentIdentifiers(fn); - validateTerminalSuccessors(fn); + assertConsistentIdentifiers(fn); + assertTerminalSuccessorsExist(fn); } } diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateFrozenLambdas.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateFrozenLambdas.ts similarity index 97% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateFrozenLambdas.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateFrozenLambdas.ts index c2d6aed9d0..cc6545b44b 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateFrozenLambdas.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateFrozenLambdas.ts @@ -18,8 +18,11 @@ import { Place, isRefValueType, isUseRefType, -} from "./HIR"; -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors"; +} from "../HIR/HIR"; +import { + eachInstructionValueOperand, + eachTerminalOperand, +} from "../HIR/visitors"; /** * Various APIs in React take ownership of the values passed to them, such that it is invalid diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateHooksUsage.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateHooksUsage.ts similarity index 96% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateHooksUsage.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateHooksUsage.ts index c1a588d126..797344b20e 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateHooksUsage.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateHooksUsage.ts @@ -10,9 +10,12 @@ import { CompilerErrorDetail, ErrorSeverity, } from "../CompilerError"; +import { HIRFunction, IdentifierId, Place, getHookKind } from "../HIR/HIR"; +import { + eachInstructionValueOperand, + eachTerminalOperand, +} from "../HIR/visitors"; import { hasBackEdge } from "../Optimization/DeadCodeElimination"; -import { HIRFunction, IdentifierId, Place, getHookKind } from "./HIR"; -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors"; /** * Validates that the function honors the [Rules of Hooks](https://react.dev/warnings/invalid-hook-call-warning) diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateNoRefAccesInRender.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateNoRefAccesInRender.ts similarity index 96% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateNoRefAccesInRender.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateNoRefAccesInRender.ts index 2f9cd040e2..148a789606 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateNoRefAccesInRender.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateNoRefAccesInRender.ts @@ -10,9 +10,12 @@ import { CompilerErrorDetail, ErrorSeverity, } from "../CompilerError"; -import { HIRFunction, Place, isRefValueType, isUseRefType } from "./HIR"; -import { printPlace } from "./PrintHIR"; -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors"; +import { HIRFunction, Place, isRefValueType, isUseRefType } from "../HIR/HIR"; +import { printPlace } from "../HIR/PrintHIR"; +import { + eachInstructionValueOperand, + eachTerminalOperand, +} from "../HIR/visitors"; /** * Validates that ref values (the `current` property) are not accessed during render. diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateUnconditionalHooks.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateUnconditionalHooks.ts similarity index 96% rename from compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateUnconditionalHooks.ts rename to compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateUnconditionalHooks.ts index 77d5656b87..cbc07d5d05 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/ValidateUnconditionalHooks.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateUnconditionalHooks.ts @@ -10,10 +10,10 @@ import { CompilerErrorDetail, ErrorSeverity, } from "../CompilerError"; +import { PostDominator, computePostDominatorTree } from "../HIR/Dominator"; +import { BlockId, HIRFunction, getHookKind } from "../HIR/HIR"; import { findBlocksWithBackEdges } from "../Optimization/DeadCodeElimination"; import { Err, Ok, Result } from "../Utils/Result"; -import { PostDominator, computePostDominatorTree } from "./Dominator"; -import { BlockId, HIRFunction, getHookKind } from "./HIR"; /** * Validates that the function honors the [Rules of Hooks](https://react.dev/warnings/invalid-hook-call-warning) diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Validation/index.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/index.ts new file mode 100644 index 0000000000..e4791235cb --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Validation/index.ts @@ -0,0 +1,11 @@ +/** + * 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. + */ + +export { validateFrozenLambdas } from "./ValidateFrozenLambdas"; +export { validateHooksUsage } from "./ValidateHooksUsage"; +export { validateNoRefAccessInRender } from "./ValidateNoRefAccesInRender"; +export { validateUnconditionalHooks } from "./ValidateUnconditionalHooks";