1//===-- AMDGPULowerIntrinsics.cpp -----------------------------------------===// 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#include "AMDGPU.h" 10#include "AMDGPUSubtarget.h" 11#include "llvm/CodeGen/TargetPassConfig.h" 12#include "llvm/Analysis/TargetTransformInfo.h" 13#include "llvm/IR/Constants.h" 14#include "llvm/IR/Instructions.h" 15#include "llvm/IR/IntrinsicInst.h" 16#include "llvm/IR/Module.h" 17#include "llvm/Transforms/Utils/LowerMemIntrinsics.h" 18 19#define DEBUG_TYPE "amdgpu-lower-intrinsics" 20 21using namespace llvm; 22 23namespace { 24 25static int MaxStaticSize; 26 27static cl::opt<int, true> MemIntrinsicExpandSizeThresholdOpt( 28 "amdgpu-mem-intrinsic-expand-size", 29 cl::desc("Set minimum mem intrinsic size to expand in IR"), 30 cl::location(MaxStaticSize), 31 cl::init(1024), 32 cl::Hidden); 33 34 35class AMDGPULowerIntrinsics : public ModulePass { 36private: 37 bool makeLIDRangeMetadata(Function &F) const; 38 39public: 40 static char ID; 41 42 AMDGPULowerIntrinsics() : ModulePass(ID) {} 43 44 bool runOnModule(Module &M) override; 45 bool expandMemIntrinsicUses(Function &F); 46 StringRef getPassName() const override { 47 return "AMDGPU Lower Intrinsics"; 48 } 49 50 void getAnalysisUsage(AnalysisUsage &AU) const override { 51 AU.addRequired<TargetTransformInfoWrapperPass>(); 52 } 53}; 54 55} 56 57char AMDGPULowerIntrinsics::ID = 0; 58 59char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID; 60 61INITIALIZE_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE, "Lower intrinsics", false, 62 false) 63 64// TODO: Should refine based on estimated number of accesses (e.g. does it 65// require splitting based on alignment) 66static bool shouldExpandOperationWithSize(Value *Size) { 67 ConstantInt *CI = dyn_cast<ConstantInt>(Size); 68 return !CI || (CI->getSExtValue() > MaxStaticSize); 69} 70 71bool AMDGPULowerIntrinsics::expandMemIntrinsicUses(Function &F) { 72 Intrinsic::ID ID = F.getIntrinsicID(); 73 bool Changed = false; 74 75 for (auto I = F.user_begin(), E = F.user_end(); I != E;) { 76 Instruction *Inst = cast<Instruction>(*I); 77 ++I; 78 79 switch (ID) { 80 case Intrinsic::memcpy: { 81 auto *Memcpy = cast<MemCpyInst>(Inst); 82 if (shouldExpandOperationWithSize(Memcpy->getLength())) { 83 Function *ParentFunc = Memcpy->getParent()->getParent(); 84 const TargetTransformInfo &TTI = 85 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*ParentFunc); 86 expandMemCpyAsLoop(Memcpy, TTI); 87 Changed = true; 88 Memcpy->eraseFromParent(); 89 } 90 91 break; 92 } 93 case Intrinsic::memmove: { 94 auto *Memmove = cast<MemMoveInst>(Inst); 95 if (shouldExpandOperationWithSize(Memmove->getLength())) { 96 expandMemMoveAsLoop(Memmove); 97 Changed = true; 98 Memmove->eraseFromParent(); 99 } 100 101 break; 102 } 103 case Intrinsic::memset: { 104 auto *Memset = cast<MemSetInst>(Inst); 105 if (shouldExpandOperationWithSize(Memset->getLength())) { 106 expandMemSetAsLoop(Memset); 107 Changed = true; 108 Memset->eraseFromParent(); 109 } 110 111 break; 112 } 113 default: 114 break; 115 } 116 } 117 118 return Changed; 119} 120 121bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const { 122 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); 123 if (!TPC) 124 return false; 125 126 const TargetMachine &TM = TPC->getTM<TargetMachine>(); 127 bool Changed = false; 128 129 for (auto *U : F.users()) { 130 auto *CI = dyn_cast<CallInst>(U); 131 if (!CI) 132 continue; 133 134 Changed |= AMDGPUSubtarget::get(TM, F).makeLIDRangeMetadata(CI); 135 } 136 return Changed; 137} 138 139bool AMDGPULowerIntrinsics::runOnModule(Module &M) { 140 bool Changed = false; 141 142 for (Function &F : M) { 143 if (!F.isDeclaration()) 144 continue; 145 146 switch (F.getIntrinsicID()) { 147 case Intrinsic::memcpy: 148 case Intrinsic::memmove: 149 case Intrinsic::memset: 150 if (expandMemIntrinsicUses(F)) 151 Changed = true; 152 break; 153 154 case Intrinsic::amdgcn_workitem_id_x: 155 case Intrinsic::r600_read_tidig_x: 156 case Intrinsic::amdgcn_workitem_id_y: 157 case Intrinsic::r600_read_tidig_y: 158 case Intrinsic::amdgcn_workitem_id_z: 159 case Intrinsic::r600_read_tidig_z: 160 case Intrinsic::r600_read_local_size_x: 161 case Intrinsic::r600_read_local_size_y: 162 case Intrinsic::r600_read_local_size_z: 163 Changed |= makeLIDRangeMetadata(F); 164 break; 165 166 default: 167 break; 168 } 169 } 170 171 return Changed; 172} 173 174ModulePass *llvm::createAMDGPULowerIntrinsicsPass() { 175 return new AMDGPULowerIntrinsics(); 176} 177