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
9#include "llvm/Analysis/DXILMetadataAnalysis.h"
10#include "AnalysisOptions.h"
11#include "llvm/ADT/APInt.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/Instructions.h"
16#include "llvm/IR/Metadata.h"
17#include "llvm/IR/Module.h"
18#include "llvm/InitializePasses.h"
19#include "llvm/Support/Compression.h"
20#include "llvm/Support/ErrorHandling.h"
21
22#define DEBUG_TYPE "dxil-metadata-analysis"
23
24using namespace llvm;
25using namespace dxil;
26
27static ModuleMetadataInfo collectMetadataInfo(Module &M) {
28 ModuleMetadataInfo MMDAI;
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(Name: "dx.valver");
34 if (ValidatorVerNode) {
35 auto *ValVerMD = cast<MDNode>(Val: ValidatorVerNode->getOperand(i: 0));
36 auto *MajorMD = mdconst::extract<ConstantInt>(MD: ValVerMD->getOperand(I: 0));
37 auto *MinorMD = mdconst::extract<ConstantInt>(MD: ValVerMD->getOperand(I: 1));
38 MMDAI.ValidatorVersion =
39 VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
40 }
41
42 NamedMDNode *ContentsNode = M.getNamedMetadata(Name: "dx.source.contents");
43 NamedMDNode *ArgsNode = M.getNamedMetadata(Name: "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(
51 compression::zlib::isAvailable()
52 ? dxbc::SourceInfo::Contents::CompressionType::Zlib
53 : dxbc::SourceInfo::Contents::CompressionType::None);
54 }
55 for (Metadata *FileInfoNode : ContentsNode->operands()) {
56 auto *FileInfo = cast<MDTuple>(Val: FileInfoNode);
57 MMDAI.SourceInfo->addFile(
58 Name: cast<MDString>(Val: FileInfo->getOperand(I: 0))->getString(),
59 Content: cast<MDString>(Val: FileInfo->getOperand(I: 1))->getString());
60 }
61 for (Metadata *ArgNode : ArgsNode->getOperand(i: 0)->operands())
62 MMDAI.SourceInfo->addArg(Name: cast<MDString>(Val: ArgNode)->getString(), Value: "");
63 }
64
65 // For all HLSL Shader functions
66 for (auto &F : M.functions()) {
67 if (!F.hasFnAttribute(Kind: "hlsl.shader"))
68 continue;
69
70 EntryProperties EFP(&F);
71 // Get "hlsl.shader" attribute
72 Attribute EntryAttr = F.getFnAttribute(Kind: "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(Kind: "hlsl.numthreads").getValueAsString();
81 if (!NumThreadsStr.empty()) {
82 SmallVector<StringRef> NumThreadsVec;
83 NumThreadsStr.split(A&: NumThreadsVec, Separator: ',');
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(S: NumThreadsVec[0], Num&: EFP.NumThreadsX, Base: 10);
88 assert(Success && "Failed to parse X component of numthreads");
89 Success = llvm::to_integer(S: NumThreadsVec[1], Num&: EFP.NumThreadsY, Base: 10);
90 assert(Success && "Failed to parse Y component of numthreads");
91 Success = llvm::to_integer(S: NumThreadsVec[2], Num&: EFP.NumThreadsZ, Base: 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(Kind: "hlsl.wavesize").getValueAsString();
97 if (!WaveSizeStr.empty()) {
98 SmallVector<StringRef> WaveSizeVec;
99 WaveSizeStr.split(A&: WaveSizeVec, Separator: ',');
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(S: WaveSizeVec[0], Num&: EFP.WaveSizeMin, Base: 10);
104 assert(Success && "Failed to parse Min component of wavesize");
105 Success = llvm::to_integer(S: WaveSizeVec[1], Num&: EFP.WaveSizeMax, Base: 10);
106 assert(Success && "Failed to parse Max component of wavesize");
107 Success = llvm::to_integer(S: WaveSizeVec[2], Num&: EFP.WaveSizePref, Base: 10);
108 assert(Success && "Failed to parse Preferred component of wavesize");
109 }
110 MMDAI.EntryPropertyVec.push_back(Elt: EFP);
111 }
112 return MMDAI;
113}
114
115void ModuleMetadataInfo::print(raw_ostream &OS) const {
116 OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
117 OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
118 OS << "Target Shader Stage : "
119 << Triple::getEnvironmentTypeName(Kind: ShaderProfile) << "\n";
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(Kind: 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
136llvm::dxil::ModuleMetadataInfo
137DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
138 return collectMetadataInfo(M);
139}
140
141PreservedAnalyses
142DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
143 llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(IR&: M);
144
145 Data.print(OS);
146 return PreservedAnalyses::all();
147}
148
149//===----------------------------------------------------------------------===//
150// DXILMetadataAnalysisWrapperPass
151
152DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
153 : ModulePass(ID) {}
154
155DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;
156
157void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
158 AnalysisUsage &AU) const {
159 AU.setPreservesAll();
160}
161
162bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
163 MetadataInfo.reset(p: new ModuleMetadataInfo(collectMetadataInfo(M)));
164 return false;
165}
166
167void DXILMetadataAnalysisWrapperPass::releaseMemory() { MetadataInfo.reset(); }
168
169void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
170 const Module *) const {
171 if (!MetadataInfo) {
172 OS << "No module metadata info has been built!\n";
173 return;
174 }
175 MetadataInfo->print(OS&: dbgs());
176}
177
178#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
179LLVM_DUMP_METHOD
180void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
181#endif
182
183INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
184 "DXIL Module Metadata analysis", false, true)
185char DXILMetadataAnalysisWrapperPass::ID = 0;
186