| 1 | //===- DirectXTargetMachine.cpp - DirectX Target Implementation -*- 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 | /// \file |
| 10 | /// This file contains DirectX target initializer. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DirectXTargetMachine.h" |
| 15 | #include "DXILCBufferAccess.h" |
| 16 | #include "DXILDataScalarization.h" |
| 17 | #include "DXILFinalizeLinkage.h" |
| 18 | #include "DXILFlattenArrays.h" |
| 19 | #include "DXILForwardHandleAccesses.h" |
| 20 | #include "DXILIntrinsicExpansion.h" |
| 21 | #include "DXILLegalizePass.h" |
| 22 | #include "DXILMemIntrinsics.h" |
| 23 | #include "DXILOpLowering.h" |
| 24 | #include "DXILPostOptimizationValidation.h" |
| 25 | #include "DXILPrettyPrinter.h" |
| 26 | #include "DXILRemoveUnusedResources.h" |
| 27 | #include "DXILResourceAccess.h" |
| 28 | #include "DXILResourceImplicitBinding.h" |
| 29 | #include "DXILRootSignature.h" |
| 30 | #include "DXILShaderFlags.h" |
| 31 | #include "DXILSignatureAnalysis.h" |
| 32 | #include "DXILTranslateMetadata.h" |
| 33 | #include "DXILWriter/DXILWriterPass.h" |
| 34 | #include "DirectX.h" |
| 35 | #include "DirectXIRPasses/DXILDebugInfo.h" |
| 36 | #include "DirectXSubtarget.h" |
| 37 | #include "DirectXTargetTransformInfo.h" |
| 38 | #include "TargetInfo/DirectXTargetInfo.h" |
| 39 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 40 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 41 | #include "llvm/CodeGen/Passes.h" |
| 42 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 43 | #include "llvm/IR/IRPrintingPasses.h" |
| 44 | #include "llvm/IR/LegacyPassManager.h" |
| 45 | #include "llvm/InitializePasses.h" |
| 46 | #include "llvm/MC/MCSectionDXContainer.h" |
| 47 | #include "llvm/MC/SectionKind.h" |
| 48 | #include "llvm/MC/TargetRegistry.h" |
| 49 | #include "llvm/Passes/PassBuilder.h" |
| 50 | #include "llvm/Support/CodeGen.h" |
| 51 | #include "llvm/Support/Compiler.h" |
| 52 | #include "llvm/Support/ErrorHandling.h" |
| 53 | #include "llvm/Support/VersionTuple.h" |
| 54 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 55 | #include "llvm/Transforms/IPO/GlobalDCE.h" |
| 56 | #include "llvm/Transforms/Scalar.h" |
| 57 | #include "llvm/Transforms/Scalar/Scalarizer.h" |
| 58 | #include "llvm/Transforms/Utils.h" |
| 59 | #include <optional> |
| 60 | |
| 61 | using namespace llvm; |
| 62 | |
| 63 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void |
| 64 | LLVMInitializeDirectXTarget() { |
| 65 | RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget()); |
| 66 | auto *PR = PassRegistry::getPassRegistry(); |
| 67 | initializeDXILDebugInfoLegacyPass(*PR); |
| 68 | initializeDXILIntrinsicExpansionLegacyPass(*PR); |
| 69 | initializeDXILMemIntrinsicsLegacyPass(*PR); |
| 70 | initializeDXILDataScalarizationLegacyPass(*PR); |
| 71 | initializeDXILFlattenArraysLegacyPass(*PR); |
| 72 | initializeScalarizerLegacyPassPass(*PR); |
| 73 | initializeDXILLegalizeLegacyPass(*PR); |
| 74 | initializeDXILPrepareModulePass(*PR); |
| 75 | initializeEmbedDXILPassPass(*PR); |
| 76 | initializeWriteDXILPassPass(*PR); |
| 77 | initializeDXContainerGlobalsPass(*PR); |
| 78 | initializeDXContainerPDBPass(*PR); |
| 79 | initializeGlobalDCELegacyPassPass(*PR); |
| 80 | initializeDXILOpLoweringLegacyPass(*PR); |
| 81 | initializeDXILRemoveUnusedResourcesLegacyPass(*PR); |
| 82 | initializeDXILResourceAccessLegacyPass(*PR); |
| 83 | initializeDXILResourceImplicitBindingLegacyPass(*PR); |
| 84 | initializeDXILTranslateMetadataLegacyPass(*PR); |
| 85 | initializeDXILPostOptimizationValidationLegacyPass(*PR); |
| 86 | initializeShaderFlagsAnalysisWrapperPass(*PR); |
| 87 | initializeRootSignatureAnalysisWrapperPass(*PR); |
| 88 | initializeSignatureAnalysisWrapperPass(*PR); |
| 89 | initializeDXILFinalizeLinkageLegacyPass(*PR); |
| 90 | initializeDXILPrettyPrinterLegacyPass(*PR); |
| 91 | initializeDXILForwardHandleAccessesLegacyPass(*PR); |
| 92 | initializeDSELegacyPassPass(*PR); |
| 93 | initializeDXILCBufferAccessLegacyPass(*PR); |
| 94 | initializeStripConvergenceIntrinsicsLegacyPassPass(*PR); |
| 95 | } |
| 96 | |
| 97 | class DXILTargetObjectFile : public TargetLoweringObjectFile { |
| 98 | public: |
| 99 | DXILTargetObjectFile() = default; |
| 100 | |
| 101 | MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, |
| 102 | const TargetMachine &TM) const override { |
| 103 | return getContext().getDXContainerSection(Section: GO->getSection(), K: Kind); |
| 104 | } |
| 105 | |
| 106 | protected: |
| 107 | MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, |
| 108 | const TargetMachine &TM) const override { |
| 109 | llvm_unreachable("Not supported!" ); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | class DirectXPassConfig : public TargetPassConfig { |
| 114 | public: |
| 115 | DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM) |
| 116 | : TargetPassConfig(TM, PM) {} |
| 117 | |
| 118 | DirectXTargetMachine &getDirectXTargetMachine() const { |
| 119 | return getTM<DirectXTargetMachine>(); |
| 120 | } |
| 121 | |
| 122 | FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; } |
| 123 | void addCodeGenPrepare() override { |
| 124 | CodeGenOptLevel OptLevel = getDirectXTargetMachine().getOptLevel(); |
| 125 | |
| 126 | addPass(P: createStripConvergenceIntrinsicsPass()); |
| 127 | addPass(P: createDXILFinalizeLinkageLegacyPass()); |
| 128 | if (OptLevel != CodeGenOptLevel::None) { |
| 129 | addPass(P: createGlobalDCEPass()); |
| 130 | } |
| 131 | addPass(P: createDXILMemIntrinsicsLegacyPass()); |
| 132 | addPass(P: createDXILCBufferAccessLegacyPass()); |
| 133 | addPass(P: createDXILRemoveUnusedResourcesLegacyPass()); |
| 134 | addPass(P: createDXILResourceAccessLegacyPass()); |
| 135 | addPass(P: createDXILIntrinsicExpansionLegacyPass()); |
| 136 | addPass(P: createDXILDataScalarizationLegacyPass()); |
| 137 | if (getDirectXTargetMachine().getTargetTriple().getOSVersion() < |
| 138 | VersionTuple(6, 9)) { |
| 139 | ScalarizerPassOptions DxilScalarOptions; |
| 140 | DxilScalarOptions.ScalarizeLoadStore = true; |
| 141 | addPass(P: createScalarizerPass(Options: DxilScalarOptions)); |
| 142 | } |
| 143 | addPass(P: createDXILFlattenArraysLegacyPass()); |
| 144 | addPass(P: createDXILForwardHandleAccessesLegacyPass()); |
| 145 | if (OptLevel != CodeGenOptLevel::None) { |
| 146 | addPass(P: createDeadStoreEliminationPass()); |
| 147 | } |
| 148 | addPass(P: createDXILLegalizeLegacyPass()); |
| 149 | addPass(P: createDXILResourceImplicitBindingLegacyPass()); |
| 150 | addPass(P: createDXILTranslateMetadataLegacyPass()); |
| 151 | addPass(P: createDXILPostOptimizationValidationLegacyPass()); |
| 152 | addPass(P: createDXILOpLoweringLegacyPass()); |
| 153 | addPass(P: createDXILPrepareModulePass()); |
| 154 | addPass(P: createDXILDebugInfoLegacyPass()); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT, |
| 159 | StringRef CPU, StringRef FS, |
| 160 | const TargetOptions &Options, |
| 161 | std::optional<Reloc::Model> RM, |
| 162 | std::optional<CodeModel::Model> CM, |
| 163 | CodeGenOptLevel OL, bool JIT) |
| 164 | : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options, Reloc::Static, |
| 165 | CodeModel::Small, OL), |
| 166 | TLOF(std::make_unique<DXILTargetObjectFile>()), |
| 167 | Subtarget(std::make_unique<DirectXSubtarget>(args: TT, args&: CPU, args&: FS, args&: *this)) { |
| 168 | initAsmInfo(); |
| 169 | } |
| 170 | |
| 171 | DirectXTargetMachine::~DirectXTargetMachine() {} |
| 172 | |
| 173 | void DirectXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { |
| 174 | #define GET_PASS_REGISTRY "DirectXPassRegistry.def" |
| 175 | #include "llvm/Passes/TargetPassRegistry.inc" |
| 176 | } |
| 177 | |
| 178 | bool DirectXTargetMachine::addPassesToEmitFile( |
| 179 | PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, |
| 180 | CodeGenFileType FileType, bool DisableVerify, |
| 181 | MachineModuleInfoWrapperPass *MMIWP) { |
| 182 | TargetPassConfig *PassConfig = createPassConfig(PM); |
| 183 | PM.add(P: PassConfig); |
| 184 | |
| 185 | if (!MMIWP) |
| 186 | MMIWP = new MachineModuleInfoWrapperPass(this); |
| 187 | PM.add(P: MMIWP); |
| 188 | |
| 189 | PM.add(P: createTargetTransformInfoWrapperPass(TIRA: getTargetIRAnalysis())); |
| 190 | PassConfig->addCodeGenPrepare(); |
| 191 | |
| 192 | switch (FileType) { |
| 193 | case CodeGenFileType::AssemblyFile: |
| 194 | PM.add(P: createDXILPrettyPrinterLegacyPass(OS&: Out)); |
| 195 | break; |
| 196 | case CodeGenFileType::ObjectFile: |
| 197 | if (PassConfig->willCompleteCodeGenPipeline()) { |
| 198 | PM.add(P: createDXILEmbedderPass()); |
| 199 | // We embed the other DXContainer globals after embedding DXIL so that the |
| 200 | // globals don't pollute the DXIL. |
| 201 | PM.add(P: createDXContainerGlobalsPass()); |
| 202 | PM.add(P: createDXContainerPDBPass()); |
| 203 | |
| 204 | if (addAsmPrinter(PM, Out, DwoOut, FileType, |
| 205 | Context&: MMIWP->getMMI().getContext())) |
| 206 | return true; |
| 207 | } else |
| 208 | PM.add(P: createDXILWriterPass(Str&: Out)); |
| 209 | break; |
| 210 | case CodeGenFileType::Null: |
| 211 | break; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM, |
| 217 | MCContext *&Ctx, |
| 218 | raw_pwrite_stream &Out, |
| 219 | bool DisableVerify) { |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 224 | return new DirectXPassConfig(*this, PM); |
| 225 | } |
| 226 | |
| 227 | const DirectXSubtarget * |
| 228 | DirectXTargetMachine::getSubtargetImpl(const Function &) const { |
| 229 | return Subtarget.get(); |
| 230 | } |
| 231 | |
| 232 | TargetTransformInfo |
| 233 | DirectXTargetMachine::getTargetTransformInfo(const Function &F) const { |
| 234 | return TargetTransformInfo(std::make_unique<DirectXTTIImpl>(args: this, args: F)); |
| 235 | } |
| 236 | |
| 237 | DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM, |
| 238 | const DirectXSubtarget &STI) |
| 239 | : TargetLowering(TM, STI) { |
| 240 | addRegisterClass(VT: MVT::i32, RC: &dxil::DXILClassRegClass); |
| 241 | computeRegisterProperties(TRI: STI.getRegisterInfo()); |
| 242 | } |
| 243 | |