10b57cec5SDimitry Andric //===-- AMDGPUPromoteAlloca.cpp - Promote Allocas -------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass eliminates allocas by either converting them into vectors or 100b57cec5SDimitry Andric // by migrating them to local address space. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "AMDGPU.h" 150b57cec5SDimitry Andric #include "AMDGPUSubtarget.h" 160b57cec5SDimitry Andric #include "Utils/AMDGPUBaseInfo.h" 170b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 180b57cec5SDimitry Andric #include "llvm/ADT/None.h" 190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 200b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 210b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 220b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 230b57cec5SDimitry Andric #include "llvm/Analysis/CaptureTracking.h" 240b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 260b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 270b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 280b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 290b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 300b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 310b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 320b57cec5SDimitry Andric #include "llvm/IR/Function.h" 330b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 340b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 350b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 360b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 370b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 380b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 390b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 40*480093f4SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h" 41*480093f4SDimitry Andric #include "llvm/IR/IntrinsicsR600.h" 420b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 430b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 440b57cec5SDimitry Andric #include "llvm/IR/Module.h" 450b57cec5SDimitry Andric #include "llvm/IR/Type.h" 460b57cec5SDimitry Andric #include "llvm/IR/User.h" 470b57cec5SDimitry Andric #include "llvm/IR/Value.h" 480b57cec5SDimitry Andric #include "llvm/Pass.h" 490b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 500b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 510b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 520b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 530b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 540b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 550b57cec5SDimitry Andric #include <algorithm> 560b57cec5SDimitry Andric #include <cassert> 570b57cec5SDimitry Andric #include <cstdint> 580b57cec5SDimitry Andric #include <map> 590b57cec5SDimitry Andric #include <tuple> 600b57cec5SDimitry Andric #include <utility> 610b57cec5SDimitry Andric #include <vector> 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric #define DEBUG_TYPE "amdgpu-promote-alloca" 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric using namespace llvm; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric namespace { 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric static cl::opt<bool> DisablePromoteAllocaToVector( 700b57cec5SDimitry Andric "disable-promote-alloca-to-vector", 710b57cec5SDimitry Andric cl::desc("Disable promote alloca to vector"), 720b57cec5SDimitry Andric cl::init(false)); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric static cl::opt<bool> DisablePromoteAllocaToLDS( 750b57cec5SDimitry Andric "disable-promote-alloca-to-lds", 760b57cec5SDimitry Andric cl::desc("Disable promote alloca to LDS"), 770b57cec5SDimitry Andric cl::init(false)); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric // FIXME: This can create globals so should be a module pass. 800b57cec5SDimitry Andric class AMDGPUPromoteAlloca : public FunctionPass { 810b57cec5SDimitry Andric private: 820b57cec5SDimitry Andric const TargetMachine *TM; 830b57cec5SDimitry Andric Module *Mod = nullptr; 840b57cec5SDimitry Andric const DataLayout *DL = nullptr; 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // FIXME: This should be per-kernel. 870b57cec5SDimitry Andric uint32_t LocalMemLimit = 0; 880b57cec5SDimitry Andric uint32_t CurrentLocalMemUsage = 0; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric bool IsAMDGCN = false; 910b57cec5SDimitry Andric bool IsAMDHSA = false; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric std::pair<Value *, Value *> getLocalSizeYZ(IRBuilder<> &Builder); 940b57cec5SDimitry Andric Value *getWorkitemID(IRBuilder<> &Builder, unsigned N); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric /// BaseAlloca is the alloca root the search started from. 970b57cec5SDimitry Andric /// Val may be that alloca or a recursive user of it. 980b57cec5SDimitry Andric bool collectUsesWithPtrTypes(Value *BaseAlloca, 990b57cec5SDimitry Andric Value *Val, 1000b57cec5SDimitry Andric std::vector<Value*> &WorkList) const; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric /// Val is a derived pointer from Alloca. OpIdx0/OpIdx1 are the operand 1030b57cec5SDimitry Andric /// indices to an instruction with 2 pointer inputs (e.g. select, icmp). 1040b57cec5SDimitry Andric /// Returns true if both operands are derived from the same alloca. Val should 1050b57cec5SDimitry Andric /// be the same value as one of the input operands of UseInst. 1060b57cec5SDimitry Andric bool binaryOpIsDerivedFromSameAlloca(Value *Alloca, Value *Val, 1070b57cec5SDimitry Andric Instruction *UseInst, 1080b57cec5SDimitry Andric int OpIdx0, int OpIdx1) const; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Check whether we have enough local memory for promotion. 1110b57cec5SDimitry Andric bool hasSufficientLocalMem(const Function &F); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric public: 1140b57cec5SDimitry Andric static char ID; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric AMDGPUPromoteAlloca() : FunctionPass(ID) {} 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric bool doInitialization(Module &M) override; 1190b57cec5SDimitry Andric bool runOnFunction(Function &F) override; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric StringRef getPassName() const override { return "AMDGPU Promote Alloca"; } 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric bool handleAlloca(AllocaInst &I, bool SufficientLDS); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1260b57cec5SDimitry Andric AU.setPreservesCFG(); 1270b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU); 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric }; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric } // end anonymous namespace 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric char AMDGPUPromoteAlloca::ID = 0; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric INITIALIZE_PASS(AMDGPUPromoteAlloca, DEBUG_TYPE, 1360b57cec5SDimitry Andric "AMDGPU promote alloca to vector or LDS", false, false) 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric char &llvm::AMDGPUPromoteAllocaID = AMDGPUPromoteAlloca::ID; 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::doInitialization(Module &M) { 1410b57cec5SDimitry Andric Mod = &M; 1420b57cec5SDimitry Andric DL = &Mod->getDataLayout(); 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric return false; 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::runOnFunction(Function &F) { 1480b57cec5SDimitry Andric if (skipFunction(F)) 1490b57cec5SDimitry Andric return false; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) 1520b57cec5SDimitry Andric TM = &TPC->getTM<TargetMachine>(); 1530b57cec5SDimitry Andric else 1540b57cec5SDimitry Andric return false; 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric const Triple &TT = TM->getTargetTriple(); 1570b57cec5SDimitry Andric IsAMDGCN = TT.getArch() == Triple::amdgcn; 1580b57cec5SDimitry Andric IsAMDHSA = TT.getOS() == Triple::AMDHSA; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(*TM, F); 1610b57cec5SDimitry Andric if (!ST.isPromoteAllocaEnabled()) 1620b57cec5SDimitry Andric return false; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric bool SufficientLDS = hasSufficientLocalMem(F); 1650b57cec5SDimitry Andric bool Changed = false; 1660b57cec5SDimitry Andric BasicBlock &EntryBB = *F.begin(); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric SmallVector<AllocaInst *, 16> Allocas; 1690b57cec5SDimitry Andric for (Instruction &I : EntryBB) { 1700b57cec5SDimitry Andric if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) 1710b57cec5SDimitry Andric Allocas.push_back(AI); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric for (AllocaInst *AI : Allocas) { 1750b57cec5SDimitry Andric if (handleAlloca(*AI, SufficientLDS)) 1760b57cec5SDimitry Andric Changed = true; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric return Changed; 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric std::pair<Value *, Value *> 1830b57cec5SDimitry Andric AMDGPUPromoteAlloca::getLocalSizeYZ(IRBuilder<> &Builder) { 1840b57cec5SDimitry Andric const Function &F = *Builder.GetInsertBlock()->getParent(); 1850b57cec5SDimitry Andric const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(*TM, F); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric if (!IsAMDHSA) { 1880b57cec5SDimitry Andric Function *LocalSizeYFn 1890b57cec5SDimitry Andric = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_y); 1900b57cec5SDimitry Andric Function *LocalSizeZFn 1910b57cec5SDimitry Andric = Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_z); 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric CallInst *LocalSizeY = Builder.CreateCall(LocalSizeYFn, {}); 1940b57cec5SDimitry Andric CallInst *LocalSizeZ = Builder.CreateCall(LocalSizeZFn, {}); 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric ST.makeLIDRangeMetadata(LocalSizeY); 1970b57cec5SDimitry Andric ST.makeLIDRangeMetadata(LocalSizeZ); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric return std::make_pair(LocalSizeY, LocalSizeZ); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // We must read the size out of the dispatch pointer. 2030b57cec5SDimitry Andric assert(IsAMDGCN); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // We are indexing into this struct, and want to extract the workgroup_size_* 2060b57cec5SDimitry Andric // fields. 2070b57cec5SDimitry Andric // 2080b57cec5SDimitry Andric // typedef struct hsa_kernel_dispatch_packet_s { 2090b57cec5SDimitry Andric // uint16_t header; 2100b57cec5SDimitry Andric // uint16_t setup; 2110b57cec5SDimitry Andric // uint16_t workgroup_size_x ; 2120b57cec5SDimitry Andric // uint16_t workgroup_size_y; 2130b57cec5SDimitry Andric // uint16_t workgroup_size_z; 2140b57cec5SDimitry Andric // uint16_t reserved0; 2150b57cec5SDimitry Andric // uint32_t grid_size_x ; 2160b57cec5SDimitry Andric // uint32_t grid_size_y ; 2170b57cec5SDimitry Andric // uint32_t grid_size_z; 2180b57cec5SDimitry Andric // 2190b57cec5SDimitry Andric // uint32_t private_segment_size; 2200b57cec5SDimitry Andric // uint32_t group_segment_size; 2210b57cec5SDimitry Andric // uint64_t kernel_object; 2220b57cec5SDimitry Andric // 2230b57cec5SDimitry Andric // #ifdef HSA_LARGE_MODEL 2240b57cec5SDimitry Andric // void *kernarg_address; 2250b57cec5SDimitry Andric // #elif defined HSA_LITTLE_ENDIAN 2260b57cec5SDimitry Andric // void *kernarg_address; 2270b57cec5SDimitry Andric // uint32_t reserved1; 2280b57cec5SDimitry Andric // #else 2290b57cec5SDimitry Andric // uint32_t reserved1; 2300b57cec5SDimitry Andric // void *kernarg_address; 2310b57cec5SDimitry Andric // #endif 2320b57cec5SDimitry Andric // uint64_t reserved2; 2330b57cec5SDimitry Andric // hsa_signal_t completion_signal; // uint64_t wrapper 2340b57cec5SDimitry Andric // } hsa_kernel_dispatch_packet_t 2350b57cec5SDimitry Andric // 2360b57cec5SDimitry Andric Function *DispatchPtrFn 2370b57cec5SDimitry Andric = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_dispatch_ptr); 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric CallInst *DispatchPtr = Builder.CreateCall(DispatchPtrFn, {}); 2400b57cec5SDimitry Andric DispatchPtr->addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); 2410b57cec5SDimitry Andric DispatchPtr->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // Size of the dispatch packet struct. 2440b57cec5SDimitry Andric DispatchPtr->addDereferenceableAttr(AttributeList::ReturnIndex, 64); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric Type *I32Ty = Type::getInt32Ty(Mod->getContext()); 2470b57cec5SDimitry Andric Value *CastDispatchPtr = Builder.CreateBitCast( 2480b57cec5SDimitry Andric DispatchPtr, PointerType::get(I32Ty, AMDGPUAS::CONSTANT_ADDRESS)); 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // We could do a single 64-bit load here, but it's likely that the basic 2510b57cec5SDimitry Andric // 32-bit and extract sequence is already present, and it is probably easier 2520b57cec5SDimitry Andric // to CSE this. The loads should be mergable later anyway. 2530b57cec5SDimitry Andric Value *GEPXY = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 1); 2540b57cec5SDimitry Andric LoadInst *LoadXY = Builder.CreateAlignedLoad(I32Ty, GEPXY, 4); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric Value *GEPZU = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 2); 2570b57cec5SDimitry Andric LoadInst *LoadZU = Builder.CreateAlignedLoad(I32Ty, GEPZU, 4); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric MDNode *MD = MDNode::get(Mod->getContext(), None); 2600b57cec5SDimitry Andric LoadXY->setMetadata(LLVMContext::MD_invariant_load, MD); 2610b57cec5SDimitry Andric LoadZU->setMetadata(LLVMContext::MD_invariant_load, MD); 2620b57cec5SDimitry Andric ST.makeLIDRangeMetadata(LoadZU); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // Extract y component. Upper half of LoadZU should be zero already. 2650b57cec5SDimitry Andric Value *Y = Builder.CreateLShr(LoadXY, 16); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric return std::make_pair(Y, LoadZU); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric Value *AMDGPUPromoteAlloca::getWorkitemID(IRBuilder<> &Builder, unsigned N) { 2710b57cec5SDimitry Andric const AMDGPUSubtarget &ST = 2720b57cec5SDimitry Andric AMDGPUSubtarget::get(*TM, *Builder.GetInsertBlock()->getParent()); 273*480093f4SDimitry Andric Intrinsic::ID IntrID = Intrinsic::not_intrinsic; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric switch (N) { 2760b57cec5SDimitry Andric case 0: 277*480093f4SDimitry Andric IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_x 278*480093f4SDimitry Andric : (Intrinsic::ID)Intrinsic::r600_read_tidig_x; 2790b57cec5SDimitry Andric break; 2800b57cec5SDimitry Andric case 1: 281*480093f4SDimitry Andric IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_y 282*480093f4SDimitry Andric : (Intrinsic::ID)Intrinsic::r600_read_tidig_y; 2830b57cec5SDimitry Andric break; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric case 2: 286*480093f4SDimitry Andric IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_z 287*480093f4SDimitry Andric : (Intrinsic::ID)Intrinsic::r600_read_tidig_z; 2880b57cec5SDimitry Andric break; 2890b57cec5SDimitry Andric default: 2900b57cec5SDimitry Andric llvm_unreachable("invalid dimension"); 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric Function *WorkitemIdFn = Intrinsic::getDeclaration(Mod, IntrID); 2940b57cec5SDimitry Andric CallInst *CI = Builder.CreateCall(WorkitemIdFn); 2950b57cec5SDimitry Andric ST.makeLIDRangeMetadata(CI); 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric return CI; 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric static VectorType *arrayTypeToVecType(ArrayType *ArrayTy) { 3010b57cec5SDimitry Andric return VectorType::get(ArrayTy->getElementType(), 3020b57cec5SDimitry Andric ArrayTy->getNumElements()); 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric static Value * 3060b57cec5SDimitry Andric calculateVectorIndex(Value *Ptr, 3070b57cec5SDimitry Andric const std::map<GetElementPtrInst *, Value *> &GEPIdx) { 3080b57cec5SDimitry Andric GetElementPtrInst *GEP = cast<GetElementPtrInst>(Ptr); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric auto I = GEPIdx.find(GEP); 3110b57cec5SDimitry Andric return I == GEPIdx.end() ? nullptr : I->second; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric static Value* GEPToVectorIndex(GetElementPtrInst *GEP) { 3150b57cec5SDimitry Andric // FIXME we only support simple cases 3160b57cec5SDimitry Andric if (GEP->getNumOperands() != 3) 3170b57cec5SDimitry Andric return nullptr; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric ConstantInt *I0 = dyn_cast<ConstantInt>(GEP->getOperand(1)); 3200b57cec5SDimitry Andric if (!I0 || !I0->isZero()) 3210b57cec5SDimitry Andric return nullptr; 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric return GEP->getOperand(2); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Not an instruction handled below to turn into a vector. 3270b57cec5SDimitry Andric // 3280b57cec5SDimitry Andric // TODO: Check isTriviallyVectorizable for calls and handle other 3290b57cec5SDimitry Andric // instructions. 3300b57cec5SDimitry Andric static bool canVectorizeInst(Instruction *Inst, User *User) { 3310b57cec5SDimitry Andric switch (Inst->getOpcode()) { 3320b57cec5SDimitry Andric case Instruction::Load: { 3330b57cec5SDimitry Andric // Currently only handle the case where the Pointer Operand is a GEP. 3340b57cec5SDimitry Andric // Also we could not vectorize volatile or atomic loads. 3350b57cec5SDimitry Andric LoadInst *LI = cast<LoadInst>(Inst); 3360b57cec5SDimitry Andric if (isa<AllocaInst>(User) && 3370b57cec5SDimitry Andric LI->getPointerOperandType() == User->getType() && 3380b57cec5SDimitry Andric isa<VectorType>(LI->getType())) 3390b57cec5SDimitry Andric return true; 3400b57cec5SDimitry Andric return isa<GetElementPtrInst>(LI->getPointerOperand()) && LI->isSimple(); 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric case Instruction::BitCast: 3430b57cec5SDimitry Andric return true; 3440b57cec5SDimitry Andric case Instruction::Store: { 3450b57cec5SDimitry Andric // Must be the stored pointer operand, not a stored value, plus 3460b57cec5SDimitry Andric // since it should be canonical form, the User should be a GEP. 3470b57cec5SDimitry Andric // Also we could not vectorize volatile or atomic stores. 3480b57cec5SDimitry Andric StoreInst *SI = cast<StoreInst>(Inst); 3490b57cec5SDimitry Andric if (isa<AllocaInst>(User) && 3500b57cec5SDimitry Andric SI->getPointerOperandType() == User->getType() && 3510b57cec5SDimitry Andric isa<VectorType>(SI->getValueOperand()->getType())) 3520b57cec5SDimitry Andric return true; 3530b57cec5SDimitry Andric return (SI->getPointerOperand() == User) && isa<GetElementPtrInst>(User) && SI->isSimple(); 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric default: 3560b57cec5SDimitry Andric return false; 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric static bool tryPromoteAllocaToVector(AllocaInst *Alloca) { 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric if (DisablePromoteAllocaToVector) { 3630b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Promotion alloca to vector is disabled\n"); 3640b57cec5SDimitry Andric return false; 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric Type *AT = Alloca->getAllocatedType(); 3680b57cec5SDimitry Andric SequentialType *AllocaTy = dyn_cast<SequentialType>(AT); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Alloca candidate for vectorization\n"); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric // FIXME: There is no reason why we can't support larger arrays, we 3730b57cec5SDimitry Andric // are just being conservative for now. 3740b57cec5SDimitry Andric // FIXME: We also reject alloca's of the form [ 2 x [ 2 x i32 ]] or equivalent. Potentially these 3750b57cec5SDimitry Andric // could also be promoted but we don't currently handle this case 3760b57cec5SDimitry Andric if (!AllocaTy || 3770b57cec5SDimitry Andric AllocaTy->getNumElements() > 16 || 3780b57cec5SDimitry Andric AllocaTy->getNumElements() < 2 || 3790b57cec5SDimitry Andric !VectorType::isValidElementType(AllocaTy->getElementType())) { 3800b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Cannot convert type to vector\n"); 3810b57cec5SDimitry Andric return false; 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric std::map<GetElementPtrInst*, Value*> GEPVectorIdx; 3850b57cec5SDimitry Andric std::vector<Value*> WorkList; 3860b57cec5SDimitry Andric for (User *AllocaUser : Alloca->users()) { 3870b57cec5SDimitry Andric GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(AllocaUser); 3880b57cec5SDimitry Andric if (!GEP) { 3890b57cec5SDimitry Andric if (!canVectorizeInst(cast<Instruction>(AllocaUser), Alloca)) 3900b57cec5SDimitry Andric return false; 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric WorkList.push_back(AllocaUser); 3930b57cec5SDimitry Andric continue; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric Value *Index = GEPToVectorIndex(GEP); 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric // If we can't compute a vector index from this GEP, then we can't 3990b57cec5SDimitry Andric // promote this alloca to vector. 4000b57cec5SDimitry Andric if (!Index) { 4010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Cannot compute vector index for GEP " << *GEP 4020b57cec5SDimitry Andric << '\n'); 4030b57cec5SDimitry Andric return false; 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric GEPVectorIdx[GEP] = Index; 4070b57cec5SDimitry Andric for (User *GEPUser : AllocaUser->users()) { 4080b57cec5SDimitry Andric if (!canVectorizeInst(cast<Instruction>(GEPUser), AllocaUser)) 4090b57cec5SDimitry Andric return false; 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric WorkList.push_back(GEPUser); 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric VectorType *VectorTy = dyn_cast<VectorType>(AllocaTy); 4160b57cec5SDimitry Andric if (!VectorTy) 4170b57cec5SDimitry Andric VectorTy = arrayTypeToVecType(cast<ArrayType>(AllocaTy)); 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Converting alloca to vector " << *AllocaTy << " -> " 4200b57cec5SDimitry Andric << *VectorTy << '\n'); 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric for (Value *V : WorkList) { 4230b57cec5SDimitry Andric Instruction *Inst = cast<Instruction>(V); 4240b57cec5SDimitry Andric IRBuilder<> Builder(Inst); 4250b57cec5SDimitry Andric switch (Inst->getOpcode()) { 4260b57cec5SDimitry Andric case Instruction::Load: { 4270b57cec5SDimitry Andric if (Inst->getType() == AT) 4280b57cec5SDimitry Andric break; 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric Type *VecPtrTy = VectorTy->getPointerTo(AMDGPUAS::PRIVATE_ADDRESS); 4310b57cec5SDimitry Andric Value *Ptr = cast<LoadInst>(Inst)->getPointerOperand(); 4320b57cec5SDimitry Andric Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy); 4350b57cec5SDimitry Andric Value *VecValue = Builder.CreateLoad(VectorTy, BitCast); 4360b57cec5SDimitry Andric Value *ExtractElement = Builder.CreateExtractElement(VecValue, Index); 4370b57cec5SDimitry Andric Inst->replaceAllUsesWith(ExtractElement); 4380b57cec5SDimitry Andric Inst->eraseFromParent(); 4390b57cec5SDimitry Andric break; 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric case Instruction::Store: { 4420b57cec5SDimitry Andric StoreInst *SI = cast<StoreInst>(Inst); 4430b57cec5SDimitry Andric if (SI->getValueOperand()->getType() == AT) 4440b57cec5SDimitry Andric break; 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric Type *VecPtrTy = VectorTy->getPointerTo(AMDGPUAS::PRIVATE_ADDRESS); 4470b57cec5SDimitry Andric Value *Ptr = SI->getPointerOperand(); 4480b57cec5SDimitry Andric Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx); 4490b57cec5SDimitry Andric Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy); 4500b57cec5SDimitry Andric Value *VecValue = Builder.CreateLoad(VectorTy, BitCast); 4510b57cec5SDimitry Andric Value *NewVecValue = Builder.CreateInsertElement(VecValue, 4520b57cec5SDimitry Andric SI->getValueOperand(), 4530b57cec5SDimitry Andric Index); 4540b57cec5SDimitry Andric Builder.CreateStore(NewVecValue, BitCast); 4550b57cec5SDimitry Andric Inst->eraseFromParent(); 4560b57cec5SDimitry Andric break; 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric case Instruction::BitCast: 4590b57cec5SDimitry Andric case Instruction::AddrSpaceCast: 4600b57cec5SDimitry Andric break; 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric default: 4630b57cec5SDimitry Andric llvm_unreachable("Inconsistency in instructions promotable to vector"); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric return true; 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric static bool isCallPromotable(CallInst *CI) { 4700b57cec5SDimitry Andric IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); 4710b57cec5SDimitry Andric if (!II) 4720b57cec5SDimitry Andric return false; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric switch (II->getIntrinsicID()) { 4750b57cec5SDimitry Andric case Intrinsic::memcpy: 4760b57cec5SDimitry Andric case Intrinsic::memmove: 4770b57cec5SDimitry Andric case Intrinsic::memset: 4780b57cec5SDimitry Andric case Intrinsic::lifetime_start: 4790b57cec5SDimitry Andric case Intrinsic::lifetime_end: 4800b57cec5SDimitry Andric case Intrinsic::invariant_start: 4810b57cec5SDimitry Andric case Intrinsic::invariant_end: 4820b57cec5SDimitry Andric case Intrinsic::launder_invariant_group: 4830b57cec5SDimitry Andric case Intrinsic::strip_invariant_group: 4840b57cec5SDimitry Andric case Intrinsic::objectsize: 4850b57cec5SDimitry Andric return true; 4860b57cec5SDimitry Andric default: 4870b57cec5SDimitry Andric return false; 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::binaryOpIsDerivedFromSameAlloca(Value *BaseAlloca, 4920b57cec5SDimitry Andric Value *Val, 4930b57cec5SDimitry Andric Instruction *Inst, 4940b57cec5SDimitry Andric int OpIdx0, 4950b57cec5SDimitry Andric int OpIdx1) const { 4960b57cec5SDimitry Andric // Figure out which operand is the one we might not be promoting. 4970b57cec5SDimitry Andric Value *OtherOp = Inst->getOperand(OpIdx0); 4980b57cec5SDimitry Andric if (Val == OtherOp) 4990b57cec5SDimitry Andric OtherOp = Inst->getOperand(OpIdx1); 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric if (isa<ConstantPointerNull>(OtherOp)) 5020b57cec5SDimitry Andric return true; 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric Value *OtherObj = GetUnderlyingObject(OtherOp, *DL); 5050b57cec5SDimitry Andric if (!isa<AllocaInst>(OtherObj)) 5060b57cec5SDimitry Andric return false; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric // TODO: We should be able to replace undefs with the right pointer type. 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric // TODO: If we know the other base object is another promotable 5110b57cec5SDimitry Andric // alloca, not necessarily this alloca, we can do this. The 5120b57cec5SDimitry Andric // important part is both must have the same address space at 5130b57cec5SDimitry Andric // the end. 5140b57cec5SDimitry Andric if (OtherObj != BaseAlloca) { 5150b57cec5SDimitry Andric LLVM_DEBUG( 5160b57cec5SDimitry Andric dbgs() << "Found a binary instruction with another alloca object\n"); 5170b57cec5SDimitry Andric return false; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric return true; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::collectUsesWithPtrTypes( 5240b57cec5SDimitry Andric Value *BaseAlloca, 5250b57cec5SDimitry Andric Value *Val, 5260b57cec5SDimitry Andric std::vector<Value*> &WorkList) const { 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric for (User *User : Val->users()) { 5290b57cec5SDimitry Andric if (is_contained(WorkList, User)) 5300b57cec5SDimitry Andric continue; 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(User)) { 5330b57cec5SDimitry Andric if (!isCallPromotable(CI)) 5340b57cec5SDimitry Andric return false; 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric WorkList.push_back(User); 5370b57cec5SDimitry Andric continue; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric Instruction *UseInst = cast<Instruction>(User); 5410b57cec5SDimitry Andric if (UseInst->getOpcode() == Instruction::PtrToInt) 5420b57cec5SDimitry Andric return false; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(UseInst)) { 5450b57cec5SDimitry Andric if (LI->isVolatile()) 5460b57cec5SDimitry Andric return false; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric continue; 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(UseInst)) { 5520b57cec5SDimitry Andric if (SI->isVolatile()) 5530b57cec5SDimitry Andric return false; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Reject if the stored value is not the pointer operand. 5560b57cec5SDimitry Andric if (SI->getPointerOperand() != Val) 5570b57cec5SDimitry Andric return false; 5580b57cec5SDimitry Andric } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UseInst)) { 5590b57cec5SDimitry Andric if (RMW->isVolatile()) 5600b57cec5SDimitry Andric return false; 5610b57cec5SDimitry Andric } else if (AtomicCmpXchgInst *CAS = dyn_cast<AtomicCmpXchgInst>(UseInst)) { 5620b57cec5SDimitry Andric if (CAS->isVolatile()) 5630b57cec5SDimitry Andric return false; 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric // Only promote a select if we know that the other select operand 5670b57cec5SDimitry Andric // is from another pointer that will also be promoted. 5680b57cec5SDimitry Andric if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { 5690b57cec5SDimitry Andric if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, ICmp, 0, 1)) 5700b57cec5SDimitry Andric return false; 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric // May need to rewrite constant operands. 5730b57cec5SDimitry Andric WorkList.push_back(ICmp); 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric if (UseInst->getOpcode() == Instruction::AddrSpaceCast) { 5770b57cec5SDimitry Andric // Give up if the pointer may be captured. 5780b57cec5SDimitry Andric if (PointerMayBeCaptured(UseInst, true, true)) 5790b57cec5SDimitry Andric return false; 5800b57cec5SDimitry Andric // Don't collect the users of this. 5810b57cec5SDimitry Andric WorkList.push_back(User); 5820b57cec5SDimitry Andric continue; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric if (!User->getType()->isPointerTy()) 5860b57cec5SDimitry Andric continue; 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UseInst)) { 5890b57cec5SDimitry Andric // Be conservative if an address could be computed outside the bounds of 5900b57cec5SDimitry Andric // the alloca. 5910b57cec5SDimitry Andric if (!GEP->isInBounds()) 5920b57cec5SDimitry Andric return false; 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric // Only promote a select if we know that the other select operand is from 5960b57cec5SDimitry Andric // another pointer that will also be promoted. 5970b57cec5SDimitry Andric if (SelectInst *SI = dyn_cast<SelectInst>(UseInst)) { 5980b57cec5SDimitry Andric if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, SI, 1, 2)) 5990b57cec5SDimitry Andric return false; 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // Repeat for phis. 6030b57cec5SDimitry Andric if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) { 6040b57cec5SDimitry Andric // TODO: Handle more complex cases. We should be able to replace loops 6050b57cec5SDimitry Andric // over arrays. 6060b57cec5SDimitry Andric switch (Phi->getNumIncomingValues()) { 6070b57cec5SDimitry Andric case 1: 6080b57cec5SDimitry Andric break; 6090b57cec5SDimitry Andric case 2: 6100b57cec5SDimitry Andric if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, Phi, 0, 1)) 6110b57cec5SDimitry Andric return false; 6120b57cec5SDimitry Andric break; 6130b57cec5SDimitry Andric default: 6140b57cec5SDimitry Andric return false; 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric WorkList.push_back(User); 6190b57cec5SDimitry Andric if (!collectUsesWithPtrTypes(BaseAlloca, User, WorkList)) 6200b57cec5SDimitry Andric return false; 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric return true; 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::hasSufficientLocalMem(const Function &F) { 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric FunctionType *FTy = F.getFunctionType(); 6290b57cec5SDimitry Andric const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(*TM, F); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric // If the function has any arguments in the local address space, then it's 6320b57cec5SDimitry Andric // possible these arguments require the entire local memory space, so 6330b57cec5SDimitry Andric // we cannot use local memory in the pass. 6340b57cec5SDimitry Andric for (Type *ParamTy : FTy->params()) { 6350b57cec5SDimitry Andric PointerType *PtrTy = dyn_cast<PointerType>(ParamTy); 6360b57cec5SDimitry Andric if (PtrTy && PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 6370b57cec5SDimitry Andric LocalMemLimit = 0; 6380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Function has local memory argument. Promoting to " 6390b57cec5SDimitry Andric "local memory disabled.\n"); 6400b57cec5SDimitry Andric return false; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric LocalMemLimit = ST.getLocalMemorySize(); 6450b57cec5SDimitry Andric if (LocalMemLimit == 0) 6460b57cec5SDimitry Andric return false; 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric const DataLayout &DL = Mod->getDataLayout(); 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric // Check how much local memory is being used by global objects 6510b57cec5SDimitry Andric CurrentLocalMemUsage = 0; 6520b57cec5SDimitry Andric for (GlobalVariable &GV : Mod->globals()) { 653*480093f4SDimitry Andric if (GV.getAddressSpace() != AMDGPUAS::LOCAL_ADDRESS) 6540b57cec5SDimitry Andric continue; 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric for (const User *U : GV.users()) { 6570b57cec5SDimitry Andric const Instruction *Use = dyn_cast<Instruction>(U); 6580b57cec5SDimitry Andric if (!Use) 6590b57cec5SDimitry Andric continue; 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric if (Use->getParent()->getParent() == &F) { 6620b57cec5SDimitry Andric unsigned Align = GV.getAlignment(); 6630b57cec5SDimitry Andric if (Align == 0) 6640b57cec5SDimitry Andric Align = DL.getABITypeAlignment(GV.getValueType()); 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric // FIXME: Try to account for padding here. The padding is currently 6670b57cec5SDimitry Andric // determined from the inverse order of uses in the function. I'm not 6680b57cec5SDimitry Andric // sure if the use list order is in any way connected to this, so the 6690b57cec5SDimitry Andric // total reported size is likely incorrect. 6700b57cec5SDimitry Andric uint64_t AllocSize = DL.getTypeAllocSize(GV.getValueType()); 6710b57cec5SDimitry Andric CurrentLocalMemUsage = alignTo(CurrentLocalMemUsage, Align); 6720b57cec5SDimitry Andric CurrentLocalMemUsage += AllocSize; 6730b57cec5SDimitry Andric break; 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric unsigned MaxOccupancy = ST.getOccupancyWithLocalMemSize(CurrentLocalMemUsage, 6790b57cec5SDimitry Andric F); 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric // Restrict local memory usage so that we don't drastically reduce occupancy, 6820b57cec5SDimitry Andric // unless it is already significantly reduced. 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric // TODO: Have some sort of hint or other heuristics to guess occupancy based 6850b57cec5SDimitry Andric // on other factors.. 6860b57cec5SDimitry Andric unsigned OccupancyHint = ST.getWavesPerEU(F).second; 6870b57cec5SDimitry Andric if (OccupancyHint == 0) 6880b57cec5SDimitry Andric OccupancyHint = 7; 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric // Clamp to max value. 6910b57cec5SDimitry Andric OccupancyHint = std::min(OccupancyHint, ST.getMaxWavesPerEU()); 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric // Check the hint but ignore it if it's obviously wrong from the existing LDS 6940b57cec5SDimitry Andric // usage. 6950b57cec5SDimitry Andric MaxOccupancy = std::min(OccupancyHint, MaxOccupancy); 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric // Round up to the next tier of usage. 6990b57cec5SDimitry Andric unsigned MaxSizeWithWaveCount 7000b57cec5SDimitry Andric = ST.getMaxLocalMemSizeWithWaveCount(MaxOccupancy, F); 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric // Program is possibly broken by using more local mem than available. 7030b57cec5SDimitry Andric if (CurrentLocalMemUsage > MaxSizeWithWaveCount) 7040b57cec5SDimitry Andric return false; 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric LocalMemLimit = MaxSizeWithWaveCount; 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << F.getName() << " uses " << CurrentLocalMemUsage 7090b57cec5SDimitry Andric << " bytes of LDS\n" 7100b57cec5SDimitry Andric << " Rounding size to " << MaxSizeWithWaveCount 7110b57cec5SDimitry Andric << " with a maximum occupancy of " << MaxOccupancy << '\n' 7120b57cec5SDimitry Andric << " and " << (LocalMemLimit - CurrentLocalMemUsage) 7130b57cec5SDimitry Andric << " available for promotion\n"); 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric return true; 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric // FIXME: Should try to pick the most likely to be profitable allocas first. 7190b57cec5SDimitry Andric bool AMDGPUPromoteAlloca::handleAlloca(AllocaInst &I, bool SufficientLDS) { 7200b57cec5SDimitry Andric // Array allocations are probably not worth handling, since an allocation of 7210b57cec5SDimitry Andric // the array type is the canonical form. 7220b57cec5SDimitry Andric if (!I.isStaticAlloca() || I.isArrayAllocation()) 7230b57cec5SDimitry Andric return false; 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric IRBuilder<> Builder(&I); 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric // First try to replace the alloca with a vector 7280b57cec5SDimitry Andric Type *AllocaTy = I.getAllocatedType(); 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to promote " << I << '\n'); 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric if (tryPromoteAllocaToVector(&I)) 7330b57cec5SDimitry Andric return true; // Promoted to vector. 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric if (DisablePromoteAllocaToLDS) 7360b57cec5SDimitry Andric return false; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric const Function &ContainingFunction = *I.getParent()->getParent(); 7390b57cec5SDimitry Andric CallingConv::ID CC = ContainingFunction.getCallingConv(); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // Don't promote the alloca to LDS for shader calling conventions as the work 7420b57cec5SDimitry Andric // item ID intrinsics are not supported for these calling conventions. 7430b57cec5SDimitry Andric // Furthermore not all LDS is available for some of the stages. 7440b57cec5SDimitry Andric switch (CC) { 7450b57cec5SDimitry Andric case CallingConv::AMDGPU_KERNEL: 7460b57cec5SDimitry Andric case CallingConv::SPIR_KERNEL: 7470b57cec5SDimitry Andric break; 7480b57cec5SDimitry Andric default: 7490b57cec5SDimitry Andric LLVM_DEBUG( 7500b57cec5SDimitry Andric dbgs() 7510b57cec5SDimitry Andric << " promote alloca to LDS not supported with calling convention.\n"); 7520b57cec5SDimitry Andric return false; 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric 7550b57cec5SDimitry Andric // Not likely to have sufficient local memory for promotion. 7560b57cec5SDimitry Andric if (!SufficientLDS) 7570b57cec5SDimitry Andric return false; 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(*TM, ContainingFunction); 7600b57cec5SDimitry Andric unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second; 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric const DataLayout &DL = Mod->getDataLayout(); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric unsigned Align = I.getAlignment(); 7650b57cec5SDimitry Andric if (Align == 0) 7660b57cec5SDimitry Andric Align = DL.getABITypeAlignment(I.getAllocatedType()); 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric // FIXME: This computed padding is likely wrong since it depends on inverse 7690b57cec5SDimitry Andric // usage order. 7700b57cec5SDimitry Andric // 7710b57cec5SDimitry Andric // FIXME: It is also possible that if we're allowed to use all of the memory 7720b57cec5SDimitry Andric // could could end up using more than the maximum due to alignment padding. 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric uint32_t NewSize = alignTo(CurrentLocalMemUsage, Align); 7750b57cec5SDimitry Andric uint32_t AllocSize = WorkGroupSize * DL.getTypeAllocSize(AllocaTy); 7760b57cec5SDimitry Andric NewSize += AllocSize; 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric if (NewSize > LocalMemLimit) { 7790b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " " << AllocSize 7800b57cec5SDimitry Andric << " bytes of local memory not available to promote\n"); 7810b57cec5SDimitry Andric return false; 7820b57cec5SDimitry Andric } 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric CurrentLocalMemUsage = NewSize; 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric std::vector<Value*> WorkList; 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric if (!collectUsesWithPtrTypes(&I, &I, WorkList)) { 7890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Do not know how to convert all uses\n"); 7900b57cec5SDimitry Andric return false; 7910b57cec5SDimitry Andric } 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Promoting alloca to local memory\n"); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric Function *F = I.getParent()->getParent(); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric Type *GVTy = ArrayType::get(I.getAllocatedType(), WorkGroupSize); 7980b57cec5SDimitry Andric GlobalVariable *GV = new GlobalVariable( 7990b57cec5SDimitry Andric *Mod, GVTy, false, GlobalValue::InternalLinkage, 8000b57cec5SDimitry Andric UndefValue::get(GVTy), 8010b57cec5SDimitry Andric Twine(F->getName()) + Twine('.') + I.getName(), 8020b57cec5SDimitry Andric nullptr, 8030b57cec5SDimitry Andric GlobalVariable::NotThreadLocal, 8040b57cec5SDimitry Andric AMDGPUAS::LOCAL_ADDRESS); 8050b57cec5SDimitry Andric GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 8068bcb0991SDimitry Andric GV->setAlignment(MaybeAlign(I.getAlignment())); 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric Value *TCntY, *TCntZ; 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric std::tie(TCntY, TCntZ) = getLocalSizeYZ(Builder); 8110b57cec5SDimitry Andric Value *TIdX = getWorkitemID(Builder, 0); 8120b57cec5SDimitry Andric Value *TIdY = getWorkitemID(Builder, 1); 8130b57cec5SDimitry Andric Value *TIdZ = getWorkitemID(Builder, 2); 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric Value *Tmp0 = Builder.CreateMul(TCntY, TCntZ, "", true, true); 8160b57cec5SDimitry Andric Tmp0 = Builder.CreateMul(Tmp0, TIdX); 8170b57cec5SDimitry Andric Value *Tmp1 = Builder.CreateMul(TIdY, TCntZ, "", true, true); 8180b57cec5SDimitry Andric Value *TID = Builder.CreateAdd(Tmp0, Tmp1); 8190b57cec5SDimitry Andric TID = Builder.CreateAdd(TID, TIdZ); 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric Value *Indices[] = { 8220b57cec5SDimitry Andric Constant::getNullValue(Type::getInt32Ty(Mod->getContext())), 8230b57cec5SDimitry Andric TID 8240b57cec5SDimitry Andric }; 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric Value *Offset = Builder.CreateInBoundsGEP(GVTy, GV, Indices); 8270b57cec5SDimitry Andric I.mutateType(Offset->getType()); 8280b57cec5SDimitry Andric I.replaceAllUsesWith(Offset); 8290b57cec5SDimitry Andric I.eraseFromParent(); 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric for (Value *V : WorkList) { 8320b57cec5SDimitry Andric CallInst *Call = dyn_cast<CallInst>(V); 8330b57cec5SDimitry Andric if (!Call) { 8340b57cec5SDimitry Andric if (ICmpInst *CI = dyn_cast<ICmpInst>(V)) { 8350b57cec5SDimitry Andric Value *Src0 = CI->getOperand(0); 8360b57cec5SDimitry Andric Type *EltTy = Src0->getType()->getPointerElementType(); 8370b57cec5SDimitry Andric PointerType *NewTy = PointerType::get(EltTy, AMDGPUAS::LOCAL_ADDRESS); 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric if (isa<ConstantPointerNull>(CI->getOperand(0))) 8400b57cec5SDimitry Andric CI->setOperand(0, ConstantPointerNull::get(NewTy)); 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric if (isa<ConstantPointerNull>(CI->getOperand(1))) 8430b57cec5SDimitry Andric CI->setOperand(1, ConstantPointerNull::get(NewTy)); 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric continue; 8460b57cec5SDimitry Andric } 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric // The operand's value should be corrected on its own and we don't want to 8490b57cec5SDimitry Andric // touch the users. 8500b57cec5SDimitry Andric if (isa<AddrSpaceCastInst>(V)) 8510b57cec5SDimitry Andric continue; 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric Type *EltTy = V->getType()->getPointerElementType(); 8540b57cec5SDimitry Andric PointerType *NewTy = PointerType::get(EltTy, AMDGPUAS::LOCAL_ADDRESS); 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric // FIXME: It doesn't really make sense to try to do this for all 8570b57cec5SDimitry Andric // instructions. 8580b57cec5SDimitry Andric V->mutateType(NewTy); 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric // Adjust the types of any constant operands. 8610b57cec5SDimitry Andric if (SelectInst *SI = dyn_cast<SelectInst>(V)) { 8620b57cec5SDimitry Andric if (isa<ConstantPointerNull>(SI->getOperand(1))) 8630b57cec5SDimitry Andric SI->setOperand(1, ConstantPointerNull::get(NewTy)); 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric if (isa<ConstantPointerNull>(SI->getOperand(2))) 8660b57cec5SDimitry Andric SI->setOperand(2, ConstantPointerNull::get(NewTy)); 8670b57cec5SDimitry Andric } else if (PHINode *Phi = dyn_cast<PHINode>(V)) { 8680b57cec5SDimitry Andric for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) { 8690b57cec5SDimitry Andric if (isa<ConstantPointerNull>(Phi->getIncomingValue(I))) 8700b57cec5SDimitry Andric Phi->setIncomingValue(I, ConstantPointerNull::get(NewTy)); 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric continue; 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric IntrinsicInst *Intr = cast<IntrinsicInst>(Call); 8780b57cec5SDimitry Andric Builder.SetInsertPoint(Intr); 8790b57cec5SDimitry Andric switch (Intr->getIntrinsicID()) { 8800b57cec5SDimitry Andric case Intrinsic::lifetime_start: 8810b57cec5SDimitry Andric case Intrinsic::lifetime_end: 8820b57cec5SDimitry Andric // These intrinsics are for address space 0 only 8830b57cec5SDimitry Andric Intr->eraseFromParent(); 8840b57cec5SDimitry Andric continue; 8850b57cec5SDimitry Andric case Intrinsic::memcpy: { 8860b57cec5SDimitry Andric MemCpyInst *MemCpy = cast<MemCpyInst>(Intr); 887*480093f4SDimitry Andric Builder.CreateMemCpy(MemCpy->getRawDest(), MemCpy->getDestAlign(), 888*480093f4SDimitry Andric MemCpy->getRawSource(), MemCpy->getSourceAlign(), 8890b57cec5SDimitry Andric MemCpy->getLength(), MemCpy->isVolatile()); 8900b57cec5SDimitry Andric Intr->eraseFromParent(); 8910b57cec5SDimitry Andric continue; 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric case Intrinsic::memmove: { 8940b57cec5SDimitry Andric MemMoveInst *MemMove = cast<MemMoveInst>(Intr); 895*480093f4SDimitry Andric Builder.CreateMemMove(MemMove->getRawDest(), MemMove->getDestAlign(), 896*480093f4SDimitry Andric MemMove->getRawSource(), MemMove->getSourceAlign(), 8970b57cec5SDimitry Andric MemMove->getLength(), MemMove->isVolatile()); 8980b57cec5SDimitry Andric Intr->eraseFromParent(); 8990b57cec5SDimitry Andric continue; 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric case Intrinsic::memset: { 9020b57cec5SDimitry Andric MemSetInst *MemSet = cast<MemSetInst>(Intr); 903*480093f4SDimitry Andric Builder.CreateMemSet( 904*480093f4SDimitry Andric MemSet->getRawDest(), MemSet->getValue(), MemSet->getLength(), 905*480093f4SDimitry Andric MaybeAlign(MemSet->getDestAlignment()), MemSet->isVolatile()); 9060b57cec5SDimitry Andric Intr->eraseFromParent(); 9070b57cec5SDimitry Andric continue; 9080b57cec5SDimitry Andric } 9090b57cec5SDimitry Andric case Intrinsic::invariant_start: 9100b57cec5SDimitry Andric case Intrinsic::invariant_end: 9110b57cec5SDimitry Andric case Intrinsic::launder_invariant_group: 9120b57cec5SDimitry Andric case Intrinsic::strip_invariant_group: 9130b57cec5SDimitry Andric Intr->eraseFromParent(); 9140b57cec5SDimitry Andric // FIXME: I think the invariant marker should still theoretically apply, 9150b57cec5SDimitry Andric // but the intrinsics need to be changed to accept pointers with any 9160b57cec5SDimitry Andric // address space. 9170b57cec5SDimitry Andric continue; 9180b57cec5SDimitry Andric case Intrinsic::objectsize: { 9190b57cec5SDimitry Andric Value *Src = Intr->getOperand(0); 9200b57cec5SDimitry Andric Type *SrcTy = Src->getType()->getPointerElementType(); 9210b57cec5SDimitry Andric Function *ObjectSize = Intrinsic::getDeclaration(Mod, 9220b57cec5SDimitry Andric Intrinsic::objectsize, 9230b57cec5SDimitry Andric { Intr->getType(), PointerType::get(SrcTy, AMDGPUAS::LOCAL_ADDRESS) } 9240b57cec5SDimitry Andric ); 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric CallInst *NewCall = Builder.CreateCall( 9270b57cec5SDimitry Andric ObjectSize, 9280b57cec5SDimitry Andric {Src, Intr->getOperand(1), Intr->getOperand(2), Intr->getOperand(3)}); 9290b57cec5SDimitry Andric Intr->replaceAllUsesWith(NewCall); 9300b57cec5SDimitry Andric Intr->eraseFromParent(); 9310b57cec5SDimitry Andric continue; 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric default: 9340b57cec5SDimitry Andric Intr->print(errs()); 9350b57cec5SDimitry Andric llvm_unreachable("Don't know how to promote alloca intrinsic use."); 9360b57cec5SDimitry Andric } 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric return true; 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric FunctionPass *llvm::createAMDGPUPromoteAlloca() { 9420b57cec5SDimitry Andric return new AMDGPUPromoteAlloca(); 9430b57cec5SDimitry Andric } 944