Extend mutable ranges if a phi is mutated after creation
Lauren Tan committed
Feb 8, 2023 at 10:26 UTC
6f661f3b799bc4aea96be2d6a9808801a11327b3
2 files changed
+15
-20
compiler/forget/src/SSA/LeaveSSA.ts
+5
-4
@@ -200,7 +200,8 @@ export function leaveSSA(fn: HIRFunction) {
200
// TODO(joe): This above statement is true, right? Could there be a value
201
// block with instructions in terminals?
202
const isPhiMutatedAfterCreation: boolean =
203
- end > (phiBlock.instructions.at(0)?.id ?? end);
203
+ phi.id.mutableRange.end >
204
+ (phiBlock.instructions.at(0)?.id ?? phiBlock.terminal.id);
205
206
// If this phi id is the canonical id we need to generate a let binding for it
207
// (otherwise, it means this phi merges into some other phi which already generated
@@ -241,12 +242,12 @@ export function leaveSSA(fn: HIRFunction) {
242
243
// Generate an assignment in each predecessor
244
for (const [predecessorId, operand] of phi.operands) {
245
+ if (isPhiMutatedAfterCreation) {
246
+ operand.mutableRange.end = phi.id.mutableRange.end;
247
+ }
248
if (operand === initOperand) {
249
continue;
250
}
247
- if (isPhiMutatedAfterCreation) {
248
- operand.mutableRange.end = canonicalId.mutableRange.end;
249
- }
251
const predecessor = fn.body.blocks.get(predecessorId)!;
252
const instr: Instruction = {
253
id: predecessor.terminal.id,
compiler/forget/src/__tests__/fixtures/hir/ssa-renaming-with-mutation.expect.md
+10
-16
@@ -22,19 +22,12 @@ function foo(props) {
22
function foo(props) {
23
const $ = React.unstable_useMemoCache();
24
const c_0 = $[0] !== props.bar;
25
- let x;
26
- if (c_0) {
27
- x = [];
28
- x.push(props.bar);
29
- $[0] = props.bar;
30
- $[1] = x;
31
- } else {
32
- x = $[1];
33
- }
34
- const c_2 = $[2] !== props.cond;
35
- const c_3 = $[3] !== props.foo;
25
+ const c_1 = $[1] !== props.cond;
26
+ const c_2 = $[2] !== props.foo;
27
let x$0;
37
- if (c_2 || c_3) {
28
+ if (c_0 || c_1 || c_2) {
29
+ const x = [];
30
+ x.push(props.bar);
31
x$0 = x;
32
if (props.cond) {
33
const x$1 = [];
@@ -43,11 +36,12 @@ function foo(props) {
36
}
37
38
mut(x$0);
46
- $[2] = props.cond;
47
- $[3] = props.foo;
48
- $[4] = x$0;
39
+ $[0] = props.bar;
40
+ $[1] = props.cond;
41
+ $[2] = props.foo;
42
+ $[3] = x$0;
43
} else {
50
- x$0 = $[4];
44
+ x$0 = $[3];
45
}
46
return x$0;
47
}