| 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 "llvm/ADT/APInt.h" |
| 11 | #include "llvm/ADT/StringExtras.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/IR/Constants.h" |
| 14 | #include "llvm/IR/Instructions.h" |
| 15 | #include "llvm/IR/Metadata.h" |
| 16 | #include "llvm/IR/Module.h" |
| 17 | #include "llvm/InitializePasses.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/Compression.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | |
| 22 | #define DEBUG_TYPE "dxil-metadata-analysis" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace dxil; |
| 26 | |
| 27 | cl::OptionCategory DXContainerCategory("DXContainer Options" ); |
| 28 | static cl::opt<dxbc::SourceInfo::Contents::CompressionType> CompressSRCI( |
| 29 | "compress-srci" , cl::ValueOptional, |
| 30 | cl::desc("Choose SCRI part compression:" ), |
| 31 | cl::values(clEnumValN(dxbc::SourceInfo::Contents::CompressionType::None, |
| 32 | "none" , "No compression" ), |
| 33 | clEnumValN(dxbc::SourceInfo::Contents::CompressionType::Zlib, |
| 34 | "zlib" , "Use zlib" )), |
| 35 | cl::cat(DXContainerCategory)); |
| 36 | |
| 37 | static ModuleMetadataInfo collectMetadataInfo(Module &M) { |
| 38 | ModuleMetadataInfo MMDAI; |
| 39 | const Triple &TT = M.getTargetTriple(); |
| 40 | MMDAI.DXILVersion = TT.getDXILVersion(); |
| 41 | MMDAI.ShaderModelVersion = TT.getOSVersion(); |
| 42 | MMDAI.ShaderProfile = TT.getEnvironment(); |
| 43 | NamedMDNode *ValidatorVerNode = M.getNamedMetadata(Name: "dx.valver" ); |
| 44 | if (ValidatorVerNode) { |
| 45 | auto *ValVerMD = cast<MDNode>(Val: ValidatorVerNode->getOperand(i: 0)); |
| 46 | auto *MajorMD = mdconst::extract<ConstantInt>(MD: ValVerMD->getOperand(I: 0)); |
| 47 | auto *MinorMD = mdconst::extract<ConstantInt>(MD: ValVerMD->getOperand(I: 1)); |
| 48 | MMDAI.ValidatorVersion = |
| 49 | VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue()); |
| 50 | } |
| 51 | |
| 52 | NamedMDNode *ContentsNode = M.getNamedMetadata(Name: "dx.source.contents" ); |
| 53 | NamedMDNode *ArgsNode = M.getNamedMetadata(Name: "dx.source.args" ); |
| 54 | if (ContentsNode && ArgsNode) { |
| 55 | MMDAI.SourceInfo.emplace(); |
| 56 | if (CompressSRCI.getNumOccurrences() > 0) { |
| 57 | MMDAI.SourceInfo->setCompressionType(CompressSRCI); |
| 58 | } else { |
| 59 | // If the option is not specified, pick zlib if available. |
| 60 | MMDAI.SourceInfo->setCompressionType( |
| 61 | compression::zlib::isAvailable() |
| 62 | ? dxbc::SourceInfo::Contents::CompressionType::Zlib |
| 63 | : dxbc::SourceInfo::Contents::CompressionType::None); |
| 64 | } |
| 65 | for (Metadata *FileInfoNode : ContentsNode->operands()) { |
| 66 | auto *FileInfo = cast<MDTuple>(Val: FileInfoNode); |
| 67 | MMDAI.SourceInfo->addFile( |
| 68 | Name: cast<MDString>(Val: FileInfo->getOperand(I: 0))->getString(), |
| 69 | Content: cast<MDString>(Val: FileInfo->getOperand(I: 1))->getString()); |
| 70 | } |
| 71 | for (Metadata *ArgNode : ArgsNode->getOperand(i: 0)->operands()) |
| 72 | MMDAI.SourceInfo->addArg(Name: cast<MDString>(Val: ArgNode)->getString(), Value: "" ); |
| 73 | } |
| 74 | |
| 75 | // For all HLSL Shader functions |
| 76 | for (auto &F : M.functions()) { |
| 77 | if (!F.hasFnAttribute(Kind: "hlsl.shader" )) |
| 78 | continue; |
| 79 | |
| 80 | EntryProperties EFP(&F); |
| 81 | // Get "hlsl.shader" attribute |
| 82 | Attribute EntryAttr = F.getFnAttribute(Kind: "hlsl.shader" ); |
| 83 | assert(EntryAttr.isValid() && |
| 84 | "Invalid value specified for HLSL function attribute hlsl.shader" ); |
| 85 | StringRef EntryProfile = EntryAttr.getValueAsString(); |
| 86 | Triple T("" , "" , "" , EntryProfile); |
| 87 | EFP.ShaderStage = T.getEnvironment(); |
| 88 | // Get numthreads attribute value, if one exists |
| 89 | StringRef NumThreadsStr = |
| 90 | F.getFnAttribute(Kind: "hlsl.numthreads" ).getValueAsString(); |
| 91 | if (!NumThreadsStr.empty()) { |
| 92 | SmallVector<StringRef> NumThreadsVec; |
| 93 | NumThreadsStr.split(A&: NumThreadsVec, Separator: ','); |
| 94 | assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified" ); |
| 95 | // Read in the three component values of numthreads |
| 96 | [[maybe_unused]] bool Success = |
| 97 | llvm::to_integer(S: NumThreadsVec[0], Num&: EFP.NumThreadsX, Base: 10); |
| 98 | assert(Success && "Failed to parse X component of numthreads" ); |
| 99 | Success = llvm::to_integer(S: NumThreadsVec[1], Num&: EFP.NumThreadsY, Base: 10); |
| 100 | assert(Success && "Failed to parse Y component of numthreads" ); |
| 101 | Success = llvm::to_integer(S: NumThreadsVec[2], Num&: EFP.NumThreadsZ, Base: 10); |
| 102 | assert(Success && "Failed to parse Z component of numthreads" ); |
| 103 | } |
| 104 | // Get wavesize attribute value, if one exists |
| 105 | StringRef WaveSizeStr = |
| 106 | F.getFnAttribute(Kind: "hlsl.wavesize" ).getValueAsString(); |
| 107 | if (!WaveSizeStr.empty()) { |
| 108 | SmallVector<StringRef> WaveSizeVec; |
| 109 | WaveSizeStr.split(A&: WaveSizeVec, Separator: ','); |
| 110 | assert(WaveSizeVec.size() == 3 && "Invalid wavesize specified" ); |
| 111 | // Read in the three component values of numthreads |
| 112 | [[maybe_unused]] bool Success = |
| 113 | llvm::to_integer(S: WaveSizeVec[0], Num&: EFP.WaveSizeMin, Base: 10); |
| 114 | assert(Success && "Failed to parse Min component of wavesize" ); |
| 115 | Success = llvm::to_integer(S: WaveSizeVec[1], Num&: EFP.WaveSizeMax, Base: 10); |
| 116 | assert(Success && "Failed to parse Max component of wavesize" ); |
| 117 | Success = llvm::to_integer(S: WaveSizeVec[2], Num&: EFP.WaveSizePref, Base: 10); |
| 118 | assert(Success && "Failed to parse Preferred component of wavesize" ); |
| 119 | } |
| 120 | MMDAI.EntryPropertyVec.push_back(Elt: EFP); |
| 121 | } |
| 122 | return MMDAI; |
| 123 | } |
| 124 | |
| 125 | void ModuleMetadataInfo::print(raw_ostream &OS) const { |
| 126 | OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n" ; |
| 127 | OS << "DXIL Version : " << DXILVersion.getAsString() << "\n" ; |
| 128 | OS << "Target Shader Stage : " |
| 129 | << Triple::getEnvironmentTypeName(Kind: ShaderProfile) << "\n" ; |
| 130 | OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n" ; |
| 131 | for (const auto &EP : EntryPropertyVec) { |
| 132 | OS << " " << EP.Entry->getName() << "\n" ; |
| 133 | OS << " Function Shader Stage : " |
| 134 | << Triple::getEnvironmentTypeName(Kind: EP.ShaderStage) << "\n" ; |
| 135 | OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << "," |
| 136 | << EP.NumThreadsZ << "\n" ; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | // DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass |
| 142 | |
| 143 | // Provide an explicit template instantiation for the static ID. |
| 144 | AnalysisKey DXILMetadataAnalysis::Key; |
| 145 | |
| 146 | llvm::dxil::ModuleMetadataInfo |
| 147 | DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) { |
| 148 | return collectMetadataInfo(M); |
| 149 | } |
| 150 | |
| 151 | PreservedAnalyses |
| 152 | DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 153 | llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(IR&: M); |
| 154 | |
| 155 | Data.print(OS); |
| 156 | return PreservedAnalyses::all(); |
| 157 | } |
| 158 | |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | // DXILMetadataAnalysisWrapperPass |
| 161 | |
| 162 | DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass() |
| 163 | : ModulePass(ID) {} |
| 164 | |
| 165 | DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default; |
| 166 | |
| 167 | void DXILMetadataAnalysisWrapperPass::getAnalysisUsage( |
| 168 | AnalysisUsage &AU) const { |
| 169 | AU.setPreservesAll(); |
| 170 | } |
| 171 | |
| 172 | bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) { |
| 173 | MetadataInfo.reset(p: new ModuleMetadataInfo(collectMetadataInfo(M))); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | void DXILMetadataAnalysisWrapperPass::releaseMemory() { MetadataInfo.reset(); } |
| 178 | |
| 179 | void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS, |
| 180 | const Module *) const { |
| 181 | if (!MetadataInfo) { |
| 182 | OS << "No module metadata info has been built!\n" ; |
| 183 | return; |
| 184 | } |
| 185 | MetadataInfo->print(OS&: dbgs()); |
| 186 | } |
| 187 | |
| 188 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 189 | LLVM_DUMP_METHOD |
| 190 | void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); } |
| 191 | #endif |
| 192 | |
| 193 | INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis" , |
| 194 | "DXIL Module Metadata analysis" , false, true) |
| 195 | char DXILMetadataAnalysisWrapperPass::ID = 0; |
| 196 | |