1 //===- AMDGPUAliasAnalysis ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This is the AMGPU address space based alias analysis pass.
10 //===----------------------------------------------------------------------===//
11 
12 #include "AMDGPUAliasAnalysis.h"
13 #include "AMDGPU.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Analysis/MemoryLocation.h"
17 #include "llvm/Analysis/ValueTracking.h"
18 #include "llvm/IR/Argument.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <cassert>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "amdgpu-aa"
34 
35 // Register this pass...
36 char AMDGPUAAWrapperPass::ID = 0;
37 char AMDGPUExternalAAWrapper::ID = 0;
38 
39 INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa",
40                 "AMDGPU Address space based Alias Analysis", false, true)
41 
42 INITIALIZE_PASS(AMDGPUExternalAAWrapper, "amdgpu-aa-wrapper",
43                 "AMDGPU Address space based Alias Analysis Wrapper", false, true)
44 
45 ImmutablePass *llvm::createAMDGPUAAWrapperPass() {
46   return new AMDGPUAAWrapperPass();
47 }
48 
49 ImmutablePass *llvm::createAMDGPUExternalAAWrapperPass() {
50   return new AMDGPUExternalAAWrapper();
51 }
52 
53 void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
54   AU.setPreservesAll();
55 }
56 
57 // These arrays are indexed by address space value enum elements 0 ... to 7
58 static const AliasResult ASAliasRules[8][8] = {
59   /*                    Flat       Global    Region    Group     Constant  Private   Constant 32-bit  Buffer Fat Ptr */
60   /* Flat     */        {MayAlias, MayAlias, NoAlias,  MayAlias, MayAlias, MayAlias, MayAlias,        MayAlias},
61   /* Global   */        {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias,        MayAlias},
62   /* Region   */        {NoAlias,  NoAlias , MayAlias, NoAlias , NoAlias,  NoAlias , NoAlias,         NoAlias},
63   /* Group    */        {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias , NoAlias ,        NoAlias},
64   /* Constant */        {MayAlias, MayAlias, NoAlias,  NoAlias , NoAlias , NoAlias , MayAlias,        MayAlias},
65   /* Private  */        {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, NoAlias ,        NoAlias},
66   /* Constant 32-bit */ {MayAlias, MayAlias, NoAlias,  NoAlias , MayAlias, NoAlias , NoAlias ,        MayAlias},
67   /* Buffer Fat Ptr  */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias,        MayAlias}
68 };
69 
70 static AliasResult getAliasResult(unsigned AS1, unsigned AS2) {
71   static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 7, "Addr space out of range");
72 
73   if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS)
74     return MayAlias;
75 
76   return ASAliasRules[AS1][AS2];
77 }
78 
79 AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
80                                   const MemoryLocation &LocB,
81                                   AAQueryInfo &AAQI) {
82   unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
83   unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
84 
85   AliasResult Result = getAliasResult(asA, asB);
86   if (Result == NoAlias)
87     return Result;
88 
89   // In general, FLAT (generic) pointers could be aliased to LOCAL or PRIVATE
90   // pointers. However, as LOCAL or PRIVATE pointers point to local objects, in
91   // certain cases, it's still viable to check whether a FLAT pointer won't
92   // alias to a LOCAL or PRIVATE pointer.
93   MemoryLocation A = LocA;
94   MemoryLocation B = LocB;
95   // Canonicalize the location order to simplify the following alias check.
96   if (asA != AMDGPUAS::FLAT_ADDRESS) {
97     std::swap(asA, asB);
98     std::swap(A, B);
99   }
100   if (asA == AMDGPUAS::FLAT_ADDRESS &&
101       (asB == AMDGPUAS::LOCAL_ADDRESS || asB == AMDGPUAS::PRIVATE_ADDRESS)) {
102     const auto *ObjA =
103         getUnderlyingObject(A.Ptr->stripPointerCastsAndInvariantGroups());
104     if (const LoadInst *LI = dyn_cast<LoadInst>(ObjA)) {
105       // If a generic pointer is loaded from the constant address space, it
106       // could only be a GLOBAL or CONSTANT one as that address space is soley
107       // prepared on the host side, where only GLOBAL or CONSTANT variables are
108       // visible. Note that this even holds for regular functions.
109       if (LI->getPointerAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS)
110         return NoAlias;
111     } else if (const Argument *Arg = dyn_cast<Argument>(ObjA)) {
112       const Function *F = Arg->getParent();
113       switch (F->getCallingConv()) {
114       case CallingConv::AMDGPU_KERNEL:
115         // In the kernel function, kernel arguments won't alias to (local)
116         // variables in shared or private address space.
117         return NoAlias;
118       default:
119         // TODO: In the regular function, if that local variable in the
120         // location B is not captured, that argument pointer won't alias to it
121         // as well.
122         break;
123       }
124     }
125   }
126 
127   // Forward the query to the next alias analysis.
128   return AAResultBase::alias(LocA, LocB, AAQI);
129 }
130 
131 bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
132                                             AAQueryInfo &AAQI, bool OrLocal) {
133   unsigned AS = Loc.Ptr->getType()->getPointerAddressSpace();
134   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
135       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT)
136     return true;
137 
138   const Value *Base = getUnderlyingObject(Loc.Ptr);
139   AS = Base->getType()->getPointerAddressSpace();
140   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
141       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT)
142     return true;
143 
144   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
145     if (GV->isConstant())
146       return true;
147   } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
148     const Function *F = Arg->getParent();
149 
150     // Only assume constant memory for arguments on kernels.
151     switch (F->getCallingConv()) {
152     default:
153       return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
154     case CallingConv::AMDGPU_LS:
155     case CallingConv::AMDGPU_HS:
156     case CallingConv::AMDGPU_ES:
157     case CallingConv::AMDGPU_GS:
158     case CallingConv::AMDGPU_VS:
159     case CallingConv::AMDGPU_PS:
160     case CallingConv::AMDGPU_CS:
161     case CallingConv::AMDGPU_KERNEL:
162     case CallingConv::SPIR_KERNEL:
163       break;
164     }
165 
166     unsigned ArgNo = Arg->getArgNo();
167     /* On an argument, ReadOnly attribute indicates that the function does
168        not write through this pointer argument, even though it may write
169        to the memory that the pointer points to.
170        On an argument, ReadNone attribute indicates that the function does
171        not dereference that pointer argument, even though it may read or write
172        the memory that the pointer points to if accessed through other pointers.
173      */
174     if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
175         (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
176          F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
177       return true;
178     }
179   }
180   return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
181 }
182