LLVM  16.0.0git
DirectXTargetMachine.cpp
Go to the documentation of this file.
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 "DXILResourceAnalysis.h"
16 #include "DXILShaderFlags.h"
18 #include "DirectX.h"
19 #include "DirectXSubtarget.h"
23 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/MC/SectionKind.h"
29 #include "llvm/MC/TargetRegistry.h"
31 #include "llvm/Support/CodeGen.h"
32 #include "llvm/Support/Compiler.h"
35 #include <optional>
36 
37 using namespace llvm;
38 
41  auto *PR = PassRegistry::getPassRegistry();
48 }
49 
51 public:
52  DXILTargetObjectFile() = default;
53 
55  const TargetMachine &TM) const override {
56  return getContext().getDXContainerSection(GO->getSection(), Kind);
57  }
58 
59 protected:
61  const TargetMachine &TM) const override {
62  llvm_unreachable("Not supported!");
63  }
64 };
65 
67 public:
69  : TargetPassConfig(TM, PM) {}
70 
72  return getTM<DirectXTargetMachine>();
73  }
74 
75  FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
76  void addCodeGenPrepare() override {
78  addPass(createDXILPrepareModulePass());
80  }
81 };
82 
84  StringRef CPU, StringRef FS,
85  const TargetOptions &Options,
86  std::optional<Reloc::Model> RM,
87  std::optional<CodeModel::Model> CM,
88  CodeGenOpt::Level OL, bool JIT)
90  "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
91  "f32:32-f64:64-n8:16:32:64",
92  TT, CPU, FS, Options, Reloc::Static, CodeModel::Small,
93  OL),
94  TLOF(std::make_unique<DXILTargetObjectFile>()),
95  Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {
96  initAsmInfo();
97 }
98 
100 
105  if (PassName == "print-dxil-resource") {
107  return true;
108  }
109  if (PassName == "print-dx-shader-flags") {
111  return true;
112  }
113  return false;
114  });
115 
117  MAM.registerPass([&] { return DXILResourceAnalysis(); });
118  MAM.registerPass([&] { return dxil::ShaderFlagsAnalysis(); });
119  });
120 }
121 
124  CodeGenFileType FileType, bool DisableVerify,
126  TargetPassConfig *PassConfig = createPassConfig(PM);
127  PassConfig->addCodeGenPrepare();
128 
131  // We embed the other DXContainer globals after embedding DXIL so that the
132  // globals don't pollute the DXIL.
134  }
135  switch (FileType) {
136  case CGFT_AssemblyFile:
138  PM.add(createPrintModulePass(Out, "", true));
139  break;
140  case CGFT_ObjectFile:
142  if (!MMIWP)
143  MMIWP = new MachineModuleInfoWrapperPass(this);
144  PM.add(MMIWP);
145  if (addAsmPrinter(PM, Out, DwoOut, FileType,
146  MMIWP->getMMI().getContext()))
147  return true;
148  } else
149  PM.add(createDXILWriterPass(Out));
150  break;
151  case CGFT_Null:
152  break;
153  }
154  return false;
155 }
156 
158  MCContext *&Ctx,
159  raw_pwrite_stream &Out,
160  bool DisableVerify) {
161  return true;
162 }
163 
165  return new DirectXPassConfig(*this, PM);
166 }
167 
168 const DirectXSubtarget *
170  return Subtarget.get();
171 }
172 
175  return TargetTransformInfo(DirectXTTIImpl(this, F));
176 }
177 
179  const DirectXSubtarget &STI)
180  : TargetLowering(TM) {}
llvm::DirectXTargetMachine::getTargetTransformInfo
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
Definition: DirectXTargetMachine.cpp:174
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PassBuilder.h
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:143
llvm::CGFT_ObjectFile
@ CGFT_ObjectFile
Definition: CodeGen.h:65
DirectXPassConfig::DirectXPassConfig
DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM)
Definition: DirectXTargetMachine.cpp:68
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::TargetPassConfig::willCompleteCodeGenPipeline
static bool willCompleteCodeGenPipeline()
Returns true if none of the -stop-before and -stop-after options is set.
Definition: TargetPassConfig.cpp:675
T
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::Function
Definition: Function.h:60
llvm::PassManager::addPass
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:544
llvm::createDXILPrettyPrinterPass
ModulePass * createDXILPrettyPrinterPass(raw_ostream &OS)
Pass to pretty print DXIL metadata.
Definition: DXILPrettyPrinter.cpp:62
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:111
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:150
llvm::DirectXTargetMachine
Definition: DirectXTargetMachine.h:20
ErrorHandling.h
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:173
llvm::DirectXSubtarget
Definition: DirectXSubtarget.h:30
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::CGFT_Null
@ CGFT_Null
Definition: CodeGen.h:66
llvm::initializeDXILPrepareModulePass
void initializeDXILPrepareModulePass(PassRegistry &)
Initializer for DXIL-prepare.
llvm::MachineModuleInfoWrapperPass::getMMI
MachineModuleInfo & getMMI()
Definition: MachineModuleInfo.h:228
llvm::PassBuilder::registerAnalysisRegistrationCallback
void registerAnalysisRegistrationCallback(const std::function< void(CGSCCAnalysisManager &)> &C)
{{@ Register callbacks for analysis registration with this PassBuilder instance.
Definition: PassBuilder.h:515
DirectXPassConfig::addCodeGenPrepare
void addCodeGenPrepare() override
Add pass to prepare the LLVM IR for code generation.
Definition: DirectXTargetMachine.cpp:76
llvm::DXILResourcePrinterPass
Printer pass for the DXILResourceAnalysis results.
Definition: DXILResourceAnalysis.h:30
LegacyPassManager.h
DXILWriterPass.h
DXILResourceAnalysis.h
llvm::createDXILWriterPass
ModulePass * createDXILWriterPass(raw_ostream &Str)
Create and return a pass that writes the module to the specified ostream.
Definition: DXILWriterPass.cpp:102
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::TargetLoweringObjectFile
Definition: TargetLoweringObjectFile.h:45
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
DirectXTargetTransformInfo.h
llvm::initializeDXILTranslateMetadataPass
void initializeDXILTranslateMetadataPass(PassRegistry &)
Initializer for DXILTranslateMetadata.
DirectX.h
llvm::MSP430Attrs::CodeModel
CodeModel
Definition: MSP430Attributes.h:37
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::createDXILTranslateMetadataPass
ModulePass * createDXILTranslateMetadataPass()
Pass to emit metadata for DXIL.
Definition: DXILTranslateMetadata.cpp:57
llvm::DirectXTargetMachine::~DirectXTargetMachine
~DirectXTargetMachine() override
Definition: DirectXTargetMachine.cpp:99
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::TargetLowering
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Definition: TargetLowering.h:3506
llvm::DirectXTargetLowering::DirectXTargetLowering
DirectXTargetLowering(const DirectXTargetMachine &TM, const DirectXSubtarget &STI)
Definition: DirectXTargetMachine.cpp:178
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::PassBuilder
This class provides access to building LLVM's passes.
Definition: PassBuilder.h:97
llvm::createDXILOpLoweringLegacyPass
ModulePass * createDXILOpLoweringLegacyPass()
Pass to lowering LLVM intrinsic call to DXIL op function call.
Definition: DXILOpLowering.cpp:108
llvm::DirectXTargetMachine::registerPassBuilderCallbacks
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline.
Definition: DirectXTargetMachine.cpp:101
llvm::DirectXTTIImpl
Definition: DirectXTargetTransformInfo.h:20
llvm::initializeEmbedDXILPassPass
void initializeEmbedDXILPassPass(PassRegistry &)
Initializer for dxil embedder pass.
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1355
IRPrintingPasses.h
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::X86AS::FS
@ FS
Definition: X86.h:200
DirectXPassConfig::createTargetRegisterAllocator
FunctionPass * createTargetRegisterAllocator(bool) override
createTargetRegisterAllocator - Create the register allocator pass for this target at the current opt...
Definition: DirectXTargetMachine.cpp:75
DXILTargetObjectFile
Definition: DirectXTargetMachine.cpp:50
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::legacy::PassManagerBase::add
virtual void add(Pass *P)=0
Add a pass to the queue of passes to run.
llvm::MachO::FileType
FileType
Defines the file type this file represents.
Definition: InterfaceFile.h:53
Passes.h
llvm::TargetPassConfig
Target-Independent Code Generator Pass Configuration Options.
Definition: TargetPassConfig.h:84
llvm::dxil::ShaderFlagsAnalysis
Definition: DXILShaderFlags.h:54
DirectXTargetMachine.h
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:415
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:525
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:214
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:127
TargetPassConfig.h
llvm::TargetPassConfig::addCodeGenPrepare
virtual void addCodeGenPrepare()
Add pass to prepare the LLVM IR for code generation.
Definition: TargetPassConfig.cpp:996
llvm::initializeDXILOpLoweringLegacyPass
void initializeDXILOpLoweringLegacyPass(PassRegistry &)
Initializer for DXILOpLowering.
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::initializeShaderFlagsAnalysisWrapperPass
void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &)
Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
MachineModuleInfo.h
llvm::PassBuilder::registerPipelineParsingCallback
void registerPipelineParsingCallback(const std::function< bool(StringRef Name, CGSCCPassManager &, ArrayRef< PipelineElement >)> &C)
{{@ Register pipeline parsing callbacks with this pass builder instance.
Definition: PassBuilder.h:537
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:487
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::CodeGenFileType
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition: CodeGen.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
TargetLoweringObjectFile.h
llvm::DXILResourceAnalysis
Analysis pass that exposes the DXILResource for a module.
Definition: DXILResourceAnalysis.h:20
Compiler.h
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
MCSectionDXContainer.h
llvm::DirectXTargetMachine::createPassConfig
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: DirectXTargetMachine.cpp:164
DXILTargetObjectFile::getExplicitSectionGlobal
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
Definition: DirectXTargetMachine.cpp:54
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::LLVMTargetMachine::initAsmInfo
void initAsmInfo()
Definition: LLVMTargetMachine.cpp:40
llvm::getTheDirectXTarget
Target & getTheDirectXTarget()
Definition: DirectXTargetInfo.cpp:19
llvm::PassManager< Module >
llvm::createDXILPrepareModulePass
ModulePass * createDXILPrepareModulePass()
Pass to convert modules into DXIL-compatable modules.
Definition: DXILPrepare.cpp:189
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
std
Definition: BitVector.h:851
DirectXPassConfig::getDirectXTargetMachine
DirectXTargetMachine & getDirectXTargetMachine() const
Definition: DirectXTargetMachine.cpp:71
llvm::DirectXTargetMachine::getSubtargetImpl
const DirectXSubtarget * getSubtargetImpl(const Function &) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
Definition: DirectXTargetMachine.cpp:169
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:409
llvm::AnalysisManager::registerPass
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
Definition: PassManager.h:836
llvm::createDXILEmbedderPass
ModulePass * createDXILEmbedderPass()
Create and return a pass that writes the module to a global variable in the module for later emission...
Definition: DXILWriterPass.cpp:109
DXILTargetObjectFile::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: DirectXTargetMachine.cpp:60
SectionKind.h
llvm::createDXContainerGlobalsPass
ModulePass * createDXContainerGlobalsPass()
Pass for generating DXContainer part globals.
Definition: DXContainerGlobals.cpp:69
llvm::DirectXTargetMachine::addPassesToEmitMC
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, raw_pwrite_stream &Out, bool DisableVerify) override
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
Definition: DirectXTargetMachine.cpp:157
CodeGen.h
LLVMInitializeDirectXTarget
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget()
Definition: DirectXTargetMachine.cpp:39
llvm::dxil::ShaderFlagsAnalysisPrinter
Printer pass for ShaderFlagsAnalysis results.
Definition: DXILShaderFlags.h:67
llvm::DirectXTargetMachine::DirectXTargetMachine
DirectXTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Definition: DirectXTargetMachine.cpp:83
DirectXTargetInfo.h
llvm::legacy::PassManagerBase
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Definition: LegacyPassManager.h:39
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::DirectXTargetMachine::addPassesToEmitFile
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, bool DisableVerify, MachineModuleInfoWrapperPass *MMIWP) override
Add passes to the specified pass manager to get the specified file emitted.
Definition: DirectXTargetMachine.cpp:122
DirectXSubtarget.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
PB
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
DirectXPassConfig
Definition: DirectXTargetMachine.cpp:66
DXILShaderFlags.h
llvm::CGFT_AssemblyFile
@ CGFT_AssemblyFile
Definition: CodeGen.h:64
TargetRegistry.h
PassName
static const char PassName[]
Definition: X86LowerAMXIntrinsics.cpp:671
llvm::initializeDXILResourceWrapperPass
void initializeDXILResourceWrapperPass(PassRegistry &)
Initializer for DXILTranslateMetadata.
llvm::LLVMTargetMachine::addAsmPrinter
bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, MCContext &Context)
Adds an AsmPrinter pass to the pipeline that prints assembly or machine code from the MI representati...
Definition: LLVMTargetMachine.cpp:125
llvm::createPrintModulePass
ModulePass * createPrintModulePass(raw_ostream &OS, const std::string &Banner="", bool ShouldPreserveUseListOrder=false)
Create and return a pass that writes the module to the specified raw_ostream.