LLVM 24.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"
15#include "NVPTXSubtarget.h"
17#include "llvm/IR/InlineAsm.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "NVPTX-aa"
23
24AnalysisKey NVPTXAA::Key;
25
28
30 "NVPTX Address space based Alias Analysis", false, true)
31
33 "NVPTX Address space based Alias Analysis Wrapper", false, true)
34
38
42
44
48
50 Result = std::make_unique<NVPTXAAResult>(getNVPTXOptions(M));
51 return false;
52}
53
57
58static unsigned getAddressSpace(const Value *V, unsigned MaxLookup) {
59 // Find the first non-generic address space traversing the UD chain.
60 // It is undefined behaviour if a pointer belongs to more than one
61 // non-overlapping address spaces along a valid execution path.
62 auto GetAS = [](const Value *V) -> unsigned {
63 if (const auto *PTy = dyn_cast<PointerType>(V->getType()))
64 return PTy->getAddressSpace();
66 };
67 while (MaxLookup-- && GetAS(V) == ADDRESS_SPACE_GENERIC) {
68 const Value *NewV = getUnderlyingObject(V, 1);
69 if (NewV == V)
70 break;
71 V = NewV;
72 }
73 return GetAS(V);
74}
75
76static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
77 if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
79
80 // PTX s6.4.1.1. Generic Addressing:
81 // A generic address maps to global memory unless it falls within
82 // the window for const, local, or shared memory. The Kernel
83 // Function Parameters (.param) window is contained within the
84 // .global window.
85 //
86 // Therefore a global pointer may alias with a param pointer on some
87 // GPUs via addrspacecast(param->generic->global) when cvta.param
88 // instruction is used (PTX 7.7+ and SM_70+).
89 //
90 // TODO: cvta.param is not yet supported. We need to change aliasing
91 // rules once it is added.
92
93 // Distributed shared memory aliases with shared memory.
94 if (((AS1 == ADDRESS_SPACE_SHARED) &&
98
99 return (AS1 == AS2 ? AliasResult::MayAlias : AliasResult::NoAlias);
100}
101
103 const MemoryLocation &Loc2, AAQueryInfo &AAQI,
104 const Instruction *) {
105 unsigned AS1 =
106 getAddressSpace(Loc1.Ptr, CLOpts.traverse_address_aliasing_limit);
107 unsigned AS2 =
108 getAddressSpace(Loc2.Ptr, CLOpts.traverse_address_aliasing_limit);
109
110 return getAliasResult(AS1, AS2);
111}
112
113// TODO: .param address space may be writable in presence of cvta.param, but
114// this instruction is currently not supported. NVPTXLowerArgs also does not
115// allow any writes to .param pointers.
116static bool isConstOrParam(unsigned AS) {
117 return AS == AddressSpace::ADDRESS_SPACE_CONST ||
118 AS == AddressSpace::ADDRESS_SPACE_ENTRY_PARAM;
119}
120
122 AAQueryInfo &AAQI,
123 bool IgnoreLocals) {
124 if (isConstOrParam(
125 getAddressSpace(Loc.Ptr, CLOpts.traverse_address_aliasing_limit)))
127
128 return ModRefInfo::ModRef;
129}
130
132 AAQueryInfo &AAQI) {
133 // Inline assembly with no side-effect or memory clobbers should not
134 // indirectly access memory in the PTX specification.
135 if (const auto *IA = dyn_cast<InlineAsm>(Call->getCalledOperand())) {
136 // Volatile is translated as side-effects.
137 if (IA->hasSideEffects())
138 return MemoryEffects::unknown();
139
140 for (const InlineAsm::ConstraintInfo &Constraint : IA->ParseConstraints()) {
141 // Indirect constraints (e.g. =*m) are unsupported in inline PTX.
142 if (Constraint.isIndirect)
143 return MemoryEffects::unknown();
144
145 // Memory clobbers prevent optimization.
146 if ((Constraint.Type & InlineAsm::ConstraintPrefix::isClobber) &&
147 any_of(Constraint.Codes,
148 [](const auto &Code) { return Code == "{memory}"; }))
149 return MemoryEffects::unknown();
150 }
151 return MemoryEffects::none();
152 }
153
154 return MemoryEffects::unknown();
155}
#define F(x, y, z)
Definition MD5.cpp:54
static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2)
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.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
A container for analyses that lazily runs them and caches their results.
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...
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:283
ImmutablePass(char &pid)
Definition Pass.h:285
static MemoryEffectsBase none()
Definition ModRef.h:128
static MemoryEffectsBase unknown()
Definition ModRef.h:123
Representation for a specific memory location.
const Value * Ptr
The address of the start of the location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
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...
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
NVPTXAAResult run(Function &F, AnalysisManager< Function > &AM)
LLVM Value Representation.
Definition Value.h:75
CallInst * Call
TargetPassConfig.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
ImmutablePass * createNVPTXExternalAAWrapperPass()
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
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:1762
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
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.
Definition ModRef.h:36
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
const NVPTXOptions & getNVPTXOptions(const LLVMContext &C)
ImmutablePass * createNVPTXAAWrapperPass()
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29