-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler][rewrite] Patch logic for aligning scopes to non-value blocks
Our previous logic for aligning scopes to block scopes constructs a tree of block and scope nodes. We ensured that blocks always mapped to the same node as their fallthroughs. e.g. ```js // source a(); if (...) { b(); } c(); // HIR bb0: a() if test=... consequent=bb1 fallthrough=bb2 bb1: b() goto bb2 bb2: c() // AlignReactiveScopesToBlockScopesHIR nodes Root node (maps to both bb0 and bb2) |- bb1 |- ... ``` There are two issues with the existing implementation: 1. Only scopes that overlap with the beginning of a block are aligned correctly. This is because the traversal does not store information about the block-fallthrough pair for scopes that begin *within* the block-fallthrough range. ``` \# This case gets handled correctly ┌──────────────┐ │ │ block start block end scope start scope end │ │ └───────────────┘ \# But not this one! ┌──────────────┐ │ │ block start block end scope start scope end │ │ └───────────────┘ ``` 2. Only scopes that are directly used by a block is considered. See the `align-scopes-nested-block-structure` fixture for details. ghstack-source-id: d9051284cec15caa1e0a6840cd20ead7acff122a Pull Request resolved: #29891
- Loading branch information
Showing
28 changed files
with
828 additions
and
411 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
76 changes: 76 additions & 0 deletions
76
...mpiler/src/__tests__/fixtures/compiler/align-scope-starts-within-cond.expect.md
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,76 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { mutate } from "shared-runtime"; | ||
|
||
/** | ||
* Similar fixture to `align-scopes-nested-block-structure`, but | ||
* a simpler case. | ||
*/ | ||
function useFoo(cond) { | ||
let s = null; | ||
if (cond) { | ||
s = {}; | ||
} else { | ||
return null; | ||
} | ||
mutate(s); | ||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { mutate } from "shared-runtime"; | ||
|
||
/** | ||
* Similar fixture to `align-scopes-nested-block-structure`, but | ||
* a simpler case. | ||
*/ | ||
function useFoo(cond) { | ||
const $ = _c(3); | ||
let s; | ||
let t0; | ||
if ($[0] !== cond) { | ||
t0 = Symbol.for("react.early_return_sentinel"); | ||
bb0: { | ||
if (cond) { | ||
s = {}; | ||
} else { | ||
t0 = null; | ||
break bb0; | ||
} | ||
|
||
mutate(s); | ||
} | ||
$[0] = cond; | ||
$[1] = t0; | ||
$[2] = s; | ||
} else { | ||
t0 = $[1]; | ||
s = $[2]; | ||
} | ||
if (t0 !== Symbol.for("react.early_return_sentinel")) { | ||
return t0; | ||
} | ||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) {"wat0":"joe"} |
21 changes: 21 additions & 0 deletions
21
...l-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scope-starts-within-cond.ts
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,21 @@ | ||
import { mutate } from "shared-runtime"; | ||
|
||
/** | ||
* Similar fixture to `align-scopes-nested-block-structure`, but | ||
* a simpler case. | ||
*/ | ||
function useFoo(cond) { | ||
let s = null; | ||
if (cond) { | ||
s = {}; | ||
} else { | ||
return null; | ||
} | ||
mutate(s); | ||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; |
Oops, something went wrong.