Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 18 | #include "AMDGPUTargetTransformInfo.h" |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/BasicTTIImpl.h" |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Intrinsics.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 26 | #include "llvm/Target/CostTable.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetLowering.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "AMDGPUtti" |
| 31 | |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 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"), |
Stanislav Mekhanoshin | 81db531 | 2017-02-03 20:08:29 +0000 | [diff] [blame] | 35 | cl::init(2000), cl::Hidden); |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | ab5cb36 | 2015-02-01 14:31:23 +0000 | [diff] [blame] | 37 | void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 38 | TTI::UnrollingPreferences &UP) { |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 39 | UP.Threshold = 300; // Twice the default. |
Tom Stellard | eea3f70 | 2015-02-05 15:32:18 +0000 | [diff] [blame] | 40 | UP.MaxCount = UINT_MAX; |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 41 | UP.Partial = true; |
| 42 | |
| 43 | // TODO: Do we want runtime unrolling? |
| 44 | |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 45 | // Maximum alloca size than can fit registers. Reserve 16 registers. |
| 46 | const unsigned MaxAlloca = (256 - 16) * 4; |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 47 | for (const BasicBlock *BB : L->getBlocks()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 48 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 49 | for (const Instruction &I : *BB) { |
| 50 | const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I); |
Matt Arsenault | 5e2b0f5 | 2014-07-17 06:13:41 +0000 | [diff] [blame] | 51 | if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 52 | continue; |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 53 | |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 54 | const Value *Ptr = GEP->getPointerOperand(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 55 | const AllocaInst *Alloca = |
| 56 | dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL)); |
Matt Arsenault | d9cd736 | 2017-02-03 19:36:00 +0000 | [diff] [blame] | 57 | if (Alloca && Alloca->isStaticAlloca()) { |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 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 | |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 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 |
Tom Stellard | fd0d86c | 2014-02-25 21:36:21 +0000 | [diff] [blame] | 82 | // alloca ptr, then we want to use a higher than normal loop unroll |
Matt Arsenault | 5e1e431 | 2014-04-04 20:13:08 +0000 | [diff] [blame] | 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. |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 88 | UP.Threshold = UnrollThresholdPrivate; |
| 89 | return; |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
Matt Arsenault | 3dd43fc | 2014-07-18 06:07:13 +0000 | [diff] [blame] | 94 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 95 | unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) { |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 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 | |
Matt Arsenault | 4339b3f | 2015-12-24 05:14:55 +0000 | [diff] [blame] | 106 | unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) { |
| 107 | return Vector ? 0 : 32; |
| 108 | } |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 109 | |
Volkan Keles | 1c38681 | 2016-10-03 10:31:34 +0000 | [diff] [blame] | 110 | unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { |
Matt Arsenault | 0994bd5 | 2016-07-01 00:56:27 +0000 | [diff] [blame] | 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 | |
Wei Mi | 062c744 | 2015-05-06 17:12:25 +0000 | [diff] [blame] | 132 | unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) { |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 133 | // Semi-arbitrary large amount. |
| 134 | return 64; |
| 135 | } |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 136 | |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 137 | int AMDGPUTTIImpl::getArithmeticInstrCost( |
| 138 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info, |
| 139 | TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, |
Mohammed Agabaria | 2c96c43 | 2017-01-11 08:23:37 +0000 | [diff] [blame] | 140 | TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) { |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 141 | |
| 142 | EVT OrigTy = TLI->getValueType(DL, Ty); |
| 143 | if (!OrigTy.isSimple()) { |
| 144 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, |
| 145 | Opd1PropInfo, Opd2PropInfo); |
| 146 | } |
| 147 | |
| 148 | // Legalize the type. |
| 149 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); |
| 150 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 151 | |
| 152 | // Because we don't have any legal vector operations, but the legal types, we |
| 153 | // need to account for split vectors. |
| 154 | unsigned NElts = LT.second.isVector() ? |
| 155 | LT.second.getVectorNumElements() : 1; |
| 156 | |
| 157 | MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy; |
| 158 | |
| 159 | switch (ISD) { |
Matt Arsenault | 8c8fcb2 | 2016-03-25 01:16:40 +0000 | [diff] [blame] | 160 | case ISD::SHL: |
| 161 | case ISD::SRL: |
| 162 | case ISD::SRA: { |
| 163 | if (SLT == MVT::i64) |
| 164 | return get64BitInstrCost() * LT.first * NElts; |
| 165 | |
| 166 | // i32 |
| 167 | return getFullRateInstrCost() * LT.first * NElts; |
| 168 | } |
| 169 | case ISD::ADD: |
| 170 | case ISD::SUB: |
| 171 | case ISD::AND: |
| 172 | case ISD::OR: |
| 173 | case ISD::XOR: { |
| 174 | if (SLT == MVT::i64){ |
| 175 | // and, or and xor are typically split into 2 VALU instructions. |
| 176 | return 2 * getFullRateInstrCost() * LT.first * NElts; |
| 177 | } |
| 178 | |
| 179 | return LT.first * NElts * getFullRateInstrCost(); |
| 180 | } |
| 181 | case ISD::MUL: { |
| 182 | const int QuarterRateCost = getQuarterRateInstrCost(); |
| 183 | if (SLT == MVT::i64) { |
| 184 | const int FullRateCost = getFullRateInstrCost(); |
| 185 | return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts; |
| 186 | } |
| 187 | |
| 188 | // i32 |
| 189 | return QuarterRateCost * NElts * LT.first; |
| 190 | } |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 191 | case ISD::FADD: |
| 192 | case ISD::FSUB: |
| 193 | case ISD::FMUL: |
| 194 | if (SLT == MVT::f64) |
| 195 | return LT.first * NElts * get64BitInstrCost(); |
| 196 | |
| 197 | if (SLT == MVT::f32 || SLT == MVT::f16) |
| 198 | return LT.first * NElts * getFullRateInstrCost(); |
| 199 | break; |
| 200 | |
| 201 | case ISD::FDIV: |
| 202 | case ISD::FREM: |
| 203 | // FIXME: frem should be handled separately. The fdiv in it is most of it, |
| 204 | // but the current lowering is also not entirely correct. |
| 205 | if (SLT == MVT::f64) { |
| 206 | int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost(); |
| 207 | |
| 208 | // Add cost of workaround. |
| 209 | if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 210 | Cost += 3 * getFullRateInstrCost(); |
| 211 | |
| 212 | return LT.first * Cost * NElts; |
| 213 | } |
| 214 | |
| 215 | // Assuming no fp32 denormals lowering. |
| 216 | if (SLT == MVT::f32 || SLT == MVT::f16) { |
| 217 | assert(!ST->hasFP32Denormals() && "will change when supported"); |
| 218 | int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost(); |
| 219 | return LT.first * NElts * Cost; |
| 220 | } |
| 221 | |
| 222 | break; |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, |
| 228 | Opd1PropInfo, Opd2PropInfo); |
| 229 | } |
| 230 | |
Matt Arsenault | e05ff15 | 2015-12-16 18:37:19 +0000 | [diff] [blame] | 231 | unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) { |
| 232 | // XXX - For some reason this isn't called for switch. |
| 233 | switch (Opcode) { |
| 234 | case Instruction::Br: |
| 235 | case Instruction::Ret: |
| 236 | return 10; |
| 237 | default: |
| 238 | return BaseT::getCFInstrCost(Opcode); |
| 239 | } |
| 240 | } |
| 241 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 242 | int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, |
| 243 | unsigned Index) { |
| 244 | switch (Opcode) { |
| 245 | case Instruction::ExtractElement: |
Matt Arsenault | 59767ce | 2016-03-25 00:14:11 +0000 | [diff] [blame] | 246 | case Instruction::InsertElement: |
| 247 | // Extracts are just reads of a subregister, so are free. Inserts are |
| 248 | // considered free because we don't want to have any cost for scalarizing |
| 249 | // operations, and we don't have to copy into a different register class. |
| 250 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 251 | // Dynamic indexing isn't free and is best avoided. |
| 252 | return Index == ~0u ? 2 : 0; |
| 253 | default: |
| 254 | return BaseT::getVectorInstrCost(Opcode, ValTy, Index); |
| 255 | } |
| 256 | } |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 257 | |
| 258 | static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII, |
| 259 | const IntrinsicInst *I) { |
| 260 | switch (I->getIntrinsicID()) { |
| 261 | default: |
| 262 | return false; |
| 263 | case Intrinsic::not_intrinsic: |
| 264 | // This means we have an intrinsic that isn't defined in |
| 265 | // IntrinsicsAMDGPU.td |
| 266 | break; |
| 267 | |
Matt Arsenault | fe26def | 2016-02-11 05:32:51 +0000 | [diff] [blame] | 268 | case Intrinsic::amdgcn_workitem_id_x: |
| 269 | case Intrinsic::amdgcn_workitem_id_y: |
| 270 | case Intrinsic::amdgcn_workitem_id_z: |
Nicolai Haehnle | f45ea4b | 2016-12-12 16:52:19 +0000 | [diff] [blame] | 271 | case Intrinsic::amdgcn_interp_mov: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 272 | case Intrinsic::amdgcn_interp_p1: |
| 273 | case Intrinsic::amdgcn_interp_p2: |
| 274 | case Intrinsic::amdgcn_mbcnt_hi: |
| 275 | case Intrinsic::amdgcn_mbcnt_lo: |
| 276 | case Intrinsic::r600_read_tidig_x: |
| 277 | case Intrinsic::r600_read_tidig_y: |
| 278 | case Intrinsic::r600_read_tidig_z: |
Matt Arsenault | 41c1499 | 2017-01-30 17:09:47 +0000 | [diff] [blame] | 279 | case Intrinsic::amdgcn_atomic_inc: |
| 280 | case Intrinsic::amdgcn_atomic_dec: |
Nicolai Haehnle | 74127fe8 | 2016-03-14 15:37:18 +0000 | [diff] [blame] | 281 | case Intrinsic::amdgcn_image_atomic_swap: |
| 282 | case Intrinsic::amdgcn_image_atomic_add: |
| 283 | case Intrinsic::amdgcn_image_atomic_sub: |
| 284 | case Intrinsic::amdgcn_image_atomic_smin: |
| 285 | case Intrinsic::amdgcn_image_atomic_umin: |
| 286 | case Intrinsic::amdgcn_image_atomic_smax: |
| 287 | case Intrinsic::amdgcn_image_atomic_umax: |
| 288 | case Intrinsic::amdgcn_image_atomic_and: |
| 289 | case Intrinsic::amdgcn_image_atomic_or: |
| 290 | case Intrinsic::amdgcn_image_atomic_xor: |
| 291 | case Intrinsic::amdgcn_image_atomic_inc: |
| 292 | case Intrinsic::amdgcn_image_atomic_dec: |
| 293 | case Intrinsic::amdgcn_image_atomic_cmpswap: |
Nicolai Haehnle | ad63638 | 2016-03-18 16:24:31 +0000 | [diff] [blame] | 294 | case Intrinsic::amdgcn_buffer_atomic_swap: |
| 295 | case Intrinsic::amdgcn_buffer_atomic_add: |
| 296 | case Intrinsic::amdgcn_buffer_atomic_sub: |
| 297 | case Intrinsic::amdgcn_buffer_atomic_smin: |
| 298 | case Intrinsic::amdgcn_buffer_atomic_umin: |
| 299 | case Intrinsic::amdgcn_buffer_atomic_smax: |
| 300 | case Intrinsic::amdgcn_buffer_atomic_umax: |
| 301 | case Intrinsic::amdgcn_buffer_atomic_and: |
| 302 | case Intrinsic::amdgcn_buffer_atomic_or: |
| 303 | case Intrinsic::amdgcn_buffer_atomic_xor: |
| 304 | case Intrinsic::amdgcn_buffer_atomic_cmpswap: |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 305 | case Intrinsic::amdgcn_ps_live: |
Matt Arsenault | 41c1499 | 2017-01-30 17:09:47 +0000 | [diff] [blame] | 306 | case Intrinsic::amdgcn_ds_swizzle: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
| 310 | StringRef Name = I->getCalledFunction()->getName(); |
| 311 | switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) { |
| 312 | default: |
| 313 | return false; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 314 | case AMDGPUIntrinsic::SI_fs_interp: |
Nicolai Haehnle | 119d3d8 | 2016-05-02 17:37:01 +0000 | [diff] [blame] | 315 | case AMDGPUIntrinsic::SI_fs_constant: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static bool isArgPassedInSGPR(const Argument *A) { |
| 321 | const Function *F = A->getParent(); |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 322 | |
| 323 | // Arguments to compute shaders are never a source of divergence. |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 324 | if (!AMDGPU::isShader(F->getCallingConv())) |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 325 | return true; |
| 326 | |
Tom Stellard | ffc1a5a | 2015-12-19 02:54:15 +0000 | [diff] [blame] | 327 | // For non-compute shaders, SGPR inputs are marked with either inreg or byval. |
| 328 | if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) || |
| 329 | F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal)) |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 330 | return true; |
| 331 | |
Tom Stellard | ffc1a5a | 2015-12-19 02:54:15 +0000 | [diff] [blame] | 332 | // Everything else is in VGPRs. |
| 333 | return false; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /// |
| 337 | /// \returns true if the result of the value could potentially be |
| 338 | /// different across workitems in a wavefront. |
| 339 | bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const { |
| 340 | |
| 341 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 342 | return !isArgPassedInSGPR(A); |
| 343 | |
| 344 | // Loads from the private address space are divergent, because threads |
| 345 | // can execute the load instruction with the same inputs and get different |
| 346 | // results. |
| 347 | // |
| 348 | // All other loads are not divergent, because if threads issue loads with the |
| 349 | // same arguments, they will always get the same result. |
| 350 | if (const LoadInst *Load = dyn_cast<LoadInst>(V)) |
| 351 | return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS; |
| 352 | |
Nicolai Haehnle | 79cad85 | 2016-03-17 16:21:59 +0000 | [diff] [blame] | 353 | // Atomics are divergent because they are executed sequentially: when an |
| 354 | // atomic operation refers to the same address in each thread, then each |
| 355 | // thread after the first sees the value written by the previous thread as |
| 356 | // original value. |
| 357 | if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V)) |
| 358 | return true; |
| 359 | |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 360 | if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) { |
| 361 | const TargetMachine &TM = getTLI()->getTargetMachine(); |
| 362 | return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic); |
| 363 | } |
| 364 | |
| 365 | // Assume all function calls are a source of divergence. |
| 366 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
| 367 | return true; |
| 368 | |
| 369 | return false; |
| 370 | } |