1 //===-- AMDGPUTargetMachine.cpp - TargetMachine for hw codegen targets-----===//
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 The AMDGPU target machine contains all of the hardware specific
12 /// information needed to emit code for R600 and SI GPUs.
14 //===----------------------------------------------------------------------===//
16 #include "AMDGPUTargetMachine.h"
17 #include "AMDGPUTargetObjectFile.h"
19 #include "AMDGPUTargetTransformInfo.h"
20 #include "R600ISelLowering.h"
21 #include "R600InstrInfo.h"
22 #include "R600MachineScheduler.h"
23 #include "SIISelLowering.h"
24 #include "SIInstrInfo.h"
25 #include "llvm/Analysis/Passes.h"
26 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/IR/Verifier.h"
31 #include "llvm/MC/MCAsmInfo.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_os_ostream.h"
35 #include "llvm/Transforms/IPO.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include <llvm/CodeGen/Passes.h>
41 extern "C" void LLVMInitializeAMDGPUTarget() {
42 // Register the target
43 RegisterTargetMachine<R600TargetMachine> X(TheAMDGPUTarget);
44 RegisterTargetMachine<GCNTargetMachine> Y(TheGCNTarget);
46 PassRegistry *PR = PassRegistry::getPassRegistry();
47 initializeSILowerI1CopiesPass(*PR);
48 initializeSIFixSGPRCopiesPass(*PR);
49 initializeSIFoldOperandsPass(*PR);
50 initializeSIFixSGPRLiveRangesPass(*PR);
51 initializeSIFixControlFlowLiveIntervalsPass(*PR);
52 initializeSILoadStoreOptimizerPass(*PR);
53 initializeAMDGPUAnnotateKernelFeaturesPass(*PR);
54 initializeAMDGPUAnnotateUniformValuesPass(*PR);
57 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
58 if (TT.getOS() == Triple::AMDHSA)
59 return make_unique<AMDGPUHSATargetObjectFile>();
61 return make_unique<AMDGPUTargetObjectFile>();
64 static ScheduleDAGInstrs *createR600MachineScheduler(MachineSchedContext *C) {
65 return new ScheduleDAGMILive(C, make_unique<R600SchedStrategy>());
68 static MachineSchedRegistry
69 SchedCustomRegistry("r600", "Run R600's custom scheduler",
70 createR600MachineScheduler);
72 static std::string computeDataLayout(const Triple &TT) {
73 std::string Ret = "e-p:32:32";
75 if (TT.getArch() == Triple::amdgcn) {
76 // 32-bit private, local, and region pointers. 64-bit global and constant.
77 Ret += "-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32-p24:64:64";
80 Ret += "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256"
81 "-v512:512-v1024:1024-v2048:2048-n32:64";
86 AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
87 StringRef CPU, StringRef FS,
88 TargetOptions Options, Reloc::Model RM,
90 CodeGenOpt::Level OptLevel)
91 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, RM, CM,
93 TLOF(createTLOF(getTargetTriple())), Subtarget(TT, CPU, FS, *this),
95 setRequiresStructuredCFG(true);
99 AMDGPUTargetMachine::~AMDGPUTargetMachine() { }
101 //===----------------------------------------------------------------------===//
102 // R600 Target Machine (R600 -> Cayman)
103 //===----------------------------------------------------------------------===//
105 R600TargetMachine::R600TargetMachine(const Target &T, const Triple &TT,
106 StringRef FS, StringRef CPU,
107 TargetOptions Options, Reloc::Model RM,
108 CodeModel::Model CM, CodeGenOpt::Level OL)
109 : AMDGPUTargetMachine(T, TT, FS, CPU, Options, RM, CM, OL) {}
111 //===----------------------------------------------------------------------===//
112 // GCN Target Machine (SI+)
113 //===----------------------------------------------------------------------===//
115 GCNTargetMachine::GCNTargetMachine(const Target &T, const Triple &TT,
116 StringRef FS, StringRef CPU,
117 TargetOptions Options, Reloc::Model RM,
118 CodeModel::Model CM, CodeGenOpt::Level OL)
119 : AMDGPUTargetMachine(T, TT, FS, CPU, Options, RM, CM, OL) {}
121 //===----------------------------------------------------------------------===//
123 //===----------------------------------------------------------------------===//
126 class AMDGPUPassConfig : public TargetPassConfig {
128 AMDGPUPassConfig(TargetMachine *TM, PassManagerBase &PM)
129 : TargetPassConfig(TM, PM) {
131 // Exceptions and StackMaps are not supported, so these passes will never do
133 disablePass(&StackMapLivenessID);
134 disablePass(&FuncletLayoutID);
137 AMDGPUTargetMachine &getAMDGPUTargetMachine() const {
138 return getTM<AMDGPUTargetMachine>();
142 createMachineScheduler(MachineSchedContext *C) const override {
143 const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();
144 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
145 return createR600MachineScheduler(C);
149 void addIRPasses() override;
150 void addCodeGenPrepare() override;
151 bool addPreISel() override;
152 bool addInstSelector() override;
153 bool addGCPasses() override;
156 class R600PassConfig : public AMDGPUPassConfig {
158 R600PassConfig(TargetMachine *TM, PassManagerBase &PM)
159 : AMDGPUPassConfig(TM, PM) { }
161 bool addPreISel() override;
162 void addPreRegAlloc() override;
163 void addPreSched2() override;
164 void addPreEmitPass() override;
167 class GCNPassConfig : public AMDGPUPassConfig {
169 GCNPassConfig(TargetMachine *TM, PassManagerBase &PM)
170 : AMDGPUPassConfig(TM, PM) { }
171 bool addPreISel() override;
172 bool addInstSelector() override;
173 void addFastRegAlloc(FunctionPass *RegAllocPass) override;
174 void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override;
175 void addPreRegAlloc() override;
176 void addPostRegAlloc() override;
177 void addPreSched2() override;
178 void addPreEmitPass() override;
181 } // End of anonymous namespace
183 TargetIRAnalysis AMDGPUTargetMachine::getTargetIRAnalysis() {
184 return TargetIRAnalysis([this](const Function &F) {
185 return TargetTransformInfo(
186 AMDGPUTTIImpl(this, F.getParent()->getDataLayout()));
190 void AMDGPUPassConfig::addIRPasses() {
191 // Function calls are not supported, so make sure we inline everything.
192 addPass(createAMDGPUAlwaysInlinePass());
193 addPass(createAlwaysInlinerPass());
194 // We need to add the barrier noop pass, otherwise adding the function
195 // inlining pass will cause all of the PassConfigs passes to be run
196 // one function at a time, which means if we have a nodule with two
197 // functions, then we will generate code for the first function
198 // without ever running any passes on the second.
199 addPass(createBarrierNoopPass());
201 // Handle uses of OpenCL image2d_t, image3d_t and sampler_t arguments.
202 addPass(createAMDGPUOpenCLImageTypeLoweringPass());
204 TargetPassConfig::addIRPasses();
207 void AMDGPUPassConfig::addCodeGenPrepare() {
208 const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();
209 if (ST.isPromoteAllocaEnabled()) {
210 addPass(createAMDGPUPromoteAlloca(ST));
211 addPass(createSROAPass());
213 TargetPassConfig::addCodeGenPrepare();
217 AMDGPUPassConfig::addPreISel() {
218 const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();
219 addPass(createFlattenCFGPass());
220 if (ST.IsIRStructurizerEnabled())
221 addPass(createStructurizeCFGPass());
225 bool AMDGPUPassConfig::addInstSelector() {
226 addPass(createAMDGPUISelDag(getAMDGPUTargetMachine()));
230 bool AMDGPUPassConfig::addGCPasses() {
231 // Do nothing. GC is not supported.
235 //===----------------------------------------------------------------------===//
237 //===----------------------------------------------------------------------===//
239 bool R600PassConfig::addPreISel() {
240 AMDGPUPassConfig::addPreISel();
241 addPass(createR600TextureIntrinsicsReplacer());
245 void R600PassConfig::addPreRegAlloc() {
246 addPass(createR600VectorRegMerger(*TM));
249 void R600PassConfig::addPreSched2() {
250 const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();
251 addPass(createR600EmitClauseMarkers(), false);
252 if (ST.isIfCvtEnabled())
253 addPass(&IfConverterID, false);
254 addPass(createR600ClauseMergePass(*TM), false);
257 void R600PassConfig::addPreEmitPass() {
258 addPass(createAMDGPUCFGStructurizerPass(), false);
259 addPass(createR600ExpandSpecialInstrsPass(*TM), false);
260 addPass(&FinalizeMachineBundlesID, false);
261 addPass(createR600Packetizer(*TM), false);
262 addPass(createR600ControlFlowFinalizer(*TM), false);
265 TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) {
266 return new R600PassConfig(this, PM);
269 //===----------------------------------------------------------------------===//
271 //===----------------------------------------------------------------------===//
273 bool GCNPassConfig::addPreISel() {
274 AMDGPUPassConfig::addPreISel();
276 // FIXME: We need to run a pass to propagate the attributes when calls are
278 addPass(&AMDGPUAnnotateKernelFeaturesID);
280 addPass(createSinkingPass());
281 addPass(createSITypeRewriter());
282 addPass(createSIAnnotateControlFlowPass());
283 addPass(createAMDGPUAnnotateUniformValues());
288 bool GCNPassConfig::addInstSelector() {
289 AMDGPUPassConfig::addInstSelector();
290 addPass(createSILowerI1CopiesPass());
291 addPass(&SIFixSGPRCopiesID);
292 addPass(createSIFoldOperandsPass());
296 void GCNPassConfig::addPreRegAlloc() {
297 const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();
299 // This needs to be run directly before register allocation because
300 // earlier passes might recompute live intervals.
301 // TODO: handle CodeGenOpt::None; fast RA ignores spill weights set by the pass
302 if (getOptLevel() > CodeGenOpt::None) {
303 insertPass(&MachineSchedulerID, &SIFixControlFlowLiveIntervalsID);
306 if (getOptLevel() > CodeGenOpt::None && ST.loadStoreOptEnabled()) {
307 // Don't do this with no optimizations since it throws away debug info by
308 // merging nonadjacent loads.
310 // This should be run after scheduling, but before register allocation. It
311 // also need extra copies to the address operand to be eliminated.
312 insertPass(&MachineSchedulerID, &SILoadStoreOptimizerID);
313 insertPass(&MachineSchedulerID, &RegisterCoalescerID);
315 addPass(createSIShrinkInstructionsPass(), false);
318 void GCNPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
319 addPass(&SIFixSGPRLiveRangesID);
320 TargetPassConfig::addFastRegAlloc(RegAllocPass);
323 void GCNPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
324 // We want to run this after LiveVariables is computed to avoid computing them
326 // FIXME: We shouldn't disable the verifier here. r249087 introduced a failure
327 // that needs to be fixed.
328 insertPass(&LiveVariablesID, &SIFixSGPRLiveRangesID, /*VerifyAfter=*/false);
329 TargetPassConfig::addOptimizedRegAlloc(RegAllocPass);
332 void GCNPassConfig::addPostRegAlloc() {
333 addPass(createSIShrinkInstructionsPass(), false);
336 void GCNPassConfig::addPreSched2() {
339 void GCNPassConfig::addPreEmitPass() {
340 addPass(createSIInsertWaits(*TM), false);
341 addPass(createSILowerControlFlowPass(*TM), false);
344 TargetPassConfig *GCNTargetMachine::createPassConfig(PassManagerBase &PM) {
345 return new GCNPassConfig(this, PM);