Skip to content

Commit

Permalink
[AIE2] Alias Analysis using AddrSpace Info
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnamtibrewala committed Oct 17, 2024
1 parent 5bced31 commit 7c9cda7
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 6 deletions.
16 changes: 16 additions & 0 deletions llvm/lib/Target/AIE/AIE2TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,19 @@ AIE2TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
}
return std::nullopt;
}

bool AIE2TTIImpl::addrspacesMayAlias(unsigned AS0, unsigned AS1) const {
if (AS0 == AS1)
return true;

// Tile Memory and Data Memory are disjoint, since we allways annotate Tile
// Memory access even if another address space is not attonated we can assume
// that they are disjoint.
const unsigned TileMemoryAS = static_cast<unsigned>(AIE2::AddressSpaces::TM);
if (AS0 == TileMemoryAS || AS1 == TileMemoryAS)
return false;

const AIEBaseAddrSpaceInfo &ASI = ST->getAddrSpaceInfo();
return ASI.getMemoryBanksFromAddressSpace(AS0) &
ASI.getMemoryBanksFromAddressSpace(AS1);
}
2 changes: 2 additions & 0 deletions llvm/lib/Target/AIE/AIE2TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class AIE2TTIImpl : public BasicTTIImplBase<AIE2TTIImpl> {
bool isProfitableOuterLSR(const Loop &L) const;
std::optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
IntrinsicInst &II) const;

bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const;
};

} // end namespace llvm
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Target/AIE/AIEBaseAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ static cl::opt<bool> DisambiguateAccessSameOriginPointers(
cl::desc("Disambiguate pointers derived from the same origin"),
cl::init(true), cl::Hidden);

static cl::opt<bool>
AddrSpaceAA("aie-alias-analysis-addrspace",
cl::desc("Disambiguate pointers based on address space"),
cl::init(false), cl::Hidden);

#define DEBUG_TYPE "aie-aa"

AnalysisKey AIEBaseAA::Key;
Expand Down Expand Up @@ -69,6 +74,7 @@ AIEBaseAAWrapperPass::AIEBaseAAWrapperPass() : ImmutablePass(ID) {

void AIEBaseAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<TargetTransformInfoWrapperPass>();
}

