LLVM 22.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 {
37class NVPTXLowerAlloca : public FunctionPass {
38 bool runOnFunction(Function &F) override;
39
40public:
41 static char ID; // Pass identification, replacement for typeid
42 NVPTXLowerAlloca() : FunctionPass(ID) {}
43 StringRef getPassName() const override {
44 return "convert address space of alloca'ed memory to local";
45 }
46};
47} // namespace
48
49char NVPTXLowerAlloca::ID = 1;
50
51INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca", "Lower Alloca", false,
52 false)
53
54// =============================================================================
55// Main function for this pass.
56// =============================================================================
57bool NVPTXLowerAlloca::runOnFunction(Function &F) {
58 if (skipFunction(F))
59 return false;
60
61 bool Changed = false;
62 for (auto &BB : F)
63 for (auto &I : BB) {
64 if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
65 Changed = true;
66
67 PointerType *AllocInstPtrTy =
68 cast<PointerType>(allocaInst->getType()->getScalarType());
69 unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
70 assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
71 AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
72 "AllocaInst can only be in Generic or Local address space for "
73 "NVPTX.");
74
75 Instruction *AllocaInLocalAS = allocaInst;
76 auto ETy = allocaInst->getAllocatedType();
77
78 // We need to make sure that LLVM has info that alloca needs to go to
79 // ADDRESS_SPACE_LOCAL for InferAddressSpace pass.
80 //
81 // For allocas in ADDRESS_SPACE_LOCAL, we add addrspacecast to
82 // ADDRESS_SPACE_LOCAL and back to ADDRESS_SPACE_GENERIC, so that
83 // the alloca's users still use a generic pointer to operate on.
84 //
85 // For allocas already in ADDRESS_SPACE_LOCAL, we just need
86 // addrspacecast to ADDRESS_SPACE_GENERIC.
87 if (AllocAddrSpace == ADDRESS_SPACE_GENERIC) {
88 auto ASCastToLocalAS = new AddrSpaceCastInst(
89 allocaInst,
90 PointerType::get(ETy->getContext(), ADDRESS_SPACE_LOCAL), "");
91 ASCastToLocalAS->insertAfter(allocaInst->getIterator());
92 AllocaInLocalAS = ASCastToLocalAS;
93 }
94
95 auto AllocaInGenericAS = new AddrSpaceCastInst(
96 AllocaInLocalAS,
97 PointerType::get(ETy->getContext(), ADDRESS_SPACE_GENERIC), "");
98 AllocaInGenericAS->insertAfter(AllocaInLocalAS->getIterator());
99
100 for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
101 // Check Load, Store, GEP, and BitCast Uses on alloca and make them
102 // use the converted generic address, in order to expose non-generic
103 // addrspacecast to NVPTXInferAddressSpaces. For other types
104 // of instructions this is unnecessary and may introduce redundant
105 // address cast.
106 auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
107 if (LI && LI->getPointerOperand() == allocaInst &&
108 !LI->isVolatile()) {
109 LI->setOperand(LI->getPointerOperandIndex(), AllocaInGenericAS);
110 continue;
111 }
112 auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
113 if (SI && SI->getPointerOperand() == allocaInst &&
114 !SI->isVolatile()) {
115 SI->setOperand(SI->getPointerOperandIndex(), AllocaInGenericAS);
116 continue;
117 }
118 auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
119 if (GI && GI->getPointerOperand() == allocaInst) {
120 GI->setOperand(GI->getPointerOperandIndex(), AllocaInGenericAS);
121 continue;
122 }
123 auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
124 if (BI && BI->getOperand(0) == allocaInst) {
125 BI->setOperand(0, AllocaInGenericAS);
126 continue;
127 }
128 }
129 }
130 }
131 return Changed;
132}
133
135 return new NVPTXLowerAlloca();
136}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:56
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:314
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
self_iterator getIterator()
Definition ilist_node.h:130
Changed
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:649
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:634
FunctionPass * createNVPTXLowerAllocaPass()
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565