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