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