| 1 | //===- DXILRootSignature.cpp - DXIL Root Signature helper objects -------===// |
| 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 | /// \file This file contains helper objects and APIs for working with DXIL |
| 10 | /// Root Signatures. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "DXILRootSignature.h" |
| 14 | #include "DirectX.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/Analysis/DXILMetadataAnalysis.h" |
| 17 | #include "llvm/BinaryFormat/DXContainer.h" |
| 18 | #include "llvm/Frontend/HLSL/RootSignatureMetadata.h" |
| 19 | #include "llvm/Frontend/HLSL/RootSignatureValidations.h" |
| 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/DiagnosticInfo.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Metadata.h" |
| 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/MC/DXContainerRootSignature.h" |
| 27 | #include "llvm/Pass.h" |
| 28 | #include "llvm/Support/Error.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/ScopedPrinter.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include <cstdint> |
| 33 | |
| 34 | using namespace llvm; |
| 35 | using namespace llvm::dxil; |
| 36 | |
| 37 | static std::optional<uint32_t> (MDNode *Node, |
| 38 | unsigned int OpId) { |
| 39 | if (auto *CI = |
| 40 | mdconst::dyn_extract<ConstantInt>(MD: Node->getOperand(I: OpId).get())) |
| 41 | return CI->getZExtValue(); |
| 42 | return std::nullopt; |
| 43 | } |
| 44 | |
| 45 | static bool reportError(LLVMContext *Ctx, Twine Message, |
| 46 | DiagnosticSeverity Severity = DS_Error) { |
| 47 | Ctx->diagnose(DI: DiagnosticInfoGeneric(Message, Severity)); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | static SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> |
| 52 | analyzeModule(Module &M) { |
| 53 | |
| 54 | /** Root Signature are specified as following in the metadata: |
| 55 | |
| 56 | !dx.rootsignatures = !{!2} ; list of function/root signature pairs |
| 57 | !2 = !{ ptr @main, !3 } ; function, root signature |
| 58 | !3 = !{ !4, !5, !6, !7 } ; list of root signature elements |
| 59 | |
| 60 | So for each MDNode inside dx.rootsignatures NamedMDNode |
| 61 | (the Root parameter of this function), the parsing process needs |
| 62 | to loop through each of its operands and process the function, |
| 63 | signature pair. |
| 64 | */ |
| 65 | |
| 66 | LLVMContext *Ctx = &M.getContext(); |
| 67 | |
| 68 | SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> RSDMap; |
| 69 | |
| 70 | NamedMDNode *RootSignatureNode = M.getNamedMetadata(Name: "dx.rootsignatures" ); |
| 71 | if (RootSignatureNode == nullptr) |
| 72 | return RSDMap; |
| 73 | |
| 74 | bool AllowNullFunctions = false; |
| 75 | if (M.getTargetTriple().getEnvironment() == |
| 76 | Triple::EnvironmentType::RootSignature) { |
| 77 | assert(RootSignatureNode->getNumOperands() == 1); |
| 78 | AllowNullFunctions = true; |
| 79 | } |
| 80 | |
| 81 | for (const auto &RSDefNode : RootSignatureNode->operands()) { |
| 82 | if (RSDefNode->getNumOperands() != 3) { |
| 83 | reportError(Ctx, Message: "Invalid Root Signature metadata - expected function, " |
| 84 | "signature, and version." ); |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Function was pruned during compilation. |
| 89 | Function *F = nullptr; |
| 90 | |
| 91 | if (!AllowNullFunctions) { |
| 92 | const MDOperand &FunctionPointerMdNode = RSDefNode->getOperand(I: 0); |
| 93 | if (FunctionPointerMdNode == nullptr) { |
| 94 | reportError( |
| 95 | Ctx, Message: "Function associated with Root Signature definition is null." ); |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | ValueAsMetadata *VAM = |
| 100 | llvm::dyn_cast<ValueAsMetadata>(Val: FunctionPointerMdNode.get()); |
| 101 | if (VAM == nullptr) { |
| 102 | reportError(Ctx, Message: "First element of root signature is not a Value" ); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | F = dyn_cast<Function>(Val: VAM->getValue()); |
| 107 | if (F == nullptr) { |
| 108 | reportError(Ctx, Message: "First element of root signature is not a Function" ); |
| 109 | continue; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Metadata *RootElementListOperand = RSDefNode->getOperand(I: 1).get(); |
| 114 | |
| 115 | if (RootElementListOperand == nullptr) { |
| 116 | reportError(Ctx, Message: "Root Element mdnode is null." ); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | MDNode *RootElementListNode = dyn_cast<MDNode>(Val: RootElementListOperand); |
| 121 | if (RootElementListNode == nullptr) { |
| 122 | reportError(Ctx, Message: "Root Element is not a metadata node." ); |
| 123 | continue; |
| 124 | } |
| 125 | std::optional<uint32_t> V = extractMdIntValue(Node: RSDefNode, OpId: 2); |
| 126 | if (!V.has_value()) { |
| 127 | reportError(Ctx, Message: "Invalid RSDefNode value, expected constant int" ); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if (!hlsl::rootsig::verifyVersion(Version: *V)) { |
| 132 | reportError(Ctx, Message: "Invalid Root Signature Version: " + Twine(*V)); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | llvm::hlsl::rootsig::MetadataParser MDParser(RootElementListNode); |
| 137 | llvm::Expected<mcdxbc::RootSignatureDesc> RSDOrErr = |
| 138 | MDParser.ParseRootSignature(Version: V.value()); |
| 139 | |
| 140 | if (!RSDOrErr) { |
| 141 | handleAllErrors(E: RSDOrErr.takeError(), Handlers: [&](ErrorInfoBase &EIB) { |
| 142 | Ctx->emitError(ErrorStr: EIB.message()); |
| 143 | }); |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | auto &RSD = *RSDOrErr; |
| 148 | |
| 149 | // Clang emits the root signature data in dxcontainer following a specific |
| 150 | // sequence. First the header, then the root parameters. So the header |
| 151 | // offset will always equal to the header size. |
| 152 | RSD.RootParameterOffset = sizeof(dxbc::RTS0::v1::RootSignatureHeader); |
| 153 | |
| 154 | // static sampler offset is calculated when writting dxcontainer. |
| 155 | RSD.StaticSamplersOffset = 0u; |
| 156 | |
| 157 | RSDMap.insert(KV: std::make_pair(x&: F, y&: RSD)); |
| 158 | } |
| 159 | |
| 160 | return RSDMap; |
| 161 | } |
| 162 | |
| 163 | AnalysisKey RootSignatureAnalysis::Key; |
| 164 | |
| 165 | RootSignatureAnalysis::Result |
| 166 | RootSignatureAnalysis::run(Module &M, ModuleAnalysisManager &AM) { |
| 167 | return RootSignatureBindingInfo(analyzeModule(M)); |
| 168 | } |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
| 172 | PreservedAnalyses |
| 173 | RootSignatureAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 174 | |
| 175 | RootSignatureBindingInfo &RSDMap = AM.getResult<RootSignatureAnalysis>(IR&: M); |
| 176 | if (RSDMap.empty()) |
| 177 | return PreservedAnalyses::all(); |
| 178 | |
| 179 | OS << "Root Signature Definitions" |
| 180 | << "\n" ; |
| 181 | for (const Function &F : M) { |
| 182 | auto It = RSDMap.find(F: &F); |
| 183 | if (It == RSDMap.end()) |
| 184 | continue; |
| 185 | const auto &RS = It->second; |
| 186 | OS << "Definition for '" << F.getName() << "':\n" ; |
| 187 | // start root signature header |
| 188 | OS << "Flags: " << format_hex(N: RS.Flags, Width: 8) << "\n" |
| 189 | << "Version: " << RS.Version << "\n" |
| 190 | << "RootParametersOffset: " << RS.RootParameterOffset << "\n" |
| 191 | << "NumParameters: " << RS.ParametersContainer.size() << "\n" ; |
| 192 | for (size_t I = 0; I < RS.ParametersContainer.size(); I++) { |
| 193 | const mcdxbc::RootParameterInfo &Info = RS.ParametersContainer.getInfo(Location: I); |
| 194 | |
| 195 | OS << "- Parameter Type: " |
| 196 | << dxbc::getRootParameterTypes().toString(Value: Info.Type) << "\n" |
| 197 | << " Shader Visibility: " |
| 198 | << dxbc::getShaderVisibility().toString(Value: Info.Visibility) << "\n" ; |
| 199 | switch (Info.Type) { |
| 200 | case dxbc::RootParameterType::Constants32Bit: { |
| 201 | const mcdxbc::RootConstants &Constants = |
| 202 | RS.ParametersContainer.getConstant(Index: Info.Location); |
| 203 | OS << " Register Space: " << Constants.RegisterSpace << "\n" |
| 204 | << " Shader Register: " << Constants.ShaderRegister << "\n" |
| 205 | << " Num 32 Bit Values: " << Constants.Num32BitValues << "\n" ; |
| 206 | break; |
| 207 | } |
| 208 | case dxbc::RootParameterType::CBV: |
| 209 | case dxbc::RootParameterType::UAV: |
| 210 | case dxbc::RootParameterType::SRV: { |
| 211 | const mcdxbc::RootDescriptor &Descriptor = |
| 212 | RS.ParametersContainer.getRootDescriptor(Index: Info.Location); |
| 213 | OS << " Register Space: " << Descriptor.RegisterSpace << "\n" |
| 214 | << " Shader Register: " << Descriptor.ShaderRegister << "\n" ; |
| 215 | if (RS.Version > 1) |
| 216 | OS << " Flags: " << Descriptor.Flags << "\n" ; |
| 217 | break; |
| 218 | } |
| 219 | case dxbc::RootParameterType::DescriptorTable: { |
| 220 | const mcdxbc::DescriptorTable &Table = |
| 221 | RS.ParametersContainer.getDescriptorTable(Index: Info.Location); |
| 222 | OS << " NumRanges: " << Table.Ranges.size() << "\n" ; |
| 223 | |
| 224 | for (const mcdxbc::DescriptorRange &Range : Table) { |
| 225 | OS << " - Range Type: " |
| 226 | << dxil::getResourceClassName(RC: Range.RangeType) << "\n" |
| 227 | << " Register Space: " << Range.RegisterSpace << "\n" |
| 228 | << " Base Shader Register: " << Range.BaseShaderRegister << "\n" |
| 229 | << " Num Descriptors: " << Range.NumDescriptors << "\n" |
| 230 | << " Offset In Descriptors From Table Start: " |
| 231 | << Range.OffsetInDescriptorsFromTableStart << "\n" ; |
| 232 | if (RS.Version > 1) |
| 233 | OS << " Flags: " << Range.Flags << "\n" ; |
| 234 | } |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | OS << "NumStaticSamplers: " << 0 << "\n" ; |
| 240 | OS << "StaticSamplersOffset: " << RS.StaticSamplersOffset << "\n" ; |
| 241 | } |
| 242 | return PreservedAnalyses::all(); |
| 243 | } |
| 244 | |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | bool RootSignatureAnalysisWrapper::runOnModule(Module &M) { |
| 247 | FuncToRsMap = std::make_unique<RootSignatureBindingInfo>( |
| 248 | args: RootSignatureBindingInfo(analyzeModule(M))); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const { |
| 253 | AU.setPreservesAll(); |
| 254 | AU.addPreserved<DXILMetadataAnalysisWrapperPass>(); |
| 255 | } |
| 256 | |
| 257 | char RootSignatureAnalysisWrapper::ID = 0; |
| 258 | |
| 259 | INITIALIZE_PASS_BEGIN(RootSignatureAnalysisWrapper, |
| 260 | "dxil-root-signature-analysis" , |
| 261 | "DXIL Root Signature Analysis" , true, true) |
| 262 | INITIALIZE_PASS_END(RootSignatureAnalysisWrapper, |
| 263 | "dxil-root-signature-analysis" , |
| 264 | "DXIL Root Signature Analysis" , true, true) |
| 265 | |