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