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