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