Skip to content

Commit ea35c41

Browse files
Joao SaffranJoao Saffran
authored andcommitted
remove the to_undernlying switch
1 parent 0efcb83 commit ea35c41

File tree

6 files changed

+72
-44
lines changed

6 files changed

+72
-44
lines changed

llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,14 @@ bool MetadataParser::validateRootSignature(
559559
assert(dxbc::isValidParameterType(Info.Header.ParameterType) &&
560560
"Invalid value for ParameterType");
561561

562-
switch (Info.Header.ParameterType) {
562+
dxbc::RootParameterType PT =
563+
static_cast<dxbc::RootParameterType>(Info.Header.ParameterType);
563564

564-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
565-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
566-
case llvm::to_underlying(dxbc::RootParameterType::SRV): {
565+
switch (PT) {
566+
567+
case dxbc::RootParameterType::CBV:
568+
case dxbc::RootParameterType::UAV:
569+
case dxbc::RootParameterType::SRV: {
567570
const dxbc::RTS0::v2::RootDescriptor &Descriptor =
568571
RSD.ParametersContainer.getRootDescriptor(Info.Location);
569572
if (!llvm::hlsl::rootsig::verifyRegisterValue(Descriptor.ShaderRegister))
@@ -580,7 +583,7 @@ bool MetadataParser::validateRootSignature(
580583
}
581584
break;
582585
}
583-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
586+
case dxbc::RootParameterType::DescriptorTable: {
584587
const mcdxbc::DescriptorTable &Table =
585588
RSD.ParametersContainer.getDescriptorTable(Info.Location);
586589
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) {

llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal) {
5353

5454
bool verifyRangeType(uint32_t Type) {
5555
switch (Type) {
56-
case llvm::to_underlying(dxbc::DescriptorRangeType::CBV):
57-
case llvm::to_underlying(dxbc::DescriptorRangeType::SRV):
58-
case llvm::to_underlying(dxbc::DescriptorRangeType::UAV):
59-
case llvm::to_underlying(dxbc::DescriptorRangeType::Sampler):
56+
#define DESCRIPTOR_RANGE(Num, Val) \
57+
case llvm::to_underlying(dxbc::DescriptorRangeType::Val):
58+
#include "llvm/BinaryFormat/DXContainerConstants.def"
6059
return true;
6160
};
6261

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/MC/DXContainerRootSignature.h"
1010
#include "llvm/ADT/SmallString.h"
11+
#include "llvm/BinaryFormat/DXContainer.h"
1112
#include "llvm/Support/EndianStream.h"
1213

1314
using namespace llvm;
@@ -35,20 +36,27 @@ size_t RootSignatureDesc::getSize() const {
3536
StaticSamplers.size() * sizeof(dxbc::RTS0::v1::StaticSampler);
3637

3738
for (const RootParameterInfo &I : ParametersContainer) {
38-
switch (I.Header.ParameterType) {
39-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
39+
// Invalid parameters are allowed while writing.
40+
if (!dxbc::isValidParameterType(I.Header.ParameterType))
41+
continue;
42+
43+
dxbc::RootParameterType PT =
44+
static_cast<dxbc::RootParameterType>(I.Header.ParameterType);
45+
46+
switch (PT) {
47+
case dxbc::RootParameterType::Constants32Bit:
4048
Size += sizeof(dxbc::RTS0::v1::RootConstants);
4149
break;
42-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
43-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
44-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
50+
case dxbc::RootParameterType::CBV:
51+
case dxbc::RootParameterType::SRV:
52+
case dxbc::RootParameterType::UAV:
4553
if (Version == 1)
4654
Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
4755
else
4856
Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
4957

5058
break;
51-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
59+
case dxbc::RootParameterType::DescriptorTable:
5260
const DescriptorTable &Table =
5361
ParametersContainer.getDescriptorTable(I.Location);
5462

@@ -97,8 +105,12 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
97105
for (size_t I = 0; I < NumParameters; ++I) {
98106
rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
99107
const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
100-
switch (Type) {
101-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
108+
if (!dxbc::isValidParameterType(Type))
109+
continue;
110+
dxbc::RootParameterType PT = static_cast<dxbc::RootParameterType>(Type);
111+
112+
switch (PT) {
113+
case dxbc::RootParameterType::Constants32Bit: {
102114
const dxbc::RTS0::v1::RootConstants &Constants =
103115
ParametersContainer.getConstant(Loc);
104116
support::endian::write(BOS, Constants.ShaderRegister,
@@ -109,9 +121,9 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
109121
llvm::endianness::little);
110122
break;
111123
}
112-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
113-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
114-
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
124+
case dxbc::RootParameterType::CBV:
125+
case dxbc::RootParameterType::SRV:
126+
case dxbc::RootParameterType::UAV: {
115127
const dxbc::RTS0::v2::RootDescriptor &Descriptor =
116128
ParametersContainer.getRootDescriptor(Loc);
117129

@@ -123,7 +135,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
123135
support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
124136
break;
125137
}
126-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
138+
case dxbc::RootParameterType::DescriptorTable: {
127139
const DescriptorTable &Table =
128140
ParametersContainer.getDescriptorTable(Loc);
129141
support::endian::write(BOS, (uint32_t)Table.Ranges.size(),

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,19 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
278278
dxbc::RTS0::v1::RootParameterHeader Header{L.Header.Type, L.Header.Visibility,
279279
L.Header.Offset};
280280

281-
switch (L.Header.Type) {
282-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
281+
if (!dxbc::isValidParameterType(L.Header.Type)) {
282+
// Handling invalid parameter type edge case. We intentionally let
283+
// obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
284+
// for that to be used as a testing tool more effectively.
285+
RS.ParametersContainer.addInvalidParameter(Header);
286+
continue;
287+
}
288+
289+
dxbc::RootParameterType ParameterType =
290+
static_cast<dxbc::RootParameterType>(L.Header.Type);
291+
292+
switch (ParameterType) {
293+
case dxbc::RootParameterType::Constants32Bit: {
283294
const DXContainerYAML::RootConstantsYaml &ConstantYaml =
284295
P.RootSignature->Parameters.getOrInsertConstants(L);
285296
dxbc::RTS0::v1::RootConstants Constants;
@@ -289,9 +300,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
289300
RS.ParametersContainer.addParameter(Header, Constants);
290301
break;
291302
}
292-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
293-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
294-
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
303+
case dxbc::RootParameterType::CBV:
304+
case dxbc::RootParameterType::SRV:
305+
case dxbc::RootParameterType::UAV: {
295306
const DXContainerYAML::RootDescriptorYaml &DescriptorYaml =
296307
P.RootSignature->Parameters.getOrInsertDescriptor(L);
297308

@@ -303,7 +314,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
303314
RS.ParametersContainer.addParameter(Header, Descriptor);
304315
break;
305316
}
306-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
317+
case dxbc::RootParameterType::DescriptorTable: {
307318
const DXContainerYAML::DescriptorTableYaml &TableYaml =
308319
P.RootSignature->Parameters.getOrInsertTable(L);
309320
mcdxbc::DescriptorTable Table;
@@ -323,11 +334,6 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
323334
RS.ParametersContainer.addParameter(Header, Table);
324335
break;
325336
}
326-
default:
327-
// Handling invalid parameter type edge case. We intentionally let
328-
// obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
329-
// for that to be used as a testing tool more effectively.
330-
RS.ParametersContainer.addInvalidParameter(Header);
331337
}
332338
}
333339

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,22 +424,28 @@ void MappingContextTraits<DXContainerYAML::RootParameterLocationYaml,
424424
IO.mapRequired("ParameterType", L.Header.Type);
425425
IO.mapRequired("ShaderVisibility", L.Header.Visibility);
426426

427-
switch (L.Header.Type) {
428-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
427+
if (!dxbc::isValidParameterType(L.Header.Type))
428+
return;
429+
dxbc::RootParameterType PT =
430+
static_cast<dxbc::RootParameterType>(L.Header.Type);
431+
432+
// We allow ParameterType to be invalid here.
433+
switch (PT) {
434+
case dxbc::RootParameterType::Constants32Bit: {
429435
DXContainerYAML::RootConstantsYaml &Constants =
430436
S.Parameters.getOrInsertConstants(L);
431437
IO.mapRequired("Constants", Constants);
432438
break;
433439
}
434-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
435-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
436-
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
440+
case dxbc::RootParameterType::CBV:
441+
case dxbc::RootParameterType::SRV:
442+
case dxbc::RootParameterType::UAV: {
437443
DXContainerYAML::RootDescriptorYaml &Descriptor =
438444
S.Parameters.getOrInsertDescriptor(L);
439445
IO.mapRequired("Descriptor", Descriptor);
440446
break;
441447
}
442-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
448+
case dxbc::RootParameterType::DescriptorTable: {
443449
DXContainerYAML::DescriptorTableYaml &Table =
444450
S.Parameters.getOrInsertTable(L);
445451
IO.mapRequired("Table", Table);

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,20 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
175175
OS << "- Parameter Type: " << Type << "\n"
176176
<< " Shader Visibility: " << Header.ShaderVisibility << "\n";
177177

178-
switch (Type) {
179-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
178+
assert(dxbc::isValidParameterType(Type) && "Invalid Parameter Type");
179+
dxbc::RootParameterType PT = static_cast<dxbc::RootParameterType>(Type);
180+
switch (PT) {
181+
case dxbc::RootParameterType::Constants32Bit: {
180182
const dxbc::RTS0::v1::RootConstants &Constants =
181183
RS.ParametersContainer.getConstant(Loc);
182184
OS << " Register Space: " << Constants.RegisterSpace << "\n"
183185
<< " Shader Register: " << Constants.ShaderRegister << "\n"
184186
<< " Num 32 Bit Values: " << Constants.Num32BitValues << "\n";
185187
break;
186188
}
187-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
188-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
189-
case llvm::to_underlying(dxbc::RootParameterType::SRV): {
189+
case dxbc::RootParameterType::CBV:
190+
case dxbc::RootParameterType::UAV:
191+
case dxbc::RootParameterType::SRV: {
190192
const dxbc::RTS0::v2::RootDescriptor &Descriptor =
191193
RS.ParametersContainer.getRootDescriptor(Loc);
192194
OS << " Register Space: " << Descriptor.RegisterSpace << "\n"
@@ -195,7 +197,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
195197
OS << " Flags: " << Descriptor.Flags << "\n";
196198
break;
197199
}
198-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
200+
case dxbc::RootParameterType::DescriptorTable: {
199201
const mcdxbc::DescriptorTable &Table =
200202
RS.ParametersContainer.getDescriptorTable(Loc);
201203
OS << " NumRanges: " << Table.Ranges.size() << "\n";

0 commit comments

Comments
 (0)