Skip to content

Commit

Permalink
[compiler] Fix merging of queues states in InferReferenceEffects
Browse files Browse the repository at this point in the history
Fixes a bug found by mofeiZ in #29878. When we merge queues states, if the new state does not introduce changes relative to the queued state we should use the queued state, not the new state.

ghstack-source-id: cb931b77f82a118fc6e434f2cd7f6368f9add3d4
Pull Request resolved: #29879
  • Loading branch information
josephsavona committed Jun 12, 2024
1 parent 195d5bb commit af17497
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from "../HIR/HIR";
import { FunctionSignature } from "../HIR/ObjectShape";
import {
printFunction,
printIdentifier,
printMixedHIR,
printPlace,
Expand Down Expand Up @@ -201,7 +202,7 @@ export default function inferReferenceEffects(
let queuedState = queuedStates.get(blockId);
if (queuedState != null) {
// merge the queued states for this block
state = queuedState.merge(state) ?? state;
state = queuedState.merge(state) ?? queuedState;
queuedStates.set(blockId, state);
} else {
/*
Expand Down Expand Up @@ -765,7 +766,7 @@ class InferenceState {
result.values[id] = { kind, value: printMixedHIR(value) };
}
for (const [variable, values] of this.#variables) {
result.variables[variable] = [...values].map(identify);
result.variables[`$${variable}`] = [...values].map(identify);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

## Input

```javascript
import { arrayPush } from "shared-runtime";

function Foo(cond) {
let x = null;
if (cond) {
x = [];
} else {
}
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
arrayPush(x, 2);

return x;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ cond: true }],
sequentialRenders: [{ cond: true }, { cond: true }],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import { arrayPush } from "shared-runtime";

function Foo(cond) {
const $ = _c(2);
let x;
if ($[0] !== cond) {
x = null;
if (cond) {
x = [];
}

arrayPush(x, 2);
$[0] = cond;
$[1] = x;
} else {
x = $[1];
}
return x;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ cond: true }],
sequentialRenders: [{ cond: true }, { cond: true }],
};

```
### Eval output
(kind: ok) [2]
[2]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { arrayPush } from "shared-runtime";

function Foo(cond) {
let x = null;
if (cond) {
x = [];
} else {
}
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
arrayPush(x, 2);

return x;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ cond: true }],
sequentialRenders: [{ cond: true }, { cond: true }],
};

0 comments on commit af17497

Please sign in to comment.