[hir] Use type inference for Effect.Store refinement
Only identifiers of type object can be safely refined to Effect.Store, default unknown identifiers to Effect.Mutate.
Sathya Gunasekaran committed
Dec 21, 2022 at 21:13 UTC
6ba211722756a78cd873ab126a3da9940f58b013
3 files changed
+15
-8
compiler/forget/src/HIR/HIR.ts
+4
@@ -522,3 +522,7 @@ function objectTypeEquals(tA: Type, tB: Type): boolean {
522
function funcTypeEquals(tA: Type, tB: Type): boolean {
523
return tA.kind === "Function" && tB.kind === "Function";
524
}
525
+
526
+export function isObjectType(id: Identifier): boolean {
527
+ return id.type.kind === "Object";
528
+}
compiler/forget/src/HIR/InferReferenceEffects.ts
+7
-4
@@ -14,6 +14,7 @@ import {
14
HIRFunction,
15
IdentifierId,
16
InstructionValue,
17
+ isObjectType,
18
Phi,
19
Place,
20
ValueKind,
@@ -585,12 +586,14 @@ function inferBlock(env: Environment, block: BasicBlock) {
586
// redefine lvalue: `a = b.c.d`
587
env.initialize(instrValue, env.kind(instrValue));
588
env.define(lvalue.place, instrValue);
588
- } else if (instrValue.memberPath === null) {
589
- // no-op: `a.b.c = d`
590
- env.reference(lvalue.place, Effect.Store);
589
} else {
590
+ // no-op: `a.b.c = d`
591
+ // or
592
// no-op: `a.b.c = d.e.f`
593
- env.reference(lvalue.place, Effect.Store);
593
+ const effect = isObjectType(lvalue.place.identifier)
594
+ ? Effect.Store
595
+ : Effect.Mutate;
596
+ env.reference(lvalue.place, effect);
597
}
598
}
599
continue;
compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md
+4
-4
@@ -63,8 +63,8 @@ function f$0() {
63
```
64
bb0:
65
[1] Const mutate $6:TPrimitive = 1
66
- [2] Const mutate $7_@0:TPrimitive[0:6] = Binary read a$5_@0.b.c + read $6:TPrimitive
67
- [3] Reassign store a$5_@0.b.c[0:6] = read $7_@0:TPrimitive
66
+ [2] Const mutate $7:TPrimitive = Binary read a$5_@0.b.c + read $6:TPrimitive
67
+ [3] Reassign read a$5_@0.b.c[0:6] = read $7:TPrimitive
68
[4] Const mutate $8:TPrimitive = 2
69
[5] Reassign mutate a$5_@0.b.c[0:6] = Binary read a$5_@0.b.c * read $8:TPrimitive
70
[6] Return
@@ -77,8 +77,8 @@ function g(
77
a,
78
) {
79
[1] Const mutate $6:TPrimitive = 1
80
- [2] Const mutate $7_@0:TPrimitive[0:6] = Binary read a$5_@0.b.c + read $6:TPrimitive
81
- [3] Reassign store a$5_@0.b.c[0:6] = read $7_@0:TPrimitive
80
+ [2] Const mutate $7:TPrimitive = Binary read a$5_@0.b.c + read $6:TPrimitive
81
+ [3] Reassign read a$5_@0.b.c[0:6] = read $7:TPrimitive
82
[4] Const mutate $8:TPrimitive = 2
83
[5] Reassign mutate a$5_@0.b.c[0:6] = Binary read a$5_@0.b.c * read $8:TPrimitive
84
return