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"
17#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/MD5.h"
27
28using namespace llvm;
29using namespace llvm::dxil;
30using namespace llvm::mcdxbc;
31
32namespace {
33class DXContainerGlobals : public llvm::ModulePass {
34
35 GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
37 GlobalVariable *getFeatureFlags(Module &M);
38 GlobalVariable *computeShaderHash(Module &M);
39 GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
41 void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
42 void addPipelineStateValidationInfo(Module &M,
44
45public:
46 static char ID; // Pass identification, replacement for typeid
47 DXContainerGlobals() : ModulePass(ID) {
49 }
50
51 StringRef getPassName() const override {
52 return "DXContainer Global Emitter";
53 }
54
55 bool runOnModule(Module &M) override;
56
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 AU.setPreservesAll();
60 }
61};
62
63} // namespace
64
65bool DXContainerGlobals::runOnModule(Module &M) {
67 Globals.push_back(getFeatureFlags(M));
68 Globals.push_back(computeShaderHash(M));
69 addSignature(M, Globals);
70 addPipelineStateValidationInfo(M, Globals);
71 appendToCompilerUsed(M, Globals);
72 return true;
73}
74
75GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {
77 static_cast<uint64_t>(getAnalysis<ShaderFlagsAnalysisWrapper>()
78 .getShaderFlags()
79 .getFeatureFlags());
80
81 Constant *FeatureFlagsConstant =
82 ConstantInt::get(M.getContext(), APInt(64, FeatureFlags));
83 return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
84}
85
86GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
87 auto *DXILConstant =
88 cast<ConstantDataArray>(M.getNamedGlobal("dx.dxil")->getInitializer());
89 MD5 Digest;
90 Digest.update(DXILConstant->getRawDataValues());
91 MD5::MD5Result Result = Digest.final();
92
93 dxbc::ShaderHash HashData = {0, {0}};
94 // The Hash's IncludesSource flag gets set whenever the hashed shader includes
95 // debug information.
96 if (M.debug_compile_units_begin() != M.debug_compile_units_end())
97 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
98
99 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
101 HashData.swapBytes();
102 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
103
104 Constant *ModuleConstant =
105 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
106 return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
107}
108
109GlobalVariable *DXContainerGlobals::buildContainerGlobal(
111 auto *GV = new llvm::GlobalVariable(
112 M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
113 GV->setSection(SectionName);
114 GV->setAlignment(Align(4));
115 return GV;
116}
117
118GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
121 SmallString<256> Data;
123 Sig.write(OS);
125 ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
126 return buildContainerGlobal(M, Constant, Name, SectionName);
127}
128
129void DXContainerGlobals::addSignature(Module &M,
131 // FIXME: support graphics shader.
132 // see issue https://github.com/llvm/llvm-project/issues/90504.
133
134 Signature InputSig;
135 Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));
136
137 Signature OutputSig;
138 Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
139}
140
141void DXContainerGlobals::addPipelineStateValidationInfo(
142 Module &M, SmallVector<GlobalValue *> &Globals) {
143 SmallString<256> Data;
145 PSVRuntimeInfo PSV;
146 Triple TT(M.getTargetTriple());
148 PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max();
150 static_cast<uint8_t>(TT.getEnvironment() - Triple::Pixel);
151
152 // Hardcoded values here to unblock loading the shader into D3D.
153 //
154 // TODO: Lots more stuff to do here!
155 //
156 // See issue https://github.com/llvm/llvm-project/issues/96674.
157 PSV.BaseData.NumThreadsX = 1;
158 PSV.BaseData.NumThreadsY = 1;
159 PSV.BaseData.NumThreadsZ = 1;
160 PSV.EntryName = "main";
161
162 PSV.finalize(TT.getEnvironment());
163 PSV.write(OS);
165 ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
166 Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.psv0", "PSV0"));
167}
168
169char DXContainerGlobals::ID = 0;
170INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
171 "DXContainer Global Emitter", false, true)
173INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
174 "DXContainer Global Emitter", false, true)
175
177 return new DXContainerGlobals();
178}
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
Module.h This file contains the declarations for the Module class.
#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 defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Class for arbitrary precision integers.
Definition: APInt.h:77
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:2900
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:706
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
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:691
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
dxbc::PSV::v3::RuntimeInfo BaseData
void finalize(Triple::EnvironmentType Stage)
void write(raw_ostream &OS, uint32_t Version=std::numeric_limits< uint32_t >::max()) const