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