LLVM  14.0.0git
AMDGPUAliasAnalysis.cpp
Go to the documentation of this file.
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"
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...
25 
27  "AMDGPU Address space based Alias Analysis", false, true)
28 
30  "AMDGPU Address space based Alias Analysis Wrapper", false, true)
31 
33  return new AMDGPUAAWrapperPass();
34 }
35 
37  return new AMDGPUExternalAAWrapper();
38 }
39 
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 
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 
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 &&
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 solely
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()) {
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 
123  AAQueryInfo &AAQI, bool OrLocal) {
124  unsigned AS = Loc.Ptr->getType()->getPointerAddressSpace();
125  if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
127  return true;
128 
129  const Value *Base = getUnderlyingObject(Loc.Ptr);
130  AS = Base->getType()->getPointerAddressSpace();
131  if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
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);
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 }
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
llvm::AliasResult::MayAlias
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:102
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::AMDGPUAAResult::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: AMDGPUAliasAnalysis.cpp:122
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::MemoryLocation::Ptr
const Value * Ptr
The address of the start of the location.
Definition: MemoryLocation.h:217
aa
aa
Definition: AliasAnalysis.cpp:846
llvm::Function
Definition: Function.h:61
llvm::CallingConv::AMDGPU_HS
@ AMDGPU_HS
Calling convention used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:223
Wrapper
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
Definition: AMDGPUAliasAnalysis.cpp:30
llvm::AMDGPUAAWrapperPass::ID
static char ID
Definition: AMDGPUAliasAnalysis.h:67
llvm::Type::getPointerAddressSpace
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:734
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::CallingConv::AMDGPU_CS
@ AMDGPU_CS
Calling convention used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:213
llvm::AMDGPUExternalAAWrapper::ID
static char ID
Definition: AMDGPUAliasAnalysis.h:93
ValueTracking.h
llvm::AMDGPUAS::MAX_AMDGPU_ADDRESS
@ MAX_AMDGPU_ADDRESS
Definition: AMDGPU.h:347
llvm::AMDGPUAAWrapperPass
Legacy wrapper pass to provide the AMDGPUAAResult object.
Definition: AMDGPUAliasAnalysis.h:63
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:81
llvm::AMDGPUAAWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: AMDGPUAliasAnalysis.cpp:40
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::createAMDGPUExternalAAWrapperPass
ImmutablePass * createAMDGPUExternalAAWrapperPass()
Definition: AMDGPUAliasAnalysis.cpp:36
llvm::CallingConv::AMDGPU_KERNEL
@ AMDGPU_KERNEL
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:216
ASMay
#define ASMay
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::AAQueryInfo
This class stores info we want to provide to or retain within an alias query.
Definition: AliasAnalysis.h:414
AMDGPUAliasAnalysis.h
llvm::AMDGPUAS::CONSTANT_ADDRESS_32BIT
@ CONSTANT_ADDRESS_32BIT
Address space for 32-bit constant memory.
Definition: AMDGPU.h:357
ASNo
#define ASNo
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:142
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::getUnderlyingObject
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
Definition: ValueTracking.cpp:4377
llvm::CallingConv::AMDGPU_GS
@ AMDGPU_GS
Calling convention used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:207
llvm::CallingConv::AMDGPU_LS
@ AMDGPU_LS
Calling convention used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:231
llvm::createAMDGPUAAWrapperPass
ImmutablePass * createAMDGPUAAWrapperPass()
Definition: AMDGPUAliasAnalysis.cpp:32
llvm::AAResultBase::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1164
llvm::CallingConv::AMDGPU_VS
@ AMDGPU_VS
Calling convention used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (ve...
Definition: CallingConv.h:204
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
getAliasResult
static AliasResult getAliasResult(unsigned AS1, unsigned AS2)
Definition: AMDGPUAliasAnalysis.cpp:44
llvm::AMDGPUExternalAAWrapper
Definition: AMDGPUAliasAnalysis.h:91
llvm::CallingConv::SPIR_KERNEL
@ SPIR_KERNEL
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:152
llvm::AMDGPUAS::LOCAL_ADDRESS
@ LOCAL_ADDRESS
Address space for local memory.
Definition: AMDGPU.h:354
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:840
INITIALIZE_PASS
INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa", "AMDGPU Address space based Alias Analysis", false, true) INITIALIZE_PASS(AMDGPUExternalAAWrapper
wrapper
amdgpu aa wrapper
Definition: AMDGPUAliasAnalysis.cpp:29
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::AMDGPUAS::CONSTANT_ADDRESS
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
Definition: AMDGPU.h:353
llvm::AliasResult::NoAlias
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:99
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:175
llvm::AMDGPUAS::FLAT_ADDRESS
@ FLAT_ADDRESS
Address space for flat memory.
Definition: AMDGPU.h:349
llvm::AMDGPUAS::PRIVATE_ADDRESS
@ PRIVATE_ADDRESS
Address space for private memory.
Definition: AMDGPU.h:355
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::orc::ReadOnly
static constexpr sys::Memory::ProtectionFlags ReadOnly
Definition: DebugObjectManagerPlugin.cpp:111
llvm::AAResultBase::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: AliasAnalysis.h:1169
Instructions.h
llvm::CallingConv::AMDGPU_ES
@ AMDGPU_ES
Calling convention used for AMDPAL shader stage before geometry shader if geometry is in use.
Definition: CallingConv.h:236
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::CallingConv::AMDGPU_PS
@ AMDGPU_PS
Calling convention used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:210
llvm::AMDGPUAAResult::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AMDGPUAliasAnalysis.cpp:70
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:209
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
INITIALIZE_PASS
TargetPassConfig.
Definition: TargetPassConfig.cpp:359