static bool isAIEPtrAddIntrinsic(Intrinsic::ID ID, unsigned &InPtrIdx) {
Expand Down Expand Up @@ -478,6 +484,14 @@ AliasResult AIEBaseAAResult::alias(const MemoryLocation &LocA,
const Value *BaseA = getUnderlyingObjectAIE(LocA.Ptr);
const Value *BaseB = getUnderlyingObjectAIE(LocB.Ptr);

if (AddrSpaceAA) {
const unsigned AddrSpaceA = LocA.Ptr->getType()->getPointerAddressSpace();
const unsigned AddrSpaceB = LocB.Ptr->getType()->getPointerAddressSpace();

if (!TTI.addrspacesMayAlias(AddrSpaceA, AddrSpaceB))
return AliasResult::NoAlias;
}

if (DisambiguateAccessSameOriginPointers &&
aliasAIEIntrinsic(LocA.Ptr, LocB.Ptr, BaseA, BaseB, 0, 0
/*No virtually unrolled*/) == AliasResult::NoAlias)
Expand Down
22 changes: 16 additions & 6 deletions llvm/lib/Target/AIE/AIEBaseAliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "AIE.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/TargetTransformInfo.h"

namespace llvm {

Expand All @@ -37,11 +38,13 @@ AliasResult aliasAcrossVirtualUnrolls(const MachineInstr *MIA,

class AIEBaseAAResult : public AAResultBase {
const DataLayout &DL;
const TargetTransformInfo &TTI;

public:
explicit AIEBaseAAResult(const DataLayout &DL) : DL(DL) {}
explicit AIEBaseAAResult(const DataLayout &DL, const TargetTransformInfo &TTI)
: DL(DL), TTI(TTI) {}
AIEBaseAAResult(AIEBaseAAResult &&Arg)
: AAResultBase(std::move(Arg)), DL(Arg.DL) {}
: AAResultBase(std::move(Arg)), DL(Arg.DL), TTI(Arg.TTI) {}

/// Handle invalidation events from the new pass manager.
///
Expand All @@ -51,8 +54,9 @@ class AIEBaseAAResult : public AAResultBase {
return false;
}

AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
AAQueryInfo &AAQI, const Instruction *CtxI);
virtual AliasResult alias(const MemoryLocation &LocA,
const MemoryLocation &LocB, AAQueryInfo &AAQI,
const Instruction *CtxI);
};

/// Analysis pass providing a never-invalidated alias analysis result.
Expand All @@ -65,7 +69,8 @@ class AIEBaseAA : public AnalysisInfoMixin<AIEBaseAA> {
using Result = AIEBaseAAResult;

AIEBaseAAResult run(Function &F, AnalysisManager<Function> &AM) {
return AIEBaseAAResult(F.getParent()->getDataLayout());
const TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
return AIEBaseAAResult(F.getParent()->getDataLayout(), TTI);
}
};

Expand All @@ -82,7 +87,12 @@ class AIEBaseAAWrapperPass : public ImmutablePass {
const AIEBaseAAResult &getResult() const { return *Result; }

bool doInitialization(Module &M) override {
Result.reset(new AIEBaseAAResult(M.getDataLayout()));
if (!M.getFunctionList().empty()) {
const TargetTransformInfo &TTI =
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
M.getFunctionList().front());
Result.reset(new AIEBaseAAResult(M.getDataLayout(), TTI));
}
return false;
}

Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Target/AIE/AIETargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class AIETTIImpl : public BasicTTIImplBase<AIETTIImpl> {
UP.Threshold = 200;
BaseT::getUnrollingPreferences(L, SE, UP, ORE);
}

bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const {
if (AS0 == AS1)
return true;

const AIEBaseAddrSpaceInfo &ASI = ST->getAddrSpaceInfo();
return ASI.getMemoryBanksFromAddressSpace(AS0) &
ASI.getMemoryBanksFromAddressSpace(AS1);
}
};

} // end namespace llvm
Expand Down
148 changes: 148 additions & 0 deletions llvm/test/CodeGen/AIE/alias-analysis-AA.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
;
; This file is licensed under the Apache License v2.0 with LLVM Exceptions.
; See https://llvm.org/LICENSE.txt for license information.
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
;
; (c) Copyright 2024 Advanced Micro Devices, Inc. or its affiliates
; RUN: opt -mtriple=aie2 -passes=aa-eval -print-all-alias-modref-info --aie-alias-analysis-addrspace=true -disable-output < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ENABLED-AIE-AS-AA
; RUN: opt -mtriple=aie2 -passes=aa-eval -print-all-alias-modref-info --aie-alias-analysis-addrspace=false -disable-output < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DISABLED-AIE-AS-AA

; CHECK-LABEL: Function: basic_without_AS
; CHECK: MayAlias: i8* %p, i8* %p1
define void @basic_without_AS(ptr %p, ptr %p1) {
load i8, ptr %p
load i8, ptr %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_56
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(5)* %p, i8 addrspace(6)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_56
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(5)* %p, i8 addrspace(6)* %p1
define void @basic_withAS_56(ptr addrspace(5) %p, ptr addrspace(6) %p1) {
load i8, ptr addrspace(5) %p
load i8, ptr addrspace(6) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_67
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(6)* %p, i8 addrspace(7)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_67
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(6)* %p, i8 addrspace(7)* %p1
define void @basic_withAS_67(ptr addrspace(6) %p, ptr addrspace(7) %p1) {
load i8, ptr addrspace(6) %p
load i8, ptr addrspace(7) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_78
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(8)* %p, i8 addrspace(7)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_78
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(8)* %p, i8 addrspace(7)* %p1
define void @basic_withAS_78(ptr addrspace(8) %p, ptr addrspace(7) %p1) {
load i8, ptr addrspace(8) %p
load i8, ptr addrspace(7) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_58
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(5)* %p, i8 addrspace(8)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_58
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(5)* %p, i8 addrspace(8)* %p1
define void @basic_withAS_58(ptr addrspace(5) %p, ptr addrspace(8) %p1) {
load i8, ptr addrspace(5) %p
load i8, ptr addrspace(8) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_A_AB
; ENABLED-AIE-AS-AA: MayAlias: i8 addrspace(5)* %p, i8 addrspace(9)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_A_AB
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(5)* %p, i8 addrspace(9)* %p1
define void @basic_withAS_compund_A_AB(ptr addrspace(5) %p, ptr addrspace(9) %p1) {
load i8, ptr addrspace(5) %p
load i8, ptr addrspace(9) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_B_AB
; ENABLED-AIE-AS-AA: MayAlias: i8 addrspace(6)* %p, i8 addrspace(9)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_B_AB
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(6)* %p, i8 addrspace(9)* %p1
define void @basic_withAS_compund_B_AB(ptr addrspace(6) %p, ptr addrspace(9) %p1) {
load i8, ptr addrspace(6) %p
load i8, ptr addrspace(9) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_C_AB
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(7)* %p, i8 addrspace(9)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_C_AB
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(7)* %p, i8 addrspace(9)* %p1
define void @basic_withAS_compund_C_AB(ptr addrspace(7) %p, ptr addrspace(9) %p1) {
load i8, ptr addrspace(7) %p
load i8, ptr addrspace(9) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_AB_CD
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(9)* %p, i8 addrspace(14)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_compund_AB_CD
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(9)* %p, i8 addrspace(14)* %p1
define void @basic_withAS_compund_AB_CD(ptr addrspace(9) %p, ptr addrspace(14) %p1) {
load i8, ptr addrspace(9) %p
load i8, ptr addrspace(14) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_TM_noAS
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(15)* %p, i8* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_TM_noAS
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(15)* %p, i8* %p1
define void @basic_TM_noAS(ptr addrspace(15) %p, ptr %p1) {
load i8, ptr addrspace(15) %p
load i8, ptr %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_noAS_TM
; ENABLED-AIE-AS-AA: NoAlias: i8* %p, i8 addrspace(15)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_noAS_TM
; DISABLED-AIE-AS-AA: MayAlias: i8* %p, i8 addrspace(15)* %p1
define void @basic_noAS_TM(ptr %p, ptr addrspace(15) %p1) {
load i8, ptr %p
load i8, ptr addrspace(15) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_TM_TM
; ENABLED-AIE-AS-AA: MayAlias: i8 addrspace(15)* %p, i8 addrspace(15)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_TM_TM
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(15)* %p, i8 addrspace(15)* %p1
define void @basic_withAS_TM_TM(ptr addrspace(15) %p, ptr addrspace(15) %p1) {
load i8, ptr addrspace(15) %p
load i8, ptr addrspace(15) %p1
ret void
}

; ENABLED-AIE-AS-AA-LABEL: Function: basic_withAS_A_TM
; ENABLED-AIE-AS-AA: NoAlias: i8 addrspace(5)* %p, i8 addrspace(15)* %p1

; DISABLED-AIE-AS-AA-LABEL: Function: basic_withAS_A_TM
; DISABLED-AIE-AS-AA: MayAlias: i8 addrspace(5)* %p, i8 addrspace(15)* %p1
define void @basic_withAS_A_TM(ptr addrspace(5) %p, ptr addrspace(15) %p1) {
load i8, ptr addrspace(5) %p
load i8, ptr addrspace(15) %p1
ret void
}

0 comments on commit 7c9cda7

Please sign in to comment.