LLVM 20.0.0git
DXILWriterPass.cpp
Go to the documentation of this file.
1//===- DXILWriterPass.cpp - Bitcode writing pass --------------------------===//
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// DXILWriterPass implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DXILWriterPass.h"
14#include "DXILBitcodeWriter.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/Constants.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/PassManager.h"
23#include "llvm/Pass.h"
26
27using namespace llvm;
28using namespace llvm::dxil;
29
30namespace {
31class WriteDXILPass : public llvm::ModulePass {
32 raw_ostream &OS; // raw_ostream to print on
33
34public:
35 static char ID; // Pass identification, replacement for typeid
36 WriteDXILPass() : ModulePass(ID), OS(dbgs()) {
38 }
39
40 explicit WriteDXILPass(raw_ostream &o) : ModulePass(ID), OS(o) {
42 }
43
44 StringRef getPassName() const override { return "Bitcode Writer"; }
45
46 bool runOnModule(Module &M) override {
47 WriteDXILToFile(M, OS);
48 return false;
49 }
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesAll();
52 }
53};
54
55class EmbedDXILPass : public llvm::ModulePass {
56public:
57 static char ID; // Pass identification, replacement for typeid
58 EmbedDXILPass() : ModulePass(ID) {
60 }
61
62 StringRef getPassName() const override { return "DXIL Embedder"; }
63
64 bool runOnModule(Module &M) override {
65 std::string Data;
67
68 const std::string OriginalTriple = M.getTargetTriple();
69 // Set to DXIL triple when write to bitcode.
70 // Only the output bitcode need to be DXIL triple.
71 M.setTargetTriple("dxil-ms-dx");
72
74
75 // Recover triple.
76 M.setTargetTriple(OriginalTriple);
77
78 Constant *ModuleConstant =
79 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
80 auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
82 ModuleConstant, "dx.dxil");
83 GV->setSection("DXIL");
84 GV->setAlignment(Align(4));
85 appendToCompilerUsed(M, {GV});
86 return true;
87 }
88
89 void getAnalysisUsage(AnalysisUsage &AU) const override {
90 AU.setPreservesAll();
91 }
92};
93} // namespace
94
95char WriteDXILPass::ID = 0;
96INITIALIZE_PASS_BEGIN(WriteDXILPass, "dxil-write-bitcode", "Write Bitcode",
97 false, true)
100 true)
101
103 return new WriteDXILPass(Str);
104}
105
106char EmbedDXILPass::ID = 0;
107INITIALIZE_PASS(EmbedDXILPass, "dxil-embed", "Embed DXIL", false, true)
108
109ModulePass *llvm::createDXILEmbedderPass() { return new EmbedDXILPass(); }
basic Basic Alias true
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil write Write Bitcode
dxil write bitcode
This file provides a bitcode writing pass.
This file defines the DenseMap class.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This is the interface to build a ModuleSummaryIndex for a module.
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
raw_pwrite_stream & OS
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:709
This is an important base class in LLVM.
Definition: Constant.h:42
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Legacy wrapper pass to provide the ModuleSummaryIndex object.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
void WriteDXILToFile(const Module &M, raw_ostream &Out)
Write the specified module to the specified raw output stream.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Write
Definition: CodeGenData.h:108
ModulePass * createDXILWriterPass(raw_ostream &Str)
Create and return a pass that writes the module to the specified ostream.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition: DWP.cpp:625
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeEmbedDXILPassPass(PassRegistry &)
Initializer for dxil embedder pass.
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
ModulePass * createDXILEmbedderPass()
Create and return a pass that writes the module to a global variable in the module for later emission...
void initializeWriteDXILPassPass(PassRegistry &)
Initializer for dxil writer pass.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39