@samitouri / QOS-React-2 / commits / fa913b423e

Distinguish internal invariant passes from user code validation

Distinguishes between two types of "validation" passes: - Passes which assert the validity of the HIR. These remain in HIR/ but are renamed "assertFoo". - Passes which validate that the user code is correct. These move to Validation/.

Joe Savona committed Jun 20, 2023 at 15:16 UTC fa913b423e5b039cb948464ccd2aef96011b6006
10 files changed +52 -34
compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts
+13 -11
@@ -9,15 +9,11 @@ import { NodePath } from "@babel/traverse";
9 import * as t from "@babel/types";
10 import {
11 HIRFunction,
12 + ReactiveFunction,
13 + assertConsistentIdentifiers,
14 + assertTerminalSuccessorsExist,
15 lower,
16 mergeConsecutiveBlocks,
14 - ReactiveFunction,
15 - validateConsistentIdentifiers,
16 - validateFrozenLambdas,
17 - validateHooksUsage,
18 - validateNoRefAccessInRender,
19 - validateTerminalSuccessors,
20 - validateUnconditionalHooks,
17 } from "../HIR";
18 import { Environment, EnvironmentConfig } from "../HIR/Environment";
19 import { findContextIdentifiers } from "../HIR/FindContextIdentifiers";
@@ -45,8 +41,8 @@ import {
41 pruneAllReactiveScopes,
42 pruneNonEscapingScopes,
43 pruneNonReactiveDependencies,
48 - pruneUnusedLabels,
44 pruneUnusedLValues,
45 + pruneUnusedLabels,
46 pruneUnusedScopes,
47 renameVariables,
48 } from "../ReactiveScopes";
@@ -54,6 +50,12 @@ import { eliminateRedundantPhi, enterSSA, leaveSSA } from "../SSA";
50 import { inferTypes } from "../TypeInference";
51 import { logHIRFunction, logReactiveFunction } from "../Utils/logger";
52 import { assertExhaustive } from "../Utils/utils";
53 +import {
54 + validateFrozenLambdas,
55 + validateHooksUsage,
56 + validateNoRefAccessInRender,
57 + validateUnconditionalHooks,
58 +} from "../Validation";
59
60 export type CompilerPipelineValue =
61 | { kind: "ast"; name: string; value: t.FunctionDeclaration }
@@ -78,8 +80,8 @@ export function* run(
80 mergeConsecutiveBlocks(hir);
81 yield log({ kind: "hir", name: "MergeConsecutiveBlocks", value: hir });
82
81 - validateConsistentIdentifiers(hir);
82 - validateTerminalSuccessors(hir);
83 + assertConsistentIdentifiers(hir);
84 + assertTerminalSuccessorsExist(hir);
85
86 enterSSA(hir);
87 yield log({ kind: "hir", name: "SSA", value: hir });
@@ -87,7 +89,7 @@ export function* run(
89 eliminateRedundantPhi(hir);
90 yield log({ kind: "hir", name: "EliminateRedundantPhi", value: hir });
91
90 - validateConsistentIdentifiers(hir);
92 + assertConsistentIdentifiers(hir);
93
94 constantPropagation(hir);
95 yield log({ kind: "hir", name: "ConstantPropagation", value: hir });
compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertConsistentIdentifiers.ts renamed
+1 -1
@@ -24,7 +24,7 @@ import {
24 * Validation pass to check that there is a 1:1 mapping between Identifier objects and IdentifierIds,
25 * ie there can only be one Identifier instance per IdentifierId.
26 */
27 -export function validateConsistentIdentifiers(fn: HIRFunction): void {
27 +export function assertConsistentIdentifiers(fn: HIRFunction): void {
28 const identifiers: Identifiers = new Map();
29 const assignments: Set<IdentifierId> = new Set();
30 for (const [, block] of fn.body.blocks) {
compiler/forget/packages/babel-plugin-react-forget/src/HIR/AssertTerminalSuccessorsExist.ts renamed
+1 -1
@@ -10,7 +10,7 @@ import { GeneratedSource, HIRFunction } from "./HIR";
10 import { printTerminal } from "./PrintHIR";
11 import { mapTerminalSuccessors } from "./visitors";
12
13 -export function validateTerminalSuccessors(fn: HIRFunction): void {
13 +export function assertTerminalSuccessorsExist(fn: HIRFunction): void {
14 for (const [, block] of fn.body.blocks) {
15 mapTerminalSuccessors(block.terminal, (successor) => {
16 if (!fn.body.blocks.has(successor)) {
compiler/forget/packages/babel-plugin-react-forget/src/HIR/index.ts
+2 -6
@@ -5,6 +5,8 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 +export { assertConsistentIdentifiers } from "./AssertConsistentIdentifiers";
9 +export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist";
10 export { lower } from "./BuildHIR";
11 export { computeDominatorTree, computePostDominatorTree } from "./Dominator";
12 export { Environment, Hook } from "./Environment";
@@ -17,9 +19,3 @@ export {
19 } from "./HIRBuilder";
20 export { mergeConsecutiveBlocks } from "./MergeConsecutiveBlocks";
21 export { printFunction, printHIR } from "./PrintHIR";
20 -export { validateConsistentIdentifiers } from "./ValidateConsistentIdentifiers";
21 -export { validateFrozenLambdas } from "./ValidateFrozenLambdas";
22 -export { validateHooksUsage } from "./ValidateHooksUsage";
23 -export { validateNoRefAccessInRender } from "./ValidateNoRefAccesInRender";
24 -export { validateTerminalSuccessors } from "./ValidateTerminalSuccessors";
25 -export { validateUnconditionalHooks } from "./ValidateUnconditionalHooks";
compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts
+6 -6
@@ -14,15 +14,15 @@ import {
14 Instruction,
15 InstructionValue,
16 LoadGlobal,
17 + Place,
18 + Primitive,
19 + assertConsistentIdentifiers,
20 + assertTerminalSuccessorsExist,
21 markInstructionIds,
22 markPredecessors,
23 mergeConsecutiveBlocks,
20 - Place,
21 - Primitive,
24 removeUnreachableFallthroughs,
25 reversePostorderBlocks,
24 - validateConsistentIdentifiers,
25 - validateTerminalSuccessors,
26 } from "../HIR";
27 import {
28 removeDeadDoWhileStatements,
@@ -81,8 +81,8 @@ function constantPropagationImpl(fn: HIRFunction, constants: Constants): void {
81 // consecutively
82 mergeConsecutiveBlocks(fn);
83
84 - validateConsistentIdentifiers(fn);
85 - validateTerminalSuccessors(fn);
84 + assertConsistentIdentifiers(fn);
85 + assertTerminalSuccessorsExist(fn);
86 }
87 }
88
compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateFrozenLambdas.ts renamed
+5 -2
@@ -18,8 +18,11 @@ import {
18 Place,
19 isRefValueType,
20 isUseRefType,
21 -} from "./HIR";
22 -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors";
21 +} from "../HIR/HIR";
22 +import {
23 + eachInstructionValueOperand,
24 + eachTerminalOperand,
25 +} from "../HIR/visitors";
26
27 /**
28 * Various APIs in React take ownership of the values passed to them, such that it is invalid
compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateHooksUsage.ts renamed
+5 -2
@@ -10,9 +10,12 @@ import {
10 CompilerErrorDetail,
11 ErrorSeverity,
12 } from "../CompilerError";
13 +import { HIRFunction, IdentifierId, Place, getHookKind } from "../HIR/HIR";
14 +import {
15 + eachInstructionValueOperand,
16 + eachTerminalOperand,
17 +} from "../HIR/visitors";
18 import { hasBackEdge } from "../Optimization/DeadCodeElimination";
14 -import { HIRFunction, IdentifierId, Place, getHookKind } from "./HIR";
15 -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors";
19
20 /**
21 * Validates that the function honors the [Rules of Hooks](https://react.dev/warnings/invalid-hook-call-warning)
compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateNoRefAccesInRender.ts renamed
+6 -3
@@ -10,9 +10,12 @@ import {
10 CompilerErrorDetail,
11 ErrorSeverity,
12 } from "../CompilerError";
13 -import { HIRFunction, Place, isRefValueType, isUseRefType } from "./HIR";
14 -import { printPlace } from "./PrintHIR";
15 -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors";
13 +import { HIRFunction, Place, isRefValueType, isUseRefType } from "../HIR/HIR";
14 +import { printPlace } from "../HIR/PrintHIR";
15 +import {
16 + eachInstructionValueOperand,
17 + eachTerminalOperand,
18 +} from "../HIR/visitors";
19
20 /**
21 * Validates that ref values (the `current` property) are not accessed during render.
compiler/forget/packages/babel-plugin-react-forget/src/Validation/ValidateUnconditionalHooks.ts renamed
+2 -2
@@ -10,10 +10,10 @@ import {
10 CompilerErrorDetail,
11 ErrorSeverity,
12 } from "../CompilerError";
13 +import { PostDominator, computePostDominatorTree } from "../HIR/Dominator";
14 +import { BlockId, HIRFunction, getHookKind } from "../HIR/HIR";
15 import { findBlocksWithBackEdges } from "../Optimization/DeadCodeElimination";
16 import { Err, Ok, Result } from "../Utils/Result";
15 -import { PostDominator, computePostDominatorTree } from "./Dominator";
16 -import { BlockId, HIRFunction, getHookKind } from "./HIR";
17
18 /**
19 * Validates that the function honors the [Rules of Hooks](https://react.dev/warnings/invalid-hook-call-warning)
compiler/forget/packages/babel-plugin-react-forget/src/Validation/index.ts new
+11
@@ -0,0 +1,11 @@
1 +/**
2 + * Copyright (c) Meta Platforms, Inc. and affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +
8 +export { validateFrozenLambdas } from "./ValidateFrozenLambdas";
9 +export { validateHooksUsage } from "./ValidateHooksUsage";
10 +export { validateNoRefAccessInRender } from "./ValidateNoRefAccesInRender";
11 +export { validateUnconditionalHooks } from "./ValidateUnconditionalHooks";