LLVM 20.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"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/Module.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/MD5.h"
29
30using namespace llvm;
31using namespace llvm::dxil;
32using namespace llvm::mcdxbc;
33
34namespace {
35class DXContainerGlobals : public llvm::ModulePass {
36
37 GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
39 GlobalVariable *getFeatureFlags(Module &M);
40 GlobalVariable *computeShaderHash(Module &M);
41 GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
43 void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
44 void addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV);
45 void addPipelineStateValidationInfo(Module &M,
47
48public:
49 static char ID; // Pass identification, replacement for typeid
50 DXContainerGlobals() : ModulePass(ID) {
52 }
53
54 StringRef getPassName() const override {
55 return "DXContainer Global Emitter";
56 }
57
58 bool runOnModule(Module &M) override;
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AU.setPreservesAll();
66 }
67};
68
69} // namespace
70
71bool DXContainerGlobals::runOnModule(Module &M) {
73 Globals.push_back(getFeatureFlags(M));
74 Globals.push_back(computeShaderHash(M));
75 addSignature(M, Globals);
76 addPipelineStateValidationInfo(M, Globals);
77 appendToCompilerUsed(M, Globals);
78 return true;
79}
80
81GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {
82 uint64_t CombinedFeatureFlags = getAnalysis<ShaderFlagsAnalysisWrapper>()
83 .getShaderFlags()
84 .getCombinedFlags()
85 .getFeatureFlags();
86
87 Constant *FeatureFlagsConstant =
88 ConstantInt::get(M.getContext(), APInt(64, CombinedFeatureFlags));
89 return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
90}
91
92GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
93 auto *DXILConstant =
94 cast<ConstantDataArray>(M.getNamedGlobal("dx.dxil")->getInitializer());
95 MD5 Digest;
96 Digest.update(DXILConstant->getRawDataValues());
97 MD5::MD5Result Result = Digest.final();
98
99 dxbc::ShaderHash HashData = {0, {0}};
100 // The Hash's IncludesSource flag gets set whenever the hashed shader includes
101 // debug information.
102 if (M.debug_compile_units_begin() != M.debug_compile_units_end())
103 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
104
105 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
107 HashData.swapBytes();
108 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
109
110 Constant *ModuleConstant =
111 ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
112 return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
113}
114
115GlobalVariable *DXContainerGlobals::buildContainerGlobal(
117 auto *GV = new llvm::GlobalVariable(
118 M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
119 GV->setSection(SectionName);
120 GV->setAlignment(Align(4));
121 return GV;
122}
123
124GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
127 SmallString<256> Data;
129 Sig.write(OS);
131 ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
132 return buildContainerGlobal(M, Constant, Name, SectionName);
133}
134
135void DXContainerGlobals::addSignature(Module &M,
137 // FIXME: support graphics shader.
138 // see issue https://github.com/llvm/llvm-project/issues/90504.
139
140 Signature InputSig;
141 Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));
142
143 Signature OutputSig;
144 Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
145}
146
147void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
148 const DXILBindingMap &DBM =
149 getAnalysis<DXILResourceBindingWrapperPass>().getBindingMap();
150 DXILResourceTypeMap &DRTM =
151 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
152
153 for (const dxil::ResourceBindingInfo &RBI : DBM) {
155 RBI.getBinding();
157 BindInfo.LowerBound = Binding.LowerBound;
158 BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
159 BindInfo.Space = Binding.Space;
160
161 dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
162 dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
163 bool IsUAV = TypeInfo.getResourceClass() == dxil::ResourceClass::UAV;
164 switch (TypeInfo.getResourceKind()) {
165 case dxil::ResourceKind::Sampler:
166 ResType = dxbc::PSV::ResourceType::Sampler;
167 break;
168 case dxil::ResourceKind::CBuffer:
169 ResType = dxbc::PSV::ResourceType::CBV;
170 break;
171 case dxil::ResourceKind::StructuredBuffer:
172 ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
173 : dxbc::PSV::ResourceType::SRVStructured;
174 if (IsUAV && TypeInfo.getUAV().HasCounter)
175 ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
176 break;
177 case dxil::ResourceKind::RTAccelerationStructure:
178 ResType = dxbc::PSV::ResourceType::SRVRaw;
179 break;
180 case dxil::ResourceKind::RawBuffer:
181 ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
182 : dxbc::PSV::ResourceType::SRVRaw;
183 break;
184 default:
185 ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
186 : dxbc::PSV::ResourceType::SRVTyped;
187 break;
188 }
189 BindInfo.Type = ResType;
190
191 BindInfo.Kind =
192 static_cast<dxbc::PSV::ResourceKind>(TypeInfo.getResourceKind());
193 // TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
194 // with https://github.com/llvm/llvm-project/issues/104392
195 BindInfo.Flags.Flags = 0u;
196
197 PSV.Resources.emplace_back(BindInfo);
198 }
199}
200
201void DXContainerGlobals::addPipelineStateValidationInfo(
202 Module &M, SmallVector<GlobalValue *> &Globals) {
203 SmallString<256> Data;
205 PSVRuntimeInfo PSV;
207 PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max();
208
210 getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
211 assert(MMI.EntryPropertyVec.size() == 1 ||
214 static_cast<uint8_t>(MMI.ShaderProfile - Triple::Pixel);
215
216 addResourcesForPSV(M, PSV);
217
218 // Hardcoded values here to unblock loading the shader into D3D.
219 //
220 // TODO: Lots more stuff to do here!
221 //
222 // See issue https://github.com/llvm/llvm-project/issues/96674.
223 switch (MMI.ShaderProfile) {
224 case Triple::Compute:
225 PSV.BaseData.NumThreadsX = MMI.EntryPropertyVec[0].NumThreadsX;
226 PSV.BaseData.NumThreadsY = MMI.EntryPropertyVec[0].NumThreadsY;
227 PSV.BaseData.NumThreadsZ = MMI.EntryPropertyVec[0].NumThreadsZ;
228 break;
229 default:
230 break;
231 }
232
234 PSV.EntryName = MMI.EntryPropertyVec[0].Entry->getName();
235
236 PSV.finalize(MMI.ShaderProfile);
237 PSV.write(OS);
239 ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
240 Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.psv0", "PSV0"));
241}
242
243char DXContainerGlobals::ID = 0;
244INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
245 "DXContainer Global Emitter", false, true)
250INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
251 "DXContainer Global Emitter", false, true)
252
254 return new DXContainerGlobals();
255}
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:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:78
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:2990
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:709
This is an important base class in LLVM.
Definition: Constant.h:42
@ 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:937
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
dxil::ResourceClass getResourceClass() const
Definition: DXILResource.h:294
dxil::ResourceKind getResourceKind() const
Definition: DXILResource.h:295
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
Triple::EnvironmentType ShaderProfile
SmallVector< EntryProperties > EntryPropertyVec
dxbc::PSV::v3::RuntimeInfo BaseData
SmallVector< dxbc::PSV::v2::ResourceBindInfo > Resources
void finalize(Triple::EnvironmentType Stage)
void write(raw_ostream &OS, uint32_t Version=std::numeric_limits< uint32_t >::max()) const