12
12
// ===----------------------------------------------------------------------===//
13
13
#include " DXILRootSignature.h"
14
14
#include " DirectX.h"
15
+ #include " llvm/ADT/STLForwardCompat.h"
15
16
#include " llvm/ADT/StringSwitch.h"
16
17
#include " llvm/ADT/Twine.h"
17
18
#include " llvm/Analysis/DXILMetadataAnalysis.h"
@@ -57,7 +58,7 @@ static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
57
58
58
59
static std::optional<StringRef> extractMdStringValue (MDNode *Node,
59
60
unsigned int OpId) {
60
- MDString *NodeText = cast <MDString>(Node->getOperand (OpId));
61
+ MDString *NodeText = dyn_cast <MDString>(Node->getOperand (OpId));
61
62
if (NodeText == nullptr )
62
63
return std::nullopt;
63
64
return NodeText->getString ();
@@ -117,25 +118,31 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
117
118
118
119
static bool parseRootDescriptors (LLVMContext *Ctx,
119
120
mcdxbc::RootSignatureDesc &RSD,
120
- MDNode *RootDescriptorNode) {
121
-
121
+ MDNode *RootDescriptorNode,
122
+ RootSignatureElementKind ElementKind) {
123
+ assert (ElementKind == RootSignatureElementKind::SRV ||
124
+ ElementKind == RootSignatureElementKind::UAV ||
125
+ ElementKind == RootSignatureElementKind::CBV &&
126
+ " parseRootDescriptors should only be called with RootDescriptor "
127
+ " element kind." );
122
128
if (RootDescriptorNode->getNumOperands () != 5 )
123
129
return reportError (Ctx, " Invalid format for Root Descriptor Element" );
124
130
125
- std::optional<StringRef> ElementText =
126
- extractMdStringValue (RootDescriptorNode, 0 );
127
-
128
- if (!ElementText.has_value ())
129
- return reportError (Ctx, " Root Descriptor, first element is not a string." );
130
-
131
131
dxbc::RTS0::v1::RootParameterHeader Header;
132
- // a default scenario is not needed here. Scenarios where ElementText is
133
- // invalid is previously checked and error handled when this method is called.
134
- Header.ParameterType =
135
- StringSwitch<uint32_t >(*ElementText)
136
- .Case (" RootCBV" , llvm::to_underlying (dxbc::RootParameterType::CBV))
137
- .Case (" RootSRV" , llvm::to_underlying (dxbc::RootParameterType::SRV))
138
- .Case (" RootUAV" , llvm::to_underlying (dxbc::RootParameterType::UAV));
132
+ switch (ElementKind) {
133
+ case RootSignatureElementKind::SRV:
134
+ Header.ParameterType = llvm::to_underlying (dxbc::RootParameterType::SRV);
135
+ break ;
136
+ case RootSignatureElementKind::UAV:
137
+ Header.ParameterType = llvm::to_underlying (dxbc::RootParameterType::UAV);
138
+ break ;
139
+ case RootSignatureElementKind::CBV:
140
+ Header.ParameterType = llvm::to_underlying (dxbc::RootParameterType::CBV);
141
+ break ;
142
+ default :
143
+ llvm_unreachable (" invalid Root Descriptor kind" );
144
+ break ;
145
+ }
139
146
140
147
if (std::optional<uint32_t > Val = extractMdIntValue (RootDescriptorNode, 1 ))
141
148
Header.ShaderVisibility = *Val;
@@ -171,17 +178,17 @@ static bool parseRootDescriptors(LLVMContext *Ctx,
171
178
static bool parseRootSignatureElement (LLVMContext *Ctx,
172
179
mcdxbc::RootSignatureDesc &RSD,
173
180
MDNode *Element) {
174
- MDString * ElementText = cast<MDString> (Element-> getOperand ( 0 ) );
175
- if (ElementText == nullptr )
181
+ std::optional<StringRef> ElementText = extractMdStringValue (Element, 0 );
182
+ if (! ElementText. has_value () )
176
183
return reportError (Ctx, " Invalid format for Root Element" );
177
184
178
185
RootSignatureElementKind ElementKind =
179
- StringSwitch<RootSignatureElementKind>(ElementText-> getString () )
186
+ StringSwitch<RootSignatureElementKind>(* ElementText)
180
187
.Case (" RootFlags" , RootSignatureElementKind::RootFlags)
181
188
.Case (" RootConstants" , RootSignatureElementKind::RootConstants)
182
- .Case (" RootCBV" , RootSignatureElementKind::RootDescriptors )
183
- .Case (" RootSRV" , RootSignatureElementKind::RootDescriptors )
184
- .Case (" RootUAV" , RootSignatureElementKind::RootDescriptors )
189
+ .Case (" RootCBV" , RootSignatureElementKind::CBV )
190
+ .Case (" RootSRV" , RootSignatureElementKind::SRV )
191
+ .Case (" RootUAV" , RootSignatureElementKind::UAV )
185
192
.Default (RootSignatureElementKind::Error);
186
193
187
194
switch (ElementKind) {
@@ -190,11 +197,12 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
190
197
return parseRootFlags (Ctx, RSD, Element);
191
198
case RootSignatureElementKind::RootConstants:
192
199
return parseRootConstants (Ctx, RSD, Element);
193
- case RootSignatureElementKind::RootDescriptors:
194
- return parseRootDescriptors (Ctx, RSD, Element);
200
+ case RootSignatureElementKind::CBV:
201
+ case RootSignatureElementKind::SRV:
202
+ case RootSignatureElementKind::UAV:
203
+ return parseRootDescriptors (Ctx, RSD, Element, ElementKind);
195
204
case RootSignatureElementKind::Error:
196
- return reportError (Ctx, " Invalid Root Signature Element: " +
197
- ElementText->getString ());
205
+ return reportError (Ctx, " Invalid Root Signature Element: " + *ElementText);
198
206
}
199
207
200
208
llvm_unreachable (" Unhandled RootSignatureElementKind enum." );
@@ -223,7 +231,7 @@ static bool verifyVersion(uint32_t Version) {
223
231
}
224
232
225
233
static bool verifyRegisterValue (uint32_t RegisterValue) {
226
- return !( RegisterValue == 0xFFFFFFFF ) ;
234
+ return RegisterValue != ~ 0U ;
227
235
}
228
236
229
237
static bool verifyRegisterSpace (uint32_t RegisterSpace) {
0 commit comments