blob: 4c69cbdce446a0f472843a4305eb01be0953108d [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- 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"
Tom Stellard000c5af2016-04-14 19:09:28 +000016#include "AMDGPUCallLowering.h"
Eric Christopherac4b69e2014-07-25 22:22:39 +000017#include "R600ISelLowering.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000018#include "R600InstrInfo.h"
Eric Christopherac4b69e2014-07-25 22:22:39 +000019#include "R600MachineScheduler.h"
Matt Arsenaultf59e5382015-11-06 18:23:00 +000020#include "SIFrameLowering.h"
Eric Christopherac4b69e2014-07-25 22:22:39 +000021#include "SIISelLowering.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "SIInstrInfo.h"
Tom Stellarde99fb652015-01-20 19:33:04 +000023#include "SIMachineFunctionInfo.h"
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000024#include "llvm/ADT/SmallString.h"
Tom Stellard83f0bce2015-01-29 16:55:25 +000025#include "llvm/CodeGen/MachineScheduler.h"
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000026
Tom Stellard75aadc22012-12-11 21:25:42 +000027using namespace llvm;
28
Chandler Carruthe96dd892014-04-21 22:55:11 +000029#define DEBUG_TYPE "amdgpu-subtarget"
30
Tom Stellard75aadc22012-12-11 21:25:42 +000031#define GET_SUBTARGETINFO_ENUM
32#define GET_SUBTARGETINFO_TARGET_DESC
33#define GET_SUBTARGETINFO_CTOR
34#include "AMDGPUGenSubtargetInfo.inc"
35
Tom Stellard000c5af2016-04-14 19:09:28 +000036#ifdef LLVM_BUILD_GLOBAL_ISEL
37namespace {
38struct AMDGPUGISelActualAccessor : public GISelAccessor {
39 std::unique_ptr<CallLowering> CallLoweringInfo;
40 const CallLowering *getCallLowering() const override {
41 return CallLoweringInfo.get();
42 }
43};
44} // End anonymous namespace.
45#endif
46
Eric Christopherac4b69e2014-07-25 22:22:39 +000047AMDGPUSubtarget &
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000048AMDGPUSubtarget::initializeSubtargetDependencies(const Triple &TT,
49 StringRef GPU, StringRef FS) {
Eric Christopherac4b69e2014-07-25 22:22:39 +000050 // Determine default and user-specified characteristics
Matt Arsenaultf171cf22014-07-14 23:40:49 +000051 // On SI+, we want FP64 denormals to be on by default. FP32 denormals can be
52 // enabled, but some instructions do not respect them and they run at the
53 // double precision rate, so don't enable by default.
54 //
55 // We want to be able to turn these off, but making this a subtarget feature
56 // for SI has the unhelpful behavior that it unsets everything else if you
57 // disable it.
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000058
Changpeng Fang71369b32016-05-26 19:35:29 +000059 SmallString<256> FullFS("+promote-alloca,+fp64-denormals,+load-store-opt,");
Changpeng Fangb41574a2015-12-22 20:55:23 +000060 if (isAmdHsaOS()) // Turn on FlatForGlobal for HSA.
61 FullFS += "+flat-for-global,";
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000062 FullFS += FS;
63
64 ParseSubtargetFeatures(GPU, FullFS);
Tom Stellard2e59a452014-06-13 01:32:00 +000065
Eric Christopherac4b69e2014-07-25 22:22:39 +000066 // 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 Stellard2e59a452014-06-13 01:32:00 +000069 if (getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Matt Arsenaultf171cf22014-07-14 23:40:49 +000070 FP32Denormals = false;
71 FP64Denormals = false;
Eric Christopherac4b69e2014-07-25 22:22:39 +000072 }
Matt Arsenault24ee0782016-02-12 02:40:47 +000073
74 // Set defaults if needed.
75 if (MaxPrivateElementSize == 0)
Matt Arsenaulte8ed8e52016-05-11 00:28:54 +000076 MaxPrivateElementSize = 4;
Matt Arsenault24ee0782016-02-12 02:40:47 +000077
Eric Christopherac4b69e2014-07-25 22:22:39 +000078 return *this;
79}
80
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000081AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
Eric Christopherac4b69e2014-07-25 22:22:39 +000082 TargetMachine &TM)
Matt Arsenaultb22828f2016-01-27 02:17:49 +000083 : AMDGPUGenSubtargetInfo(TT, GPU, FS),
Daniel Sanders50f17232015-09-15 16:17:27 +000084 DumpCode(false), R600ALUInst(false), HasVertexCache(false),
85 TexVTXClauseSize(0), Gen(AMDGPUSubtarget::R600), FP64(false),
Matt Arsenaultf639c322016-01-28 20:53:42 +000086 FP64Denormals(false), FP32Denormals(false), FPExceptions(false),
87 FastFMAF32(false), HalfRate64Ops(false), CaymanISA(false),
88 FlatAddressSpace(false), FlatForGlobal(false), EnableIRStructurizer(true),
Matt Arsenaulte83690c2016-01-18 21:13:50 +000089 EnablePromoteAlloca(false),
90 EnableIfCvt(true), EnableLoadStoreOpt(false),
91 EnableUnsafeDSOffsetFolding(false),
Nicolai Haehnle5b504972016-01-04 23:35:53 +000092 EnableXNACK(false),
Matt Arsenaulte83690c2016-01-18 21:13:50 +000093 WavefrontSize(0), CFALUBug(false),
Matt Arsenault24ee0782016-02-12 02:40:47 +000094 LocalMemorySize(0), MaxPrivateElementSize(0),
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000095 EnableVGPRSpilling(false), SGPRInitBug(false), IsGCN(false),
Matt Arsenault9d82ee72016-02-27 08:53:55 +000096 GCN1Encoding(false), GCN3Encoding(false), CIInsts(false),
97 HasSMemRealTime(false), Has16BitInsts(false),
Matt Arsenault61738cb2016-02-27 08:53:46 +000098 LDSBankCount(0),
Matt Arsenault3a619852016-02-27 20:26:57 +000099 IsaVersion(ISAVersion0_0_0),
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +0000100 EnableSIScheduler(false),
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000101 DebuggerInsertNops(false), DebuggerReserveRegs(false),
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +0000102 FrameLowering(nullptr),
Tom Stellard000c5af2016-04-14 19:09:28 +0000103 GISel(),
Eric Christopher111de892015-02-19 00:15:33 +0000104 InstrItins(getInstrItineraryForCPU(GPU)), TargetTriple(TT) {
Tom Stellard40ce8af2015-01-28 16:04:26 +0000105
106 initializeSubtargetDependencies(TT, GPU, FS);
107
Matt Arsenault0c90e952015-11-06 18:17:45 +0000108 const unsigned MaxStackAlign = 64 * 16; // Maximum stack alignment (long16)
109
Eric Christopherac4b69e2014-07-25 22:22:39 +0000110 if (getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
111 InstrInfo.reset(new R600InstrInfo(*this));
Eric Christopher7792e322015-01-30 23:24:40 +0000112 TLInfo.reset(new R600TargetLowering(TM, *this));
Matt Arsenault0c90e952015-11-06 18:17:45 +0000113
114 // FIXME: Should have R600 specific FrameLowering
115 FrameLowering.reset(new AMDGPUFrameLowering(
116 TargetFrameLowering::StackGrowsUp,
117 MaxStackAlign,
118 0));
Tom Stellard2e59a452014-06-13 01:32:00 +0000119 } else {
120 InstrInfo.reset(new SIInstrInfo(*this));
Eric Christopher7792e322015-01-30 23:24:40 +0000121 TLInfo.reset(new SITargetLowering(TM, *this));
Matt Arsenault0c90e952015-11-06 18:17:45 +0000122 FrameLowering.reset(new SIFrameLowering(
123 TargetFrameLowering::StackGrowsUp,
124 MaxStackAlign,
125 0));
Tom Stellard000c5af2016-04-14 19:09:28 +0000126#ifndef LLVM_BUILD_GLOBAL_ISEL
127 GISelAccessor *GISel = new GISelAccessor();
128#else
129 AMDGPUGISelActualAccessor *GISel =
130 new AMDGPUGISelActualAccessor();
131 GISel->CallLoweringInfo.reset(
132 new AMDGPUCallLowering(*getTargetLowering()));
133#endif
134 setGISelAccessor(*GISel);
Tom Stellard2e59a452014-06-13 01:32:00 +0000135 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000136}
137
Tom Stellard000c5af2016-04-14 19:09:28 +0000138const CallLowering *AMDGPUSubtarget::getCallLowering() const {
139 assert(GISel && "Access to GlobalISel APIs not set");
140 return GISel->getCallLowering();
141}
142
Matt Arsenaultd782d052014-06-27 17:57:00 +0000143unsigned AMDGPUSubtarget::getStackEntrySize() const {
Tom Stellarda40f9712014-01-22 21:55:43 +0000144 assert(getGeneration() <= NORTHERN_ISLANDS);
145 switch(getWavefrontSize()) {
146 case 16:
147 return 8;
148 case 32:
Matt Arsenaultd782d052014-06-27 17:57:00 +0000149 return hasCaymanISA() ? 4 : 8;
Tom Stellarda40f9712014-01-22 21:55:43 +0000150 case 64:
151 return 4;
152 default:
153 llvm_unreachable("Illegal wavefront size.");
154 }
155}
Tom Stellardb8fd6ef2014-12-02 22:00:07 +0000156
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000157// FIXME: These limits are for SI. Did they change with the larger maximum LDS
158// size?
159unsigned AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves) const {
160 switch (NWaves) {
161 case 10:
162 return 1638;
163 case 9:
164 return 1820;
165 case 8:
166 return 2048;
167 case 7:
168 return 2340;
169 case 6:
170 return 2730;
171 case 5:
172 return 3276;
173 case 4:
174 return 4096;
175 case 3:
176 return 5461;
177 case 2:
178 return 8192;
179 default:
180 return getLocalMemorySize();
181 }
182}
183
184unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes) const {
185 if (Bytes <= 1638)
186 return 10;
187
188 if (Bytes <= 1820)
189 return 9;
190
191 if (Bytes <= 2048)
192 return 8;
193
194 if (Bytes <= 2340)
195 return 7;
196
197 if (Bytes <= 2730)
198 return 6;
199
200 if (Bytes <= 3276)
201 return 5;
202
203 if (Bytes <= 4096)
204 return 4;
205
206 if (Bytes <= 5461)
207 return 3;
208
209 if (Bytes <= 8192)
210 return 2;
211
212 return 1;
213}
214
Tom Stellardb8fd6ef2014-12-02 22:00:07 +0000215unsigned AMDGPUSubtarget::getAmdKernelCodeChipID() const {
216 switch(getGeneration()) {
217 default: llvm_unreachable("ChipID unknown");
218 case SEA_ISLANDS: return 12;
219 }
220}
Tom Stellarde99fb652015-01-20 19:33:04 +0000221
Tom Stellard347ac792015-06-26 21:15:07 +0000222AMDGPU::IsaVersion AMDGPUSubtarget::getIsaVersion() const {
223 return AMDGPU::getIsaVersion(getFeatureBits());
224}
225
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000226bool AMDGPUSubtarget::isVGPRSpillingEnabled(const Function& F) const {
227 return !AMDGPU::isShader(F.getCallingConv()) || EnableVGPRSpilling;
Tom Stellarde99fb652015-01-20 19:33:04 +0000228}
Tom Stellard83f0bce2015-01-29 16:55:25 +0000229
230void AMDGPUSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
231 MachineInstr *begin,
232 MachineInstr *end,
233 unsigned NumRegionInstrs) const {
234 if (getGeneration() >= SOUTHERN_ISLANDS) {
235
236 // Track register pressure so the scheduler can try to decrease
237 // pressure once register usage is above the threshold defined by
238 // SIRegisterInfo::getRegPressureSetLimit()
239 Policy.ShouldTrackPressure = true;
240
241 // Enabling both top down and bottom up scheduling seems to give us less
242 // register spills than just using one of these approaches on its own.
243 Policy.OnlyTopDown = false;
244 Policy.OnlyBottomUp = false;
Tom Stellard0bc954e2016-03-30 16:35:09 +0000245
246 // Enabling ShouldTrackLaneMasks crashes the SI Machine Scheduler.
247 if (!enableSIScheduler())
248 Policy.ShouldTrackLaneMasks = true;
Tom Stellard83f0bce2015-01-29 16:55:25 +0000249 }
250}
Tom Stellard347ac792015-06-26 21:15:07 +0000251