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*85285be9SXiang Li   initializeDXILOpLoweringLegacyPass(*PR);
3844a14a6aSChris Bieneman }
3944a14a6aSChris Bieneman 
4044a14a6aSChris Bieneman class DXILTargetObjectFile : public TargetLoweringObjectFile {
4144a14a6aSChris Bieneman public:
4244a14a6aSChris Bieneman   DXILTargetObjectFile() = default;
4344a14a6aSChris Bieneman 
4444a14a6aSChris Bieneman   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
4544a14a6aSChris Bieneman                                       const TargetMachine &TM) const override {
4644a14a6aSChris Bieneman     llvm_unreachable("Not supported!");
4744a14a6aSChris Bieneman   }
4844a14a6aSChris Bieneman 
4944a14a6aSChris Bieneman protected:
5044a14a6aSChris Bieneman   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
5144a14a6aSChris Bieneman                                     const TargetMachine &TM) const override {
5244a14a6aSChris Bieneman     llvm_unreachable("Not supported!");
5344a14a6aSChris Bieneman   }
5444a14a6aSChris Bieneman };
5544a14a6aSChris Bieneman 
5644a14a6aSChris Bieneman class DirectXPassConfig : public TargetPassConfig {
5744a14a6aSChris Bieneman public:
5844a14a6aSChris Bieneman   DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM)
5944a14a6aSChris Bieneman       : TargetPassConfig(TM, PM) {}
6044a14a6aSChris Bieneman 
6144a14a6aSChris Bieneman   DirectXTargetMachine &getDirectXTargetMachine() const {
6244a14a6aSChris Bieneman     return getTM<DirectXTargetMachine>();
6344a14a6aSChris Bieneman   }
6444a14a6aSChris Bieneman 
6544a14a6aSChris Bieneman   FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
6644a14a6aSChris Bieneman };
6744a14a6aSChris Bieneman 
6844a14a6aSChris Bieneman DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
6944a14a6aSChris Bieneman                                            StringRef CPU, StringRef FS,
7044a14a6aSChris Bieneman                                            const TargetOptions &Options,
7144a14a6aSChris Bieneman                                            Optional<Reloc::Model> RM,
7244a14a6aSChris Bieneman                                            Optional<CodeModel::Model> CM,
7344a14a6aSChris Bieneman                                            CodeGenOpt::Level OL, bool JIT)
7444a14a6aSChris Bieneman     : LLVMTargetMachine(T,
7544a14a6aSChris Bieneman                         "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
7644a14a6aSChris Bieneman                         "f32:32-f64:64-n8:16:32:64",
7744a14a6aSChris Bieneman                         TT, CPU, FS, Options, Reloc::Static, CodeModel::Small,
7844a14a6aSChris Bieneman                         OL),
7944a14a6aSChris Bieneman       TLOF(std::make_unique<DXILTargetObjectFile>()),
8044a14a6aSChris Bieneman       Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {}
8144a14a6aSChris Bieneman 
8244a14a6aSChris Bieneman DirectXTargetMachine::~DirectXTargetMachine() {}
8344a14a6aSChris Bieneman 
8444a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitFile(
8544a14a6aSChris Bieneman     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
8644a14a6aSChris Bieneman     CodeGenFileType FileType, bool DisableVerify,
8744a14a6aSChris Bieneman     MachineModuleInfoWrapperPass *MMIWP) {
88*85285be9SXiang Li   PM.add(createDXILOpLoweringLegacyPass());
896599fdabSChris Bieneman   PM.add(createDXILPrepareModulePass());
9044a14a6aSChris Bieneman   switch (FileType) {
9144a14a6aSChris Bieneman   case CGFT_AssemblyFile:
9244a14a6aSChris Bieneman     PM.add(createPrintModulePass(Out, "", true));
9344a14a6aSChris Bieneman     break;
9444a14a6aSChris Bieneman   case CGFT_ObjectFile:
95f2526c1aSChris Bieneman     // TODO: Use MC Object streamer to write DXContainer
96f2526c1aSChris Bieneman     PM.add(createDXILWriterPass(Out));
9744a14a6aSChris Bieneman     break;
9844a14a6aSChris Bieneman   case CGFT_Null:
9944a14a6aSChris Bieneman     break;
10044a14a6aSChris Bieneman   }
10144a14a6aSChris Bieneman   return false;
10244a14a6aSChris Bieneman }
10344a14a6aSChris Bieneman 
10444a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
10544a14a6aSChris Bieneman                                              MCContext *&Ctx,
10644a14a6aSChris Bieneman                                              raw_pwrite_stream &Out,
10744a14a6aSChris Bieneman                                              bool DisableVerify) {
10844a14a6aSChris Bieneman   return true;
10944a14a6aSChris Bieneman }
11044a14a6aSChris Bieneman 
11144a14a6aSChris Bieneman TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) {
11244a14a6aSChris Bieneman   return new DirectXPassConfig(*this, PM);
11344a14a6aSChris Bieneman }
11444a14a6aSChris Bieneman 
11544a14a6aSChris Bieneman const DirectXSubtarget *
11644a14a6aSChris Bieneman DirectXTargetMachine::getSubtargetImpl(const Function &) const {
11744a14a6aSChris Bieneman   return Subtarget.get();
11844a14a6aSChris Bieneman }
11944a14a6aSChris Bieneman 
12044a14a6aSChris Bieneman TargetTransformInfo
12144a14a6aSChris Bieneman DirectXTargetMachine::getTargetTransformInfo(const Function &F) const {
12244a14a6aSChris Bieneman   return TargetTransformInfo(DirectXTTIImpl(this, F));
12344a14a6aSChris Bieneman }
12444a14a6aSChris Bieneman 
12544a14a6aSChris Bieneman DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM,
12644a14a6aSChris Bieneman                                              const DirectXSubtarget &STI)
12744a14a6aSChris Bieneman     : TargetLowering(TM) {}
128