LLVM 24.0.0git
NVPTXMarkKernelPtrsGlobal.cpp
Go to the documentation of this file.
1//===-- NVPTXMarkKernelPtrsGlobal.cpp - Mark kernel pointers as global ----===//
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//
9// For CUDA kernels, pointers loaded from byval parameters are known to be in
10// global address space. This pass inserts addrspacecast pairs to make that
11// explicit, enabling later address-space inference to propagate the global AS.
12// It also handles the pattern where a pointer is loaded as an integer and then
13// converted via inttoptr.
14//
15//===----------------------------------------------------------------------===//
16
17#include "NVPTX.h"
18#include "NVVMProperties.h"
22#include "llvm/Pass.h"
24
25using namespace llvm;
26using namespace NVPTXAS;
27
28static void markPointerAsAS(Value *Ptr, unsigned AS) {
30 return;
31
32 BasicBlock::iterator InsertPt;
33 if (auto *Arg = dyn_cast<Argument>(Ptr)) {
34 InsertPt = Arg->getParent()->getEntryBlock().begin();
35 } else {
36 InsertPt = ++cast<Instruction>(Ptr)->getIterator();
37 assert(InsertPt != InsertPt->getParent()->end() &&
38 "We don't call this function with Ptr being a terminator.");
39 }
40
41 Instruction *PtrInGlobal = new AddrSpaceCastInst(
42 Ptr, PointerType::get(Ptr->getContext(), AS), Ptr->getName(), InsertPt);
43 Value *PtrInGeneric = new AddrSpaceCastInst(PtrInGlobal, Ptr->getType(),
44 Ptr->getName(), InsertPt);
45 Ptr->replaceAllUsesWith(PtrInGeneric);
46 PtrInGlobal->setOperand(0, Ptr);
47}
48
52
53static void handleIntToPtr(Value &V) {
54 if (!all_of(V.users(), [](User *U) { return isa<IntToPtrInst>(U); }))
55 return;
56
57 SmallVector<User *, 16> UsersToUpdate(V.users());
58 for (User *U : UsersToUpdate)
60}
61
63 if (!isKernelFunction(F))
64 return false;
65
66 // Copying of byval aggregates + SROA may result in pointers being loaded as
67 // integers, followed by inttoptr. We mark those as global too, but only if
68 // the loaded integer is used exclusively for conversion to a pointer.
69 for (auto &I : instructions(F)) {
70 auto *LI = dyn_cast<LoadInst>(&I);
71 if (!LI)
72 continue;
73
74 if (LI->getType()->isPointerTy() || LI->getType()->isIntegerTy()) {
75 Value *UO = getUnderlyingObject(LI->getPointerOperand());
76 if (auto *Arg = dyn_cast<Argument>(UO)) {
77 if (Arg->hasByValAttr()) {
78 if (LI->getType()->isPointerTy())
80 else
81 handleIntToPtr(*LI);
82 }
83 }
84 }
85 }
86
87 for (Argument &Arg : F.args())
88 if (Arg.getType()->isIntegerTy())
89 handleIntToPtr(Arg);
90
91 return true;
92}
93
94namespace {
95
96class NVPTXMarkKernelPtrsGlobalLegacyPass : public FunctionPass {
97public:
98 static char ID;
99 NVPTXMarkKernelPtrsGlobalLegacyPass() : FunctionPass(ID) {}
100 bool runOnFunction(Function &F) override;
101};
102
103} // namespace
104
105INITIALIZE_PASS(NVPTXMarkKernelPtrsGlobalLegacyPass,
106 "nvptx-mark-kernel-ptrs-global",
107 "NVPTX Mark Kernel Pointers Global", false, false)
108
109bool NVPTXMarkKernelPtrsGlobalLegacyPass::runOnFunction(Function &F) {
110 return markKernelPtrsGlobal(F);
111}
112
113char NVPTXMarkKernelPtrsGlobalLegacyPass::ID = 0;
114
116 return new NVPTXMarkKernelPtrsGlobalLegacyPass();
117}
118
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
NVPTX address space definition.
static bool markKernelPtrsGlobal(Function &F)
static void markPointerAsAS(Value *Ptr, unsigned AS)
static void handleIntToPtr(Value &V)
static void markPointerAsGlobal(Value *Ptr)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This class represents a conversion between pointers from one address space to another.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:911
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
void setOperand(unsigned i, Value *Val)
Definition User.h:212
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionPass * createNVPTXMarkKernelPtrsGlobalPass()
bool isKernelFunction(const Function &F)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....