| 1 | //===- DXILContainerGlobals.cpp - DXContainer global generator pass -------===// |
| 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 | // DXILContainerGlobalsPass implementation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "DXILRootSignature.h" |
| 14 | #include "DXILShaderFlags.h" |
| 15 | #include "DXILSignatureAnalysis.h" |
| 16 | #include "DXILWriter/DXILWriterPass.h" |
| 17 | #include "DirectX.h" |
| 18 | #include "MCTargetDesc/DirectXContainerObjectWriter.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/Analysis/DXILMetadataAnalysis.h" |
| 23 | #include "llvm/Analysis/DXILResource.h" |
| 24 | #include "llvm/BinaryFormat/DXContainer.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
| 26 | #include "llvm/IR/Analysis.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/Module.h" |
| 29 | #include "llvm/IR/PassManager.h" |
| 30 | #include "llvm/InitializePasses.h" |
| 31 | #include "llvm/MC/DXContainerInfo.h" |
| 32 | #include "llvm/MC/DXContainerPSVInfo.h" |
| 33 | #include "llvm/MC/MCDXContainerWriter.h" |
| 34 | #include "llvm/Pass.h" |
| 35 | #include "llvm/Support/CommandLine.h" |
| 36 | #include "llvm/Support/MD5.h" |
| 37 | #include "llvm/Support/Path.h" |
| 38 | #include "llvm/TargetParser/Triple.h" |
| 39 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 40 | #include <cstdint> |
| 41 | |
| 42 | using namespace llvm; |
| 43 | using namespace llvm::dxil; |
| 44 | using namespace llvm::mcdxbc; |
| 45 | |
| 46 | static cl::opt<bool> ShaderHashDependsOnSource( |
| 47 | "dx-Zss" , cl::desc("Compute Shader Hash considering source information" )); |
| 48 | |
| 49 | cl::opt<bool> dxil::PdbInPrivate("dx-pdb-in-private" , |
| 50 | cl::desc("Store PDB in private user data" )); |
| 51 | |
| 52 | namespace { |
| 53 | class DXILContainerGlobalsLegacy : public ModulePass { |
| 54 | public: |
| 55 | static char ID; // Pass identification, replacement for typeid |
| 56 | DXILContainerGlobalsLegacy() : ModulePass(ID) {} |
| 57 | |
| 58 | StringRef getPassName() const override { |
| 59 | return "DXContainer Global Emitter" ; |
| 60 | } |
| 61 | |
| 62 | bool runOnModule(Module &M) override; |
| 63 | |
| 64 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 65 | AU.setPreservesAll(); |
| 66 | AU.addRequired<ShaderFlagsAnalysisWrapper>(); |
| 67 | AU.addRequired<RootSignatureAnalysisWrapper>(); |
| 68 | AU.addRequired<SignatureAnalysisWrapper>(); |
| 69 | AU.addRequired<DXILMetadataAnalysisWrapperPass>(); |
| 70 | AU.addRequired<DXILResourceTypeWrapperPass>(); |
| 71 | AU.addRequired<DXILResourceWrapperPass>(); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | static GlobalVariable *buildContainerGlobal(Module &M, Constant *Content, |
| 78 | StringRef Name, |
| 79 | StringRef SectionName) { |
| 80 | auto *GV = new llvm::GlobalVariable( |
| 81 | M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name); |
| 82 | GV->setSection(SectionName); |
| 83 | GV->setAlignment(Align(4)); |
| 84 | return GV; |
| 85 | } |
| 86 | |
| 87 | static GlobalVariable * |
| 88 | getFeatureFlags(Module &M, |
| 89 | function_ref<const ModuleShaderFlags &()> GetShaderFlags) { |
| 90 | uint64_t CombinedFeatureFlags = |
| 91 | GetShaderFlags().getCombinedFlags().getFeatureFlags(); |
| 92 | |
| 93 | Constant *FeatureFlagsConstant = |
| 94 | ConstantInt::get(Context&: M.getContext(), V: APInt(64, CombinedFeatureFlags)); |
| 95 | return buildContainerGlobal(M, Content: FeatureFlagsConstant, Name: "dx.sfi0" , SectionName: "SFI0" ); |
| 96 | } |
| 97 | |
| 98 | static void addSection(Module &M, SmallVector<GlobalValue *> &Globals, |
| 99 | StringRef SectionData, StringRef MetadataName, |
| 100 | StringRef SectionName) { |
| 101 | Constant *SectionConstant = ConstantDataArray::getString( |
| 102 | Context&: M.getContext(), Initializer: SectionData, /*AddNull*/ false); |
| 103 | Globals.emplace_back( |
| 104 | Args: buildContainerGlobal(M, Content: SectionConstant, Name: MetadataName, SectionName)); |
| 105 | } |
| 106 | |
| 107 | static void computeShaderHashAndDebugName(Module &M, |
| 108 | SmallVector<GlobalValue *> &Globals) { |
| 109 | ConstantDataArray *DXILConstant; |
| 110 | MD5 Digest; |
| 111 | dxbc::ShaderHash HashData = {.Flags: 0, .Digest: {0}}; |
| 112 | |
| 113 | if (ShaderHashDependsOnSource) { |
| 114 | if (auto *ILDB = M.getNamedGlobal(Name: "dx.ildb" )) { |
| 115 | DXILConstant = cast<ConstantDataArray>(Val: ILDB->getInitializer()); |
| 116 | HashData.Flags = static_cast<uint32_t>(dxbc::HashFlags::IncludesSource); |
| 117 | } else { |
| 118 | reportFatalUsageError(reason: "/Zss requires debug info (/Zi or /Zs)" ); |
| 119 | } |
| 120 | } else { |
| 121 | DXILConstant = |
| 122 | cast<ConstantDataArray>(Val: M.getNamedGlobal(Name: "dx.dxil" )->getInitializer()); |
| 123 | } |
| 124 | |
| 125 | Digest.update(Str: DXILConstant->getRawDataValues()); |
| 126 | MD5::MD5Result MD5 = Digest.final(); |
| 127 | |
| 128 | memcpy(dest: reinterpret_cast<void *>(&HashData.Digest), src: MD5.data(), n: 16); |
| 129 | if (sys::IsBigEndianHost) |
| 130 | HashData.swapBytes(); |
| 131 | StringRef Data(reinterpret_cast<char *>(&HashData), sizeof(dxbc::ShaderHash)); |
| 132 | |
| 133 | Constant *ModuleConstant = |
| 134 | ConstantDataArray::get(Context&: M.getContext(), Elts: arrayRefFromStringRef(Input: Data)); |
| 135 | Globals.emplace_back( |
| 136 | Args: buildContainerGlobal(M, Content: ModuleConstant, Name: "dx.hash" , SectionName: "HASH" )); |
| 137 | |
| 138 | if (M.debug_compile_units().empty()) |
| 139 | return; |
| 140 | |
| 141 | SmallString<40> DebugNameStr; |
| 142 | Digest.stringifyResult(Result&: MD5, Str&: DebugNameStr); |
| 143 | DebugNameStr += ".pdb" ; |
| 144 | if (!PdbDebugPath.empty() || PdbInPrivate) { |
| 145 | if (!PdbDebugPath.empty()) { |
| 146 | StringRef DebugFile = PdbDebugPath.getValue(); |
| 147 | SmallString<256> AbsoluteDebugName; |
| 148 | if (sys::path::is_separator(value: DebugFile.back())) { |
| 149 | // If PDB output path was specified as a directory, put the MD5.pdb file |
| 150 | // there. |
| 151 | AbsoluteDebugName = DebugFile; |
| 152 | sys::path::append(path&: AbsoluteDebugName, a: DebugNameStr); |
| 153 | } else { |
| 154 | // Otherwise, use PDB output path as a user-provided PDB file name. |
| 155 | DebugNameStr = DebugFile; |
| 156 | AbsoluteDebugName = DebugNameStr; |
| 157 | } |
| 158 | |
| 159 | // Pass PDB name to DXContainerPDBPass via PDBNAME section. |
| 160 | addSection(M, Globals, SectionData: AbsoluteDebugName, MetadataName: "dx.pdb.name" , |
| 161 | SectionName: PdbFileNameSectionName); |
| 162 | } |
| 163 | |
| 164 | // Pass module hash to DXContainerPDBPass. |
| 165 | Globals.emplace_back(Args: buildContainerGlobal( |
| 166 | M, Content: ConstantDataArray::get(Context&: M.getContext(), Elts: ArrayRef(HashData.Digest)), |
| 167 | Name: "dx.pdb.hash" , SectionName: ModuleHashSectionName)); |
| 168 | } |
| 169 | |
| 170 | // Emit ILDN part in debug info mode. |
| 171 | mcdxbc::DebugName DebugName; |
| 172 | DebugName.setFilename(DebugNameStr); |
| 173 | SmallString<64> ILDNData; |
| 174 | raw_svector_ostream OS(ILDNData); |
| 175 | DebugName.write(OS); |
| 176 | addSection(M, Globals, SectionData: ILDNData, MetadataName: "dx.ildn" , SectionName: "ILDN" ); |
| 177 | } |
| 178 | |
| 179 | static GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name, |
| 180 | StringRef SectionName, |
| 181 | const dxil::ModuleMetadataInfo &MMI) { |
| 182 | SmallString<256> Data; |
| 183 | raw_svector_ostream OS(Data); |
| 184 | VersionTuple V = MMI.ValidatorVersion; |
| 185 | bool Legacy = !V.empty() && V != VersionTuple(0, 0) && V < VersionTuple(1, 7); |
| 186 | Sig.write(OS, /*PadToDword=*/!Legacy); |
| 187 | Constant *Constant = |
| 188 | ConstantDataArray::getString(Context&: M.getContext(), Initializer: Data, /*AddNull*/ false); |
| 189 | return buildContainerGlobal(M, Content: Constant, Name, SectionName); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | addSignature(Module &M, SmallVector<GlobalValue *> &Globals, |
| 194 | function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo, |
| 195 | function_ref<const ModuleSignatureInfo &()> GetSignatureInfo) { |
| 196 | const ModuleMetadataInfo &MMI = GetModuleMetadataInfo(); |
| 197 | const ModuleSignatureInfo &Signatures = GetSignatureInfo(); |
| 198 | Signature InputSig, OutputSig; |
| 199 | if (MMI.ShaderProfile != Triple::Library && MMI.EntryPropertyVec.size() == 1) |
| 200 | if (const auto *Sig = Signatures.get(F: MMI.EntryPropertyVec[0].Entry)) |
| 201 | Sig->buildSignatures(Input&: InputSig, Output&: OutputSig, ValidatorVersion: MMI.ValidatorVersion); |
| 202 | |
| 203 | Globals.emplace_back(Args: buildSignature(M, Sig&: InputSig, Name: "dx.isg1" , SectionName: "ISG1" , MMI)); |
| 204 | Globals.emplace_back(Args: buildSignature(M, Sig&: OutputSig, Name: "dx.osg1" , SectionName: "OSG1" , MMI)); |
| 205 | } |
| 206 | |
| 207 | static void addRootSignature( |
| 208 | Module &M, SmallVector<GlobalValue *> &Globals, |
| 209 | function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo, |
| 210 | function_ref<RootSignatureBindingInfo &()> GetRootSignatureBindingInfo) { |
| 211 | |
| 212 | dxil::ModuleMetadataInfo &MMI = GetModuleMetadataInfo(); |
| 213 | |
| 214 | // Root Signature in Library don't compile to DXContainer. |
| 215 | if (MMI.ShaderProfile == llvm::Triple::Library) |
| 216 | return; |
| 217 | |
| 218 | RootSignatureBindingInfo &RSA = GetRootSignatureBindingInfo(); |
| 219 | const Function *EntryFunction = nullptr; |
| 220 | |
| 221 | if (MMI.ShaderProfile != llvm::Triple::RootSignature) { |
| 222 | assert(MMI.EntryPropertyVec.size() == 1); |
| 223 | EntryFunction = MMI.EntryPropertyVec[0].Entry; |
| 224 | } |
| 225 | |
| 226 | const mcdxbc::RootSignatureDesc *RS = RSA.getDescForFunction(F: EntryFunction); |
| 227 | if (!RS) |
| 228 | return; |
| 229 | |
| 230 | SmallString<256> Data; |
| 231 | raw_svector_ostream OS(Data); |
| 232 | |
| 233 | RS->write(OS); |
| 234 | |
| 235 | addSection(M, Globals, SectionData: Data, MetadataName: "dx.rts0" , SectionName: "RTS0" ); |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV, |
| 240 | function_ref<DXILResourceMap &()> GetResourceMap, |
| 241 | function_ref<DXILResourceTypeMap &()> GetResourceTypeMap) { |
| 242 | const DXILResourceMap &DRM = GetResourceMap(); |
| 243 | DXILResourceTypeMap &DRTM = GetResourceTypeMap(); |
| 244 | |
| 245 | auto MakeBinding = [](const dxil::ResourceInfo::ResourceBinding &Binding, |
| 246 | const dxbc::PSV::ResourceType Type, |
| 247 | const dxil::ResourceKind Kind, |
| 248 | const dxbc::PSV::ResourceFlags Flags = |
| 249 | dxbc::PSV::ResourceFlags()) { |
| 250 | dxbc::PSV::v2::ResourceBindInfo BindInfo; |
| 251 | BindInfo.Type = Type; |
| 252 | BindInfo.LowerBound = Binding.LowerBound; |
| 253 | assert((Binding.Size == 0 || |
| 254 | (uint64_t)Binding.LowerBound + Binding.Size - 1 <= UINT32_MAX) && |
| 255 | "Resource range is too large" ); |
| 256 | BindInfo.UpperBound = (Binding.Size == 0) |
| 257 | ? UINT32_MAX |
| 258 | : Binding.LowerBound + Binding.Size - 1; |
| 259 | BindInfo.Space = Binding.Space; |
| 260 | BindInfo.Kind = static_cast<dxbc::PSV::ResourceKind>(Kind); |
| 261 | BindInfo.Flags = Flags; |
| 262 | return BindInfo; |
| 263 | }; |
| 264 | |
| 265 | for (const dxil::ResourceInfo &RI : DRM.cbuffers()) { |
| 266 | if (!RI.hasBinding()) |
| 267 | continue; |
| 268 | const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding(); |
| 269 | PSV.Resources.push_back(Elt: MakeBinding(Binding, dxbc::PSV::ResourceType::CBV, |
| 270 | dxil::ResourceKind::CBuffer)); |
| 271 | } |
| 272 | for (const dxil::ResourceInfo &RI : DRM.samplers()) { |
| 273 | if (!RI.hasBinding()) |
| 274 | continue; |
| 275 | const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding(); |
| 276 | PSV.Resources.push_back(Elt: MakeBinding(Binding, |
| 277 | dxbc::PSV::ResourceType::Sampler, |
| 278 | dxil::ResourceKind::Sampler)); |
| 279 | } |
| 280 | for (const dxil::ResourceInfo &RI : DRM.srvs()) { |
| 281 | if (!RI.hasBinding()) |
| 282 | continue; |
| 283 | const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding(); |
| 284 | |
| 285 | dxil::ResourceTypeInfo &TypeInfo = DRTM[RI.getHandleTy()]; |
| 286 | dxbc::PSV::ResourceType ResType; |
| 287 | if (TypeInfo.isStruct()) |
| 288 | ResType = dxbc::PSV::ResourceType::SRVStructured; |
| 289 | else if (TypeInfo.isTyped()) |
| 290 | ResType = dxbc::PSV::ResourceType::SRVTyped; |
| 291 | else |
| 292 | ResType = dxbc::PSV::ResourceType::SRVRaw; |
| 293 | |
| 294 | PSV.Resources.push_back( |
| 295 | Elt: MakeBinding(Binding, ResType, TypeInfo.getResourceKind())); |
| 296 | } |
| 297 | for (const dxil::ResourceInfo &RI : DRM.uavs()) { |
| 298 | if (!RI.hasBinding()) |
| 299 | continue; |
| 300 | const dxil::ResourceInfo::ResourceBinding &Binding = RI.getBinding(); |
| 301 | |
| 302 | dxil::ResourceTypeInfo &TypeInfo = DRTM[RI.getHandleTy()]; |
| 303 | dxbc::PSV::ResourceType ResType; |
| 304 | if (RI.hasCounter()) |
| 305 | ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter; |
| 306 | else if (TypeInfo.isStruct()) |
| 307 | ResType = dxbc::PSV::ResourceType::UAVStructured; |
| 308 | else if (TypeInfo.isTyped()) |
| 309 | ResType = dxbc::PSV::ResourceType::UAVTyped; |
| 310 | else |
| 311 | ResType = dxbc::PSV::ResourceType::UAVRaw; |
| 312 | |
| 313 | dxbc::PSV::ResourceFlags Flags; |
| 314 | Flags.Bits.UsedByAtomic64 = RI.HasAtomic64Use; |
| 315 | |
| 316 | PSV.Resources.push_back( |
| 317 | Elt: MakeBinding(Binding, ResType, TypeInfo.getResourceKind(), Flags)); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static void addPipelineStateValidationInfo( |
| 322 | Module &M, SmallVector<GlobalValue *> &Globals, |
| 323 | function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo, |
| 324 | function_ref<DXILResourceMap &()> GetResourceMap, |
| 325 | function_ref<DXILResourceTypeMap &()> GetResourceTypeMap, |
| 326 | function_ref<const ModuleShaderFlags &()> GetModuleShaderFlags, |
| 327 | function_ref<const ModuleSignatureInfo &()> GetModuleSignatureInfo) { |
| 328 | SmallString<256> Data; |
| 329 | raw_svector_ostream OS(Data); |
| 330 | PSVRuntimeInfo PSV; |
| 331 | PSV.BaseData.MinimumWaveLaneCount = 0; |
| 332 | PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max(); |
| 333 | |
| 334 | dxil::ModuleMetadataInfo &MMI = GetModuleMetadataInfo(); |
| 335 | assert(MMI.EntryPropertyVec.size() == 1 || |
| 336 | MMI.ShaderProfile == Triple::Library || |
| 337 | MMI.ShaderProfile == Triple::RootSignature); |
| 338 | PSV.BaseData.ShaderStage = |
| 339 | static_cast<uint8_t>(MMI.ShaderProfile - Triple::Pixel); |
| 340 | |
| 341 | addResourcesForPSV(M, PSV, GetResourceMap, GetResourceTypeMap); |
| 342 | unsigned PSVVersion = 3; |
| 343 | if (MMI.ShaderProfile != Triple::Library && |
| 344 | MMI.EntryPropertyVec.size() == 1) { |
| 345 | const Function *Entry = MMI.EntryPropertyVec[0].Entry; |
| 346 | bool UsesViewID = GetModuleShaderFlags().getFunctionFlags(Entry).ViewID; |
| 347 | PSV.BaseData.UsesViewID = UsesViewID; |
| 348 | const ModuleSignatureInfo &Signatures = GetModuleSignatureInfo(); |
| 349 | if (const auto *Sig = Signatures.get(F: Entry)) { |
| 350 | Sig->updatePSV(PSV, ValidatorVersion: MMI.ValidatorVersion, UsesViewID); |
| 351 | // Preserve the existing empty-signature path, and select an ABI version |
| 352 | // compatible with the validator for graphics signature data. |
| 353 | VersionTuple V = MMI.ValidatorVersion; |
| 354 | if ((!Sig->Inputs.empty() || !Sig->Outputs.empty()) && !V.empty() && |
| 355 | V != VersionTuple(0, 0)) |
| 356 | PSVVersion = V < VersionTuple(1, 1) ? 0 |
| 357 | : V < VersionTuple(1, 6) ? 1 |
| 358 | : V < VersionTuple(1, 8) ? 2 |
| 359 | : 3; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Hardcoded values here to unblock loading the shader into D3D. |
| 364 | // |
| 365 | // TODO: Lots more stuff to do here! |
| 366 | // |
| 367 | // See issue https://github.com/llvm/llvm-project/issues/96674. |
| 368 | switch (MMI.ShaderProfile) { |
| 369 | case Triple::Compute: |
| 370 | PSV.BaseData.NumThreadsX = MMI.EntryPropertyVec[0].NumThreadsX; |
| 371 | PSV.BaseData.NumThreadsY = MMI.EntryPropertyVec[0].NumThreadsY; |
| 372 | PSV.BaseData.NumThreadsZ = MMI.EntryPropertyVec[0].NumThreadsZ; |
| 373 | if (MMI.EntryPropertyVec[0].WaveSizeMin) { |
| 374 | PSV.BaseData.MinimumWaveLaneCount = MMI.EntryPropertyVec[0].WaveSizeMin; |
| 375 | PSV.BaseData.MaximumWaveLaneCount = |
| 376 | MMI.EntryPropertyVec[0].WaveSizeMax |
| 377 | ? MMI.EntryPropertyVec[0].WaveSizeMax |
| 378 | : MMI.EntryPropertyVec[0].WaveSizeMin; |
| 379 | } |
| 380 | break; |
| 381 | default: |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (MMI.ShaderProfile != Triple::Library && |
| 386 | MMI.ShaderProfile != Triple::RootSignature) |
| 387 | PSV.EntryName = MMI.EntryPropertyVec[0].Entry->getName(); |
| 388 | |
| 389 | PSV.finalize(Stage: MMI.ShaderProfile, Version: PSVVersion); |
| 390 | PSV.write(OS, Version: PSVVersion); |
| 391 | addSection(M, Globals, SectionData: Data, MetadataName: "dx.psv0" , SectionName: "PSV0" ); |
| 392 | } |
| 393 | |
| 394 | static void addCompilerVersion(Module &M, SmallVector<GlobalValue *> &Globals) { |
| 395 | if (M.debug_compile_units().empty()) |
| 396 | return; |
| 397 | |
| 398 | SmallString<256> Data; |
| 399 | raw_svector_ostream OS(Data); |
| 400 | mcdxbc::CompilerVersion CompilerVersion; |
| 401 | CompilerVersion.write(OS); |
| 402 | addSection(M, Globals, SectionData: Data, MetadataName: "dx.vers" , SectionName: "VERS" ); |
| 403 | } |
| 404 | |
| 405 | static void addSourceInfo( |
| 406 | Module &M, SmallVector<GlobalValue *> &Globals, |
| 407 | function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo) { |
| 408 | dxil::ModuleMetadataInfo &MMI = GetModuleMetadataInfo(); |
| 409 | |
| 410 | if (!MMI.SourceInfo || SourceInDebugModule) |
| 411 | return; |
| 412 | |
| 413 | MMI.SourceInfo->computeEntries(); |
| 414 | MMI.SourceInfo->finalize(); |
| 415 | SmallString<256> Data; |
| 416 | raw_svector_ostream OS(Data); |
| 417 | MMI.SourceInfo->write(OS); |
| 418 | addSection(M, Globals, SectionData: Data, MetadataName: "dx.srci" , SectionName: "SRCI" ); |
| 419 | } |
| 420 | |
| 421 | static bool runContainerGlobalsOnModule( |
| 422 | Module &M, function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo, |
| 423 | function_ref<DXILResourceMap &()> GetResourceMap, |
| 424 | function_ref<DXILResourceTypeMap &()> GetResourceTypeMap, |
| 425 | function_ref<const ModuleShaderFlags &()> GetModuleShaderFlags, |
| 426 | function_ref<const ModuleSignatureInfo &()> GetModuleSignatureInfo, |
| 427 | function_ref<RootSignatureBindingInfo &()> GetRootSignatureBindingInfo) { |
| 428 | llvm::SmallVector<GlobalValue *> Globals; |
| 429 | Globals.push_back(Elt: getFeatureFlags(M, GetShaderFlags: GetModuleShaderFlags)); |
| 430 | computeShaderHashAndDebugName(M, Globals); |
| 431 | addSignature(M, Globals, GetModuleMetadataInfo, GetSignatureInfo: GetModuleSignatureInfo); |
| 432 | addRootSignature(M, Globals, GetModuleMetadataInfo, |
| 433 | GetRootSignatureBindingInfo); |
| 434 | addPipelineStateValidationInfo(M, Globals, GetModuleMetadataInfo, |
| 435 | GetResourceMap, GetResourceTypeMap, |
| 436 | GetModuleShaderFlags, GetModuleSignatureInfo); |
| 437 | addCompilerVersion(M, Globals); |
| 438 | addSourceInfo(M, Globals, GetModuleMetadataInfo); |
| 439 | appendToCompilerUsed(M, Values: Globals); |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | bool DXILContainerGlobalsLegacy::runOnModule(Module &M) { |
| 444 | return runContainerGlobalsOnModule( |
| 445 | M, |
| 446 | GetModuleMetadataInfo: [&]() -> ModuleMetadataInfo & { |
| 447 | return getAnalysis<DXILMetadataAnalysisWrapperPass>() |
| 448 | .getModuleMetadata(); |
| 449 | }, |
| 450 | GetResourceMap: [&]() -> DXILResourceMap & { |
| 451 | return getAnalysis<DXILResourceWrapperPass>().getResourceMap(); |
| 452 | }, |
| 453 | GetResourceTypeMap: [&]() -> DXILResourceTypeMap & { |
| 454 | return getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap(); |
| 455 | }, |
| 456 | GetModuleShaderFlags: [&]() -> const ModuleShaderFlags & { |
| 457 | return getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags(); |
| 458 | }, |
| 459 | GetModuleSignatureInfo: [&]() -> const ModuleSignatureInfo & { |
| 460 | return getAnalysis<SignatureAnalysisWrapper>().getSignatureInfo(); |
| 461 | }, |
| 462 | GetRootSignatureBindingInfo: [&]() -> RootSignatureBindingInfo & { |
| 463 | return getAnalysis<RootSignatureAnalysisWrapper>().getRSInfo(); |
| 464 | }); |
| 465 | } |
| 466 | |
| 467 | PreservedAnalyses DXILContainerGlobalsPass::run(Module &M, |
| 468 | ModuleAnalysisManager &MAM) { |
| 469 | runContainerGlobalsOnModule( |
| 470 | M, |
| 471 | GetModuleMetadataInfo: [&]() -> ModuleMetadataInfo & { |
| 472 | return MAM.getResult<DXILMetadataAnalysis>(IR&: M); |
| 473 | }, |
| 474 | GetResourceMap: [&]() -> DXILResourceMap & { |
| 475 | return MAM.getResult<DXILResourceAnalysis>(IR&: M); |
| 476 | }, |
| 477 | GetResourceTypeMap: [&]() -> DXILResourceTypeMap & { |
| 478 | return MAM.getResult<DXILResourceTypeAnalysis>(IR&: M); |
| 479 | }, |
| 480 | GetModuleShaderFlags: [&]() -> const ModuleShaderFlags & { |
| 481 | return MAM.getResult<ShaderFlagsAnalysis>(IR&: M); |
| 482 | }, |
| 483 | GetModuleSignatureInfo: [&]() -> const ModuleSignatureInfo & { |
| 484 | return MAM.getResult<SignatureAnalysis>(IR&: M); |
| 485 | }, |
| 486 | GetRootSignatureBindingInfo: [&]() -> RootSignatureBindingInfo & { |
| 487 | return MAM.getResult<RootSignatureAnalysis>(IR&: M); |
| 488 | }); |
| 489 | return PreservedAnalyses::all(); |
| 490 | } |
| 491 | |
| 492 | char DXILContainerGlobalsLegacy::ID = 0; |
| 493 | INITIALIZE_PASS_BEGIN(DXILContainerGlobalsLegacy, "dxil-container-globals" , |
| 494 | "DXContainer Global Emitter" , false, true) |
| 495 | INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper) |
| 496 | INITIALIZE_PASS_DEPENDENCY(SignatureAnalysisWrapper) |
| 497 | INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass) |
| 498 | INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) |
| 499 | INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass) |
| 500 | INITIALIZE_PASS_END(DXILContainerGlobalsLegacy, "dxil-container-globals" , |
| 501 | "DXContainer Global Emitter" , false, true) |
| 502 | |
| 503 | ModulePass *llvm::createDXILContainerGlobalsLegacyPass() { |
| 504 | return new DXILContainerGlobalsLegacy(); |
| 505 | } |
| 506 | |