LLVM 19.0.0git
DXContainerGlobals.cpp
Go to the documentation of this file.
1//===- DXContainerGlobals.cpp - DXContainer global generator 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// DXContainerGlobalsPass implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DXILShaderFlags.h"
14#include "DirectX.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/Constants.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/MD5.h"
25
26using namespace llvm;
27using namespace llvm::dxil;
28using namespace llvm::mcdxbc;
29
30namespace {
31class DXContainerGlobals : public llvm::ModulePass {
32
33 GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
35 GlobalVariable *getFeatureFlags(Module &M);
36 GlobalVariable *computeShaderHash(Module &M);
37 GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
39 void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
40
41public:
42 static char ID; // Pass identification, replacement for typeid
43 DXContainerGlobals() : ModulePass(ID) {
45 }
46
47 StringRef getPassName() const override {
48 return "DXContainer Global Emitter";
49 }
50
51 bool runOnModule(Module &M) override;
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.setPreservesAll();
56 }
57};
58
59} // namespace
60
61bool DXContainerGlobals::runOnModule(Module &M) {
63 Globals.push_back(getFeatureFlags(M));
64 Globals.push_back(computeShaderHash(M));
65 addSignature(M, Globals);
66 appendToCompilerUsed(M, Globals);
67 return true;
68}
69
70GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {
72 static_cast<uint64_t>(getAnalysis<ShaderFlagsAnalysisWrapper>()
73 .getShaderFlags()
74 .getFeatureFlags());
75
76 Constant *FeatureFlagsConstant =
77 ConstantInt::get(M.getContext(), APInt(64, FeatureFlags));
78 return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
79}
80
81GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
82 auto *DXILConstant =
83 cast<ConstantDataArray>(M.getNamedGlobal("dx.dxil")->getInitializer());
84 MD5 Digest;
85 Digest.update(DXILConstant->getRawDataValues());
86 MD5::MD5Result Result = Digest.final();
87
88 dxbc::ShaderHash HashData = {0, {0}};
89 // The Hash's IncludesSource flag gets set whenever the hashed shader includes
90 // debug information.
91 if (M.debug_compile_units_begin() != M.debug_compile_units_end())
92 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
93
94 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
96 HashData.swapBytes();
97 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
98
99 Constant *ModuleConstant =
100 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
101 return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
102}
103
104GlobalVariable *DXContainerGlobals::buildContainerGlobal(
106 auto *GV = new llvm::GlobalVariable(
107 M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
108 GV->setSection(SectionName);
109 GV->setAlignment(Align(4));
110 return GV;
111}
112
113GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
116 SmallString<256> Data;
118 Sig.write(OS);
120 ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
121 return buildContainerGlobal(M, Constant, Name, SectionName);
122}
123
124void DXContainerGlobals::addSignature(Module &M,
126 // FIXME: support graphics shader.
127 // see issue https://github.com/llvm/llvm-project/issues/90504.
128
129 Signature InputSig;
130 Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));
131
132 Signature OutputSig;
133 Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
134}
135
136char DXContainerGlobals::ID = 0;
137INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
138 "DXContainer Global Emitter", false, true)
140INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
141 "DXContainer Global Emitter", false, true)
142
144 return new DXContainerGlobals();
145}
basic Basic Alias true
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil DXContainer Global Emitter
dxil globals
T Content
std::string Name
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
Class for arbitrary precision integers.
Definition: APInt.h:76
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2881
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:705
This is an important base class in LLVM.
Definition: Constant.h:41
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
Definition: MD5.h:41
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
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.
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
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Wrapper pass for the legacy pass manager.
void write(raw_ostream &OS)
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:26
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createDXContainerGlobalsPass()
Pass for generating DXContainer part globals.
@ Global
Append to llvm.global_dtors.
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
void initializeDXContainerGlobalsPass(PassRegistry &)
Initializer for DXContainerGlobals pass.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39