LLVM 24.0.0git
DXILMetadataAnalysis.cpp
Go to the documentation of this file.
1//=- DXILMetadataAnalysis.cpp - Representation of Module metadata -*- C++ -*=//
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 "AnalysisOptions.h"
11#include "llvm/ADT/APInt.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/IR/Constants.h"
16#include "llvm/IR/Metadata.h"
17#include "llvm/IR/Module.h"
21
22#define DEBUG_TYPE "dxil-metadata-analysis"
23
24using namespace llvm;
25using namespace dxil;
26
29 const Triple &TT = M.getTargetTriple();
30 MMDAI.DXILVersion = TT.getDXILVersion();
31 MMDAI.ShaderModelVersion = TT.getOSVersion();
32 MMDAI.ShaderProfile = TT.getEnvironment();
33 NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
34 if (ValidatorVerNode) {
35 auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
36 auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
37 auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
38 MMDAI.ValidatorVersion =
39 VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
40 }
41
42 NamedMDNode *ContentsNode = M.getNamedMetadata("dx.source.contents");
43 NamedMDNode *ArgsNode = M.getNamedMetadata("dx.source.args");
44 if (ContentsNode && ArgsNode) {
45 MMDAI.SourceInfo.emplace();
46 if (auto Type = getAnalysisOptions(M).compress_srci) {
47 MMDAI.SourceInfo->setCompressionType(*Type);
48 } else {
49 // If the option is not specified, pick zlib if available.
50 MMDAI.SourceInfo->setCompressionType(
52 ? dxbc::SourceInfo::Contents::CompressionType::Zlib
53 : dxbc::SourceInfo::Contents::CompressionType::None);
54 }
55 for (Metadata *FileInfoNode : ContentsNode->operands()) {
56 auto *FileInfo = cast<MDTuple>(FileInfoNode);
57 MMDAI.SourceInfo->addFile(
58 cast<MDString>(FileInfo->getOperand(0))->getString(),
59 cast<MDString>(FileInfo->getOperand(1))->getString());
60 }
61 for (Metadata *ArgNode : ArgsNode->getOperand(0)->operands())
62 MMDAI.SourceInfo->addArg(cast<MDString>(ArgNode)->getString(), "");
63 }
64
65 // For all HLSL Shader functions
66 for (auto &F : M.functions()) {
67 if (!F.hasFnAttribute("hlsl.shader"))
68 continue;
69
70 EntryProperties EFP(&F);
71 // Get "hlsl.shader" attribute
72 Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
73 assert(EntryAttr.isValid() &&
74 "Invalid value specified for HLSL function attribute hlsl.shader");
75 StringRef EntryProfile = EntryAttr.getValueAsString();
76 Triple T("", "", "", EntryProfile);
77 EFP.ShaderStage = T.getEnvironment();
78 // Get numthreads attribute value, if one exists
79 StringRef NumThreadsStr =
80 F.getFnAttribute("hlsl.numthreads").getValueAsString();
81 if (!NumThreadsStr.empty()) {
82 SmallVector<StringRef> NumThreadsVec;
83 NumThreadsStr.split(NumThreadsVec, ',');
84 assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified");
85 // Read in the three component values of numthreads
86 [[maybe_unused]] bool Success =
87 llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
88 assert(Success && "Failed to parse X component of numthreads");
89 Success = llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10);
90 assert(Success && "Failed to parse Y component of numthreads");
91 Success = llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10);
92 assert(Success && "Failed to parse Z component of numthreads");
93 }
94 // Get wavesize attribute value, if one exists
95 StringRef WaveSizeStr =
96 F.getFnAttribute("hlsl.wavesize").getValueAsString();
97 if (!WaveSizeStr.empty()) {
98 SmallVector<StringRef> WaveSizeVec;
99 WaveSizeStr.split(WaveSizeVec, ',');
100 assert(WaveSizeVec.size() == 3 && "Invalid wavesize specified");
101 // Read in the three component values of numthreads
102 [[maybe_unused]] bool Success =
103 llvm::to_integer(WaveSizeVec[0], EFP.WaveSizeMin, 10);
104 assert(Success && "Failed to parse Min component of wavesize");
105 Success = llvm::to_integer(WaveSizeVec[1], EFP.WaveSizeMax, 10);
106 assert(Success && "Failed to parse Max component of wavesize");
107 Success = llvm::to_integer(WaveSizeVec[2], EFP.WaveSizePref, 10);
108 assert(Success && "Failed to parse Preferred component of wavesize");
109 }
110 MMDAI.EntryPropertyVec.push_back(EFP);
111 }
112 return MMDAI;
113}
114
116 OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
117 OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
118 OS << "Target Shader Stage : "
120 OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
121 for (const auto &EP : EntryPropertyVec) {
122 OS << " " << EP.Entry->getName() << "\n";
123 OS << " Function Shader Stage : "
124 << Triple::getEnvironmentTypeName(EP.ShaderStage) << "\n";
125 OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << ","
126 << EP.NumThreadsZ << "\n";
127 }
128}
129
130//===----------------------------------------------------------------------===//
131// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
132
133// Provide an explicit template instantiation for the static ID.
134AnalysisKey DXILMetadataAnalysis::Key;
135
140
148
149//===----------------------------------------------------------------------===//
150// DXILMetadataAnalysisWrapperPass
151
154
156
161
163 MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
164 return false;
165}
166
168
170 const Module *) const {
171 if (!MetadataInfo) {
172 OS << "No module metadata info has been built!\n";
173 return;
174 }
175 MetadataInfo->print(dbgs());
176}
177
178#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
181#endif
182
183INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
184 "DXIL Module Metadata analysis", false, true)
#define Success
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:690
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static ModuleMetadataInfo collectMetadataInfo(Module &M)
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
This file contains the declarations for metadata subclasses.
#define T
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some functions that are useful when dealing with strings.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:262
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
LLVM_ABI dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM)
Gather module metadata info for the module M.
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1435
Root of the metadata hierarchy.
Definition Metadata.h:64
ModulePass(char &pid)
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A tuple of MDNodes.
Definition Metadata.h:1797
LLVM_ABI MDNode * getOperand(unsigned i) const
iterator_range< op_iterator > operands()
Definition Metadata.h:1893
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:736
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
static LLVM_ABI StringRef getEnvironmentTypeName(EnvironmentType Kind)
Get the canonical name for the Kind environment.
Definition Triple.cpp:405
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Represents a version number in the form major[.minor[.subminor[.build]]].
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI bool isAvailable()
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:679
This is an optimization pass for GlobalISel generic memory operations.
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
Triple::EnvironmentType ShaderStage
std::optional< mcdxbc::SourceInfoBuilder > SourceInfo
Triple::EnvironmentType ShaderProfile
LLVM_ABI void print(raw_ostream &OS) const
SmallVector< EntryProperties > EntryPropertyVec