1 //=====-- AMDGPUSubtarget.h - Define Subtarget for AMDGPU ------*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //==-----------------------------------------------------------------------===//
11 /// \brief AMDGPU specific subclass of TargetSubtarget.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
19 #include "R600InstrInfo.h"
20 #include "R600ISelLowering.h"
21 #include "R600FrameLowering.h"
22 #include "SIInstrInfo.h"
23 #include "SIISelLowering.h"
24 #include "SIFrameLowering.h"
25 #include "Utils/AMDGPUBaseInfo.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
30 #include "llvm/MC/MCInstrItineraries.h"
31 #include "llvm/Support/MathExtras.h"
37 #define GET_SUBTARGETINFO_HEADER
38 #include "AMDGPUGenSubtargetInfo.inc"
44 class AMDGPUSubtarget : public AMDGPUGenSubtargetInfo {
70 // Basic subtarget description.
74 unsigned WavefrontSize;
77 unsigned MaxPrivateElementSize;
79 // Possibly statically set by tablegen, but may want to be overridden.
83 // Dynamially set bits that enable features.
89 bool UnalignedScratchAccess;
90 bool UnalignedBufferAccess;
92 bool DebuggerInsertNops;
93 bool DebuggerReserveRegs;
94 bool DebuggerEmitPrologue;
97 bool EnableVGPRSpilling;
98 bool EnablePromoteAlloca;
99 bool EnableLoadStoreOpt;
100 bool EnableUnsafeDSOffsetFolding;
101 bool EnableSIScheduler;
104 // Subtarget statically properties set by tablegen
111 bool HasSMemRealTime;
114 bool HasVGPRIndexMode;
115 bool HasScalarStores;
116 bool HasInv2PiInlineImm;
117 bool FlatAddressSpace;
122 short TexVTXClauseSize;
123 bool ScalarizeGlobal;
125 // Dummy feature to use for assembler in tablegen.
128 InstrItineraryData InstrItins;
129 SelectionDAGTargetInfo TSInfo;
132 AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
133 const TargetMachine &TM);
134 ~AMDGPUSubtarget() override;
136 AMDGPUSubtarget &initializeSubtargetDependencies(const Triple &TT,
137 StringRef GPU, StringRef FS);
139 const AMDGPUInstrInfo *getInstrInfo() const override = 0;
140 const AMDGPUFrameLowering *getFrameLowering() const override = 0;
141 const AMDGPUTargetLowering *getTargetLowering() const override = 0;
142 const AMDGPURegisterInfo *getRegisterInfo() const override = 0;
144 const InstrItineraryData *getInstrItineraryData() const override {
148 // Nothing implemented, just prevent crashes on use.
149 const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
153 void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
155 bool isAmdHsaOS() const {
156 return TargetTriple.getOS() == Triple::AMDHSA;
159 bool isMesa3DOS() const {
160 return TargetTriple.getOS() == Triple::Mesa3D;
163 bool isOpenCLEnv() const {
164 return TargetTriple.getEnvironment() == Triple::OpenCL;
167 Generation getGeneration() const {
171 unsigned getWavefrontSize() const {
172 return WavefrontSize;
175 int getLocalMemorySize() const {
176 return LocalMemorySize;
179 int getLDSBankCount() const {
183 unsigned getMaxPrivateElementSize() const {
184 return MaxPrivateElementSize;
187 bool has16BitInsts() const {
188 return Has16BitInsts;
191 bool hasHWFP64() const {
195 bool hasFastFMAF32() const {
199 bool hasHalfRate64Ops() const {
200 return HalfRate64Ops;
203 bool hasAddr64() const {
204 return (getGeneration() < VOLCANIC_ISLANDS);
207 bool hasBFE() const {
208 return (getGeneration() >= EVERGREEN);
211 bool hasBFI() const {
212 return (getGeneration() >= EVERGREEN);
215 bool hasBFM() const {
219 bool hasBCNT(unsigned Size) const {
221 return (getGeneration() >= EVERGREEN);
224 return (getGeneration() >= SOUTHERN_ISLANDS);
229 bool hasMulU24() const {
230 return (getGeneration() >= EVERGREEN);
233 bool hasMulI24() const {
234 return (getGeneration() >= SOUTHERN_ISLANDS ||
238 bool hasFFBL() const {
239 return (getGeneration() >= EVERGREEN);
242 bool hasFFBH() const {
243 return (getGeneration() >= EVERGREEN);
246 bool hasCARRY() const {
247 return (getGeneration() >= EVERGREEN);
250 bool hasBORROW() const {
251 return (getGeneration() >= EVERGREEN);
254 bool hasCaymanISA() const {
258 bool isPromoteAllocaEnabled() const {
259 return EnablePromoteAlloca;
262 bool unsafeDSOffsetFoldingEnabled() const {
263 return EnableUnsafeDSOffsetFolding;
266 bool dumpCode() const {
270 bool enableIEEEBit(const MachineFunction &MF) const {
271 return AMDGPU::isCompute(MF.getFunction()->getCallingConv());
274 /// Return the amount of LDS that can be used that will not restrict the
275 /// occupancy lower than WaveCount.
276 unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount) const;
278 /// Inverse of getMaxLocalMemWithWaveCount. Return the maximum wavecount if
279 /// the given LDS memory size is the only constraint.
280 unsigned getOccupancyWithLocalMemSize(uint32_t Bytes) const;
282 bool hasFP16Denormals() const {
283 return FP16Denormals;
286 bool hasFP32Denormals() const {
287 return FP32Denormals;
290 bool hasFP64Denormals() const {
291 return FP64Denormals;
294 bool hasFPExceptions() const {
298 bool useFlatForGlobal() const {
299 return FlatForGlobal;
302 bool hasUnalignedBufferAccess() const {
303 return UnalignedBufferAccess;
306 bool hasUnalignedScratchAccess() const {
307 return UnalignedScratchAccess;
310 bool isXNACKEnabled() const {
314 bool isAmdCodeObjectV2() const {
315 return isAmdHsaOS() || isMesa3DOS();
318 /// \brief Returns the offset in bytes from the start of the input buffer
319 /// of the first explicit kernel argument.
320 unsigned getExplicitKernelArgOffset() const {
321 return isAmdCodeObjectV2() ? 0 : 36;
324 unsigned getAlignmentForImplicitArgPtr() const {
325 return isAmdHsaOS() ? 8 : 4;
328 unsigned getImplicitArgNumBytes() const {
331 if (isAmdHsaOS() && isOpenCLEnv())
336 unsigned getStackAlignment() const {
337 // Scratch is allocated in 256 dword per wave blocks.
338 return 4 * 256 / getWavefrontSize();
341 bool enableMachineScheduler() const override {
345 bool enableSubRegLiveness() const override {
349 /// \returns Number of execution units per compute unit supported by the
351 unsigned getEUsPerCU() const {
355 /// \returns Maximum number of work groups per compute unit supported by the
356 /// subtarget and limited by given flat work group size.
357 unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const {
358 if (getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
360 return getWavesPerWorkGroup(FlatWorkGroupSize) == 1 ? 40 : 16;
363 /// \returns Maximum number of waves per compute unit supported by the
364 /// subtarget without any kind of limitation.
365 unsigned getMaxWavesPerCU() const {
366 return getMaxWavesPerEU() * getEUsPerCU();
369 /// \returns Maximum number of waves per compute unit supported by the
370 /// subtarget and limited by given flat work group size.
371 unsigned getMaxWavesPerCU(unsigned FlatWorkGroupSize) const {
372 return getWavesPerWorkGroup(FlatWorkGroupSize);
375 /// \returns Minimum number of waves per execution unit supported by the
377 unsigned getMinWavesPerEU() const {
381 /// \returns Maximum number of waves per execution unit supported by the
382 /// subtarget without any kind of limitation.
383 unsigned getMaxWavesPerEU() const {
384 if (getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
386 // FIXME: Need to take scratch memory into account.
390 /// \returns Maximum number of waves per execution unit supported by the
391 /// subtarget and limited by given flat work group size.
392 unsigned getMaxWavesPerEU(unsigned FlatWorkGroupSize) const {
393 return alignTo(getMaxWavesPerCU(FlatWorkGroupSize), getEUsPerCU()) /
397 /// \returns Minimum flat work group size supported by the subtarget.
398 unsigned getMinFlatWorkGroupSize() const {
402 /// \returns Maximum flat work group size supported by the subtarget.
403 unsigned getMaxFlatWorkGroupSize() const {
407 /// \returns Number of waves per work group given the flat work group size.
408 unsigned getWavesPerWorkGroup(unsigned FlatWorkGroupSize) const {
409 return alignTo(FlatWorkGroupSize, getWavefrontSize()) / getWavefrontSize();
412 void setScalarizeGlobalBehavior(bool b) { ScalarizeGlobal = b;}
413 bool getScalarizeGlobalBehavior() const { return ScalarizeGlobal;}
415 /// \returns Subtarget's default pair of minimum/maximum flat work group sizes
416 /// for function \p F, or minimum/maximum flat work group sizes explicitly
417 /// requested using "amdgpu-flat-work-group-size" attribute attached to
420 /// \returns Subtarget's default values if explicitly requested values cannot
421 /// be converted to integer, or violate subtarget's specifications.
422 std::pair<unsigned, unsigned> getFlatWorkGroupSizes(const Function &F) const;
424 /// \returns Subtarget's default pair of minimum/maximum number of waves per
425 /// execution unit for function \p F, or minimum/maximum number of waves per
426 /// execution unit explicitly requested using "amdgpu-waves-per-eu" attribute
427 /// attached to function \p F.
429 /// \returns Subtarget's default values if explicitly requested values cannot
430 /// be converted to integer, violate subtarget's specifications, or are not
431 /// compatible with minimum/maximum number of waves limited by flat work group
432 /// size, register usage, and/or lds usage.
433 std::pair<unsigned, unsigned> getWavesPerEU(const Function &F) const;
436 class R600Subtarget final : public AMDGPUSubtarget {
438 R600InstrInfo InstrInfo;
439 R600FrameLowering FrameLowering;
440 R600TargetLowering TLInfo;
443 R600Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
444 const TargetMachine &TM);
446 const R600InstrInfo *getInstrInfo() const override {
450 const R600FrameLowering *getFrameLowering() const override {
451 return &FrameLowering;
454 const R600TargetLowering *getTargetLowering() const override {
458 const R600RegisterInfo *getRegisterInfo() const override {
459 return &InstrInfo.getRegisterInfo();
462 bool hasCFAluBug() const {
466 bool hasVertexCache() const {
467 return HasVertexCache;
470 short getTexVTXClauseSize() const {
471 return TexVTXClauseSize;
475 class SISubtarget final : public AMDGPUSubtarget {
478 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
479 // doesn't spill SGPRs as much as when 80 is set.
480 FIXED_SGPR_COUNT_FOR_INIT_BUG = 96
484 SIInstrInfo InstrInfo;
485 SIFrameLowering FrameLowering;
486 SITargetLowering TLInfo;
487 std::unique_ptr<GISelAccessor> GISel;
490 SISubtarget(const Triple &TT, StringRef CPU, StringRef FS,
491 const TargetMachine &TM);
493 const SIInstrInfo *getInstrInfo() const override {
497 const SIFrameLowering *getFrameLowering() const override {
498 return &FrameLowering;
501 const SITargetLowering *getTargetLowering() const override {
505 const CallLowering *getCallLowering() const override {
506 assert(GISel && "Access to GlobalISel APIs not set");
507 return GISel->getCallLowering();
510 const SIRegisterInfo *getRegisterInfo() const override {
511 return &InstrInfo.getRegisterInfo();
514 void setGISelAccessor(GISelAccessor &GISel) {
515 this->GISel.reset(&GISel);
518 void overrideSchedPolicy(MachineSchedPolicy &Policy,
519 unsigned NumRegionInstrs) const override;
521 bool isVGPRSpillingEnabled(const Function& F) const;
523 unsigned getMaxNumUserSGPRs() const {
527 bool hasFlatAddressSpace() const {
528 return FlatAddressSpace;
531 bool hasSMemRealTime() const {
532 return HasSMemRealTime;
535 bool hasMovrel() const {
539 bool hasVGPRIndexMode() const {
540 return HasVGPRIndexMode;
543 bool hasScalarCompareEq64() const {
544 return getGeneration() >= VOLCANIC_ISLANDS;
547 bool hasScalarStores() const {
548 return HasScalarStores;
551 bool hasInv2PiInlineImm() const {
552 return HasInv2PiInlineImm;
555 bool enableSIScheduler() const {
556 return EnableSIScheduler;
559 bool debuggerSupported() const {
560 return debuggerInsertNops() && debuggerReserveRegs() &&
561 debuggerEmitPrologue();
564 bool debuggerInsertNops() const {
565 return DebuggerInsertNops;
568 bool debuggerReserveRegs() const {
569 return DebuggerReserveRegs;
572 bool debuggerEmitPrologue() const {
573 return DebuggerEmitPrologue;
576 bool loadStoreOptEnabled() const {
577 return EnableLoadStoreOpt;
580 bool hasSGPRInitBug() const {
584 bool has12DWordStoreHazard() const {
585 return getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS;
588 unsigned getKernArgSegmentSize(unsigned ExplictArgBytes) const;
590 /// Return the maximum number of waves per SIMD for kernels using \p SGPRs SGPRs
591 unsigned getOccupancyWithNumSGPRs(unsigned SGPRs) const;
593 /// Return the maximum number of waves per SIMD for kernels using \p VGPRs VGPRs
594 unsigned getOccupancyWithNumVGPRs(unsigned VGPRs) const;
596 /// \returns True if waitcnt instruction is needed before barrier instruction,
598 bool needWaitcntBeforeBarrier() const {
602 unsigned getMaxNumSGPRs() const;
605 } // end namespace llvm
607 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H