Skip to content

Commit d67f7d3

Browse files
author
joaosaffran
committed
addressing comment
1 parent 8ff4845 commit d67f7d3

File tree

4 files changed

+76
-74
lines changed

4 files changed

+76
-74
lines changed

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct RootParametersContainer {
3535
SmallVector<RootParameterInfo> ParametersInfo;
3636

3737
SmallVector<dxbc::RootConstants> Constants;
38-
SmallVector<RootDescriptor> Descriptors;
38+
SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors;
3939

4040
void addInfo(dxbc::RootParameterHeader H, size_t L) {
4141
ParametersInfo.push_back(RootParameterInfo(H, L));
@@ -47,33 +47,28 @@ struct RootParametersContainer {
4747
}
4848

4949
void addParameter(dxbc::RootParameterHeader H,
50-
dxbc::RTS0::v1::RootDescriptor D) {
50+
dxbc::RTS0::v2::RootDescriptor D) {
5151
addInfo(H, Descriptors.size());
5252
Descriptors.push_back(D);
5353
}
5454

55-
void addParameter(dxbc::RootParameterHeader H,
56-
dxbc::RTS0::v2::RootDescriptor D) {
57-
addInfo(H, Descriptors.size());
58-
Descriptors.push_back(D);
55+
const std::pair<uint32_t, uint32_t>
56+
getTypeAndLocForParameter(uint32_t Index) const {
57+
const RootParameterInfo &Info = ParametersInfo[Index];
58+
return {Info.Header.ParameterType, Info.Location};
59+
}
60+
61+
const dxbc::RootParameterHeader &getHeader(size_t Index) const {
62+
const RootParameterInfo &Info = ParametersInfo[Index];
63+
return Info.Header;
64+
}
65+
66+
const dxbc::RootConstants &getConstant(size_t Index) const {
67+
return Constants[Index];
5968
}
6069

61-
std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
62-
switch (H->Header.ParameterType) {
63-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
64-
return &Constants[H->Location];
65-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
66-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
67-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
68-
const RootDescriptor &VersionedParam = Descriptors[H->Location];
69-
if (std::holds_alternative<dxbc::RTS0::v1::RootDescriptor>(
70-
VersionedParam)) {
71-
return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
72-
}
73-
return &std::get<dxbc::RTS0::v2::RootDescriptor>(VersionedParam);
74-
}
75-
76-
return std::nullopt;
70+
const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const {
71+
return Descriptors[Index];
7772
}
7873

7974
size_t size() const { return ParametersInfo.size(); }

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct RootParameterYamlDesc {
9494
uint32_t Type;
9595
uint32_t Visibility;
9696
uint32_t Offset;
97-
RootParameterYamlDesc(){};
97+
RootParameterYamlDesc() {};
9898
RootParameterYamlDesc(uint32_t T) : Type(T) {
9999
switch (T) {
100100

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/MC/DXContainerRootSignature.h"
10+
#include "llvm/ADT/STLForwardCompat.h"
1011
#include "llvm/ADT/SmallString.h"
12+
#include "llvm/BinaryFormat/DXContainer.h"
1113
#include "llvm/Support/EndianStream.h"
1214

1315
using namespace llvm;
@@ -33,15 +35,20 @@ size_t RootSignatureDesc::getSize() const {
3335
ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
3436

3537
for (const auto &I : ParametersContainer) {
36-
std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
37-
if (!P)
38-
continue;
39-
std::visit(
40-
[&Size](auto &Value) -> void {
41-
using T = std::decay_t<decltype(*Value)>;
42-
Size += sizeof(T);
43-
},
44-
*P);
38+
switch (I.Header.ParameterType) {
39+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
40+
Size += sizeof(dxbc::RootConstants);
41+
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):
45+
if (Version == 1)
46+
Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
47+
else
48+
Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
49+
50+
break;
51+
}
4552
}
4653

4754
return Size;
@@ -62,7 +69,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
6269
support::endian::write(BOS, Flags, llvm::endianness::little);
6370

6471
SmallVector<uint32_t> ParamsOffsets;
65-
for (const auto &P : ParametersContainer) {
72+
for (const RootParameterInfo &P : ParametersContainer) {
6673
support::endian::write(BOS, P.Header.ParameterType,
6774
llvm::endianness::little);
6875
support::endian::write(BOS, P.Header.ShaderVisibility,
@@ -75,35 +82,31 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
7582
const RootParameterInfo *H = ParametersContainer.begin();
7683
for (size_t I = 0; I < NumParameters; ++I, H++) {
7784
rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
78-
auto P = ParametersContainer.getParameter(H);
79-
if (!P)
80-
continue;
81-
if (std::holds_alternative<const dxbc::RootConstants *>(P.value())) {
82-
auto *Constants = std::get<const dxbc::RootConstants *>(P.value());
83-
support::endian::write(BOS, Constants->ShaderRegister,
84-
llvm::endianness::little);
85-
support::endian::write(BOS, Constants->RegisterSpace,
85+
const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
86+
switch (Type) {
87+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
88+
const dxbc::RootConstants Constants =
89+
ParametersContainer.getConstant(Loc);
90+
support::endian::write(BOS, Constants.ShaderRegister,
8691
llvm::endianness::little);
87-
support::endian::write(BOS, Constants->Num32BitValues,
92+
support::endian::write(BOS, Constants.RegisterSpace,
8893
llvm::endianness::little);
89-
} else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
90-
*P)) {
91-
auto *Descriptor =
92-
std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
93-
support::endian::write(BOS, Descriptor->ShaderRegister,
94+
support::endian::write(BOS, Constants.Num32BitValues,
9495
llvm::endianness::little);
95-
support::endian::write(BOS, Descriptor->RegisterSpace,
96-
llvm::endianness::little);
97-
} else if (std::holds_alternative<const dxbc::RTS0::v2::RootDescriptor *>(
98-
*P)) {
99-
auto *Descriptor =
100-
std::get<const dxbc::RTS0::v2::RootDescriptor *>(P.value());
96+
} break;
97+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
98+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
99+
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
100+
const dxbc::RTS0::v2::RootDescriptor Descriptor =
101+
ParametersContainer.getRootDescriptor(Loc);
101102

102-
support::endian::write(BOS, Descriptor->ShaderRegister,
103+
support::endian::write(BOS, Descriptor.ShaderRegister,
103104
llvm::endianness::little);
104-
support::endian::write(BOS, Descriptor->RegisterSpace,
105+
support::endian::write(BOS, Descriptor.RegisterSpace,
105106
llvm::endianness::little);
106-
support::endian::write(BOS, Descriptor->Flags, llvm::endianness::little);
107+
if (Version > 1)
108+
support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
109+
}
107110
}
108111
}
109112
assert(Storage.size() == getSize());

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 22 additions & 18 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"
@@ -27,6 +28,7 @@
2728
#include "llvm/Support/Error.h"
2829
#include "llvm/Support/ErrorHandling.h"
2930
#include "llvm/Support/raw_ostream.h"
31+
#include <cstddef>
3032
#include <cstdint>
3133
#include <optional>
3234
#include <utility>
@@ -291,35 +293,37 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
291293
OS << indent(Space) << "NumParameters: " << RS.ParametersContainer.size()
292294
<< "\n";
293295
Space++;
294-
for (auto const &Info : RS.ParametersContainer) {
295-
OS << indent(Space) << "- Parameter Type: " << Info.Header.ParameterType
296-
<< "\n";
296+
for (size_t I = 0; I < RS.ParametersContainer.size(); I++) {
297+
const auto &[Type, Loc] =
298+
RS.ParametersContainer.getTypeAndLocForParameter(I);
299+
const dxbc::RootParameterHeader Header =
300+
RS.ParametersContainer.getHeader(I);
301+
302+
OS << indent(Space) << "- Parameter Type: " << Type << "\n";
297303
OS << indent(Space + 2)
298-
<< "Shader Visibility: " << Info.Header.ShaderVisibility << "\n";
299-
std::optional<mcdxbc::ParametersView> P =
300-
RS.ParametersContainer.getParameter(&Info);
301-
if (!P)
302-
continue;
303-
if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
304-
auto *Constants = std::get<const dxbc::RootConstants *>(*P);
305-
OS << indent(Space + 2)
306-
<< "Register Space: " << Constants->RegisterSpace << "\n";
304+
<< "Shader Visibility: " << Header.ShaderVisibility << "\n";
305+
306+
switch (Type) {
307+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
308+
auto Constants = RS.ParametersContainer.getConstant(Loc);
309+
OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
310+
<< "\n";
307311
OS << indent(Space + 2)
308-
<< "Shader Register: " << Constants->ShaderRegister << "\n";
312+
<< "Shader Register: " << Constants.ShaderRegister << "\n";
309313
OS << indent(Space + 2)
310-
<< "Num 32 Bit Values: " << Constants->Num32BitValues << "\n";
314+
<< "Num 32 Bit Values: " << Constants.Num32BitValues << "\n";
315+
}
311316
}
317+
Space--;
312318
}
313-
Space--;
314319
OS << indent(Space) << "NumStaticSamplers: " << 0 << "\n";
315320
OS << indent(Space) << "StaticSamplersOffset: " << RS.StaticSamplersOffset
316321
<< "\n";
317322

318323
Space--;
319324
// end root signature header
320-
}
321-
322-
return PreservedAnalyses::all();
325+
}
326+
return PreservedAnalyses::all();
323327
}
324328

325329
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)