Add StoreGlobal instruction

ghstack-source-id: a715b1385da6648d867118d2b7486ffdb0ff89f6
Pull Request resolved: https://github.com/facebook/react-forget/pull/2871
This commit is contained in:
Joe Savona
2024-04-18 13:21:31 -07:00
parent 682f5a920a
commit d870c979a1
9 changed files with 62 additions and 3 deletions
@@ -897,6 +897,7 @@ export type InstructionValue =
loc: SourceLocation;
}
| LoadGlobal
| StoreGlobal
| FunctionExpression
| {
kind: "TaggedTemplateExpression";
@@ -1030,6 +1031,13 @@ export type LoadGlobal = {
loc: SourceLocation;
};
export type StoreGlobal = {
kind: "StoreGlobal";
name: string;
value: Place;
loc: SourceLocation;
};
export type BuiltinTag = {
kind: "BuiltinTag";
name: string;
@@ -571,7 +571,13 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
break;
}
case "LoadGlobal": {
value = `Global ${instrValue.name}`;
value = `LoadGlobal ${instrValue.name}`;
break;
}
case "StoreGlobal": {
value = `StoreGlobal ${instrValue.name} = ${printPlace(
instrValue.value
)}`;
break;
}
case "OptionalExpression": {
@@ -93,6 +93,10 @@ export function* eachInstructionValueOperand(
yield instrValue.value;
break;
}
case "StoreGlobal": {
yield instrValue.value;
break;
}
case "Destructure": {
yield instrValue.value;
break;
@@ -421,6 +425,10 @@ export function mapInstructionValueOperands(
instrValue.value = fn(instrValue.value);
break;
}
case "StoreGlobal": {
instrValue.value = fn(instrValue.value);
break;
}
case "Destructure": {
instrValue.value = fn(instrValue.value);
break;
@@ -1572,6 +1572,17 @@ function inferBlock(
lvalue.effect = Effect.Store;
continue;
}
case "StoreGlobal": {
state.reference(
instrValue.value,
functionEffects,
Effect.Capture,
ValueReason.Other
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.Store;
continue;
}
case "Destructure": {
let effect: Effect = Effect.Capture;
for (const place of eachPatternOperand(instrValue.lvalue.pattern)) {
@@ -310,7 +310,8 @@ function pruneableValue(value: InstructionValue, state: State): boolean {
case "ComputedStore":
case "PropertyDelete":
case "MethodCall":
case "PropertyStore": {
case "PropertyStore":
case "StoreGlobal": {
/*
* Mutating instructions are not safe to prune.
* TODO: we could be more precise and make this conditional on whether
@@ -1813,6 +1813,14 @@ function codegenInstructionValue(
);
break;
}
case "StoreGlobal": {
value = t.assignmentExpression(
"=",
t.identifier(instrValue.name),
codegenPlaceToExpression(cx, instrValue.value)
);
break;
}
case "ReactiveFunctionValue":
case "StartMemoize":
case "FinishMemoize":
@@ -195,7 +195,8 @@ function mayAllocate(env: Environment, instruction: Instruction): boolean {
case "FinishMemoize":
case "UnaryExpression":
case "BinaryExpression":
case "PropertyLoad": {
case "PropertyLoad":
case "StoreGlobal": {
return false;
}
case "CallExpression":
@@ -571,6 +571,17 @@ function computeMemoizationInputs(
rvalues: [value.value],
};
}
case "StoreGlobal": {
const lvalues = [];
if (lvalue !== null) {
lvalues.push({ place: lvalue, level: MemoizationLevel.Unmemoized });
}
return {
lvalues,
rvalues: [value.value],
};
}
case "Destructure": {
// Indirection for the inner value, memoized if the value is
const lvalues = [];
@@ -174,6 +174,11 @@ function* generateInstructionTypes(
break;
}
case "StoreGlobal": {
yield equation(left, value.value.identifier.type);
break;
}
case "BinaryExpression": {
if (isPrimitiveBinaryOp(value.operator)) {
yield equation(value.left.identifier.type, { kind: "Primitive" });