Skip to content

Commit e8ab8f7

Browse files
author
joaosaffran
committed
addressing commets
1 parent aabd424 commit e8ab8f7

File tree

3 files changed

+103
-67
lines changed

3 files changed

+103
-67
lines changed

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ObjectYAML/YAML.h"
2222
#include "llvm/Support/YAMLTraits.h"
2323
#include <array>
24+
#include <cstdint>
2425
#include <optional>
2526
#include <string>
2627
#include <vector>
@@ -90,18 +91,54 @@ struct RootDescriptorYaml {
9091
#include "llvm/BinaryFormat/DXContainerConstants.def"
9192
};
9293

93-
using ParameterData = std::variant<RootConstantsYaml, RootDescriptorYaml>;
94-
95-
struct RootParameterYamlDesc {
94+
struct RootParameterHeaderYaml {
9695
uint32_t Type;
9796
uint32_t Visibility;
9897
uint32_t Offset;
99-
ParameterData Data;
10098

101-
RootParameterYamlDesc() {};
102-
RootParameterYamlDesc(uint32_t T) : Type(T) {}
99+
RootParameterHeaderYaml() {};
100+
RootParameterHeaderYaml(uint32_t T) : Type(T) {}
101+
};
102+
103+
struct RootParameterLocationYaml {
104+
RootParameterHeaderYaml Header;
105+
std::optional<size_t> IndexInSignature;
106+
107+
RootParameterLocationYaml() {};
108+
explicit RootParameterLocationYaml(RootParameterHeaderYaml Header) : Header(Header) {}
103109
};
104110

111+
struct RootParameterYamlDesc {
112+
SmallVector<RootParameterLocationYaml> Locations;
113+
114+
SmallVector<RootConstantsYaml> Constants;
115+
SmallVector<RootDescriptorYaml> Descriptors;
116+
117+
118+
template <typename T>
119+
T &getOrInsertImpl(RootParameterLocationYaml &ParamDesc,
120+
SmallVectorImpl<T> &Container) {
121+
if (!ParamDesc.IndexInSignature) {
122+
ParamDesc.IndexInSignature = Container.size();
123+
Container.emplace_back();
124+
}
125+
return Container[*ParamDesc.IndexInSignature];
126+
}
127+
128+
RootConstantsYaml &getOrInsertConstants(RootParameterLocationYaml &ParamDesc) {
129+
return getOrInsertImpl(ParamDesc, Constants);
130+
}
131+
132+
RootDescriptorYaml &getOrInsertDescriptor(RootParameterLocationYaml &ParamDesc) {
133+
return getOrInsertImpl(ParamDesc, Descriptors);
134+
}
135+
136+
void addLocation(RootParameterLocationYaml &Location){
137+
Locations.push_back(Location);
138+
}
139+
};
140+
141+
105142
struct RootSignatureYamlDesc {
106143
RootSignatureYamlDesc() = default;
107144

@@ -111,14 +148,10 @@ struct RootSignatureYamlDesc {
111148
uint32_t NumStaticSamplers;
112149
uint32_t StaticSamplersOffset;
113150

114-
SmallVector<RootParameterYamlDesc> Parameters;
151+
RootParameterYamlDesc Parameters;
115152

116153
uint32_t getEncodedFlags();
117154

118-
iterator_range<RootParameterYamlDesc *> params() {
119-
return make_range(Parameters.begin(), Parameters.end());
120-
}
121-
122155
static llvm::Expected<DXContainerYAML::RootSignatureYamlDesc>
123156
create(const object::DirectX::RootSignature &Data);
124157

@@ -229,7 +262,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::ResourceBindInfo)
229262
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureElement)
230263
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector)
231264
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter)
232-
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::RootParameterYamlDesc)
265+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::RootParameterLocationYaml)
233266
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind)
234267
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType)
235268
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode)
@@ -302,8 +335,8 @@ template <> struct MappingTraits<DXContainerYAML::RootSignatureYamlDesc> {
302335
DXContainerYAML::RootSignatureYamlDesc &RootSignature);
303336
};
304337

