1 //===-- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // \file 11 // This file implements a TargetTransformInfo analysis pass specific to the 12 // AMDGPU target machine. It uses the target's detailed information to provide 13 // more precise answers to certain TTI queries, while letting the target 14 // independent and default TTI implementations handle the rest. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "AMDGPUTargetTransformInfo.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/CodeGen/BasicTTIImpl.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/Intrinsics.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Target/CostTable.h" 27 #include "llvm/Target/TargetLowering.h" 28 using namespace llvm; 29 30 #define DEBUG_TYPE "AMDGPUtti" 31 32 static cl::opt<unsigned> UnrollThresholdPrivate( 33 "amdgpu-unroll-threshold-private", 34 cl::desc("Unroll threshold for AMDGPU if private memory used in a loop"), 35 cl::init(2000), cl::Hidden); 36 37 void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, 38 TTI::UnrollingPreferences &UP) { 39 UP.Threshold = 300; // Twice the default. 40 UP.MaxCount = UINT_MAX; 41 UP.Partial = true; 42 43 // TODO: Do we want runtime unrolling? 44 45 // Maximum alloca size than can fit registers. Reserve 16 registers. 46 const unsigned MaxAlloca = (256 - 16) * 4; 47 for (const BasicBlock *BB : L->getBlocks()) { 48 const DataLayout &DL = BB->getModule()->getDataLayout(); 49 for (const Instruction &I : *BB) { 50 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I); 51 if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) 52 continue; 53 54 const Value *Ptr = GEP->getPointerOperand(); 55 const AllocaInst *Alloca = 56 dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL)); 57 if (Alloca && Alloca->isStaticAlloca()) { 58 Type *Ty = Alloca->getAllocatedType(); 59 unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0; 60 if (AllocaSize > MaxAlloca) 61 continue; 62 63 // Check if GEP depends on a value defined by this loop itself. 64 bool HasLoopDef = false; 65 for (const Value *Op : GEP->operands()) { 66 const Instruction *Inst = dyn_cast<Instruction>(Op); 67 if (!Inst || L->isLoopInvariant(Op)) 68 continue; 69 if (any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) { 70 return SubLoop->contains(Inst); })) 71 continue; 72 HasLoopDef = true; 73 break; 74 } 75 if (!HasLoopDef) 76 continue; 77 78 // We want to do whatever we can to limit the number of alloca 79 // instructions that make it through to the code generator. allocas 80 // require us to use indirect addressing, which is slow and prone to 81 // compiler bugs. If this loop does an address calculation on an 82 // alloca ptr, then we want to use a higher than normal loop unroll 83 // threshold. This will give SROA a better chance to eliminate these 84 // allocas. 85 // 86 // Don't use the maximum allowed value here as it will make some 87 // programs way too big. 88 UP.Threshold = UnrollThresholdPrivate; 89 return; 90 } 91 } 92 } 93 } 94 95 unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) { 96 if (Vec) 97 return 0; 98 99 // Number of VGPRs on SI. 100 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) 101 return 256; 102 103 return 4 * 128; // XXX - 4 channels. Should these count as vector instead? 104 } 105 106 unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) { 107 return Vector ? 0 : 32; 108 } 109 110 unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { 111 switch (AddrSpace) { 112 case AMDGPUAS::GLOBAL_ADDRESS: 113 case AMDGPUAS::CONSTANT_ADDRESS: 114 case AMDGPUAS::FLAT_ADDRESS: 115 return 128; 116 case AMDGPUAS::LOCAL_ADDRESS: 117 case AMDGPUAS::REGION_ADDRESS: 118 return 64; 119 case AMDGPUAS::PRIVATE_ADDRESS: 120 return 8 * ST->getMaxPrivateElementSize(); 121 default: 122 if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS && 123 (AddrSpace == AMDGPUAS::PARAM_D_ADDRESS || 124 AddrSpace == AMDGPUAS::PARAM_I_ADDRESS || 125 (AddrSpace >= AMDGPUAS::CONSTANT_BUFFER_0 && 126 AddrSpace <= AMDGPUAS::CONSTANT_BUFFER_15))) 127 return 128; 128 llvm_unreachable("unhandled address space"); 129 } 130 } 131 132 bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, 133 unsigned Alignment, 134 unsigned AddrSpace) const { 135 // We allow vectorization of flat stores, even though we may need to decompose 136 // them later if they may access private memory. We don't have enough context 137 // here, and legalization can handle it. 138 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) { 139 return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) && 140 ChainSizeInBytes <= ST->getMaxPrivateElementSize(); 141 } 142 return true; 143 } 144 145 bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, 146 unsigned Alignment, 147 unsigned AddrSpace) const { 148 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); 149 } 150 151 bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, 152 unsigned Alignment, 153 unsigned AddrSpace) const { 154 return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); 155 } 156 157 unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) { 158 // Semi-arbitrary large amount. 159 return 64; 160 } 161 162 int AMDGPUTTIImpl::getArithmeticInstrCost( 163 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info, 164 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, 165 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) { 166 167 EVT OrigTy = TLI->getValueType(DL, Ty); 168 if (!OrigTy.isSimple()) { 169 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, 170 Opd1PropInfo, Opd2PropInfo); 171 } 172 173 // Legalize the type. 174 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 175 int ISD = TLI->InstructionOpcodeToISD(Opcode); 176 177 // Because we don't have any legal vector operations, but the legal types, we 178 // need to account for split vectors. 179 unsigned NElts = LT.second.isVector() ? 180 LT.second.getVectorNumElements() : 1; 181 182 MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy; 183 184 switch (ISD) { 185 case ISD::SHL: 186 case ISD::SRL: 187 case ISD::SRA: { 188 if (SLT == MVT::i64) 189 return get64BitInstrCost() * LT.first * NElts; 190 191 // i32 192 return getFullRateInstrCost() * LT.first * NElts; 193 } 194 case ISD::ADD: 195 case ISD::SUB: 196 case ISD::AND: 197 case ISD::OR: 198 case ISD::XOR: { 199 if (SLT == MVT::i64){ 200 // and, or and xor are typically split into 2 VALU instructions. 201 return 2 * getFullRateInstrCost() * LT.first * NElts; 202 } 203 204 return LT.first * NElts * getFullRateInstrCost(); 205 } 206 case ISD::MUL: { 207 const int QuarterRateCost = getQuarterRateInstrCost(); 208 if (SLT == MVT::i64) { 209 const int FullRateCost = getFullRateInstrCost(); 210 return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts; 211 } 212 213 // i32 214 return QuarterRateCost * NElts * LT.first; 215 } 216 case ISD::FADD: 217 case ISD::FSUB: 218 case ISD::FMUL: 219 if (SLT == MVT::f64) 220 return LT.first * NElts * get64BitInstrCost(); 221 222 if (SLT == MVT::f32 || SLT == MVT::f16) 223 return LT.first * NElts * getFullRateInstrCost(); 224 break; 225 226 case ISD::FDIV: 227 case ISD::FREM: 228 // FIXME: frem should be handled separately. The fdiv in it is most of it, 229 // but the current lowering is also not entirely correct. 230 if (SLT == MVT::f64) { 231 int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost(); 232 233 // Add cost of workaround. 234 if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) 235 Cost += 3 * getFullRateInstrCost(); 236 237 return LT.first * Cost * NElts; 238 } 239 240 // Assuming no fp32 denormals lowering. 241 if (SLT == MVT::f32 || SLT == MVT::f16) { 242 assert(!ST->hasFP32Denormals() && "will change when supported"); 243 int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost(); 244 return LT.first * NElts * Cost; 245 } 246 247 break; 248 default: 249 break; 250 } 251 252 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, 253 Opd1PropInfo, Opd2PropInfo); 254 } 255 256 unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) { 257 // XXX - For some reason this isn't called for switch. 258 switch (Opcode) { 259 case Instruction::Br: 260 case Instruction::Ret: 261 return 10; 262 default: 263 return BaseT::getCFInstrCost(Opcode); 264 } 265 } 266 267 int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, 268 unsigned Index) { 269 switch (Opcode) { 270 case Instruction::ExtractElement: 271 case Instruction::InsertElement: 272 // Extracts are just reads of a subregister, so are free. Inserts are 273 // considered free because we don't want to have any cost for scalarizing 274 // operations, and we don't have to copy into a different register class. 275 276 // Dynamic indexing isn't free and is best avoided. 277 return Index == ~0u ? 2 : 0; 278 default: 279 return BaseT::getVectorInstrCost(Opcode, ValTy, Index); 280 } 281 } 282 283 static bool isIntrinsicSourceOfDivergence(const IntrinsicInst *I) { 284 switch (I->getIntrinsicID()) { 285 case Intrinsic::amdgcn_workitem_id_x: 286 case Intrinsic::amdgcn_workitem_id_y: 287 case Intrinsic::amdgcn_workitem_id_z: 288 case Intrinsic::amdgcn_interp_mov: 289 case Intrinsic::amdgcn_interp_p1: 290 case Intrinsic::amdgcn_interp_p2: 291 case Intrinsic::amdgcn_mbcnt_hi: 292 case Intrinsic::amdgcn_mbcnt_lo: 293 case Intrinsic::r600_read_tidig_x: 294 case Intrinsic::r600_read_tidig_y: 295 case Intrinsic::r600_read_tidig_z: 296 case Intrinsic::amdgcn_atomic_inc: 297 case Intrinsic::amdgcn_atomic_dec: 298 case Intrinsic::amdgcn_image_atomic_swap: 299 case Intrinsic::amdgcn_image_atomic_add: 300 case Intrinsic::amdgcn_image_atomic_sub: 301 case Intrinsic::amdgcn_image_atomic_smin: 302 case Intrinsic::amdgcn_image_atomic_umin: 303 case Intrinsic::amdgcn_image_atomic_smax: 304 case Intrinsic::amdgcn_image_atomic_umax: 305 case Intrinsic::amdgcn_image_atomic_and: 306 case Intrinsic::amdgcn_image_atomic_or: 307 case Intrinsic::amdgcn_image_atomic_xor: 308 case Intrinsic::amdgcn_image_atomic_inc: 309 case Intrinsic::amdgcn_image_atomic_dec: 310 case Intrinsic::amdgcn_image_atomic_cmpswap: 311 case Intrinsic::amdgcn_buffer_atomic_swap: 312 case Intrinsic::amdgcn_buffer_atomic_add: 313 case Intrinsic::amdgcn_buffer_atomic_sub: 314 case Intrinsic::amdgcn_buffer_atomic_smin: 315 case Intrinsic::amdgcn_buffer_atomic_umin: 316 case Intrinsic::amdgcn_buffer_atomic_smax: 317 case Intrinsic::amdgcn_buffer_atomic_umax: 318 case Intrinsic::amdgcn_buffer_atomic_and: 319 case Intrinsic::amdgcn_buffer_atomic_or: 320 case Intrinsic::amdgcn_buffer_atomic_xor: 321 case Intrinsic::amdgcn_buffer_atomic_cmpswap: 322 case Intrinsic::amdgcn_ps_live: 323 case Intrinsic::amdgcn_ds_swizzle: 324 return true; 325 default: 326 return false; 327 } 328 } 329 330 static bool isArgPassedInSGPR(const Argument *A) { 331 const Function *F = A->getParent(); 332 333 // Arguments to compute shaders are never a source of divergence. 334 if (!AMDGPU::isShader(F->getCallingConv())) 335 return true; 336 337 // For non-compute shaders, SGPR inputs are marked with either inreg or byval. 338 if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) || 339 F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal)) 340 return true; 341 342 // Everything else is in VGPRs. 343 return false; 344 } 345 346 /// 347 /// \returns true if the result of the value could potentially be 348 /// different across workitems in a wavefront. 349 bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const { 350 351 if (const Argument *A = dyn_cast<Argument>(V)) 352 return !isArgPassedInSGPR(A); 353 354 // Loads from the private address space are divergent, because threads 355 // can execute the load instruction with the same inputs and get different 356 // results. 357 // 358 // All other loads are not divergent, because if threads issue loads with the 359 // same arguments, they will always get the same result. 360 if (const LoadInst *Load = dyn_cast<LoadInst>(V)) 361 return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS; 362 363 // Atomics are divergent because they are executed sequentially: when an 364 // atomic operation refers to the same address in each thread, then each 365 // thread after the first sees the value written by the previous thread as 366 // original value. 367 if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V)) 368 return true; 369 370 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) 371 return isIntrinsicSourceOfDivergence(Intrinsic); 372 373 // Assume all function calls are a source of divergence. 374 if (isa<CallInst>(V) || isa<InvokeInst>(V)) 375 return true; 376 377 return false; 378 } 379