@samitouri / QOS-React-2 / commits / ec2a179572

[be] Move phi aliasing to InferAliasForPhis

InferMutableRangesForAlias is about extending the mutable ranges, not for updating the alias sets. Let's refactor this into a separate pass. InferMutableRangesForAlias was iterating over alias sets and not the HIR so this refactor isn't costing us any additional perf cost (in terms of an extra iteration over the HIR).

Sathya Gunasekaran committed Mar 3, 2023 at 13:54 UTC ec2a179572401a2d30cd1a14a07f54da59497982
3 files changed +30 -13
compiler/forget/src/Inference/InferAliasForPhis.ts new
+26
@@ -0,0 +1,26 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +import { HIRFunction, Identifier } from "../HIR/HIR";
8 +import DisjointSet from "../Utils/DisjointSet";
9 +
10 +export function inferAliasForPhis(
11 + func: HIRFunction,
12 + aliases: DisjointSet<Identifier>
13 +) {
14 + for (const [_, block] of func.body.blocks) {
15 + for (const phi of block.phis) {
16 + const isPhiMutatedAfterCreation: boolean =
17 + phi.id.mutableRange.end >
18 + (block.instructions.at(0)?.id ?? block.terminal.id);
19 + if (isPhiMutatedAfterCreation) {
20 + for (const [, operand] of phi.operands) {
21 + aliases.union([phi.id, operand]);
22 + }
23 + }
24 + }
25 + }
26 +}
compiler/forget/src/Inference/InferMutableRanges.ts
+4
@@ -7,6 +7,7 @@
7
8 import { HIRFunction, Identifier } from "../HIR/HIR";
9 import { inferAliases } from "./InferAlias";
10 +import { inferAliasForPhis } from "./InferAliasForPhis";
11 import { inferAliasForStores } from "./InferAliasForStores";
12 import { inferMutableLifetimes } from "./InferMutableLifetimes";
13 import { inferMutableRangesForAlias } from "./InferMutableRangesForAlias";
@@ -27,6 +28,9 @@ export function inferMutableRanges(ir: HIRFunction) {
28 // Update aliasing information of fields
29 inferAliasForStores(ir, aliases);
30
31 + // Update aliasing information of phis
32 + inferAliasForPhis(ir, aliases);
33 +
34 const nextAliases = aliases.canonicalize();
35 if (areEqualMaps(prevAliases, nextAliases)) {
36 break;
compiler/forget/src/Inference/InferMutableRangesForAlias.ts
-13
@@ -31,17 +31,4 @@ export function inferMutableRangesForAlias(
31 }
32 }
33 }
34 -
35 - for (const [_, block] of fn.body.blocks) {
36 - for (const phi of block.phis) {
37 - const isPhiMutatedAfterCreation: boolean =
38 - phi.id.mutableRange.end >
39 - (block.instructions.at(0)?.id ?? block.terminal.id);
40 - if (isPhiMutatedAfterCreation) {
41 - for (const [, operand] of phi.operands) {
42 - aliases.union([phi.id, operand]);
43 - }
44 - }
45 - }
46 - }
34 }