Commit Graph
487 Commits
Author SHA1 Message Date
Joe Savona 490c204dcf Move logic for making reactive scope decls all reactive
InferReactiveIdentifiers has some extra logic to find identifiers declared in 
the same scope, and promote non-reactive identifiers to reactive if they appear 
inside a reactive scope (reactive scope == scope with one or more (reactive) 
dependencies). Even though the identifier alone might not be technically 
reactive (have no reactive inputs), it can get re-recreated if the scope 
re-evaluates. 

We can now do this during PruneNonReactiveDependencies as we exit out of each 
scope.
2023-02-14 15:27:52 -08:00
Joe Savona b1ee356805 InferReactiveIdentifiers: fixpoint iteration is now unnecessary
I removed fixpoint iteration and all tests pass, which matches my intuition that 
it's really that we need strictly two passes. Removing to simplify and for 
performance (avoid unnecessary extra visits of the ast)
2023-02-14 15:24:01 -08:00
Joe Savona 2b47cac5fd Create a separate pass to prune non-reactive dependencies
The fact that InferReactiveIdentifiers is integrated directly into 
PropagateScopeDependencies has made the latter pretty tricky to debug at times. 
If a dependency is missing, we have to introspect and figure out if that's 
because it was somehow inferred as non-reactive. This PR creates a new 
PruneNonReactiveDependencies pass to separate out these phases.
2023-02-14 14:09:53 -08:00
Sathya Gunasekaran fb8f293c32 [hir] Add a DropMemoCall pass
This drops the memo hook calls from the IR
2023-02-14 23:07:19 +00:00
Sathya Gunasekaran e0562bbd51 [typer] Type hook callee as Hook type 2023-02-14 23:07:18 +00:00
Sathya Gunasekaran d0f1a98144 [typer] Introduce a Hook type 2023-02-14 23:07:17 +00:00
Sathya Gunasekaran 1dfd51cdb0 [hir] Add name field to Hook 2023-02-14 23:07:17 +00:00
Sathya Gunasekaran a65bf197d7 [hir] Add Memo hooks 2023-02-14 23:07:16 +00:00
Sathya Gunasekaran 12ca0ba61b [hir] Simplify HookKind 2023-02-14 23:07:16 +00:00
Sathya Gunasekaran 81b23f9242 [typer][be] Add helper for checking type.kind 2023-02-14 19:10:37 +00:00
Joe Savona 5673588be4 [be] Tidy up some mutableRange logic 2023-02-13 15:26:07 -08:00
Joe Savona 225fe0835c Optimize DCE to visit CFG only once when there are no loops
Optimizes dead code elimination. Currently it keeps iterating the control flow 
graph until no new usages have been discovered, which accounts for usages across 
loops. However, when there are no loops it's sufficient to iterate the CFG 
exactly once.
2023-02-13 13:59:44 -08:00
Joe Savona 6b67f597a6 Capturing a frozen value is a Read
If a value is known to be frozen (or potentially frozen), then it doesn't need 
to be considered 'captured' since no mutation can occur via aliasing.
2023-02-14 09:33:23 -08:00
Lauren Tan 759a7e027b Fix incorrectly recording declarations in reassignments in
PropagateScopeDependencies 

This was incorrectly added in #1190, oops!
2023-02-13 16:52:08 -05:00
Lauren Tan b5a0739e8c Scopes with reassignments should still emit memo block 2023-02-13 16:52:07 -05:00
Lauren Tan a76627c972 Use IdentifierIds to when comparing Identifier
With the upcoming changes to SSA renaming in #1194, we rewrite phi operand 
identifiers to have the same IdentifierId as the declaration the identifier 
originated from: so downstream checks need to compare ids instead of the 
identifier instance.
2023-02-13 16:52:05 -05:00
Lauren Tan 985a289a4c [BE] Various linter fixes 2023-02-13 16:52:04 -05:00
Sathya Gunasekaran 31d8aad40e [hir] Check if defined before looking up ValueKind
This handles globals now without throwing
2023-02-13 16:32:50 +00:00
Sathya Gunasekaran 7277897405 [test] Add test for broken lambda capturing
SSA redefines context refs which breaks our inference. The correct fix here is 
to not overwrite context refs in EnterSSA.
2023-02-10 17:59:59 +00:00
Sathya Gunasekaran b56f413e99 [hir] Mark ArrayExpression capturing a context ref as a context ref
A context ref capture is transitive.
2023-02-10 17:59:58 +00:00
Sathya Gunasekaran 343ebb47bd [hir] Mark ObjectExpression capturing a context ref as a context ref
A context ref capture is transitive.
2023-02-10 17:59:57 +00:00
Sathya Gunasekaran fa57551dd5 [hir] Introduce ValueKind.Context
This tracks whether a value is a context ref or generated from a context ref. 

