blob: b94548a5f5c475f30b211f87e54f23c01b6491b5 [file] [log] [blame]
Matt Arsenault0c90e952015-11-06 18:17:45 +00001//=====-- AMDGPUSubtarget.h - Define Subtarget for AMDGPU ------*- C++ -*-====//
Tom Stellard75aadc22012-12-11 21:25:42 +00002//
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 AMDGPU specific subclass of TargetSubtarget.
12//
13//===----------------------------------------------------------------------===//
14
Matt Arsenault0c90e952015-11-06 18:17:45 +000015#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
16#define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
Matt Arsenaultf59e5382015-11-06 18:23:00 +000017
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000018#include "AMDGPU.h"
Matt Arsenault43e92fe2016-06-24 06:30:11 +000019#include "R600InstrInfo.h"
20#include "R600ISelLowering.h"
21#include "R600FrameLowering.h"
22#include "SIInstrInfo.h"
23#include "SIISelLowering.h"
24#include "SIFrameLowering.h"
Tom Stellard347ac792015-06-26 21:15:07 +000025#include "Utils/AMDGPUBaseInfo.h"
Tom Stellard000c5af2016-04-14 19:09:28 +000026#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
Matt Arsenault56684d42016-08-11 17:31:42 +000027#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000028#include "llvm/Target/TargetSubtargetInfo.h"
29
30#define GET_SUBTARGETINFO_HEADER
31#include "AMDGPUGenSubtargetInfo.inc"
32
Tom Stellard75aadc22012-12-11 21:25:42 +000033namespace llvm {
34
Tom Stellarde99fb652015-01-20 19:33:04 +000035class SIMachineFunctionInfo;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000036class StringRef;
Tom Stellarde99fb652015-01-20 19:33:04 +000037
Tom Stellard75aadc22012-12-11 21:25:42 +000038class AMDGPUSubtarget : public AMDGPUGenSubtargetInfo {
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000039public:
40 enum Generation {
41 R600 = 0,
42 R700,
43 EVERGREEN,
44 NORTHERN_ISLANDS,
Tom Stellard6e1ee472013-10-29 16:37:28 +000045 SOUTHERN_ISLANDS,
Marek Olsak5df00d62014-12-07 12:18:57 +000046 SEA_ISLANDS,
47 VOLCANIC_ISLANDS,
Tom Stellarda6c6e1b2013-06-07 20:37:48 +000048 };
49
Marek Olsak4d00dd22015-03-09 15:48:09 +000050 enum {
Tom Stellard347ac792015-06-26 21:15:07 +000051 ISAVersion0_0_0,
52 ISAVersion7_0_0,
53 ISAVersion7_0_1,
54 ISAVersion8_0_0,
Changpeng Fangc16be002016-01-13 20:39:25 +000055 ISAVersion8_0_1,
56 ISAVersion8_0_3
Tom Stellard347ac792015-06-26 21:15:07 +000057 };
58
Matt Arsenault43e92fe2016-06-24 06:30:11 +000059protected:
60 // Basic subtarget description.
61 Triple TargetTriple;
Matt Arsenaultd782d052014-06-27 17:57:00 +000062 Generation Gen;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000063 unsigned IsaVersion;
64 unsigned WavefrontSize;
65 int LocalMemorySize;
66 int LDSBankCount;
67 unsigned MaxPrivateElementSize;
68
69 // Possibly statically set by tablegen, but may want to be overridden.
Matt Arsenaultb035a572015-01-29 19:34:25 +000070 bool FastFMAF32;
Matt Arsenaulte83690c2016-01-18 21:13:50 +000071 bool HalfRate64Ops;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000072
73 // Dynamially set bits that enable features.
74 bool FP32Denormals;
75 bool FP64Denormals;
76 bool FPExceptions;
Changpeng Fangb41574a2015-12-22 20:55:23 +000077 bool FlatForGlobal;
Matt Arsenault7f681ac2016-07-01 23:03:44 +000078 bool UnalignedBufferAccess;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000079 bool EnableXNACK;
80 bool DebuggerInsertNops;
81 bool DebuggerReserveRegs;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000082 bool DebuggerEmitPrologue;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000083
84 // Used as options.
85 bool EnableVGPRSpilling;
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000086 bool EnablePromoteAlloca;
Matt Arsenault41033282014-10-10 22:01:59 +000087 bool EnableLoadStoreOpt;
Matt Arsenault706f9302015-07-06 16:01:58 +000088 bool EnableUnsafeDSOffsetFolding;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000089 bool EnableSIScheduler;
90 bool DumpCode;
91
92 // Subtarget statically properties set by tablegen
93 bool FP64;
Tom Stellardd7e6f132015-04-08 01:09:26 +000094 bool IsGCN;
95 bool GCN1Encoding;
96 bool GCN3Encoding;
Tom Stellardd1f0f022015-04-23 19:33:54 +000097 bool CIInsts;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000098 bool SGPRInitBug;
Matt Arsenault9d82ee72016-02-27 08:53:55 +000099 bool HasSMemRealTime;
100 bool Has16BitInsts;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000101 bool FlatAddressSpace;
102 bool R600ALUInst;
103 bool CaymanISA;
104 bool CFALUBug;
105 bool HasVertexCache;
106 short TexVTXClauseSize;
Tom Stellard75aadc22012-12-11 21:25:42 +0000107
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000108 // Dummy feature to use for assembler in tablegen.
109 bool FeatureDisable;
110
Tom Stellard75aadc22012-12-11 21:25:42 +0000111 InstrItineraryData InstrItins;
Matt Arsenault56684d42016-08-11 17:31:42 +0000112 SelectionDAGTargetInfo TSInfo;
Tom Stellard75aadc22012-12-11 21:25:42 +0000113
114public:
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000115 AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
116 const TargetMachine &TM);
117 virtual ~AMDGPUSubtarget();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000118 AMDGPUSubtarget &initializeSubtargetDependencies(const Triple &TT,
119 StringRef GPU, StringRef FS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000120
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000121 const AMDGPUInstrInfo *getInstrInfo() const override = 0;
122 const AMDGPUFrameLowering *getFrameLowering() const override = 0;
123 const AMDGPUTargetLowering *getTargetLowering() const override = 0;
124 const AMDGPURegisterInfo *getRegisterInfo() const override = 0;
Tom Stellard000c5af2016-04-14 19:09:28 +0000125
Eric Christopherd9134482014-08-04 21:25:23 +0000126 const InstrItineraryData *getInstrItineraryData() const override {
127 return &InstrItins;
128 }
Matt Arsenaultd782d052014-06-27 17:57:00 +0000129
Matt Arsenault56684d42016-08-11 17:31:42 +0000130 // Nothing implemented, just prevent crashes on use.
131 const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
132 return &TSInfo;
133 }
134
Craig Topperee7b0f32014-04-30 05:53:27 +0000135 void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000136
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000137 bool isAmdHsaOS() const {
138 return TargetTriple.getOS() == Triple::AMDHSA;
Matt Arsenaultd782d052014-06-27 17:57:00 +0000139 }
140
141 Generation getGeneration() const {
142 return Gen;
143 }
144
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000145 unsigned getWavefrontSize() const {
146 return WavefrontSize;
147 }
148
149 int getLocalMemorySize() const {
150 return LocalMemorySize;
151 }
152
153 int getLDSBankCount() const {
154 return LDSBankCount;
155 }
156
157 unsigned getMaxPrivateElementSize() const {
158 return MaxPrivateElementSize;
159 }
160
Matt Arsenaultd782d052014-06-27 17:57:00 +0000161 bool hasHWFP64() const {
162 return FP64;
163 }
164
Matt Arsenaultb035a572015-01-29 19:34:25 +0000165 bool hasFastFMAF32() const {
166 return FastFMAF32;
167 }
168
Matt Arsenaulte83690c2016-01-18 21:13:50 +0000169 bool hasHalfRate64Ops() const {
170 return HalfRate64Ops;
171 }
172
Matt Arsenault88701812016-06-09 23:42:48 +0000173 bool hasAddr64() const {
174 return (getGeneration() < VOLCANIC_ISLANDS);
175 }
176
Matt Arsenaultfae02982014-03-17 18:58:11 +0000177 bool hasBFE() const {
178 return (getGeneration() >= EVERGREEN);
179 }
180
Matt Arsenault6e439652014-06-10 19:00:20 +0000181 bool hasBFI() const {
182 return (getGeneration() >= EVERGREEN);
183 }
184
Matt Arsenaultfae02982014-03-17 18:58:11 +0000185 bool hasBFM() const {
186 return hasBFE();
187 }
188
Matt Arsenault60425062014-06-10 19:18:28 +0000189 bool hasBCNT(unsigned Size) const {
190 if (Size == 32)
191 return (getGeneration() >= EVERGREEN);
192
Matt Arsenault3dd43fc2014-07-18 06:07:13 +0000193 if (Size == 64)
194 return (getGeneration() >= SOUTHERN_ISLANDS);
195
196 return false;
Matt Arsenault60425062014-06-10 19:18:28 +0000197 }
198
Tom Stellard50122a52014-04-07 19:45:41 +0000199 bool hasMulU24() const {
200 return (getGeneration() >= EVERGREEN);
201 }
202
203 bool hasMulI24() const {
204 return (getGeneration() >= SOUTHERN_ISLANDS ||
205 hasCaymanISA());
206 }
207
Jan Vesely6ddb8dd2014-07-15 15:51:09 +0000208 bool hasFFBL() const {
209 return (getGeneration() >= EVERGREEN);
210 }
211
212 bool hasFFBH() const {
213 return (getGeneration() >= EVERGREEN);
214 }
215
Jan Vesely808fff52015-04-30 17:15:56 +0000216 bool hasCARRY() const {
217 return (getGeneration() >= EVERGREEN);
218 }
219
220 bool hasBORROW() const {
221 return (getGeneration() >= EVERGREEN);
222 }
223
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000224 bool hasCaymanISA() const {
225 return CaymanISA;
226 }
227
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +0000228 bool isPromoteAllocaEnabled() const {
229 return EnablePromoteAlloca;
230 }
231
Matt Arsenault706f9302015-07-06 16:01:58 +0000232 bool unsafeDSOffsetFoldingEnabled() const {
233 return EnableUnsafeDSOffsetFolding;
234 }
235
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000236 bool dumpCode() const {
237 return DumpCode;
Matt Arsenaultd782d052014-06-27 17:57:00 +0000238 }
239
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000240 /// Return the amount of LDS that can be used that will not restrict the
241 /// occupancy lower than WaveCount.
242 unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount) const;
243
244 /// Inverse of getMaxLocalMemWithWaveCount. Return the maximum wavecount if
245 /// the given LDS memory size is the only constraint.
246 unsigned getOccupancyWithLocalMemSize(uint32_t Bytes) const;
247
248
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000249 bool hasFP32Denormals() const {
250 return FP32Denormals;
Matt Arsenaultd782d052014-06-27 17:57:00 +0000251 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000252
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000253 bool hasFP64Denormals() const {
254 return FP64Denormals;
Matt Arsenault24ee0782016-02-12 02:40:47 +0000255 }
256
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000257 bool hasFPExceptions() const {
258 return FPExceptions;
Marek Olsak4d00dd22015-03-09 15:48:09 +0000259 }
260
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000261 bool useFlatForGlobal() const {
262 return FlatForGlobal;
Tom Stellardec87f842015-05-25 16:15:54 +0000263 }
264
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000265 bool hasUnalignedBufferAccess() const {
266 return UnalignedBufferAccess;
267 }
268
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000269 bool isXNACKEnabled() const {
270 return EnableXNACK;
271 }
Tom Stellardb8fd6ef2014-12-02 22:00:07 +0000272
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000273 /// \brief Returns the offset in bytes from the start of the input buffer
274 /// of the first explicit kernel argument.
275 unsigned getExplicitKernelArgOffset() const {
276 return isAmdHsaOS() ? 0 : 36;
277 }
278
279 unsigned getStackAlignment() const {
280 // Scratch is allocated in 256 dword per wave blocks.
281 return 4 * 256 / getWavefrontSize();
282 }
Tom Stellard347ac792015-06-26 21:15:07 +0000283
Craig Topper5656db42014-04-29 07:57:24 +0000284 bool enableMachineScheduler() const override {
Tom Stellard83f0bce2015-01-29 16:55:25 +0000285 return true;
Andrew Trick978674b2013-09-20 05:14:41 +0000286 }
287
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000288 bool enableSubRegLiveness() const override {
289 return true;
290 }
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000291
292 /// \returns Number of execution units per compute unit supported by the
293 /// subtarget.
294 unsigned getEUsPerCU() const {
295 return 4;
296 }
297
298 /// \returns Maximum number of work groups per compute unit supported by the
299 /// subtarget and limited by given flat work group size.
300 unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const {
301 if (getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
302 return 8;
303 return getWavesPerWorkGroup(FlatWorkGroupSize) == 1 ? 40 : 16;
304 }
305
306 /// \returns Maximum number of waves per compute unit supported by the
307 /// subtarget without any kind of limitation.
308 unsigned getMaxWavesPerCU() const {
309 return getMaxWavesPerEU() * getEUsPerCU();
310 }
311
312 /// \returns Maximum number of waves per compute unit supported by the
313 /// subtarget and limited by given flat work group size.
314 unsigned getMaxWavesPerCU(unsigned FlatWorkGroupSize) const {
315 return getWavesPerWorkGroup(FlatWorkGroupSize);
316 }
317
318 /// \returns Minimum number of waves per execution unit supported by the
319 /// subtarget.
320 unsigned getMinWavesPerEU() const {
321 return 1;
322 }
323
324 /// \returns Maximum number of waves per execution unit supported by the
325 /// subtarget without any kind of limitation.
326 unsigned getMaxWavesPerEU() const {
327 if (getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
328 return 8;
329 // FIXME: Need to take scratch memory into account.
330 return 10;
331 }
332
333 /// \returns Maximum number of waves per execution unit supported by the
334 /// subtarget and limited by given flat work group size.
335 unsigned getMaxWavesPerEU(unsigned FlatWorkGroupSize) const {
336 return alignTo(getMaxWavesPerCU(FlatWorkGroupSize), getEUsPerCU()) /
337 getEUsPerCU();
338 }
339
340 /// \returns Minimum flat work group size supported by the subtarget.
341 unsigned getMinFlatWorkGroupSize() const {
342 return 1;
343 }
344
345 /// \returns Maximum flat work group size supported by the subtarget.
346 unsigned getMaxFlatWorkGroupSize() const {
347 return 2048;
348 }
349
350 /// \returns Number of waves per work group given the flat work group size.
351 unsigned getWavesPerWorkGroup(unsigned FlatWorkGroupSize) const {
352 return alignTo(FlatWorkGroupSize, getWavefrontSize()) / getWavefrontSize();
353 }
354
355 /// \returns Subtarget's default pair of minimum/maximum flat work group sizes
356 /// for function \p F, or minimum/maximum flat work group sizes explicitly
357 /// requested using "amdgpu-flat-work-group-size" attribute attached to
358 /// function \p F.
359 ///
360 /// \returns Subtarget's default values if explicitly requested values cannot
361 /// be converted to integer, or violate subtarget's specifications.
362 std::pair<unsigned, unsigned> getFlatWorkGroupSizes(const Function &F) const;
363
364 /// \returns Subtarget's default pair of minimum/maximum number of waves per
365 /// execution unit for function \p F, or minimum/maximum number of waves per
366 /// execution unit explicitly requested using "amdgpu-waves-per-eu" attribute
367 /// attached to function \p F.
368 ///
369 /// \returns Subtarget's default values if explicitly requested values cannot
370 /// be converted to integer, violate subtarget's specifications, or are not
371 /// compatible with minimum/maximum number of waves limited by flat work group
372 /// size, register usage, and/or lds usage.
373 std::pair<unsigned, unsigned> getWavesPerEU(const Function &F) const;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000374};
375
376class R600Subtarget final : public AMDGPUSubtarget {
377private:
378 R600InstrInfo InstrInfo;
379 R600FrameLowering FrameLowering;
380 R600TargetLowering TLInfo;
381
382public:
383 R600Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
384 const TargetMachine &TM);
385
386 const R600InstrInfo *getInstrInfo() const override {
387 return &InstrInfo;
388 }
389
390 const R600FrameLowering *getFrameLowering() const override {
391 return &FrameLowering;
392 }
393
394 const R600TargetLowering *getTargetLowering() const override {
395 return &TLInfo;
396 }
397
398 const R600RegisterInfo *getRegisterInfo() const override {
399 return &InstrInfo.getRegisterInfo();
400 }
401
402 bool hasCFAluBug() const {
403 return CFALUBug;
404 }
405
406 bool hasVertexCache() const {
407 return HasVertexCache;
408 }
409
410 short getTexVTXClauseSize() const {
411 return TexVTXClauseSize;
412 }
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000413};
414
415class SISubtarget final : public AMDGPUSubtarget {
416public:
417 enum {
Marek Olsak355a8642016-08-05 21:23:29 +0000418 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
419 // doesn't spill SGPRs as much as when 80 is set.
420 FIXED_SGPR_COUNT_FOR_INIT_BUG = 96
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000421 };
422
423private:
424 SIInstrInfo InstrInfo;
425 SIFrameLowering FrameLowering;
426 SITargetLowering TLInfo;
427 std::unique_ptr<GISelAccessor> GISel;
428
429public:
430 SISubtarget(const Triple &TT, StringRef CPU, StringRef FS,
431 const TargetMachine &TM);
432
433 const SIInstrInfo *getInstrInfo() const override {
434 return &InstrInfo;
435 }
436
437 const SIFrameLowering *getFrameLowering() const override {
438 return &FrameLowering;
439 }
440
441 const SITargetLowering *getTargetLowering() const override {
442 return &TLInfo;
443 }
444
445 const CallLowering *getCallLowering() const override {
446 assert(GISel && "Access to GlobalISel APIs not set");
447 return GISel->getCallLowering();
448 }
449
450 const SIRegisterInfo *getRegisterInfo() const override {
451 return &InstrInfo.getRegisterInfo();
452 }
453
454 void setGISelAccessor(GISelAccessor &GISel) {
455 this->GISel.reset(&GISel);
456 }
457
Tom Stellard83f0bce2015-01-29 16:55:25 +0000458 void overrideSchedPolicy(MachineSchedPolicy &Policy,
Tom Stellard83f0bce2015-01-29 16:55:25 +0000459 unsigned NumRegionInstrs) const override;
460
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000461 bool isVGPRSpillingEnabled(const Function& F) const;
462
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000463 unsigned getMaxNumUserSGPRs() const {
464 return 16;
465 }
466
467 bool hasFlatAddressSpace() const {
468 return FlatAddressSpace;
469 }
470
471 bool hasSMemRealTime() const {
472 return HasSMemRealTime;
473 }
474
475 bool has16BitInsts() const {
476 return Has16BitInsts;
Matt Arsenaultd782d052014-06-27 17:57:00 +0000477 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000478
Tom Stellardde008d32016-01-21 04:28:34 +0000479 bool enableSIScheduler() const {
480 return EnableSIScheduler;
481 }
482
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000483 bool debuggerSupported() const {
484 return debuggerInsertNops() && debuggerReserveRegs() &&
485 debuggerEmitPrologue();
486 }
487
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +0000488 bool debuggerInsertNops() const {
489 return DebuggerInsertNops;
490 }
491
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000492 bool debuggerReserveRegs() const {
493 return DebuggerReserveRegs;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000494 }
495
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000496 bool debuggerEmitPrologue() const {
497 return DebuggerEmitPrologue;
498 }
499
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000500 bool loadStoreOptEnabled() const {
501 return EnableLoadStoreOpt;
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000502 }
503
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000504 bool hasSGPRInitBug() const {
505 return SGPRInitBug;
Matt Arsenault41003af2015-11-30 21:16:07 +0000506 }
Tom Stellard0d23ebe2016-08-29 19:42:52 +0000507
508 /// Return the maximum number of waves per SIMD for kernels using \p SGPRs SGPRs
509 unsigned getOccupancyWithNumSGPRs(unsigned SGPRs) const;
510
511 /// Return the maximum number of waves per SIMD for kernels using \p VGPRs VGPRs
512 unsigned getOccupancyWithNumVGPRs(unsigned VGPRs) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000513};
514
515} // End namespace llvm
516
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000517#endif