Skip to content

Commit a394ad0

Browse files
author
joaosaffran
committed
Merge branch 'obj2yaml/root-descriptors' into refactoring/remove-union
2 parents 46cc8c1 + a31e5a5 commit a394ad0

File tree

7 files changed

+52
-42
lines changed

7 files changed

+52
-42
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ struct ProgramSignatureElement {
585585

586586
static_assert(sizeof(ProgramSignatureElement) == 32,
587587
"ProgramSignatureElement is misaligned");
588-
namespace RST0 {
589-
namespace v0 {
588+
namespace RTS0 {
589+
namespace v1 {
590590
struct RootDescriptor {
591591
uint32_t ShaderRegister;
592592
uint32_t RegisterSpace;
@@ -595,18 +595,23 @@ struct RootDescriptor {
595595
sys::swapByteOrder(RegisterSpace);
596596
}
597597
};
598-
} // namespace v0
598+
} // namespace v1
599599

600-
namespace v1 {
601-
struct RootDescriptor : public v0::RootDescriptor {
600+
namespace v2 {
601+
struct RootDescriptor : public v1::RootDescriptor {
602602
uint32_t Flags;
603+
604+
RootDescriptor() = default;
605+
RootDescriptor(v1::RootDescriptor &Base)
606+
: v1::RootDescriptor(Base), Flags(0u) {}
607+
603608
void swapBytes() {
604-
v0::RootDescriptor::swapBytes();
609+
v1::RootDescriptor::swapBytes();
605610
sys::swapByteOrder(Flags);
606611
}
607612
};
608-
} // namespace v1
609-
} // namespace RST0
613+
} // namespace v2
614+
} // namespace RTS0
610615
// following dx12 naming
611616
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
612617
struct RootConstants {

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ struct RootParameterInfo {
2727
: Header(H), Location(L) {}
2828
};
2929

30-
using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
31-
dxbc::RST0::v1::RootDescriptor>;
30+
using RootDescriptor = std::variant<dxbc::RTS0::v1::RootDescriptor,
31+
dxbc::RTS0::v2::RootDescriptor>;
3232
using ParametersView = std::variant<const dxbc::RootConstants *,
33-
const dxbc::RST0::v0::RootDescriptor *,
34-
const dxbc::RST0::v1::RootDescriptor *>;
33+
const dxbc::RTS0::v1::RootDescriptor *,
34+
const dxbc::RTS0::v2::RootDescriptor *>;
3535
struct RootParametersContainer {
3636
SmallVector<RootParameterInfo> ParametersInfo;
3737

@@ -48,13 +48,13 @@ struct RootParametersContainer {
4848
}
4949

5050
void addParameter(dxbc::RootParameterHeader H,
51-
dxbc::RST0::v0::RootDescriptor D) {
51+
dxbc::RTS0::v1::RootDescriptor D) {
5252
addInfo(H, Descriptors.size());
5353
Descriptors.push_back(D);
5454
}
5555

5656
void addParameter(dxbc::RootParameterHeader H,
57-
dxbc::RST0::v1::RootDescriptor D) {
57+
dxbc::RTS0::v2::RootDescriptor D) {
5858
addInfo(H, Descriptors.size());
5959
Descriptors.push_back(D);
6060
}
@@ -67,11 +67,11 @@ struct RootParametersContainer {
6767
case llvm::to_underlying(dxbc::RootParameterType::SRV):
6868
case llvm::to_underlying(dxbc::RootParameterType::UAV):
6969
const RootDescriptor &VersionedParam = Descriptors[H->Location];
70-
if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
70+
if (std::holds_alternative<dxbc::RTS0::v1::RootDescriptor>(
7171
VersionedParam)) {
72-
return &std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
72+
return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
7373
}
74-
return &std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
74+
return &std::get<dxbc::RTS0::v2::RootDescriptor>(VersionedParam);
7575
}
7676

7777
return std::nullopt;

llvm/include/llvm/Object/DXContainer.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/ADT/StringRef.h"
20+
#include "llvm/ADT/Twine.h"
2021
#include "llvm/BinaryFormat/DXContainer.h"
2122
#include "llvm/Object/Error.h"
2223
#include "llvm/Support/Error.h"
2324
#include "llvm/Support/MemoryBufferRef.h"
2425
#include "llvm/TargetParser/Triple.h"
2526
#include <array>
2627
#include <cstddef>
28+
#include <cstdint>
2729
#include <variant>
2830

2931
namespace llvm {
@@ -121,19 +123,17 @@ namespace DirectX {
121123
struct RootParameterView {
122124
const dxbc::RootParameterHeader &Header;
123125
StringRef ParamData;
124-
RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
126+
127+
RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
125128
: Header(H), ParamData(P) {}
126129

127-
template <typename T, typename VersionT = T> Expected<T> readParameter() {
128-
assert(sizeof(VersionT) <= sizeof(T) &&
129-
"Parameter of higher version must inherit all previous version data "
130-
"members");
131-
if (sizeof(VersionT) != ParamData.size())
130+
template <typename T> Expected<T> readParameter() {
131+
T Struct;
132+
if (sizeof(T) != ParamData.size())
132133
return make_error<GenericBinaryError>(
133134
"Reading structure out of file bounds", object_error::parse_failed);
134135

135-
T Struct;
136-
memcpy(&Struct, ParamData.data(), sizeof(VersionT));
136+
memcpy(&Struct, ParamData.data(), sizeof(T));
137137
// DXContainer is always little endian
138138
if (sys::IsBigEndianHost)
139139
Struct.swapBytes();
@@ -162,11 +162,18 @@ struct RootDescriptorView : RootParameterView {
162162
llvm::to_underlying(dxbc::RootParameterType::UAV));
163163
}
164164

165-
llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) {
166-
if (Version == 1)
167-
return readParameter<dxbc::RST0::v1::RootDescriptor,
168-
dxbc::RST0::v0::RootDescriptor>();
169-
return readParameter<dxbc::RST0::v1::RootDescriptor>();
165+
llvm::Expected<dxbc::RTS0::v2::RootDescriptor> read(uint32_t Version) {
166+
if (Version == 1) {
167+
auto Descriptor = readParameter<dxbc::RTS0::v1::RootDescriptor>();
168+
if (Error E = Descriptor.takeError())
169+
return E;
170+
return dxbc::RTS0::v2::RootDescriptor(*Descriptor);
171+
}
172+
if (Version != 2)
173+
return make_error<GenericBinaryError>("Invalid Root Signature version: " +
174+
Twine(Version),
175+
object_error::parse_failed);
176+
return readParameter<dxbc::RTS0::v2::RootDescriptor>();
170177
}
171178
};
172179

@@ -217,9 +224,9 @@ class RootSignature {
217224
case dxbc::RootParameterType::SRV:
218225
case dxbc::RootParameterType::UAV:
219226
if (Version == 1)
220-
DataSize = sizeof(dxbc::RST0::v0::RootDescriptor);
227+
DataSize = sizeof(dxbc::RTS0::v1::RootDescriptor);
221228
else
222-
DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
229+
DataSize = sizeof(dxbc::RTS0::v2::RootDescriptor);
223230
break;
224231
}
225232
size_t EndOfSectionByte = getNumStaticSamplers() == 0
@@ -230,7 +237,7 @@ class RootSignature {
230237
return parseFailed("Reading structure out of file bounds");
231238

232239
StringRef Buff = PartData.substr(Header.ParameterOffset, DataSize);
233-
RootParameterView View = RootParameterView(Version, Header, Buff);
240+
RootParameterView View = RootParameterView(Header, Buff);
234241
return View;
235242
}
236243
};

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "llvm/ObjectYAML/YAML.h"
2222
#include "llvm/Support/YAMLTraits.h"
2323
#include <array>
24-
#include <cstdint>
2524
#include <optional>
2625
#include <string>
2726
#include <vector>

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
8686
llvm::endianness::little);
8787
support::endian::write(BOS, Constants->Num32BitValues,
8888
llvm::endianness::little);
89-
} else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor *>(
89+
} else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
9090
*P)) {
9191
auto *Descriptor =
92-
std::get<const dxbc::RST0::v0::RootDescriptor *>(P.value());
92+
std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
9393
support::endian::write(BOS, Descriptor->ShaderRegister,
9494
llvm::endianness::little);
9595
support::endian::write(BOS, Descriptor->RegisterSpace,
9696
llvm::endianness::little);
97-
} else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor *>(
97+
} else if (std::holds_alternative<const dxbc::RTS0::v2::RootDescriptor *>(
9898
*P)) {
9999
auto *Descriptor =
100-
std::get<const dxbc::RST0::v1::RootDescriptor *>(P.value());
100+
std::get<const dxbc::RTS0::v2::RootDescriptor *>(P.value());
101101

102102
support::endian::write(BOS, Descriptor->ShaderRegister,
103103
llvm::endianness::little);

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
289289
case llvm::to_underlying(dxbc::RootParameterType::UAV):
290290
case llvm::to_underlying(dxbc::RootParameterType::CBV):
291291
if (RS.Version == 1) {
292-
dxbc::RST0::v0::RootDescriptor Descriptor;
292+
dxbc::RTS0::v1::RootDescriptor Descriptor;
293293
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
294294
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
295295
RS.ParametersContainer.addParameter(Header, Descriptor);
296296
} else {
297-
dxbc::RST0::v1::RootDescriptor Descriptor;
297+
dxbc::RTS0::v2::RootDescriptor Descriptor;
298298
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
299299
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
300300
Descriptor.Flags = Param.Descriptor.getEncodedFlags();

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvm/ADT/STLForwardCompat.h"
1616
#include "llvm/ADT/ScopeExit.h"
1717
#include "llvm/BinaryFormat/DXContainer.h"
18-
#include "llvm/Object/DXContainer.h"
1918
#include "llvm/Support/Error.h"
2019
#include "llvm/Support/ScopedPrinter.h"
2120
#include <cstdint>
@@ -82,7 +81,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
8281
NewP.Constants.RegisterSpace = Constants.RegisterSpace;
8382
} else if (auto *RDV =
8483
dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
85-
llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
84+
llvm::Expected<dxbc::RTS0::v2::RootDescriptor> DescriptorOrErr =
8685
RDV->read(Version);
8786
if (Error E = DescriptorOrErr.takeError())
8887
return std::move(E);

0 commit comments

Comments
 (0)