This lets us track mutations to context refs and treat it separately as we want 
this to be more conservative than our existing inference. 

ValueKind.Context is exactly like ValueKind.Mutable but is more conservative.
2023-02-10 17:59:56 +00:00
Sathya Gunasekaran 2a9fab001a [be] Fix test
'e' does not exist, use 'd' instead
2023-02-10 17:26:25 +00:00
Sathya Gunasekaran 199e284eda [hir] Mark Identifiers aliased as Effect.Capture 2023-02-10 17:26:21 +00:00
Sathya Gunasekaran 40a85432ed [hir] Move inference logic to Env.reference 2023-02-10 17:26:17 +00:00
Joe Savona 6877ac6500 Fix update assignment on computed memberexpression 2023-02-10 08:59:15 -08:00
Joe Savona 5746d5b07f Temporary workaround for emitting temporaries multiple times
This is a temporary fix for the issue we discovered on our first integration, 
where destructuring of a function return value is emitting the function call 
multiple times: 

```javascript 

// Input 

const [x, setX] = useState(null); 

// Output 

const x = useState(null)[0]; 

const setX = useState(null)[1]; 

``` 

The reason this happens is that we lower `useState(null)` to a temporary, and 
then generate a ComputedLoad for each of x and setX. Codegen doesn't emit 
temporaries eagerly - it assumes they are going to be used exactly once and it 
re-emits the value each time the temporary is used. Hence why the 
`useState(null)` part gets duplicated in the output. 

Right now destructuring is the only place i'm aware of where we reuse 
temporaries this way. And we do want to change codegen to preserve destructuring 
in the output to correctly handle array patterns. However, that's a more 
involved change. For now, this PR is a stopgap. During the pass where we promote 
temporaries used in scopes to named variables, we now check to see if those 
temporaries are used multiple times and promote them. 

The above example would then generate something like 

```javascript 

const t0 = useState(null); 

const x = t0[0]; 

const setX = t0[1]; 

``` 

This is still incorrect (it assumes t0 is an array), but it's more likely to 
work in practice. I'll revert this change once we correctly handle 
destructuring.
2023-02-09 17:22:54 -08:00
Joe Savona dd228c9ed0 [be] non-blocking ESLint config, fixes
Configures typescript-eslint for the project with an initial configuration that 
starts with their recommended rules, and adds/disables a few (generally either 
disabling warnings or promoting them to errors). The new `yarn lint` command is 
not hooked up to CI yet, so for now this is something we can opt-in to running 
locally. If you have some free time, help get us down to zero errors! 

My general philosophy for linting, which I propose we follow, is that lints 
should be very high-signal: 

* Error, don't warn. If it's worth mentioning it's worth fixing. 

* Enable rules that consistently identify real problems. If we frequently would 
have to disable the rule due to false positives, it isn't high-signal. 

* Enable rules that help improve consistent style (to avoid code review about 
style rather than substance).
2023-02-09 11:58:06 -08:00
Joe Savona 8eb5feb847 [be] Cleanup BuildHIR 2023-02-09 11:41:04 -08:00
Joe Savona 41f52b73c2 Change reference effects for hooks
I realized we hadn't updated InferReferenceEffects to match our latest thinking 
on hooks. Specifically, we will default to assuming that hooks can mutate their 
arguments and return mutable values — this works with our model since we don't 
treat hooks specially for reactive scope construction. Ie, first we figure out 
what variables construct together, then we create scopes, then we prune scopes 
that contain hooks. So changing the reference effects for hooks "just works". 

