Skip to content

Commit d1ca37d

Browse files
author
joaosaffran
committed
addressing PR comments
1 parent 0c570c8 commit d1ca37d

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313
#include "DXILRootSignature.h"
1414
#include "DirectX.h"
15+
#include "llvm/ADT/STLForwardCompat.h"
1516
#include "llvm/ADT/StringSwitch.h"
1617
#include "llvm/ADT/Twine.h"
1718
#include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -57,7 +58,7 @@ static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
5758

5859
static std::optional<StringRef> extractMdStringValue(MDNode *Node,
5960
unsigned int OpId) {
60-
MDString *NodeText = cast<MDString>(Node->getOperand(OpId));
61+
MDString *NodeText = dyn_cast<MDString>(Node->getOperand(OpId));
6162
if (NodeText == nullptr)
6263
return std::nullopt;
6364
return NodeText->getString();
@@ -117,25 +118,31 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
117118

118119
static bool parseRootDescriptors(LLVMContext *Ctx,
119120
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.");
122128
if (RootDescriptorNode->getNumOperands() != 5)
123129
return reportError(Ctx, "Invalid format for Root Descriptor Element");
124130

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-
131131
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+
}
139146

140147
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 1))
141148
Header.ShaderVisibility = *Val;
@@ -171,17 +178,17 @@ static bool parseRootDescriptors(LLVMContext *Ctx,
171178
static bool parseRootSignatureElement(LLVMContext *Ctx,
172179
mcdxbc::RootSignatureDesc &RSD,
173180
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())
176183
return reportError(Ctx, "Invalid format for Root Element");
177184

178185
RootSignatureElementKind ElementKind =
179-
StringSwitch<RootSignatureElementKind>(ElementText->getString())
186+
StringSwitch<RootSignatureElementKind>(*ElementText)
180187
.Case("RootFlags", RootSignatureElementKind::RootFlags)
181188
.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)
185192
.Default(RootSignatureElementKind::Error);
186193

187194
switch (ElementKind) {
@@ -190,11 +197,12 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
190197
return parseRootFlags(Ctx, RSD, Element);
191198
case RootSignatureElementKind::RootConstants:
192199
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);
195204
case RootSignatureElementKind::Error:
196-
return reportError(Ctx, "Invalid Root Signature Element: " +
197-
ElementText->getString());
205+
return reportError(Ctx, "Invalid Root Signature Element: " + *ElementText);
198206
}
199207

200208
llvm_unreachable("Unhandled RootSignatureElementKind enum.");
@@ -223,7 +231,7 @@ static bool verifyVersion(uint32_t Version) {
223231
}
224232

225233
static bool verifyRegisterValue(uint32_t RegisterValue) {
226-
return !(RegisterValue == 0xFFFFFFFF);
234+
return RegisterValue != ~0U;
227235
}
228236

229237
static bool verifyRegisterSpace(uint32_t RegisterSpace) {

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ enum class RootSignatureElementKind {
2828
Error = 0,
2929
RootFlags = 1,
3030
RootConstants = 2,
31-
RootDescriptors = 3
31+
SRV = 3,
32+
UAV = 4,
33+
CBV = 5,
3234
};
3335
class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
3436
friend AnalysisInfoMixin<RootSignatureAnalysis>;

0 commit comments

Comments
 (0)