Skip to content

Commit eaf6adb

Browse files
authored
[compiler][wip] Remove old mutation/aliasing implementation (#34028)
--- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34028). * #34029 * __->__ #34028
1 parent 6ffcac8 commit eaf6adb

File tree

36 files changed

+311
-4065
lines changed

36 files changed

+311
-4065
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ import {findContextIdentifiers} from '../HIR/FindContextIdentifiers';
3333
import {
3434
analyseFunctions,
3535
dropManualMemoization,
36-
inferMutableRanges,
3736
inferReactivePlaces,
38-
inferReferenceEffects,
3937
inlineImmediatelyInvokedFunctionExpressions,
4038
inferEffectDependencies,
4139
} from '../Inference';
@@ -100,7 +98,6 @@ import {outlineJSX} from '../Optimization/OutlineJsx';
10098
import {optimizePropsMethodCalls} from '../Optimization/OptimizePropsMethodCalls';
10199
import {transformFire} from '../Transform';
102100
import {validateNoImpureFunctionsInRender} from '../Validation/ValidateNoImpureFunctionsInRender';
103-
import {CompilerError} from '..';
104101
import {validateStaticComponents} from '../Validation/ValidateStaticComponents';
105102
import {validateNoFreezingKnownMutableFunctions} from '../Validation/ValidateNoFreezingKnownMutableFunctions';
106103
import {inferMutationAliasingEffects} from '../Inference/InferMutationAliasingEffects';
@@ -229,28 +226,14 @@ function runWithEnvironment(
229226
analyseFunctions(hir);
230227
log({kind: 'hir', name: 'AnalyseFunctions', value: hir});
231228

232-
if (!env.config.enableNewMutationAliasingModel) {
233-
const fnEffectErrors = inferReferenceEffects(hir);
234-
if (env.isInferredMemoEnabled) {
235-
if (fnEffectErrors.length > 0) {
236-
CompilerError.throw(fnEffectErrors[0]);
237-
}
238-
}
239-
log({kind: 'hir', name: 'InferReferenceEffects', value: hir});
240-
} else {
241-
const mutabilityAliasingErrors = inferMutationAliasingEffects(hir);
242-
log({kind: 'hir', name: 'InferMutationAliasingEffects', value: hir});
243-
if (env.isInferredMemoEnabled) {
244-
if (mutabilityAliasingErrors.isErr()) {
245-
throw mutabilityAliasingErrors.unwrapErr();
246-
}
229+
const mutabilityAliasingErrors = inferMutationAliasingEffects(hir);
230+
log({kind: 'hir', name: 'InferMutationAliasingEffects', value: hir});
231+
if (env.isInferredMemoEnabled) {
232+
if (mutabilityAliasingErrors.isErr()) {
233+
throw mutabilityAliasingErrors.unwrapErr();
247234
}
248235
}
249236

250-
if (!env.config.enableNewMutationAliasingModel) {
251-
validateLocalsNotReassignedAfterRender(hir);
252-
}
253-
254237
// Note: Has to come after infer reference effects because "dead" code may still affect inference
255238
deadCodeElimination(hir);
256239
log({kind: 'hir', name: 'DeadCodeElimination', value: hir});
@@ -263,20 +246,15 @@ function runWithEnvironment(
263246
pruneMaybeThrows(hir);
264247
log({kind: 'hir', name: 'PruneMaybeThrows', value: hir});
265248

266-
if (!env.config.enableNewMutationAliasingModel) {
267-
inferMutableRanges(hir);
268-
log({kind: 'hir', name: 'InferMutableRanges', value: hir});
269-
} else {
270-
const mutabilityAliasingErrors = inferMutationAliasingRanges(hir, {
271-
isFunctionExpression: false,
272-
});
273-
log({kind: 'hir', name: 'InferMutationAliasingRanges', value: hir});
274-
if (env.isInferredMemoEnabled) {
275-
if (mutabilityAliasingErrors.isErr()) {
276-
throw mutabilityAliasingErrors.unwrapErr();
277-
}
278-
validateLocalsNotReassignedAfterRender(hir);
249+
const mutabilityAliasingRangeErrors = inferMutationAliasingRanges(hir, {
250+
isFunctionExpression: false,
251+
});
252+
log({kind: 'hir', name: 'InferMutationAliasingRanges', value: hir});
253+
if (env.isInferredMemoEnabled) {
254+
if (mutabilityAliasingRangeErrors.isErr()) {
255+
throw mutabilityAliasingRangeErrors.unwrapErr();
279256
}
257+
validateLocalsNotReassignedAfterRender(hir);
280258
}
281259

282260
if (env.isInferredMemoEnabled) {
@@ -308,12 +286,7 @@ function runWithEnvironment(
308286
validateNoImpureFunctionsInRender(hir).unwrap();
309287
}
310288

311-
if (
312-
env.config.validateNoFreezingKnownMutableFunctions ||
313-
env.config.enableNewMutationAliasingModel
314-
) {
315-
validateNoFreezingKnownMutableFunctions(hir).unwrap();
316-
}
289+
validateNoFreezingKnownMutableFunctions(hir).unwrap();
317290
}
318291

319292
inferReactivePlaces(hir);

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,6 @@ export const EnvironmentConfigSchema = z.object({
250250
*/
251251
flowTypeProvider: z.nullable(z.function().args(z.string())).default(null),
252252

253-
/**
254-
* Enable a new model for mutability and aliasing inference
255-
*/
256-
enableNewMutationAliasingModel: z.boolean().default(true),
257-
258253
/**
259254
* Enables inference of optional dependency chains. Without this flag
260255
* a property chain such as `props?.items?.foo` will infer as a dep on

compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66
*/
77

88
import {CompilerError} from '../CompilerError';
9-
import {
10-
Effect,
11-
HIRFunction,
12-
Identifier,
13-
IdentifierId,
14-
LoweredFunction,
15-
isRefOrRefValue,
16-
makeInstructionId,
17-
} from '../HIR';
9+
import {Effect, HIRFunction, IdentifierId, makeInstructionId} from '../HIR';
1810
import {deadCodeElimination} from '../Optimization';
1911
import {inferReactiveScopeVariables} from '../ReactiveScopes';
2012
import {rewriteInstructionKindsBasedOnReassignment} from '../SSA';
21-
import {inferMutableRanges} from './InferMutableRanges';
22-
import inferReferenceEffects from './InferReferenceEffects';
2313
import {assertExhaustive} from '../Utils/utils';
2414
import {inferMutationAliasingEffects} from './InferMutationAliasingEffects';
2515
import {inferMutationAliasingRanges} from './InferMutationAliasingRanges';
@@ -30,12 +20,7 @@ export default function analyseFunctions(func: HIRFunction): void {
3020
switch (instr.value.kind) {
3121
case 'ObjectMethod':
3222
case 'FunctionExpression': {
33-
if (!func.env.config.enableNewMutationAliasingModel) {
34-
lower(instr.value.loweredFunc.func);
35-
infer(instr.value.loweredFunc);
36-
} else {
37-
lowerWithMutationAliasing(instr.value.loweredFunc.func);
38-
}
23+
lowerWithMutationAliasing(instr.value.loweredFunc.func);
3924

4025
/**
4126
* Reset mutable range for outer inferReferenceEffects
@@ -140,58 +125,3 @@ function lowerWithMutationAliasing(fn: HIRFunction): void {
140125
value: fn,
141126
});
142127
}
143-
144-
function lower(func: HIRFunction): void {
145-
analyseFunctions(func);
146-
inferReferenceEffects(func, {isFunctionExpression: true});
147-
deadCodeElimination(func);
148-
inferMutableRanges(func);
149-
rewriteInstructionKindsBasedOnReassignment(func);
150-
inferReactiveScopeVariables(func);
151-
func.env.logger?.debugLogIRs?.({
152-
kind: 'hir',
153-
name: 'AnalyseFunction (inner)',
154-
value: func,
155-
});
156-
}
157-
158-
function infer(loweredFunc: LoweredFunction): void {
159-
for (const operand of loweredFunc.func.context) {
160-
const identifier = operand.identifier;
161-
CompilerError.invariant(operand.effect === Effect.Unknown, {
162-
reason:
163-
'[AnalyseFunctions] Expected Function context effects to not have been set',
164-
loc: operand.loc,
165-
});
166-
if (isRefOrRefValue(identifier)) {
167-
/*
168-
* TODO: this is a hack to ensure we treat functions which reference refs
169-
* as having a capture and therefore being considered mutable. this ensures
170-
* the function gets a mutable range which accounts for anywhere that it
171-
* could be called, and allows us to help ensure it isn't called during
172-
* render
173-
*/
174-
operand.effect = Effect.Capture;
175-
} else if (isMutatedOrReassigned(identifier)) {
176-
/**
177-
* Reflects direct reassignments, PropertyStores, and ConditionallyMutate
178-
* (directly or through maybe-aliases)
179-
*/
180-
operand.effect = Effect.Capture;
181-
} else {
182-
operand.effect = Effect.Read;
183-
}
184-
}
185-
}
186-
187-
function isMutatedOrReassigned(id: Identifier): boolean {
188-
/*
189-
* This check checks for mutation and reassingnment, so the usual check for
190-
* mutation (ie, `mutableRange.end - mutableRange.start > 1`) isn't quite
191-
* enough.
192-
*
193-
* We need to track re-assignments in context refs as we need to reflect the
194-
* re-assignment back to the captured refs.
195-
*/
196-
return id.mutableRange.end > id.mutableRange.start;
197-
}

compiler/packages/babel-plugin-react-compiler/src/Inference/InerAliasForUncalledFunctions.ts

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)