@samitouri / QOS-React / commits / fee423e083

[compiler] Handle earlier creation of early return on scopes

I'm experimenting with a new pass that sometimes creates scopes with early returns earlier in the pipeline, but there are a few passes that assume that can't happen. This PR is updating those passes just to be more resilient to help unblock experimentation. ghstack-source-id: a9e348181ddad1a1e936ef023b5d5ee44aaf3d8c Pull Request resolved: https://github.com/facebook/react/pull/30333

Joe Savona committed Jul 15, 2024 at 17:32 UTC fee423e083eccc19d69ecc3db4483c6740db7a75
2 files changed +14 -4
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PropagateEarlyReturns.ts
+8 -2
@@ -24,8 +24,6 @@ import { EARLY_RETURN_SENTINEL } from "./CodegenReactiveFunction";
24 import { ReactiveFunctionTransform, Transformed } from "./visitors";
25
26 /**
27 - * TODO: Actualy propagate early return information, for now we throw a Todo bailout.
28 - *
27 * This pass ensures that reactive blocks honor the control flow behavior of the
28 * original code including early return semantics. Specifically, if a reactive
29 * scope early returned during the previous execution and the inputs to that block
@@ -135,6 +133,14 @@ class Transform extends ReactiveFunctionTransform<State> {
133 scopeBlock: ReactiveScopeBlock,
134 parentState: State
135 ): void {
136 + /**
137 + * Exit early if an earlier pass has already created an early return,
138 + * which may happen in alternate compiler configurations.
139 + */
140 + if (scopeBlock.scope.earlyReturnValue !== null) {
141 + return;
142 + }
143 +
144 const innerState: State = {
145 withinReactiveScope: true,
146 earlyReturnValue: parentState.earlyReturnValue,
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonEscapingScopes.ts
+6 -2
@@ -932,10 +932,14 @@ class PruneScopesTransform extends ReactiveFunctionTransform<
932 * is early-returned from within the scope. For now we intentionaly keep
933 * these scopes, and let them get pruned later by PruneUnusedScopes
934 * _after_ handling the early-return case in PropagateEarlyReturns.
935 + *
936 + * Also keep the scope if an early return was created by some earlier pass,
937 + * which may happen in alternate compiler configurations.
938 */
939 if (
937 - scopeBlock.scope.declarations.size === 0 &&
938 - scopeBlock.scope.reassignments.size === 0
940 + (scopeBlock.scope.declarations.size === 0 &&
941 + scopeBlock.scope.reassignments.size === 0) ||
942 + scopeBlock.scope.earlyReturnValue !== null
943 ) {
944 return { kind: "keep" };
945 }