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