LLVM 22.0.0git
NVPTXAliasAnalysis.cpp
Go to the documentation of this file.
1//===--------------------- NVPTXAliasAnalysis.cpp--------------------------===//
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 NVPTX address space based alias analysis pass.
10//===----------------------------------------------------------------------===//
11
12#include "NVPTXAliasAnalysis.h"
14#include "NVPTX.h"
16#include "llvm/IR/InlineAsm.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "NVPTX-aa"
23
25 "nvptx-traverse-address-aliasing-limit", cl::Hidden,
26 cl::desc("Depth limit for finding address space through traversal"),
27 cl::init(6));
28
29AnalysisKey NVPTXAA::Key;
30
33
35 "NVPTX Address space based Alias Analysis", false, true)
36
38 "NVPTX Address space based Alias Analysis Wrapper", false, true)
39
41 return new NVPTXAAWrapperPass();
42}
43
45 return new NVPTXExternalAAWrapper();
46}
47
49
51 AU.setPreservesAll();
52}
53
54static unsigned getAddressSpace(const Value *V, unsigned MaxLookup) {
55 // Find the first non-generic address space traversing the UD chain.
56 // It is undefined behaviour if a pointer belongs to more than one
57 // non-overlapping address spaces along a valid execution path.
58 auto GetAS = [](const Value *V) -> unsigned {
59 if (const auto *PTy = dyn_cast<PointerType>(V->getType()))
60 return PTy->getAddressSpace();
62 };
63 while (MaxLookup-- && GetAS(V) == ADDRESS_SPACE_GENERIC) {
64 const Value *NewV = getUnderlyingObject(V, 1);
65 if (NewV == V)
66 break;
67 V = NewV;
68 }
69 return GetAS(V);
70}
71
72static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
73 if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
75
76 // PTX s6.4.1.1. Generic Addressing:
77 // A generic address maps to global memory unless it falls within
78 // the window for const, local, or shared memory. The Kernel
79 // Function Parameters (.param) window is contained within the
80 // .global window.
81 //
82 // Therefore a global pointer may alias with a param pointer on some
83 // GPUs via addrspacecast(param->generic->global) when cvta.param
84 // instruction is used (PTX 7.7+ and SM_70+).
85 //
86 // TODO: cvta.param is not yet supported. We need to change aliasing
87 // rules once it is added.
88
89 // Distributed shared memory aliases with shared memory.
90 if (((AS1 == ADDRESS_SPACE_SHARED) &&
94
95 return (AS1 == AS2 ? AliasResult::MayAlias : AliasResult::NoAlias);
96}
97
99 const MemoryLocation &Loc2, AAQueryInfo &AAQI,
100 const Instruction *) {
101 unsigned AS1 = getAddressSpace(Loc1.Ptr, TraverseAddressSpacesLimit);
102 unsigned AS2 = getAddressSpace(Loc2.Ptr, TraverseAddressSpacesLimit);
103
104 return getAliasResult(AS1, AS2);
105}
106
107// TODO: .param address space may be writable in presence of cvta.param, but
108// this instruction is currently not supported. NVPTXLowerArgs also does not
109// allow any writes to .param pointers.
110static bool isConstOrParam(unsigned AS) {
111 return AS == AddressSpace::ADDRESS_SPACE_CONST ||
112 AS == AddressSpace::ADDRESS_SPACE_PARAM;
113}
114
116 AAQueryInfo &AAQI,
117 bool IgnoreLocals) {
120
121 return ModRefInfo::ModRef;
122}
123
125 AAQueryInfo &AAQI) {
126 // Inline assembly with no side-effect or memory clobbers should not
127 // indirectly access memory in the PTX specification.
128 if (const auto *IA = dyn_cast<InlineAsm>(Call->getCalledOperand())) {
129 // Volatile is translated as side-effects.
130 if (IA->hasSideEffects())
131 return MemoryEffects::unknown();
132
133 for (const InlineAsm::ConstraintInfo &Constraint : IA->ParseConstraints()) {
134 // Indirect constraints (e.g. =*m) are unsupported in inline PTX.
135 if (Constraint.isIndirect)
136 return MemoryEffects::unknown();
137
138 // Memory clobbers prevent optimization.
139 if ((Constraint.Type & InlineAsm::ConstraintPrefix::isClobber) &&
140 any_of(Constraint.Codes,
141 [](const auto &Code) { return Code == "{memory}"; }))
142 return MemoryEffects::unknown();
143 }
144 return MemoryEffects::none();
145 }
146
147 return MemoryEffects::unknown();
148}
block Block Frequency Analysis
static cl::opt< unsigned > TraverseAddressSpacesLimit("nvptx-traverse-address-aliasing-limit", cl::Hidden, cl::desc("Depth limit for finding address space through traversal"), cl::init(6))
nvptx aa NVPTX Address space based Alias Analysis Wrapper
static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2)
nvptx aa wrapper
static bool isConstOrParam(unsigned AS)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
This is the NVPTX address space based alias analysis pass.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
This class stores info we want to provide to or retain within an alias query.
The possible results of an alias query.
Definition: AliasAnalysis.h:78
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:99
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:96
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:285
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition: ModRef.h:120
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
Definition: ModRef.h:115
Representation for a specific memory location.
const Value * Ptr
The address of the start of the location.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI=nullptr)
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
Legacy wrapper pass to provide the NVPTXAAResult object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM Value Representation.
Definition: Value.h:75
TargetPassConfig.
@ ADDRESS_SPACE_SHARED_CLUSTER
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ImmutablePass * createNVPTXExternalAAWrapperPass()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:28
@ ModRef
The access may reference and may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
ImmutablePass * createNVPTXAAWrapperPass()
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29