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