1 //===-- AMDGPUPromoteAlloca.cpp - Promote Allocas -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass eliminates allocas by either converting them into vectors or
10 // by migrating them to local address space.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDGPU.h"
15 #include "GCNSubtarget.h"
16 #include "llvm/Analysis/CaptureTracking.h"
17 #include "llvm/Analysis/ValueTracking.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/IntrinsicsAMDGPU.h"
21 #include "llvm/IR/IntrinsicsR600.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "Utils/AMDGPUBaseInfo.h"
25 
26 #define DEBUG_TYPE "amdgpu-promote-alloca"
27 
28 using namespace llvm;
29 
30 namespace {
31 
32 static cl::opt<bool> DisablePromoteAllocaToVector(
33   "disable-promote-alloca-to-vector",
34   cl::desc("Disable promote alloca to vector"),
35   cl::init(false));
36 
37 static cl::opt<bool> DisablePromoteAllocaToLDS(
38   "disable-promote-alloca-to-lds",
39   cl::desc("Disable promote alloca to LDS"),
40   cl::init(false));
41 
42 static cl::opt<unsigned> PromoteAllocaToVectorLimit(
43   "amdgpu-promote-alloca-to-vector-limit",
44   cl::desc("Maximum byte size to consider promote alloca to vector"),
45   cl::init(0));
46 
47 // FIXME: This can create globals so should be a module pass.
48 class AMDGPUPromoteAlloca : public FunctionPass {
49 public:
50   static char ID;
51 
52   AMDGPUPromoteAlloca() : FunctionPass(ID) {}
53 
54   bool runOnFunction(Function &F) override;
55 
56   StringRef getPassName() const override { return "AMDGPU Promote Alloca"; }
57 
58   bool handleAlloca(AllocaInst &I, bool SufficientLDS);
59 
60   void getAnalysisUsage(AnalysisUsage &AU) const override {
61     AU.setPreservesCFG();
62     FunctionPass::getAnalysisUsage(AU);
63   }
64 };
65 
66 class AMDGPUPromoteAllocaImpl {
67 private:
68   const TargetMachine &TM;
69   Module *Mod = nullptr;
70   const DataLayout *DL = nullptr;
71 
72   // FIXME: This should be per-kernel.
73   uint32_t LocalMemLimit = 0;
74   uint32_t CurrentLocalMemUsage = 0;
75   unsigned MaxVGPRs;
76 
77   bool IsAMDGCN = false;
78   bool IsAMDHSA = false;
79 
80   std::pair<Value *, Value *> getLocalSizeYZ(IRBuilder<> &Builder);
81   Value *getWorkitemID(IRBuilder<> &Builder, unsigned N);
82 
83   /// BaseAlloca is the alloca root the search started from.
84   /// Val may be that alloca or a recursive user of it.
85   bool collectUsesWithPtrTypes(Value *BaseAlloca,
86                                Value *Val,
87                                std::vector<Value*> &WorkList) const;
88 
89   /// Val is a derived pointer from Alloca. OpIdx0/OpIdx1 are the operand
90   /// indices to an instruction with 2 pointer inputs (e.g. select, icmp).
91   /// Returns true if both operands are derived from the same alloca. Val should
92   /// be the same value as one of the input operands of UseInst.
93   bool binaryOpIsDerivedFromSameAlloca(Value *Alloca, Value *Val,
94                                        Instruction *UseInst,
95                                        int OpIdx0, int OpIdx1) const;
96 
97   /// Check whether we have enough local memory for promotion.
98   bool hasSufficientLocalMem(const Function &F);
99 
100   bool handleAlloca(AllocaInst &I, bool SufficientLDS);
101 
102 public:
103   AMDGPUPromoteAllocaImpl(TargetMachine &TM) : TM(TM) {}
104   bool run(Function &F);
105 };
106 
107 class AMDGPUPromoteAllocaToVector : public FunctionPass {
108 public:
109   static char ID;
110 
111   AMDGPUPromoteAllocaToVector() : FunctionPass(ID) {}
112 
113   bool runOnFunction(Function &F) override;
114 
115   StringRef getPassName() const override {
116     return "AMDGPU Promote Alloca to vector";
117   }
118 
119   void getAnalysisUsage(AnalysisUsage &AU) const override {
120     AU.setPreservesCFG();
121     FunctionPass::getAnalysisUsage(AU);
122   }
123 };
124 
125 } // end anonymous namespace
126 
127 char AMDGPUPromoteAlloca::ID = 0;
128 char AMDGPUPromoteAllocaToVector::ID = 0;
129 
130 INITIALIZE_PASS_BEGIN(AMDGPUPromoteAlloca, DEBUG_TYPE,
131                       "AMDGPU promote alloca to vector or LDS", false, false)
132 // Move LDS uses from functions to kernels before promote alloca for accurate
133 // estimation of LDS available
134 INITIALIZE_PASS_DEPENDENCY(AMDGPULowerModuleLDS)
135 INITIALIZE_PASS_END(AMDGPUPromoteAlloca, DEBUG_TYPE,
136                     "AMDGPU promote alloca to vector or LDS", false, false)
137 
138 INITIALIZE_PASS(AMDGPUPromoteAllocaToVector, DEBUG_TYPE "-to-vector",
139                 "AMDGPU promote alloca to vector", false, false)
140 
141 char &llvm::AMDGPUPromoteAllocaID = AMDGPUPromoteAlloca::ID;
142 char &llvm::AMDGPUPromoteAllocaToVectorID = AMDGPUPromoteAllocaToVector::ID;
143 
144 bool AMDGPUPromoteAlloca::runOnFunction(Function &F) {
145   if (skipFunction(F))
146     return false;
147 
148   if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) {
149     return AMDGPUPromoteAllocaImpl(TPC->getTM<TargetMachine>()).run(F);
150   }
151   return false;
152 }
153 
154 PreservedAnalyses AMDGPUPromoteAllocaPass::run(Function &F,
155                                                FunctionAnalysisManager &AM) {
156   bool Changed = AMDGPUPromoteAllocaImpl(TM).run(F);
157   if (Changed) {
158     PreservedAnalyses PA;
159     PA.preserveSet<CFGAnalyses>();
160     return PA;
161   }
162   return PreservedAnalyses::all();
163 }
164 
165 bool AMDGPUPromoteAllocaImpl::run(Function &F) {
166   Mod = F.getParent();
167   DL = &Mod->getDataLayout();
168 
169   const Triple &TT = TM.getTargetTriple();
170   IsAMDGCN = TT.getArch() == Triple::amdgcn;
171   IsAMDHSA = TT.getOS() == Triple::AMDHSA;
172 
173   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, F);
174   if (!ST.isPromoteAllocaEnabled())
175     return false;
176 
177   if (IsAMDGCN) {
178     const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
179     MaxVGPRs = ST.getMaxNumVGPRs(ST.getWavesPerEU(F).first);
180     // A non-entry function has only 32 caller preserved registers.
181     // Do not promote alloca which will force spilling.
182     if (!AMDGPU::isEntryFunctionCC(F.getCallingConv()))
183       MaxVGPRs = std::min(MaxVGPRs, 32u);
184   } else {
185     MaxVGPRs = 128;
186   }
187 
188   bool SufficientLDS = hasSufficientLocalMem(F);
189   bool Changed = false;
190   BasicBlock &EntryBB = *F.begin();
191 
192   SmallVector<AllocaInst *, 16> Allocas;
193   for (Instruction &I : EntryBB) {
194     if (AllocaInst *AI = dyn_cast<AllocaInst>(&I))
195       Allocas.push_back(AI);
196   }
197 
198   for (AllocaInst *AI : Allocas) {
199     if (handleAlloca(*AI, SufficientLDS))
200       Changed = true;
201   }
202 
203   return Changed;
204 }
205 
206 std::pair<Value *, Value *>
207 AMDGPUPromoteAllocaImpl::getLocalSizeYZ(IRBuilder<> &Builder) {
208   Function &F = *Builder.GetInsertBlock()->getParent();
209   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, F);
210 
211   if (!IsAMDHSA) {
212     Function *LocalSizeYFn
213       = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_y);
214     Function *LocalSizeZFn
215       = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_z);
216 
217     CallInst *LocalSizeY = Builder.CreateCall(LocalSizeYFn, {});
218     CallInst *LocalSizeZ = Builder.CreateCall(LocalSizeZFn, {});
219 
220     ST.makeLIDRangeMetadata(LocalSizeY);
221     ST.makeLIDRangeMetadata(LocalSizeZ);
222 
223     return std::make_pair(LocalSizeY, LocalSizeZ);
224   }
225 
226   // We must read the size out of the dispatch pointer.
227   assert(IsAMDGCN);
228 
229   // We are indexing into this struct, and want to extract the workgroup_size_*
230   // fields.
231   //
232   //   typedef struct hsa_kernel_dispatch_packet_s {
233   //     uint16_t header;
234   //     uint16_t setup;
235   //     uint16_t workgroup_size_x ;
236   //     uint16_t workgroup_size_y;
237   //     uint16_t workgroup_size_z;
238   //     uint16_t reserved0;
239   //     uint32_t grid_size_x ;
240   //     uint32_t grid_size_y ;
241   //     uint32_t grid_size_z;
242   //
243   //     uint32_t private_segment_size;
244   //     uint32_t group_segment_size;
245   //     uint64_t kernel_object;
246   //
247   // #ifdef HSA_LARGE_MODEL
248   //     void *kernarg_address;
249   // #elif defined HSA_LITTLE_ENDIAN
250   //     void *kernarg_address;
251   //     uint32_t reserved1;
252   // #else
253   //     uint32_t reserved1;
254   //     void *kernarg_address;
255   // #endif
256   //     uint64_t reserved2;
257   //     hsa_signal_t completion_signal; // uint64_t wrapper
258   //   } hsa_kernel_dispatch_packet_t
259   //
260   Function *DispatchPtrFn
261     = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_dispatch_ptr);
262 
263   CallInst *DispatchPtr = Builder.CreateCall(DispatchPtrFn, {});
264   DispatchPtr->addRetAttr(Attribute::NoAlias);
265   DispatchPtr->addRetAttr(Attribute::NonNull);
266   F.removeFnAttr("amdgpu-no-dispatch-ptr");
267 
268   // Size of the dispatch packet struct.
269   DispatchPtr->addDereferenceableRetAttr(64);
270 
271   Type *I32Ty = Type::getInt32Ty(Mod->getContext());
272   Value *CastDispatchPtr = Builder.CreateBitCast(
273     DispatchPtr, PointerType::get(I32Ty, AMDGPUAS::CONSTANT_ADDRESS));
274 
275   // We could do a single 64-bit load here, but it's likely that the basic
276   // 32-bit and extract sequence is already present, and it is probably easier
277   // to CSE this. The loads should be mergeable later anyway.
278   Value *GEPXY = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 1);
279   LoadInst *LoadXY = Builder.CreateAlignedLoad(I32Ty, GEPXY, Align(4));
280 
281   Value *GEPZU = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 2);
282   LoadInst *LoadZU = Builder.CreateAlignedLoad(I32Ty, GEPZU, Align(4));
283 
284   MDNode *MD = MDNode::get(Mod->getContext(), None);
285   LoadXY->setMetadata(LLVMContext::MD_invariant_load, MD);
286   LoadZU->setMetadata(LLVMContext::MD_invariant_load, MD);
287   ST.makeLIDRangeMetadata(LoadZU);
288 
289   // Extract y component. Upper half of LoadZU should be zero already.
290   Value *Y = Builder.CreateLShr(LoadXY, 16);
291 
292   return std::make_pair(Y, LoadZU);
293 }
294 
295 Value *AMDGPUPromoteAllocaImpl::getWorkitemID(IRBuilder<> &Builder,
296                                               unsigned N) {
297   Function *F = Builder.GetInsertBlock()->getParent();
298   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, *F);
299   Intrinsic::ID IntrID = Intrinsic::not_intrinsic;
300   StringRef AttrName;
301 
302   switch (N) {
303   case 0:
304     IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_x
305                       : (Intrinsic::ID)Intrinsic::r600_read_tidig_x;
306     AttrName = "amdgpu-no-workitem-id-x";
307     break;
308   case 1:
309     IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_y
310                       : (Intrinsic::ID)Intrinsic::r600_read_tidig_y;
311     AttrName = "amdgpu-no-workitem-id-y";
312     break;
313 
314   case 2:
315     IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_z
316                       : (Intrinsic::ID)Intrinsic::r600_read_tidig_z;
317     AttrName = "amdgpu-no-workitem-id-z";
318     break;
319   default:
320     llvm_unreachable("invalid dimension");
321   }
322 
323   Function *WorkitemIdFn = Intrinsic::getDeclaration(Mod, IntrID);
324   CallInst *CI = Builder.CreateCall(WorkitemIdFn);
325   ST.makeLIDRangeMetadata(CI);
326   F->removeFnAttr(AttrName);
327 
328   return CI;
329 }
330 
331 static FixedVectorType *arrayTypeToVecType(ArrayType *ArrayTy) {
332   return FixedVectorType::get(ArrayTy->getElementType(),
333                               ArrayTy->getNumElements());
334 }
335 
336 static Value *stripBitcasts(Value *V) {
337   while (Instruction *I = dyn_cast<Instruction>(V)) {
338     if (I->getOpcode() != Instruction::BitCast)
339       break;
340     V = I->getOperand(0);
341   }
342   return V;
343 }
344 
345 static Value *
346 calculateVectorIndex(Value *Ptr,
347                      const std::map<GetElementPtrInst *, Value *> &GEPIdx) {
348   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(stripBitcasts(Ptr));
349   if (!GEP)
350     return nullptr;
351 
352   auto I = GEPIdx.find(GEP);
353   return I == GEPIdx.end() ? nullptr : I->second;
354 }
355 
356 static Value* GEPToVectorIndex(GetElementPtrInst *GEP) {
357   // FIXME we only support simple cases
358   if (GEP->getNumOperands() != 3)
359     return nullptr;
360 
361   ConstantInt *I0 = dyn_cast<ConstantInt>(GEP->getOperand(1));
362   if (!I0 || !I0->isZero())
363     return nullptr;
364 
365   return GEP->getOperand(2);
366 }
367 
368 // Not an instruction handled below to turn into a vector.
369 //
370 // TODO: Check isTriviallyVectorizable for calls and handle other
371 // instructions.
372 static bool canVectorizeInst(Instruction *Inst, User *User,
373                              const DataLayout &DL) {
374   switch (Inst->getOpcode()) {
375   case Instruction::Load: {
376     // Currently only handle the case where the Pointer Operand is a GEP.
377     // Also we could not vectorize volatile or atomic loads.
378     LoadInst *LI = cast<LoadInst>(Inst);
379     if (isa<AllocaInst>(User) &&
380         LI->getPointerOperandType() == User->getType() &&
381         isa<VectorType>(LI->getType()))
382       return true;
383 
384     Instruction *PtrInst = dyn_cast<Instruction>(LI->getPointerOperand());
385     if (!PtrInst)
386       return false;
387 
388     return (PtrInst->getOpcode() == Instruction::GetElementPtr ||
389             PtrInst->getOpcode() == Instruction::BitCast) &&
390            LI->isSimple();
391   }
392   case Instruction::BitCast:
393     return true;
394   case Instruction::Store: {
395     // Must be the stored pointer operand, not a stored value, plus
396     // since it should be canonical form, the User should be a GEP.
397     // Also we could not vectorize volatile or atomic stores.
398     StoreInst *SI = cast<StoreInst>(Inst);
399     if (isa<AllocaInst>(User) &&
400         SI->getPointerOperandType() == User->getType() &&
401         isa<VectorType>(SI->getValueOperand()->getType()))
402       return true;
403 
404     Instruction *UserInst = dyn_cast<Instruction>(User);
405     if (!UserInst)
406       return false;
407 
408     return (SI->getPointerOperand() == User) &&
409            (UserInst->getOpcode() == Instruction::GetElementPtr ||
410             UserInst->getOpcode() == Instruction::BitCast) &&
411            SI->isSimple();
412   }
413   default:
414     return false;
415   }
416 }
417 
418 static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL,
419                                      unsigned MaxVGPRs) {
420 
421   if (DisablePromoteAllocaToVector) {
422     LLVM_DEBUG(dbgs() << "  Promotion alloca to vector is disabled\n");
423     return false;
424   }
425 
426   Type *AllocaTy = Alloca->getAllocatedType();
427   auto *VectorTy = dyn_cast<FixedVectorType>(AllocaTy);
428   if (auto *ArrayTy = dyn_cast<ArrayType>(AllocaTy)) {
429     if (VectorType::isValidElementType(ArrayTy->getElementType()) &&
430         ArrayTy->getNumElements() > 0)
431       VectorTy = arrayTypeToVecType(ArrayTy);
432   }
433 
434   // Use up to 1/4 of available register budget for vectorization.
435   unsigned Limit = PromoteAllocaToVectorLimit ? PromoteAllocaToVectorLimit * 8
436                                               : (MaxVGPRs * 32);
437 
438   if (DL.getTypeSizeInBits(AllocaTy) * 4 > Limit) {
439     LLVM_DEBUG(dbgs() << "  Alloca too big for vectorization with "
440                       << MaxVGPRs << " registers available\n");
441     return false;
442   }
443 
444   LLVM_DEBUG(dbgs() << "Alloca candidate for vectorization\n");
445 
446   // FIXME: There is no reason why we can't support larger arrays, we
447   // are just being conservative for now.
448   // FIXME: We also reject alloca's of the form [ 2 x [ 2 x i32 ]] or equivalent. Potentially these
449   // could also be promoted but we don't currently handle this case
450   if (!VectorTy || VectorTy->getNumElements() > 16 ||
451       VectorTy->getNumElements() < 2) {
452     LLVM_DEBUG(dbgs() << "  Cannot convert type to vector\n");
453     return false;
454   }
455 
456   std::map<GetElementPtrInst*, Value*> GEPVectorIdx;
457   std::vector<Value *> WorkList;
458   SmallVector<User *, 8> Users(Alloca->users());
459   SmallVector<User *, 8> UseUsers(Users.size(), Alloca);
460   Type *VecEltTy = VectorTy->getElementType();
461   while (!Users.empty()) {
462     User *AllocaUser = Users.pop_back_val();
463     User *UseUser = UseUsers.pop_back_val();
464     Instruction *Inst = dyn_cast<Instruction>(AllocaUser);
465 
466     GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(AllocaUser);
467     if (!GEP) {
468       if (!canVectorizeInst(Inst, UseUser, DL))
469         return false;
470 
471       if (Inst->getOpcode() == Instruction::BitCast) {
472         Type *FromTy = Inst->getOperand(0)->getType()->getPointerElementType();
473         Type *ToTy = Inst->getType()->getPointerElementType();
474         if (FromTy->isAggregateType() || ToTy->isAggregateType() ||
475             DL.getTypeSizeInBits(FromTy) != DL.getTypeSizeInBits(ToTy))
476           continue;
477 
478         for (User *CastUser : Inst->users()) {
479           if (isAssumeLikeIntrinsic(cast<Instruction>(CastUser)))
480             continue;
481           Users.push_back(CastUser);
482           UseUsers.push_back(Inst);
483         }
484 
485         continue;
486       }
487 
488       WorkList.push_back(AllocaUser);
489       continue;
490     }
491 
492     Value *Index = GEPToVectorIndex(GEP);
493 
494     // If we can't compute a vector index from this GEP, then we can't
495     // promote this alloca to vector.
496     if (!Index) {
497       LLVM_DEBUG(dbgs() << "  Cannot compute vector index for GEP " << *GEP
498                         << '\n');
499       return false;
500     }
501 
502     GEPVectorIdx[GEP] = Index;
503     Users.append(GEP->user_begin(), GEP->user_end());
504     UseUsers.append(GEP->getNumUses(), GEP);
505   }
506 
507   LLVM_DEBUG(dbgs() << "  Converting alloca to vector " << *AllocaTy << " -> "
508                     << *VectorTy << '\n');
509 
510   for (Value *V : WorkList) {
511     Instruction *Inst = cast<Instruction>(V);
512     IRBuilder<> Builder(Inst);
513     switch (Inst->getOpcode()) {
514     case Instruction::Load: {
515       if (Inst->getType() == AllocaTy || Inst->getType()->isVectorTy())
516         break;
517 
518       Value *Ptr = cast<LoadInst>(Inst)->getPointerOperand();
519       Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx);
520       if (!Index)
521         break;
522 
523       Type *VecPtrTy = VectorTy->getPointerTo(AMDGPUAS::PRIVATE_ADDRESS);
524       Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy);
525       Value *VecValue = Builder.CreateLoad(VectorTy, BitCast);
526       Value *ExtractElement = Builder.CreateExtractElement(VecValue, Index);
527       if (Inst->getType() != VecEltTy)
528         ExtractElement = Builder.CreateBitOrPointerCast(ExtractElement, Inst->getType());
529       Inst->replaceAllUsesWith(ExtractElement);
530       Inst->eraseFromParent();
531       break;
532     }
533     case Instruction::Store: {
534       StoreInst *SI = cast<StoreInst>(Inst);
535       if (SI->getValueOperand()->getType() == AllocaTy ||
536           SI->getValueOperand()->getType()->isVectorTy())
537         break;
538 
539       Value *Ptr = SI->getPointerOperand();
540       Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx);
541       if (!Index)
542         break;
543 
544       Type *VecPtrTy = VectorTy->getPointerTo(AMDGPUAS::PRIVATE_ADDRESS);
545       Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy);
546       Value *VecValue = Builder.CreateLoad(VectorTy, BitCast);
547       Value *Elt = SI->getValueOperand();
548       if (Elt->getType() != VecEltTy)
549         Elt = Builder.CreateBitOrPointerCast(Elt, VecEltTy);
550       Value *NewVecValue = Builder.CreateInsertElement(VecValue, Elt, Index);
551       Builder.CreateStore(NewVecValue, BitCast);
552       Inst->eraseFromParent();
553       break;
554     }
555 
556     default:
557       llvm_unreachable("Inconsistency in instructions promotable to vector");
558     }
559   }
560   return true;
561 }
562 
563 static bool isCallPromotable(CallInst *CI) {
564   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
565   if (!II)
566     return false;
567 
568   switch (II->getIntrinsicID()) {
569   case Intrinsic::memcpy:
570   case Intrinsic::memmove:
571   case Intrinsic::memset:
572   case Intrinsic::lifetime_start:
573   case Intrinsic::lifetime_end:
574   case Intrinsic::invariant_start:
575   case Intrinsic::invariant_end:
576   case Intrinsic::launder_invariant_group:
577   case Intrinsic::strip_invariant_group:
578   case Intrinsic::objectsize:
579     return true;
580   default:
581     return false;
582   }
583 }
584 
585 bool AMDGPUPromoteAllocaImpl::binaryOpIsDerivedFromSameAlloca(
586     Value *BaseAlloca, Value *Val, Instruction *Inst, int OpIdx0,
587     int OpIdx1) const {
588   // Figure out which operand is the one we might not be promoting.
589   Value *OtherOp = Inst->getOperand(OpIdx0);
590   if (Val == OtherOp)
591     OtherOp = Inst->getOperand(OpIdx1);
592 
593   if (isa<ConstantPointerNull>(OtherOp))
594     return true;
595 
596   Value *OtherObj = getUnderlyingObject(OtherOp);
597   if (!isa<AllocaInst>(OtherObj))
598     return false;
599 
600   // TODO: We should be able to replace undefs with the right pointer type.
601 
602   // TODO: If we know the other base object is another promotable
603   // alloca, not necessarily this alloca, we can do this. The
604   // important part is both must have the same address space at
605   // the end.
606   if (OtherObj != BaseAlloca) {
607     LLVM_DEBUG(
608         dbgs() << "Found a binary instruction with another alloca object\n");
609     return false;
610   }
611 
612   return true;
613 }
614 
615 bool AMDGPUPromoteAllocaImpl::collectUsesWithPtrTypes(
616     Value *BaseAlloca, Value *Val, std::vector<Value *> &WorkList) const {
617 
618   for (User *User : Val->users()) {
619     if (is_contained(WorkList, User))
620       continue;
621 
622     if (CallInst *CI = dyn_cast<CallInst>(User)) {
623       if (!isCallPromotable(CI))
624         return false;
625 
626       WorkList.push_back(User);
627       continue;
628     }
629 
630     Instruction *UseInst = cast<Instruction>(User);
631     if (UseInst->getOpcode() == Instruction::PtrToInt)
632       return false;
633 
634     if (LoadInst *LI = dyn_cast<LoadInst>(UseInst)) {
635       if (LI->isVolatile())
636         return false;
637 
638       continue;
639     }
640 
641     if (StoreInst *SI = dyn_cast<StoreInst>(UseInst)) {
642       if (SI->isVolatile())
643         return false;
644 
645       // Reject if the stored value is not the pointer operand.
646       if (SI->getPointerOperand() != Val)
647         return false;
648     } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UseInst)) {
649       if (RMW->isVolatile())
650         return false;
651     } else if (AtomicCmpXchgInst *CAS = dyn_cast<AtomicCmpXchgInst>(UseInst)) {
652       if (CAS->isVolatile())
653         return false;
654     }
655 
656     // Only promote a select if we know that the other select operand
657     // is from another pointer that will also be promoted.
658     if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
659       if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, ICmp, 0, 1))
660         return false;
661 
662       // May need to rewrite constant operands.
663       WorkList.push_back(ICmp);
664     }
665 
666     if (UseInst->getOpcode() == Instruction::AddrSpaceCast) {
667       // Give up if the pointer may be captured.
668       if (PointerMayBeCaptured(UseInst, true, true))
669         return false;
670       // Don't collect the users of this.
671       WorkList.push_back(User);
672       continue;
673     }
674 
675     // Do not promote vector/aggregate type instructions. It is hard to track
676     // their users.
677     if (isa<InsertValueInst>(User) || isa<InsertElementInst>(User))
678       return false;
679 
680     if (!User->getType()->isPointerTy())
681       continue;
682 
683     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UseInst)) {
684       // Be conservative if an address could be computed outside the bounds of
685       // the alloca.
686       if (!GEP->isInBounds())
687         return false;
688     }
689 
690     // Only promote a select if we know that the other select operand is from
691     // another pointer that will also be promoted.
692     if (SelectInst *SI = dyn_cast<SelectInst>(UseInst)) {
693       if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, SI, 1, 2))
694         return false;
695     }
696 
697     // Repeat for phis.
698     if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
699       // TODO: Handle more complex cases. We should be able to replace loops
700       // over arrays.
701       switch (Phi->getNumIncomingValues()) {
702       case 1:
703         break;
704       case 2:
705         if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, Phi, 0, 1))
706           return false;
707         break;
708       default:
709         return false;
710       }
711     }
712 
713     WorkList.push_back(User);
714     if (!collectUsesWithPtrTypes(BaseAlloca, User, WorkList))
715       return false;
716   }
717 
718   return true;
719 }
720 
721 bool AMDGPUPromoteAllocaImpl::hasSufficientLocalMem(const Function &F) {
722 
723   FunctionType *FTy = F.getFunctionType();
724   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, F);
725 
726   // If the function has any arguments in the local address space, then it's
727   // possible these arguments require the entire local memory space, so
728   // we cannot use local memory in the pass.
729   for (Type *ParamTy : FTy->params()) {
730     PointerType *PtrTy = dyn_cast<PointerType>(ParamTy);
731     if (PtrTy && PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
732       LocalMemLimit = 0;
733       LLVM_DEBUG(dbgs() << "Function has local memory argument. Promoting to "
734                            "local memory disabled.\n");
735       return false;
736     }
737   }
738 
739   LocalMemLimit = ST.getLocalMemorySize();
740   if (LocalMemLimit == 0)
741     return false;
742 
743   SmallVector<const Constant *, 16> Stack;
744   SmallPtrSet<const Constant *, 8> VisitedConstants;
745   SmallPtrSet<const GlobalVariable *, 8> UsedLDS;
746 
747   auto visitUsers = [&](const GlobalVariable *GV, const Constant *Val) -> bool {
748     for (const User *U : Val->users()) {
749       if (const Instruction *Use = dyn_cast<Instruction>(U)) {
750         if (Use->getParent()->getParent() == &F)
751           return true;
752       } else {
753         const Constant *C = cast<Constant>(U);
754         if (VisitedConstants.insert(C).second)
755           Stack.push_back(C);
756       }
757     }
758 
759     return false;
760   };
761 
762   for (GlobalVariable &GV : Mod->globals()) {
763     if (GV.getAddressSpace() != AMDGPUAS::LOCAL_ADDRESS)
764       continue;
765 
766     if (visitUsers(&GV, &GV)) {
767       UsedLDS.insert(&GV);
768       Stack.clear();
769       continue;
770     }
771 
772     // For any ConstantExpr uses, we need to recursively search the users until
773     // we see a function.
774     while (!Stack.empty()) {
775       const Constant *C = Stack.pop_back_val();
776       if (visitUsers(&GV, C)) {
777         UsedLDS.insert(&GV);
778         Stack.clear();
779         break;
780       }
781     }
782   }
783 
784   const DataLayout &DL = Mod->getDataLayout();
785   SmallVector<std::pair<uint64_t, Align>, 16> AllocatedSizes;
786   AllocatedSizes.reserve(UsedLDS.size());
787 
788   for (const GlobalVariable *GV : UsedLDS) {
789     Align Alignment =
790         DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
791     uint64_t AllocSize = DL.getTypeAllocSize(GV->getValueType());
792 
793     // HIP uses an extern unsized array in local address space for dynamically
794     // allocated shared memory.  In that case, we have to disable the promotion.
795     if (GV->hasExternalLinkage() && AllocSize == 0) {
796       LocalMemLimit = 0;
797       LLVM_DEBUG(dbgs() << "Function has a reference to externally allocated "
798                            "local memory. Promoting to local memory "
799                            "disabled.\n");
800       return false;
801     }
802 
803     AllocatedSizes.emplace_back(AllocSize, Alignment);
804   }
805 
806   // Sort to try to estimate the worst case alignment padding
807   //
808   // FIXME: We should really do something to fix the addresses to a more optimal
809   // value instead
810   llvm::sort(AllocatedSizes, [](std::pair<uint64_t, Align> LHS,
811                                 std::pair<uint64_t, Align> RHS) {
812     return LHS.second < RHS.second;
813   });
814 
815   // Check how much local memory is being used by global objects
816   CurrentLocalMemUsage = 0;
817 
818   // FIXME: Try to account for padding here. The real padding and address is
819   // currently determined from the inverse order of uses in the function when
820   // legalizing, which could also potentially change. We try to estimate the
821   // worst case here, but we probably should fix the addresses earlier.
822   for (auto Alloc : AllocatedSizes) {
823     CurrentLocalMemUsage = alignTo(CurrentLocalMemUsage, Alloc.second);
824     CurrentLocalMemUsage += Alloc.first;
825   }
826 
827   unsigned MaxOccupancy = ST.getOccupancyWithLocalMemSize(CurrentLocalMemUsage,
828                                                           F);
829 
830   // Restrict local memory usage so that we don't drastically reduce occupancy,
831   // unless it is already significantly reduced.
832 
833   // TODO: Have some sort of hint or other heuristics to guess occupancy based
834   // on other factors..
835   unsigned OccupancyHint = ST.getWavesPerEU(F).second;
836   if (OccupancyHint == 0)
837     OccupancyHint = 7;
838 
839   // Clamp to max value.
840   OccupancyHint = std::min(OccupancyHint, ST.getMaxWavesPerEU());
841 
842   // Check the hint but ignore it if it's obviously wrong from the existing LDS
843   // usage.
844   MaxOccupancy = std::min(OccupancyHint, MaxOccupancy);
845 
846 
847   // Round up to the next tier of usage.
848   unsigned MaxSizeWithWaveCount
849     = ST.getMaxLocalMemSizeWithWaveCount(MaxOccupancy, F);
850 
851   // Program is possibly broken by using more local mem than available.
852   if (CurrentLocalMemUsage > MaxSizeWithWaveCount)
853     return false;
854 
855   LocalMemLimit = MaxSizeWithWaveCount;
856 
857   LLVM_DEBUG(dbgs() << F.getName() << " uses " << CurrentLocalMemUsage
858                     << " bytes of LDS\n"
859                     << "  Rounding size to " << MaxSizeWithWaveCount
860                     << " with a maximum occupancy of " << MaxOccupancy << '\n'
861                     << " and " << (LocalMemLimit - CurrentLocalMemUsage)
862                     << " available for promotion\n");
863 
864   return true;
865 }
866 
867 // FIXME: Should try to pick the most likely to be profitable allocas first.
868 bool AMDGPUPromoteAllocaImpl::handleAlloca(AllocaInst &I, bool SufficientLDS) {
869   // Array allocations are probably not worth handling, since an allocation of
870   // the array type is the canonical form.
871   if (!I.isStaticAlloca() || I.isArrayAllocation())
872     return false;
873 
874   const DataLayout &DL = Mod->getDataLayout();
875   IRBuilder<> Builder(&I);
876 
877   // First try to replace the alloca with a vector
878   Type *AllocaTy = I.getAllocatedType();
879 
880   LLVM_DEBUG(dbgs() << "Trying to promote " << I << '\n');
881 
882   if (tryPromoteAllocaToVector(&I, DL, MaxVGPRs))
883     return true; // Promoted to vector.
884 
885   if (DisablePromoteAllocaToLDS)
886     return false;
887 
888   const Function &ContainingFunction = *I.getParent()->getParent();
889   CallingConv::ID CC = ContainingFunction.getCallingConv();
890 
891   // Don't promote the alloca to LDS for shader calling conventions as the work
892   // item ID intrinsics are not supported for these calling conventions.
893   // Furthermore not all LDS is available for some of the stages.
894   switch (CC) {
895   case CallingConv::AMDGPU_KERNEL:
896   case CallingConv::SPIR_KERNEL:
897     break;
898   default:
899     LLVM_DEBUG(
900         dbgs()
901         << " promote alloca to LDS not supported with calling convention.\n");
902     return false;
903   }
904 
905   // Not likely to have sufficient local memory for promotion.
906   if (!SufficientLDS)
907     return false;
908 
909   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, ContainingFunction);
910   unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second;
911 
912   Align Alignment =
913       DL.getValueOrABITypeAlignment(I.getAlign(), I.getAllocatedType());
914 
915   // FIXME: This computed padding is likely wrong since it depends on inverse
916   // usage order.
917   //
918   // FIXME: It is also possible that if we're allowed to use all of the memory
919   // could could end up using more than the maximum due to alignment padding.
920 
921   uint32_t NewSize = alignTo(CurrentLocalMemUsage, Alignment);
922   uint32_t AllocSize = WorkGroupSize * DL.getTypeAllocSize(AllocaTy);
923   NewSize += AllocSize;
924 
925   if (NewSize > LocalMemLimit) {
926     LLVM_DEBUG(dbgs() << "  " << AllocSize
927                       << " bytes of local memory not available to promote\n");
928     return false;
929   }
930 
931   CurrentLocalMemUsage = NewSize;
932 
933   std::vector<Value*> WorkList;
934 
935   if (!collectUsesWithPtrTypes(&I, &I, WorkList)) {
936     LLVM_DEBUG(dbgs() << " Do not know how to convert all uses\n");
937     return false;
938   }
939 
940   LLVM_DEBUG(dbgs() << "Promoting alloca to local memory\n");
941 
942   Function *F = I.getParent()->getParent();
943 
944   Type *GVTy = ArrayType::get(I.getAllocatedType(), WorkGroupSize);
945   GlobalVariable *GV = new GlobalVariable(
946       *Mod, GVTy, false, GlobalValue::InternalLinkage,
947       UndefValue::get(GVTy),
948       Twine(F->getName()) + Twine('.') + I.getName(),
949       nullptr,
950       GlobalVariable::NotThreadLocal,
951       AMDGPUAS::LOCAL_ADDRESS);
952   GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
953   GV->setAlignment(I.getAlign());
954 
955   Value *TCntY, *TCntZ;
956 
957   std::tie(TCntY, TCntZ) = getLocalSizeYZ(Builder);
958   Value *TIdX = getWorkitemID(Builder, 0);
959   Value *TIdY = getWorkitemID(Builder, 1);
960   Value *TIdZ = getWorkitemID(Builder, 2);
961 
962   Value *Tmp0 = Builder.CreateMul(TCntY, TCntZ, "", true, true);
963   Tmp0 = Builder.CreateMul(Tmp0, TIdX);
964   Value *Tmp1 = Builder.CreateMul(TIdY, TCntZ, "", true, true);
965   Value *TID = Builder.CreateAdd(Tmp0, Tmp1);
966   TID = Builder.CreateAdd(TID, TIdZ);
967 
968   Value *Indices[] = {
969     Constant::getNullValue(Type::getInt32Ty(Mod->getContext())),
970     TID
971   };
972 
973   Value *Offset = Builder.CreateInBoundsGEP(GVTy, GV, Indices);
974   I.mutateType(Offset->getType());
975   I.replaceAllUsesWith(Offset);
976   I.eraseFromParent();
977 
978   SmallVector<IntrinsicInst *> DeferredIntrs;
979 
980   for (Value *V : WorkList) {
981     CallInst *Call = dyn_cast<CallInst>(V);
982     if (!Call) {
983       if (ICmpInst *CI = dyn_cast<ICmpInst>(V)) {
984         Value *Src0 = CI->getOperand(0);
985         PointerType *NewTy = PointerType::getWithSamePointeeType(
986             cast<PointerType>(Src0->getType()), AMDGPUAS::LOCAL_ADDRESS);
987 
988         if (isa<ConstantPointerNull>(CI->getOperand(0)))
989           CI->setOperand(0, ConstantPointerNull::get(NewTy));
990 
991         if (isa<ConstantPointerNull>(CI->getOperand(1)))
992           CI->setOperand(1, ConstantPointerNull::get(NewTy));
993 
994         continue;
995       }
996 
997       // The operand's value should be corrected on its own and we don't want to
998       // touch the users.
999       if (isa<AddrSpaceCastInst>(V))
1000         continue;
1001 
1002       PointerType *NewTy = PointerType::getWithSamePointeeType(
1003           cast<PointerType>(V->getType()), AMDGPUAS::LOCAL_ADDRESS);
1004 
1005       // FIXME: It doesn't really make sense to try to do this for all
1006       // instructions.
1007       V->mutateType(NewTy);
1008 
1009       // Adjust the types of any constant operands.
1010       if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
1011         if (isa<ConstantPointerNull>(SI->getOperand(1)))
1012           SI->setOperand(1, ConstantPointerNull::get(NewTy));
1013 
1014         if (isa<ConstantPointerNull>(SI->getOperand(2)))
1015           SI->setOperand(2, ConstantPointerNull::get(NewTy));
1016       } else if (PHINode *Phi = dyn_cast<PHINode>(V)) {
1017         for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1018           if (isa<ConstantPointerNull>(Phi->getIncomingValue(I)))
1019             Phi->setIncomingValue(I, ConstantPointerNull::get(NewTy));
1020         }
1021       }
1022 
1023       continue;
1024     }
1025 
1026     IntrinsicInst *Intr = cast<IntrinsicInst>(Call);
1027     Builder.SetInsertPoint(Intr);
1028     switch (Intr->getIntrinsicID()) {
1029     case Intrinsic::lifetime_start:
1030     case Intrinsic::lifetime_end:
1031       // These intrinsics are for address space 0 only
1032       Intr->eraseFromParent();
1033       continue;
1034     case Intrinsic::memcpy:
1035     case Intrinsic::memmove:
1036       // These have 2 pointer operands. In case if second pointer also needs
1037       // to be replaced we defer processing of these intrinsics until all
1038       // other values are processed.
1039       DeferredIntrs.push_back(Intr);
1040       continue;
1041     case Intrinsic::memset: {
1042       MemSetInst *MemSet = cast<MemSetInst>(Intr);
1043       Builder.CreateMemSet(
1044           MemSet->getRawDest(), MemSet->getValue(), MemSet->getLength(),
1045           MaybeAlign(MemSet->getDestAlignment()), MemSet->isVolatile());
1046       Intr->eraseFromParent();
1047       continue;
1048     }
1049     case Intrinsic::invariant_start:
1050     case Intrinsic::invariant_end:
1051     case Intrinsic::launder_invariant_group:
1052     case Intrinsic::strip_invariant_group:
1053       Intr->eraseFromParent();
1054       // FIXME: I think the invariant marker should still theoretically apply,
1055       // but the intrinsics need to be changed to accept pointers with any
1056       // address space.
1057       continue;
1058     case Intrinsic::objectsize: {
1059       Value *Src = Intr->getOperand(0);
1060       Function *ObjectSize = Intrinsic::getDeclaration(
1061           Mod, Intrinsic::objectsize,
1062           {Intr->getType(),
1063            PointerType::getWithSamePointeeType(
1064                cast<PointerType>(Src->getType()), AMDGPUAS::LOCAL_ADDRESS)});
1065 
1066       CallInst *NewCall = Builder.CreateCall(
1067           ObjectSize,
1068           {Src, Intr->getOperand(1), Intr->getOperand(2), Intr->getOperand(3)});
1069       Intr->replaceAllUsesWith(NewCall);
1070       Intr->eraseFromParent();
1071       continue;
1072     }
1073     default:
1074       Intr->print(errs());
1075       llvm_unreachable("Don't know how to promote alloca intrinsic use.");
1076     }
1077   }
1078 
1079   for (IntrinsicInst *Intr : DeferredIntrs) {
1080     Builder.SetInsertPoint(Intr);
1081     Intrinsic::ID ID = Intr->getIntrinsicID();
1082     assert(ID == Intrinsic::memcpy || ID == Intrinsic::memmove);
1083 
1084     MemTransferInst *MI = cast<MemTransferInst>(Intr);
1085     auto *B =
1086       Builder.CreateMemTransferInst(ID, MI->getRawDest(), MI->getDestAlign(),
1087                                     MI->getRawSource(), MI->getSourceAlign(),
1088                                     MI->getLength(), MI->isVolatile());
1089 
1090     for (unsigned I = 0; I != 2; ++I) {
1091       if (uint64_t Bytes = Intr->getParamDereferenceableBytes(I)) {
1092         B->addDereferenceableParamAttr(I, Bytes);
1093       }
1094     }
1095 
1096     Intr->eraseFromParent();
1097   }
1098 
1099   return true;
1100 }
1101 
1102 bool handlePromoteAllocaToVector(AllocaInst &I, unsigned MaxVGPRs) {
1103   // Array allocations are probably not worth handling, since an allocation of
1104   // the array type is the canonical form.
1105   if (!I.isStaticAlloca() || I.isArrayAllocation())
1106     return false;
1107 
1108   LLVM_DEBUG(dbgs() << "Trying to promote " << I << '\n');
1109 
1110   Module *Mod = I.getParent()->getParent()->getParent();
1111   return tryPromoteAllocaToVector(&I, Mod->getDataLayout(), MaxVGPRs);
1112 }
1113 
1114 bool promoteAllocasToVector(Function &F, TargetMachine &TM) {
1115   if (DisablePromoteAllocaToVector)
1116     return false;
1117 
1118   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, F);
1119   if (!ST.isPromoteAllocaEnabled())
1120     return false;
1121 
1122   unsigned MaxVGPRs;
1123   if (TM.getTargetTriple().getArch() == Triple::amdgcn) {
1124     const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
1125     MaxVGPRs = ST.getMaxNumVGPRs(ST.getWavesPerEU(F).first);
1126     // A non-entry function has only 32 caller preserved registers.
1127     // Do not promote alloca which will force spilling.
1128     if (!AMDGPU::isEntryFunctionCC(F.getCallingConv()))
1129       MaxVGPRs = std::min(MaxVGPRs, 32u);
1130   } else {
1131     MaxVGPRs = 128;
1132   }
1133 
1134   bool Changed = false;
1135   BasicBlock &EntryBB = *F.begin();
1136 
1137   SmallVector<AllocaInst *, 16> Allocas;
1138   for (Instruction &I : EntryBB) {
1139     if (AllocaInst *AI = dyn_cast<AllocaInst>(&I))
1140       Allocas.push_back(AI);
1141   }
1142 
1143   for (AllocaInst *AI : Allocas) {
1144     if (handlePromoteAllocaToVector(*AI, MaxVGPRs))
1145       Changed = true;
1146   }
1147 
1148   return Changed;
1149 }
1150 
1151 bool AMDGPUPromoteAllocaToVector::runOnFunction(Function &F) {
1152   if (skipFunction(F))
1153     return false;
1154   if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) {
1155     return promoteAllocasToVector(F, TPC->getTM<TargetMachine>());
1156   }
1157   return false;
1158 }
1159 
1160 PreservedAnalyses
1161 AMDGPUPromoteAllocaToVectorPass::run(Function &F, FunctionAnalysisManager &AM) {
1162   bool Changed = promoteAllocasToVector(F, TM);
1163   if (Changed) {
1164     PreservedAnalyses PA;
1165     PA.preserveSet<CFGAnalyses>();
1166     return PA;
1167   }
1168   return PreservedAnalyses::all();
1169 }
1170 
1171 FunctionPass *llvm::createAMDGPUPromoteAlloca() {
1172   return new AMDGPUPromoteAlloca();
1173 }
1174 
1175 FunctionPass *llvm::createAMDGPUPromoteAllocaToVector() {
1176   return new AMDGPUPromoteAllocaToVector();
1177 }
1178