Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1 | //===-- AMDGPUTargetMachine.cpp - TargetMachine for hw codegen targets-----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// The AMDGPU target machine contains all of the hardware specific |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 11 | /// information needed to emit code for R600 and SI GPUs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AMDGPUTargetMachine.h" |
| 16 | #include "AMDGPU.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 17 | #include "AMDGPUAliasAnalysis.h" |
Matt Arsenault | eb9025d | 2016-06-28 17:42:09 +0000 | [diff] [blame] | 18 | #include "AMDGPUCallLowering.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 19 | #include "AMDGPUInstructionSelector.h" |
| 20 | #include "AMDGPULegalizerInfo.h" |
Matt Arsenault | 9aa45f0 | 2017-07-06 20:57:05 +0000 | [diff] [blame] | 21 | #include "AMDGPUMacroFusion.h" |
Matt Arsenault | eb9025d | 2016-06-28 17:42:09 +0000 | [diff] [blame] | 22 | #include "AMDGPUTargetObjectFile.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 23 | #include "AMDGPUTargetTransformInfo.h" |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 24 | #include "GCNIterativeScheduler.h" |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 25 | #include "GCNSchedStrategy.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 26 | #include "R600MachineScheduler.h" |
Matt Arsenault | 2ffe8fd | 2016-08-11 19:18:50 +0000 | [diff] [blame] | 27 | #include "SIMachineScheduler.h" |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/GlobalISel/IRTranslator.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/GlobalISel/Legalizer.h" |
| 31 | #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/Passes.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/TargetPassConfig.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Attributes.h" |
| 35 | #include "llvm/IR/Function.h" |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 36 | #include "llvm/IR/LegacyPassManager.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
| 38 | #include "llvm/Support/CommandLine.h" |
| 39 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 40 | #include "llvm/Support/TargetRegistry.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/IPO.h" |
| 43 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
| 44 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 45 | #include "llvm/Transforms/Scalar.h" |
| 46 | #include "llvm/Transforms/Scalar/GVN.h" |
Sameer Sahasrabuddhe | b4f2d1c | 2018-09-25 09:39:21 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Vectorize.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 49 | #include <memory> |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace llvm; |
| 52 | |
Matt Arsenault | c581611 | 2016-06-24 06:30:22 +0000 | [diff] [blame] | 53 | static cl::opt<bool> EnableR600StructurizeCFG( |
| 54 | "r600-ir-structurize", |
| 55 | cl::desc("Use StructurizeCFG IR pass"), |
| 56 | cl::init(true)); |
| 57 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 58 | static cl::opt<bool> EnableSROA( |
| 59 | "amdgpu-sroa", |
| 60 | cl::desc("Run SROA after promote alloca pass"), |
| 61 | cl::ReallyHidden, |
| 62 | cl::init(true)); |
| 63 | |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | EnableEarlyIfConversion("amdgpu-early-ifcvt", cl::Hidden, |
| 66 | cl::desc("Run early if-conversion"), |
| 67 | cl::init(false)); |
| 68 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 69 | static cl::opt<bool> EnableR600IfConvert( |
| 70 | "r600-if-convert", |
| 71 | cl::desc("Use if conversion pass"), |
| 72 | cl::ReallyHidden, |
| 73 | cl::init(true)); |
| 74 | |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 75 | // Option to disable vectorizer for tests. |
| 76 | static cl::opt<bool> EnableLoadStoreVectorizer( |
| 77 | "amdgpu-load-store-vectorizer", |
| 78 | cl::desc("Enable load store vectorizer"), |
Matt Arsenault | 0efdd06 | 2016-09-09 22:29:28 +0000 | [diff] [blame] | 79 | cl::init(true), |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 80 | cl::Hidden); |
| 81 | |
Hiroshi Inoue | c8e9245 | 2018-01-29 05:17:03 +0000 | [diff] [blame] | 82 | // Option to control global loads scalarization |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 83 | static cl::opt<bool> ScalarizeGlobal( |
| 84 | "amdgpu-scalarize-global-loads", |
| 85 | cl::desc("Enable global load scalarization"), |
Alexander Timofeev | 982aee6 | 2017-07-04 17:32:00 +0000 | [diff] [blame] | 86 | cl::init(true), |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 87 | cl::Hidden); |
| 88 | |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 89 | // Option to run internalize pass. |
| 90 | static cl::opt<bool> InternalizeSymbols( |
| 91 | "amdgpu-internalize-symbols", |
| 92 | cl::desc("Enable elimination of non-kernel functions and unused globals"), |
| 93 | cl::init(false), |
| 94 | cl::Hidden); |
| 95 | |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 96 | // Option to inline all early. |
| 97 | static cl::opt<bool> EarlyInlineAll( |
| 98 | "amdgpu-early-inline-all", |
| 99 | cl::desc("Inline all functions early"), |
| 100 | cl::init(false), |
| 101 | cl::Hidden); |
| 102 | |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 103 | static cl::opt<bool> EnableSDWAPeephole( |
| 104 | "amdgpu-sdwa-peephole", |
| 105 | cl::desc("Enable SDWA peepholer"), |
Sam Kolton | 9fa1696 | 2017-04-06 15:03:28 +0000 | [diff] [blame] | 106 | cl::init(true)); |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 107 | |
Valery Pykhtin | 3d9afa2 | 2018-11-30 14:21:56 +0000 | [diff] [blame] | 108 | static cl::opt<bool> EnableDPPCombine( |
| 109 | "amdgpu-dpp-combine", |
| 110 | cl::desc("Enable DPP combiner"), |
Valery Pykhtin | ded96df | 2019-02-11 11:15:03 +0000 | [diff] [blame] | 111 | cl::init(true)); |
Valery Pykhtin | 3d9afa2 | 2018-11-30 14:21:56 +0000 | [diff] [blame] | 112 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 113 | // Enable address space based alias analysis |
| 114 | static cl::opt<bool> EnableAMDGPUAliasAnalysis("enable-amdgpu-aa", cl::Hidden, |
| 115 | cl::desc("Enable AMDGPU Alias Analysis"), |
| 116 | cl::init(true)); |
| 117 | |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 118 | // Option to run late CFG structurizer |
Matt Arsenault | cc85223 | 2017-10-10 20:22:07 +0000 | [diff] [blame] | 119 | static cl::opt<bool, true> LateCFGStructurize( |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 120 | "amdgpu-late-structurize", |
| 121 | cl::desc("Enable late CFG structurization"), |
Matt Arsenault | cc85223 | 2017-10-10 20:22:07 +0000 | [diff] [blame] | 122 | cl::location(AMDGPUTargetMachine::EnableLateStructurizeCFG), |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 123 | cl::Hidden); |
| 124 | |
Matt Arsenault | 5d567dc | 2019-02-28 00:40:32 +0000 | [diff] [blame^] | 125 | static cl::opt<bool, true> EnableAMDGPUFunctionCallsOpt( |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 126 | "amdgpu-function-calls", |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 127 | cl::desc("Enable AMDGPU function call support"), |
Matt Arsenault | a680199 | 2018-07-10 14:03:41 +0000 | [diff] [blame] | 128 | cl::location(AMDGPUTargetMachine::EnableFunctionCalls), |
Matt Arsenault | 5d567dc | 2019-02-28 00:40:32 +0000 | [diff] [blame^] | 129 | cl::init(true), |
Matt Arsenault | a680199 | 2018-07-10 14:03:41 +0000 | [diff] [blame] | 130 | cl::Hidden); |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 131 | |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 132 | // Enable lib calls simplifications |
| 133 | static cl::opt<bool> EnableLibCallSimplify( |
| 134 | "amdgpu-simplify-libcall", |
Matt Arsenault | 2e4d338 | 2018-05-29 19:35:46 +0000 | [diff] [blame] | 135 | cl::desc("Enable amdgpu library simplifications"), |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 136 | cl::init(true), |
| 137 | cl::Hidden); |
| 138 | |
Matt Arsenault | 8c4a352 | 2018-06-26 19:10:00 +0000 | [diff] [blame] | 139 | static cl::opt<bool> EnableLowerKernelArguments( |
| 140 | "amdgpu-ir-lower-kernel-arguments", |
| 141 | cl::desc("Lower kernel argument loads in IR pass"), |
| 142 | cl::init(true), |
| 143 | cl::Hidden); |
| 144 | |
Neil Henning | 6641657 | 2018-10-08 15:49:19 +0000 | [diff] [blame] | 145 | // Enable atomic optimization |
| 146 | static cl::opt<bool> EnableAtomicOptimizations( |
| 147 | "amdgpu-atomic-optimizations", |
| 148 | cl::desc("Enable atomic optimizations"), |
| 149 | cl::init(false), |
| 150 | cl::Hidden); |
| 151 | |
Tim Corringham | 4c4d2fe | 2018-12-10 12:06:10 +0000 | [diff] [blame] | 152 | // Enable Mode register optimization |
| 153 | static cl::opt<bool> EnableSIModeRegisterPass( |
| 154 | "amdgpu-mode-register", |
| 155 | cl::desc("Enable mode register pass"), |
| 156 | cl::init(true), |
| 157 | cl::Hidden); |
| 158 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 159 | extern "C" void LLVMInitializeAMDGPUTarget() { |
| 160 | // Register the target |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 161 | RegisterTargetMachine<R600TargetMachine> X(getTheAMDGPUTarget()); |
| 162 | RegisterTargetMachine<GCNTargetMachine> Y(getTheGCNTarget()); |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 163 | |
| 164 | PassRegistry *PR = PassRegistry::getPassRegistry(); |
Tom Stellard | a2f57be | 2017-08-02 22:19:45 +0000 | [diff] [blame] | 165 | initializeR600ClauseMergePassPass(*PR); |
| 166 | initializeR600ControlFlowFinalizerPass(*PR); |
| 167 | initializeR600PacketizerPass(*PR); |
| 168 | initializeR600ExpandSpecialInstrsPassPass(*PR); |
| 169 | initializeR600VectorRegMergerPass(*PR); |
Tom Stellard | e753c52 | 2018-04-09 16:09:13 +0000 | [diff] [blame] | 170 | initializeGlobalISel(*PR); |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 171 | initializeAMDGPUDAGToDAGISelPass(*PR); |
Valery Pykhtin | 3d9afa2 | 2018-11-30 14:21:56 +0000 | [diff] [blame] | 172 | initializeGCNDPPCombinePass(*PR); |
Matt Arsenault | 8c0ef8b | 2015-10-12 17:43:59 +0000 | [diff] [blame] | 173 | initializeSILowerI1CopiesPass(*PR); |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 174 | initializeSIFixSGPRCopiesPass(*PR); |
Stanislav Mekhanoshin | 22a56f2 | 2017-01-24 17:46:17 +0000 | [diff] [blame] | 175 | initializeSIFixVGPRCopiesPass(*PR); |
Ron Lieberman | cac749a | 2018-11-16 01:13:34 +0000 | [diff] [blame] | 176 | initializeSIFixupVectorISelPass(*PR); |
Matt Arsenault | 8c0ef8b | 2015-10-12 17:43:59 +0000 | [diff] [blame] | 177 | initializeSIFoldOperandsPass(*PR); |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 178 | initializeSIPeepholeSDWAPass(*PR); |
Matt Arsenault | c3a01ec | 2016-06-09 23:18:47 +0000 | [diff] [blame] | 179 | initializeSIShrinkInstructionsPass(*PR); |
Stanislav Mekhanoshin | 37e7f95 | 2017-08-01 23:14:32 +0000 | [diff] [blame] | 180 | initializeSIOptimizeExecMaskingPreRAPass(*PR); |
Matt Arsenault | 187276f | 2015-10-07 00:42:53 +0000 | [diff] [blame] | 181 | initializeSILoadStoreOptimizerPass(*PR); |
Scott Linder | 11ef798 | 2018-10-26 13:18:36 +0000 | [diff] [blame] | 182 | initializeAMDGPUFixFunctionBitcastsPass(*PR); |
Matt Arsenault | 746e065 | 2017-06-02 18:02:42 +0000 | [diff] [blame] | 183 | initializeAMDGPUAlwaysInlinePass(*PR); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 184 | initializeAMDGPUAnnotateKernelFeaturesPass(*PR); |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 185 | initializeAMDGPUAnnotateUniformValuesPass(*PR); |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 186 | initializeAMDGPUArgumentUsageInfoPass(*PR); |
Neil Henning | 6641657 | 2018-10-08 15:49:19 +0000 | [diff] [blame] | 187 | initializeAMDGPUAtomicOptimizerPass(*PR); |
Matt Arsenault | 8c4a352 | 2018-06-26 19:10:00 +0000 | [diff] [blame] | 188 | initializeAMDGPULowerKernelArgumentsPass(*PR); |
Matt Arsenault | 372d796 | 2018-05-18 21:35:00 +0000 | [diff] [blame] | 189 | initializeAMDGPULowerKernelAttributesPass(*PR); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 190 | initializeAMDGPULowerIntrinsicsPass(*PR); |
Yaxun Liu | de4b88d | 2017-10-10 19:39:48 +0000 | [diff] [blame] | 191 | initializeAMDGPUOpenCLEnqueuedBlockLoweringPass(*PR); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 192 | initializeAMDGPUPromoteAllocaPass(*PR); |
Matt Arsenault | 86de486 | 2016-06-24 07:07:55 +0000 | [diff] [blame] | 193 | initializeAMDGPUCodeGenPreparePass(*PR); |
Matt Arsenault | c06574f | 2017-07-28 18:40:05 +0000 | [diff] [blame] | 194 | initializeAMDGPURewriteOutArgumentsPass(*PR); |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 195 | initializeAMDGPUUnifyMetadataPass(*PR); |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 196 | initializeSIAnnotateControlFlowPass(*PR); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 197 | initializeSIInsertWaitcntsPass(*PR); |
Tim Corringham | 4c4d2fe | 2018-12-10 12:06:10 +0000 | [diff] [blame] | 198 | initializeSIModeRegisterPass(*PR); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 199 | initializeSIWholeQuadModePass(*PR); |
Matt Arsenault | 55d49cf | 2016-02-12 02:16:10 +0000 | [diff] [blame] | 200 | initializeSILowerControlFlowPass(*PR); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 201 | initializeSIInsertSkipsPass(*PR); |
Konstantin Zhuravlyov | e9a5a77 | 2017-07-21 21:19:23 +0000 | [diff] [blame] | 202 | initializeSIMemoryLegalizerPass(*PR); |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 203 | initializeSIOptimizeExecMaskingPass(*PR); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 204 | initializeSIFixWWMLivenessPass(*PR); |
Stanislav Mekhanoshin | 739174c | 2018-05-31 20:13:51 +0000 | [diff] [blame] | 205 | initializeSIFormMemoryClausesPass(*PR); |
Matt Arsenault | b8f8dbc | 2017-03-24 19:52:05 +0000 | [diff] [blame] | 206 | initializeAMDGPUUnifyDivergentExitNodesPass(*PR); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 207 | initializeAMDGPUAAWrapperPassPass(*PR); |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 208 | initializeAMDGPUExternalAAWrapperPass(*PR); |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 209 | initializeAMDGPUUseNativeCallsPass(*PR); |
| 210 | initializeAMDGPUSimplifyLibCallsPass(*PR); |
Stanislav Mekhanoshin | 5670e6d | 2017-09-20 04:25:58 +0000 | [diff] [blame] | 211 | initializeAMDGPUInlinerPass(*PR); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Tom Stellard | e135ffd | 2015-09-25 21:41:28 +0000 | [diff] [blame] | 214 | static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 215 | return llvm::make_unique<AMDGPUTargetObjectFile>(); |
Tom Stellard | e135ffd | 2015-09-25 21:41:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 218 | static ScheduleDAGInstrs *createR600MachineScheduler(MachineSchedContext *C) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 219 | return new ScheduleDAGMILive(C, llvm::make_unique<R600SchedStrategy>()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Matt Arsenault | 2ffe8fd | 2016-08-11 19:18:50 +0000 | [diff] [blame] | 222 | static ScheduleDAGInstrs *createSIMachineScheduler(MachineSchedContext *C) { |
| 223 | return new SIScheduleDAGMI(C); |
| 224 | } |
| 225 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 226 | static ScheduleDAGInstrs * |
| 227 | createGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) { |
| 228 | ScheduleDAGMILive *DAG = |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 229 | new GCNScheduleDAGMILive(C, make_unique<GCNMaxOccupancySchedStrategy>(C)); |
Matthias Braun | 115efcd | 2016-11-28 20:11:54 +0000 | [diff] [blame] | 230 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 231 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
Matt Arsenault | 9aa45f0 | 2017-07-06 20:57:05 +0000 | [diff] [blame] | 232 | DAG->addMutation(createAMDGPUMacroFusionDAGMutation()); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 233 | return DAG; |
| 234 | } |
| 235 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 236 | static ScheduleDAGInstrs * |
| 237 | createIterativeGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) { |
| 238 | auto DAG = new GCNIterativeScheduler(C, |
| 239 | GCNIterativeScheduler::SCHEDULE_LEGACYMAXOCCUPANCY); |
| 240 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 241 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 242 | return DAG; |
| 243 | } |
| 244 | |
| 245 | static ScheduleDAGInstrs *createMinRegScheduler(MachineSchedContext *C) { |
| 246 | return new GCNIterativeScheduler(C, |
| 247 | GCNIterativeScheduler::SCHEDULE_MINREGFORCED); |
| 248 | } |
| 249 | |
Valery Pykhtin | f2fe972 | 2017-11-20 14:35:53 +0000 | [diff] [blame] | 250 | static ScheduleDAGInstrs * |
| 251 | createIterativeILPMachineScheduler(MachineSchedContext *C) { |
| 252 | auto DAG = new GCNIterativeScheduler(C, |
| 253 | GCNIterativeScheduler::SCHEDULE_ILP); |
| 254 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 255 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 256 | DAG->addMutation(createAMDGPUMacroFusionDAGMutation()); |
| 257 | return DAG; |
| 258 | } |
| 259 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 260 | static MachineSchedRegistry |
Nicolai Haehnle | 02c3291 | 2016-01-13 16:10:10 +0000 | [diff] [blame] | 261 | R600SchedRegistry("r600", "Run R600's custom scheduler", |
| 262 | createR600MachineScheduler); |
| 263 | |
| 264 | static MachineSchedRegistry |
| 265 | SISchedRegistry("si", "Run SI's custom scheduler", |
| 266 | createSIMachineScheduler); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 267 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 268 | static MachineSchedRegistry |
| 269 | GCNMaxOccupancySchedRegistry("gcn-max-occupancy", |
| 270 | "Run GCN scheduler to maximize occupancy", |
| 271 | createGCNMaxOccupancyMachineScheduler); |
| 272 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 273 | static MachineSchedRegistry |
| 274 | IterativeGCNMaxOccupancySchedRegistry("gcn-max-occupancy-experimental", |
| 275 | "Run GCN scheduler to maximize occupancy (experimental)", |
| 276 | createIterativeGCNMaxOccupancyMachineScheduler); |
| 277 | |
| 278 | static MachineSchedRegistry |
| 279 | GCNMinRegSchedRegistry("gcn-minreg", |
| 280 | "Run GCN iterative scheduler for minimal register usage (experimental)", |
| 281 | createMinRegScheduler); |
| 282 | |
Valery Pykhtin | f2fe972 | 2017-11-20 14:35:53 +0000 | [diff] [blame] | 283 | static MachineSchedRegistry |
| 284 | GCNILPSchedRegistry("gcn-ilp", |
| 285 | "Run GCN iterative scheduler for ILP scheduling (experimental)", |
| 286 | createIterativeILPMachineScheduler); |
| 287 | |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 288 | static StringRef computeDataLayout(const Triple &TT) { |
| 289 | if (TT.getArch() == Triple::r600) { |
| 290 | // 32-bit pointers. |
Yaxun Liu | cc56a8b | 2017-11-06 14:32:33 +0000 | [diff] [blame] | 291 | return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
Matt Arsenault | 95329f8 | 2018-03-27 19:26:40 +0000 | [diff] [blame] | 292 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5"; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 295 | // 32-bit private, local, and region pointers. 64-bit global, constant and |
| 296 | // flat. |
Yaxun Liu | 0124b54 | 2018-02-13 18:00:25 +0000 | [diff] [blame] | 297 | return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32" |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 298 | "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
Matt Arsenault | 95329f8 | 2018-03-27 19:26:40 +0000 | [diff] [blame] | 299 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5"; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 302 | LLVM_READNONE |
| 303 | static StringRef getGPUOrDefault(const Triple &TT, StringRef GPU) { |
| 304 | if (!GPU.empty()) |
| 305 | return GPU; |
| 306 | |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 307 | if (TT.getArch() == Triple::amdgcn) |
Matt Arsenault | 8728c5f | 2017-08-07 14:58:04 +0000 | [diff] [blame] | 308 | return "generic"; |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 309 | |
Matt Arsenault | 8e00194 | 2016-06-02 18:37:16 +0000 | [diff] [blame] | 310 | return "r600"; |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 313 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 314 | // The AMDGPU toolchain only supports generating shared objects, so we |
| 315 | // must always use PIC. |
| 316 | return Reloc::PIC_; |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 319 | AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT, |
| 320 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 321 | TargetOptions Options, |
| 322 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 323 | Optional<CodeModel::Model> CM, |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 324 | CodeGenOpt::Level OptLevel) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 325 | : LLVMTargetMachine(T, computeDataLayout(TT), TT, getGPUOrDefault(TT, CPU), |
| 326 | FS, Options, getEffectiveRelocModel(RM), |
David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 327 | getEffectiveCodeModel(CM, CodeModel::Small), OptLevel), |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 328 | TLOF(createTLOF(getTargetTriple())) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 329 | initAsmInfo(); |
| 330 | } |
| 331 | |
Vlad Tsyrklevich | 688e752 | 2018-07-10 00:46:07 +0000 | [diff] [blame] | 332 | bool AMDGPUTargetMachine::EnableLateStructurizeCFG = false; |
Matt Arsenault | a680199 | 2018-07-10 14:03:41 +0000 | [diff] [blame] | 333 | bool AMDGPUTargetMachine::EnableFunctionCalls = false; |
| 334 | |
| 335 | AMDGPUTargetMachine::~AMDGPUTargetMachine() = default; |
Vlad Tsyrklevich | 688e752 | 2018-07-10 00:46:07 +0000 | [diff] [blame] | 336 | |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 337 | StringRef AMDGPUTargetMachine::getGPUName(const Function &F) const { |
| 338 | Attribute GPUAttr = F.getFnAttribute("target-cpu"); |
| 339 | return GPUAttr.hasAttribute(Attribute::None) ? |
| 340 | getTargetCPU() : GPUAttr.getValueAsString(); |
| 341 | } |
| 342 | |
| 343 | StringRef AMDGPUTargetMachine::getFeatureString(const Function &F) const { |
| 344 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| 345 | |
| 346 | return FSAttr.hasAttribute(Attribute::None) ? |
| 347 | getTargetFeatureString() : |
| 348 | FSAttr.getValueAsString(); |
| 349 | } |
| 350 | |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 351 | /// Predicate for Internalize pass. |
Benjamin Kramer | f9ab3dd | 2017-10-31 23:21:30 +0000 | [diff] [blame] | 352 | static bool mustPreserveGV(const GlobalValue &GV) { |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 353 | if (const Function *F = dyn_cast<Function>(&GV)) |
| 354 | return F->isDeclaration() || AMDGPU::isEntryFunctionCC(F->getCallingConv()); |
| 355 | |
| 356 | return !GV.use_empty(); |
| 357 | } |
| 358 | |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 359 | void AMDGPUTargetMachine::adjustPassManager(PassManagerBuilder &Builder) { |
Stanislav Mekhanoshin | ee2dd78 | 2017-03-17 17:13:41 +0000 | [diff] [blame] | 360 | Builder.DivergentTarget = true; |
| 361 | |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 362 | bool EnableOpt = getOptLevel() > CodeGenOpt::None; |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 363 | bool Internalize = InternalizeSymbols; |
Matt Arsenault | 5d567dc | 2019-02-28 00:40:32 +0000 | [diff] [blame^] | 364 | bool EarlyInline = EarlyInlineAll && EnableOpt && !EnableFunctionCalls; |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 365 | bool AMDGPUAA = EnableAMDGPUAliasAnalysis && EnableOpt; |
| 366 | bool LibCallSimplify = EnableLibCallSimplify && EnableOpt; |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 367 | |
Matt Arsenault | 5d567dc | 2019-02-28 00:40:32 +0000 | [diff] [blame^] | 368 | if (EnableFunctionCalls) { |
Stanislav Mekhanoshin | 2e3bf37 | 2017-09-20 06:34:28 +0000 | [diff] [blame] | 369 | delete Builder.Inliner; |
Stanislav Mekhanoshin | 5641820 | 2017-09-20 06:10:15 +0000 | [diff] [blame] | 370 | Builder.Inliner = createAMDGPUFunctionInliningPass(); |
Stanislav Mekhanoshin | 2e3bf37 | 2017-09-20 06:34:28 +0000 | [diff] [blame] | 371 | } |
Stanislav Mekhanoshin | 5670e6d | 2017-09-20 04:25:58 +0000 | [diff] [blame] | 372 | |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 373 | Builder.addExtension( |
Stanislav Mekhanoshin | f6c1feb | 2017-01-27 16:38:10 +0000 | [diff] [blame] | 374 | PassManagerBuilder::EP_ModuleOptimizerEarly, |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 375 | [Internalize, EarlyInline, AMDGPUAA](const PassManagerBuilder &, |
| 376 | legacy::PassManagerBase &PM) { |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 377 | if (AMDGPUAA) { |
| 378 | PM.add(createAMDGPUAAWrapperPass()); |
| 379 | PM.add(createAMDGPUExternalAAWrapperPass()); |
| 380 | } |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 381 | PM.add(createAMDGPUUnifyMetadataPass()); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 382 | if (Internalize) { |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 383 | PM.add(createInternalizePass(mustPreserveGV)); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 384 | PM.add(createGlobalDCEPass()); |
| 385 | } |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 386 | if (EarlyInline) |
Stanislav Mekhanoshin | 89653df | 2017-03-30 20:16:02 +0000 | [diff] [blame] | 387 | PM.add(createAMDGPUAlwaysInlinePass(false)); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 388 | }); |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 389 | |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 390 | const auto &Opt = Options; |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 391 | Builder.addExtension( |
| 392 | PassManagerBuilder::EP_EarlyAsPossible, |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 393 | [AMDGPUAA, LibCallSimplify, &Opt](const PassManagerBuilder &, |
| 394 | legacy::PassManagerBase &PM) { |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 395 | if (AMDGPUAA) { |
| 396 | PM.add(createAMDGPUAAWrapperPass()); |
| 397 | PM.add(createAMDGPUExternalAAWrapperPass()); |
| 398 | } |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 399 | PM.add(llvm::createAMDGPUUseNativeCallsPass()); |
| 400 | if (LibCallSimplify) |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 401 | PM.add(llvm::createAMDGPUSimplifyLibCallsPass(Opt)); |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 402 | }); |
Stanislav Mekhanoshin | 50c2f25 | 2017-06-19 23:17:36 +0000 | [diff] [blame] | 403 | |
| 404 | Builder.addExtension( |
| 405 | PassManagerBuilder::EP_CGSCCOptimizerLate, |
| 406 | [](const PassManagerBuilder &, legacy::PassManagerBase &PM) { |
| 407 | // Add infer address spaces pass to the opt pipeline after inlining |
| 408 | // but before SROA to increase SROA opportunities. |
| 409 | PM.add(createInferAddressSpacesPass()); |
Matt Arsenault | 372d796 | 2018-05-18 21:35:00 +0000 | [diff] [blame] | 410 | |
| 411 | // This should run after inlining to have any chance of doing anything, |
| 412 | // and before other cleanup optimizations. |
| 413 | PM.add(createAMDGPULowerKernelAttributesPass()); |
Stanislav Mekhanoshin | 50c2f25 | 2017-06-19 23:17:36 +0000 | [diff] [blame] | 414 | }); |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 417 | //===----------------------------------------------------------------------===// |
| 418 | // R600 Target Machine (R600 -> Cayman) |
| 419 | //===----------------------------------------------------------------------===// |
| 420 | |
| 421 | R600TargetMachine::R600TargetMachine(const Target &T, const Triple &TT, |
Tom Stellard | 5dde1d2 | 2016-02-05 18:29:17 +0000 | [diff] [blame] | 422 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 423 | TargetOptions Options, |
| 424 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 425 | Optional<CodeModel::Model> CM, |
| 426 | CodeGenOpt::Level OL, bool JIT) |
| 427 | : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) { |
Matt Arsenault | ad55ee5 | 2016-12-06 01:02:51 +0000 | [diff] [blame] | 428 | setRequiresStructuredCFG(true); |
Matt Arsenault | 5d567dc | 2019-02-28 00:40:32 +0000 | [diff] [blame^] | 429 | |
| 430 | // Override the default since calls aren't ssupported for r600. |
| 431 | if (EnableFunctionCalls && |
| 432 | EnableAMDGPUFunctionCallsOpt.getNumOccurrences() == 0) |
| 433 | EnableFunctionCalls = false; |
Matt Arsenault | ad55ee5 | 2016-12-06 01:02:51 +0000 | [diff] [blame] | 434 | } |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 435 | |
| 436 | const R600Subtarget *R600TargetMachine::getSubtargetImpl( |
| 437 | const Function &F) const { |
| 438 | StringRef GPU = getGPUName(F); |
| 439 | StringRef FS = getFeatureString(F); |
| 440 | |
| 441 | SmallString<128> SubtargetKey(GPU); |
| 442 | SubtargetKey.append(FS); |
| 443 | |
| 444 | auto &I = SubtargetMap[SubtargetKey]; |
| 445 | if (!I) { |
| 446 | // This needs to be done before we create a new subtarget since any |
| 447 | // creation will depend on the TM and the code generation flags on the |
| 448 | // function that reside in TargetOptions. |
| 449 | resetTargetOptions(F); |
| 450 | I = llvm::make_unique<R600Subtarget>(TargetTriple, GPU, FS, *this); |
| 451 | } |
| 452 | |
| 453 | return I.get(); |
| 454 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 455 | |
Tom Stellard | c762431 | 2018-05-30 22:55:35 +0000 | [diff] [blame] | 456 | TargetTransformInfo |
| 457 | R600TargetMachine::getTargetTransformInfo(const Function &F) { |
| 458 | return TargetTransformInfo(R600TTIImpl(this, F)); |
| 459 | } |
| 460 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 461 | //===----------------------------------------------------------------------===// |
| 462 | // GCN Target Machine (SI+) |
| 463 | //===----------------------------------------------------------------------===// |
| 464 | |
| 465 | GCNTargetMachine::GCNTargetMachine(const Target &T, const Triple &TT, |
Tom Stellard | 5dde1d2 | 2016-02-05 18:29:17 +0000 | [diff] [blame] | 466 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 467 | TargetOptions Options, |
| 468 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 469 | Optional<CodeModel::Model> CM, |
| 470 | CodeGenOpt::Level OL, bool JIT) |
| 471 | : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {} |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 472 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 473 | const GCNSubtarget *GCNTargetMachine::getSubtargetImpl(const Function &F) const { |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 474 | StringRef GPU = getGPUName(F); |
| 475 | StringRef FS = getFeatureString(F); |
| 476 | |
| 477 | SmallString<128> SubtargetKey(GPU); |
| 478 | SubtargetKey.append(FS); |
| 479 | |
| 480 | auto &I = SubtargetMap[SubtargetKey]; |
| 481 | if (!I) { |
| 482 | // This needs to be done before we create a new subtarget since any |
| 483 | // creation will depend on the TM and the code generation flags on the |
| 484 | // function that reside in TargetOptions. |
| 485 | resetTargetOptions(F); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 486 | I = llvm::make_unique<GCNSubtarget>(TargetTriple, GPU, FS, *this); |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 489 | I->setScalarizeGlobalBehavior(ScalarizeGlobal); |
| 490 | |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 491 | return I.get(); |
| 492 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 493 | |
Tom Stellard | c762431 | 2018-05-30 22:55:35 +0000 | [diff] [blame] | 494 | TargetTransformInfo |
| 495 | GCNTargetMachine::getTargetTransformInfo(const Function &F) { |
| 496 | return TargetTransformInfo(GCNTTIImpl(this, F)); |
| 497 | } |
| 498 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 499 | //===----------------------------------------------------------------------===// |
| 500 | // AMDGPU Pass Setup |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | |
| 503 | namespace { |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 504 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 505 | class AMDGPUPassConfig : public TargetPassConfig { |
| 506 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 507 | AMDGPUPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 508 | : TargetPassConfig(TM, PM) { |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 509 | // Exceptions and StackMaps are not supported, so these passes will never do |
| 510 | // anything. |
| 511 | disablePass(&StackMapLivenessID); |
| 512 | disablePass(&FuncletLayoutID); |
| 513 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 514 | |
| 515 | AMDGPUTargetMachine &getAMDGPUTargetMachine() const { |
| 516 | return getTM<AMDGPUTargetMachine>(); |
| 517 | } |
| 518 | |
Matthias Braun | 115efcd | 2016-11-28 20:11:54 +0000 | [diff] [blame] | 519 | ScheduleDAGInstrs * |
| 520 | createMachineScheduler(MachineSchedContext *C) const override { |
| 521 | ScheduleDAGMILive *DAG = createGenericSchedLive(C); |
| 522 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 523 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 524 | return DAG; |
| 525 | } |
| 526 | |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 527 | void addEarlyCSEOrGVNPass(); |
| 528 | void addStraightLineScalarOptimizationPasses(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 529 | void addIRPasses() override; |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 530 | void addCodeGenPrepare() override; |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 531 | bool addPreISel() override; |
| 532 | bool addInstSelector() override; |
| 533 | bool addGCPasses() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
Matt Arsenault | 6b6a2c3 | 2016-03-11 08:00:27 +0000 | [diff] [blame] | 536 | class R600PassConfig final : public AMDGPUPassConfig { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 537 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 538 | R600PassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 539 | : AMDGPUPassConfig(TM, PM) {} |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 540 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 541 | ScheduleDAGInstrs *createMachineScheduler( |
| 542 | MachineSchedContext *C) const override { |
| 543 | return createR600MachineScheduler(C); |
| 544 | } |
| 545 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 546 | bool addPreISel() override; |
Tom Stellard | 2028769 | 2017-08-08 04:57:55 +0000 | [diff] [blame] | 547 | bool addInstSelector() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 548 | void addPreRegAlloc() override; |
| 549 | void addPreSched2() override; |
| 550 | void addPreEmitPass() override; |
| 551 | }; |
| 552 | |
Matt Arsenault | 6b6a2c3 | 2016-03-11 08:00:27 +0000 | [diff] [blame] | 553 | class GCNPassConfig final : public AMDGPUPassConfig { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 554 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 555 | GCNPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 556 | : AMDGPUPassConfig(TM, PM) { |
Matt Arsenault | a202538 | 2017-08-03 23:24:05 +0000 | [diff] [blame] | 557 | // It is necessary to know the register usage of the entire call graph. We |
| 558 | // allow calls without EnableAMDGPUFunctionCalls if they are marked |
| 559 | // noinline, so this is always required. |
| 560 | setRequiresCodeGenSCCOrder(true); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 561 | } |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 562 | |
| 563 | GCNTargetMachine &getGCNTargetMachine() const { |
| 564 | return getTM<GCNTargetMachine>(); |
| 565 | } |
| 566 | |
| 567 | ScheduleDAGInstrs * |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 568 | createMachineScheduler(MachineSchedContext *C) const override; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 569 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 570 | bool addPreISel() override; |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 571 | void addMachineSSAOptimization() override; |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 572 | bool addILPOpts() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 573 | bool addInstSelector() override; |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 574 | bool addIRTranslator() override; |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 575 | bool addLegalizeMachineIR() override; |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 576 | bool addRegBankSelect() override; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 577 | bool addGlobalInstructionSelect() override; |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 578 | void addFastRegAlloc(FunctionPass *RegAllocPass) override; |
| 579 | void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 580 | void addPreRegAlloc() override; |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 581 | void addPostRegAlloc() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 582 | void addPreSched2() override; |
| 583 | void addPreEmitPass() override; |
| 584 | }; |
| 585 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 586 | } // end anonymous namespace |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 587 | |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 588 | void AMDGPUPassConfig::addEarlyCSEOrGVNPass() { |
| 589 | if (getOptLevel() == CodeGenOpt::Aggressive) |
| 590 | addPass(createGVNPass()); |
| 591 | else |
| 592 | addPass(createEarlyCSEPass()); |
| 593 | } |
| 594 | |
| 595 | void AMDGPUPassConfig::addStraightLineScalarOptimizationPasses() { |
Stanislav Mekhanoshin | 20d4795 | 2018-06-29 16:26:53 +0000 | [diff] [blame] | 596 | addPass(createLICMPass()); |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 597 | addPass(createSeparateConstOffsetFromGEPPass()); |
| 598 | addPass(createSpeculativeExecutionPass()); |
| 599 | // ReassociateGEPs exposes more opportunites for SLSR. See |
| 600 | // the example in reassociate-geps-and-slsr.ll. |
| 601 | addPass(createStraightLineStrengthReducePass()); |
| 602 | // SeparateConstOffsetFromGEP and SLSR creates common expressions which GVN or |
| 603 | // EarlyCSE can reuse. |
| 604 | addEarlyCSEOrGVNPass(); |
| 605 | // Run NaryReassociate after EarlyCSE/GVN to be more effective. |
| 606 | addPass(createNaryReassociatePass()); |
| 607 | // NaryReassociate on GEPs creates redundant common expressions, so run |
| 608 | // EarlyCSE after it. |
| 609 | addPass(createEarlyCSEPass()); |
| 610 | } |
| 611 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 612 | void AMDGPUPassConfig::addIRPasses() { |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 613 | const AMDGPUTargetMachine &TM = getAMDGPUTargetMachine(); |
| 614 | |
Matt Arsenault | bde8034 | 2016-05-18 15:41:07 +0000 | [diff] [blame] | 615 | // There is no reason to run these. |
| 616 | disablePass(&StackMapLivenessID); |
| 617 | disablePass(&FuncletLayoutID); |
| 618 | disablePass(&PatchableFunctionID); |
| 619 | |
Matt Arsenault | ab41193 | 2018-10-02 03:50:56 +0000 | [diff] [blame] | 620 | addPass(createAtomicExpandPass()); |
Scott Linder | 11ef798 | 2018-10-26 13:18:36 +0000 | [diff] [blame] | 621 | |
| 622 | // This must occur before inlining, as the inliner will not look through |
| 623 | // bitcast calls. |
| 624 | addPass(createAMDGPUFixFunctionBitcastsPass()); |
| 625 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 626 | addPass(createAMDGPULowerIntrinsicsPass()); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 627 | |
Matt Arsenault | 635d479 | 2018-10-03 02:47:25 +0000 | [diff] [blame] | 628 | // Function calls are not supported, so make sure we inline everything. |
| 629 | addPass(createAMDGPUAlwaysInlinePass()); |
| 630 | addPass(createAlwaysInlinerLegacyPass()); |
| 631 | // We need to add the barrier noop pass, otherwise adding the function |
| 632 | // inlining pass will cause all of the PassConfigs passes to be run |
| 633 | // one function at a time, which means if we have a nodule with two |
| 634 | // functions, then we will generate code for the first function |
| 635 | // without ever running any passes on the second. |
| 636 | addPass(createBarrierNoopPass()); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 637 | |
Matt Arsenault | 0c32938 | 2017-01-30 18:40:29 +0000 | [diff] [blame] | 638 | if (TM.getTargetTriple().getArch() == Triple::amdgcn) { |
| 639 | // TODO: May want to move later or split into an early and late one. |
| 640 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 641 | addPass(createAMDGPUCodeGenPreparePass()); |
Matt Arsenault | 0c32938 | 2017-01-30 18:40:29 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Tom Stellard | fd25395 | 2015-08-07 23:19:30 +0000 | [diff] [blame] | 644 | // Handle uses of OpenCL image2d_t, image3d_t and sampler_t arguments. |
Matt Arsenault | 432aaea | 2018-05-13 10:04:48 +0000 | [diff] [blame] | 645 | if (TM.getTargetTriple().getArch() == Triple::r600) |
| 646 | addPass(createR600OpenCLImageTypeLoweringPass()); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 647 | |
Yaxun Liu | de4b88d | 2017-10-10 19:39:48 +0000 | [diff] [blame] | 648 | // Replace OpenCL enqueued block function pointers with global variables. |
| 649 | addPass(createAMDGPUOpenCLEnqueuedBlockLoweringPass()); |
| 650 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 651 | if (TM.getOptLevel() > CodeGenOpt::None) { |
Matt Arsenault | 417e007 | 2017-02-08 06:16:04 +0000 | [diff] [blame] | 652 | addPass(createInferAddressSpacesPass()); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 653 | addPass(createAMDGPUPromoteAlloca()); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 654 | |
| 655 | if (EnableSROA) |
| 656 | addPass(createSROAPass()); |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 657 | |
Konstantin Zhuravlyov | 4658e5f | 2016-09-30 16:39:24 +0000 | [diff] [blame] | 658 | addStraightLineScalarOptimizationPasses(); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 659 | |
| 660 | if (EnableAMDGPUAliasAnalysis) { |
| 661 | addPass(createAMDGPUAAWrapperPass()); |
| 662 | addPass(createExternalAAWrapperPass([](Pass &P, Function &, |
| 663 | AAResults &AAR) { |
| 664 | if (auto *WrapperPass = P.getAnalysisIfAvailable<AMDGPUAAWrapperPass>()) |
| 665 | AAR.addAAResult(WrapperPass->getResult()); |
| 666 | })); |
| 667 | } |
Konstantin Zhuravlyov | 4658e5f | 2016-09-30 16:39:24 +0000 | [diff] [blame] | 668 | } |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 669 | |
| 670 | TargetPassConfig::addIRPasses(); |
| 671 | |
| 672 | // EarlyCSE is not always strong enough to clean up what LSR produces. For |
| 673 | // example, GVN can combine |
| 674 | // |
| 675 | // %0 = add %a, %b |
| 676 | // %1 = add %b, %a |
| 677 | // |
| 678 | // and |
| 679 | // |
| 680 | // %0 = shl nsw %a, 2 |
| 681 | // %1 = shl %a, 2 |
| 682 | // |
| 683 | // but EarlyCSE can do neither of them. |
| 684 | if (getOptLevel() != CodeGenOpt::None) |
| 685 | addEarlyCSEOrGVNPass(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 688 | void AMDGPUPassConfig::addCodeGenPrepare() { |
Matt Arsenault | 8c4a352 | 2018-06-26 19:10:00 +0000 | [diff] [blame] | 689 | if (TM->getTargetTriple().getArch() == Triple::amdgcn && |
| 690 | EnableLowerKernelArguments) |
| 691 | addPass(createAMDGPULowerKernelArgumentsPass()); |
| 692 | |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 693 | TargetPassConfig::addCodeGenPrepare(); |
| 694 | |
| 695 | if (EnableLoadStoreVectorizer) |
| 696 | addPass(createLoadStoreVectorizerPass()); |
| 697 | } |
| 698 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 699 | bool AMDGPUPassConfig::addPreISel() { |
Sameer Sahasrabuddhe | b4f2d1c | 2018-09-25 09:39:21 +0000 | [diff] [blame] | 700 | addPass(createLowerSwitchPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 701 | addPass(createFlattenCFGPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 702 | return false; |
| 703 | } |
| 704 | |
| 705 | bool AMDGPUPassConfig::addInstSelector() { |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 706 | addPass(createAMDGPUISelDag(&getAMDGPUTargetMachine(), getOptLevel())); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 710 | bool AMDGPUPassConfig::addGCPasses() { |
| 711 | // Do nothing. GC is not supported. |
| 712 | return false; |
| 713 | } |
| 714 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 715 | //===----------------------------------------------------------------------===// |
| 716 | // R600 Pass Setup |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | |
| 719 | bool R600PassConfig::addPreISel() { |
| 720 | AMDGPUPassConfig::addPreISel(); |
Matt Arsenault | c581611 | 2016-06-24 06:30:22 +0000 | [diff] [blame] | 721 | |
| 722 | if (EnableR600StructurizeCFG) |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 723 | addPass(createStructurizeCFGPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
Tom Stellard | 2028769 | 2017-08-08 04:57:55 +0000 | [diff] [blame] | 727 | bool R600PassConfig::addInstSelector() { |
| 728 | addPass(createR600ISelDag(&getAMDGPUTargetMachine(), getOptLevel())); |
| 729 | return false; |
| 730 | } |
| 731 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 732 | void R600PassConfig::addPreRegAlloc() { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 733 | addPass(createR600VectorRegMerger()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void R600PassConfig::addPreSched2() { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 737 | addPass(createR600EmitClauseMarkers(), false); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 738 | if (EnableR600IfConvert) |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 739 | addPass(&IfConverterID, false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 740 | addPass(createR600ClauseMergePass(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void R600PassConfig::addPreEmitPass() { |
| 744 | addPass(createAMDGPUCFGStructurizerPass(), false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 745 | addPass(createR600ExpandSpecialInstrsPass(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 746 | addPass(&FinalizeMachineBundlesID, false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 747 | addPass(createR600Packetizer(), false); |
| 748 | addPass(createR600ControlFlowFinalizer(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 752 | return new R600PassConfig(*this, PM); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | //===----------------------------------------------------------------------===// |
| 756 | // GCN Pass Setup |
| 757 | //===----------------------------------------------------------------------===// |
| 758 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 759 | ScheduleDAGInstrs *GCNPassConfig::createMachineScheduler( |
| 760 | MachineSchedContext *C) const { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 761 | const GCNSubtarget &ST = C->MF->getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 762 | if (ST.enableSIScheduler()) |
| 763 | return createSIMachineScheduler(C); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 764 | return createGCNMaxOccupancyMachineScheduler(C); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 767 | bool GCNPassConfig::addPreISel() { |
| 768 | AMDGPUPassConfig::addPreISel(); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 769 | |
Neil Henning | 6641657 | 2018-10-08 15:49:19 +0000 | [diff] [blame] | 770 | if (EnableAtomicOptimizations) { |
| 771 | addPass(createAMDGPUAtomicOptimizerPass()); |
| 772 | } |
| 773 | |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 774 | // FIXME: We need to run a pass to propagate the attributes when calls are |
| 775 | // supported. |
Aakanksha Patil | bc56876 | 2018-12-13 21:23:12 +0000 | [diff] [blame] | 776 | addPass(createAMDGPUAnnotateKernelFeaturesPass()); |
Matt Arsenault | b8f8dbc | 2017-03-24 19:52:05 +0000 | [diff] [blame] | 777 | |
| 778 | // Merge divergent exit nodes. StructurizeCFG won't recognize the multi-exit |
| 779 | // regions formed by them. |
| 780 | addPass(&AMDGPUUnifyDivergentExitNodesID); |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 781 | if (!LateCFGStructurize) { |
| 782 | addPass(createStructurizeCFGPass(true)); // true -> SkipUniformRegions |
| 783 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 784 | addPass(createSinkingPass()); |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 785 | addPass(createAMDGPUAnnotateUniformValues()); |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 786 | if (!LateCFGStructurize) { |
| 787 | addPass(createSIAnnotateControlFlowPass()); |
| 788 | } |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 789 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 790 | return false; |
| 791 | } |
| 792 | |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 793 | void GCNPassConfig::addMachineSSAOptimization() { |
| 794 | TargetPassConfig::addMachineSSAOptimization(); |
| 795 | |
| 796 | // We want to fold operands after PeepholeOptimizer has run (or as part of |
| 797 | // it), because it will eliminate extra copies making it easier to fold the |
| 798 | // real source operand. We want to eliminate dead instructions after, so that |
| 799 | // we see fewer uses of the copies. We then need to clean up the dead |
| 800 | // instructions leftover after the operands are folded as well. |
| 801 | // |
| 802 | // XXX - Can we get away without running DeadMachineInstructionElim again? |
| 803 | addPass(&SIFoldOperandsID); |
Valery Pykhtin | 3d9afa2 | 2018-11-30 14:21:56 +0000 | [diff] [blame] | 804 | if (EnableDPPCombine) |
| 805 | addPass(&GCNDPPCombineID); |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 806 | addPass(&DeadMachineInstructionElimID); |
Tom Stellard | c2ff0eb | 2016-08-29 19:15:22 +0000 | [diff] [blame] | 807 | addPass(&SILoadStoreOptimizerID); |
Sam Kolton | 6e79529 | 2017-04-07 10:53:12 +0000 | [diff] [blame] | 808 | if (EnableSDWAPeephole) { |
| 809 | addPass(&SIPeepholeSDWAID); |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 810 | addPass(&EarlyMachineLICMID); |
Stanislav Mekhanoshin | 56ea488 | 2017-05-30 16:49:24 +0000 | [diff] [blame] | 811 | addPass(&MachineCSEID); |
| 812 | addPass(&SIFoldOperandsID); |
Sam Kolton | 6e79529 | 2017-04-07 10:53:12 +0000 | [diff] [blame] | 813 | addPass(&DeadMachineInstructionElimID); |
| 814 | } |
Stanislav Mekhanoshin | 0330660 | 2017-06-03 17:39:47 +0000 | [diff] [blame] | 815 | addPass(createSIShrinkInstructionsPass()); |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 818 | bool GCNPassConfig::addILPOpts() { |
| 819 | if (EnableEarlyIfConversion) |
| 820 | addPass(&EarlyIfConverterID); |
| 821 | |
| 822 | TargetPassConfig::addILPOpts(); |
| 823 | return false; |
| 824 | } |
| 825 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 826 | bool GCNPassConfig::addInstSelector() { |
| 827 | AMDGPUPassConfig::addInstSelector(); |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 828 | addPass(&SIFixSGPRCopiesID); |
Nicolai Haehnle | 814abb5 | 2018-10-31 13:27:08 +0000 | [diff] [blame] | 829 | addPass(createSILowerI1CopiesPass()); |
Ron Lieberman | cac749a | 2018-11-16 01:13:34 +0000 | [diff] [blame] | 830 | addPass(createSIFixupVectorISelPass()); |
David Stuttard | f77079f | 2019-01-14 11:55:24 +0000 | [diff] [blame] | 831 | addPass(createSIAddIMGInitPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 832 | return false; |
| 833 | } |
| 834 | |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 835 | bool GCNPassConfig::addIRTranslator() { |
| 836 | addPass(new IRTranslator()); |
| 837 | return false; |
| 838 | } |
| 839 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 840 | bool GCNPassConfig::addLegalizeMachineIR() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 841 | addPass(new Legalizer()); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 845 | bool GCNPassConfig::addRegBankSelect() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 846 | addPass(new RegBankSelect()); |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 847 | return false; |
| 848 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 849 | |
| 850 | bool GCNPassConfig::addGlobalInstructionSelect() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 851 | addPass(new InstructionSelect()); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 852 | return false; |
| 853 | } |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 854 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 855 | void GCNPassConfig::addPreRegAlloc() { |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 856 | if (LateCFGStructurize) { |
| 857 | addPass(createAMDGPUMachineCFGStructurizerPass()); |
| 858 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 859 | addPass(createSIWholeQuadModePass()); |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | void GCNPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 863 | // FIXME: We have to disable the verifier here because of PHIElimination + |
| 864 | // TwoAddressInstructions disabling it. |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 865 | |
| 866 | // This must be run immediately after phi elimination and before |
| 867 | // TwoAddressInstructions, otherwise the processing of the tied operand of |
| 868 | // SI_ELSE will introduce a copy of the tied operand source after the else. |
| 869 | insertPass(&PHIEliminationID, &SILowerControlFlowID, false); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 870 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 871 | // This must be run after SILowerControlFlow, since it needs to use the |
| 872 | // machine-level CFG, but before register allocation. |
| 873 | insertPass(&SILowerControlFlowID, &SIFixWWMLivenessID, false); |
| 874 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 875 | TargetPassConfig::addFastRegAlloc(RegAllocPass); |
| 876 | } |
| 877 | |
| 878 | void GCNPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { |
Matt Arsenault | 9d288e6 | 2017-08-07 18:12:48 +0000 | [diff] [blame] | 879 | insertPass(&MachineSchedulerID, &SIOptimizeExecMaskingPreRAID); |
Stanislav Mekhanoshin | 37e7f95 | 2017-08-01 23:14:32 +0000 | [diff] [blame] | 880 | |
Stanislav Mekhanoshin | 739174c | 2018-05-31 20:13:51 +0000 | [diff] [blame] | 881 | insertPass(&SIOptimizeExecMaskingPreRAID, &SIFormMemoryClausesID); |
| 882 | |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 883 | // This must be run immediately after phi elimination and before |
| 884 | // TwoAddressInstructions, otherwise the processing of the tied operand of |
| 885 | // SI_ELSE will introduce a copy of the tied operand source after the else. |
| 886 | insertPass(&PHIEliminationID, &SILowerControlFlowID, false); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 887 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 888 | // This must be run after SILowerControlFlow, since it needs to use the |
| 889 | // machine-level CFG, but before register allocation. |
| 890 | insertPass(&SILowerControlFlowID, &SIFixWWMLivenessID, false); |
| 891 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 892 | TargetPassConfig::addOptimizedRegAlloc(RegAllocPass); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 895 | void GCNPassConfig::addPostRegAlloc() { |
Stanislav Mekhanoshin | 22a56f2 | 2017-01-24 17:46:17 +0000 | [diff] [blame] | 896 | addPass(&SIFixVGPRCopiesID); |
Matt Arsenault | 105fc1a | 2018-11-26 17:02:02 +0000 | [diff] [blame] | 897 | if (getOptLevel() > CodeGenOpt::None) |
| 898 | addPass(&SIOptimizeExecMaskingID); |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 899 | TargetPassConfig::addPostRegAlloc(); |
| 900 | } |
| 901 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 902 | void GCNPassConfig::addPreSched2() { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | void GCNPassConfig::addPreEmitPass() { |
Mark Searles | 72da47d | 2018-07-16 10:02:41 +0000 | [diff] [blame] | 906 | addPass(createSIMemoryLegalizerPass()); |
| 907 | addPass(createSIInsertWaitcntsPass()); |
| 908 | addPass(createSIShrinkInstructionsPass()); |
Tim Corringham | 4c4d2fe | 2018-12-10 12:06:10 +0000 | [diff] [blame] | 909 | addPass(createSIModeRegisterPass()); |
Mark Searles | 72da47d | 2018-07-16 10:02:41 +0000 | [diff] [blame] | 910 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 911 | // The hazard recognizer that runs as part of the post-ra scheduler does not |
Matt Arsenault | 254a645 | 2016-06-28 16:59:53 +0000 | [diff] [blame] | 912 | // guarantee to be able handle all hazards correctly. This is because if there |
| 913 | // are multiple scheduling regions in a basic block, the regions are scheduled |
| 914 | // bottom up, so when we begin to schedule a region we don't know what |
| 915 | // instructions were emitted directly before it. |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 916 | // |
Matt Arsenault | 254a645 | 2016-06-28 16:59:53 +0000 | [diff] [blame] | 917 | // Here we add a stand-alone hazard recognizer pass which can handle all |
| 918 | // cases. |
Mark Searles | 72da47d | 2018-07-16 10:02:41 +0000 | [diff] [blame] | 919 | // |
| 920 | // FIXME: This stand-alone pass will emit indiv. S_NOP 0, as needed. It would |
| 921 | // be better for it to emit S_NOP <N> when possible. |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 922 | addPass(&PostRAHazardRecognizerID); |
| 923 | |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 924 | addPass(&SIInsertSkipsPassID); |
Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 925 | addPass(&BranchRelaxationPassID); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | TargetPassConfig *GCNTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 929 | return new GCNPassConfig(*this, PM); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 930 | } |