305-
template <> struct MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc> {
306-
static void mapping(IO &IO, llvm::DXContainerYAML::RootParameterYamlDesc &P);
338+
template <> struct MappingContextTraits<DXContainerYAML::RootParameterLocationYaml, DXContainerYAML::RootSignatureYamlDesc> {
339+
static void mapping(IO &IO, llvm::DXContainerYAML::RootParameterLocationYaml &L, DXContainerYAML::RootSignatureYamlDesc &S);
307340
};
308341

309342
template <> struct MappingTraits<llvm::DXContainerYAML::RootConstantsYaml> {

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
///
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/ADT/STLForwardCompat.h"
1415
#include "llvm/BinaryFormat/DXContainer.h"
1516
#include "llvm/MC/DXContainerPSVInfo.h"
1617
#include "llvm/MC/DXContainerRootSignature.h"
18+
#include "llvm/ObjectYAML/DXContainerYAML.h"
1719
#include "llvm/ObjectYAML/ObjectYAML.h"
1820
#include "llvm/ObjectYAML/yaml2obj.h"
1921
#include "llvm/Support/Errc.h"
@@ -273,27 +275,33 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
273275
RS.NumStaticSamplers = P.RootSignature->NumStaticSamplers;
274276
RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
275277

276-
for (const auto &Param : P.RootSignature->Parameters) {
277-
auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
278-
Param.Offset};
278+
for (DXContainerYAML::RootParameterLocationYaml &L : P.RootSignature->Parameters.Locations) {
279+
auto Header = dxbc::RootParameterHeader{L.Header.Type, L.Header.Visibility,
280+
L.Header.Offset};
279281

280-
if (auto *ConstantYaml =
281-
std::get_if<DXContainerYAML::RootConstantsYaml>(&Param.Data)) {
282+
switch(L.Header.Type) {
283+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
284+
DXContainerYAML::RootConstantsYaml ConstantYaml = P.RootSignature->Parameters.getOrInsertConstants(L);
282285
dxbc::RootConstants Constants;
283-
Constants.Num32BitValues = ConstantYaml->Num32BitValues;
284-
Constants.RegisterSpace = ConstantYaml->RegisterSpace;
285-
Constants.ShaderRegister = ConstantYaml->ShaderRegister;
286+
Constants.Num32BitValues = ConstantYaml.Num32BitValues;
287+
Constants.RegisterSpace = ConstantYaml.RegisterSpace;
288+
Constants.ShaderRegister = ConstantYaml.ShaderRegister;
286289
RS.ParametersContainer.addParameter(Header, Constants);
287-
} else if (auto *DescriptorYaml =
288-
std::get_if<DXContainerYAML::RootDescriptorYaml>(
289-
&Param.Data)) {
290+
break;
291+
}
292+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
293+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
294+
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
295+
DXContainerYAML::RootDescriptorYaml DescriptorYaml = P.RootSignature->Parameters.getOrInsertDescriptor(L);
296+
290297
dxbc::RTS0::v2::RootDescriptor Descriptor;
291-
Descriptor.RegisterSpace = DescriptorYaml->RegisterSpace;
292-
Descriptor.ShaderRegister = DescriptorYaml->ShaderRegister;
298+
Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
299+
Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
293300
if (RS.Version > 1)
294-
Descriptor.Flags = DescriptorYaml->getEncodedFlags();
301+
Descriptor.Flags = DescriptorYaml.getEncodedFlags();
295302
RS.ParametersContainer.addParameter(Header, Descriptor);
296-
} else {
303+
break;
304+
} default:
297305
// Handling invalid parameter type edge case. We intentionally let
298306
// obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
299307
// for that to be used as a testing tool more effectively.

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ DXContainerYAML::RootSignatureYamlDesc::create(
5353
return createStringError(std::errc::invalid_argument,
5454
"Invalid value for parameter type");
5555

56-
RootParameterYamlDesc NewP(PH.ParameterType);
57-
NewP.Offset = PH.ParameterOffset;
58-
NewP.Type = PH.ParameterType;
56+
RootParameterHeaderYaml Header(PH.ParameterType);
57+
Header.Offset = PH.ParameterOffset;
58+
Header.Type = PH.ParameterType;
5959

6060
if (!dxbc::isValidShaderVisibility(PH.ShaderVisibility))
6161
return createStringError(std::errc::invalid_argument,
6262
"Invalid value for shader visibility");
6363

64-
NewP.Visibility = PH.ShaderVisibility;
64+
Header.Visibility = PH.ShaderVisibility;
6565

6666
llvm::Expected<object::DirectX::RootParameterView> ParamViewOrErr =
6767
Data.getParameter(PH);
@@ -75,19 +75,24 @@ DXContainerYAML::RootSignatureYamlDesc::create(
7575
return std::move(E);
7676

7777
auto Constants = *ConstantsOrErr;
78-
RootConstantsYaml ConstantYaml;
78+
RootParameterLocationYaml Location (Header);
79+
RootConstantsYaml &ConstantYaml = RootSigDesc.Parameters.getOrInsertConstants(Location);
80+
RootSigDesc.Parameters.addLocation(Location);
7981
ConstantYaml.Num32BitValues = Constants.Num32BitValues;
8082
ConstantYaml.ShaderRegister = Constants.ShaderRegister;
8183
ConstantYaml.RegisterSpace = Constants.RegisterSpace;
82-
NewP.Data = ConstantYaml;
84+
8385
} else if (auto *RDV =
8486
dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
8587
llvm::Expected<dxbc::RTS0::v2::RootDescriptor> DescriptorOrErr =
8688
RDV->read(Version);
8789
if (Error E = DescriptorOrErr.takeError())
8890
return std::move(E);
8991
auto Descriptor = *DescriptorOrErr;
90-
RootDescriptorYaml YamlDescriptor;
92+
RootParameterLocationYaml Location (Header);
93+
RootDescriptorYaml &YamlDescriptor = RootSigDesc.Parameters.getOrInsertDescriptor(Location);
94+
RootSigDesc.Parameters.addLocation(Location);
95+
9196
YamlDescriptor.ShaderRegister = Descriptor.ShaderRegister;
9297
YamlDescriptor.RegisterSpace = Descriptor.RegisterSpace;
9398
if (Version > 1) {
@@ -97,10 +102,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
97102
llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
98103
#include "llvm/BinaryFormat/DXContainerConstants.def"
99104
}
100-
NewP.Data = YamlDescriptor;
101105
}
102-
103-
RootSigDesc.Parameters.push_back(NewP);
104106
}
105107
#define ROOT_ELEMENT_FLAG(Num, Val) \
106108
RootSigDesc.Val = \
@@ -293,11 +295,31 @@ void MappingTraits<DXContainerYAML::RootSignatureYamlDesc>::mapping(
293295
IO.mapRequired("RootParametersOffset", S.RootParametersOffset);
294296
IO.mapRequired("NumStaticSamplers", S.NumStaticSamplers);
295297
IO.mapRequired("StaticSamplersOffset", S.StaticSamplersOffset);
296-
IO.mapRequired("Parameters", S.Parameters);
298+
IO.mapRequired("Parameters", S.Parameters.Locations, S);
297299
#define ROOT_ELEMENT_FLAG(Num, Val) IO.mapOptional(#Val, S.Val, false);
298300
#include "llvm/BinaryFormat/DXContainerConstants.def"
299301
}
300302

303+
void MappingContextTraits<DXContainerYAML::RootParameterLocationYaml, DXContainerYAML::RootSignatureYamlDesc>::mapping(IO &IO, DXContainerYAML::RootParameterLocationYaml &L, DXContainerYAML::RootSignatureYamlDesc &S) {
304+
IO.mapRequired("ParameterType", L.Header.Type);
305+
IO.mapRequired("ShaderVisibility", L.Header.Visibility);
306+
307+
switch (L.Header.Type) {
308+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
309+
DXContainerYAML::RootConstantsYaml &Constants = S.Parameters.getOrInsertConstants(L);
310+
IO.mapRequired("Constants", Constants);
311+
break;
312+
}
313+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
314+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
315+
case llvm::to_underlying(dxbc::RootParameterType::UAV):{
316+
DXContainerYAML::RootDescriptorYaml &Descriptor = S.Parameters.getOrInsertDescriptor(L);
317+
IO.mapRequired("Descriptor", Descriptor);
318+
break;
319+
}
320+
}
321+
322+
}
301323
void MappingTraits<llvm::DXContainerYAML::RootConstantsYaml>::mapping(
302324
IO &IO, llvm::DXContainerYAML::RootConstantsYaml &C) {
303325
IO.mapRequired("Num32BitValues", C.Num32BitValues);
@@ -313,33 +335,6 @@ void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
313335
#include "llvm/BinaryFormat/DXContainerConstants.def"
314336
}
315337

316-
void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
317-
IO &IO, llvm::DXContainerYAML::RootParameterYamlDesc &P) {
318-
IO.mapRequired("ParameterType", P.Type);
319-
IO.mapRequired("ShaderVisibility", P.Visibility);
320-
321-
switch (P.Type) {
322-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
323-
DXContainerYAML::RootConstantsYaml Constants;
324-
if (IO.outputting())
325-
Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
326-
IO.mapRequired("Constants", Constants);
327-
if (!IO.outputting())
328-
P.Data = Constants;
329-
} break;
330-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
331-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
332-
case llvm::to_underlying(dxbc::RootParameterType::UAV): {
333-
DXContainerYAML::RootDescriptorYaml Descriptor;
334-
if (IO.outputting())
335-
Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
336-
IO.mapRequired("Descriptor", Descriptor);
337-
if (!IO.outputting())
338-
P.Data = Descriptor;
339-
} break;
340-
}
341-
}
342-
343338
void MappingTraits<DXContainerYAML::Part>::mapping(IO &IO,
344339
DXContainerYAML::Part &P) {
345340
IO.mapRequired("Name", P.Name);

0 commit comments

Comments
 (0)