LLVM 20.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 Triple TT(Triple(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
97 return collectMetadataInfo(M);
98}
99
103
104 Data.print(OS);
105 return PreservedAnalyses::all();
106}
107
108//===----------------------------------------------------------------------===//
109// DXILMetadataAnalysisWrapperPass
110
112 : ModulePass(ID) {
115}
116
118
120 AnalysisUsage &AU) const {
121 AU.setPreservesAll();
122}
123
125 MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
126 return false;
127}
128
130
132 const Module *) const {
133 if (!MetadataInfo) {
134 OS << "No module metadata info has been built!\n";
135 return;
136 }
137 MetadataInfo->print(dbgs());
138}
139
140#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
143#endif
144
145INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
146 "DXIL Module Metadata analysis", false, true)
#define Success
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:622
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 INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
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.
void setPreservesAll()
Set by analyses that do not transform their input at all.
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:392
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
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 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
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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
size_t size() const
Definition: SmallVector.h:78
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
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:700
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static StringRef getEnvironmentTypeName(EnvironmentType Kind)
Get the canonical name for the Kind environment.
Definition: Triple.cpp:307
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
std::string getAsString() const
Retrieve a string representation of the version number.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
Triple::EnvironmentType ShaderStage
Triple::EnvironmentType ShaderProfile
void print(raw_ostream &OS) const
SmallVector< EntryProperties > EntryPropertyVec