LLVM 17.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"
15#include "llvm/ADT/StringRef.h"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/IR/Constants.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/MD5.h"
23
24using namespace llvm;
25using namespace llvm::dxil;
26
27namespace {
28class DXContainerGlobals : public llvm::ModulePass {
29
30 GlobalVariable *getShaderFlags(Module &M);
31 GlobalVariable *computeShaderHash(Module &M);
32
33public:
34 static char ID; // Pass identification, replacement for typeid
35 DXContainerGlobals() : ModulePass(ID) {
37 }
38
39 StringRef getPassName() const override {
40 return "DXContainer Global Emitter";
41 }
42
43 bool runOnModule(Module &M) override;
44
45 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 AU.setPreservesAll();
48 }
49};
50
51} // namespace
52
53bool DXContainerGlobals::runOnModule(Module &M) {
55 Globals.push_back(getShaderFlags(M));
56 Globals.push_back(computeShaderHash(M));
57
58 appendToCompilerUsed(M, Globals);
59 return true;
60}
61
62GlobalVariable *DXContainerGlobals::getShaderFlags(Module &M) {
63 const uint64_t Flags =
64 (uint64_t)(getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags());
65
66 Constant *FlagsConstant = ConstantInt::get(M.getContext(), APInt(64, Flags));
67 auto *GV = new llvm::GlobalVariable(M, FlagsConstant->getType(), true,
69 FlagsConstant, "dx.sfi0");
70 GV->setSection("SFI0");
71 GV->setAlignment(Align(4));
72 return GV;
73}
74
75GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
76 auto *DXILConstant =
77 cast<ConstantDataArray>(M.getNamedGlobal("dx.dxil")->getInitializer());
78 MD5 Digest;
79 Digest.update(DXILConstant->getRawDataValues());
80 MD5::MD5Result Result = Digest.final();
81
82 dxbc::ShaderHash HashData = {0, {0}};
83 // The Hash's IncludesSource flag gets set whenever the hashed shader includes
84 // debug information.
85 if (M.debug_compile_units_begin() != M.debug_compile_units_end())
86 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
87
88 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
90 HashData.swapBytes();
91 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
92
93 Constant *ModuleConstant =
94 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
95 auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
97 ModuleConstant, "dx.hash");
98 GV->setSection("HASH");
99 GV->setAlignment(Align(4));
100 return GV;
101}
102
103char DXContainerGlobals::ID = 0;
104INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
105 "DXContainer Global Emitter", false, true)
107INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
108 "DXContainer Global Emitter", false, true)
109
111 return new DXContainerGlobals();
112}
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
#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
Class for arbitrary precision integers.
Definition: APInt.h:75
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:691
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:887
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:248
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:64
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