From ec2a179572401a2d30cd1a14a07f54da59497982 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 3 Mar 2023 13:54:52 +0000 Subject: [PATCH] [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). --- .../forget/src/Inference/InferAliasForPhis.ts | 26 +++++++++++++++++++ .../src/Inference/InferMutableRanges.ts | 4 +++ .../Inference/InferMutableRangesForAlias.ts | 13 ---------- 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 compiler/forget/src/Inference/InferAliasForPhis.ts diff --git a/compiler/forget/src/Inference/InferAliasForPhis.ts b/compiler/forget/src/Inference/InferAliasForPhis.ts new file mode 100644 index 0000000000..0059cd3003 --- /dev/null +++ b/compiler/forget/src/Inference/InferAliasForPhis.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +import { HIRFunction, Identifier } from "../HIR/HIR"; +import DisjointSet from "../Utils/DisjointSet"; + +export function inferAliasForPhis( + func: HIRFunction, + aliases: DisjointSet +) { + for (const [_, block] of func.body.blocks) { + for (const phi of block.phis) { + const isPhiMutatedAfterCreation: boolean = + phi.id.mutableRange.end > + (block.instructions.at(0)?.id ?? block.terminal.id); + if (isPhiMutatedAfterCreation) { + for (const [, operand] of phi.operands) { + aliases.union([phi.id, operand]); + } + } + } + } +} diff --git a/compiler/forget/src/Inference/InferMutableRanges.ts b/compiler/forget/src/Inference/InferMutableRanges.ts index 0253fe2845..c5c168371f 100644 --- a/compiler/forget/src/Inference/InferMutableRanges.ts +++ b/compiler/forget/src/Inference/InferMutableRanges.ts @@ -7,6 +7,7 @@ import { HIRFunction, Identifier } from "../HIR/HIR"; import { inferAliases } from "./InferAlias"; +import { inferAliasForPhis } from "./InferAliasForPhis"; import { inferAliasForStores } from "./InferAliasForStores"; import { inferMutableLifetimes } from "./InferMutableLifetimes"; import { inferMutableRangesForAlias } from "./InferMutableRangesForAlias"; @@ -27,6 +28,9 @@ export function inferMutableRanges(ir: HIRFunction) { // Update aliasing information of fields inferAliasForStores(ir, aliases); + // Update aliasing information of phis + inferAliasForPhis(ir, aliases); + const nextAliases = aliases.canonicalize(); if (areEqualMaps(prevAliases, nextAliases)) { break; diff --git a/compiler/forget/src/Inference/InferMutableRangesForAlias.ts b/compiler/forget/src/Inference/InferMutableRangesForAlias.ts index 95b96c8ef7..4de85bec4b 100644 --- a/compiler/forget/src/Inference/InferMutableRangesForAlias.ts +++ b/compiler/forget/src/Inference/InferMutableRangesForAlias.ts @@ -31,17 +31,4 @@ export function inferMutableRangesForAlias( } } } - - for (const [_, block] of fn.body.blocks) { - for (const phi of block.phis) { - const isPhiMutatedAfterCreation: boolean = - phi.id.mutableRange.end > - (block.instructions.at(0)?.id ?? block.terminal.id); - if (isPhiMutatedAfterCreation) { - for (const [, operand] of phi.operands) { - aliases.union([phi.id, operand]); - } - } - } - } }