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 "DirectXSubtarget.h"
16 #include "DirectXTargetTransformInfo.h"
17 #include "TargetInfo/DirectXTargetInfo.h"
18 #include "llvm/Bitcode/BitcodeWriterPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/TargetPassConfig.h"
21 #include "llvm/IR/IRPrintingPasses.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/MC/SectionKind.h"
24 #include "llvm/MC/TargetRegistry.h"
25 #include "llvm/Support/CodeGen.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 
30 using namespace llvm;
31 
32 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
33   RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget());
34 }
35 
36 class DXILTargetObjectFile : public TargetLoweringObjectFile {
37 public:
38   DXILTargetObjectFile() = default;
39 
40   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
41                                       const TargetMachine &TM) const override {
42     llvm_unreachable("Not supported!");
43   }
44 
45 protected:
46   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
47                                     const TargetMachine &TM) const override {
48     llvm_unreachable("Not supported!");
49   }
50 };
51 
52 class DirectXPassConfig : public TargetPassConfig {
53 public:
54   DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM)
55       : TargetPassConfig(TM, PM) {}
56 
57   DirectXTargetMachine &getDirectXTargetMachine() const {
58     return getTM<DirectXTargetMachine>();
59   }
60 
61   FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
62 };
63 
64 DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
65                                            StringRef CPU, StringRef FS,
66                                            const TargetOptions &Options,
67                                            Optional<Reloc::Model> RM,
68                                            Optional<CodeModel::Model> CM,
69                                            CodeGenOpt::Level OL, bool JIT)
70     : LLVMTargetMachine(T,
71                         "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
72                         "f32:32-f64:64-n8:16:32:64",
73                         TT, CPU, FS, Options, Reloc::Static, CodeModel::Small,
74                         OL),
75       TLOF(std::make_unique<DXILTargetObjectFile>()),
76       Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {}
77 
78 DirectXTargetMachine::~DirectXTargetMachine() {}
79 
80 bool DirectXTargetMachine::addPassesToEmitFile(
81     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
82     CodeGenFileType FileType, bool DisableVerify,
83     MachineModuleInfoWrapperPass *MMIWP) {
84   switch (FileType) {
85   case CGFT_AssemblyFile:
86     PM.add(createPrintModulePass(Out, "", true));
87     break;
88   case CGFT_ObjectFile:
89     // TODO: Write DXIL instead of bitcode
90     PM.add(createBitcodeWriterPass(Out, true, false, false));
91     break;
92   case CGFT_Null:
93     break;
94   }
95   return false;
96 }
97 
98 bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
99                                              MCContext *&Ctx,
100                                              raw_pwrite_stream &Out,
101                                              bool DisableVerify) {
102   return true;
103 }
104 
105 TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) {
106   return new DirectXPassConfig(*this, PM);
107 }
108 
109 const DirectXSubtarget *
110 DirectXTargetMachine::getSubtargetImpl(const Function &) const {
111   return Subtarget.get();
112 }
113 
114 TargetTransformInfo
115 DirectXTargetMachine::getTargetTransformInfo(const Function &F) const {
116   return TargetTransformInfo(DirectXTTIImpl(this, F));
117 }
118 
119 DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM,
120                                              const DirectXSubtarget &STI)
121     : TargetLowering(TM) {}
122