LLVM 20.0.0git
NVPTXLowerAlloca.cpp
Go to the documentation of this file.
1//===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
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 all alloca instructions, and add a pair of cast to local address for
10// each of them. For example,
11//
12// %A = alloca i32
13// store i32 0, i32* %A ; emits st.u32
14//
15// will be transformed to
16//
17// %A = alloca i32
18// %Local = addrspacecast i32* %A to i32 addrspace(5)*
19// %Generic = addrspacecast i32 addrspace(5)* %A to i32*
20// store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
21//
22// And we will rely on NVPTXInferAddressSpaces to combine the last two
23// instructions.
24//
25//===----------------------------------------------------------------------===//
26
28#include "NVPTX.h"
29#include "llvm/IR/Function.h"
31#include "llvm/IR/Type.h"
32#include "llvm/Pass.h"
33
34using namespace llvm;
35
36namespace llvm {
38}
39
40namespace {
41class NVPTXLowerAlloca : public FunctionPass {
42 bool runOnFunction(Function &F) override;
43
44public:
45 static char ID; // Pass identification, replacement for typeid
46 NVPTXLowerAlloca() : FunctionPass(ID) {}
47 StringRef getPassName() const override {
48 return "convert address space of alloca'ed memory to local";
49 }
50};
51} // namespace
52
53char NVPTXLowerAlloca::ID = 1;
54
55INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca", "Lower Alloca", false,
56 false)
57
58// =============================================================================
59// Main function for this pass.
60// =============================================================================
61bool NVPTXLowerAlloca::runOnFunction(Function &F) {
62 if (skipFunction(F))
63 return false;
64
65 bool Changed = false;
66 for (auto &BB : F)
67 for (auto &I : BB) {
68 if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
69 Changed = true;
70
71 PointerType *AllocInstPtrTy =
72 cast<PointerType>(allocaInst->getType()->getScalarType());
73 unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
74 assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
75 AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
76 "AllocaInst can only be in Generic or Local address space for "
77 "NVPTX.");
78
79 Instruction *AllocaInLocalAS = allocaInst;
80 auto ETy = allocaInst->getAllocatedType();
81
82 // We need to make sure that LLVM has info that alloca needs to go to
83 // ADDRESS_SPACE_LOCAL for InferAddressSpace pass.
84 //
85 // For allocas in ADDRESS_SPACE_LOCAL, we add addrspacecast to
86 // ADDRESS_SPACE_LOCAL and back to ADDRESS_SPACE_GENERIC, so that
87 // the alloca's users still use a generic pointer to operate on.
88 //
89 // For allocas already in ADDRESS_SPACE_LOCAL, we just need
90 // addrspacecast to ADDRESS_SPACE_GENERIC.
91 if (AllocAddrSpace == ADDRESS_SPACE_GENERIC) {
92 auto ASCastToLocalAS = new AddrSpaceCastInst(
93 allocaInst, PointerType::get(ETy, ADDRESS_SPACE_LOCAL), "");
94 ASCastToLocalAS->insertAfter(allocaInst);
95 AllocaInLocalAS = ASCastToLocalAS;
96 }
97
98 auto AllocaInGenericAS = new AddrSpaceCastInst(
99 AllocaInLocalAS, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
100 AllocaInGenericAS->insertAfter(AllocaInLocalAS);
101
102 for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
103 // Check Load, Store, GEP, and BitCast Uses on alloca and make them
104 // use the converted generic address, in order to expose non-generic
105 // addrspacecast to NVPTXInferAddressSpaces. For other types
106 // of instructions this is unnecessary and may introduce redundant
107 // address cast.
108 auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
109 if (LI && LI->getPointerOperand() == allocaInst &&
110 !LI->isVolatile()) {
111 LI->setOperand(LI->getPointerOperandIndex(), AllocaInGenericAS);
112 continue;
113 }
114 auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
115 if (SI && SI->getPointerOperand() == allocaInst &&
116 !SI->isVolatile()) {
117 SI->setOperand(SI->getPointerOperandIndex(), AllocaInGenericAS);
118 continue;
119 }
120 auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
121 if (GI && GI->getPointerOperand() == allocaInst) {
122 GI->setOperand(GI->getPointerOperandIndex(), AllocaInGenericAS);
123 continue;
124 }
125 auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
126 if (BI && BI->getOperand(0) == allocaInst) {
127 BI->setOperand(0, AllocaInGenericAS);
128 continue;
129 }
130 }
131 }
132 }
133 return Changed;
134}
135
137 return new NVPTXLowerAlloca();
138}
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class represents a conversion between pointers from one address space to another.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeNVPTXLowerAllocaPass(PassRegistry &)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
FunctionPass * createNVPTXLowerAllocaPass()