26
26
#include " llvm/Pass.h"
27
27
#include " llvm/Support/Error.h"
28
28
#include " llvm/Support/ErrorHandling.h"
29
+ #include " llvm/Support/raw_ostream.h"
29
30
#include < cstdint>
30
31
#include < optional>
31
32
#include < utility>
@@ -39,20 +40,20 @@ static bool reportError(LLVMContext *Ctx, Twine Message,
39
40
return true ;
40
41
}
41
42
42
- static bool parseRootFlags (LLVMContext *Ctx, ModuleRootSignature &MRS ,
43
+ static bool parseRootFlags (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD ,
43
44
MDNode *RootFlagNode) {
44
45
45
46
if (RootFlagNode->getNumOperands () != 2 )
46
47
return reportError (Ctx, " Invalid format for RootFlag Element" );
47
48
48
49
auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand (1 ));
49
- MRS .Flags = Flag->getZExtValue ();
50
+ RSD .Flags = Flag->getZExtValue ();
50
51
51
52
return false ;
52
53
}
53
54
54
55
static bool parseRootSignatureElement (LLVMContext *Ctx,
55
- ModuleRootSignature &MRS ,
56
+ mcdxbc::RootSignatureDesc &RSD ,
56
57
MDNode *Element) {
57
58
MDString *ElementText = cast<MDString>(Element->getOperand (0 ));
58
59
if (ElementText == nullptr )
@@ -61,21 +62,22 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
61
62
RootSignatureElementKind ElementKind =
62
63
StringSwitch<RootSignatureElementKind>(ElementText->getString ())
63
64
.Case (" RootFlags" , RootSignatureElementKind::RootFlags)
64
- .Default (RootSignatureElementKind::None );
65
+ .Default (RootSignatureElementKind::Error );
65
66
66
67
switch (ElementKind) {
67
68
68
69
case RootSignatureElementKind::RootFlags:
69
- return parseRootFlags (Ctx, MRS , Element);
70
- case RootSignatureElementKind::None :
70
+ return parseRootFlags (Ctx, RSD , Element);
71
+ case RootSignatureElementKind::Error :
71
72
return reportError (Ctx, " Invalid Root Signature Element: " +
72
73
ElementText->getString ());
73
74
}
74
75
75
- llvm_unreachable (" Root signature element kind not expected ." );
76
+ llvm_unreachable (" Unhandled RootSignatureElementKind enum ." );
76
77
}
77
78
78
- static bool parse (LLVMContext *Ctx, ModuleRootSignature &MRS, MDNode *Node) {
79
+ static bool parse (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
80
+ MDNode *Node) {
79
81
bool HasError = false ;
80
82
81
83
// Loop through the Root Elements of the root signature.
@@ -84,20 +86,20 @@ static bool parse(LLVMContext *Ctx, ModuleRootSignature &MRS, MDNode *Node) {
84
86
if (Element == nullptr )
85
87
return reportError (Ctx, " Missing Root Element Metadata Node." );
86
88
87
- HasError = HasError || parseRootSignatureElement (Ctx, MRS , Element);
89
+ HasError = HasError || parseRootSignatureElement (Ctx, RSD , Element);
88
90
}
89
91
90
92
return HasError;
91
93
}
92
94
93
- static bool validate (LLVMContext *Ctx, const ModuleRootSignature &MRS ) {
94
- if (!dxbc::RootSignatureValidations::isValidRootFlag (MRS .Flags )) {
95
+ static bool validate (LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD ) {
96
+ if (!dxbc::RootSignatureValidations::isValidRootFlag (RSD .Flags )) {
95
97
return reportError (Ctx, " Invalid Root Signature flag value" );
96
98
}
97
99
return false ;
98
100
}
99
101
100
- static SmallDenseMap<const Function *, ModuleRootSignature >
102
+ static SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc >
101
103
analyzeModule (Module &M) {
102
104
103
105
/* * Root Signature are specified as following in the metadata:
@@ -114,11 +116,11 @@ analyzeModule(Module &M) {
114
116
115
117
LLVMContext *Ctx = &M.getContext ();
116
118
117
- SmallDenseMap<const Function *, ModuleRootSignature> MRSMap ;
119
+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> RSDMap ;
118
120
119
121
NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
120
122
if (RootSignatureNode == nullptr )
121
- return MRSMap ;
123
+ return RSDMap ;
122
124
123
125
for (const auto &RSDefNode : RootSignatureNode->operands ()) {
124
126
if (RSDefNode->getNumOperands () != 2 ) {
@@ -153,49 +155,50 @@ analyzeModule(Module &M) {
153
155
reportError (Ctx, " Missing Root Element List Metadata node." );
154
156
}
155
157
156
- ModuleRootSignature MRS ;
158
+ mcdxbc::RootSignatureDesc RSD ;
157
159
158
- if (parse (Ctx, MRS , RootElementListNode) || validate (Ctx, MRS )) {
159
- return MRSMap ;
160
+ if (parse (Ctx, RSD , RootElementListNode) || validate (Ctx, RSD )) {
161
+ return RSDMap ;
160
162
}
161
163
162
- MRSMap .insert (std::make_pair (F, MRS ));
164
+ RSDMap .insert (std::make_pair (F, RSD ));
163
165
}
164
166
165
- return MRSMap ;
167
+ return RSDMap ;
166
168
}
167
169
168
170
AnalysisKey RootSignatureAnalysis::Key;
169
171
170
- SmallDenseMap<const Function *, ModuleRootSignature >
172
+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc >
171
173
RootSignatureAnalysis::run (Module &M, ModuleAnalysisManager &AM) {
172
174
return analyzeModule (M);
173
175
}
174
176
175
177
// ===----------------------------------------------------------------------===//
176
178
177
- static void printSpaces (raw_ostream &Stream, unsigned int Count) {
178
- for (unsigned int I = 0 ; I < Count; ++I) {
179
- Stream << ' ' ;
180
- }
181
- }
182
-
183
179
PreservedAnalyses RootSignatureAnalysisPrinter::run (Module &M,
184
180
ModuleAnalysisManager &AM) {
185
181
186
- SmallDenseMap<const Function *, ModuleRootSignature > &MRSMap =
182
+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc > &RSDMap =
187
183
AM.getResult <RootSignatureAnalysis>(M);
188
184
OS << " Root Signature Definitions"
189
185
<< " \n " ;
190
186
uint8_t Space = 0 ;
191
- for (const auto &P : MRSMap ) {
192
- const auto &[Function, MRS ] = P;
187
+ for (const auto &P : RSDMap ) {
188
+ const auto &[Function, RSD ] = P;
193
189
OS << " Definition for '" << Function->getName () << " ':\n " ;
194
190
195
191
// start root signature header
196
192
Space++;
197
- printSpaces (OS, Space);
198
- OS << " Flags: " << format_hex (MRS.Flags , 8 ) << " :\n " ;
193
+ OS << indent (Space) << " Flags: " << format_hex (RSD.Flags , 8 ) << " :\n " ;
194
+ OS << indent (Space) << " Version: " << RSD.Version << " :\n " ;
195
+ OS << indent (Space) << " NumParameters: " << RSD.NumParameters << " :\n " ;
196
+ OS << indent (Space) << " RootParametersOffset: " << RSD.RootParametersOffset
197
+ << " :\n " ;
198
+ OS << indent (Space) << " NumStaticSamplers: " << RSD.NumStaticSamplers
199
+ << " :\n " ;
200
+ OS << indent (Space) << " StaticSamplersOffset: " << RSD.StaticSamplersOffset
201
+ << " :\n " ;
199
202
Space--;
200
203
// end root signature header
201
204
}
@@ -205,7 +208,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
205
208
206
209
// ===----------------------------------------------------------------------===//
207
210
bool RootSignatureAnalysisWrapper::runOnModule (Module &M) {
208
- MRS = analyzeModule (M);
211
+ FuncToRsMap = analyzeModule (M);
209
212
return false ;
210
213
}
211
214
0 commit comments