LLVM 18.0.0git
Go to the documentation of this file.
1//===- DXContainerGlobals.cpp - DXContainer global generator pass ---------===//
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
9// DXContainerGlobalsPass implementation.
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"
21#include "llvm/Pass.h"
22#include "llvm/Support/MD5.h"
25using namespace llvm;
26using namespace llvm::dxil;
28namespace {
29class DXContainerGlobals : public llvm::ModulePass {
31 GlobalVariable *getShaderFlags(Module &M);
32 GlobalVariable *computeShaderHash(Module &M);
35 static char ID; // Pass identification, replacement for typeid
36 DXContainerGlobals() : ModulePass(ID) {
38 }
40 StringRef getPassName() const override {
41 return "DXContainer Global Emitter";
42 }
44 bool runOnModule(Module &M) override;
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.setPreservesAll();
49 }
52} // namespace
54bool DXContainerGlobals::runOnModule(Module &M) {
56 Globals.push_back(getShaderFlags(M));
57 Globals.push_back(computeShaderHash(M));
59 appendToCompilerUsed(M, Globals);
60 return true;
63GlobalVariable *DXContainerGlobals::getShaderFlags(Module &M) {
64 const uint64_t Flags =
65 (uint64_t)(getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags());
67 Constant *FlagsConstant = ConstantInt::get(M.getContext(), APInt(64, Flags));
68 auto *GV = new llvm::GlobalVariable(M, FlagsConstant->getType(), true,
70 FlagsConstant, "dx.sfi0");
71 GV->setSection("SFI0");
72 GV->setAlignment(Align(4));
73 return GV;
76GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
77 auto *DXILConstant =
78 cast<ConstantDataArray>(M.getNamedGlobal("dx.dxil")->getInitializer());
79 MD5 Digest;
80 Digest.update(DXILConstant->getRawDataValues());
81 MD5::MD5Result Result = Digest.final();
83 dxbc::ShaderHash HashData = {0, {0}};
84 // The Hash's IncludesSource flag gets set whenever the hashed shader includes
85 // debug information.
86 if (M.debug_compile_units_begin() != M.debug_compile_units_end())
87 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
89 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
91 HashData.swapBytes();
92 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
94 Constant *ModuleConstant =
95 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
96 auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
98 ModuleConstant, "dx.hash");
99 GV->setSection("HASH");
100 GV->setAlignment(Align(4));
101 return GV;
104char DXContainerGlobals::ID = 0;
105INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
106 "DXContainer Global Emitter", false, true)
108INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
109 "DXContainer Global Emitter", false, true)
112 return new DXContainerGlobals();
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
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
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 * 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:690
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
This is an important base class in LLVM.
Definition: Constant.h:41
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
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
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
Wrapper pass for the legacy pass manager.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:54
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