Skip to content
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

[compiler] transform fire calls #31796

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

jbrown215
Copy link
Contributor

@jbrown215 jbrown215 commented Dec 15, 2024

This is the diff with the meaningful changes. The approach is:

  1. Collect fire callees and remove fire() calls, create a new binding for the useFire result
  2. Update LoadLocals for captured callees to point to the useFire result
  3. Update function context to reference useFire results
  4. Insert useFire calls after getting to the component scope

This approach aims to minimize the amount of new bindings we introduce for the function expressions
to minimize bookkeeping for dependency arrays. We keep all of the LoadLocals leading up to function
calls as they are and insert new instructions to load the originally captured function, call useFire,
and store the result in a new promoted temporary. The lvalues that referenced the original callee are
changed to point to the new useFire result.

This is the minimal diff to implement the expected behavior (up to importing the useFire call, next diff)
and further stacked diffs implement error handling. The rules for fire are:

  1. If you use fire for a callee in the effect once you must use it for every time you call it in that effect
  2. You can only use fire in a useEffect lambda/functions defined inside the useEffect lambda

There is still more work to do here, like updating the effect dependency array and handling object methods

--

Stack created with Sapling. Best reviewed with ReviewStack.

Copy link

vercel bot commented Dec 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 7:22pm

Makes `fire` a known export for type-based analysis

--
@jbrown215 jbrown215 marked this pull request as ready for review December 16, 2024 01:45
jbrown215 added a commit that referenced this pull request Dec 16, 2024
Config flag for `fire`

--
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31794).
* #31811
* #31798
* #31797
* #31796
* #31795
* __->__ #31794
jbrown215 added a commit that referenced this pull request Dec 16, 2024
Makes `fire` a known export for type-based analysis

--
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31795).
* #31811
* #31798
* #31797
* #31796
* __->__ #31795
* #31794
Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting!!!! I'm still wrapping my head around this, sharing some early feedback

Comment on lines 444 to 445
const rewriteInstrs = new Map<InstructionId, Array<Instruction>>();
const deleteInstrs = new Set<InstructionId>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pattern we usually use for this is a nullable nextInstructions array, initialized by slicing up to the point where you need to start making edits. then after that you push instructions to keep/add, don't push instructions you want to delete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's an inconvenient approach for this pass specifically.

We insert the useFire calls before the load of the useEffect. If we were to wait until the point at which we know to generate them to insert, which is at the useEffect call, then we have to keep track of the index of the useEffect load index and insert the instructions there. This is easy to do with the InstructionIds because adding new instructions to the mapping does not shift the index of another useEffect load that occurs in the same block. If we push to the array, we'll need to additionally keep track of the number of added instructions so that we can account for the shifted offset in the nextInstructions array at which we should insert the useFire calls. If we naively insert the useFire calls right before the useEffect call then we introduce something like:

const effectLambda = () => {
  fireFunction();
}
const fireFunction = useFire(aFunction);
useEffect(effectLambda);

This code is legit, but the HIR representation for this is not as simple (requires a DeclareContext HoistedConst). It doesn't cause any of the tests to fail, but I'm not sure if that's really saying much.

} {
const useFirePlace = createTemporaryPlace(env, GeneratedSource);
useFirePlace.effect = Effect.Read;
useFirePlace.identifier.type = DefaultNonmutatingHook;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we be more precise and use the BuiltInFireId type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BuiltInFireId is for fire, not useFire- is there a benefit to also introducing a useFire built in type?

This is the diff with the meaningful changes. The approach is:
1. Collect fire callees and remove fire() calls, create a new binding for the useFire result
2. Update LoadLocals for captured callees to point to the useFire result
3. Update function context to reference useFire results
4. Insert useFire calls after getting to the component scope

This approach aims to minimize the amount of new bindings we introduce for the function expressions
to minimize bookkeeping for dependency arrays. We keep all of the LoadLocals leading up to function
calls as they are and insert new instructions to load the originally captured function, call useFire,
and store the result in a new promoted temporary. The lvalues that referenced the original callee are
changed to point to the new useFire result.

This is the minimal diff to implement the expected behavior (up to importing the useFire call, next diff)
and further stacked diffs implement error handling. The rules for fire are:
1. If you use fire for a callee in the effect once you must use it for every time you call it in that effect
2. You can only use fire in a useEffect lambda/functions defined inside the useEffect lambda

There is still more work to do here, like updating the effect dependency array and handling object methods

--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants