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