Skip to content

[compiler] Clean up deadcode: ReactiveFunctionValue #32098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,7 @@ export type ReactiveValue =
| ReactiveLogicalValue
| ReactiveSequenceValue
| ReactiveTernaryValue
| ReactiveOptionalCallValue
| ReactiveFunctionValue;

export type ReactiveFunctionValue = {
kind: 'ReactiveFunctionValue';
fn: ReactiveFunction;
dependencies: Array<Place>;
returnType: t.FlowType | t.TSType | null;
loc: SourceLocation;
};
| ReactiveOptionalCallValue;

export type ReactiveLogicalValue = {
kind: 'LogicalExpression';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import generate from '@babel/generator';
import {printReactiveFunction} from '..';
import {CompilerError} from '../CompilerError';
import {printReactiveScopeSummary} from '../ReactiveScopes/PrintReactiveFunction';
import DisjointSet from '../Utils/DisjointSet';
Expand Down Expand Up @@ -701,10 +700,6 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
value = `FinishMemoize decl=${printPlace(instrValue.decl)}`;
break;
}
case 'ReactiveFunctionValue': {
value = `FunctionValue ${printReactiveFunction(instrValue.fn)}`;
break;
}
default: {
assertExhaustive(
instrValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,16 @@ import {
Effect,
HIRFunction,
Identifier,
IdentifierId,
LoweredFunction,
isRefOrRefValue,
makeInstructionId,
} from '../HIR';
import {deadCodeElimination} from '../Optimization';
import {inferReactiveScopeVariables} from '../ReactiveScopes';
import {rewriteInstructionKindsBasedOnReassignment} from '../SSA';
import {inferMutableContextVariables} from './InferMutableContextVariables';
import {inferMutableRanges} from './InferMutableRanges';
import inferReferenceEffects from './InferReferenceEffects';

// Helper class to track indirections such as LoadLocal and PropertyLoad.
export class IdentifierState {
properties: Map<IdentifierId, Identifier> = new Map();

resolve(identifier: Identifier): Identifier {
const resolved = this.properties.get(identifier.id);
if (resolved !== undefined) {
return resolved;
}
return identifier;
}

alias(lvalue: Identifier, value: Identifier): void {
this.properties.set(lvalue.id, this.properties.get(value.id) ?? value);
}
}

export default function analyseFunctions(func: HIRFunction): void {
for (const [_, block] of func.body.blocks) {
for (const instr of block.instructions) {
Expand Down Expand Up @@ -78,7 +59,6 @@ function lower(func: HIRFunction): void {
}

function infer(loweredFunc: LoweredFunction): void {
const knownMutated = inferMutableContextVariables(loweredFunc.func);
for (const operand of loweredFunc.func.context) {
const identifier = operand.identifier;
CompilerError.invariant(operand.effect === Effect.Unknown, {
Expand All @@ -95,10 +75,11 @@ function infer(loweredFunc: LoweredFunction): void {
* render
*/
operand.effect = Effect.Capture;
} else if (knownMutated.has(operand)) {
operand.effect = Effect.Mutate;
} else if (isMutatedOrReassigned(identifier)) {
// Note that this also reflects if identifier is ConditionallyMutated
/**
* Reflects direct reassignments, PropertyStores, and ConditionallyMutate
* (directly or through maybe-aliases)
*/
operand.effect = Effect.Capture;
} else {
operand.effect = Effect.Read;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,6 @@ function codegenInstructionValue(
);
break;
}
case 'ReactiveFunctionValue':
case 'StartMemoize':
case 'FinishMemoize':
case 'Debugger':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,6 @@ function computeMemoizationInputs(
rvalues: operands,
};
}
case 'ReactiveFunctionValue': {
CompilerError.invariant(false, {
reason: `Unexpected ReactiveFunctionValue node`,
description: null,
loc: value.loc,
suggestions: null,
});
}
case 'UnsupportedNode': {
CompilerError.invariant(false, {
reason: `Unexpected unsupported node`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ export class ReactiveFunctionVisitor<TState = void> {
this.visitValue(value.id, value.value, state);
break;
}
case 'ReactiveFunctionValue': {
this.visitReactiveFunctionValue(
id,
value.dependencies,
value.fn,
state,
);
break;
}
default: {
for (const place of eachInstructionValueOperand(value)) {
this.visitPlace(id, place, state);
Expand Down Expand Up @@ -434,18 +425,6 @@ export class ReactiveFunctionTransform<
}
break;
}
case 'ReactiveFunctionValue': {
const nextValue = this.transformReactiveFunctionValue(
id,
value.dependencies,
value.fn,
state,
);
if (nextValue.kind === 'replace') {
value.fn = nextValue.value;
}
break;
}
default: {
for (const place of eachInstructionValueOperand(value)) {
this.visitPlace(id, place, state);
Expand Down Expand Up @@ -619,10 +598,6 @@ export function* eachReactiveValueOperand(
yield* eachReactiveValueOperand(instrValue.alternate);
break;
}
case 'ReactiveFunctionValue': {
yield* instrValue.dependencies;
break;
}
default: {
yield* eachInstructionValueOperand(instrValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
case 'OptionalExpression': {
return this.recordDepsInValue(value.value, state);
}
case 'ReactiveFunctionValue': {
CompilerError.throwTodo({
reason:
'Handle ReactiveFunctionValue in ValidatePreserveManualMemoization',
loc: value.loc,
});
}
case 'ConditionalExpression': {
this.recordDepsInValue(value.test, state);
this.recordDepsInValue(value.consequent, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ function Component() {

// capture into a separate variable that is not a context variable.
const y = x;
/**
* Note that this fixture currently produces a stale effect closure if `y = x
* = someGlobal` changes between renders. Under current compiler assumptions,
* that would be a rule of react violation.
*/
useEffect(() => {
y.value = 'hello';
}, []);
});

useEffect(() => {
setState(someGlobal.value);
Expand All @@ -46,57 +51,50 @@ import { useEffect, useState } from "react";
let someGlobal = { value: null };

function Component() {
const $ = _c(7);
const $ = _c(5);
const [state, setState] = useState(someGlobal);

let x = someGlobal;
while (x == null) {
x = someGlobal;
}

const y = x;
let t0;
let t1;
let t2;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
let x = someGlobal;
while (x == null) {
x = someGlobal;
}

const y = x;
t0 = useEffect;
t1 = () => {
t0 = () => {
y.value = "hello";
};
t2 = [];
$[0] = t0;
} else {
t0 = $[0];
}
useEffect(t0);
let t1;
let t2;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = () => {
setState(someGlobal.value);
};
t2 = [someGlobal];
$[1] = t1;
$[2] = t2;
} else {
t0 = $[0];
t1 = $[1];
t2 = $[2];
}
t0(t1, t2);
let t3;
useEffect(t1, t2);

const t3 = String(state);
let t4;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = () => {
setState(someGlobal.value);
};
t4 = [someGlobal];
if ($[3] !== t3) {
t4 = <div>{t3}</div>;
$[3] = t3;
$[4] = t4;
} else {
t3 = $[3];
t4 = $[4];
}
useEffect(t3, t4);

const t5 = String(state);
let t6;
if ($[5] !== t5) {
t6 = <div>{t5}</div>;
$[5] = t5;
$[6] = t6;
} else {
t6 = $[6];
}
return t6;
return t4;
}

export const FIXTURE_ENTRYPOINT = {
Expand Down
Loading
Loading