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