-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Infer phi types, extend mutable ranges to account for Stor…
…e effects Redo of an earlier (pre-OSS) PR to infer types of phi nodes. There are a few pieces to this: 1. Update InferTypes to infer the type of `phi.id.type`, not the unused `phi.type`. 2. Update the algorithm to verify that all the phi types are actually equal, not just have the same kind. 3. Handle circular types by removing the cycle. However, that reveals another issue: InferMutableRanges currently infers the results of `Store` effects _after_ its fixpoint loop. That was fine when a Store could never occur on a phi (since they wouldn't have a type to get a function signature from). Now though, we can have Store effects occur on phis, and we need to ensure that this correctly updates the mutable range of the phi operands - recursively. See new test that fails without the fixpoint loop. ghstack-source-id: 2e1b02844d3a814dce094b7e3812df799e54343f Pull Request resolved: #30796
- Loading branch information
1 parent
c9c170b
commit 4f54674
Showing
6 changed files
with
313 additions
and
29 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
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
110 changes: 110 additions & 0 deletions
110
...ts__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.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,110 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {makeArray} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const x = {}; | ||
let y; | ||
if (props.cond) { | ||
if (props.cond2) { | ||
y = [props.value]; | ||
} else { | ||
y = [props.value2]; | ||
} | ||
} else { | ||
y = []; | ||
} | ||
// This should be inferred as `<store> y` s.t. `x` can still | ||
// be independently memoized. *But* this also must properly | ||
// extend the mutable range of the array literals in the | ||
// if/else branches | ||
y.push(x); | ||
|
||
return [x, y]; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{cond: true, cond2: true, value: 42}], | ||
sequentialRenders: [ | ||
{cond: true, cond2: true, value: 3.14}, | ||
{cond: true, cond2: true, value: 42}, | ||
{cond: true, cond2: true, value: 3.14}, | ||
{cond: true, cond2: false, value2: 3.14}, | ||
{cond: true, cond2: false, value2: 42}, | ||
{cond: true, cond2: false, value2: 3.14}, | ||
{cond: false}, | ||
{cond: false}, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { makeArray } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
const $ = _c(3); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = {}; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
const x = t0; | ||
let t1; | ||
if ($[1] !== props) { | ||
let y; | ||
if (props.cond) { | ||
if (props.cond2) { | ||
y = [props.value]; | ||
} else { | ||
y = [props.value2]; | ||
} | ||
} else { | ||
y = []; | ||
} | ||
|
||
y.push(x); | ||
|
||
t1 = [x, y]; | ||
$[1] = props; | ||
$[2] = t1; | ||
} else { | ||
t1 = $[2]; | ||
} | ||
return t1; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ cond: true, cond2: true, value: 42 }], | ||
sequentialRenders: [ | ||
{ cond: true, cond2: true, value: 3.14 }, | ||
{ cond: true, cond2: true, value: 42 }, | ||
{ cond: true, cond2: true, value: 3.14 }, | ||
{ cond: true, cond2: false, value2: 3.14 }, | ||
{ cond: true, cond2: false, value2: 42 }, | ||
{ cond: true, cond2: false, value2: 3.14 }, | ||
{ cond: false }, | ||
{ cond: false }, | ||
], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) [{},[3.14,"[[ cyclic ref *1 ]]"]] | ||
[{},[42,"[[ cyclic ref *1 ]]"]] | ||
[{},[3.14,"[[ cyclic ref *1 ]]"]] | ||
[{},[3.14,"[[ cyclic ref *1 ]]"]] | ||
[{},[42,"[[ cyclic ref *1 ]]"]] | ||
[{},[3.14,"[[ cyclic ref *1 ]]"]] | ||
[{},["[[ cyclic ref *1 ]]"]] | ||
[{},["[[ cyclic ref *1 ]]"]] |
37 changes: 37 additions & 0 deletions
37
...ompiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.js
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,37 @@ | ||
import {makeArray} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const x = {}; | ||
let y; | ||
if (props.cond) { | ||
if (props.cond2) { | ||
y = [props.value]; | ||
} else { | ||
y = [props.value2]; | ||
} | ||
} else { | ||
y = []; | ||
} | ||
// This should be inferred as `<store> y` s.t. `x` can still | ||
// be independently memoized. *But* this also must properly | ||
// extend the mutable range of the array literals in the | ||
// if/else branches | ||
y.push(x); | ||
|
||
return [x, y]; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{cond: true, cond2: true, value: 42}], | ||
sequentialRenders: [ | ||
{cond: true, cond2: true, value: 3.14}, | ||
{cond: true, cond2: true, value: 42}, | ||
{cond: true, cond2: true, value: 3.14}, | ||
{cond: true, cond2: false, value2: 3.14}, | ||
{cond: true, cond2: false, value2: 42}, | ||
{cond: true, cond2: false, value2: 3.14}, | ||
{cond: false}, | ||
{cond: false}, | ||
], | ||
}; |
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.