1414#include " DirectX.h"
1515#include " llvm/ADT/StringSwitch.h"
1616#include " llvm/ADT/Twine.h"
17+ #include " llvm/Analysis/DXILMetadataAnalysis.h"
1718#include " llvm/IR/Constants.h"
19+ #include " llvm/IR/Function.h"
1820#include " llvm/IR/Module.h"
19- #include < cstdint>
21+ #include " llvm/InitializePasses.h"
22+ #include " llvm/Pass.h"
2023
2124using namespace llvm ;
2225using namespace llvm ::dxil;
@@ -80,7 +83,7 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS,
8083 return true ;
8184}
8285
83- bool ModuleRootSignature::parse (NamedMDNode *Root) {
86+ bool ModuleRootSignature::parse (NamedMDNode *Root, const Function *EF ) {
8487 bool HasError = false ;
8588
8689 /* * Root Signature are specified as following in the metadata:
@@ -96,11 +99,25 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
9699 */
97100
98101 for (const MDNode *Node : Root->operands ()) {
99-
100102 if (Node->getNumOperands () != 2 )
101103 return reportError (" Invalid format for Root Signature Definition. Pairs "
102104 " of function, root signature expected." );
103105
106+ Metadata *MD = Node->getOperand (0 ).get ();
107+ if (auto *VAM = llvm::dyn_cast<llvm::ValueAsMetadata>(MD)) {
108+ llvm::Value *V = VAM->getValue ();
109+ if (Function *F = dyn_cast<Function>(V)) {
110+ if (F != EF)
111+ continue ;
112+ } else {
113+ return reportError (
114+ " Root Signature MD node, first element is not a function." );
115+ }
116+ } else {
117+ return reportError (
118+ " Root Signature MD node, first element is not a function." );
119+ }
120+
104121 // Get the Root Signature Description from the function signature pair.
105122 MDNode *RS = dyn_cast<MDNode>(Node->getOperand (1 ).get ());
106123
@@ -120,12 +137,13 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
120137 return HasError;
121138}
122139
123- ModuleRootSignature ModuleRootSignature::analyzeModule (Module &M) {
140+ ModuleRootSignature ModuleRootSignature::analyzeModule (Module &M,
141+ const Function *F) {
124142 ModuleRootSignature MRS;
125143
126144 NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
127145 if (RootSignatureNode) {
128- if (MRS.parse (RootSignatureNode))
146+ if (MRS.parse (RootSignatureNode, F ))
129147 llvm_unreachable (" Invalid Root Signature Metadata." );
130148 }
131149
@@ -136,22 +154,43 @@ AnalysisKey RootSignatureAnalysis::Key;
136154
137155ModuleRootSignature RootSignatureAnalysis::run (Module &M,
138156 ModuleAnalysisManager &AM) {
139- return ModuleRootSignature::analyzeModule (M);
157+ auto MMI = AM.getResult <DXILMetadataAnalysis>(M);
158+
159+ if (MMI.ShaderProfile == Triple::Library)
160+ return ModuleRootSignature ();
161+
162+ assert (MMI.EntryPropertyVec .size () == 1 );
163+
164+ const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
165+ return ModuleRootSignature::analyzeModule (M, EntryFunction);
140166}
141167
142168// ===----------------------------------------------------------------------===//
143169bool RootSignatureAnalysisWrapper::runOnModule (Module &M) {
144170
145- this ->MRS = MRS = ModuleRootSignature::analyzeModule (M);
171+ dxil::ModuleMetadataInfo &MMI =
172+ getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata ();
173+
174+ if (MMI.ShaderProfile == Triple::Library)
175+ return false ;
176+ assert (MMI.EntryPropertyVec .size () == 1 );
177+
178+ const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
179+ this ->MRS = MRS = ModuleRootSignature::analyzeModule (M, EntryFunction);
146180
147181 return false ;
148182}
149183
150184void RootSignatureAnalysisWrapper::getAnalysisUsage (AnalysisUsage &AU) const {
151185 AU.setPreservesAll ();
186+ AU.addRequired <DXILMetadataAnalysisWrapperPass>();
152187}
153188
154189char RootSignatureAnalysisWrapper::ID = 0 ;
155190
156- INITIALIZE_PASS (RootSignatureAnalysisWrapper, " dx-root-signature-analysis" ,
157- " DXIL Root Signature Analysis" , true , true )
191+ INITIALIZE_PASS_BEGIN (RootSignatureAnalysisWrapper,
192+ " dx-root-signature-analysis" ,
193+ " DXIL Root Signature Analysis" , true , true )
194+ INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
195+ INITIALIZE_PASS_END(RootSignatureAnalysisWrapper, " dx-root-signature-analysis" ,
196+ " DXIL Root Signature Analysis" , true , true )
0 commit comments