Note that it is helpful for our unit tests to have an example hook that we know 
_does_ freeze its input and return a frozen value, so i've temporarily added 
`useFreeze()` to the list of defined hooks. That is meant as a stopgap: the 
right solution is to allow some way to tell the compiler about specific custom 
hooks and their semantics.
2023-02-08 14:24:14 -08:00
Joe Savona 7f60f32118 Dont memoize scopes with hook calls 2023-02-08 14:07:01 -08:00
Joe Savona c7e3bc4d41 Visitor extension for transforming ReactiveFunction
Adds a subclass of ReactiveFunctionVisitor, ReactiveFunctionTransform, which 
makes it easier to write passes that change the shape of a ReactiveFunction. The 
two use-cases converted so far are both flattening away certain categories of 
reactive scopes — this will make it easier to add a similar pass to prune scopes 
that contain hook calls.
2023-02-08 14:07:00 -08:00
Sathya Gunasekaran 394666118d [hir] Lower function expressions into HIR 2023-02-08 17:45:33 +00:00
Sathya Gunasekaran 662b8d2ff3 [hir] Add lvalue effect for PropertyLoad and ComputedLoad 2023-02-08 16:01:36 +00:00
Lauren Tan d47f608c61 Record reassignments
Record a variable that is declared in some other scope and that is being 
reassigned in the current one as a reassignment
2023-02-08 10:26:33 -05:00
Lauren Tan e7f4eae619 Reset canonicalId mutable range if not mutated after creation 2023-02-08 10:26:31 -05:00
Lauren Tan 6f661f3b79 Extend mutable ranges if a phi is mutated after creation 2023-02-08 10:26:29 -05:00
Lauren Tan d43a5014f4 Rename ReactiveScope.outputs to ReactiveScope.declarations 2023-02-08 10:26:27 -05:00
Lauren Tan 1809ffd06d Add new test cases for SSA
Add these as a separate commit so we can see how later PRs in the stack change 
them
2023-02-08 10:26:26 -05:00
Lauren Tan 5888cbdd9b Don't throw on invalid compiler flags 2023-02-08 10:26:24 -05:00
Sathya Gunasekaran 24b28ae257 [λ] Remove broken support for identifiers defined after use
This also adds some book keeping to throw if ever define identifiers after use 
to make sure we don't generate incorrect code.
2023-02-07 18:25:55 +00:00
Sathya Gunasekaran d1dbb00790 [hir] Throw if Effect.Store is not handled 2023-02-07 16:59:37 +00:00
Sathya Gunasekaran 3df1ae66c0 [λ] Use Effect.Capture for mutating deps
Leverage Effect.Capture to differentiate between mutating and non mutating deps.
2023-02-07 16:59:36 +00:00
Sathya Gunasekaran 85c92bcfbe [aliasing] Introduce Effect.Capture
Effect.Capture is very similar to Effect.Read, but the only difference is that 
this reference is stored somewhere via a Effect.Store. 

Previously, any operand associated with a Effect.Store in the same instruction 
would get aliased -- so there was no need to explicitly differentiate between a 
"normal read" and "read that gets stored". 

This difference is now explicit with FunctionExpression where every dependency 
is "read" but only a few are "captured" for store (and mutation). In a follow up 
PR, the mutating deps will have an Effect.Capture to differentiate from the 
other non mutating deps (Effect.Read).
2023-02-07 16:59:35 +00:00
Sathya Gunasekaran 4e07b8a869 [hir] Do not alias Identifier as Store
Identifiers are never stored (Effect.Store), they are always mutated 
(Effect.Mutate) and aliased in the InferAlias pass.
2023-02-07 16:59:35 +00:00
Lauren Tan 195f473cb4 s/useMemoCache/unstable_useMemoCache
uMC is still prefixed with unstable in React 
(https://github.com/facebook/react/blob/653dd2348ccfd7bfa4e11d814e247ed3ff7c5fa7/packages/react/src/React.js#L143)
2023-02-07 11:40:06 -05:00
Sathya Gunasekaran 1f7cd2ff21 [hir] Remove unnecessary null check 2023-02-07 15:08:38 +00:00
Lauren Tan 9cac0b0068 Ensure Forget runs first if specified in a Babel config
This commit adds a new Program visitor to our Babel plugin which then calls our 
FunctionDeclaration visitor. Babel does some "smart" merging of plugin passes so 
so even if plugin A is inserted prior to plugin B, if A does not have a Program 
visitor and B does, B will run first. 

Note that we also can't use Forget inside of a Babel preset as plugins run 
_before_ presets (https://babeljs.io/docs/en/plugins/#plugin-ordering).
2023-02-07 11:11:58 -05:00
Lauren Tan e159844b0d Add generated code to errors thrown by BabelPlugin
If we've generated an erroneous AST (eg duplicate declarations), this lets us 
observe what the code looked like to aid debugging
2023-02-06 19:03:54 -05:00
Lauren Tan 0d2653dbd4 Use parent of AssigmentExpressions when bailing out
For some reason the expression itself wont print the codeframe, but its parent 
will, so print that instead
2023-02-06 17:32:30 -05:00