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 | |
Chandler Carruth | ab5cb36 | 2015-02-01 14:31:23 +0000 | [diff] [blame] | 32 | void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 33 | TTI::UnrollingPreferences &UP) { |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 34 | UP.Threshold = 300; // Twice the default. |
Tom Stellard | eea3f70 | 2015-02-05 15:32:18 +0000 | [diff] [blame] | 35 | UP.MaxCount = UINT_MAX; |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 36 | UP.Partial = true; |
| 37 | |
| 38 | // TODO: Do we want runtime unrolling? |
| 39 | |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 40 | for (const BasicBlock *BB : L->getBlocks()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 41 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 42 | for (const Instruction &I : *BB) { |
| 43 | const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I); |
Matt Arsenault | 5e2b0f5 | 2014-07-17 06:13:41 +0000 | [diff] [blame] | 44 | if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 45 | continue; |
Matt Arsenault | ac6e39c | 2014-07-17 06:19:06 +0000 | [diff] [blame] | 46 | |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 47 | const Value *Ptr = GEP->getPointerOperand(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 48 | const AllocaInst *Alloca = |
| 49 | dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL)); |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 50 | if (Alloca) { |
| 51 | // We want to do whatever we can to limit the number of alloca |
| 52 | // instructions that make it through to the code generator. allocas |
| 53 | // require us to use indirect addressing, which is slow and prone to |
| 54 | // compiler bugs. If this loop does an address calculation on an |
Tom Stellard | fd0d86c | 2014-02-25 21:36:21 +0000 | [diff] [blame] | 55 | // 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] | 56 | // threshold. This will give SROA a better chance to eliminate these |
| 57 | // allocas. |
| 58 | // |
| 59 | // Don't use the maximum allowed value here as it will make some |
| 60 | // programs way too big. |
Matt Arsenault | c824458 | 2014-07-25 23:02:42 +0000 | [diff] [blame] | 61 | UP.Threshold = 800; |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
Matt Arsenault | 3dd43fc | 2014-07-18 06:07:13 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 67 | unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) { |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 68 | if (Vec) |
| 69 | return 0; |
| 70 | |
| 71 | // Number of VGPRs on SI. |
| 72 | if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 73 | return 256; |
| 74 | |
| 75 | return 4 * 128; // XXX - 4 channels. Should these count as vector instead? |
| 76 | } |
| 77 | |
Matt Arsenault | 4339b3f | 2015-12-24 05:14:55 +0000 | [diff] [blame] | 78 | unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool Vector) { |
| 79 | return Vector ? 0 : 32; |
| 80 | } |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 81 | |
Wei Mi | 062c744 | 2015-05-06 17:12:25 +0000 | [diff] [blame] | 82 | unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) { |
Matt Arsenault | a93441f | 2014-07-19 18:15:16 +0000 | [diff] [blame] | 83 | // Semi-arbitrary large amount. |
| 84 | return 64; |
| 85 | } |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 86 | |
Matt Arsenault | e05ff15 | 2015-12-16 18:37:19 +0000 | [diff] [blame] | 87 | unsigned AMDGPUTTIImpl::getCFInstrCost(unsigned Opcode) { |
| 88 | // XXX - For some reason this isn't called for switch. |
| 89 | switch (Opcode) { |
| 90 | case Instruction::Br: |
| 91 | case Instruction::Ret: |
| 92 | return 10; |
| 93 | default: |
| 94 | return BaseT::getCFInstrCost(Opcode); |
| 95 | } |
| 96 | } |
| 97 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 98 | int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, |
| 99 | unsigned Index) { |
| 100 | switch (Opcode) { |
| 101 | case Instruction::ExtractElement: |
Matt Arsenault | 59767ce | 2016-03-25 00:14:11 +0000 | [diff] [blame^] | 102 | case Instruction::InsertElement: |
| 103 | // Extracts are just reads of a subregister, so are free. Inserts are |
| 104 | // considered free because we don't want to have any cost for scalarizing |
| 105 | // operations, and we don't have to copy into a different register class. |
| 106 | |
Matt Arsenault | e830f54 | 2015-12-01 19:08:39 +0000 | [diff] [blame] | 107 | // Dynamic indexing isn't free and is best avoided. |
| 108 | return Index == ~0u ? 2 : 0; |
| 109 | default: |
| 110 | return BaseT::getVectorInstrCost(Opcode, ValTy, Index); |
| 111 | } |
| 112 | } |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 113 | |
| 114 | static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII, |
| 115 | const IntrinsicInst *I) { |
| 116 | switch (I->getIntrinsicID()) { |
| 117 | default: |
| 118 | return false; |
| 119 | case Intrinsic::not_intrinsic: |
| 120 | // This means we have an intrinsic that isn't defined in |
| 121 | // IntrinsicsAMDGPU.td |
| 122 | break; |
| 123 | |
Matt Arsenault | fe26def | 2016-02-11 05:32:51 +0000 | [diff] [blame] | 124 | case Intrinsic::amdgcn_workitem_id_x: |
| 125 | case Intrinsic::amdgcn_workitem_id_y: |
| 126 | case Intrinsic::amdgcn_workitem_id_z: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 127 | case Intrinsic::amdgcn_interp_p1: |
| 128 | case Intrinsic::amdgcn_interp_p2: |
| 129 | case Intrinsic::amdgcn_mbcnt_hi: |
| 130 | case Intrinsic::amdgcn_mbcnt_lo: |
| 131 | case Intrinsic::r600_read_tidig_x: |
| 132 | case Intrinsic::r600_read_tidig_y: |
| 133 | case Intrinsic::r600_read_tidig_z: |
Nicolai Haehnle | 74127fe8 | 2016-03-14 15:37:18 +0000 | [diff] [blame] | 134 | case Intrinsic::amdgcn_image_atomic_swap: |
| 135 | case Intrinsic::amdgcn_image_atomic_add: |
| 136 | case Intrinsic::amdgcn_image_atomic_sub: |
| 137 | case Intrinsic::amdgcn_image_atomic_smin: |
| 138 | case Intrinsic::amdgcn_image_atomic_umin: |
| 139 | case Intrinsic::amdgcn_image_atomic_smax: |
| 140 | case Intrinsic::amdgcn_image_atomic_umax: |
| 141 | case Intrinsic::amdgcn_image_atomic_and: |
| 142 | case Intrinsic::amdgcn_image_atomic_or: |
| 143 | case Intrinsic::amdgcn_image_atomic_xor: |
| 144 | case Intrinsic::amdgcn_image_atomic_inc: |
| 145 | case Intrinsic::amdgcn_image_atomic_dec: |
| 146 | case Intrinsic::amdgcn_image_atomic_cmpswap: |
Nicolai Haehnle | ad63638 | 2016-03-18 16:24:31 +0000 | [diff] [blame] | 147 | case Intrinsic::amdgcn_buffer_atomic_swap: |
| 148 | case Intrinsic::amdgcn_buffer_atomic_add: |
| 149 | case Intrinsic::amdgcn_buffer_atomic_sub: |
| 150 | case Intrinsic::amdgcn_buffer_atomic_smin: |
| 151 | case Intrinsic::amdgcn_buffer_atomic_umin: |
| 152 | case Intrinsic::amdgcn_buffer_atomic_smax: |
| 153 | case Intrinsic::amdgcn_buffer_atomic_umax: |
| 154 | case Intrinsic::amdgcn_buffer_atomic_and: |
| 155 | case Intrinsic::amdgcn_buffer_atomic_or: |
| 156 | case Intrinsic::amdgcn_buffer_atomic_xor: |
| 157 | case Intrinsic::amdgcn_buffer_atomic_cmpswap: |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
| 161 | StringRef Name = I->getCalledFunction()->getName(); |
| 162 | switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) { |
| 163 | default: |
| 164 | return false; |
| 165 | case AMDGPUIntrinsic::SI_tid: |
| 166 | case AMDGPUIntrinsic::SI_fs_interp: |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static bool isArgPassedInSGPR(const Argument *A) { |
| 172 | const Function *F = A->getParent(); |
| 173 | unsigned ShaderType = AMDGPU::getShaderType(*F); |
| 174 | |
| 175 | // Arguments to compute shaders are never a source of divergence. |
| 176 | if (ShaderType == ShaderType::COMPUTE) |
| 177 | return true; |
| 178 | |
Tom Stellard | ffc1a5a | 2015-12-19 02:54:15 +0000 | [diff] [blame] | 179 | // For non-compute shaders, SGPR inputs are marked with either inreg or byval. |
| 180 | if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) || |
| 181 | F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal)) |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 182 | return true; |
| 183 | |
Tom Stellard | ffc1a5a | 2015-12-19 02:54:15 +0000 | [diff] [blame] | 184 | // Everything else is in VGPRs. |
| 185 | return false; |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /// |
| 189 | /// \returns true if the result of the value could potentially be |
| 190 | /// different across workitems in a wavefront. |
| 191 | bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const { |
| 192 | |
| 193 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 194 | return !isArgPassedInSGPR(A); |
| 195 | |
| 196 | // Loads from the private address space are divergent, because threads |
| 197 | // can execute the load instruction with the same inputs and get different |
| 198 | // results. |
| 199 | // |
| 200 | // All other loads are not divergent, because if threads issue loads with the |
| 201 | // same arguments, they will always get the same result. |
| 202 | if (const LoadInst *Load = dyn_cast<LoadInst>(V)) |
| 203 | return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS; |
| 204 | |
Nicolai Haehnle | 79cad85 | 2016-03-17 16:21:59 +0000 | [diff] [blame] | 205 | // Atomics are divergent because they are executed sequentially: when an |
| 206 | // atomic operation refers to the same address in each thread, then each |
| 207 | // thread after the first sees the value written by the previous thread as |
| 208 | // original value. |
| 209 | if (isa<AtomicRMWInst>(V) || isa<AtomicCmpXchgInst>(V)) |
| 210 | return true; |
| 211 | |
Tom Stellard | dbe374b | 2015-12-15 18:04:38 +0000 | [diff] [blame] | 212 | if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) { |
| 213 | const TargetMachine &TM = getTLI()->getTargetMachine(); |
| 214 | return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic); |
| 215 | } |
| 216 | |
| 217 | // Assume all function calls are a source of divergence. |
| 218 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
| 219 | return true; |
| 220 | |
| 221 | return false; |
| 222 | } |