From 748859a00e5658c953c6732051ac374ba2bf39da Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 1 Nov 2023 17:13:01 -0700 Subject: [PATCH] Add `reactive` flag to Place This is part of a stack for inferring variables which are reactive via *control dependencies* as opposed to a data dependency. In compiler engineering, a statement S2 is control-dependent on statement S1 if S1 is in the post-dominance frontier of S2. Stated more intuitively: if S1 decides whether or not S2 is reached, then S1 is a control dependency of S2. As a start, we add `Place.reactive: boolean` so that individual places can track whether they are reactive or not. This lets us do fine-grained reactivity inference on the control-flow graph, even taking into account different SSA instances of a variable, so that we can say that a particular SSA version of `x` is reactive, while other "versions" of x (due to reassignment) are not. --- .../babel-plugin-react-forget/src/HIR/BuildHIR.ts | 10 ++++++++++ .../packages/babel-plugin-react-forget/src/HIR/HIR.ts | 1 + .../src/HIR/MergeConsecutiveBlocks.ts | 2 ++ .../src/Inference/DropManualMemoization.ts | 1 + .../InlineImmediatelyInvokedFunctionExpressions.ts | 2 ++ 5 files changed, 16 insertions(+) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 0404b02d03..5987809e51 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -76,6 +76,7 @@ export function lower( kind: "Identifier", identifier: builder.resolveBinding(ref), effect: Effect.Unknown, + reactive: false, loc: ref.loc ?? GeneratedSource, }); } @@ -106,6 +107,7 @@ export function lower( kind: "Identifier", identifier, effect: Effect.Unknown, + reactive: false, loc: param.node.loc ?? GeneratedSource, }; params.push(place); @@ -118,6 +120,7 @@ export function lower( kind: "Identifier", identifier: builder.makeTemporary(), effect: Effect.Unknown, + reactive: false, loc: param.node.loc ?? GeneratedSource, }; params.push(place); @@ -133,6 +136,7 @@ export function lower( kind: "Identifier", identifier: builder.makeTemporary(), effect: Effect.Unknown, + reactive: false, loc: param.node.loc ?? GeneratedSource, }; params.push({ @@ -403,6 +407,7 @@ function lowerStatement( effect: Effect.Unknown, identifier, kind: "Identifier", + reactive: false, loc: id.node.loc ?? GeneratedSource, }; lowerValueToTemporary(builder, { @@ -790,6 +795,7 @@ function lowerStatement( effect: Effect.Unknown, identifier, kind: "Identifier", + reactive: false, loc: id.node.loc ?? GeneratedSource, }; if (builder.isContextIdentifier(id)) { @@ -1158,6 +1164,7 @@ function lowerStatement( kind: "Identifier", identifier: builder.makeTemporary(), effect: Effect.Unknown, + reactive: false, loc: handlerBindingPath.node.loc ?? GeneratedSource, }; lowerValueToTemporary(builder, { @@ -3021,6 +3028,7 @@ function lowerIdentifier( kind: "Identifier", identifier: identifier, effect: Effect.Unknown, + reactive: false, loc: exprLoc, }; return place; @@ -3034,6 +3042,7 @@ function buildTemporaryPlace(builder: HIRBuilder, loc: SourceLocation): Place { kind: "Identifier", identifier: builder.makeTemporary(), effect: Effect.Unknown, + reactive: false, loc, }; return place; @@ -3087,6 +3096,7 @@ function lowerIdentifierForAssignment( kind: "Identifier", identifier: identifier, effect: Effect.Unknown, + reactive: false, loc, }; return place; diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts index d7fcaa41e0..c805555a36 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts @@ -877,6 +877,7 @@ export type Place = { kind: "Identifier"; identifier: Identifier; effect: Effect; + reactive: boolean; loc: SourceLocation; }; diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/MergeConsecutiveBlocks.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/MergeConsecutiveBlocks.ts index 666346c289..da9587aea3 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/MergeConsecutiveBlocks.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/MergeConsecutiveBlocks.ts @@ -75,6 +75,7 @@ export function mergeConsecutiveBlocks(fn: HIRFunction): void { kind: "Identifier", identifier: phi.id, effect: Effect.ConditionallyMutate, + reactive: false, loc: GeneratedSource, }, value: { @@ -83,6 +84,7 @@ export function mergeConsecutiveBlocks(fn: HIRFunction): void { kind: "Identifier", identifier: operand, effect: Effect.Read, + reactive: false, loc: GeneratedSource, }, loc: GeneratedSource, diff --git a/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts b/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts index ee2d8ea515..b7f3653d58 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts @@ -58,6 +58,7 @@ export function dropManualMemoization(func: HIRFunction): void { kind: "Identifier", identifier: fn.identifier, effect: Effect.Unknown, + reactive: false, loc: instr.value.loc, }, loc: instr.value.loc, diff --git a/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts b/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts index 9735f94d20..8a42ca8441 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts @@ -226,6 +226,7 @@ function rewriteBlock( type: makeType(), }, kind: "Identifier", + reactive: false, loc: terminal.loc, }, value: { @@ -265,6 +266,7 @@ function declareTemporary( type: makeType(), }, kind: "Identifier", + reactive: false, loc: GeneratedSource, }, value: {