Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- AMDGPUSubtarget.cpp - AMDGPU Subtarget Information ----------------===// |
| 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 | /// \brief Implements the AMDGPU specific subclass of TargetSubtarget. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AMDGPUSubtarget.h" |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 16 | #include "SIMachineFunctionInfo.h" |
Matt Arsenault | d9a23ab | 2014-07-13 02:08:26 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Tom Stellard | 83f0bce | 2015-01-29 16:55:25 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineScheduler.h" |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 19 | #include "llvm/IR/MDBuilder.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetFrameLowering.h" |
| 21 | #include <algorithm> |
Matt Arsenault | d9a23ab | 2014-07-13 02:08:26 +0000 | [diff] [blame] | 22 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "amdgpu-subtarget" |
| 26 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 27 | #define GET_SUBTARGETINFO_TARGET_DESC |
| 28 | #define GET_SUBTARGETINFO_CTOR |
| 29 | #include "AMDGPUGenSubtargetInfo.inc" |
| 30 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 31 | AMDGPUSubtarget::~AMDGPUSubtarget() = default; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 32 | |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 33 | AMDGPUSubtarget & |
Daniel Sanders | a73f1fd | 2015-06-10 12:11:26 +0000 | [diff] [blame] | 34 | AMDGPUSubtarget::initializeSubtargetDependencies(const Triple &TT, |
| 35 | StringRef GPU, StringRef FS) { |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 36 | // Determine default and user-specified characteristics |
Matt Arsenault | f171cf2 | 2014-07-14 23:40:49 +0000 | [diff] [blame] | 37 | // On SI+, we want FP64 denormals to be on by default. FP32 denormals can be |
| 38 | // enabled, but some instructions do not respect them and they run at the |
| 39 | // double precision rate, so don't enable by default. |
| 40 | // |
| 41 | // We want to be able to turn these off, but making this a subtarget feature |
| 42 | // for SI has the unhelpful behavior that it unsets everything else if you |
| 43 | // disable it. |
Matt Arsenault | d9a23ab | 2014-07-13 02:08:26 +0000 | [diff] [blame] | 44 | |
Matt Arsenault | 2fdf2a1 | 2017-02-21 23:35:48 +0000 | [diff] [blame] | 45 | SmallString<256> FullFS("+promote-alloca,+fp64-fp16-denormals,+dx10-clamp,+load-store-opt,"); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 46 | if (isAmdHsaOS()) // Turn on FlatForGlobal for HSA. |
Wei Ding | 205bfdb | 2017-02-10 02:15:29 +0000 | [diff] [blame] | 47 | FullFS += "+flat-for-global,+unaligned-buffer-access,+trap-handler,"; |
Matt Arsenault | a6867fd | 2017-01-23 22:31:03 +0000 | [diff] [blame] | 48 | |
Matt Arsenault | d9a23ab | 2014-07-13 02:08:26 +0000 | [diff] [blame] | 49 | FullFS += FS; |
| 50 | |
| 51 | ParseSubtargetFeatures(GPU, FullFS); |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 52 | |
Matt Arsenault | d8f7ea3 | 2017-01-27 17:42:26 +0000 | [diff] [blame] | 53 | // Unless +-flat-for-global is specified, turn on FlatForGlobal for all OS-es |
| 54 | // on VI and newer hardware to avoid assertion failures due to missing ADDR64 |
| 55 | // variants of MUBUF instructions. |
| 56 | if (!hasAddr64() && !FS.contains("flat-for-global")) { |
| 57 | FlatForGlobal = true; |
| 58 | } |
| 59 | |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 60 | // FIXME: I don't think think Evergreen has any useful support for |
| 61 | // denormals, but should be checked. Should we issue a warning somewhere |
| 62 | // if someone tries to enable these? |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 63 | if (getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) { |
Matt Arsenault | a6867fd | 2017-01-23 22:31:03 +0000 | [diff] [blame] | 64 | FP64FP16Denormals = false; |
Matt Arsenault | f171cf2 | 2014-07-14 23:40:49 +0000 | [diff] [blame] | 65 | FP32Denormals = false; |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 66 | } |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 67 | |
| 68 | // Set defaults if needed. |
| 69 | if (MaxPrivateElementSize == 0) |
Matt Arsenault | e8ed8e5 | 2016-05-11 00:28:54 +0000 | [diff] [blame] | 70 | MaxPrivateElementSize = 4; |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 71 | |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 72 | return *this; |
| 73 | } |
| 74 | |
Daniel Sanders | a73f1fd | 2015-06-10 12:11:26 +0000 | [diff] [blame] | 75 | AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS, |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 76 | const TargetMachine &TM) |
| 77 | : AMDGPUGenSubtargetInfo(TT, GPU, FS), |
| 78 | TargetTriple(TT), |
| 79 | Gen(TT.getArch() == Triple::amdgcn ? SOUTHERN_ISLANDS : R600), |
| 80 | IsaVersion(ISAVersion0_0_0), |
| 81 | WavefrontSize(64), |
| 82 | LocalMemorySize(0), |
| 83 | LDSBankCount(0), |
| 84 | MaxPrivateElementSize(0), |
Tom Stellard | 40ce8af | 2015-01-28 16:04:26 +0000 | [diff] [blame] | 85 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 86 | FastFMAF32(false), |
| 87 | HalfRate64Ops(false), |
| 88 | |
| 89 | FP32Denormals(false), |
Matt Arsenault | a6867fd | 2017-01-23 22:31:03 +0000 | [diff] [blame] | 90 | FP64FP16Denormals(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 91 | FPExceptions(false), |
Matt Arsenault | 2fdf2a1 | 2017-02-21 23:35:48 +0000 | [diff] [blame] | 92 | DX10Clamp(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 93 | FlatForGlobal(false), |
Tom Stellard | 64a9d08 | 2016-10-14 18:10:39 +0000 | [diff] [blame] | 94 | UnalignedScratchAccess(false), |
Matt Arsenault | 7f681ac | 2016-07-01 23:03:44 +0000 | [diff] [blame] | 95 | UnalignedBufferAccess(false), |
| 96 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 97 | HasApertureRegs(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 98 | EnableXNACK(false), |
Wei Ding | 205bfdb | 2017-02-10 02:15:29 +0000 | [diff] [blame] | 99 | TrapHandler(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 100 | DebuggerInsertNops(false), |
| 101 | DebuggerReserveRegs(false), |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 102 | DebuggerEmitPrologue(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 103 | |
| 104 | EnableVGPRSpilling(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 105 | EnablePromoteAlloca(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 106 | EnableLoadStoreOpt(false), |
| 107 | EnableUnsafeDSOffsetFolding(false), |
| 108 | EnableSIScheduler(false), |
| 109 | DumpCode(false), |
| 110 | |
| 111 | FP64(false), |
| 112 | IsGCN(false), |
| 113 | GCN1Encoding(false), |
| 114 | GCN3Encoding(false), |
| 115 | CIInsts(false), |
Matt Arsenault | 2021f08 | 2017-02-18 19:12:26 +0000 | [diff] [blame] | 116 | GFX9Insts(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 117 | SGPRInitBug(false), |
| 118 | HasSMemRealTime(false), |
| 119 | Has16BitInsts(false), |
Matt Arsenault | 9be7b0d | 2017-02-27 18:49:11 +0000 | [diff] [blame] | 120 | HasVOP3PInsts(false), |
Matt Arsenault | cc88ce3 | 2016-10-12 18:00:51 +0000 | [diff] [blame] | 121 | HasMovrel(false), |
| 122 | HasVGPRIndexMode(false), |
Matt Arsenault | c88ba36 | 2016-10-29 04:05:06 +0000 | [diff] [blame] | 123 | HasScalarStores(false), |
Benjamin Kramer | 11590b8 | 2017-01-20 10:37:53 +0000 | [diff] [blame] | 124 | HasInv2PiInlineImm(false), |
Sam Kolton | 07dbde2 | 2017-01-20 10:01:25 +0000 | [diff] [blame] | 125 | HasSDWA(false), |
| 126 | HasDPP(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 127 | FlatAddressSpace(false), |
Matt Arsenault | acdc765 | 2017-05-10 21:19:05 +0000 | [diff] [blame^] | 128 | FlatInstOffsets(false), |
| 129 | FlatGlobalInsts(false), |
| 130 | FlatScratchInsts(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 131 | |
| 132 | R600ALUInst(false), |
| 133 | CaymanISA(false), |
| 134 | CFALUBug(false), |
| 135 | HasVertexCache(false), |
| 136 | TexVTXClauseSize(0), |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 137 | ScalarizeGlobal(false), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 138 | |
| 139 | FeatureDisable(false), |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 140 | InstrItins(getInstrItineraryForCPU(GPU)) { |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 141 | AS = AMDGPU::getAMDGPUAS(TT); |
Tom Stellard | 40ce8af | 2015-01-28 16:04:26 +0000 | [diff] [blame] | 142 | initializeSubtargetDependencies(TT, GPU, FS); |
Tom Stellard | a40f971 | 2014-01-22 21:55:43 +0000 | [diff] [blame] | 143 | } |
Tom Stellard | b8fd6ef | 2014-12-02 22:00:07 +0000 | [diff] [blame] | 144 | |
Stanislav Mekhanoshin | 2b913b1 | 2017-02-01 22:59:50 +0000 | [diff] [blame] | 145 | unsigned AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves, |
| 146 | const Function &F) const { |
| 147 | if (NWaves == 1) |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 148 | return getLocalMemorySize(); |
Stanislav Mekhanoshin | 2b913b1 | 2017-02-01 22:59:50 +0000 | [diff] [blame] | 149 | unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second; |
| 150 | unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize); |
| 151 | unsigned MaxWaves = getMaxWavesPerEU(); |
| 152 | return getLocalMemorySize() * MaxWaves / WorkGroupsPerCu / NWaves; |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Stanislav Mekhanoshin | 2b913b1 | 2017-02-01 22:59:50 +0000 | [diff] [blame] | 155 | unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes, |
| 156 | const Function &F) const { |
| 157 | unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second; |
| 158 | unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize); |
| 159 | unsigned MaxWaves = getMaxWavesPerEU(); |
| 160 | unsigned Limit = getLocalMemorySize() * MaxWaves / WorkGroupsPerCu; |
| 161 | unsigned NumWaves = Limit / (Bytes ? Bytes : 1u); |
| 162 | NumWaves = std::min(NumWaves, MaxWaves); |
| 163 | NumWaves = std::max(NumWaves, 1u); |
| 164 | return NumWaves; |
Matt Arsenault | 8a028bf | 2016-05-16 21:19:59 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 167 | std::pair<unsigned, unsigned> AMDGPUSubtarget::getFlatWorkGroupSizes( |
| 168 | const Function &F) const { |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 169 | // Default minimum/maximum flat work group sizes. |
| 170 | std::pair<unsigned, unsigned> Default = |
| 171 | AMDGPU::isCompute(F.getCallingConv()) ? |
| 172 | std::pair<unsigned, unsigned>(getWavefrontSize() * 2, |
| 173 | getWavefrontSize() * 4) : |
| 174 | std::pair<unsigned, unsigned>(1, getWavefrontSize()); |
| 175 | |
| 176 | // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa |
| 177 | // starts using "amdgpu-flat-work-group-size" attribute. |
| 178 | Default.second = AMDGPU::getIntegerAttribute( |
| 179 | F, "amdgpu-max-work-group-size", Default.second); |
| 180 | Default.first = std::min(Default.first, Default.second); |
| 181 | |
| 182 | // Requested minimum/maximum flat work group sizes. |
| 183 | std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute( |
| 184 | F, "amdgpu-flat-work-group-size", Default); |
| 185 | |
| 186 | // Make sure requested minimum is less than requested maximum. |
| 187 | if (Requested.first > Requested.second) |
| 188 | return Default; |
| 189 | |
| 190 | // Make sure requested values do not violate subtarget's specifications. |
| 191 | if (Requested.first < getMinFlatWorkGroupSize()) |
| 192 | return Default; |
| 193 | if (Requested.second > getMaxFlatWorkGroupSize()) |
| 194 | return Default; |
| 195 | |
| 196 | return Requested; |
| 197 | } |
| 198 | |
| 199 | std::pair<unsigned, unsigned> AMDGPUSubtarget::getWavesPerEU( |
| 200 | const Function &F) const { |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 201 | // Default minimum/maximum number of waves per execution unit. |
Konstantin Zhuravlyov | fd87137 | 2017-02-09 21:33:23 +0000 | [diff] [blame] | 202 | std::pair<unsigned, unsigned> Default(1, getMaxWavesPerEU()); |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 203 | |
| 204 | // Default/requested minimum/maximum flat work group sizes. |
| 205 | std::pair<unsigned, unsigned> FlatWorkGroupSizes = getFlatWorkGroupSizes(F); |
| 206 | |
| 207 | // If minimum/maximum flat work group sizes were explicitly requested using |
| 208 | // "amdgpu-flat-work-group-size" attribute, then set default minimum/maximum |
| 209 | // number of waves per execution unit to values implied by requested |
| 210 | // minimum/maximum flat work group sizes. |
| 211 | unsigned MinImpliedByFlatWorkGroupSize = |
| 212 | getMaxWavesPerEU(FlatWorkGroupSizes.second); |
| 213 | bool RequestedFlatWorkGroupSize = false; |
| 214 | |
| 215 | // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa |
| 216 | // starts using "amdgpu-flat-work-group-size" attribute. |
| 217 | if (F.hasFnAttribute("amdgpu-max-work-group-size") || |
| 218 | F.hasFnAttribute("amdgpu-flat-work-group-size")) { |
| 219 | Default.first = MinImpliedByFlatWorkGroupSize; |
| 220 | RequestedFlatWorkGroupSize = true; |
| 221 | } |
| 222 | |
| 223 | // Requested minimum/maximum number of waves per execution unit. |
| 224 | std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute( |
| 225 | F, "amdgpu-waves-per-eu", Default, true); |
| 226 | |
| 227 | // Make sure requested minimum is less than requested maximum. |
| 228 | if (Requested.second && Requested.first > Requested.second) |
| 229 | return Default; |
| 230 | |
| 231 | // Make sure requested values do not violate subtarget's specifications. |
| 232 | if (Requested.first < getMinWavesPerEU() || |
| 233 | Requested.first > getMaxWavesPerEU()) |
| 234 | return Default; |
| 235 | if (Requested.second > getMaxWavesPerEU()) |
| 236 | return Default; |
| 237 | |
| 238 | // Make sure requested values are compatible with values implied by requested |
| 239 | // minimum/maximum flat work group sizes. |
| 240 | if (RequestedFlatWorkGroupSize && |
| 241 | Requested.first > MinImpliedByFlatWorkGroupSize) |
| 242 | return Default; |
| 243 | |
| 244 | return Requested; |
| 245 | } |
| 246 | |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 247 | bool AMDGPUSubtarget::makeLIDRangeMetadata(Instruction *I) const { |
| 248 | Function *Kernel = I->getParent()->getParent(); |
| 249 | unsigned MinSize = 0; |
| 250 | unsigned MaxSize = getFlatWorkGroupSizes(*Kernel).second; |
| 251 | bool IdQuery = false; |
| 252 | |
| 253 | // If reqd_work_group_size is present it narrows value down. |
| 254 | if (auto *CI = dyn_cast<CallInst>(I)) { |
| 255 | const Function *F = CI->getCalledFunction(); |
| 256 | if (F) { |
| 257 | unsigned Dim = UINT_MAX; |
| 258 | switch (F->getIntrinsicID()) { |
| 259 | case Intrinsic::amdgcn_workitem_id_x: |
| 260 | case Intrinsic::r600_read_tidig_x: |
| 261 | IdQuery = true; |
| 262 | case Intrinsic::r600_read_local_size_x: |
| 263 | Dim = 0; |
| 264 | break; |
| 265 | case Intrinsic::amdgcn_workitem_id_y: |
| 266 | case Intrinsic::r600_read_tidig_y: |
| 267 | IdQuery = true; |
| 268 | case Intrinsic::r600_read_local_size_y: |
| 269 | Dim = 1; |
| 270 | break; |
| 271 | case Intrinsic::amdgcn_workitem_id_z: |
| 272 | case Intrinsic::r600_read_tidig_z: |
| 273 | IdQuery = true; |
| 274 | case Intrinsic::r600_read_local_size_z: |
| 275 | Dim = 2; |
| 276 | break; |
| 277 | default: |
| 278 | break; |
| 279 | } |
| 280 | if (Dim <= 3) { |
| 281 | if (auto Node = Kernel->getMetadata("reqd_work_group_size")) |
| 282 | if (Node->getNumOperands() == 3) |
| 283 | MinSize = MaxSize = mdconst::extract<ConstantInt>( |
| 284 | Node->getOperand(Dim))->getZExtValue(); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (!MaxSize) |
| 290 | return false; |
| 291 | |
| 292 | // Range metadata is [Lo, Hi). For ID query we need to pass max size |
| 293 | // as Hi. For size query we need to pass Hi + 1. |
| 294 | if (IdQuery) |
| 295 | MinSize = 0; |
| 296 | else |
| 297 | ++MaxSize; |
| 298 | |
| 299 | MDBuilder MDB(I->getContext()); |
| 300 | MDNode *MaxWorkGroupSizeRange = MDB.createRange(APInt(32, MinSize), |
| 301 | APInt(32, MaxSize)); |
| 302 | I->setMetadata(LLVMContext::MD_range, MaxWorkGroupSizeRange); |
| 303 | return true; |
| 304 | } |
| 305 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 306 | R600Subtarget::R600Subtarget(const Triple &TT, StringRef GPU, StringRef FS, |
| 307 | const TargetMachine &TM) : |
| 308 | AMDGPUSubtarget(TT, GPU, FS, TM), |
| 309 | InstrInfo(*this), |
| 310 | FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0), |
| 311 | TLInfo(TM, *this) {} |
| 312 | |
| 313 | SISubtarget::SISubtarget(const Triple &TT, StringRef GPU, StringRef FS, |
| 314 | const TargetMachine &TM) : |
| 315 | AMDGPUSubtarget(TT, GPU, FS, TM), |
| 316 | InstrInfo(*this), |
| 317 | FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0), |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 318 | TLInfo(TM, *this) {} |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 319 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 320 | void SISubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy, |
Matt Arsenault | 55dff27 | 2016-06-28 00:11:26 +0000 | [diff] [blame] | 321 | unsigned NumRegionInstrs) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 322 | // Track register pressure so the scheduler can try to decrease |
| 323 | // pressure once register usage is above the threshold defined by |
| 324 | // SIRegisterInfo::getRegPressureSetLimit() |
| 325 | Policy.ShouldTrackPressure = true; |
Tom Stellard | 83f0bce | 2015-01-29 16:55:25 +0000 | [diff] [blame] | 326 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 327 | // Enabling both top down and bottom up scheduling seems to give us less |
| 328 | // register spills than just using one of these approaches on its own. |
| 329 | Policy.OnlyTopDown = false; |
| 330 | Policy.OnlyBottomUp = false; |
Tom Stellard | 83f0bce | 2015-01-29 16:55:25 +0000 | [diff] [blame] | 331 | |
Alexander Timofeev | 9f61fea | 2017-02-14 14:29:05 +0000 | [diff] [blame] | 332 | // Enabling ShouldTrackLaneMasks crashes the SI Machine Scheduler. |
| 333 | if (!enableSIScheduler()) |
| 334 | Policy.ShouldTrackLaneMasks = true; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 335 | } |
Tom Stellard | 0bc954e | 2016-03-30 16:35:09 +0000 | [diff] [blame] | 336 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 337 | bool SISubtarget::isVGPRSpillingEnabled(const Function& F) const { |
| 338 | return EnableVGPRSpilling || !AMDGPU::isShader(F.getCallingConv()); |
| 339 | } |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 340 | |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 341 | unsigned SISubtarget::getKernArgSegmentSize(const MachineFunction &MF, |
Konstantin Zhuravlyov | 27d64c3 | 2017-02-08 13:29:23 +0000 | [diff] [blame] | 342 | unsigned ExplicitArgBytes) const { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 343 | unsigned ImplicitBytes = getImplicitArgNumBytes(MF); |
Tom Stellard | e88bbc3 | 2016-09-23 01:33:26 +0000 | [diff] [blame] | 344 | if (ImplicitBytes == 0) |
| 345 | return ExplicitArgBytes; |
| 346 | |
| 347 | unsigned Alignment = getAlignmentForImplicitArgPtr(); |
| 348 | return alignTo(ExplicitArgBytes, Alignment) + ImplicitBytes; |
| 349 | } |
| 350 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 351 | unsigned SISubtarget::getOccupancyWithNumSGPRs(unsigned SGPRs) const { |
| 352 | if (getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { |
| 353 | if (SGPRs <= 80) |
| 354 | return 10; |
| 355 | if (SGPRs <= 88) |
| 356 | return 9; |
| 357 | if (SGPRs <= 100) |
| 358 | return 8; |
| 359 | return 7; |
| 360 | } |
| 361 | if (SGPRs <= 48) |
| 362 | return 10; |
| 363 | if (SGPRs <= 56) |
| 364 | return 9; |
| 365 | if (SGPRs <= 64) |
| 366 | return 8; |
| 367 | if (SGPRs <= 72) |
| 368 | return 7; |
| 369 | if (SGPRs <= 80) |
| 370 | return 6; |
| 371 | return 5; |
| 372 | } |
| 373 | |
| 374 | unsigned SISubtarget::getOccupancyWithNumVGPRs(unsigned VGPRs) const { |
| 375 | if (VGPRs <= 24) |
| 376 | return 10; |
| 377 | if (VGPRs <= 28) |
| 378 | return 9; |
| 379 | if (VGPRs <= 32) |
| 380 | return 8; |
| 381 | if (VGPRs <= 36) |
| 382 | return 7; |
| 383 | if (VGPRs <= 40) |
| 384 | return 6; |
| 385 | if (VGPRs <= 48) |
| 386 | return 5; |
| 387 | if (VGPRs <= 64) |
| 388 | return 4; |
| 389 | if (VGPRs <= 84) |
| 390 | return 3; |
| 391 | if (VGPRs <= 128) |
| 392 | return 2; |
| 393 | return 1; |
| 394 | } |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 395 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 396 | unsigned SISubtarget::getReservedNumSGPRs(const MachineFunction &MF) const { |
| 397 | const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>(); |
| 398 | if (MFI.hasFlatScratchInit()) { |
| 399 | if (getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 400 | return 6; // FLAT_SCRATCH, XNACK, VCC (in that order). |
| 401 | if (getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) |
| 402 | return 4; // FLAT_SCRATCH, VCC (in that order). |
| 403 | } |
| 404 | |
| 405 | if (isXNACKEnabled()) |
| 406 | return 4; // XNACK, VCC (in that order). |
| 407 | return 2; // VCC. |
| 408 | } |
| 409 | |
| 410 | unsigned SISubtarget::getMaxNumSGPRs(const MachineFunction &MF) const { |
| 411 | const Function &F = *MF.getFunction(); |
| 412 | const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>(); |
| 413 | |
| 414 | // Compute maximum number of SGPRs function can use using default/requested |
| 415 | // minimum number of waves per execution unit. |
| 416 | std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU(); |
| 417 | unsigned MaxNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, false); |
| 418 | unsigned MaxAddressableNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, true); |
| 419 | |
| 420 | // Check if maximum number of SGPRs was explicitly requested using |
| 421 | // "amdgpu-num-sgpr" attribute. |
| 422 | if (F.hasFnAttribute("amdgpu-num-sgpr")) { |
| 423 | unsigned Requested = AMDGPU::getIntegerAttribute( |
| 424 | F, "amdgpu-num-sgpr", MaxNumSGPRs); |
| 425 | |
| 426 | // Make sure requested value does not violate subtarget's specifications. |
| 427 | if (Requested && (Requested <= getReservedNumSGPRs(MF))) |
| 428 | Requested = 0; |
| 429 | |
| 430 | // If more SGPRs are required to support the input user/system SGPRs, |
| 431 | // increase to accommodate them. |
| 432 | // |
| 433 | // FIXME: This really ends up using the requested number of SGPRs + number |
| 434 | // of reserved special registers in total. Theoretically you could re-use |
| 435 | // the last input registers for these special registers, but this would |
| 436 | // require a lot of complexity to deal with the weird aliasing. |
| 437 | unsigned InputNumSGPRs = MFI.getNumPreloadedSGPRs(); |
| 438 | if (Requested && Requested < InputNumSGPRs) |
| 439 | Requested = InputNumSGPRs; |
| 440 | |
| 441 | // Make sure requested value is compatible with values implied by |
| 442 | // default/requested minimum/maximum number of waves per execution unit. |
| 443 | if (Requested && Requested > getMaxNumSGPRs(WavesPerEU.first, false)) |
| 444 | Requested = 0; |
| 445 | if (WavesPerEU.second && |
| 446 | Requested && Requested < getMinNumSGPRs(WavesPerEU.second)) |
| 447 | Requested = 0; |
| 448 | |
| 449 | if (Requested) |
| 450 | MaxNumSGPRs = Requested; |
| 451 | } |
| 452 | |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 453 | if (hasSGPRInitBug()) |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 454 | MaxNumSGPRs = AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG; |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 455 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 456 | return std::min(MaxNumSGPRs - getReservedNumSGPRs(MF), |
| 457 | MaxAddressableNumSGPRs); |
| 458 | } |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 459 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 460 | unsigned SISubtarget::getMaxNumVGPRs(const MachineFunction &MF) const { |
| 461 | const Function &F = *MF.getFunction(); |
| 462 | const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>(); |
| 463 | |
| 464 | // Compute maximum number of VGPRs function can use using default/requested |
| 465 | // minimum number of waves per execution unit. |
| 466 | std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU(); |
| 467 | unsigned MaxNumVGPRs = getMaxNumVGPRs(WavesPerEU.first); |
| 468 | |
| 469 | // Check if maximum number of VGPRs was explicitly requested using |
| 470 | // "amdgpu-num-vgpr" attribute. |
| 471 | if (F.hasFnAttribute("amdgpu-num-vgpr")) { |
| 472 | unsigned Requested = AMDGPU::getIntegerAttribute( |
| 473 | F, "amdgpu-num-vgpr", MaxNumVGPRs); |
| 474 | |
| 475 | // Make sure requested value does not violate subtarget's specifications. |
| 476 | if (Requested && Requested <= getReservedNumVGPRs(MF)) |
| 477 | Requested = 0; |
| 478 | |
| 479 | // Make sure requested value is compatible with values implied by |
| 480 | // default/requested minimum/maximum number of waves per execution unit. |
| 481 | if (Requested && Requested > getMaxNumVGPRs(WavesPerEU.first)) |
| 482 | Requested = 0; |
| 483 | if (WavesPerEU.second && |
| 484 | Requested && Requested < getMinNumVGPRs(WavesPerEU.second)) |
| 485 | Requested = 0; |
| 486 | |
| 487 | if (Requested) |
| 488 | MaxNumVGPRs = Requested; |
| 489 | } |
| 490 | |
| 491 | return MaxNumVGPRs - getReservedNumVGPRs(MF); |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 492 | } |