1 //===- DXILRootSignature.h - 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 #ifndef LLVM_LIB_TARGET_DIRECTX_DXILROOTSIGNATURE_H 14 #define LLVM_LIB_TARGET_DIRECTX_DXILROOTSIGNATURE_H 15 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/Analysis/DXILMetadataAnalysis.h" 18 #include "llvm/IR/DiagnosticInfo.h" 19 #include "llvm/IR/Metadata.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/MC/DXContainerRootSignature.h" 23 #include "llvm/Pass.h" 24 #include <optional> 25 26 namespace llvm { 27 namespace dxil { 28 29 enum class RootSignatureElementKind { 30 Error = 0, 31 RootFlags = 1, 32 RootConstants = 2, 33 SRV = 3, 34 UAV = 4, 35 CBV = 5, 36 DescriptorTable = 6, 37 StaticSamplers = 7 38 }; 39 40 class RootSignatureBindingInfo { 41 private: 42 SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> FuncToRsMap; 43 44 public: 45 using iterator = 46 SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc>::iterator; 47 48 RootSignatureBindingInfo() = default; RootSignatureBindingInfo(SmallDenseMap<const Function *,mcdxbc::RootSignatureDesc> Map)49 RootSignatureBindingInfo( 50 SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> Map) 51 : FuncToRsMap(Map) {}; 52 find(const Function * F)53 iterator find(const Function *F) { return FuncToRsMap.find(F); } 54 end()55 iterator end() { return FuncToRsMap.end(); } 56 57 std::optional<mcdxbc::RootSignatureDesc> getDescForFunction(const Function * F)58 getDescForFunction(const Function *F) { 59 const auto FuncRs = find(F); 60 if (FuncRs == end()) 61 return std::nullopt; 62 63 return FuncRs->second; 64 } 65 }; 66 67 class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> { 68 friend AnalysisInfoMixin<RootSignatureAnalysis>; 69 static AnalysisKey Key; 70 71 public: 72 RootSignatureAnalysis() = default; 73 74 using Result = RootSignatureBindingInfo; 75 76 Result run(Module &M, ModuleAnalysisManager &AM); 77 }; 78 79 /// Wrapper pass for the legacy pass manager. 80 /// 81 /// This is required because the passes that will depend on this are codegen 82 /// passes which run through the legacy pass manager. 83 class RootSignatureAnalysisWrapper : public ModulePass { 84 private: 85 std::unique_ptr<RootSignatureBindingInfo> FuncToRsMap; 86 87 public: 88 static char ID; RootSignatureAnalysisWrapper()89 RootSignatureAnalysisWrapper() : ModulePass(ID) {} 90 getRSInfo()91 RootSignatureBindingInfo &getRSInfo() { return *FuncToRsMap; } 92 93 bool runOnModule(Module &M) override; 94 95 void getAnalysisUsage(AnalysisUsage &AU) const override; 96 }; 97 98 /// Printer pass for RootSignatureAnalysis results. 99 class RootSignatureAnalysisPrinter 100 : public PassInfoMixin<RootSignatureAnalysisPrinter> { 101 raw_ostream &OS; 102 103 public: RootSignatureAnalysisPrinter(raw_ostream & OS)104 explicit RootSignatureAnalysisPrinter(raw_ostream &OS) : OS(OS) {} 105 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 106 }; 107 108 } // namespace dxil 109 } // namespace llvm 110 #endif 111