[hir] Don't redefine context refs in EnterSSA

Context references are special since reassignments need to update the original 
reference, not define a new place.
This commit is contained in:
Sathya Gunasekaran
2023-03-03 13:35:04 +00:00
parent 1e5e75746d
commit 1fffc842bc
3 changed files with 19 additions and 16 deletions
+13 -1
View File
@@ -36,6 +36,7 @@ class SSABuilder {
#blocks: Map<BlockId, BasicBlock>;
#env: Environment;
#unknown: Set<Identifier> = new Set();
#context: Set<Identifier> = new Set();
constructor(env: Environment, blocks: Map<BlockId, BasicBlock>) {
this.#blocks = blocks;
@@ -67,6 +68,12 @@ class SSABuilder {
};
}
defineContext(oldPlace: Place): Place {
const newPlace = this.definePlace(oldPlace);
this.#context.add(oldPlace.identifier);
return newPlace;
}
definePlace(oldPlace: Place): Place {
const oldId = oldPlace.identifier;
if (this.#unknown.has(oldId)) {
@@ -78,6 +85,11 @@ class SSABuilder {
);
}
// Do not redefine context references.
if (this.#context.has(oldId)) {
return this.getPlace(oldPlace);
}
const newId = this.makeId(oldId);
this.state().defs.set(oldId, newId);
return {
@@ -206,7 +218,7 @@ export default function enterSSA(func: HIRFunction): void {
builder.startBlock(block);
if (func.body.entry === blockId) {
func.context = func.context.map((p) => builder.definePlace(p));
func.context = func.context.map((p) => builder.defineContext(p));
func.params = func.params.map((p) => builder.definePlace(p));
}
@@ -18,29 +18,20 @@ function component(a) {
```javascript
function component(a) {
const $ = React.unstable_useMemoCache(4);
const $ = React.unstable_useMemoCache(2);
const c_0 = $[0] !== a;
let t0;
if (c_0) {
t0 = { a: a };
$[0] = a;
$[1] = t0;
} else {
t0 = $[1];
}
const x = t0;
const c_2 = $[2] !== x;
let y;
if (c_2) {
if (c_0) {
const x = { a: a };
y = {};
(function () {
y = x;
})();
mutate(y);
$[2] = x;
$[3] = y;
$[0] = a;
$[1] = y;
} else {
y = $[3];
y = $[1];
}
return y;
}