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" |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 24 | #include "llvm/IR/Module.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 | 478b819 | 2017-04-07 16:26:28 +0000 | [diff] [blame] | 35 | cl::init(2500), cl::Hidden); |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 36 | |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 37 | static cl::opt<unsigned> UnrollThresholdLocal( |
| 38 | "amdgpu-unroll-threshold-local", |
| 39 | cl::desc("Unroll threshold for AMDGPU if local memory used in a loop"), |
| 40 | cl::init(1000), cl::Hidden); |
| 41 | |
Stanislav Mekhanoshin | 478b819 | 2017-04-07 16:26:28 +0000 | [diff] [blame] | 42 | static cl::opt<unsigned> UnrollThresholdIf( |
| 43 | "amdgpu-unroll-threshold-if", |
| 44 | cl::desc("Unroll threshold increment for AMDGPU for each if statement inside loop"), |
| 45 | cl::init(150), cl::Hidden); |
| 46 | |
| 47 | static bool dependsOnLocalPhi(const Loop *L, const Value *Cond, |
| 48 | unsigned Depth = 0) { |
| 49 | const Instruction *I = dyn_cast<Instruction>(Cond); |
| 50 | if (!I) |
| 51 | return false; |
| 52 | |
| 53 | for (const Value *V : I->operand_values()) { |
| 54 | if (!L->contains(I)) |
| 55 | continue; |
| 56 | if (const PHINode *PHI = dyn_cast<PHINode>(V)) { |
| 57 | if (none_of(L->getSubLoops(), [PHI](const Loop* SubLoop) { |
| 58 | return SubLoop->contains(PHI); })) |
| 59 | return true; |
| 60 | } else if (Depth < 10 && dependsOnLocalPhi(L, V, Depth+1)) |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
Chandler Carruth | ab5cb36 | 2015-02-01 14:31:23 +0000 | [diff] [blame] | 66 | void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 67 | TTI::UnrollingPreferences &UP) { |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 68 | UP.Threshold = 300; // Twice the default. |
Tom Stellard | eea3f70 | 2015-02-05 15:32:18 +0000 | [diff] [blame] | 69 | UP.MaxCount = UINT_MAX; |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 70 | UP.Partial = true; |
| 71 | |
| 72 | // TODO: Do we want runtime unrolling? |
| 73 | |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 74 | // Maximum alloca size than can fit registers. Reserve 16 registers. |
| 75 | const unsigned MaxAlloca = (256 - 16) * 4; |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 76 | unsigned ThresholdPrivate = UnrollThresholdPrivate; |
| 77 | unsigned ThresholdLocal = UnrollThresholdLocal; |
| 78 | unsigned MaxBoost = std::max(ThresholdPrivate, ThresholdLocal); |
| 79 | AMDGPUAS ASST = ST->getAMDGPUAS(); |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 80 | for (const BasicBlock *BB : L->getBlocks()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 81 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 82 | unsigned LocalGEPsSeen = 0; |
| 83 | |
Stanislav Mekhanoshin | 478b819 | 2017-04-07 16:26:28 +0000 | [diff] [blame] | 84 | if (any_of(L->getSubLoops(), [BB](const Loop* SubLoop) { |
| 85 | return SubLoop->contains(BB); })) |
| 86 | continue; // Block belongs to an inner loop. |
| 87 | |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 88 | for (const Instruction &I : *BB) { |
Stanislav Mekhanoshin | 478b819 | 2017-04-07 16:26:28 +0000 | [diff] [blame] | 89 | |
| 90 | // Unroll a loop which contains an "if" statement whose condition |
| 91 | // defined by a PHI belonging to the loop. This may help to eliminate |
| 92 | // if region and potentially even PHI itself, saving on both divergence |
| 93 | // and registers used for the PHI. |
| 94 | // Add a small bonus for each of such "if" statements. |
| 95 | if (const BranchInst *Br = dyn_cast<BranchInst>(&I)) { |
| 96 | if (UP.Threshold < MaxBoost && Br->isConditional()) { |
| 97 | if (L->isLoopExiting(Br->getSuccessor(0)) || |
| 98 | L->isLoopExiting(Br->getSuccessor(1))) |
| 99 | continue; |
| 100 | if (dependsOnLocalPhi(L, Br->getCondition())) { |
| 101 | UP.Threshold += UnrollThresholdIf; |
| 102 | DEBUG(dbgs() << "Set unroll threshold " << UP.Threshold |
| 103 | << " for loop:\n" << *L << " due to " << *Br << '\n'); |
| 104 | if (UP.Threshold >= MaxBoost) |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | continue; |
| 109 | } |
| 110 | |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 111 | const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I); |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 112 | if (!GEP) |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 113 | continue; |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 114 | |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 115 | unsigned AS = GEP->getAddressSpace(); |
| 116 | unsigned Threshold = 0; |
| 117 | if (AS == ASST.PRIVATE_ADDRESS) |
| 118 | Threshold = ThresholdPrivate; |
| 119 | else if (AS == ASST.LOCAL_ADDRESS) |
| 120 | Threshold = ThresholdLocal; |
| 121 | else |
| 122 | continue; |
| 123 | |
| 124 | if (UP.Threshold >= Threshold) |
| 125 | continue; |
| 126 | |
| 127 | if (AS == ASST.PRIVATE_ADDRESS) { |
| 128 | const Value *Ptr = GEP->getPointerOperand(); |
| 129 | const AllocaInst *Alloca = |
| 130 | dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL)); |
| 131 | if (!Alloca || !Alloca->isStaticAlloca()) |
| 132 | continue; |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 133 | Type *Ty = Alloca->getAllocatedType(); |
| 134 | unsigned AllocaSize = Ty->isSized() ? DL.getTypeAllocSize(Ty) : 0; |
| 135 | if (AllocaSize > MaxAlloca) |
| 136 | continue; |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 137 | } else if (AS == ASST.LOCAL_ADDRESS) { |
| 138 | LocalGEPsSeen++; |
| 139 | // Inhibit unroll for local memory if we have seen addressing not to |
| 140 | // a variable, most likely we will be unable to combine it. |
| 141 | // Do not unroll too deep inner loops for local memory to give a chance |
| 142 | // to unroll an outer loop for a more important reason. |
| 143 | if (LocalGEPsSeen > 1 || L->getLoopDepth() > 2 || |
| 144 | (!isa<GlobalVariable>(GEP->getPointerOperand()) && |
| 145 | !isa<Argument>(GEP->getPointerOperand()))) |
| 146 | continue; |
| 147 | } |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 148 | |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 149 | // Check if GEP depends on a value defined by this loop itself. |
| 150 | bool HasLoopDef = false; |
| 151 | for (const Value *Op : GEP->operands()) { |
| 152 | const Instruction *Inst = dyn_cast<Instruction>(Op); |
| 153 | if (!Inst || L->isLoopInvariant(Op)) |
Stanislav Mekhanoshin | f29602d | 2017-02-03 02:20:05 +0000 | [diff] [blame] | 154 | continue; |
| 155 | |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 156 | if (any_of(L->getSubLoops(), [Inst](const Loop* SubLoop) { |
| 157 | return SubLoop->contains(Inst); })) |
| 158 | continue; |
| 159 | HasLoopDef = true; |
| 160 | break; |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 161 | } |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 162 | if (!HasLoopDef) |
| 163 | continue; |
| 164 | |
| 165 | // We want to do whatever we can to limit the number of alloca |
| 166 | // instructions that make it through to the code generator. allocas |
| 167 | // require us to use indirect addressing, which is slow and prone to |
| 168 | // compiler bugs. If this loop does an address calculation on an |
| 169 | // alloca ptr, then we want to use a higher than normal loop unroll |
| 170 | // threshold. This will give SROA a better chance to eliminate these |
| 171 | // allocas. |
| 172 | // |
| 173 | // We also want to have more unrolling for local memory to let ds |
| 174 | // instructions with different offsets combine. |
| 175 | // |
| 176 | // Don't use the maximum allowed value here as it will make some |
| 177 | // programs way too big. |
| 178 | UP.Threshold = Threshold; |
| 179 | DEBUG(dbgs() << "Set unroll threshold " << Threshold << " for loop:\n" |
| 180 | << *L << " due to " << *GEP << '\n'); |
Stanislav Mekhanoshin | 478b819 | 2017-04-07 16:26:28 +0000 | [diff] [blame] | 181 | if (UP.Threshold >= MaxBoost) |
Stanislav Mekhanoshin | baf31ac | 2017-03-28 22:13:51 +0000 | [diff] [blame] | 182 | return; |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
Matt Arsenault | 3dd43fc | 2014-07-18 06:07:13 +0000 | [diff] [blame] | 186 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 187 | unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) { |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 188 | if (Vec) |
| 189 | return 0; |
| 190 | |
| 191 | // Number of VGPRs on SI. |
| 192 | if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 193 | return 256; |
| 194 | |
| 195 | return 4 * 128; // XXX - 4 channels. Should these count as vector instead? |
| 196 | } |
| 197 | |
Matt Arsenault | 4339b3f | 2015-12-24 05:14:55 +0000 | [diff] [blame] | 198 | unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) { |
| 199 | return Vector ? 0 : 32; |
| 200 | } |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 201 | |
Volkan Keles | 1c38681 | 2016-10-03 10:31:34 +0000 | [diff] [blame] | 202 | unsigned AMDGPUTTIImpl::getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 203 | AMDGPUAS AS = ST->getAMDGPUAS(); |
| 204 | if (AddrSpace == AS.GLOBAL_ADDRESS || |
| 205 | AddrSpace == AS.CONSTANT_ADDRESS || |
| 206 | AddrSpace == AS.FLAT_ADDRESS) |
Matt Arsenault | 0994bd5 | 2016-07-01 00:56:27 +0000 | [diff] [blame] | 207 | return 128; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 208 | if (AddrSpace == AS.LOCAL_ADDRESS || |
| 209 | AddrSpace == AS.REGION_ADDRESS) |
Matt Arsenault | 0994bd5 | 2016-07-01 00:56:27 +0000 | [diff] [blame] | 210 | return 64; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 211 | if (AddrSpace == AS.PRIVATE_ADDRESS) |
Matt Arsenault | 0994bd5 | 2016-07-01 00:56:27 +0000 | [diff] [blame] | 212 | return 8 * ST->getMaxPrivateElementSize(); |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 213 | |
| 214 | if (ST->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS && |
| 215 | (AddrSpace == AS.PARAM_D_ADDRESS || |
| 216 | AddrSpace == AS.PARAM_I_ADDRESS || |
| 217 | (AddrSpace >= AS.CONSTANT_BUFFER_0 && |
| 218 | AddrSpace <= AS.CONSTANT_BUFFER_15))) |
| 219 | return 128; |
| 220 | llvm_unreachable("unhandled address space"); |
Matt Arsenault | 0994bd5 | 2016-07-01 00:56:27 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Matt Arsenault | f0a88db | 2017-02-23 03:58:53 +0000 | [diff] [blame] | 223 | bool AMDGPUTTIImpl::isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, |
| 224 | unsigned Alignment, |
| 225 | unsigned AddrSpace) const { |
| 226 | // We allow vectorization of flat stores, even though we may need to decompose |
| 227 | // them later if they may access private memory. We don't have enough context |
| 228 | // here, and legalization can handle it. |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 229 | if (AddrSpace == ST->getAMDGPUAS().PRIVATE_ADDRESS) { |
Matt Arsenault | f0a88db | 2017-02-23 03:58:53 +0000 | [diff] [blame] | 230 | return (Alignment >= 4 || ST->hasUnalignedScratchAccess()) && |
| 231 | ChainSizeInBytes <= ST->getMaxPrivateElementSize(); |
| 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool AMDGPUTTIImpl::isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, |
| 237 | unsigned Alignment, |
| 238 | unsigned AddrSpace) const { |
| 239 | return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); |
| 240 | } |
| 241 | |
| 242 | bool AMDGPUTTIImpl::isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, |
| 243 | unsigned Alignment, |
| 244 | unsigned AddrSpace) const { |
| 245 | return isLegalToVectorizeMemChain(ChainSizeInBytes, Alignment, AddrSpace); |
| 246 | } |
| 247 | |
Wei Mi | 062c744 | 2015-05-06 17:12:25 +0000 | [diff] [blame] | 248 | unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) { |
Changpeng Fang | 1be9b9f | 2017-03-09 00:07:00 +0000 | [diff] [blame] | 249 | // Disable unrolling if the loop is not vectorized. |
| 250 | if (VF == 1) |
| 251 | return 1; |
| 252 | |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 253 | // Semi-arbitrary large amount. |
| 254 | return 64; |
| 255 | } |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 256 | |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 257 | int AMDGPUTTIImpl::getArithmeticInstrCost( |
| 258 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info, |
| 259 | TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, |
Mohammed Agabaria | 2c96c43 | 2017-01-11 08:23:37 +0000 | [diff] [blame] | 260 | TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args ) { |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 261 | |
| 262 | EVT OrigTy = TLI->getValueType(DL, Ty); |
| 263 | if (!OrigTy.isSimple()) { |
| 264 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, |
| 265 | Opd1PropInfo, Opd2PropInfo); |
| 266 | } |
| 267 | |
| 268 | // Legalize the type. |
| 269 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); |
| 270 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 271 | |
| 272 | // Because we don't have any legal vector operations, but the legal types, we |
| 273 | // need to account for split vectors. |
| 274 | unsigned NElts = LT.second.isVector() ? |
| 275 | LT.second.getVectorNumElements() : 1; |
| 276 | |
| 277 | MVT::SimpleValueType SLT = LT.second.getScalarType().SimpleTy; |
| 278 | |
| 279 | switch (ISD) { |
Matt Arsenault | 8c8fcb2 | 2016-03-25 01:16:40 +0000 | [diff] [blame] | 280 | case ISD::SHL: |
| 281 | case ISD::SRL: |
| 282 | case ISD::SRA: { |
| 283 | if (SLT == MVT::i64) |
| 284 | return get64BitInstrCost() * LT.first * NElts; |
| 285 | |
| 286 | // i32 |
| 287 | return getFullRateInstrCost() * LT.first * NElts; |
| 288 | } |
| 289 | case ISD::ADD: |
| 290 | case ISD::SUB: |
| 291 | case ISD::AND: |
| 292 | case ISD::OR: |
| 293 | case ISD::XOR: { |
| 294 | if (SLT == MVT::i64){ |
| 295 | // and, or and xor are typically split into 2 VALU instructions. |
| 296 | return 2 * getFullRateInstrCost() * LT.first * NElts; |
| 297 | } |
| 298 | |
| 299 | return LT.first * NElts * getFullRateInstrCost(); |
| 300 | } |
| 301 | case ISD::MUL: { |
| 302 | const int QuarterRateCost = getQuarterRateInstrCost(); |
| 303 | if (SLT == MVT::i64) { |
| 304 | const int FullRateCost = getFullRateInstrCost(); |
| 305 | return (4 * QuarterRateCost + (2 * 2) * FullRateCost) * LT.first * NElts; |
| 306 | } |
| 307 | |
| 308 | // i32 |
| 309 | return QuarterRateCost * NElts * LT.first; |
| 310 | } |
Matt Arsenault | 9651813 | 2016-03-25 01:00:32 +0000 | [diff] [blame] | 311 | case ISD::FADD: |
| 312 | case ISD::FSUB: |
| 313 | case ISD::FMUL: |
| 314 | if (SLT == MVT::f64) |
| 315 | return LT.first * NElts * get64BitInstrCost(); |
| 316 | |
| 317 | if (SLT == MVT::f32 || SLT == MVT::f16) |
| 318 | return LT.first * NElts * getFullRateInstrCost(); |
| 319 | break; |
| 320 | |
| 321 | case ISD::FDIV: |
| 322 | case ISD::FREM: |
| 323 | // FIXME: frem should be handled separately. The fdiv in it is most of it, |
| 324 | // but the current lowering is also not entirely correct. |
| 325 | if (SLT == MVT::f64) { |
| 326 | int Cost = 4 * get64BitInstrCost() + 7 * getQuarterRateInstrCost(); |
| 327 | |
| 328 | // Add cost of workaround. |
| 329 | if (ST->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 330 | Cost += 3 * getFullRateInstrCost(); |
| 331 | |
| 332 | return LT.first * Cost * NElts; |
| 333 | } |
| 334 | |
| 335 | // Assuming no fp32 denormals lowering. |
| 336 | if (SLT == MVT::f32 || SLT == MVT::f16) { |
| 337 | assert(!ST->hasFP32Denormals() && "will change when supported"); |
| 338 | int Cost = 7 * getFullRateInstrCost() + 1 * getQuarterRateInstrCost(); |
| 339 | return LT.first * NElts * Cost; |
| 340 | } |
| 341 | |
| 342 | break; |
| 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, |
| 348 | Opd1PropInfo, Opd2PropInfo); |
| 349 | } |
| 350 | |
Matt Arsenault | e05ff15 | 2015-12-16 18:37:19 +0000 | [diff] [blame] | 351 | unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) { |
| 352 | // XXX - For some reason this isn't called for switch. |
| 353 | switch (Opcode) { |
| 354 | case Instruction::Br: |
| 355 | case Instruction::Ret: |
| 356 | return 10; |
| 357 | default: |
| 358 | return BaseT::getCFInstrCost(Opcode); |
| 359 | } |
| 360 | } |
| 361 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 362 | int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, |
| 363 | unsigned Index) { |
| 364 | switch (Opcode) { |
| 365 | case Instruction::ExtractElement: |
Matt Arsenault | 3c5e423 | 2017-05-10 21:29:33 +0000 | [diff] [blame] | 366 | case Instruction::InsertElement: { |
| 367 | unsigned EltSize |
| 368 | = DL.getTypeSizeInBits(cast<VectorType>(ValTy)->getElementType()); |
| 369 | if (EltSize < 32) { |
| 370 | if (EltSize == 16 && Index == 0 && ST->has16BitInsts()) |
| 371 | return 0; |
| 372 | return BaseT::getVectorInstrCost(Opcode, ValTy, Index); |
| 373 | } |
| 374 | |
Matt Arsenault | 59767ce | 2016-03-25 00:14:11 +0000 | [diff] [blame] | 375 | // Extracts are just reads of a subregister, so are free. Inserts are |
| 376 | // considered free because we don't want to have any cost for scalarizing |
| 377 | // operations, and we don't have to copy into a different register class. |
| 378 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 379 | // Dynamic indexing isn't free and is best avoided. |
| 380 | return Index == ~0u ? 2 : 0; |
Matt Arsenault | 3c5e423 | 2017-05-10 21:29:33 +0000 | [diff] [blame] | 381 | } |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 382 | default: |
| 383 | return BaseT::getVectorInstrCost(Opcode, ValTy, Index); |
| 384 | } |
| 385 | } |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 386 | |
Matt Arsenault | d2c8a33 | 2017-02-16 02:01:13 +0000 | [diff] [blame] | 387 | static bool isIntrinsicSourceOfDivergence(const IntrinsicInst *I) { |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 388 | switch (I->getIntrinsicID()) { |
Matt Arsenault | fe26def | 2016-02-11 05:32:51 +0000 | [diff] [blame] | 389 | case Intrinsic::amdgcn_workitem_id_x: |
| 390 | case Intrinsic::amdgcn_workitem_id_y: |
| 391 | case Intrinsic::amdgcn_workitem_id_z: |
Nicolai Haehnle | f45ea4b | 2016-12-12 16:52:19 +0000 | [diff] [blame] | 392 | case Intrinsic::amdgcn_interp_mov: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 393 | case Intrinsic::amdgcn_interp_p1: |
| 394 | case Intrinsic::amdgcn_interp_p2: |
| 395 | case Intrinsic::amdgcn_mbcnt_hi: |
| 396 | case Intrinsic::amdgcn_mbcnt_lo: |
| 397 | case Intrinsic::r600_read_tidig_x: |
| 398 | case Intrinsic::r600_read_tidig_y: |
| 399 | case Intrinsic::r600_read_tidig_z: |
Matt Arsenault | 41c1499 | 2017-01-30 17:09:47 +0000 | [diff] [blame] | 400 | case Intrinsic::amdgcn_atomic_inc: |
| 401 | case Intrinsic::amdgcn_atomic_dec: |
Nicolai Haehnle | 74127fe8 | 2016-03-14 15:37:18 +0000 | [diff] [blame] | 402 | case Intrinsic::amdgcn_image_atomic_swap: |
| 403 | case Intrinsic::amdgcn_image_atomic_add: |
| 404 | case Intrinsic::amdgcn_image_atomic_sub: |
| 405 | case Intrinsic::amdgcn_image_atomic_smin: |
| 406 | case Intrinsic::amdgcn_image_atomic_umin: |
| 407 | case Intrinsic::amdgcn_image_atomic_smax: |
| 408 | case Intrinsic::amdgcn_image_atomic_umax: |
| 409 | case Intrinsic::amdgcn_image_atomic_and: |
| 410 | case Intrinsic::amdgcn_image_atomic_or: |
| 411 | case Intrinsic::amdgcn_image_atomic_xor: |
| 412 | case Intrinsic::amdgcn_image_atomic_inc: |
| 413 | case Intrinsic::amdgcn_image_atomic_dec: |
| 414 | case Intrinsic::amdgcn_image_atomic_cmpswap: |
Nicolai Haehnle | ad63638 | 2016-03-18 16:24:31 +0000 | [diff] [blame] | 415 | case Intrinsic::amdgcn_buffer_atomic_swap: |
| 416 | case Intrinsic::amdgcn_buffer_atomic_add: |
| 417 | case Intrinsic::amdgcn_buffer_atomic_sub: |
| 418 | case Intrinsic::amdgcn_buffer_atomic_smin: |
| 419 | case Intrinsic::amdgcn_buffer_atomic_umin: |
| 420 | case Intrinsic::amdgcn_buffer_atomic_smax: |
| 421 | case Intrinsic::amdgcn_buffer_atomic_umax: |
| 422 | case Intrinsic::amdgcn_buffer_atomic_and: |
| 423 | case Intrinsic::amdgcn_buffer_atomic_or: |
| 424 | case Intrinsic::amdgcn_buffer_atomic_xor: |
| 425 | case Intrinsic::amdgcn_buffer_atomic_cmpswap: |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 426 | case Intrinsic::amdgcn_ps_live: |
Matt Arsenault | 41c1499 | 2017-01-30 17:09:47 +0000 | [diff] [blame] | 427 | case Intrinsic::amdgcn_ds_swizzle: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 428 | return true; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 429 | default: |
| 430 | return false; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | static bool isArgPassedInSGPR(const Argument *A) { |
| 435 | const Function *F = A->getParent(); |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 436 | |
| 437 | // Arguments to compute shaders are never a source of divergence. |
Matt Arsenault | 4c1ecde | 2017-04-19 17:42:34 +0000 | [diff] [blame] | 438 | CallingConv::ID CC = F->getCallingConv(); |
| 439 | switch (CC) { |
| 440 | case CallingConv::AMDGPU_KERNEL: |
| 441 | case CallingConv::SPIR_KERNEL: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 442 | return true; |
Matt Arsenault | 4c1ecde | 2017-04-19 17:42:34 +0000 | [diff] [blame] | 443 | case CallingConv::AMDGPU_VS: |
Marek Olsak | a302a736 | 2017-05-02 15:41:10 +0000 | [diff] [blame] | 444 | case CallingConv::AMDGPU_HS: |
Matt Arsenault | 4c1ecde | 2017-04-19 17:42:34 +0000 | [diff] [blame] | 445 | case CallingConv::AMDGPU_GS: |
| 446 | case CallingConv::AMDGPU_PS: |
| 447 | case CallingConv::AMDGPU_CS: |
| 448 | // For non-compute shaders, SGPR inputs are marked with either inreg or byval. |
| 449 | // Everything else is in VGPRs. |
| 450 | return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) || |
| 451 | F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal); |
| 452 | default: |
| 453 | // TODO: Should calls support inreg for SGPR inputs? |
| 454 | return false; |
| 455 | } |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// |
| 459 | /// \returns true if the result of the value could potentially be |
| 460 | /// different across workitems in a wavefront. |
| 461 | bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const { |
| 462 | |
| 463 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 464 | return !isArgPassedInSGPR(A); |
| 465 | |
| 466 | // Loads from the private address space are divergent, because threads |
| 467 | // can execute the load instruction with the same inputs and get different |
| 468 | // results. |
| 469 | // |
| 470 | // All other loads are not divergent, because if threads issue loads with the |
| 471 | // same arguments, they will always get the same result. |
| 472 | if (const LoadInst *Load = dyn_cast<LoadInst>(V)) |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 473 | return Load->getPointerAddressSpace() == ST->getAMDGPUAS().PRIVATE_ADDRESS; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 474 | |
Nicolai Haehnle | 79cad85 | 2016-03-17 16:21:59 +0000 | [diff] [blame] | 475 | // Atomics are divergent because they are executed sequentially: when an |
| 476 | // atomic operation refers to the same address in each thread, then each |
| 477 | // thread after the first sees the value written by the previous thread as |
| 478 | // original value. |
| 479 | if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V)) |
| 480 | return true; |
| 481 | |
Matt Arsenault | d2c8a33 | 2017-02-16 02:01:13 +0000 | [diff] [blame] | 482 | if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) |
| 483 | return isIntrinsicSourceOfDivergence(Intrinsic); |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 484 | |
| 485 | // Assume all function calls are a source of divergence. |
| 486 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
| 487 | return true; |
| 488 | |
| 489 | return false; |
| 490 | } |
Matt Arsenault | 3c5e423 | 2017-05-10 21:29:33 +0000 | [diff] [blame] | 491 | |
| 492 | unsigned AMDGPUTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 493 | Type *SubTp) { |
| 494 | if (ST->hasVOP3PInsts()) { |
| 495 | VectorType *VT = cast<VectorType>(Tp); |
| 496 | if (VT->getNumElements() == 2 && |
| 497 | DL.getTypeSizeInBits(VT->getElementType()) == 16) { |
| 498 | // With op_sel VOP3P instructions freely can access the low half or high |
| 499 | // half of a register, so any swizzle is free. |
| 500 | |
| 501 | switch (Kind) { |
| 502 | case TTI::SK_Broadcast: |
| 503 | case TTI::SK_Reverse: |
| 504 | case TTI::SK_PermuteSingleSrc: |
| 505 | return 0; |
| 506 | default: |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
| 513 | } |