144a14a6aSChris Bieneman //===- DirectXTargetMachine.cpp - DirectX Target Implementation -*- C++ -*-===// 244a14a6aSChris Bieneman // 344a14a6aSChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 444a14a6aSChris Bieneman // See https://llvm.org/LICENSE.txt for license information. 544a14a6aSChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 644a14a6aSChris Bieneman // 744a14a6aSChris Bieneman //===----------------------------------------------------------------------===// 844a14a6aSChris Bieneman /// 944a14a6aSChris Bieneman /// \file 1044a14a6aSChris Bieneman /// This file contains DirectX target initializer. 1144a14a6aSChris Bieneman /// 1244a14a6aSChris Bieneman //===----------------------------------------------------------------------===// 1344a14a6aSChris Bieneman 1444a14a6aSChris Bieneman #include "DirectXTargetMachine.h" 15f2526c1aSChris Bieneman #include "DXILWriter/DXILWriterPass.h" 166599fdabSChris Bieneman #include "DirectX.h" 1744a14a6aSChris Bieneman #include "DirectXSubtarget.h" 1844a14a6aSChris Bieneman #include "DirectXTargetTransformInfo.h" 1944a14a6aSChris Bieneman #include "TargetInfo/DirectXTargetInfo.h" 2044a14a6aSChris Bieneman #include "llvm/CodeGen/Passes.h" 2144a14a6aSChris Bieneman #include "llvm/CodeGen/TargetPassConfig.h" 2244a14a6aSChris Bieneman #include "llvm/IR/IRPrintingPasses.h" 2344a14a6aSChris Bieneman #include "llvm/IR/LegacyPassManager.h" 2444a14a6aSChris Bieneman #include "llvm/MC/SectionKind.h" 2544a14a6aSChris Bieneman #include "llvm/MC/TargetRegistry.h" 2644a14a6aSChris Bieneman #include "llvm/Support/CodeGen.h" 2744a14a6aSChris Bieneman #include "llvm/Support/Compiler.h" 2844a14a6aSChris Bieneman #include "llvm/Support/ErrorHandling.h" 2944a14a6aSChris Bieneman #include "llvm/Target/TargetLoweringObjectFile.h" 3044a14a6aSChris Bieneman 3144a14a6aSChris Bieneman using namespace llvm; 3244a14a6aSChris Bieneman 3344a14a6aSChris Bieneman extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() { 3444a14a6aSChris Bieneman RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget()); 356599fdabSChris Bieneman auto *PR = PassRegistry::getPassRegistry(); 366599fdabSChris Bieneman initializeDXILPrepareModulePass(*PR); 37*d401a993SChris Bieneman initializeEmbedDXILPassPass(*PR); 3885285be9SXiang Li initializeDXILOpLoweringLegacyPass(*PR); 390c7f7f1bSpython3kgae initializeDXILTranslateMetadataPass(*PR); 4044a14a6aSChris Bieneman } 4144a14a6aSChris Bieneman 4244a14a6aSChris Bieneman class DXILTargetObjectFile : public TargetLoweringObjectFile { 4344a14a6aSChris Bieneman public: 4444a14a6aSChris Bieneman DXILTargetObjectFile() = default; 4544a14a6aSChris Bieneman 4644a14a6aSChris Bieneman MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 4744a14a6aSChris Bieneman const TargetMachine &TM) const override { 4844a14a6aSChris Bieneman llvm_unreachable("Not supported!"); 4944a14a6aSChris Bieneman } 5044a14a6aSChris Bieneman 5144a14a6aSChris Bieneman protected: 5244a14a6aSChris Bieneman MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 5344a14a6aSChris Bieneman const TargetMachine &TM) const override { 5444a14a6aSChris Bieneman llvm_unreachable("Not supported!"); 5544a14a6aSChris Bieneman } 5644a14a6aSChris Bieneman }; 5744a14a6aSChris Bieneman 5844a14a6aSChris Bieneman class DirectXPassConfig : public TargetPassConfig { 5944a14a6aSChris Bieneman public: 6044a14a6aSChris Bieneman DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM) 6144a14a6aSChris Bieneman : TargetPassConfig(TM, PM) {} 6244a14a6aSChris Bieneman 6344a14a6aSChris Bieneman DirectXTargetMachine &getDirectXTargetMachine() const { 6444a14a6aSChris Bieneman return getTM<DirectXTargetMachine>(); 6544a14a6aSChris Bieneman } 6644a14a6aSChris Bieneman 6744a14a6aSChris Bieneman FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; } 6844a14a6aSChris Bieneman }; 6944a14a6aSChris Bieneman 7044a14a6aSChris Bieneman DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT, 7144a14a6aSChris Bieneman StringRef CPU, StringRef FS, 7244a14a6aSChris Bieneman const TargetOptions &Options, 7344a14a6aSChris Bieneman Optional<Reloc::Model> RM, 7444a14a6aSChris Bieneman Optional<CodeModel::Model> CM, 7544a14a6aSChris Bieneman CodeGenOpt::Level OL, bool JIT) 7644a14a6aSChris Bieneman : LLVMTargetMachine(T, 7744a14a6aSChris Bieneman "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-" 7844a14a6aSChris Bieneman "f32:32-f64:64-n8:16:32:64", 7944a14a6aSChris Bieneman TT, CPU, FS, Options, Reloc::Static, CodeModel::Small, 8044a14a6aSChris Bieneman OL), 8144a14a6aSChris Bieneman TLOF(std::make_unique<DXILTargetObjectFile>()), 8244a14a6aSChris Bieneman Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {} 8344a14a6aSChris Bieneman 8444a14a6aSChris Bieneman DirectXTargetMachine::~DirectXTargetMachine() {} 8544a14a6aSChris Bieneman 8644a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitFile( 8744a14a6aSChris Bieneman PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, 8844a14a6aSChris Bieneman CodeGenFileType FileType, bool DisableVerify, 8944a14a6aSChris Bieneman MachineModuleInfoWrapperPass *MMIWP) { 9085285be9SXiang Li PM.add(createDXILOpLoweringLegacyPass()); 916599fdabSChris Bieneman PM.add(createDXILPrepareModulePass()); 920c7f7f1bSpython3kgae PM.add(createDXILTranslateMetadataPass()); 9344a14a6aSChris Bieneman switch (FileType) { 9444a14a6aSChris Bieneman case CGFT_AssemblyFile: 95*d401a993SChris Bieneman if (TargetPassConfig::willCompleteCodeGenPipeline()) { 96*d401a993SChris Bieneman PM.add(createDXILEmbedderPass()); 97*d401a993SChris Bieneman } 9844a14a6aSChris Bieneman PM.add(createPrintModulePass(Out, "", true)); 9944a14a6aSChris Bieneman break; 10044a14a6aSChris Bieneman case CGFT_ObjectFile: 101f2526c1aSChris Bieneman // TODO: Use MC Object streamer to write DXContainer 102f2526c1aSChris Bieneman PM.add(createDXILWriterPass(Out)); 10344a14a6aSChris Bieneman break; 10444a14a6aSChris Bieneman case CGFT_Null: 10544a14a6aSChris Bieneman break; 10644a14a6aSChris Bieneman } 10744a14a6aSChris Bieneman return false; 10844a14a6aSChris Bieneman } 10944a14a6aSChris Bieneman 11044a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM, 11144a14a6aSChris Bieneman MCContext *&Ctx, 11244a14a6aSChris Bieneman raw_pwrite_stream &Out, 11344a14a6aSChris Bieneman bool DisableVerify) { 11444a14a6aSChris Bieneman return true; 11544a14a6aSChris Bieneman } 11644a14a6aSChris Bieneman 11744a14a6aSChris Bieneman TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) { 11844a14a6aSChris Bieneman return new DirectXPassConfig(*this, PM); 11944a14a6aSChris Bieneman } 12044a14a6aSChris Bieneman 12144a14a6aSChris Bieneman const DirectXSubtarget * 12244a14a6aSChris Bieneman DirectXTargetMachine::getSubtargetImpl(const Function &) const { 12344a14a6aSChris Bieneman return Subtarget.get(); 12444a14a6aSChris Bieneman } 12544a14a6aSChris Bieneman 12644a14a6aSChris Bieneman TargetTransformInfo 12744a14a6aSChris Bieneman DirectXTargetMachine::getTargetTransformInfo(const Function &F) const { 12844a14a6aSChris Bieneman return TargetTransformInfo(DirectXTTIImpl(this, F)); 12944a14a6aSChris Bieneman } 13044a14a6aSChris Bieneman 13144a14a6aSChris Bieneman DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM, 13244a14a6aSChris Bieneman const DirectXSubtarget &STI) 13344a14a6aSChris Bieneman : TargetLowering(TM) {} 134