Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 1 | //===-- AMDGPUPromoteAlloca.cpp - Promote Allocas -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass eliminates allocas by either converting them into vectors or |
| 11 | // by migrating them to local address space. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AMDGPU.h" |
| 16 | #include "AMDGPUSubtarget.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 17 | #include "Utils/AMDGPUBaseInfo.h" |
| 18 | #include "llvm/ADT/APInt.h" |
| 19 | #include "llvm/ADT/None.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/ADT/Triple.h" |
| 23 | #include "llvm/ADT/Twine.h" |
Changpeng Fang | c85abbd | 2017-01-24 19:06:28 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CaptureTracking.h" |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/TargetPassConfig.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Attributes.h" |
| 28 | #include "llvm/IR/BasicBlock.h" |
| 29 | #include "llvm/IR/Constant.h" |
| 30 | #include "llvm/IR/Constants.h" |
| 31 | #include "llvm/IR/DataLayout.h" |
| 32 | #include "llvm/IR/DerivedTypes.h" |
| 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/IR/GlobalValue.h" |
| 35 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Instruction.h" |
| 38 | #include "llvm/IR/Instructions.h" |
Matt Arsenault | bafc9dc | 2016-03-11 08:20:50 +0000 | [diff] [blame] | 39 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Intrinsics.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 41 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Metadata.h" |
| 43 | #include "llvm/IR/Module.h" |
| 44 | #include "llvm/IR/Type.h" |
| 45 | #include "llvm/IR/User.h" |
| 46 | #include "llvm/IR/Value.h" |
| 47 | #include "llvm/Pass.h" |
| 48 | #include "llvm/Support/Casting.h" |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ErrorHandling.h" |
| 51 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 52 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetMachine.h" |
| 54 | #include <algorithm> |
| 55 | #include <cassert> |
| 56 | #include <cstdint> |
| 57 | #include <map> |
| 58 | #include <tuple> |
| 59 | #include <utility> |
| 60 | #include <vector> |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 61 | |
| 62 | #define DEBUG_TYPE "amdgpu-promote-alloca" |
| 63 | |
| 64 | using namespace llvm; |
| 65 | |
| 66 | namespace { |
| 67 | |
Changpeng Fang | ba92059 | 2018-02-16 19:14:17 +0000 | [diff] [blame] | 68 | static cl::opt<bool> DisablePromoteAllocaToVector( |
| 69 | "disable-promote-alloca-to-vector", |
| 70 | cl::desc("Disable promote alloca to vector"), |
| 71 | cl::init(false)); |
| 72 | |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 73 | // FIXME: This can create globals so should be a module pass. |
Matt Arsenault | bafc9dc | 2016-03-11 08:20:50 +0000 | [diff] [blame] | 74 | class AMDGPUPromoteAlloca : public FunctionPass { |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 75 | private: |
| 76 | const TargetMachine *TM; |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 77 | Module *Mod = nullptr; |
| 78 | const DataLayout *DL = nullptr; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 79 | AMDGPUAS AS; |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 80 | |
| 81 | // FIXME: This should be per-kernel. |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 82 | uint32_t LocalMemLimit = 0; |
| 83 | uint32_t CurrentLocalMemUsage = 0; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 84 | |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 85 | bool IsAMDGCN = false; |
| 86 | bool IsAMDHSA = false; |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 87 | |
| 88 | std::pair<Value *, Value *> getLocalSizeYZ(IRBuilder<> &Builder); |
| 89 | Value *getWorkitemID(IRBuilder<> &Builder, unsigned N); |
| 90 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 91 | /// BaseAlloca is the alloca root the search started from. |
| 92 | /// Val may be that alloca or a recursive user of it. |
| 93 | bool collectUsesWithPtrTypes(Value *BaseAlloca, |
| 94 | Value *Val, |
| 95 | std::vector<Value*> &WorkList) const; |
| 96 | |
| 97 | /// Val is a derived pointer from Alloca. OpIdx0/OpIdx1 are the operand |
| 98 | /// indices to an instruction with 2 pointer inputs (e.g. select, icmp). |
| 99 | /// Returns true if both operands are derived from the same alloca. Val should |
| 100 | /// be the same value as one of the input operands of UseInst. |
| 101 | bool binaryOpIsDerivedFromSameAlloca(Value *Alloca, Value *Val, |
| 102 | Instruction *UseInst, |
| 103 | int OpIdx0, int OpIdx1) const; |
| 104 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 105 | /// Check whether we have enough local memory for promotion. |
| 106 | bool hasSufficientLocalMem(const Function &F); |
| 107 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 108 | public: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 109 | static char ID; |
| 110 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 111 | AMDGPUPromoteAlloca() : FunctionPass(ID) {} |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 112 | |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 113 | bool doInitialization(Module &M) override; |
| 114 | bool runOnFunction(Function &F) override; |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 115 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 116 | StringRef getPassName() const override { return "AMDGPU Promote Alloca"; } |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 117 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 118 | bool handleAlloca(AllocaInst &I, bool SufficientLDS); |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 119 | |
| 120 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 121 | AU.setPreservesCFG(); |
| 122 | FunctionPass::getAnalysisUsage(AU); |
| 123 | } |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 126 | } // end anonymous namespace |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 127 | |
| 128 | char AMDGPUPromoteAlloca::ID = 0; |
| 129 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 130 | INITIALIZE_PASS(AMDGPUPromoteAlloca, DEBUG_TYPE, |
| 131 | "AMDGPU promote alloca to vector or LDS", false, false) |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 132 | |
| 133 | char &llvm::AMDGPUPromoteAllocaID = AMDGPUPromoteAlloca::ID; |
| 134 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 135 | bool AMDGPUPromoteAlloca::doInitialization(Module &M) { |
| 136 | Mod = &M; |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 137 | DL = &Mod->getDataLayout(); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 138 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool AMDGPUPromoteAlloca::runOnFunction(Function &F) { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 143 | if (skipFunction(F)) |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 144 | return false; |
| 145 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 146 | if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) |
| 147 | TM = &TPC->getTM<TargetMachine>(); |
| 148 | else |
| 149 | return false; |
| 150 | |
| 151 | const Triple &TT = TM->getTargetTriple(); |
| 152 | IsAMDGCN = TT.getArch() == Triple::amdgcn; |
| 153 | IsAMDHSA = TT.getOS() == Triple::AMDHSA; |
| 154 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 155 | const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>(F); |
| 156 | if (!ST.isPromoteAllocaEnabled()) |
| 157 | return false; |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 158 | |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 159 | AS = AMDGPU::getAMDGPUAS(*F.getParent()); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 160 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 161 | bool SufficientLDS = hasSufficientLocalMem(F); |
| 162 | bool Changed = false; |
Matt Arsenault | bafc9dc | 2016-03-11 08:20:50 +0000 | [diff] [blame] | 163 | BasicBlock &EntryBB = *F.begin(); |
| 164 | for (auto I = EntryBB.begin(), E = EntryBB.end(); I != E; ) { |
| 165 | AllocaInst *AI = dyn_cast<AllocaInst>(I); |
| 166 | |
| 167 | ++I; |
| 168 | if (AI) |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 169 | Changed |= handleAlloca(*AI, SufficientLDS); |
Matt Arsenault | bafc9dc | 2016-03-11 08:20:50 +0000 | [diff] [blame] | 170 | } |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 171 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 172 | return Changed; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 175 | std::pair<Value *, Value *> |
| 176 | AMDGPUPromoteAlloca::getLocalSizeYZ(IRBuilder<> &Builder) { |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 177 | const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>( |
| 178 | *Builder.GetInsertBlock()->getParent()); |
| 179 | |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 180 | if (!IsAMDHSA) { |
| 181 | Function *LocalSizeYFn |
| 182 | = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_y); |
| 183 | Function *LocalSizeZFn |
| 184 | = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_z); |
| 185 | |
| 186 | CallInst *LocalSizeY = Builder.CreateCall(LocalSizeYFn, {}); |
| 187 | CallInst *LocalSizeZ = Builder.CreateCall(LocalSizeZFn, {}); |
| 188 | |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 189 | ST.makeLIDRangeMetadata(LocalSizeY); |
| 190 | ST.makeLIDRangeMetadata(LocalSizeZ); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 191 | |
| 192 | return std::make_pair(LocalSizeY, LocalSizeZ); |
| 193 | } |
| 194 | |
| 195 | // We must read the size out of the dispatch pointer. |
| 196 | assert(IsAMDGCN); |
| 197 | |
| 198 | // We are indexing into this struct, and want to extract the workgroup_size_* |
| 199 | // fields. |
| 200 | // |
| 201 | // typedef struct hsa_kernel_dispatch_packet_s { |
| 202 | // uint16_t header; |
| 203 | // uint16_t setup; |
| 204 | // uint16_t workgroup_size_x ; |
| 205 | // uint16_t workgroup_size_y; |
| 206 | // uint16_t workgroup_size_z; |
| 207 | // uint16_t reserved0; |
| 208 | // uint32_t grid_size_x ; |
| 209 | // uint32_t grid_size_y ; |
| 210 | // uint32_t grid_size_z; |
| 211 | // |
| 212 | // uint32_t private_segment_size; |
| 213 | // uint32_t group_segment_size; |
| 214 | // uint64_t kernel_object; |
| 215 | // |
| 216 | // #ifdef HSA_LARGE_MODEL |
| 217 | // void *kernarg_address; |
| 218 | // #elif defined HSA_LITTLE_ENDIAN |
| 219 | // void *kernarg_address; |
| 220 | // uint32_t reserved1; |
| 221 | // #else |
| 222 | // uint32_t reserved1; |
| 223 | // void *kernarg_address; |
| 224 | // #endif |
| 225 | // uint64_t reserved2; |
| 226 | // hsa_signal_t completion_signal; // uint64_t wrapper |
| 227 | // } hsa_kernel_dispatch_packet_t |
| 228 | // |
| 229 | Function *DispatchPtrFn |
| 230 | = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_dispatch_ptr); |
| 231 | |
| 232 | CallInst *DispatchPtr = Builder.CreateCall(DispatchPtrFn, {}); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 233 | DispatchPtr->addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
| 234 | DispatchPtr->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 235 | |
| 236 | // Size of the dispatch packet struct. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 237 | DispatchPtr->addDereferenceableAttr(AttributeList::ReturnIndex, 64); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 238 | |
| 239 | Type *I32Ty = Type::getInt32Ty(Mod->getContext()); |
| 240 | Value *CastDispatchPtr = Builder.CreateBitCast( |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 241 | DispatchPtr, PointerType::get(I32Ty, AS.CONSTANT_ADDRESS)); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 242 | |
| 243 | // We could do a single 64-bit load here, but it's likely that the basic |
| 244 | // 32-bit and extract sequence is already present, and it is probably easier |
| 245 | // to CSE this. The loads should be mergable later anyway. |
| 246 | Value *GEPXY = Builder.CreateConstInBoundsGEP1_64(CastDispatchPtr, 1); |
| 247 | LoadInst *LoadXY = Builder.CreateAlignedLoad(GEPXY, 4); |
| 248 | |
| 249 | Value *GEPZU = Builder.CreateConstInBoundsGEP1_64(CastDispatchPtr, 2); |
| 250 | LoadInst *LoadZU = Builder.CreateAlignedLoad(GEPZU, 4); |
| 251 | |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 252 | MDNode *MD = MDNode::get(Mod->getContext(), None); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 253 | LoadXY->setMetadata(LLVMContext::MD_invariant_load, MD); |
| 254 | LoadZU->setMetadata(LLVMContext::MD_invariant_load, MD); |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 255 | ST.makeLIDRangeMetadata(LoadZU); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 256 | |
| 257 | // Extract y component. Upper half of LoadZU should be zero already. |
| 258 | Value *Y = Builder.CreateLShr(LoadXY, 16); |
| 259 | |
| 260 | return std::make_pair(Y, LoadZU); |
| 261 | } |
| 262 | |
| 263 | Value *AMDGPUPromoteAlloca::getWorkitemID(IRBuilder<> &Builder, unsigned N) { |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 264 | const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>( |
| 265 | *Builder.GetInsertBlock()->getParent()); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 266 | Intrinsic::ID IntrID = Intrinsic::ID::not_intrinsic; |
| 267 | |
| 268 | switch (N) { |
| 269 | case 0: |
| 270 | IntrID = IsAMDGCN ? Intrinsic::amdgcn_workitem_id_x |
| 271 | : Intrinsic::r600_read_tidig_x; |
| 272 | break; |
| 273 | case 1: |
| 274 | IntrID = IsAMDGCN ? Intrinsic::amdgcn_workitem_id_y |
| 275 | : Intrinsic::r600_read_tidig_y; |
| 276 | break; |
| 277 | |
| 278 | case 2: |
| 279 | IntrID = IsAMDGCN ? Intrinsic::amdgcn_workitem_id_z |
| 280 | : Intrinsic::r600_read_tidig_z; |
| 281 | break; |
| 282 | default: |
| 283 | llvm_unreachable("invalid dimension"); |
| 284 | } |
| 285 | |
| 286 | Function *WorkitemIdFn = Intrinsic::getDeclaration(Mod, IntrID); |
| 287 | CallInst *CI = Builder.CreateCall(WorkitemIdFn); |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 288 | ST.makeLIDRangeMetadata(CI); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 289 | |
| 290 | return CI; |
| 291 | } |
| 292 | |
Matt Arsenault | 37ab4cf | 2017-09-14 18:02:29 +0000 | [diff] [blame] | 293 | static VectorType *arrayTypeToVecType(ArrayType *ArrayTy) { |
| 294 | return VectorType::get(ArrayTy->getElementType(), |
| 295 | ArrayTy->getNumElements()); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 298 | static Value * |
| 299 | calculateVectorIndex(Value *Ptr, |
| 300 | const std::map<GetElementPtrInst *, Value *> &GEPIdx) { |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 301 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(Ptr); |
| 302 | |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 303 | auto I = GEPIdx.find(GEP); |
| 304 | return I == GEPIdx.end() ? nullptr : I->second; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static Value* GEPToVectorIndex(GetElementPtrInst *GEP) { |
| 308 | // FIXME we only support simple cases |
| 309 | if (GEP->getNumOperands() != 3) |
Matt Arsenault | efb2454 | 2016-07-18 18:34:53 +0000 | [diff] [blame] | 310 | return nullptr; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 311 | |
| 312 | ConstantInt *I0 = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
| 313 | if (!I0 || !I0->isZero()) |
Matt Arsenault | efb2454 | 2016-07-18 18:34:53 +0000 | [diff] [blame] | 314 | return nullptr; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 315 | |
| 316 | return GEP->getOperand(2); |
| 317 | } |
| 318 | |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 319 | // Not an instruction handled below to turn into a vector. |
| 320 | // |
| 321 | // TODO: Check isTriviallyVectorizable for calls and handle other |
| 322 | // instructions. |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 323 | static bool canVectorizeInst(Instruction *Inst, User *User) { |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 324 | switch (Inst->getOpcode()) { |
Changpeng Fang | 161e8c39af | 2017-05-12 20:31:12 +0000 | [diff] [blame] | 325 | case Instruction::Load: { |
| 326 | LoadInst *LI = cast<LoadInst>(Inst); |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 327 | // Currently only handle the case where the Pointer Operand is a GEP so check for that case. |
| 328 | return isa<GetElementPtrInst>(LI->getPointerOperand()) && !LI->isVolatile(); |
Changpeng Fang | 161e8c39af | 2017-05-12 20:31:12 +0000 | [diff] [blame] | 329 | } |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 330 | case Instruction::BitCast: |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 331 | return true; |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 332 | case Instruction::Store: { |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 333 | // Must be the stored pointer operand, not a stored value, plus |
| 334 | // since it should be canonical form, the User should be a GEP. |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 335 | StoreInst *SI = cast<StoreInst>(Inst); |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 336 | return (SI->getPointerOperand() == User) && isa<GetElementPtrInst>(User) && !SI->isVolatile(); |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 337 | } |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 338 | default: |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 343 | static bool tryPromoteAllocaToVector(AllocaInst *Alloca, AMDGPUAS AS) { |
Changpeng Fang | ba92059 | 2018-02-16 19:14:17 +0000 | [diff] [blame] | 344 | |
| 345 | if (DisablePromoteAllocaToVector) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 346 | LLVM_DEBUG(dbgs() << " Promotion alloca to vector is disabled\n"); |
Changpeng Fang | ba92059 | 2018-02-16 19:14:17 +0000 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | |
Matt Arsenault | fb8cdba | 2016-02-02 19:32:35 +0000 | [diff] [blame] | 350 | ArrayType *AllocaTy = dyn_cast<ArrayType>(Alloca->getAllocatedType()); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 351 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 352 | LLVM_DEBUG(dbgs() << "Alloca candidate for vectorization\n"); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 353 | |
| 354 | // FIXME: There is no reason why we can't support larger arrays, we |
| 355 | // are just being conservative for now. |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 356 | // FIXME: We also reject alloca's of the form [ 2 x [ 2 x i32 ]] or equivalent. Potentially these |
| 357 | // could also be promoted but we don't currently handle this case |
Matt Arsenault | fb8cdba | 2016-02-02 19:32:35 +0000 | [diff] [blame] | 358 | if (!AllocaTy || |
Changpeng Fang | ba92059 | 2018-02-16 19:14:17 +0000 | [diff] [blame] | 359 | AllocaTy->getNumElements() > 16 || |
Matt Arsenault | 37ab4cf | 2017-09-14 18:02:29 +0000 | [diff] [blame] | 360 | AllocaTy->getNumElements() < 2 || |
| 361 | !VectorType::isValidElementType(AllocaTy->getElementType())) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 362 | LLVM_DEBUG(dbgs() << " Cannot convert type to vector\n"); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
| 366 | std::map<GetElementPtrInst*, Value*> GEPVectorIdx; |
| 367 | std::vector<Value*> WorkList; |
| 368 | for (User *AllocaUser : Alloca->users()) { |
| 369 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(AllocaUser); |
| 370 | if (!GEP) { |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 371 | if (!canVectorizeInst(cast<Instruction>(AllocaUser), Alloca)) |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 372 | return false; |
| 373 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 374 | WorkList.push_back(AllocaUser); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | Value *Index = GEPToVectorIndex(GEP); |
| 379 | |
| 380 | // If we can't compute a vector index from this GEP, then we can't |
| 381 | // promote this alloca to vector. |
| 382 | if (!Index) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 383 | LLVM_DEBUG(dbgs() << " Cannot compute vector index for GEP " << *GEP |
| 384 | << '\n'); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | GEPVectorIdx[GEP] = Index; |
| 389 | for (User *GEPUser : AllocaUser->users()) { |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 390 | if (!canVectorizeInst(cast<Instruction>(GEPUser), AllocaUser)) |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 391 | return false; |
| 392 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 393 | WorkList.push_back(GEPUser); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | VectorType *VectorTy = arrayTypeToVecType(AllocaTy); |
| 398 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 399 | LLVM_DEBUG(dbgs() << " Converting alloca to vector " << *AllocaTy << " -> " |
| 400 | << *VectorTy << '\n'); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 401 | |
Matt Arsenault | fb8cdba | 2016-02-02 19:32:35 +0000 | [diff] [blame] | 402 | for (Value *V : WorkList) { |
| 403 | Instruction *Inst = cast<Instruction>(V); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 404 | IRBuilder<> Builder(Inst); |
| 405 | switch (Inst->getOpcode()) { |
| 406 | case Instruction::Load: { |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 407 | Type *VecPtrTy = VectorTy->getPointerTo(AS.PRIVATE_ADDRESS); |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 408 | Value *Ptr = cast<LoadInst>(Inst)->getPointerOperand(); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 409 | Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx); |
Matt Arsenault | efb2454 | 2016-07-18 18:34:53 +0000 | [diff] [blame] | 410 | |
| 411 | Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 412 | Value *VecValue = Builder.CreateLoad(BitCast); |
| 413 | Value *ExtractElement = Builder.CreateExtractElement(VecValue, Index); |
| 414 | Inst->replaceAllUsesWith(ExtractElement); |
| 415 | Inst->eraseFromParent(); |
| 416 | break; |
| 417 | } |
| 418 | case Instruction::Store: { |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 419 | Type *VecPtrTy = VectorTy->getPointerTo(AS.PRIVATE_ADDRESS); |
Matt Arsenault | efb2454 | 2016-07-18 18:34:53 +0000 | [diff] [blame] | 420 | |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 421 | StoreInst *SI = cast<StoreInst>(Inst); |
| 422 | Value *Ptr = SI->getPointerOperand(); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 423 | Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx); |
Matt Arsenault | efb2454 | 2016-07-18 18:34:53 +0000 | [diff] [blame] | 424 | Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 425 | Value *VecValue = Builder.CreateLoad(BitCast); |
| 426 | Value *NewVecValue = Builder.CreateInsertElement(VecValue, |
David Stuttard | 82618ba | 2017-06-09 14:16:22 +0000 | [diff] [blame] | 427 | SI->getValueOperand(), |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 428 | Index); |
| 429 | Builder.CreateStore(NewVecValue, BitCast); |
| 430 | Inst->eraseFromParent(); |
| 431 | break; |
| 432 | } |
| 433 | case Instruction::BitCast: |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 434 | case Instruction::AddrSpaceCast: |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 435 | break; |
| 436 | |
| 437 | default: |
Matt Arsenault | 642d2e7 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 438 | llvm_unreachable("Inconsistency in instructions promotable to vector"); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 444 | static bool isCallPromotable(CallInst *CI) { |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 445 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
| 446 | if (!II) |
| 447 | return false; |
| 448 | |
| 449 | switch (II->getIntrinsicID()) { |
| 450 | case Intrinsic::memcpy: |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 451 | case Intrinsic::memmove: |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 452 | case Intrinsic::memset: |
| 453 | case Intrinsic::lifetime_start: |
| 454 | case Intrinsic::lifetime_end: |
| 455 | case Intrinsic::invariant_start: |
| 456 | case Intrinsic::invariant_end: |
Piotr Padlewski | 5dde809 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 457 | case Intrinsic::launder_invariant_group: |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 458 | case Intrinsic::objectsize: |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 459 | return true; |
| 460 | default: |
| 461 | return false; |
| 462 | } |
| 463 | } |
| 464 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 465 | bool AMDGPUPromoteAlloca::binaryOpIsDerivedFromSameAlloca(Value *BaseAlloca, |
| 466 | Value *Val, |
| 467 | Instruction *Inst, |
| 468 | int OpIdx0, |
| 469 | int OpIdx1) const { |
| 470 | // Figure out which operand is the one we might not be promoting. |
| 471 | Value *OtherOp = Inst->getOperand(OpIdx0); |
| 472 | if (Val == OtherOp) |
| 473 | OtherOp = Inst->getOperand(OpIdx1); |
| 474 | |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 475 | if (isa<ConstantPointerNull>(OtherOp)) |
| 476 | return true; |
| 477 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 478 | Value *OtherObj = GetUnderlyingObject(OtherOp, *DL); |
| 479 | if (!isa<AllocaInst>(OtherObj)) |
| 480 | return false; |
| 481 | |
| 482 | // TODO: We should be able to replace undefs with the right pointer type. |
| 483 | |
| 484 | // TODO: If we know the other base object is another promotable |
| 485 | // alloca, not necessarily this alloca, we can do this. The |
| 486 | // important part is both must have the same address space at |
| 487 | // the end. |
| 488 | if (OtherObj != BaseAlloca) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 489 | LLVM_DEBUG( |
| 490 | dbgs() << "Found a binary instruction with another alloca object\n"); |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | bool AMDGPUPromoteAlloca::collectUsesWithPtrTypes( |
| 498 | Value *BaseAlloca, |
| 499 | Value *Val, |
| 500 | std::vector<Value*> &WorkList) const { |
| 501 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 502 | for (User *User : Val->users()) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 503 | if (is_contained(WorkList, User)) |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 504 | continue; |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 505 | |
Matt Arsenault | fdcd39a | 2015-07-28 18:29:14 +0000 | [diff] [blame] | 506 | if (CallInst *CI = dyn_cast<CallInst>(User)) { |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 507 | if (!isCallPromotable(CI)) |
Matt Arsenault | fdcd39a | 2015-07-28 18:29:14 +0000 | [diff] [blame] | 508 | return false; |
| 509 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 510 | WorkList.push_back(User); |
| 511 | continue; |
| 512 | } |
Tom Stellard | 5b2927f | 2014-10-31 20:52:04 +0000 | [diff] [blame] | 513 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 514 | Instruction *UseInst = cast<Instruction>(User); |
| 515 | if (UseInst->getOpcode() == Instruction::PtrToInt) |
Tom Stellard | 5b2927f | 2014-10-31 20:52:04 +0000 | [diff] [blame] | 516 | return false; |
| 517 | |
Matt Arsenault | 210b7cf | 2016-07-18 19:00:07 +0000 | [diff] [blame] | 518 | if (LoadInst *LI = dyn_cast<LoadInst>(UseInst)) { |
Matt Arsenault | c438ef5 | 2016-05-18 23:20:24 +0000 | [diff] [blame] | 519 | if (LI->isVolatile()) |
| 520 | return false; |
| 521 | |
| 522 | continue; |
| 523 | } |
| 524 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 525 | if (StoreInst *SI = dyn_cast<StoreInst>(UseInst)) { |
Matt Arsenault | 0a30e45 | 2016-03-23 23:17:29 +0000 | [diff] [blame] | 526 | if (SI->isVolatile()) |
| 527 | return false; |
| 528 | |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 529 | // Reject if the stored value is not the pointer operand. |
| 530 | if (SI->getPointerOperand() != Val) |
| 531 | return false; |
Matt Arsenault | 210b7cf | 2016-07-18 19:00:07 +0000 | [diff] [blame] | 532 | } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UseInst)) { |
Matt Arsenault | 0a30e45 | 2016-03-23 23:17:29 +0000 | [diff] [blame] | 533 | if (RMW->isVolatile()) |
| 534 | return false; |
Matt Arsenault | 210b7cf | 2016-07-18 19:00:07 +0000 | [diff] [blame] | 535 | } else if (AtomicCmpXchgInst *CAS = dyn_cast<AtomicCmpXchgInst>(UseInst)) { |
Matt Arsenault | 0a30e45 | 2016-03-23 23:17:29 +0000 | [diff] [blame] | 536 | if (CAS->isVolatile()) |
| 537 | return false; |
Matt Arsenault | 7227cc1 | 2015-07-28 18:47:00 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 540 | // Only promote a select if we know that the other select operand |
| 541 | // is from another pointer that will also be promoted. |
| 542 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 543 | if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, ICmp, 0, 1)) |
| 544 | return false; |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 545 | |
| 546 | // May need to rewrite constant operands. |
| 547 | WorkList.push_back(ICmp); |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Matt Arsenault | 2402b95 | 2016-12-10 00:52:50 +0000 | [diff] [blame] | 550 | if (UseInst->getOpcode() == Instruction::AddrSpaceCast) { |
Changpeng Fang | c85abbd | 2017-01-24 19:06:28 +0000 | [diff] [blame] | 551 | // Give up if the pointer may be captured. |
| 552 | if (PointerMayBeCaptured(UseInst, true, true)) |
| 553 | return false; |
Matt Arsenault | 2402b95 | 2016-12-10 00:52:50 +0000 | [diff] [blame] | 554 | // Don't collect the users of this. |
| 555 | WorkList.push_back(User); |
| 556 | continue; |
| 557 | } |
| 558 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 559 | if (!User->getType()->isPointerTy()) |
| 560 | continue; |
Tom Stellard | 5b2927f | 2014-10-31 20:52:04 +0000 | [diff] [blame] | 561 | |
Matt Arsenault | de42081 | 2016-02-02 21:16:12 +0000 | [diff] [blame] | 562 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UseInst)) { |
| 563 | // Be conservative if an address could be computed outside the bounds of |
| 564 | // the alloca. |
| 565 | if (!GEP->isInBounds()) |
| 566 | return false; |
| 567 | } |
| 568 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 569 | // Only promote a select if we know that the other select operand is from |
| 570 | // another pointer that will also be promoted. |
| 571 | if (SelectInst *SI = dyn_cast<SelectInst>(UseInst)) { |
| 572 | if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, SI, 1, 2)) |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | // Repeat for phis. |
| 577 | if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) { |
| 578 | // TODO: Handle more complex cases. We should be able to replace loops |
| 579 | // over arrays. |
| 580 | switch (Phi->getNumIncomingValues()) { |
| 581 | case 1: |
| 582 | break; |
| 583 | case 2: |
| 584 | if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, Phi, 0, 1)) |
| 585 | return false; |
| 586 | break; |
| 587 | default: |
| 588 | return false; |
| 589 | } |
| 590 | } |
| 591 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 592 | WorkList.push_back(User); |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 593 | if (!collectUsesWithPtrTypes(BaseAlloca, User, WorkList)) |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 594 | return false; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 595 | } |
Matt Arsenault | ad13484 | 2016-02-02 19:18:53 +0000 | [diff] [blame] | 596 | |
| 597 | return true; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 600 | bool AMDGPUPromoteAlloca::hasSufficientLocalMem(const Function &F) { |
| 601 | |
| 602 | FunctionType *FTy = F.getFunctionType(); |
| 603 | const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>(F); |
| 604 | |
| 605 | // If the function has any arguments in the local address space, then it's |
| 606 | // possible these arguments require the entire local memory space, so |
| 607 | // we cannot use local memory in the pass. |
| 608 | for (Type *ParamTy : FTy->params()) { |
| 609 | PointerType *PtrTy = dyn_cast<PointerType>(ParamTy); |
| 610 | if (PtrTy && PtrTy->getAddressSpace() == AS.LOCAL_ADDRESS) { |
| 611 | LocalMemLimit = 0; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 612 | LLVM_DEBUG(dbgs() << "Function has local memory argument. Promoting to " |
| 613 | "local memory disabled.\n"); |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | LocalMemLimit = ST.getLocalMemorySize(); |
| 619 | if (LocalMemLimit == 0) |
| 620 | return false; |
| 621 | |
| 622 | const DataLayout &DL = Mod->getDataLayout(); |
| 623 | |
| 624 | // Check how much local memory is being used by global objects |
| 625 | CurrentLocalMemUsage = 0; |
| 626 | for (GlobalVariable &GV : Mod->globals()) { |
| 627 | if (GV.getType()->getAddressSpace() != AS.LOCAL_ADDRESS) |
| 628 | continue; |
| 629 | |
| 630 | for (const User *U : GV.users()) { |
| 631 | const Instruction *Use = dyn_cast<Instruction>(U); |
| 632 | if (!Use) |
| 633 | continue; |
| 634 | |
| 635 | if (Use->getParent()->getParent() == &F) { |
| 636 | unsigned Align = GV.getAlignment(); |
| 637 | if (Align == 0) |
| 638 | Align = DL.getABITypeAlignment(GV.getValueType()); |
| 639 | |
| 640 | // FIXME: Try to account for padding here. The padding is currently |
| 641 | // determined from the inverse order of uses in the function. I'm not |
| 642 | // sure if the use list order is in any way connected to this, so the |
| 643 | // total reported size is likely incorrect. |
| 644 | uint64_t AllocSize = DL.getTypeAllocSize(GV.getValueType()); |
| 645 | CurrentLocalMemUsage = alignTo(CurrentLocalMemUsage, Align); |
| 646 | CurrentLocalMemUsage += AllocSize; |
| 647 | break; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | unsigned MaxOccupancy = ST.getOccupancyWithLocalMemSize(CurrentLocalMemUsage, |
| 653 | F); |
| 654 | |
| 655 | // Restrict local memory usage so that we don't drastically reduce occupancy, |
| 656 | // unless it is already significantly reduced. |
| 657 | |
| 658 | // TODO: Have some sort of hint or other heuristics to guess occupancy based |
| 659 | // on other factors.. |
| 660 | unsigned OccupancyHint = ST.getWavesPerEU(F).second; |
| 661 | if (OccupancyHint == 0) |
| 662 | OccupancyHint = 7; |
| 663 | |
| 664 | // Clamp to max value. |
| 665 | OccupancyHint = std::min(OccupancyHint, ST.getMaxWavesPerEU()); |
| 666 | |
| 667 | // Check the hint but ignore it if it's obviously wrong from the existing LDS |
| 668 | // usage. |
| 669 | MaxOccupancy = std::min(OccupancyHint, MaxOccupancy); |
| 670 | |
| 671 | |
| 672 | // Round up to the next tier of usage. |
| 673 | unsigned MaxSizeWithWaveCount |
| 674 | = ST.getMaxLocalMemSizeWithWaveCount(MaxOccupancy, F); |
| 675 | |
| 676 | // Program is possibly broken by using more local mem than available. |
| 677 | if (CurrentLocalMemUsage > MaxSizeWithWaveCount) |
| 678 | return false; |
| 679 | |
| 680 | LocalMemLimit = MaxSizeWithWaveCount; |
| 681 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 682 | LLVM_DEBUG(dbgs() << F.getName() << " uses " << CurrentLocalMemUsage |
| 683 | << " bytes of LDS\n" |
| 684 | << " Rounding size to " << MaxSizeWithWaveCount |
| 685 | << " with a maximum occupancy of " << MaxOccupancy << '\n' |
| 686 | << " and " << (LocalMemLimit - CurrentLocalMemUsage) |
| 687 | << " available for promotion\n"); |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 692 | // FIXME: Should try to pick the most likely to be profitable allocas first. |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 693 | bool AMDGPUPromoteAlloca::handleAlloca(AllocaInst &I, bool SufficientLDS) { |
Matt Arsenault | c5fce69 | 2016-04-28 18:38:48 +0000 | [diff] [blame] | 694 | // Array allocations are probably not worth handling, since an allocation of |
| 695 | // the array type is the canonical form. |
| 696 | if (!I.isStaticAlloca() || I.isArrayAllocation()) |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 697 | return false; |
Matt Arsenault | 19c5488 | 2015-08-26 18:37:13 +0000 | [diff] [blame] | 698 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 699 | IRBuilder<> Builder(&I); |
| 700 | |
| 701 | // First try to replace the alloca with a vector |
| 702 | Type *AllocaTy = I.getAllocatedType(); |
| 703 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 704 | LLVM_DEBUG(dbgs() << "Trying to promote " << I << '\n'); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 705 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 706 | if (tryPromoteAllocaToVector(&I, AS)) |
| 707 | return true; // Promoted to vector. |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 708 | |
Tom Stellard | 79a1fd7 | 2016-04-14 16:27:07 +0000 | [diff] [blame] | 709 | const Function &ContainingFunction = *I.getParent()->getParent(); |
Matt Arsenault | 5c80618 | 2017-05-02 18:33:18 +0000 | [diff] [blame] | 710 | CallingConv::ID CC = ContainingFunction.getCallingConv(); |
Tom Stellard | 79a1fd7 | 2016-04-14 16:27:07 +0000 | [diff] [blame] | 711 | |
Nicolai Haehnle | bef1ceb | 2016-07-18 09:02:47 +0000 | [diff] [blame] | 712 | // Don't promote the alloca to LDS for shader calling conventions as the work |
| 713 | // item ID intrinsics are not supported for these calling conventions. |
| 714 | // Furthermore not all LDS is available for some of the stages. |
Matt Arsenault | 5c80618 | 2017-05-02 18:33:18 +0000 | [diff] [blame] | 715 | switch (CC) { |
| 716 | case CallingConv::AMDGPU_KERNEL: |
| 717 | case CallingConv::SPIR_KERNEL: |
| 718 | break; |
| 719 | default: |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 720 | LLVM_DEBUG( |
| 721 | dbgs() |
| 722 | << " promote alloca to LDS not supported with calling convention.\n"); |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 723 | return false; |
Matt Arsenault | 5c80618 | 2017-05-02 18:33:18 +0000 | [diff] [blame] | 724 | } |
Nicolai Haehnle | bef1ceb | 2016-07-18 09:02:47 +0000 | [diff] [blame] | 725 | |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 726 | // Not likely to have sufficient local memory for promotion. |
| 727 | if (!SufficientLDS) |
| 728 | return false; |
| 729 | |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 730 | const AMDGPUSubtarget &ST = |
| 731 | TM->getSubtarget<AMDGPUSubtarget>(ContainingFunction); |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 732 | unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second; |
Tom Stellard | 79a1fd7 | 2016-04-14 16:27:07 +0000 | [diff] [blame] | 733 | |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 734 | const DataLayout &DL = Mod->getDataLayout(); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 735 | |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 736 | unsigned Align = I.getAlignment(); |
| 737 | if (Align == 0) |
| 738 | Align = DL.getABITypeAlignment(I.getAllocatedType()); |
| 739 | |
| 740 | // FIXME: This computed padding is likely wrong since it depends on inverse |
| 741 | // usage order. |
| 742 | // |
| 743 | // FIXME: It is also possible that if we're allowed to use all of the memory |
| 744 | // could could end up using more than the maximum due to alignment padding. |
| 745 | |
| 746 | uint32_t NewSize = alignTo(CurrentLocalMemUsage, Align); |
| 747 | uint32_t AllocSize = WorkGroupSize * DL.getTypeAllocSize(AllocaTy); |
| 748 | NewSize += AllocSize; |
| 749 | |
| 750 | if (NewSize > LocalMemLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 751 | LLVM_DEBUG(dbgs() << " " << AllocSize |
| 752 | << " bytes of local memory not available to promote\n"); |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 753 | return false; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 756 | CurrentLocalMemUsage = NewSize; |
| 757 | |
Tom Stellard | 5b2927f | 2014-10-31 20:52:04 +0000 | [diff] [blame] | 758 | std::vector<Value*> WorkList; |
| 759 | |
Matt Arsenault | a61cb48 | 2016-05-12 01:58:58 +0000 | [diff] [blame] | 760 | if (!collectUsesWithPtrTypes(&I, &I, WorkList)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 761 | LLVM_DEBUG(dbgs() << " Do not know how to convert all uses\n"); |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 762 | return false; |
Tom Stellard | 5b2927f | 2014-10-31 20:52:04 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 765 | LLVM_DEBUG(dbgs() << "Promoting alloca to local memory\n"); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 766 | |
Matt Arsenault | cf84e26 | 2016-02-05 19:47:23 +0000 | [diff] [blame] | 767 | Function *F = I.getParent()->getParent(); |
| 768 | |
Tom Stellard | 79a1fd7 | 2016-04-14 16:27:07 +0000 | [diff] [blame] | 769 | Type *GVTy = ArrayType::get(I.getAllocatedType(), WorkGroupSize); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 770 | GlobalVariable *GV = new GlobalVariable( |
Matt Arsenault | cf84e26 | 2016-02-05 19:47:23 +0000 | [diff] [blame] | 771 | *Mod, GVTy, false, GlobalValue::InternalLinkage, |
| 772 | UndefValue::get(GVTy), |
| 773 | Twine(F->getName()) + Twine('.') + I.getName(), |
| 774 | nullptr, |
| 775 | GlobalVariable::NotThreadLocal, |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 776 | AS.LOCAL_ADDRESS); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 777 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Matt Arsenault | cf84e26 | 2016-02-05 19:47:23 +0000 | [diff] [blame] | 778 | GV->setAlignment(I.getAlignment()); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 779 | |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 780 | Value *TCntY, *TCntZ; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 781 | |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 782 | std::tie(TCntY, TCntZ) = getLocalSizeYZ(Builder); |
| 783 | Value *TIdX = getWorkitemID(Builder, 0); |
| 784 | Value *TIdY = getWorkitemID(Builder, 1); |
| 785 | Value *TIdZ = getWorkitemID(Builder, 2); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 786 | |
Matt Arsenault | 853a1fc | 2016-02-02 19:18:48 +0000 | [diff] [blame] | 787 | Value *Tmp0 = Builder.CreateMul(TCntY, TCntZ, "", true, true); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 788 | Tmp0 = Builder.CreateMul(Tmp0, TIdX); |
Matt Arsenault | 853a1fc | 2016-02-02 19:18:48 +0000 | [diff] [blame] | 789 | Value *Tmp1 = Builder.CreateMul(TIdY, TCntZ, "", true, true); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 790 | Value *TID = Builder.CreateAdd(Tmp0, Tmp1); |
| 791 | TID = Builder.CreateAdd(TID, TIdZ); |
| 792 | |
Matt Arsenault | 853a1fc | 2016-02-02 19:18:48 +0000 | [diff] [blame] | 793 | Value *Indices[] = { |
| 794 | Constant::getNullValue(Type::getInt32Ty(Mod->getContext())), |
| 795 | TID |
| 796 | }; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 797 | |
Matt Arsenault | 853a1fc | 2016-02-02 19:18:48 +0000 | [diff] [blame] | 798 | Value *Offset = Builder.CreateInBoundsGEP(GVTy, GV, Indices); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 799 | I.mutateType(Offset->getType()); |
| 800 | I.replaceAllUsesWith(Offset); |
| 801 | I.eraseFromParent(); |
| 802 | |
Matt Arsenault | fb8cdba | 2016-02-02 19:32:35 +0000 | [diff] [blame] | 803 | for (Value *V : WorkList) { |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 804 | CallInst *Call = dyn_cast<CallInst>(V); |
| 805 | if (!Call) { |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 806 | if (ICmpInst *CI = dyn_cast<ICmpInst>(V)) { |
| 807 | Value *Src0 = CI->getOperand(0); |
| 808 | Type *EltTy = Src0->getType()->getPointerElementType(); |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 809 | PointerType *NewTy = PointerType::get(EltTy, AS.LOCAL_ADDRESS); |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 810 | |
| 811 | if (isa<ConstantPointerNull>(CI->getOperand(0))) |
| 812 | CI->setOperand(0, ConstantPointerNull::get(NewTy)); |
| 813 | |
| 814 | if (isa<ConstantPointerNull>(CI->getOperand(1))) |
| 815 | CI->setOperand(1, ConstantPointerNull::get(NewTy)); |
| 816 | |
| 817 | continue; |
| 818 | } |
Matt Arsenault | 65f67e4 | 2014-09-15 15:41:44 +0000 | [diff] [blame] | 819 | |
Matt Arsenault | 2402b95 | 2016-12-10 00:52:50 +0000 | [diff] [blame] | 820 | // The operand's value should be corrected on its own and we don't want to |
| 821 | // touch the users. |
Matt Arsenault | 65f67e4 | 2014-09-15 15:41:44 +0000 | [diff] [blame] | 822 | if (isa<AddrSpaceCastInst>(V)) |
| 823 | continue; |
| 824 | |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 825 | Type *EltTy = V->getType()->getPointerElementType(); |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 826 | PointerType *NewTy = PointerType::get(EltTy, AS.LOCAL_ADDRESS); |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 827 | |
Matt Arsenault | 65f67e4 | 2014-09-15 15:41:44 +0000 | [diff] [blame] | 828 | // FIXME: It doesn't really make sense to try to do this for all |
| 829 | // instructions. |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 830 | V->mutateType(NewTy); |
Matt Arsenault | 891fccc | 2016-05-18 15:57:21 +0000 | [diff] [blame] | 831 | |
| 832 | // Adjust the types of any constant operands. |
| 833 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 834 | if (isa<ConstantPointerNull>(SI->getOperand(1))) |
| 835 | SI->setOperand(1, ConstantPointerNull::get(NewTy)); |
| 836 | |
| 837 | if (isa<ConstantPointerNull>(SI->getOperand(2))) |
| 838 | SI->setOperand(2, ConstantPointerNull::get(NewTy)); |
| 839 | } else if (PHINode *Phi = dyn_cast<PHINode>(V)) { |
| 840 | for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) { |
| 841 | if (isa<ConstantPointerNull>(Phi->getIncomingValue(I))) |
| 842 | Phi->setIncomingValue(I, ConstantPointerNull::get(NewTy)); |
| 843 | } |
| 844 | } |
| 845 | |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 846 | continue; |
| 847 | } |
| 848 | |
Matt Arsenault | 2e08e18 | 2016-07-18 18:34:48 +0000 | [diff] [blame] | 849 | IntrinsicInst *Intr = cast<IntrinsicInst>(Call); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 850 | Builder.SetInsertPoint(Intr); |
| 851 | switch (Intr->getIntrinsicID()) { |
| 852 | case Intrinsic::lifetime_start: |
| 853 | case Intrinsic::lifetime_end: |
| 854 | // These intrinsics are for address space 0 only |
| 855 | Intr->eraseFromParent(); |
| 856 | continue; |
| 857 | case Intrinsic::memcpy: { |
| 858 | MemCpyInst *MemCpy = cast<MemCpyInst>(Intr); |
Daniel Neilson | a60f462 | 2018-02-09 21:56:15 +0000 | [diff] [blame] | 859 | Builder.CreateMemCpy(MemCpy->getRawDest(), MemCpy->getDestAlignment(), |
| 860 | MemCpy->getRawSource(), MemCpy->getSourceAlignment(), |
| 861 | MemCpy->getLength(), MemCpy->isVolatile()); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 862 | Intr->eraseFromParent(); |
| 863 | continue; |
| 864 | } |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 865 | case Intrinsic::memmove: { |
| 866 | MemMoveInst *MemMove = cast<MemMoveInst>(Intr); |
Daniel Neilson | a60f462 | 2018-02-09 21:56:15 +0000 | [diff] [blame] | 867 | Builder.CreateMemMove(MemMove->getRawDest(), MemMove->getDestAlignment(), |
| 868 | MemMove->getRawSource(), MemMove->getSourceAlignment(), |
| 869 | MemMove->getLength(), MemMove->isVolatile()); |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 870 | Intr->eraseFromParent(); |
| 871 | continue; |
| 872 | } |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 873 | case Intrinsic::memset: { |
| 874 | MemSetInst *MemSet = cast<MemSetInst>(Intr); |
| 875 | Builder.CreateMemSet(MemSet->getRawDest(), MemSet->getValue(), |
Daniel Neilson | a60f462 | 2018-02-09 21:56:15 +0000 | [diff] [blame] | 876 | MemSet->getLength(), MemSet->getDestAlignment(), |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 877 | MemSet->isVolatile()); |
| 878 | Intr->eraseFromParent(); |
| 879 | continue; |
| 880 | } |
Matt Arsenault | 0b783ef0 | 2016-01-22 19:47:54 +0000 | [diff] [blame] | 881 | case Intrinsic::invariant_start: |
| 882 | case Intrinsic::invariant_end: |
Piotr Padlewski | 5dde809 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 883 | case Intrinsic::launder_invariant_group: |
Matt Arsenault | 0b783ef0 | 2016-01-22 19:47:54 +0000 | [diff] [blame] | 884 | Intr->eraseFromParent(); |
| 885 | // FIXME: I think the invariant marker should still theoretically apply, |
| 886 | // but the intrinsics need to be changed to accept pointers with any |
| 887 | // address space. |
| 888 | continue; |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 889 | case Intrinsic::objectsize: { |
| 890 | Value *Src = Intr->getOperand(0); |
| 891 | Type *SrcTy = Src->getType()->getPointerElementType(); |
| 892 | Function *ObjectSize = Intrinsic::getDeclaration(Mod, |
| 893 | Intrinsic::objectsize, |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 894 | { Intr->getType(), PointerType::get(SrcTy, AS.LOCAL_ADDRESS) } |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 895 | ); |
| 896 | |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 897 | CallInst *NewCall = Builder.CreateCall( |
| 898 | ObjectSize, {Src, Intr->getOperand(1), Intr->getOperand(2)}); |
Matt Arsenault | 7e747f1 | 2016-02-02 20:28:10 +0000 | [diff] [blame] | 899 | Intr->replaceAllUsesWith(NewCall); |
| 900 | Intr->eraseFromParent(); |
| 901 | continue; |
| 902 | } |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 903 | default: |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 904 | Intr->print(errs()); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 905 | llvm_unreachable("Don't know how to promote alloca intrinsic use."); |
| 906 | } |
| 907 | } |
Changpeng Fang | 1dbace1 | 2017-05-23 20:25:41 +0000 | [diff] [blame] | 908 | return true; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 911 | FunctionPass *llvm::createAMDGPUPromoteAlloca() { |
| 912 | return new AMDGPUPromoteAlloca(); |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 913 | } |