LLVM: lib/Target/DirectX/DXContainerGlobals.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
31#include
32
33using namespace llvm;
36
37namespace {
39
40 GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
41 StringRef Name, StringRef SectionName);
42 GlobalVariable *getFeatureFlags(Module &M);
43 GlobalVariable *computeShaderHash(Module &M);
44 GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
45 StringRef SectionName);
48 void addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV);
49 void addPipelineStateValidationInfo(Module &M,
51
52public:
53 static char ID;
54 DXContainerGlobals() : ModulePass(ID) {}
55
56 StringRef getPassName() const override {
57 return "DXContainer Global Emitter";
58 }
59
60 bool runOnModule(Module &M) override;
61
62 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.addRequired();
65 AU.addRequired();
66 AU.addRequired();
67 AU.addRequired();
68 AU.addRequired();
69 }
70};
71
72}
73
74bool DXContainerGlobals::runOnModule(Module &M) {
76 Globals.push_back(getFeatureFlags(M));
77 Globals.push_back(computeShaderHash(M));
78 addSignature(M, Globals);
79 addRootSignature(M, Globals);
80 addPipelineStateValidationInfo(M, Globals);
82 return true;
83}
84
85GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {
86 uint64_t CombinedFeatureFlags = getAnalysis()
87 .getShaderFlags()
88 .getCombinedFlags()
89 .getFeatureFlags();
90
91 Constant *FeatureFlagsConstant =
92 ConstantInt::get(M.getContext(), APInt(64, CombinedFeatureFlags));
93 return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
94}
95
96GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
97 auto *DXILConstant =
99 MD5 Digest;
100 Digest.update(DXILConstant->getRawDataValues());
102
103 dxbc::ShaderHash HashData = {0, {0}};
104
105
106 if (.debug_compile_units().empty())
107 HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource);
108
109 memcpy(reinterpret_cast<void *>(&HashData.Digest), Result.data(), 16);
112 StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash));
113
116 return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
117}
118
119GlobalVariable *DXContainerGlobals::buildContainerGlobal(
120 Module &M, Constant *Content, StringRef Name, StringRef SectionName) {
121 auto *GV = new llvm::GlobalVariable(
123 GV->setSection(SectionName);
124 GV->setAlignment(Align(4));
125 return GV;
126}
127
128GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
129 StringRef Name,
130 StringRef SectionName) {
131 SmallString<256> Data;
132 raw_svector_ostream OS(Data);
136 return buildContainerGlobal(M, Constant, Name, SectionName);
137}
138
139void DXContainerGlobals::addSignature(Module &M,
141
142
143
144 Signature InputSig;
145 Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));
146
147 Signature OutputSig;
148 Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
149}
150
151void DXContainerGlobals::addRootSignature(Module &M,
153
154 dxil::ModuleMetadataInfo &MMI =
155 getAnalysis().getModuleMetadata();
156
157
159 return;
160
161 auto &RSA = getAnalysis().getRSInfo();
162 const Function *EntryFunction = nullptr;
163
167 }
168
169 const mcdxbc::RootSignatureDesc *RS = RSA.getDescForFunction(EntryFunction);
170 if (!RS)
171 return;
172
173 SmallString<256> Data;
174 raw_svector_ostream OS(Data);
175
176 RS->write(OS);
177
180 Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.rts0", "RTS0"));
181}
182
183void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
184 const DXILResourceMap &DRM =
185 getAnalysis().getResourceMap();
186 DXILResourceTypeMap &DRTM =
187 getAnalysis().getResourceTypeMap();
188
189 auto MakeBinding =
190 [](const dxil::ResourceInfo::ResourceBinding &Binding,
192 const dxbc::PSV::ResourceFlags Flags = dxbc::PSV::ResourceFlags()) {
193 dxbc::PSV::v2::ResourceBindInfo BindInfo;
197 (Binding.Size == UINT32_MAX ||
198 (uint64_t)Binding.LowerBound + Binding.Size - 1 <= UINT32_MAX) &&
199 "Resource range is too large");
201 ? UINT32_MAX
206 return BindInfo;
207 };
208
209 for (const dxil::ResourceInfo &RI : DRM.cbuffers()) {
210 const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding();
211 PSV.Resources.push_back(MakeBinding(Binding, dxbc::PSV::ResourceType::CBV,
212 dxil::ResourceKind::CBuffer));
213 }
214 for (const dxil::ResourceInfo &RI : DRM.samplers()) {
215 const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding();
217 dxbc::PSV::ResourceType::Sampler,
218 dxil::ResourceKind::Sampler));
219 }
220 for (const dxil::ResourceInfo &RI : DRM.srvs()) {
221 const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding();
222
223 dxil::ResourceTypeInfo &TypeInfo = DRTM[RI.getHandleTy()];
226 ResType = dxbc::PSV::ResourceType::SRVStructured;
227 else if (TypeInfo.isTyped())
228 ResType = dxbc::PSV::ResourceType::SRVTyped;
229 else
230 ResType = dxbc::PSV::ResourceType::SRVRaw;
231
234 }
235 for (const dxil::ResourceInfo &RI : DRM.uavs()) {
236 const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding();
237
238 dxil::ResourceTypeInfo &TypeInfo = DRTM[RI.getHandleTy()];
240 if (RI.hasCounter())
241 ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
242 else if (TypeInfo.isStruct())
243 ResType = dxbc::PSV::ResourceType::UAVStructured;
244 else if (TypeInfo.isTyped())
245 ResType = dxbc::PSV::ResourceType::UAVTyped;
246 else
247 ResType = dxbc::PSV::ResourceType::UAVRaw;
248
249 dxbc::PSV::ResourceFlags Flags;
250
251
253
256 }
257}
258
259void DXContainerGlobals::addPipelineStateValidationInfo(
261 SmallString<256> Data;
262 raw_svector_ostream OS(Data);
263 PSVRuntimeInfo PSV;
266
267 dxil::ModuleMetadataInfo &MMI =
268 getAnalysis().getModuleMetadata();
274
275 addResourcesForPSV(M, PSV);
276
277
278
279
280
281
293 }
294 break;
295 default:
296 break;
297 }
298
302
307 Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.psv0", "PSV0"));
308}
309
310char DXContainerGlobals::ID = 0;
312 "DXContainer Global Emitter", false, true)
319
321 return new DXContainerGlobals();
322}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Resource Implicit Binding
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallVector class.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
static LLVM_ABI Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
iterator_range< iterator > samplers()
iterator_range< iterator > srvs()
iterator_range< iterator > cbuffers()
iterator_range< iterator > uavs()
@ PrivateLinkage
Like Internal, but omit from symbol table.
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
LLVM_ABI void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI bool isTyped() const
LLVM_ABI bool isStruct() const
dxil::ResourceKind getResourceKind() const
Wrapper pass for the legacy pass manager.
void write(raw_ostream &OS)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
ResourceKind
The kind of resource for an SRV or UAV resource.
constexpr bool IsBigEndianHost
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
ModulePass * createDXContainerGlobalsPass()
Pass for generating DXContainer part globals.
Definition DXContainerGlobals.cpp:320
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
uint32_t MaximumWaveLaneCount
uint32_t MinimumWaveLaneCount
dxbc::PSV::v3::RuntimeInfo BaseData
llvm::StringRef EntryName
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