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