Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 1 | //===- AMDGPUAliasAnalysis ------------------------------------------------===// |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This is the AMGPU address space based alias analysis pass. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 12 | #include "AMDGPUAliasAnalysis.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 13 | #include "AMDGPU.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Triple.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Argument.h" |
| 19 | #include "llvm/IR/Attributes.h" |
| 20 | #include "llvm/IR/CallingConv.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GlobalVariable.h" |
| 23 | #include "llvm/IR/Type.h" |
| 24 | #include "llvm/IR/Value.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include <cassert> |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "amdgpu-aa" |
| 33 | |
| 34 | // Register this pass... |
| 35 | char AMDGPUAAWrapperPass::ID = 0; |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 36 | char AMDGPUExternalAAWrapper::ID = 0; |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 37 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 38 | INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa", |
| 39 | "AMDGPU Address space based Alias Analysis", false, true) |
| 40 | |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 41 | INITIALIZE_PASS(AMDGPUExternalAAWrapper, "amdgpu-aa-wrapper", |
| 42 | "AMDGPU Address space based Alias Analysis Wrapper", false, true) |
| 43 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 44 | ImmutablePass *llvm::createAMDGPUAAWrapperPass() { |
| 45 | return new AMDGPUAAWrapperPass(); |
| 46 | } |
| 47 | |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 48 | ImmutablePass *llvm::createAMDGPUExternalAAWrapperPass() { |
| 49 | return new AMDGPUExternalAAWrapper(); |
| 50 | } |
| 51 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 52 | void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 53 | AU.setPreservesAll(); |
| 54 | } |
| 55 | |
Neil Henning | 523dab0 | 2019-03-18 14:44:28 +0000 | [diff] [blame] | 56 | // These arrays are indexed by address space value enum elements 0 ... to 7 |
| 57 | static const AliasResult ASAliasRules[8][8] = { |
| 58 | /* Flat Global Region Group Constant Private Constant 32-bit Buffer Fat Ptr */ |
| 59 | /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias}, |
| 60 | /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias, MayAlias}, |
| 61 | /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias , MayAlias, NoAlias , MayAlias, NoAlias}, |
| 62 | /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias , NoAlias , NoAlias}, |
| 63 | /* Constant */ {MayAlias, MayAlias, MayAlias, NoAlias , NoAlias , NoAlias , MayAlias, MayAlias}, |
| 64 | /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, NoAlias , NoAlias}, |
| 65 | /* Constant 32-bit */ {MayAlias, MayAlias, MayAlias, NoAlias , MayAlias, NoAlias , NoAlias , MayAlias}, |
| 66 | /* Buffer Fat Ptr */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias, MayAlias} |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 67 | }; |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 69 | static AliasResult getAliasResult(unsigned AS1, unsigned AS2) { |
Neil Henning | 523dab0 | 2019-03-18 14:44:28 +0000 | [diff] [blame] | 70 | static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 7, "Addr space out of range"); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 71 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 72 | if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS) |
| 73 | return MayAlias; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 74 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 75 | return ASAliasRules[AS1][AS2]; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 78 | AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 79 | const MemoryLocation &LocB, |
| 80 | AAQueryInfo &AAQI) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 81 | unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace(); |
| 82 | unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace(); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 83 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 84 | AliasResult Result = getAliasResult(asA, asB); |
| 85 | if (Result == NoAlias) |
| 86 | return Result; |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 87 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 88 | // Forward the query to the next alias analysis. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 89 | return AAResultBase::alias(LocA, LocB, AAQI); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 93 | AAQueryInfo &AAQI, bool OrLocal) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 94 | const Value *Base = GetUnderlyingObject(Loc.Ptr, DL); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 95 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 96 | if (AS == AMDGPUAS::CONSTANT_ADDRESS || |
| 97 | AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 102 | if (GV->isConstant()) |
| 103 | return true; |
| 104 | } else if (const Argument *Arg = dyn_cast<Argument>(Base)) { |
| 105 | const Function *F = Arg->getParent(); |
| 106 | |
| 107 | // Only assume constant memory for arguments on kernels. |
| 108 | switch (F->getCallingConv()) { |
| 109 | default: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 110 | return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal); |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 111 | case CallingConv::AMDGPU_LS: |
| 112 | case CallingConv::AMDGPU_HS: |
| 113 | case CallingConv::AMDGPU_ES: |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 114 | case CallingConv::AMDGPU_GS: |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 115 | case CallingConv::AMDGPU_VS: |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 116 | case CallingConv::AMDGPU_PS: |
| 117 | case CallingConv::AMDGPU_CS: |
| 118 | case CallingConv::AMDGPU_KERNEL: |
| 119 | case CallingConv::SPIR_KERNEL: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | unsigned ArgNo = Arg->getArgNo(); |
| 124 | /* On an argument, ReadOnly attribute indicates that the function does |
| 125 | not write through this pointer argument, even though it may write |
| 126 | to the memory that the pointer points to. |
| 127 | On an argument, ReadNone attribute indicates that the function does |
| 128 | not dereference that pointer argument, even though it may read or write |
| 129 | the memory that the pointer points to if accessed through other pointers. |
| 130 | */ |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 131 | if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) && |
| 132 | (F->hasParamAttribute(ArgNo, Attribute::ReadNone) || |
| 133 | F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | } |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 137 | return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 138 | } |