LLVM 23.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 unsigned AllocAddrSpace = allocaInst->getAddressSpace();
68 assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
69 AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
70 "AllocaInst can only be in Generic or Local address space for "
71 "NVPTX.");
72
73 Instruction *AllocaInLocalAS = allocaInst;
74
75 // We need to make sure that LLVM has info that alloca needs to go to
76 // ADDRESS_SPACE_LOCAL for InferAddressSpace pass.
77 //
78 // For allocas in ADDRESS_SPACE_LOCAL, we add addrspacecast to
79 // ADDRESS_SPACE_LOCAL and back to ADDRESS_SPACE_GENERIC, so that
80 // the alloca's users still use a generic pointer to operate on.
81 //
82 // For allocas already in ADDRESS_SPACE_LOCAL, we just need
83 // addrspacecast to ADDRESS_SPACE_GENERIC.
84 if (AllocAddrSpace == ADDRESS_SPACE_GENERIC) {
85 auto ASCastToLocalAS = new AddrSpaceCastInst(
86 allocaInst,
87 PointerType::get(allocaInst->getContext(), ADDRESS_SPACE_LOCAL),
88 "");
89 ASCastToLocalAS->insertAfter(allocaInst->getIterator());
90 AllocaInLocalAS = ASCastToLocalAS;
91 }
92
93 auto AllocaInGenericAS = new AddrSpaceCastInst(
94 AllocaInLocalAS,
95 PointerType::get(allocaInst->getContext(), ADDRESS_SPACE_GENERIC),
96 "");
97 AllocaInGenericAS->insertAfter(AllocaInLocalAS->getIterator());
98
99 for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
100 // Check Load, Store, GEP, and BitCast Uses on alloca and make them
101 // use the converted generic address, in order to expose non-generic
102 // addrspacecast to NVPTXInferAddressSpaces. For other types
103 // of instructions this is unnecessary and may introduce redundant
104 // address cast.
105 auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
106 if (LI && LI->getPointerOperand() == allocaInst &&
107 !LI->isVolatile()) {
108 LI->setOperand(LI->getPointerOperandIndex(), AllocaInGenericAS);
109 continue;
110 }
111 auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
112 if (SI && SI->getPointerOperand() == allocaInst &&
113 !SI->isVolatile()) {
114 SI->setOperand(SI->getPointerOperandIndex(), AllocaInGenericAS);
115 continue;
116 }
117 auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
118 if (GI && GI->getPointerOperand() == allocaInst) {
119 GI->setOperand(GI->getPointerOperandIndex(), AllocaInGenericAS);
120 continue;
121 }
122 auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
123 if (BI && BI->getOperand(0) == allocaInst) {
124 BI->setOperand(0, AllocaInGenericAS);
125 continue;
126 }
127 }
128 }
129 }
130 return Changed;
131}
132
134 return new NVPTXLowerAlloca();
135}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
#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:123
Changed
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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:632
FunctionPass * createNVPTXLowerAllocaPass()