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