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] Collect temporaries and optional chains from inner functions #31346

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

mofeiZ
Copy link
Contributor

@mofeiZ mofeiZ commented Oct 24, 2024

Recursively collect identifier / property loads and optional chains from inner functions. This PR is in preparation for #31200

Previously, we only did this in collectHoistablePropertyLoads to understand hoistable property loads from inner functions.

  1. collectTemporariesSidemap
  2. collectOptionalChainSidemap
  3. collectHoistablePropertyLoads
    • ^ this recursively calls collectTemporariesSidemap, collectOptionalChainSidemap, and collectOptionalChainSidemap on inner functions
  4. collectDependencies

Now, we have

  1. collectTemporariesSidemap
    • recursively record identifiers in inner functions. Note that we track all temporaries in the same map as IdentifierIds are currently unique across functions
  2. collectOptionalChainSidemap
    • recursively records optional chain sidemaps in inner functions
  3. collectHoistablePropertyLoads
    • (unchanged, except to remove recursive collection of temporaries)
  4. collectDependencies
    • unchanged: to be modified to recursively collect dependencies in next PR

'

Stack created with Sapling. Best reviewed with ReviewStack.

Copy link

vercel bot commented Oct 24, 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 Nov 5, 2024 11:58pm

*/
declare(identifier: Identifier, decl: Decl): void {
if (this.inInnerFn) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

why don't we need to track declarations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the above comment for details, but tldr:

  • inner functions cannot produce scope decls by construction
  • inner function instruction ids are invalid to compare against outer instruction ids (which we use to determine mutable range eligibility)

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.

Yesss i wondered about the inner function traversals. I had hacked on explicitly hoisting deps and needed to add the traversal to the temporary sidemap creation too.

mofeiZ added a commit that referenced this pull request Nov 5, 2024
Extends #31066 to ObjectMethods (somehow missed this before).

'
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31197).
* #31204
* #31202
* #31203
* #31201
* #31200
* #31346
* #31199
* #31431
* #31345
* __->__ #31197
mofeiZ added a commit that referenced this pull request Nov 5, 2024
`PropertyPathRegistry` is responsible for uniqueing identifier and
property paths. This is necessary for the hoistability CFG merging logic
which takes unions and intersections of these nodes to determine a basic
block's hoistable reads, as a function of its neighbors. We also depend
on this to merge optional chained and non-optional chained property
paths

This fixes a small bug in #31066 in which we create a new registry for
nested functions. Now, we use the same registry for a component / hook
and all its inner functions

'
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31345).
* #31204
* #31202
* #31203
* #31201
* #31200
* #31346
* #31199
* #31431
* __->__ #31345
* #31197
mofeiZ added a commit that referenced this pull request Nov 5, 2024
This bugfix is needed to land #31199 PropagateScopeDepsHIR infers scope
declarations for the `inline-jsx-transform` test fixture (the non-hir
version does not).

These declarations must get the rewritten phi identifiers
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31431).
* #31204
* #31202
* #31203
* #31201
* #31200
* #31346
* #31199
* __->__ #31431
* #31345
* #31197
`enablePropagateScopeDepsHIR` is now used extensively in Meta. This has been tested for over two weeks in our e2e tests and production.

The rest of this stack deletes `LoweredFunction.dependencies`, which the non-hir version of `PropagateScopeDeps` depends on. To avoid a more forked HIR (non-hir with dependencies and hir with no dependencies), let's go ahead and clean up the non-hir version of PropagateScopeDepsHIR.

Note that all fixture changes in this PR were previously reviewed when they were copied to `propagate-scope-deps-hir-fork`. Will clean up / merge these duplicate fixtures in a later PR

'
Recursively collect identifier / property loads and optional chains from inner functions. This PR is in preparation for #31200

Previously, we only did this in `collectHoistablePropertyLoads` to understand hoistable property loads from inner functions.
1. collectTemporariesSidemap
2. collectOptionalChainSidemap
3. collectHoistablePropertyLoads
    - ^ this recursively calls `collectTemporariesSidemap`, `collectOptionalChainSidemap`, and `collectOptionalChainSidemap` on inner functions
4. collectDependencies

Now, we have
1. collectTemporariesSidemap
    - recursively record identifiers in inner functions. Note that we track all temporaries in the same map as `IdentifierIds` are currently unique across functions
2. collectOptionalChainSidemap
    - recursively records optional chain sidemaps in inner functions
3. collectHoistablePropertyLoads
    - (unchanged, except to remove recursive collection of temporaries)
4. collectDependencies
    - unchanged: to be modified to recursively collect dependencies in next PR

'
mofeiZ added a commit that referenced this pull request Nov 6, 2024
`enablePropagateScopeDepsHIR` is now used extensively in Meta. This has
been tested for over two weeks in our e2e tests and production.

The rest of this stack deletes `LoweredFunction.dependencies`, which the
non-hir version of `PropagateScopeDeps` depends on. To avoid a more
forked HIR (non-hir with dependencies and hir with no dependencies),
let's go ahead and clean up the non-hir version of
PropagateScopeDepsHIR.

Note that all fixture changes in this PR were previously reviewed when
they were copied to `propagate-scope-deps-hir-fork`. Will clean up /
merge these duplicate fixtures in a later PR

'
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31199).
* #31202
* #31203
* #31201
* #31200
* #31346
* __->__ #31199
@mofeiZ
Copy link
Contributor Author

mofeiZ commented Nov 6, 2024

Minor cleanup: changed processedInstrsInOptional's type from Map<HIRFunction, Set<InstructionId>> to Set<Instruction | Terminal> (similar to what we do with InstructionValue in InferReferenceEffect). This is now a lot cleaner

@mofeiZ mofeiZ merged commit c3570b1 into main Nov 6, 2024
19 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants