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"
13 #include "AMDGPU.h"
15 #include "llvm/IR/Instructions.h"
16 
17 using namespace llvm;
18 
19 #define DEBUG_TYPE "amdgpu-aa"
20 
21 AnalysisKey AMDGPUAA::Key;
22 
23 // Register this pass...
26 
28  "AMDGPU Address space based Alias Analysis", false, true)
29 
31  "AMDGPU Address space based Alias Analysis Wrapper", false, true)
32 
34  return new AMDGPUAAWrapperPass();
35 }
36 
38  return new AMDGPUExternalAAWrapper();
39 }
40 
43 }
44 
46  AU.setPreservesAll();
47 }
48 
49 static AliasResult getAliasResult(unsigned AS1, unsigned AS2) {
50  static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 7, "Addr space out of range");
51 
53  return AliasResult::MayAlias;
54 
55 #define ASMay AliasResult::MayAlias
56 #define ASNo AliasResult::NoAlias
57  // This array is indexed by address space value enum elements 0 ... to 7
58  static const AliasResult ASAliasRules[8][8] = {
59  /* Flat Global Region Group Constant Private Const32 Buf Fat Ptr */
60  /* Flat */ {ASMay, ASMay, ASNo, ASMay, ASMay, ASMay, ASMay, ASMay},
61  /* Global */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay},
62  /* Region */ {ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo, ASNo},
63  /* Group */ {ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo},
64  /* Constant */ {ASMay, ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASMay},
65  /* Private */ {ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASNo, ASNo},
66  /* Constant 32-bit */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASMay},
67  /* Buffer Fat Ptr */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay}
68  };
69 #undef ASMay
70 #undef ASNo
71 
72  return ASAliasRules[AS1][AS2];
73 }
74 
76  const MemoryLocation &LocB,
77  AAQueryInfo &AAQI) {
78  unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
79  unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
80 
81  AliasResult Result = getAliasResult(asA, asB);
82  if (Result == AliasResult::NoAlias)
83  return Result;
84 
85  // In general, FLAT (generic) pointers could be aliased to LOCAL or PRIVATE
86  // pointers. However, as LOCAL or PRIVATE pointers point to local objects, in
87  // certain cases, it's still viable to check whether a FLAT pointer won't
88  // alias to a LOCAL or PRIVATE pointer.
89  MemoryLocation A = LocA;
90  MemoryLocation B = LocB;
91  // Canonicalize the location order to simplify the following alias check.
92  if (asA != AMDGPUAS::FLAT_ADDRESS) {
93  std::swap(asA, asB);
94  std::swap(A, B);
95  }
96  if (asA == AMDGPUAS::FLAT_ADDRESS &&
98  const auto *ObjA =
99  getUnderlyingObject(A.Ptr->stripPointerCastsForAliasAnalysis());
100  if (const LoadInst *LI = dyn_cast<LoadInst>(ObjA)) {
101  // If a generic pointer is loaded from the constant address space, it
102  // could only be a GLOBAL or CONSTANT one as that address space is solely
103  // prepared on the host side, where only GLOBAL or CONSTANT variables are
104  // visible. Note that this even holds for regular functions.
105  if (LI->getPointerAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS)
106  return AliasResult::NoAlias;
107  } else if (const Argument *Arg = dyn_cast<Argument>(ObjA)) {
108  const Function *F = Arg->getParent();
109  switch (F->getCallingConv()) {
111  // In the kernel function, kernel arguments won't alias to (local)
112  // variables in shared or private address space.
113  return AliasResult::NoAlias;
114  default:
115  // TODO: In the regular function, if that local variable in the
116  // location B is not captured, that argument pointer won't alias to it
117  // as well.
118  break;
119  }
120  }
121  }
122 
123  // Forward the query to the next alias analysis.
124  return AAResultBase::alias(LocA, LocB, AAQI);
125 }
126 
128  AAQueryInfo &AAQI, bool OrLocal) {
129  unsigned AS = Loc.Ptr->getType()->getPointerAddressSpace();
130  if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
132  return true;
133 
134  const Value *Base = getUnderlyingObject(Loc.Ptr);
135  AS = Base->getType()->getPointerAddressSpace();
136  if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
138  return true;
139 
140  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
141  if (GV->isConstant())
142  return true;
143  } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
144  const Function *F = Arg->getParent();
145 
146  // Only assume constant memory for arguments on kernels.
147  switch (F->getCallingConv()) {
148  default:
149  return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
159  break;
160  }
161 
162  unsigned ArgNo = Arg->getArgNo();
163  /* On an argument, ReadOnly attribute indicates that the function does
164  not write through this pointer argument, even though it may write
165  to the memory that the pointer points to.
166  On an argument, ReadNone attribute indicates that the function does
167  not dereference that pointer argument, even though it may read or write
168  the memory that the pointer points to if accessed through other pointers.
169  */
170  if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
171  (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
172  F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
173  return true;
174  }
175  }
176  return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
177 }
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:103
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::AMDGPUAAResult::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: AMDGPUAliasAnalysis.cpp:127
llvm::AMDGPUAS::PRIVATE_ADDRESS
@ PRIVATE_ADDRESS
Address space for private memory.
Definition: AMDGPU.h:364
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:62
Wrapper
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
Definition: AMDGPUAliasAnalysis.cpp:31
llvm::AMDGPUAAWrapperPass::ID
static char ID
Definition: AMDGPUAliasAnalysis.h:66
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::AMDGPUExternalAAWrapper::ID
static char ID
Definition: AMDGPUAliasAnalysis.h:90
ValueTracking.h
llvm::CallingConv::AMDGPU_PS
@ AMDGPU_PS
Calling convention used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:210
llvm::AMDGPUAAWrapperPass
Legacy wrapper pass to provide the AMDGPUAAResult object.
Definition: AMDGPUAliasAnalysis.h:62
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:82
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
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:45
llvm::AMDGPUAS::CONSTANT_ADDRESS
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
Definition: AMDGPU.h:362
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::createAMDGPUExternalAAWrapperPass
ImmutablePass * createAMDGPUExternalAAWrapperPass()
Definition: AMDGPUAliasAnalysis.cpp:37
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:459
AMDGPUAliasAnalysis.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
ASNo
#define ASNo
llvm::AMDGPUAS::LOCAL_ADDRESS
@ LOCAL_ADDRESS
Address space for local memory.
Definition: AMDGPU.h:363
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:4376
llvm::CallingConv::AMDGPU_GS
@ AMDGPU_GS
Calling convention used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:207
llvm::AMDGPUAAWrapperPass::AMDGPUAAWrapperPass
AMDGPUAAWrapperPass()
Definition: AMDGPUAliasAnalysis.cpp:41
llvm::createAMDGPUAAWrapperPass
ImmutablePass * createAMDGPUAAWrapperPass()
Definition: AMDGPUAliasAnalysis.cpp:33
llvm::CallingConv::AMDGPU_CS
@ AMDGPU_CS
Calling convention used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:213
llvm::AAResultBase::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1219
llvm::CallingConv::SPIR_KERNEL
@ SPIR_KERNEL
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:152
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:49
llvm::AMDGPUExternalAAWrapper
Definition: AMDGPUAliasAnalysis.h:88
llvm::AMDGPUAS::MAX_AMDGPU_ADDRESS
@ MAX_AMDGPU_ADDRESS
Definition: AMDGPU.h:356
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:840
llvm::initializeAMDGPUAAWrapperPassPass
void initializeAMDGPUAAWrapperPassPass(PassRegistry &)
llvm::AMDGPUAS::FLAT_ADDRESS
@ FLAT_ADDRESS
Address space for flat memory.
Definition: AMDGPU.h:358
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:30
AMDGPU.h
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::AliasResult::NoAlias
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:100
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:175
llvm::CallingConv::AMDGPU_KERNEL
@ AMDGPU_KERNEL
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:216
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
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::CallingConv::AMDGPU_HS
@ AMDGPU_HS
Calling convention used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:223
llvm::CallingConv::AMDGPU_LS
@ AMDGPU_LS
Calling convention used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:231
llvm::AAResultBase::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: AliasAnalysis.h:1224
Instructions.h
llvm::AMDGPUAS::CONSTANT_ADDRESS_32BIT
@ CONSTANT_ADDRESS_32BIT
Address space for 32-bit constant memory.
Definition: AMDGPU.h:366
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1903
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::AMDGPUAAResult::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AMDGPUAliasAnalysis.cpp:75
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:209
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37
INITIALIZE_PASS
TargetPassConfig.
Definition: TargetPassConfig.cpp:359