| 1 | //===- SPIRVTargetMachine.cpp - Define TargetMachine for SPIR-V -*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implements the info about SPIR-V target spec. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "SPIRVTargetMachine.h" |
| 14 | #include "Analysis/SPIRVConvergenceRegionAnalysis.h" |
| 15 | #include "SPIRV.h" |
| 16 | #include "SPIRVGlobalRegistry.h" |
| 17 | #include "SPIRVLegalizerInfo.h" |
| 18 | #include "SPIRVTargetObjectFile.h" |
| 19 | #include "SPIRVTargetTransformInfo.h" |
| 20 | #include "TargetInfo/SPIRVTargetInfo.h" |
| 21 | #include "llvm/CodeGen/CSEConfigBase.h" |
| 22 | #include "llvm/CodeGen/GlobalISel/IRTranslator.h" |
| 23 | #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" |
| 24 | #include "llvm/CodeGen/GlobalISel/Legalizer.h" |
| 25 | #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" |
| 26 | #include "llvm/CodeGen/Passes.h" |
| 27 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 28 | #include "llvm/InitializePasses.h" |
| 29 | #include "llvm/MC/TargetRegistry.h" |
| 30 | #include "llvm/Option/LibraryOptions.h" |
| 31 | #include "llvm/Pass.h" |
| 32 | #include "llvm/Passes/PassBuilder.h" |
| 33 | #include "llvm/Support/CodeGen.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | #include "llvm/Target/TargetOptions.h" |
| 36 | #include "llvm/Transforms/IPO/ExpandVariadics.h" |
| 37 | #include "llvm/Transforms/Scalar.h" |
| 38 | #include "llvm/Transforms/Utils.h" |
| 39 | #include <optional> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() { |
| 44 | // Register the target. |
| 45 | RegisterTargetMachine<SPIRVTargetMachine> X(getTheSPIRV32Target()); |
| 46 | RegisterTargetMachine<SPIRVTargetMachine> Y(getTheSPIRV64Target()); |
| 47 | RegisterTargetMachine<SPIRVTargetMachine> Z(getTheSPIRVLogicalTarget()); |
| 48 | static opt::RegisterLibraryOptions<SPIRVOptions> O; |
| 49 | |
| 50 | PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| 51 | initializeGlobalISel(PR); |
| 52 | initializeSPIRVModuleAnalysisWrapperPassPass(PR); |
| 53 | initializeSPIRVAsmPrinterPass(PR); |
| 54 | initializeSPIRVConvergenceRegionAnalysisWrapperPassPass(PR); |
| 55 | initializeSPIRVStructurizerPass(PR); |
| 56 | initializeSPIRVCBufferAccessLegacyPass(PR); |
| 57 | initializeSPIRVPushConstantAccessLegacyPass(PR); |
| 58 | initializeSPIRVPreLegalizerCombinerLegacyPass(PR); |
| 59 | initializeSPIRVLegalizePointerCastLegacyPass(PR); |
| 60 | initializeSPIRVLegalizeZeroSizeArraysLegacyPass(PR); |
| 61 | initializeSPIRVRegularizerLegacyPass(PR); |
| 62 | initializeSPIRVPreLegalizerLegacyPass(PR); |
| 63 | initializeSPIRVPostLegalizerLegacyPass(PR); |
| 64 | initializeSPIRVPrepareModuleAnalysisLegacyPass(PR); |
| 65 | initializeSPIRVMergeRegionExitTargetsLegacyPass(PR); |
| 66 | initializeSPIRVEmitIntrinsicsLegacyPass(PR); |
| 67 | initializeSPIRVPrepareFunctionsLegacyPass(PR); |
| 68 | initializeSPIRVPrepareGlobalsLegacyPass(PR); |
| 69 | initializeSPIRVLegalizeResourceBindingLegacyPass(PR); |
| 70 | initializeSPIRVCtorDtorLoweringLegacyPass(PR); |
| 71 | initializeSPIRVFinalizeShaderLinkageLegacyPass(PR); |
| 72 | initializeSPIRVRemoveUnusedResourcesLegacyPass(PR); |
| 73 | } |
| 74 | |
| 75 | static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) { |
| 76 | if (!RM) |
| 77 | return Reloc::PIC_; |
| 78 | return *RM; |
| 79 | } |
| 80 | |
| 81 | // Pin SPIRVTargetObjectFile's vtables to this file. |
| 82 | SPIRVTargetObjectFile::~SPIRVTargetObjectFile() = default; |
| 83 | |
| 84 | SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT, |
| 85 | StringRef CPU, StringRef FS, |
| 86 | const TargetOptions &Options, |
| 87 | std::optional<Reloc::Model> RM, |
| 88 | std::optional<CodeModel::Model> CM, |
| 89 | CodeGenOptLevel OL, bool JIT) |
| 90 | : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options, |
| 91 | getEffectiveRelocModel(RM), |
| 92 | getEffectiveCodeModel(CM, Default: CodeModel::Small), OL), |
| 93 | CLOpts(cl::LibraryOptionsContext::get<SPIRVOptions>( |
| 94 | C: Options.MCOptions.LibraryOptions)), |
| 95 | TLOF(std::make_unique<SPIRVTargetObjectFile>()), |
| 96 | Subtarget(TT, CPU.str(), FS.str(), *this) { |
| 97 | initAsmInfo(); |
| 98 | LLT::setUseExtended(true); |
| 99 | setGlobalISel(true); |
| 100 | setFastISel(false); |
| 101 | setO0WantsFastISel(false); |
| 102 | setRequiresStructuredCFG(false); |
| 103 | } |
| 104 | |
| 105 | namespace { |
| 106 | // SPIR-V Code Generator Pass Configuration Options. |
| 107 | class SPIRVPassConfig : public TargetPassConfig { |
| 108 | public: |
| 109 | SPIRVPassConfig(SPIRVTargetMachine &TM, PassManagerBase &PM) |
| 110 | : TargetPassConfig(TM, PM), TM(TM) { |
| 111 | // Logical SPIR-V cannot express the pointer induction variables that loop |
| 112 | // strength reduction creates. |
| 113 | if (TM.getSubtargetImpl()->isLogicalSPIRV()) |
| 114 | EnableLoopStrengthReduce = false; |
| 115 | } |
| 116 | |
| 117 | void addMachineSSAOptimization() override; |
| 118 | void addIRPasses() override; |
| 119 | void addISelPrepare() override; |
| 120 | |
| 121 | bool addIRTranslator() override; |
| 122 | void addPreLegalizeMachineIR() override; |
| 123 | bool addLegalizeMachineIR() override; |
| 124 | bool addRegBankSelect() override; |
| 125 | bool addGlobalInstructionSelect() override; |
| 126 | |
| 127 | FunctionPass *createTargetRegisterAllocator(bool) override; |
| 128 | void addFastRegAlloc() override {} |
| 129 | void addOptimizedRegAlloc() override {} |
| 130 | |
| 131 | void addPostRegAlloc() override; |
| 132 | void addPreEmitPass2() override; |
| 133 | |
| 134 | private: |
| 135 | const SPIRVTargetMachine &TM; |
| 136 | }; |
| 137 | } // namespace |
| 138 | |
| 139 | // We do not use physical registers, and maintain virtual registers throughout |
| 140 | // the entire pipeline, so return nullptr to disable register allocation. |
| 141 | FunctionPass *SPIRVPassConfig::createTargetRegisterAllocator(bool) { |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | // A place to disable passes that may break CFG. |
| 146 | void SPIRVPassConfig::addMachineSSAOptimization() { |
| 147 | TargetPassConfig::addMachineSSAOptimization(); |
| 148 | } |
| 149 | |
| 150 | // Disable passes that break from assuming no virtual registers exist. |
| 151 | void SPIRVPassConfig::addPostRegAlloc() { |
| 152 | // Do not work with vregs instead of physical regs. |
| 153 | disablePass(PassID: &MachineCopyPropagationID); |
| 154 | disablePass(PassID: &PostRAMachineSinkingID); |
| 155 | disablePass(PassID: &PostRASchedulerID); |
| 156 | disablePass(PassID: &FuncletLayoutID); |
| 157 | disablePass(PassID: &StackMapLivenessID); |
| 158 | disablePass(PassID: &PatchableFunctionID); |
| 159 | disablePass(PassID: &ShrinkWrapID); |
| 160 | disablePass(PassID: &LiveDebugValuesID); |
| 161 | disablePass(PassID: &MachineLateInstrsCleanupID); |
| 162 | disablePass(PassID: &RemoveLoadsIntoFakeUsesID); |
| 163 | |
| 164 | // Do not work with OpPhi. |
| 165 | disablePass(PassID: &BranchFolderPassID); |
| 166 | disablePass(PassID: &MachineBlockPlacementID); |
| 167 | |
| 168 | TargetPassConfig::addPostRegAlloc(); |
| 169 | } |
| 170 | |
| 171 | void SPIRVPassConfig::addPreEmitPass2() { |
| 172 | addPass(P: createSPIRVPrepareModuleAnalysisLegacyPass()); |
| 173 | } |
| 174 | |
| 175 | TargetTransformInfo |
| 176 | SPIRVTargetMachine::getTargetTransformInfo(const Function &F) const { |
| 177 | return TargetTransformInfo(std::make_unique<SPIRVTTIImpl>(args: this, args: F)); |
| 178 | } |
| 179 | |
| 180 | TargetPassConfig *SPIRVTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 181 | return new SPIRVPassConfig(*this, PM); |
| 182 | } |
| 183 | |
| 184 | // CSE can merge same-LLT values with different SPIR-V types. |
| 185 | CSEConfigBase *SPIRVTargetMachine::createCSEConfig() const { |
| 186 | return new CSEConfigBase(); |
| 187 | } |
| 188 | |
| 189 | void SPIRVPassConfig::addIRPasses() { |
| 190 | addPass(P: createAtomicExpandLegacyPass()); |
| 191 | |
| 192 | TargetPassConfig::addIRPasses(); |
| 193 | |
| 194 | if (TM.getSubtargetImpl()->isShader()) { |
| 195 | if (getOptLevel() != CodeGenOptLevel::None) |
| 196 | addPass(P: createSPIRVFinalizeShaderLinkagePass(TM)); |
| 197 | addPass(P: createSPIRVRemoveUnusedResourcesLegacyPass()); |
| 198 | } else { |
| 199 | // Variadic function calls aren't supported in shader code. |
| 200 | // This needs to come before SPIRVPrepareFunctions because this |
| 201 | // may introduce intrinsic calls. |
| 202 | addPass(P: createExpandVariadicsPass(ExpandVariadicsMode::Lowering)); |
| 203 | } |
| 204 | |
| 205 | addPass(P: createSPIRVRegularizerPass()); |
| 206 | addPass(P: createSPIRVCtorDtorLoweringLegacyPass()); |
| 207 | addPass(P: createSPIRVPrepareFunctionsPass(TM)); |
| 208 | addPass(P: createSPIRVPrepareGlobalsPass()); |
| 209 | } |
| 210 | |
| 211 | void SPIRVPassConfig::addISelPrepare() { |
| 212 | if (TM.getSubtargetImpl()->isShader()) { |
| 213 | // Vulkan does not allow address space casts. This pass is run to remove |
| 214 | // address space casts that can be removed. |
| 215 | // If an address space cast is not removed while targeting Vulkan, lowering |
| 216 | // will fail during MIR lowering. |
| 217 | addPass(P: createInferAddressSpacesPass()); |
| 218 | |
| 219 | // 1. Simplify loop for subsequent transformations. After this steps, loops |
| 220 | // have the following properties: |
| 221 | // - loops have a single entry edge (pre-header to loop header). |
| 222 | // - all loop exits are dominated by the loop pre-header. |
| 223 | // - loops have a single back-edge. |
| 224 | addPass(P: createLoopSimplifyPass()); |
| 225 | |
| 226 | // 2. Removes registers whose lifetime spans across basic blocks. Also |
| 227 | // removes phi nodes. This will greatly simplify the next steps. |
| 228 | addPass(P: createRegToMemWrapperPass()); |
| 229 | |
| 230 | // 3. Merge the convergence region exit nodes into one. After this step, |
| 231 | // regions are single-entry, single-exit. This will help determine the |
| 232 | // correct merge block. |
| 233 | addPass(P: createSPIRVMergeRegionExitTargetsPass()); |
| 234 | |
| 235 | // 4. Structurize. |
| 236 | addPass(P: createSPIRVStructurizerPass()); |
| 237 | |
| 238 | // 5. Reduce the amount of variables required by pushing some operations |
| 239 | // back to virtual registers. |
| 240 | addPass(P: createPromoteMemoryToRegisterPass()); |
| 241 | } else { |
| 242 | // Canonicalize loops so they have a single latch and preheader. |
| 243 | // This enables OpLoopMerge emission for non-shader targets. |
| 244 | addPass(P: createLoopSimplifyPass()); |
| 245 | } |
| 246 | SPIRVTargetMachine &TM = getTM<SPIRVTargetMachine>(); |
| 247 | addPass(P: createStripConvergenceIntrinsicsPass()); |
| 248 | addPass(P: createSPIRVLegalizeResourceBindingPass()); |
| 249 | addPass(P: createSPIRVLegalizeZeroSizeArraysPass(TM)); |
| 250 | addPass(P: createSPIRVCBufferAccessLegacyPass()); |
| 251 | addPass(P: createSPIRVPushConstantAccessLegacyPass(TM: &TM)); |
| 252 | addPass(P: createSPIRVEmitIntrinsicsPass(TM)); |
| 253 | if (TM.getSubtargetImpl()->isLogicalSPIRV()) |
| 254 | addPass(P: createSPIRVLegalizePointerCastPass(TM: &TM)); |
| 255 | TargetPassConfig::addISelPrepare(); |
| 256 | } |
| 257 | |
| 258 | bool SPIRVPassConfig::addIRTranslator() { |
| 259 | addPass(P: new IRTranslatorLegacy(getOptLevel())); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | void SPIRVPassConfig::addPreLegalizeMachineIR() { |
| 264 | addPass(P: createSPIRVPreLegalizerCombinerLegacyPass()); |
| 265 | addPass(P: createSPIRVPreLegalizerLegacyPass()); |
| 266 | } |
| 267 | |
| 268 | // Use the default legalizer. |
| 269 | bool SPIRVPassConfig::addLegalizeMachineIR() { |
| 270 | addPass(P: new LegalizerLegacy()); |
| 271 | addPass(P: createSPIRVPostLegalizerLegacyPass()); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | // Do not add the RegBankSelect pass, as we only ever need virtual registers. |
| 276 | bool SPIRVPassConfig::addRegBankSelect() { |
| 277 | disablePass(PassID: &RegBankSelectLegacy::ID); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Add the custom SPIRVInstructionSelect from above. |
| 282 | bool SPIRVPassConfig::addGlobalInstructionSelect() { |
| 283 | addPass(P: new InstructionSelectLegacy(getOptLevel())); |
| 284 | return false; |
| 285 | } |
| 286 | |