[hir] Validate globals are equal before propagating

Make sure the value of the globals in phi operands are the same before constant 
propagating them.
This commit is contained in:
Sathya Gunasekaran
2023-08-16 15:39:01 +01:00
parent b686b5dd9d
commit b968d4da48
3 changed files with 52 additions and 0 deletions
@@ -206,6 +206,19 @@ function evaluatePhi(phi: Phi, constants: Constants): Constant | null {
}
break;
}
case "LoadGlobal": {
CompilerError.invariant(value.kind === "LoadGlobal", {
reason: "value kind expected to be LoadGlobal",
loc: null,
suggestions: null,
});
// different global values, can't constant propogate
if (operandValue.name !== value.name) {
return null;
}
break;
}
default:
return null;
}
@@ -0,0 +1,33 @@
## Input
```javascript
function Test() {
const { tab } = useFoo();
const currentTab = tab === WAT ? WAT : WAT;
return <Foo value={currentTab} />;
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Test() {
const $ = useMemoCache(1);
const { tab } = useFoo();
tab === WAT ? WAT : WAT;
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <Foo value={WAT} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
```
@@ -0,0 +1,6 @@
function Test() {
const { tab } = useFoo();
const currentTab = tab === WAT ? WAT : WAT;
return <Foo value={currentTab} />;
}