@@ -40,21 +40,19 @@ static bool reportError(LLVMContext *Ctx, Twine Message,
40
40
return true ;
41
41
}
42
42
43
- static bool reportValueError (LLVMContext *Ctx, Twine ParamName, uint32_t Value,
44
- DiagnosticSeverity Severity = DS_Error ) {
43
+ static bool reportValueError (LLVMContext *Ctx, Twine ParamName,
44
+ uint32_t Value ) {
45
45
Ctx->diagnose (DiagnosticInfoGeneric (
46
- " Invalid value for " + ParamName + " : " + Twine (Value), Severity ));
46
+ " Invalid value for " + ParamName + " : " + Twine (Value), DS_Error ));
47
47
return true ;
48
48
}
49
49
50
- static bool extractMdIntValue (uint32_t &Value, MDNode *Node,
51
- unsigned int OpId) {
52
- auto *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand (OpId).get ());
53
- if (CI == nullptr )
54
- return true ;
55
-
56
- Value = CI->getZExtValue ();
57
- return false ;
50
+ static std::optional<uint32_t > extractMdIntValue (MDNode *Node,
51
+ unsigned int OpId) {
52
+ if (auto *CI =
53
+ mdconst::dyn_extract<ConstantInt>(Node->getOperand (OpId).get ()))
54
+ return CI->getZExtValue ();
55
+ return std::nullopt;
58
56
}
59
57
60
58
static bool parseRootFlags (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
@@ -63,7 +61,9 @@ static bool parseRootFlags(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
63
61
if (RootFlagNode->getNumOperands () != 2 )
64
62
return reportError (Ctx, " Invalid format for RootFlag Element" );
65
63
66
- if (extractMdIntValue (RSD.Flags , RootFlagNode, 1 ))
64
+ if (std::optional<uint32_t > Val = extractMdIntValue (RootFlagNode, 1 ))
65
+ RSD.Flags = *Val;
66
+ else
67
67
return reportError (Ctx, " Invalid value for RootFlag" );
68
68
69
69
return false ;
@@ -79,22 +79,24 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
79
79
NewParameter.Header .ParameterType =
80
80
llvm::to_underlying (dxbc::RootParameterType::Constants32Bit);
81
81
82
- uint32_t SV;
83
- if (extractMdIntValue (SV, RootConstantNode, 1 ))
82
+ if (std::optional<uint32_t > Val = extractMdIntValue (RootConstantNode, 1 ))
83
+ NewParameter.Header .ShaderVisibility = *Val;
84
+ else
84
85
return reportError (Ctx, " Invalid value for ShaderVisibility" );
85
86
86
- NewParameter.Header .ShaderVisibility = SV;
87
-
88
- if (extractMdIntValue (NewParameter.Constants .ShaderRegister , RootConstantNode,
89
- 2 ))
87
+ if (std::optional<uint32_t > Val = extractMdIntValue (RootConstantNode, 2 ))
88
+ NewParameter.Constants .ShaderRegister = *Val;
89
+ else
90
90
return reportError (Ctx, " Invalid value for ShaderRegister" );
91
91
92
- if (extractMdIntValue (NewParameter.Constants .RegisterSpace , RootConstantNode,
93
- 3 ))
92
+ if (std::optional<uint32_t > Val = extractMdIntValue (RootConstantNode, 3 ))
93
+ NewParameter.Constants .RegisterSpace = *Val;
94
+ else
94
95
return reportError (Ctx, " Invalid value for RegisterSpace" );
95
96
96
- if (extractMdIntValue (NewParameter.Constants .Num32BitValues , RootConstantNode,
97
- 4 ))
97
+ if (std::optional<uint32_t > Val = extractMdIntValue (RootConstantNode, 4 ))
98
+ NewParameter.Constants .Num32BitValues = *Val;
99
+ else
98
100
return reportError (Ctx, " Invalid value for Num32BitValues" );
99
101
100
102
RSD.Parameters .push_back (NewParameter);
@@ -148,32 +150,6 @@ static bool parse(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
148
150
149
151
static bool verifyRootFlag (uint32_t Flags) { return (Flags & ~0xfff ) == 0 ; }
150
152
151
- static bool verifyShaderVisibility (uint32_t Flags) {
152
- switch (Flags) {
153
-
154
- case llvm::to_underlying (dxbc::ShaderVisibility::All):
155
- case llvm::to_underlying (dxbc::ShaderVisibility::Vertex):
156
- case llvm::to_underlying (dxbc::ShaderVisibility::Hull):
157
- case llvm::to_underlying (dxbc::ShaderVisibility::Domain):
158
- case llvm::to_underlying (dxbc::ShaderVisibility::Geometry):
159
- case llvm::to_underlying (dxbc::ShaderVisibility::Pixel):
160
- case llvm::to_underlying (dxbc::ShaderVisibility::Amplification):
161
- case llvm::to_underlying (dxbc::ShaderVisibility::Mesh):
162
- return true ;
163
- }
164
-
165
- return false ;
166
- }
167
-
168
- static bool verifyParameterType (uint32_t Type) {
169
- switch (Type) {
170
- case llvm::to_underlying (dxbc::RootParameterType::Constants32Bit):
171
- return true ;
172
- }
173
-
174
- return false ;
175
- }
176
-
177
153
static bool verifyVersion (uint32_t Version) {
178
154
return (Version == 1 || Version == 2 );
179
155
}
@@ -188,12 +164,12 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
188
164
return reportValueError (Ctx, " RootFlags" , RSD.Flags );
189
165
}
190
166
191
- for (const auto &P : RSD.Parameters ) {
192
- if (!verifyShaderVisibility (P.Header .ShaderVisibility ))
167
+ for (const mcdxbc::RootParameter &P : RSD.Parameters ) {
168
+ if (!dxbc::isValidShaderVisibility (P.Header .ShaderVisibility ))
193
169
return reportValueError (Ctx, " ShaderVisibility" ,
194
- ( uint32_t ) P.Header .ShaderVisibility );
170
+ P.Header .ShaderVisibility );
195
171
196
- assert (verifyParameterType (P.Header .ParameterType ) &&
172
+ assert (dxbc::isValidParameterType (P.Header .ParameterType ) &&
197
173
" Invalid value for ParameterType" );
198
174
}
199
175
@@ -265,6 +241,10 @@ analyzeModule(Module &M) {
265
241
}
266
242
267
243
mcdxbc::RootSignatureDesc RSD;
244
+ // Clang emits the root signature data in dxcontainer following a specific
245
+ // sequence. First the header, then the root parameters. So the header
246
+ // offset will always equal to the header size.
247
+ RSD.RootParameterOffset = sizeof (dxbc::RootSignatureHeader);
268
248
269
249
if (parse (Ctx, RSD, RootElementListNode) || validate (Ctx, RSD)) {
270
250
return RSDMap;
@@ -291,7 +271,6 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
291
271
SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> &RSDMap =
292
272
AM.getResult <RootSignatureAnalysis>(M);
293
273
294
- const size_t RSHSize = sizeof (dxbc::RootSignatureHeader);
295
274
OS << " Root Signature Definitions"
296
275
<< " \n " ;
297
276
uint8_t Space = 0 ;
@@ -306,32 +285,30 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
306
285
Space++;
307
286
OS << indent (Space) << " Flags: " << format_hex (RS.Flags , 8 ) << " \n " ;
308
287
OS << indent (Space) << " Version: " << RS.Version << " \n " ;
309
- OS << indent (Space) << " NumParameters: " << RS.Parameters .size () << " \n " ;
310
- OS << indent (Space) << " RootParametersOffset: " << RSHSize << " \n " ;
311
- OS << indent (Space) << " NumStaticSamplers: " << 0 << " \n " ;
312
- OS << indent (Space)
313
- << " StaticSamplersOffset: " << RSHSize + RS.Parameters .size_in_bytes ()
288
+ OS << indent (Space) << " RootParametersOffset: " << RS.RootParameterOffset
314
289
<< " \n " ;
315
-
290
+ OS << indent (Space) << " NumParameters: " << RS. Parameters . size () << " \n " ;
316
291
Space++;
317
292
for (auto const &P : RS.Parameters ) {
318
- OS << indent (Space)
319
- << " Parameter Type: " << (uint32_t )P.Header .ParameterType << " \n " ;
320
- OS << indent (Space)
321
- << " Shader Visibility: " << (uint32_t )P.Header .ShaderVisibility
293
+ OS << indent (Space) << " - Parameter Type: " << P.Header .ParameterType
322
294
<< " \n " ;
295
+ OS << indent (Space + 2 )
296
+ << " Shader Visibility: " << P.Header .ShaderVisibility << " \n " ;
323
297
switch (P.Header .ParameterType ) {
324
298
case llvm::to_underlying (dxbc::RootParameterType::Constants32Bit):
325
- OS << indent (Space) << " Register Space: " << P. Constants . RegisterSpace
326
- << " \n " ;
327
- OS << indent (Space) << " Shader Register: " << P. Constants . ShaderRegister
328
- << " \n " ;
329
- OS << indent (Space)
299
+ OS << indent (Space + 2 )
300
+ << " Register Space: " << P. Constants . RegisterSpace << " \n " ;
301
+ OS << indent (Space + 2 )
302
+ << " Shader Register: " << P. Constants . ShaderRegister << " \n " ;
303
+ OS << indent (Space + 2 )
330
304
<< " Num 32 Bit Values: " << P.Constants .Num32BitValues << " \n " ;
331
305
break ;
332
306
}
333
307
}
334
308
Space--;
309
+ OS << indent (Space) << " NumStaticSamplers: " << 0 << " \n " ;
310
+ OS << indent (Space) << " StaticSamplersOffset: " << RS.StaticSamplersOffset
311
+ << " \n " ;
335
312
336
313
Space--;
337
314
// end root signature header
0 commit comments