@samitouri / QOS-React-2 / commits / 2683c0a18c

[compiler] Fix mode for generating scopes for reassignments

We have an experimental mode where we generate scopes for simple phi values, even if they aren't subsequently mutated. This mode was incorrectly generating scope ranges, leaving the start at 0 which is invalid. The fix is to allow non-zero identifier ranges to overwrite the scope start (rather than taking the min) if the scope start is still zero. ghstack-source-id: ecbb04c96ed4de62f781e48cda46309c42aa07e0 Pull Request resolved: https://github.com/facebook/react/pull/30321

Joe Savona committed Jul 12, 2024 at 13:00 UTC 2683c0a18c1d0c2834f371d9dc6ef25495a208de
3 files changed +94 -3
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts
+7 -3
@@ -114,9 +114,13 @@ export function inferReactiveScopeVariables(fn: HIRFunction): void {
114 };
115 scopes.set(groupIdentifier, scope);
116 } else {
117 - scope.range.start = makeInstructionId(
118 - Math.min(scope.range.start, identifier.mutableRange.start)
119 - );
117 + if (scope.range.start === 0) {
118 + scope.range.start = identifier.mutableRange.start;
119 + } else if (identifier.mutableRange.start !== 0) {
120 + scope.range.start = makeInstructionId(
121 + Math.min(scope.range.start, identifier.mutableRange.start)
122 + );
123 + }
124 scope.range.end = makeInstructionId(
125 Math.max(scope.range.end, identifier.mutableRange.end)
126 );
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.expect.md new
+68
@@ -0,0 +1,68 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @enableForest
6 +function Component({ base, start, increment, test }) {
7 + let value = base;
8 + for (let i = start; i < test; i += increment) {
9 + value += i;
10 + }
11 + return <div>{value}</div>;
12 +}
13 +
14 +export const FIXTURE_ENTRYPOINT = {
15 + fn: Component,
16 + params: [{ base: 0, start: 0, test: 10, increment: 1 }],
17 + sequentialRenders: [
18 + { base: 0, start: 1, test: 10, increment: 1 },
19 + { base: 0, start: 0, test: 10, increment: 2 },
20 + { base: 2, start: 0, test: 10, increment: 2 },
21 + { base: 0, start: 0, test: 11, increment: 2 },
22 + ],
23 +};
24 +
25 +```
26 +
27 +## Code
28 +
29 +```javascript
30 +import { c as _c } from "react/compiler-runtime"; // @enableForest
31 +function Component(t0) {
32 + const $ = _c(5);
33 + const { base, start, increment, test } = t0;
34 + let value;
35 + if ($[0] !== base || $[1] !== start || $[2] !== test || $[3] !== increment) {
36 + value = base;
37 + for (let i = start; i < test; i = i + increment, i) {
38 + value = value + i;
39 + }
40 + $[0] = base;
41 + $[1] = start;
42 + $[2] = test;
43 + $[3] = increment;
44 + $[4] = value;
45 + } else {
46 + value = $[4];
47 + }
48 + return <div>{value}</div>;
49 +}
50 +
51 +export const FIXTURE_ENTRYPOINT = {
52 + fn: Component,
53 + params: [{ base: 0, start: 0, test: 10, increment: 1 }],
54 + sequentialRenders: [
55 + { base: 0, start: 1, test: 10, increment: 1 },
56 + { base: 0, start: 0, test: 10, increment: 2 },
57 + { base: 2, start: 0, test: 10, increment: 2 },
58 + { base: 0, start: 0, test: 11, increment: 2 },
59 + ],
60 +};
61 +
62 +```
63 +
64 +### Eval output
65 +(kind: ok) <div>45</div>
66 +<div>20</div>
67 +<div>22</div>
68 +<div>30</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.js new
+19
@@ -0,0 +1,19 @@
1 +// @enableForest
2 +function Component({ base, start, increment, test }) {
3 + let value = base;
4 + for (let i = start; i < test; i += increment) {
5 + value += i;
6 + }
7 + return <div>{value}</div>;
8 +}
9 +
10 +export const FIXTURE_ENTRYPOINT = {
11 + fn: Component,
12 + params: [{ base: 0, start: 0, test: 10, increment: 1 }],
13 + sequentialRenders: [
14 + { base: 0, start: 1, test: 10, increment: 1 },
15 + { base: 0, start: 0, test: 10, increment: 2 },
16 + { base: 2, start: 0, test: 10, increment: 2 },
17 + { base: 0, start: 0, test: 11, increment: 2 },
18 + ],
19 +};