Skip to content

Commit 0596ff3

Browse files
committed
[compiler][fixtures] Bug repros: codegen, alignScope, phis
ghstack-source-id: 2dfcfcd Pull Request resolved: #29878
1 parent 55c9d45 commit 0596ff3

9 files changed

+401
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
## Input
3+
4+
```javascript
5+
import { makeArray, print } from "shared-runtime";
6+
7+
/**
8+
* Exposes bug involving iife inlining + codegen.
9+
* We currently inline iifes to labeled blocks (not value-blocks).
10+
*
11+
* Here, print(1) and the evaluation of makeArray(...) get the same scope
12+
* as the compiler infers that the makeArray call may mutate its arguments.
13+
* Since print(1) does not get its own scope (and is thus not a declaration
14+
* or dependency), it does not get promoted.
15+
* As a result, print(1) gets reordered across the labeled-block instructions
16+
* to be inlined at the makeArray callsite.
17+
*
18+
* Current evaluator results:
19+
* Found differences in evaluator results
20+
* Non-forget (expected):
21+
* (kind: ok) [null,2]
22+
* logs: [1,2]
23+
* Forget:
24+
* (kind: ok) [null,2]
25+
* logs: [2,1]
26+
*/
27+
function useTest() {
28+
return makeArray<number | void>(
29+
print(1),
30+
(function foo() {
31+
print(2);
32+
return 2;
33+
})()
34+
);
35+
}
36+
37+
export const FIXTURE_ENTRYPOINT = {
38+
fn: useTest,
39+
params: [],
40+
};
41+
42+
```
43+
44+
## Code
45+
46+
```javascript
47+
import { c as _c } from "react/compiler-runtime";
48+
import { makeArray, print } from "shared-runtime";
49+
50+
/**
51+
* Exposes bug involving iife inlining + codegen.
52+
* We currently inline iifes to labeled blocks (not value-blocks).
53+
*
54+
* Here, print(1) and the evaluation of makeArray(...) get the same scope
55+
* as the compiler infers that the makeArray call may mutate its arguments.
56+
* Since print(1) does not get its own scope (and is thus not a declaration
57+
* or dependency), it does not get promoted.
58+
* As a result, print(1) gets reordered across the labeled-block instructions
59+
* to be inlined at the makeArray callsite.
60+
*
61+
* Current evaluator results:
62+
* Found differences in evaluator results
63+
* Non-forget (expected):
64+
* (kind: ok) [null,2]
65+
* logs: [1,2]
66+
* Forget:
67+
* (kind: ok) [null,2]
68+
* logs: [2,1]
69+
*/
70+
function useTest() {
71+
const $ = _c(1);
72+
let t0;
73+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
74+
let t1;
75+
76+
print(2);
77+
t1 = 2;
78+
t0 = makeArray(print(1), t1);
79+
$[0] = t0;
80+
} else {
81+
t0 = $[0];
82+
}
83+
return t0;
84+
}
85+
86+
export const FIXTURE_ENTRYPOINT = {
87+
fn: useTest,
88+
params: [],
89+
};
90+
91+
```
92+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { makeArray, print } from "shared-runtime";
2+
3+
/**
4+
* Exposes bug involving iife inlining + codegen.
5+
* We currently inline iifes to labeled blocks (not value-blocks).
6+
*
7+
* Here, print(1) and the evaluation of makeArray(...) get the same scope
8+
* as the compiler infers that the makeArray call may mutate its arguments.
9+
* Since print(1) does not get its own scope (and is thus not a declaration
10+
* or dependency), it does not get promoted.
11+
* As a result, print(1) gets reordered across the labeled-block instructions
12+
* to be inlined at the makeArray callsite.
13+
*
14+
* Current evaluator results:
15+
* Found differences in evaluator results
16+
* Non-forget (expected):
17+
* (kind: ok) [null,2]
18+
* logs: [1,2]
19+
* Forget:
20+
* (kind: ok) [null,2]
21+
* logs: [2,1]
22+
*/
23+
function useTest() {
24+
return makeArray<number | void>(
25+
print(1),
26+
(function foo() {
27+
print(2);
28+
return 2;
29+
})()
30+
);
31+
}
32+
33+
export const FIXTURE_ENTRYPOINT = {
34+
fn: useTest,
35+
params: [],
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
## Input
3+
4+
```javascript
5+
import { arrayPush } from "shared-runtime";
6+
7+
/**
8+
* Evaluator error:
9+
* Found differences in evaluator results
10+
* Non-forget (expected):
11+
* (kind: ok) [2]
12+
* [2]
13+
* Forget:
14+
* (kind: ok) [2]
15+
* [2,2]
16+
*/
17+
function Foo(cond) {
18+
let x = null;
19+
if (cond) {
20+
x = [];
21+
} else {
22+
}
23+
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
24+
arrayPush(x, 2);
25+
26+
return x;
27+
}
28+
29+
export const FIXTURE_ENTRYPOINT = {
30+
fn: Foo,
31+
params: [{ cond: true }],
32+
sequentialRenders: [{ cond: true }, { cond: true }],
33+
};
34+
35+
```
36+
37+
## Code
38+
39+
```javascript
40+
import { c as _c } from "react/compiler-runtime";
41+
import { arrayPush } from "shared-runtime";
42+
43+
/**
44+
* Evaluator error:
45+
* Found differences in evaluator results
46+
* Non-forget (expected):
47+
* (kind: ok) [2]
48+
* [2]
49+
* Forget:
50+
* (kind: ok) [2]
51+
* [2,2]
52+
*/
53+
function Foo(cond) {
54+
const $ = _c(1);
55+
let x = null;
56+
if (cond) {
57+
let t0;
58+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
59+
t0 = [];
60+
$[0] = t0;
61+
} else {
62+
t0 = $[0];
63+
}
64+
x = t0;
65+
}
66+
67+
arrayPush(x, 2);
68+
return x;
69+
}
70+
71+
export const FIXTURE_ENTRYPOINT = {
72+
fn: Foo,
73+
params: [{ cond: true }],
74+
sequentialRenders: [{ cond: true }, { cond: true }],
75+
};
76+
77+
```
78+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { arrayPush } from "shared-runtime";
2+
3+
/**
4+
* Evaluator error:
5+
* Found differences in evaluator results
6+
* Non-forget (expected):
7+
* (kind: ok) [2]
8+
* [2]
9+
* Forget:
10+
* (kind: ok) [2]
11+
* [2,2]
12+
*/
13+
function Foo(cond) {
14+
let x = null;
15+
if (cond) {
16+
x = [];
17+
} else {
18+
}
19+
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
20+
arrayPush(x, 2);
21+
22+
return x;
23+
}
24+
25+
export const FIXTURE_ENTRYPOINT = {
26+
fn: Foo,
27+
params: [{ cond: true }],
28+
sequentialRenders: [{ cond: true }, { cond: true }],
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
## Input
3+
4+
```javascript
5+
/**
6+
* Similar fixture to `error.todo-align-scopes-nested-block-structure`, but
7+
* a simpler case.
8+
*/
9+
function useFoo(cond) {
10+
let s = null;
11+
if (cond) {
12+
s = {};
13+
} else {
14+
return null;
15+
}
16+
mutate(s);
17+
return s;
18+
}
19+
20+
```
21+
22+
23+
## Error
24+
25+
```
26+
Invariant: Invalid nesting in program blocks or scopes. Items overlap but are not nested: 4:10(5:13)
27+
```
28+
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Similar fixture to `error.todo-align-scopes-nested-block-structure`, but
3+
* a simpler case.
4+
*/
5+
function useFoo(cond) {
6+
let s = null;
7+
if (cond) {
8+
s = {};
9+
} else {
10+
return null;
11+
}
12+
mutate(s);
13+
return s;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
## Input
3+
4+
```javascript
5+
/**
6+
* Fixture showing that it's not sufficient to only align direct scoped
7+
* accesses of a block-fallthrough pair.
8+
* Below is a simplified view of HIR blocks in this fixture.
9+
* Note that here, s is mutated in both bb1 and bb4. However, neither
10+
* bb1 nor bb4 have terminal fallthroughs or are fallthroughs themselves.
11+
*
12+
* This means that we need to recursively visit all scopes accessed between
13+
* a block and its fallthrough and extend the range of those scopes which overlap
14+
* with an active block/fallthrough pair,
15+
*
16+
* bb0
17+
* ┌──────────────┐
18+
* │let s = null │
19+
* │test cond1 │
20+
* │ <fallthr=bb3>│
21+
* └┬─────────────┘
22+
* │ bb1
23+
* ├─►┌───────┐
24+
* │ │s = {} ├────┐
25+
* │ └───────┘ │
26+
* │ bb2 │
27+
* └─►┌───────┐ │
28+
* │return;│ │
29+
* └───────┘ │
30+
* bb3 │
31+
* ┌──────────────┐◄┘
32+
* │test cond2 │
33+
* │ <fallthr=bb5>│
34+
* └┬─────────────┘
35+
* │ bb4
36+
* ├─►┌─────────┐
37+
* │ │mutate(s)├─┐
38+
* ▼ └─────────┘ │
39+
* bb5 │
40+
* ┌───────────┐ │
41+
* │return s; │◄──┘
42+
* └───────────┘
43+
*/
44+
function useFoo(cond1, cond2) {
45+
let s = null;
46+
if (cond1) {
47+
s = {};
48+
} else {
49+
return null;
50+
}
51+
52+
if (cond2) {
53+
mutate(s);
54+
}
55+
56+
return s;
57+
}
58+
59+
```
60+
61+
62+
## Error
63+
64+
```
65+
Invariant: Invalid nesting in program blocks or scopes. Items overlap but are not nested: 4:10(5:15)
66+
```
67+
68+

0 commit comments

Comments
 (0)