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"
15#include "llvm/IR/Constants.h"
17#include "llvm/IR/Metadata.h"
18#include "llvm/IR/Module.h"
22
23#define DEBUG_TYPE "dxil-metadata-analysis"
24
25using namespace llvm;
26using namespace dxil;
27
30 const Triple &TT = M.getTargetTriple();
31 MMDAI.DXILVersion = TT.getDXILVersion();
32 MMDAI.ShaderModelVersion = TT.getOSVersion();
33 MMDAI.ShaderProfile = TT.getEnvironment();
34 NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
35 if (ValidatorVerNode) {
36 auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
37 auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
38 auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
39 MMDAI.ValidatorVersion =
40 VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
41 }
42
43 NamedMDNode *ContentsNode = M.getNamedMetadata("dx.source.contents");
44 NamedMDNode *ArgsNode = M.getNamedMetadata("dx.source.args");
45 if (ContentsNode && ArgsNode) {
46 MMDAI.SourceInfo.emplace();
47 if (auto Type = getAnalysisOptions(M).compress_srci) {
48 MMDAI.SourceInfo->setCompressionType(*Type);
49 } else {
50 // If the option is not specified, pick zlib if available.
51 MMDAI.SourceInfo->setCompressionType(
53 ? dxbc::SourceInfo::Contents::CompressionType::Zlib
54 : dxbc::SourceInfo::Contents::CompressionType::None);
55 }
56 for (Metadata *FileInfoNode : ContentsNode->operands()) {
57 auto *FileInfo = cast<MDTuple>(FileInfoNode);
58 MMDAI.SourceInfo->addFile(
59 cast<MDString>(FileInfo->getOperand(0))->getString(),
60 cast<MDString>(FileInfo->getOperand(1))->getString());
61 }
62 for (Metadata *ArgNode : ArgsNode->getOperand(0)->operands())
63 MMDAI.SourceInfo->addArg(cast<MDString>(ArgNode)->getString(), "");
64 }
65
66 // For all HLSL Shader functions
67 for (auto &F : M.functions()) {
68 if (!F.hasFnAttribute("hlsl.shader"))
69 continue;
70
71 EntryProperties EFP(&F);
72 // Get "hlsl.shader" attribute
73 Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
74 assert(EntryAttr.isValid() &&
75 "Invalid value specified for HLSL function attribute hlsl.shader");
76 StringRef EntryProfile = EntryAttr.getValueAsString();
77 Triple T("", "", "", EntryProfile);
78 EFP.ShaderStage = T.getEnvironment();
79
80 // Get numthreads attribute value, if one exists
81 StringRef NumThreadsStr =
82 F.getFnAttribute("hlsl.numthreads").getValueAsString();
83 if (!NumThreadsStr.empty())
84 hlsl::parseThreadCounts(NumThreadsStr, EFP.NumThreadsX, EFP.NumThreadsY,
85 EFP.NumThreadsZ);
86
87 // Get wavesize attribute value, if one exists
88 StringRef WaveSizeStr =
89 F.getFnAttribute("hlsl.wavesize").getValueAsString();
90 if (!WaveSizeStr.empty())
92 EFP.WaveSizePref);
93
94 MMDAI.EntryPropertyVec.push_back(EFP);
95 }
96 return MMDAI;
97}
98
100 OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
101 OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
102 OS << "Target Shader Stage : "
104 OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
105 for (const auto &EP : EntryPropertyVec) {
106 OS << " " << EP.Entry->getName() << "\n";
107 OS << " Function Shader Stage : "
108 << Triple::getEnvironmentTypeName(EP.ShaderStage) << "\n";
109 OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << ","
110 << EP.NumThreadsZ << "\n";
111 }
112}
113
114//===----------------------------------------------------------------------===//
115// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
116
117// Provide an explicit template instantiation for the static ID.
118AnalysisKey DXILMetadataAnalysis::Key;
119
124
132
133//===----------------------------------------------------------------------===//
134// DXILMetadataAnalysisWrapperPass
135
138
140
145
147 MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
148 return false;
149}
150
152
154 const Module *) const {
155 if (!MetadataInfo) {
156 OS << "No module metadata info has been built!\n";
157 return;
158 }
159 MetadataInfo->print(dbgs());
160}
161
162#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
165#endif
166
167INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
168 "DXIL Module Metadata analysis", false, true)
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:702
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:107
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:263
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:1441
Root of the metadata hierarchy.
Definition Metadata.h:65
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:1806
LLVM_ABI MDNode * getOperand(unsigned i) const
iterator_range< op_iterator > operands()
Definition Metadata.h:1902
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:410
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()
LLVM_ABI void parseThreadCounts(StringRef Str, unsigned &Count1, unsigned &Count2, unsigned &Count3)
Definition HLSLUtils.cpp:23
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:680
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
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