14
14
#include " DirectX.h"
15
15
#include " llvm/ADT/StringSwitch.h"
16
16
#include " llvm/ADT/Twine.h"
17
+ #include " llvm/Analysis/DXILMetadataAnalysis.h"
17
18
#include " llvm/IR/Constants.h"
19
+ #include " llvm/IR/Function.h"
18
20
#include " llvm/IR/Module.h"
19
- #include < cstdint>
21
+ #include " llvm/InitializePasses.h"
22
+ #include " llvm/Pass.h"
20
23
21
24
using namespace llvm ;
22
25
using namespace llvm ::dxil;
@@ -80,7 +83,7 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS,
80
83
return true ;
81
84
}
82
85
83
- bool ModuleRootSignature::parse (NamedMDNode *Root) {
86
+ bool ModuleRootSignature::parse (NamedMDNode *Root, const Function *EF ) {
84
87
bool HasError = false ;
85
88
86
89
/* * Root Signature are specified as following in the metadata:
@@ -96,11 +99,25 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
96
99
*/
97
100
98
101
for (const MDNode *Node : Root->operands ()) {
99
-
100
102
if (Node->getNumOperands () != 2 )
101
103
return reportError (" Invalid format for Root Signature Definition. Pairs "
102
104
" of function, root signature expected." );
103
105
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
+
104
121
// Get the Root Signature Description from the function signature pair.
105
122
MDNode *RS = dyn_cast<MDNode>(Node->getOperand (1 ).get ());
106
123
@@ -120,12 +137,13 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
120
137
return HasError;
121
138
}
122
139
123
- ModuleRootSignature ModuleRootSignature::analyzeModule (Module &M) {
140
+ ModuleRootSignature ModuleRootSignature::analyzeModule (Module &M,
141
+ const Function *F) {
124
142
ModuleRootSignature MRS;
125
143
126
144
NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
127
145
if (RootSignatureNode) {
128
- if (MRS.parse (RootSignatureNode))
146
+ if (MRS.parse (RootSignatureNode, F ))
129
147
llvm_unreachable (" Invalid Root Signature Metadata." );
130
148
}
131
149
@@ -136,22 +154,43 @@ AnalysisKey RootSignatureAnalysis::Key;
136
154
137
155
ModuleRootSignature RootSignatureAnalysis::run (Module &M,
138
156
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);
140
166
}
141
167
142
168
// ===----------------------------------------------------------------------===//
143
169
bool RootSignatureAnalysisWrapper::runOnModule (Module &M) {
144
170
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);
146
180
147
181
return false ;
148
182
}
149
183
150
184
void RootSignatureAnalysisWrapper::getAnalysisUsage (AnalysisUsage &AU) const {
151
185
AU.setPreservesAll ();
186
+ AU.addRequired <DXILMetadataAnalysisWrapperPass>();
152
187
}
153
188
154
189
char RootSignatureAnalysisWrapper::ID = 0 ;
155
190
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