LLVM 22.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 "llvm/ADT/APInt.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/IR/Constants.h"
15#include "llvm/IR/Metadata.h"
16#include "llvm/IR/Module.h"
19
20#define DEBUG_TYPE "dxil-metadata-analysis"
21
22using namespace llvm;
23using namespace dxil;
24
27 const Triple &TT = M.getTargetTriple();
28 MMDAI.DXILVersion = TT.getDXILVersion();
29 MMDAI.ShaderModelVersion = TT.getOSVersion();
30 MMDAI.ShaderProfile = TT.getEnvironment();
31 NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
32 if (ValidatorVerNode) {
33 auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
34 auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
35 auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
36 MMDAI.ValidatorVersion =
37 VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
38 }
39
40 // For all HLSL Shader functions
41 for (auto &F : M.functions()) {
42 if (!F.hasFnAttribute("hlsl.shader"))
43 continue;
44
45 EntryProperties EFP(&F);
46 // Get "hlsl.shader" attribute
47 Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
48 assert(EntryAttr.isValid() &&
49 "Invalid value specified for HLSL function attribute hlsl.shader");
50 StringRef EntryProfile = EntryAttr.getValueAsString();
51 Triple T("", "", "", EntryProfile);
52 EFP.ShaderStage = T.getEnvironment();
53 // Get numthreads attribute value, if one exists
54 StringRef NumThreadsStr =
55 F.getFnAttribute("hlsl.numthreads").getValueAsString();
56 if (!NumThreadsStr.empty()) {
57 SmallVector<StringRef> NumThreadsVec;
58 NumThreadsStr.split(NumThreadsVec, ',');
59 assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified");
60 // Read in the three component values of numthreads
61 [[maybe_unused]] bool Success =
62 llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
63 assert(Success && "Failed to parse X component of numthreads");
64 Success = llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10);
65 assert(Success && "Failed to parse Y component of numthreads");
66 Success = llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10);
67 assert(Success && "Failed to parse Z component of numthreads");
68 }
69 MMDAI.EntryPropertyVec.push_back(EFP);
70 }
71 return MMDAI;
72}
73
75 OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
76 OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
77 OS << "Target Shader Stage : "
79 OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
80 for (const auto &EP : EntryPropertyVec) {
81 OS << " " << EP.Entry->getName() << "\n";
82 OS << " Function Shader Stage : "
83 << Triple::getEnvironmentTypeName(EP.ShaderStage) << "\n";
84 OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << ","
85 << EP.NumThreadsZ << "\n";
86 }
87}
88
89//===----------------------------------------------------------------------===//
90// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
91
92// Provide an explicit template instantiation for the static ID.
93AnalysisKey DXILMetadataAnalysis::Key;
94
99
107
108//===----------------------------------------------------------------------===//
109// DXILMetadataAnalysisWrapperPass
110
113
115
120
122 MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
123 return false;
124}
125
127
129 const Module *) const {
130 if (!MetadataInfo) {
131 OS << "No module metadata info has been built!\n";
132 return;
133 }
134 MetadataInfo->print(dbgs());
135}
136
137#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
140#endif
141
142INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
143 "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:638
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:55
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:69
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:223
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.
dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM)
Gather module metadata info for the module M.
ModulePass(char &pid)
Definition Pass.h:257
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A tuple of MDNodes.
Definition Metadata.h:1753
LLVM_ABI MDNode * getOperand(unsigned i) const
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:710
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
static LLVM_ABI StringRef getEnvironmentTypeName(EnvironmentType Kind)
Get the canonical name for the Kind environment.
Definition Triple.cpp:341
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:666
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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
Triple::EnvironmentType ShaderProfile
void print(raw_ostream &OS) const
SmallVector< EntryProperties > EntryPropertyVec