Skip to content

Commit

Permalink
[compiler] rewrite invariant in InferReferenceEffects
Browse files Browse the repository at this point in the history
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
  • Loading branch information
mofeiZ committed Dec 27, 2024
1 parent fc8a898 commit 9c1760b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ function inferOperandEffect(state: State, place: Place): null | FunctionEffect {
if (isRefOrRefValue(place.identifier)) {
break;
} else if (value.kind === ValueKind.Context) {
CompilerError.invariant(value.context.size > 0, {
reason: 'Expected context value places',
loc: place.loc,
});
return {
kind: 'ContextMutation',
loc: place.loc,
effect: place.effect,
places: value.context.size === 0 ? new Set([place]) : value.context,
places: value.context,
};
} else if (
value.kind !== ValueKind.Mutable &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FunctionEffect,
GeneratedSource,
HIRFunction,
Identifier,

Check failure on line 19 in compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

'Identifier' is defined but never used. Allowed unused vars must match /^_/u
IdentifierId,
InstructionKind,
InstructionValue,
Expand Down Expand Up @@ -857,17 +858,19 @@ function inferBlock(
break;
}
case 'ArrayExpression': {
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
const contextRefOperands = getContextRefOperand(state, instrValue);
const valueKind: AbstractValue =
contextRefOperands.length > 0
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(contextRefOperands),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
continuation = {
kind: 'initialize',
valueKind,
Expand Down Expand Up @@ -918,17 +921,19 @@ function inferBlock(
break;
}
case 'ObjectExpression': {
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
const contextRefOperands = getContextRefOperand(state, instrValue);
const valueKind: AbstractValue =
contextRefOperands.length > 0
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(contextRefOperands),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};

for (const property of instrValue.properties) {
switch (property.kind) {
Expand Down Expand Up @@ -1593,15 +1598,20 @@ function inferBlock(
}
case 'LoadLocal': {
const lvalue = instr.lvalue;
const effect =
state.isDefined(lvalue) &&
state.kind(lvalue).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
CompilerError.invariant(
!(
state.isDefined(lvalue) &&
state.kind(lvalue).kind === ValueKind.Context
),
{
reason: 'Unexpected LoadLocal with context lvalue',
loc: lvalue.loc,
},
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.place,
effect,
Effect.Capture,
ValueReason.Other,
);
lvalue.effect = Effect.ConditionallyMutate;
Expand Down Expand Up @@ -1932,19 +1942,20 @@ function inferBlock(
);
}

function hasContextRefOperand(
function getContextRefOperand(
state: InferenceState,
instrValue: InstructionValue,
): boolean {
): Array<Place> {
const result = [];
for (const place of eachInstructionValueOperand(instrValue)) {
if (
state.isDefined(place) &&
state.kind(place).kind === ValueKind.Context
) {
return true;
result.push(place);
}
}
return false;
return result;
}

export function getFunctionCallSignature(
Expand Down

0 comments on commit 9c1760b

Please sign in to comment.