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"
20*2af61e62SChris Bieneman #include "llvm/CodeGen/MachineModuleInfo.h"
2144a14a6aSChris Bieneman #include "llvm/CodeGen/Passes.h"
2244a14a6aSChris Bieneman #include "llvm/CodeGen/TargetPassConfig.h"
2344a14a6aSChris Bieneman #include "llvm/IR/IRPrintingPasses.h"
2444a14a6aSChris Bieneman #include "llvm/IR/LegacyPassManager.h"
25*2af61e62SChris Bieneman #include "llvm/MC/MCSectionDXContainer.h"
2644a14a6aSChris Bieneman #include "llvm/MC/SectionKind.h"
2744a14a6aSChris Bieneman #include "llvm/MC/TargetRegistry.h"
2844a14a6aSChris Bieneman #include "llvm/Support/CodeGen.h"
2944a14a6aSChris Bieneman #include "llvm/Support/Compiler.h"
3044a14a6aSChris Bieneman #include "llvm/Support/ErrorHandling.h"
3144a14a6aSChris Bieneman #include "llvm/Target/TargetLoweringObjectFile.h"
3244a14a6aSChris Bieneman
3344a14a6aSChris Bieneman using namespace llvm;
3444a14a6aSChris Bieneman
LLVMInitializeDirectXTarget()3544a14a6aSChris Bieneman extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
3644a14a6aSChris Bieneman RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget());
376599fdabSChris Bieneman auto *PR = PassRegistry::getPassRegistry();
386599fdabSChris Bieneman initializeDXILPrepareModulePass(*PR);
39d401a993SChris Bieneman initializeEmbedDXILPassPass(*PR);
4085285be9SXiang Li initializeDXILOpLoweringLegacyPass(*PR);
410c7f7f1bSpython3kgae initializeDXILTranslateMetadataPass(*PR);
4244a14a6aSChris Bieneman }
4344a14a6aSChris Bieneman
4444a14a6aSChris Bieneman class DXILTargetObjectFile : public TargetLoweringObjectFile {
4544a14a6aSChris Bieneman public:
4644a14a6aSChris Bieneman DXILTargetObjectFile() = default;
4744a14a6aSChris Bieneman
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const4844a14a6aSChris Bieneman MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
4944a14a6aSChris Bieneman const TargetMachine &TM) const override {
50*2af61e62SChris Bieneman return getContext().getDXContainerSection(GO->getSection(), Kind);
5144a14a6aSChris Bieneman }
5244a14a6aSChris Bieneman
5344a14a6aSChris Bieneman protected:
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const5444a14a6aSChris Bieneman MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
5544a14a6aSChris Bieneman const TargetMachine &TM) const override {
5644a14a6aSChris Bieneman llvm_unreachable("Not supported!");
5744a14a6aSChris Bieneman }
5844a14a6aSChris Bieneman };
5944a14a6aSChris Bieneman
6044a14a6aSChris Bieneman class DirectXPassConfig : public TargetPassConfig {
6144a14a6aSChris Bieneman public:
DirectXPassConfig(DirectXTargetMachine & TM,PassManagerBase & PM)6244a14a6aSChris Bieneman DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM)
6344a14a6aSChris Bieneman : TargetPassConfig(TM, PM) {}
6444a14a6aSChris Bieneman
getDirectXTargetMachine() const6544a14a6aSChris Bieneman DirectXTargetMachine &getDirectXTargetMachine() const {
6644a14a6aSChris Bieneman return getTM<DirectXTargetMachine>();
6744a14a6aSChris Bieneman }
6844a14a6aSChris Bieneman
createTargetRegisterAllocator(bool)6944a14a6aSChris Bieneman FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
7044a14a6aSChris Bieneman };
7144a14a6aSChris Bieneman
DirectXTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)7244a14a6aSChris Bieneman DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
7344a14a6aSChris Bieneman StringRef CPU, StringRef FS,
7444a14a6aSChris Bieneman const TargetOptions &Options,
7544a14a6aSChris Bieneman Optional<Reloc::Model> RM,
7644a14a6aSChris Bieneman Optional<CodeModel::Model> CM,
7744a14a6aSChris Bieneman CodeGenOpt::Level OL, bool JIT)
7844a14a6aSChris Bieneman : LLVMTargetMachine(T,
7944a14a6aSChris Bieneman "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
8044a14a6aSChris Bieneman "f32:32-f64:64-n8:16:32:64",
8144a14a6aSChris Bieneman TT, CPU, FS, Options, Reloc::Static, CodeModel::Small,
8244a14a6aSChris Bieneman OL),
8344a14a6aSChris Bieneman TLOF(std::make_unique<DXILTargetObjectFile>()),
84*2af61e62SChris Bieneman Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {
85*2af61e62SChris Bieneman initAsmInfo();
86*2af61e62SChris Bieneman }
8744a14a6aSChris Bieneman
~DirectXTargetMachine()8844a14a6aSChris Bieneman DirectXTargetMachine::~DirectXTargetMachine() {}
8944a14a6aSChris Bieneman
addPassesToEmitFile(PassManagerBase & PM,raw_pwrite_stream & Out,raw_pwrite_stream * DwoOut,CodeGenFileType FileType,bool DisableVerify,MachineModuleInfoWrapperPass * MMIWP)9044a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitFile(
9144a14a6aSChris Bieneman PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
9244a14a6aSChris Bieneman CodeGenFileType FileType, bool DisableVerify,
9344a14a6aSChris Bieneman MachineModuleInfoWrapperPass *MMIWP) {
9485285be9SXiang Li PM.add(createDXILOpLoweringLegacyPass());
956599fdabSChris Bieneman PM.add(createDXILPrepareModulePass());
960c7f7f1bSpython3kgae PM.add(createDXILTranslateMetadataPass());
97d401a993SChris Bieneman if (TargetPassConfig::willCompleteCodeGenPipeline()) {
98d401a993SChris Bieneman PM.add(createDXILEmbedderPass());
99d401a993SChris Bieneman }
100*2af61e62SChris Bieneman switch (FileType) {
101*2af61e62SChris Bieneman case CGFT_AssemblyFile:
10244a14a6aSChris Bieneman PM.add(createPrintModulePass(Out, "", true));
10344a14a6aSChris Bieneman break;
10444a14a6aSChris Bieneman case CGFT_ObjectFile:
105*2af61e62SChris Bieneman if (TargetPassConfig::willCompleteCodeGenPipeline()) {
106*2af61e62SChris Bieneman if (!MMIWP)
107*2af61e62SChris Bieneman MMIWP = new MachineModuleInfoWrapperPass(this);
108*2af61e62SChris Bieneman PM.add(MMIWP);
109*2af61e62SChris Bieneman if (addAsmPrinter(PM, Out, DwoOut, FileType,
110*2af61e62SChris Bieneman MMIWP->getMMI().getContext()))
111*2af61e62SChris Bieneman return true;
112*2af61e62SChris Bieneman } else
113f2526c1aSChris Bieneman PM.add(createDXILWriterPass(Out));
11444a14a6aSChris Bieneman break;
11544a14a6aSChris Bieneman case CGFT_Null:
11644a14a6aSChris Bieneman break;
11744a14a6aSChris Bieneman }
11844a14a6aSChris Bieneman return false;
11944a14a6aSChris Bieneman }
12044a14a6aSChris Bieneman
addPassesToEmitMC(PassManagerBase & PM,MCContext * & Ctx,raw_pwrite_stream & Out,bool DisableVerify)12144a14a6aSChris Bieneman bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
12244a14a6aSChris Bieneman MCContext *&Ctx,
12344a14a6aSChris Bieneman raw_pwrite_stream &Out,
12444a14a6aSChris Bieneman bool DisableVerify) {
12544a14a6aSChris Bieneman return true;
12644a14a6aSChris Bieneman }
12744a14a6aSChris Bieneman
createPassConfig(PassManagerBase & PM)12844a14a6aSChris Bieneman TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) {
12944a14a6aSChris Bieneman return new DirectXPassConfig(*this, PM);
13044a14a6aSChris Bieneman }
13144a14a6aSChris Bieneman
13244a14a6aSChris Bieneman const DirectXSubtarget *
getSubtargetImpl(const Function &) const13344a14a6aSChris Bieneman DirectXTargetMachine::getSubtargetImpl(const Function &) const {
13444a14a6aSChris Bieneman return Subtarget.get();
13544a14a6aSChris Bieneman }
13644a14a6aSChris Bieneman
13744a14a6aSChris Bieneman TargetTransformInfo
getTargetTransformInfo(const Function & F) const13844a14a6aSChris Bieneman DirectXTargetMachine::getTargetTransformInfo(const Function &F) const {
13944a14a6aSChris Bieneman return TargetTransformInfo(DirectXTTIImpl(this, F));
14044a14a6aSChris Bieneman }
14144a14a6aSChris Bieneman
DirectXTargetLowering(const DirectXTargetMachine & TM,const DirectXSubtarget & STI)14244a14a6aSChris Bieneman DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM,
14344a14a6aSChris Bieneman const DirectXSubtarget &STI)
14444a14a6aSChris Bieneman : TargetLowering(TM) {}
145