LLVM 20.0.0git
DXILTranslateMetadata.cpp
Go to the documentation of this file.
1//===- DXILTranslateMetadata.cpp - Pass to emit DXIL metadata -------------===//
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
10#include "DXILResource.h"
12#include "DXILShaderFlags.h"
13#include "DirectX.h"
15#include "llvm/ADT/Twine.h"
18#include "llvm/IR/Constants.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Metadata.h"
25#include "llvm/IR/Module.h"
27#include "llvm/Pass.h"
31#include <cstdint>
32
33using namespace llvm;
34using namespace llvm::dxil;
35
36namespace {
37/// A simple Wrapper DiagnosticInfo that generates Module-level diagnostic
38/// for TranslateMetadata pass
39class DiagnosticInfoTranslateMD : public DiagnosticInfo {
40private:
41 const Twine &Msg;
42 const Module &Mod;
43
44public:
45 /// \p M is the module for which the diagnostic is being emitted. \p Msg is
46 /// the message to show. Note that this class does not copy this message, so
47 /// this reference must be valid for the whole life time of the diagnostic.
48 DiagnosticInfoTranslateMD(const Module &M, const Twine &Msg,
50 : DiagnosticInfo(DK_Unsupported, Severity), Msg(Msg), Mod(M) {}
51
52 void print(DiagnosticPrinter &DP) const override {
53 DP << Mod.getName() << ": " << Msg << '\n';
54 }
55};
56
57enum class EntryPropsTag {
58 ShaderFlags = 0,
59 GSState,
60 DSState,
61 HSState,
62 NumThreads,
63 AutoBindingSpace,
64 RayPayloadSize,
65 RayAttribSize,
66 ShaderKind,
67 MSState,
68 ASStateTag,
69 WaveSize,
70 EntryRootSig,
71};
72
73} // namespace
74
77 const dxil::Resources &MDResources) {
78 LLVMContext &Context = M.getContext();
79
80 for (ResourceBindingInfo &RI : DBM)
81 if (!RI.hasSymbol())
82 RI.createSymbol(M, DRTM[RI.getHandleTy()].createElementStruct());
83
84 SmallVector<Metadata *> SRVs, UAVs, CBufs, Smps;
85 for (const ResourceBindingInfo &RI : DBM.srvs())
86 SRVs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));
87 for (const ResourceBindingInfo &RI : DBM.uavs())
88 UAVs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));
89 for (const ResourceBindingInfo &RI : DBM.cbuffers())
90 CBufs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));
91 for (const ResourceBindingInfo &RI : DBM.samplers())
92 Smps.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));
93
94 Metadata *SRVMD = SRVs.empty() ? nullptr : MDNode::get(Context, SRVs);
95 Metadata *UAVMD = UAVs.empty() ? nullptr : MDNode::get(Context, UAVs);
96 Metadata *CBufMD = CBufs.empty() ? nullptr : MDNode::get(Context, CBufs);
97 Metadata *SmpMD = Smps.empty() ? nullptr : MDNode::get(Context, Smps);
98 bool HasResources = !DBM.empty();
99
100 if (MDResources.hasUAVs()) {
101 assert(!UAVMD && "Old and new UAV representations can't coexist");
102 UAVMD = MDResources.writeUAVs(M);
103 HasResources = true;
104 }
105
106 if (MDResources.hasCBuffers()) {
107 assert(!CBufMD && "Old and new cbuffer representations can't coexist");
108 CBufMD = MDResources.writeCBuffers(M);
109 HasResources = true;
110 }
111
112 if (!HasResources)
113 return nullptr;
114
115 NamedMDNode *ResourceMD = M.getOrInsertNamedMetadata("dx.resources");
116 ResourceMD->addOperand(
117 MDNode::get(M.getContext(), {SRVMD, UAVMD, CBufMD, SmpMD}));
118
119 return ResourceMD;
120}
121
123 switch (Env) {
124 case Triple::Pixel:
125 return "ps";
126 case Triple::Vertex:
127 return "vs";
128 case Triple::Geometry:
129 return "gs";
130 case Triple::Hull:
131 return "hs";
132 case Triple::Domain:
133 return "ds";
134 case Triple::Compute:
135 return "cs";
136 case Triple::Library:
137 return "lib";
138 case Triple::Mesh:
139 return "ms";
141 return "as";
142 default:
143 break;
144 }
145 llvm_unreachable("Unsupported environment for DXIL generation.");
146}
147
150}
151
156 ConstantInt::get(Type::getInt32Ty(Ctx), static_cast<int>(Tag))));
157 switch (Tag) {
158 case EntryPropsTag::ShaderFlags:
160 ConstantInt::get(Type::getInt64Ty(Ctx), Value)));
161 break;
162 case EntryPropsTag::ShaderKind:
164 ConstantInt::get(Type::getInt32Ty(Ctx), Value)));
165 break;
166 case EntryPropsTag::GSState:
167 case EntryPropsTag::DSState:
168 case EntryPropsTag::HSState:
169 case EntryPropsTag::NumThreads:
170 case EntryPropsTag::AutoBindingSpace:
171 case EntryPropsTag::RayPayloadSize:
172 case EntryPropsTag::RayAttribSize:
173 case EntryPropsTag::MSState:
174 case EntryPropsTag::ASStateTag:
175 case EntryPropsTag::WaveSize:
176 case EntryPropsTag::EntryRootSig:
177 llvm_unreachable("NYI: Unhandled entry property tag");
178 }
179 return MDVals;
180}
181
182static MDTuple *
184 const Triple::EnvironmentType ShaderProfile) {
186 LLVMContext &Ctx = EP.Entry->getContext();
187 if (EntryShaderFlags != 0)
188 MDVals.append(getTagValueAsMetadata(EntryPropsTag::ShaderFlags,
189 EntryShaderFlags, Ctx));
190
191 if (EP.Entry != nullptr) {
192 // FIXME: support more props.
193 // See https://github.com/llvm/llvm-project/issues/57948.
194 // Add shader kind for lib entries.
195 if (ShaderProfile == Triple::EnvironmentType::Library &&
196 EP.ShaderStage != Triple::EnvironmentType::Library)
197 MDVals.append(getTagValueAsMetadata(EntryPropsTag::ShaderKind,
198 getShaderStage(EP.ShaderStage), Ctx));
199
200 if (EP.ShaderStage == Triple::EnvironmentType::Compute) {
201 MDVals.emplace_back(ConstantAsMetadata::get(ConstantInt::get(
202 Type::getInt32Ty(Ctx), static_cast<int>(EntryPropsTag::NumThreads))));
203 Metadata *NumThreadVals[] = {ConstantAsMetadata::get(ConstantInt::get(
204 Type::getInt32Ty(Ctx), EP.NumThreadsX)),
205 ConstantAsMetadata::get(ConstantInt::get(
206 Type::getInt32Ty(Ctx), EP.NumThreadsY)),
207 ConstantAsMetadata::get(ConstantInt::get(
208 Type::getInt32Ty(Ctx), EP.NumThreadsZ))};
209 MDVals.emplace_back(MDNode::get(Ctx, NumThreadVals));
210 }
211 }
212 if (MDVals.empty())
213 return nullptr;
214 return MDNode::get(Ctx, MDVals);
215}
216
218 MDNode *Resources, MDTuple *Properties,
219 LLVMContext &Ctx) {
220 // Each entry point metadata record specifies:
221 // * reference to the entry point function global symbol
222 // * unmangled name
223 // * list of signatures
224 // * list of resources
225 // * list of tag-value pairs of shader capabilities and other properties
226 Metadata *MDVals[5];
227 MDVals[0] =
228 EntryFn ? ValueAsMetadata::get(const_cast<Function *>(EntryFn)) : nullptr;
229 MDVals[1] = MDString::get(Ctx, EntryFn ? EntryFn->getName() : "");
230 MDVals[2] = Signatures;
231 MDVals[3] = Resources;
232 MDVals[4] = Properties;
233 return MDNode::get(Ctx, MDVals);
234}
235
237 MDNode *MDResources,
238 const uint64_t EntryShaderFlags,
239 const Triple::EnvironmentType ShaderProfile) {
240 MDTuple *Properties =
241 getEntryPropAsMetadata(EP, EntryShaderFlags, ShaderProfile);
242 return constructEntryMetadata(EP.Entry, Signatures, MDResources, Properties,
243 EP.Entry->getContext());
244}
245
247 if (MMDI.ValidatorVersion.empty())
248 return;
249
250 LLVMContext &Ctx = M.getContext();
251 IRBuilder<> IRB(Ctx);
252 Metadata *MDVals[2];
253 MDVals[0] =
255 MDVals[1] = ConstantAsMetadata::get(
256 IRB.getInt32(MMDI.ValidatorVersion.getMinor().value_or(0)));
257 NamedMDNode *ValVerNode = M.getOrInsertNamedMetadata("dx.valver");
258 // Set validator version obtained from DXIL Metadata Analysis pass
259 ValVerNode->clearOperands();
260 ValVerNode->addOperand(MDNode::get(Ctx, MDVals));
261}
262
264 const ModuleMetadataInfo &MMDI) {
265 LLVMContext &Ctx = M.getContext();
266 IRBuilder<> IRB(Ctx);
267 Metadata *SMVals[3];
269 SMVals[0] = MDString::get(Ctx, getShortShaderStage(MMDI.ShaderProfile));
270 SMVals[1] = ConstantAsMetadata::get(IRB.getInt32(SM.getMajor()));
271 SMVals[2] = ConstantAsMetadata::get(IRB.getInt32(SM.getMinor().value_or(0)));
272 NamedMDNode *SMMDNode = M.getOrInsertNamedMetadata("dx.shaderModel");
273 SMMDNode->addOperand(MDNode::get(Ctx, SMVals));
274}
275
277 LLVMContext &Ctx = M.getContext();
278 IRBuilder<> IRB(Ctx);
279 VersionTuple DXILVer = MMDI.DXILVersion;
280 Metadata *DXILVals[2];
281 DXILVals[0] = ConstantAsMetadata::get(IRB.getInt32(DXILVer.getMajor()));
282 DXILVals[1] =
283 ConstantAsMetadata::get(IRB.getInt32(DXILVer.getMinor().value_or(0)));
284 NamedMDNode *DXILVerMDNode = M.getOrInsertNamedMetadata("dx.version");
285 DXILVerMDNode->addOperand(MDNode::get(Ctx, DXILVals));
286}
287
289 uint64_t ShaderFlags) {
290 LLVMContext &Ctx = M.getContext();
291 MDTuple *Properties = nullptr;
292 if (ShaderFlags != 0) {
294 MDVals.append(
295 getTagValueAsMetadata(EntryPropsTag::ShaderFlags, ShaderFlags, Ctx));
296 Properties = MDNode::get(Ctx, MDVals);
297 }
298 // Library has an entry metadata with resource table metadata and all other
299 // MDNodes as null.
300 return constructEntryMetadata(nullptr, nullptr, RMD, Properties, Ctx);
301}
302
305 const Resources &MDResources,
306 const ModuleShaderFlags &ShaderFlags,
307 const ModuleMetadataInfo &MMDI) {
308 LLVMContext &Ctx = M.getContext();
309 IRBuilder<> IRB(Ctx);
310 SmallVector<MDNode *> EntryFnMDNodes;
311
312 emitValidatorVersionMD(M, MMDI);
314 emitDXILVersionTupleMD(M, MMDI);
315 NamedMDNode *NamedResourceMD =
316 emitResourceMetadata(M, DBM, DRTM, MDResources);
317 auto *ResourceMD =
318 (NamedResourceMD != nullptr) ? NamedResourceMD->getOperand(0) : nullptr;
319 // FIXME: Add support to construct Signatures
320 // See https://github.com/llvm/llvm-project/issues/57928
321 MDTuple *Signatures = nullptr;
322
323 if (MMDI.ShaderProfile == Triple::EnvironmentType::Library) {
324 // Get the combined shader flag mask of all functions in the library to be
325 // used as shader flags mask value associated with top-level library entry
326 // metadata.
327 uint64_t CombinedMask = ShaderFlags.getCombinedFlags();
328 EntryFnMDNodes.emplace_back(
329 emitTopLevelLibraryNode(M, ResourceMD, CombinedMask));
330 } else if (MMDI.EntryPropertyVec.size() > 1) {
331 M.getContext().diagnose(DiagnosticInfoTranslateMD(
332 M, "Non-library shader: One and only one entry expected"));
333 }
334
335 for (const EntryProperties &EntryProp : MMDI.EntryPropertyVec) {
336 const ComputedShaderFlags &EntrySFMask =
337 ShaderFlags.getFunctionFlags(EntryProp.Entry);
338
339 // If ShaderProfile is Library, mask is already consolidated in the
340 // top-level library node. Hence it is not emitted.
341 uint64_t EntryShaderFlags = 0;
342 if (MMDI.ShaderProfile != Triple::EnvironmentType::Library) {
343 EntryShaderFlags = EntrySFMask;
344 if (EntryProp.ShaderStage != MMDI.ShaderProfile) {
345 M.getContext().diagnose(DiagnosticInfoTranslateMD(
346 M,
347 "Shader stage '" +
349 "' for entry '" + Twine(EntryProp.Entry->getName()) +
350 "' different from specified target profile '" +
352 "'"))));
353 }
354 }
355 EntryFnMDNodes.emplace_back(emitEntryMD(EntryProp, Signatures, ResourceMD,
356 EntryShaderFlags,
357 MMDI.ShaderProfile));
358 }
359
360 NamedMDNode *EntryPointsNamedMD =
361 M.getOrInsertNamedMetadata("dx.entryPoints");
362 for (auto *Entry : EntryFnMDNodes)
363 EntryPointsNamedMD->addOperand(Entry);
364}
365
370 const dxil::Resources &MDResources = MAM.getResult<DXILResourceMDAnalysis>(M);
371 const ModuleShaderFlags &ShaderFlags = MAM.getResult<ShaderFlagsAnalysis>(M);
373
374 translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI);
375
376 return PreservedAnalyses::all();
377}
378
379namespace {
380class DXILTranslateMetadataLegacy : public ModulePass {
381public:
382 static char ID; // Pass identification, replacement for typeid
383 explicit DXILTranslateMetadataLegacy() : ModulePass(ID) {}
384
385 StringRef getPassName() const override { return "DXIL Translate Metadata"; }
386
387 void getAnalysisUsage(AnalysisUsage &AU) const override {
397 }
398
399 bool runOnModule(Module &M) override {
400 DXILBindingMap &DBM =
401 getAnalysis<DXILResourceBindingWrapperPass>().getBindingMap();
402 DXILResourceTypeMap &DRTM =
403 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
404 const dxil::Resources &MDResources =
405 getAnalysis<DXILResourceMDWrapper>().getDXILResource();
406 const ModuleShaderFlags &ShaderFlags =
407 getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags();
409 getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
410
411 translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI);
412 return true;
413 }
414};
415
416} // namespace
417
418char DXILTranslateMetadataLegacy::ID = 0;
419
421 return new DXILTranslateMetadataLegacy();
422}
423
424INITIALIZE_PASS_BEGIN(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
425 "DXIL Translate Metadata", false, false)
430INITIALIZE_PASS_END(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
431 "DXIL Translate Metadata", false, false)
Add AMDGPU uniform metadata
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void emitDXILVersionTupleMD(Module &M, const ModuleMetadataInfo &MMDI)
static MDTuple * getEntryPropAsMetadata(const EntryProperties &EP, uint64_t EntryShaderFlags, const Triple::EnvironmentType ShaderProfile)
static void translateMetadata(Module &M, DXILBindingMap &DBM, DXILResourceTypeMap &DRTM, const Resources &MDResources, const ModuleShaderFlags &ShaderFlags, const ModuleMetadataInfo &MMDI)
static void emitValidatorVersionMD(Module &M, const ModuleMetadataInfo &MMDI)
static MDTuple * emitTopLevelLibraryNode(Module &M, MDNode *RMD, uint64_t ShaderFlags)
static NamedMDNode * emitResourceMetadata(Module &M, DXILBindingMap &DBM, DXILResourceTypeMap &DRTM, const dxil::Resources &MDResources)
static SmallVector< Metadata * > getTagValueAsMetadata(EntryPropsTag Tag, uint64_t Value, LLVMContext &Ctx)
static StringRef getShortShaderStage(Triple::EnvironmentType Env)
static MDTuple * emitEntryMD(const EntryProperties &EP, MDTuple *Signatures, MDNode *MDResources, const uint64_t EntryShaderFlags, const Triple::EnvironmentType ShaderProfile)
static uint32_t getShaderStage(Triple::EnvironmentType Env)
MDTuple * constructEntryMetadata(const Function *EntryFn, MDTuple *Signatures, MDNode *Resources, MDTuple *Properties, LLVMContext &Ctx)
static void emitShaderModelVersionMD(Module &M, const ModuleMetadataInfo &MMDI)
Module.h This file contains the declarations for the Module class.
This file contains the declarations for metadata subclasses.
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
ModuleAnalysisManager MAM
#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())
This file defines the SmallVector class.
static const FuncProtoTy Signatures[]
Defines the llvm::VersionTuple class, which represents a version in the form major[....
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
iterator_range< iterator > cbuffers()
Definition: DXILResource.h:476
bool empty() const
Definition: DXILResource.h:442
iterator_range< iterator > srvs()
Definition: DXILResource.h:458
iterator_range< iterator > samplers()
Definition: DXILResource.h:487
iterator_range< iterator > uavs()
Definition: DXILResource.h:467
Analysis pass that exposes the DXILResource for a module.
The legacy pass manager's analysis pass to compute DXIL resource information.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
This is the base abstract class for diagnostic reporting in the backend.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
Interface for custom diagnostic printing.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:483
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1543
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
Tuple of metadata.
Definition: Metadata.h:1473
Root of the metadata hierarchy.
Definition: Metadata.h:62
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1731
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1425
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1440
void addOperand(MDNode *M)
Definition: Metadata.cpp:1431
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
bool empty() const
Definition: SmallVector.h:81
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
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
@ Amplification
Definition: Triple.h:299
static StringRef getEnvironmentTypeName(EnvironmentType Kind)
Get the canonical name for the Kind environment.
Definition: Triple.cpp:307
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:501
LLVM Value Representation.
Definition: Value.h:74
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:71
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
Definition: VersionTuple.h:66
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:74
Metadata * writeUAVs(Module &M) const
bool hasUAVs() const
Definition: DXILResource.h:121
Metadata * writeCBuffers(Module &M) const
bool hasCBuffers() const
Definition: DXILResource.h:124
Wrapper pass for the legacy pass manager.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createDXILTranslateMetadataLegacyPass()
Pass to emit metadata for DXIL.
@ DK_Unsupported
@ Mod
The access may modify the value stored in memory.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
@ DS_Error
Triple::EnvironmentType ShaderStage
Triple::EnvironmentType ShaderProfile
SmallVector< EntryProperties > EntryPropertyVec