-
Notifications
You must be signed in to change notification settings - Fork 47.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update on "[compiler][rewrite] PropagateScopeDeps hir rewrite"
\### Quick background: \#### Rvalues / temporaries: In the compiler, unnamed temporaries that represents the evaluation of an expression In the code snippet below, $1, $2, $3, and $4 are temporaries. ```js // input function Component({ bar} ) { const x = {a: foo(bar), b: {}}; } // gets lowered to [1] $2 = LoadGlobal(global) foo [2] $3 = LoadLocal bar$1 [3] $4 = Call $2(<unknown> $3) [4] $5 = Object { } [5] $6 = Object { a: $4, b: $5 } [6] $8 = StoreLocal Const x$7 = $6 ``` The compiler currently treats temporaries and named variables (e.g. `x`) differently in this pass. - named variables may be reassigned (in fact, since we're running after LeaveSSA, a single named identifier's IdentifierId may map to multiple `Identifier` instances -- each with its own scope and mutable range) - temporaries are replaced with their represented expressions during codegen. This is correct (mostly correct, see #29878) as we're careful to always lower the correct evaluation semantics. However, since we rewrite reactive scopes entirely (to if/else blocks), we need to track temporaries that a scope produces in `ReactiveScope.declarations` and later promote them to named variables. In the same example, $4, $5, and $6 need to be promoted: $2 ->`t0`, $5 ->`t1`, and $6 ->`t2`. ```js [1] $2 = LoadGlobal(global) foo [2] $3 = LoadLocal bar$1 scope 0: [3] $4 = Call $2(<unknown> $3) scope 1: [4] $5 = Object { } scope 2: [5] $6 = Object { a: $4, b: $5 } [6] $8 = StoreLocal Const x$7 = $6 ``` \#### Dependencies `ReactiveScope.dependencies` records the set of (read-only) values that a reactive scope is dependent on. This is currently limited to just variables (named variables from source and promoted temporaries) and property-loads. All dependencies we record need to be hoistable -- i.e. reordered to just before the ReactiveScope begins. Not all PropertyLoads are hoistable. In this example, we should not evaluate `obj.a.b` without before creating x and checking `objIsNull`. ```js // reduce-reactive-deps/no-uncond.js function useFoo({ obj, objIsNull }) { const x = []; if (isFalse(objIsNull)) { x.push(obj.a.b); } return x; } ``` While other memoization strategies with different constraints exist, the current compiler requires that `ReactiveScope.dependencies` be re-orderable to the beginning of the reactive scope. But.. `PropertyLoad`s from null values will throw `TypeError`. This means that evaluating hoisted dependencies should throw if and only if the source program throws. (It is also a bug if source throws and compiler output does not throw. See facebook/react-forget#2709) --- \### Rough high level overview 1. Pass 1 Walk over instructions to gather every temporary used outside of its defining scope (same as ReactiveFunction version). These determine the sidemaps we produce, as temporaries used outside of their declaring scopes get promoted to named variables later (and are not considered hoistable rvals). 2. Pass 2 (collectHoistablePropertyLoads) Walk over instructions to generate sidemaps: a. temporary identifier -> named variable and property path (e.g. `$3 -> {obj: props, path: ["a", "b"]}`) b. block -> accessed variables and properties (e.g. `bb0 -> [ {obj: props, path: ["a", "b"]} ]`) c. Walk over control flow graph to understand the set of object and property paths that can be read by each basic block. This analysis: - relies on post-dominator trees - traverses the CFG from entry (producing the set of variables/paths unconditionally evaluated *before* a block). - traverses the CFG from exit (producing the set of variables/paths unconditionally evaluated *after* a block). 4. Pass 3: (collectDependencies) Walks over instructions again to record dependencies and declarations, using the previously produced sidemaps Will add more fixture tests (although most cases should be covered in `reduce-reactive-deps`). Tested by syncing internally and checking compilation output differences ([internal link](https://fburl.com/wiki_markdown/nazsiszd)) --- \### Followups: 1. Rewrite function expression deps This change produces much more optimal output as the compiler now uses the function CFG to understand which variables / paths are assumed to be non-null. However, it may exacerbate [this function-expr hoisting bug](https://github.com/facebook/react/blob/main/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-invalid-hoisting-functionexpr.ts). A short term fix here is to simply call some form of `collectNonNullObjects` on every function expression to find hoistable variable / paths. In the longer term, we should refactor out `FunctionExpression.deps`. 2. Enable optional paths (a) don't count optional load temporaries as dependencies (e.g. `collectOptionalLoadRValues(...)`). (b) add optional paths back. This is a bit tricky as we'll want to implement some merging logic for `ConditionalAccess | OptionalChain | UnconditionalAccess`. In addition, our current optional chain lowering is slightly incorrect / imprecise [ghstack-poisoned]
- Loading branch information
Showing
67 changed files
with
1,896 additions
and
1,736 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.github/workflows/compiler-playground.yml → .github/workflows/compiler_playground.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Compiler Playground | ||
name: (Compiler) Playground | ||
|
||
on: | ||
push: | ||
|
2 changes: 1 addition & 1 deletion
2
.github/workflows/compiler-rust.yml → .github/workflows/compiler_rust.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: React Compiler (Rust) | ||
name: (Compiler) Rust | ||
|
||
on: | ||
push: | ||
|
2 changes: 1 addition & 1 deletion
2
.github/workflows/compiler-typescript.yml → .github/workflows/compiler_typescript.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: React Compiler (TypeScript) | ||
name: (Compiler) TypeScript | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: DevTools Check for bug repro | ||
name: (DevTools) Check for bug repro | ||
on: | ||
issues: | ||
types: [opened, edited] | ||
|
2 changes: 1 addition & 1 deletion
2
.github/workflows/commit_artifacts.yml → ...ub/workflows/runtime_commit_artifacts.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: (Runtime) Fizz | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
paths-ignore: | ||
- 'compiler/**' | ||
|
||
jobs: | ||
check_generated_fizz_runtime: | ||
name: Confirm generated inline Fizz runtime is up to date | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18.x | ||
cache: "yarn" | ||
cache-dependency-path: yarn.lock | ||
- name: Restore cached node_modules | ||
uses: actions/cache@v4 | ||
id: node_modules | ||
with: | ||
path: "**/node_modules" | ||
key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }} | ||
- run: yarn install --frozen-lockfile | ||
- run: | | ||
yarn generate-inline-fizz-runtime | ||
git diff --quiet || (echo "There was a change to the Fizz runtime. Run `yarn generate-inline-fizz-runtime` and check in the result." && false) |
2 changes: 1 addition & 1 deletion
2
.github/workflows/flags.yml → .github/workflows/runtime_flags.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Flags | ||
name: (Runtime) Flags | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
name: (Runtime) Test | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
paths-ignore: | ||
- 'compiler/**' | ||
|
||
env: | ||
# Number of workers (one per shard) to spawn | ||
SHARD_COUNT: 5 | ||
|
||
jobs: | ||
# Define the various test parameters and parallelism for this workflow | ||
build_test_params: | ||
name: Build test params | ||
runs-on: ubuntu-latest | ||
outputs: | ||
params: ${{ steps.define-params.outputs.result }} | ||
shard_id: ${{ steps.define-shards.outputs.result }} | ||
steps: | ||
- uses: actions/github-script@v7 | ||
id: define-shards | ||
with: | ||
script: | | ||
function range(from, to) { | ||
const arr = []; | ||
for (let n = from; n <= to; n++) { | ||
arr.push(n); | ||
} | ||
return arr; | ||
} | ||
return range(1, process.env.SHARD_COUNT); | ||
- uses: actions/github-script@v7 | ||
id: define-params | ||
with: | ||
script: | | ||
return [ | ||
"-r=stable --env=development", | ||
"-r=stable --env=production", | ||
"-r=experimental --env=development", | ||
"-r=experimental --env=production", | ||
"-r=www-classic --env=development --variant=false", | ||
"-r=www-classic --env=production --variant=false", | ||
"-r=www-classic --env=development --variant=true", | ||
"-r=www-classic --env=production --variant=true", | ||
"-r=www-modern --env=development --variant=false", | ||
"-r=www-modern --env=production --variant=false", | ||
"-r=www-modern --env=development --variant=true", | ||
"-r=www-modern --env=production --variant=true", | ||
"-r=xplat --env=development --variant=false", | ||
"-r=xplat --env=development --variant=true", | ||
"-r=xplat --env=production --variant=false", | ||
"-r=xplat --env=production --variant=true", | ||
// TODO: Test more persistent configurations? | ||
"-r=stable --env=development --persistent", | ||
"-r=experimental --env=development --persistent" | ||
]; | ||
# Spawn a job for each shard for a given set of test params | ||
test: | ||
name: yarn test ${{ matrix.params }} (Shard ${{ matrix.shard_id }}) | ||
runs-on: ubuntu-latest | ||
needs: build_test_params | ||
strategy: | ||
matrix: | ||
params: ${{ fromJSON(needs.build_test_params.outputs.params) }} | ||
shard_id: ${{ fromJSON(needs.build_test_params.outputs.shard_id) }} | ||
continue-on-error: true | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18.x | ||
cache: "yarn" | ||
cache-dependency-path: yarn.lock | ||
- name: Restore cached node_modules | ||
uses: actions/cache@v4 | ||
id: node_modules | ||
with: | ||
path: "**/node_modules" | ||
key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }} | ||
- run: yarn install --frozen-lockfile | ||
- run: yarn test ${{ matrix.params }} --ci=github --shard=${{ matrix.shard_id }}/${{ env.SHARD_COUNT }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Lint | ||
name: (Shared) Lint | ||
|
||
on: | ||
push: | ||
|
2 changes: 1 addition & 1 deletion
2
.github/workflows/stale.yml → .github/workflows/shared_stale.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.