Skip to content

Commit bd516ff

Browse files
author
joaosaffran
committed
refactoring root signature dxcontainer yaml representation
1 parent ac51bf6 commit bd516ff

File tree

4 files changed

+74
-137
lines changed

4 files changed

+74
-137
lines changed

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

+11-84
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <cstdint>
2727
#include <optional>
2828
#include <string>
29+
#include <variant>
2930
#include <vector>
3031

3132
namespace llvm {
@@ -112,97 +113,23 @@ struct DescriptorTableYaml {
112113
SmallVector<DescriptorRangeYaml> Ranges;
113114
};
114115

116+
117+
using ParameterData = std::variant<
118+
RootConstantsYaml,
119+
RootDescriptorYaml,
120+
DescriptorTableYaml
121+
>;
122+
115123
struct RootParameterYamlDesc {
116124
uint32_t Type;
117125
uint32_t Visibility;
118126
uint32_t Offset;
127+
ParameterData Data;
128+
119129
RootParameterYamlDesc(){};
120130
RootParameterYamlDesc(uint32_t T) : Type(T) {
121-
switch (T) {
122-
123-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
124-
Constants = RootConstantsYaml();
125-
break;
126-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
127-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
128-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
129-
Descriptor = RootDescriptorYaml();
130-
break;
131-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
132-
Table = DescriptorTableYaml();
133-
break;
134-
}
135-
}
136-
137-
~RootParameterYamlDesc() {
138-
switch (Type) {
139-
140-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
141-
Constants.~RootConstantsYaml();
142-
break;
143-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
144-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
145-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
146-
Descriptor.~RootDescriptorYaml();
147-
break;
148-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
149-
Table.~DescriptorTableYaml();
150-
break;
151-
}
131+
152132
}
153-
154-
RootParameterYamlDesc(const RootParameterYamlDesc &Other)
155-
: Type(Other.Type), Visibility(Other.Visibility), Offset(Other.Offset) {
156-
// Initialize the appropriate union member based on Type
157-
switch (Type) {
158-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
159-
// Placement new to construct the union member
160-
new (&Constants) RootConstantsYaml(Other.Constants);
161-
break;
162-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
163-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
164-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
165-
new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
166-
break;
167-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
168-
new (&Table) DescriptorTableYaml(Other.Table);
169-
break;
170-
}
171-
}
172-
173-
RootParameterYamlDesc &operator=(const RootParameterYamlDesc &other) {
174-
if (this != &other) {
175-
// First, destroy the current union member
176-
this->~RootParameterYamlDesc();
177-
178-
// Copy the basic members
179-
Type = other.Type;
180-
Visibility = other.Visibility;
181-
Offset = other.Offset;
182-
183-
// Initialize the new union member based on the Type from 'other'
184-
switch (Type) {
185-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
186-
new (&Constants) RootConstantsYaml(other.Constants);
187-
break;
188-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
189-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
190-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
191-
new (&Descriptor) RootDescriptorYaml(other.Descriptor);
192-
break;
193-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
194-
new (&Table) DescriptorTableYaml(other.Table);
195-
break;
196-
}
197-
}
198-
return *this;
199-
}
200-
201-
union {
202-
RootConstantsYaml Constants;
203-
RootDescriptorYaml Descriptor;
204-
DescriptorTableYaml Table;
205-
};
206133
};
207134

208135
struct RootSignatureYamlDesc {

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

+22-19
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include "llvm/BinaryFormat/DXContainer.h"
1616
#include "llvm/MC/DXContainerPSVInfo.h"
1717
#include "llvm/MC/DXContainerRootSignature.h"
18+
#include "llvm/ObjectYAML/DXContainerYAML.h"
1819
#include "llvm/ObjectYAML/ObjectYAML.h"
1920
#include "llvm/ObjectYAML/yaml2obj.h"
2021
#include "llvm/Support/Errc.h"
2122
#include "llvm/Support/Error.h"
2223
#include "llvm/Support/raw_ostream.h"
24+
#include <variant>
2325

2426
using namespace llvm;
2527

@@ -278,33 +280,35 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
278280
auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
279281
Param.Offset};
280282

281-
switch (Param.Type) {
282-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
283+
if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){
284+
auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
285+
283286
dxbc::RootConstants Constants;
284-
Constants.Num32BitValues = Param.Constants.Num32BitValues;
285-
Constants.RegisterSpace = Param.Constants.RegisterSpace;
286-
Constants.ShaderRegister = Param.Constants.ShaderRegister;
287+
Constants.Num32BitValues = ConstantYaml.Num32BitValues;
288+
Constants.RegisterSpace = ConstantYaml.RegisterSpace;
289+
Constants.ShaderRegister = ConstantYaml.ShaderRegister;
287290
RS.ParametersContainer.addParameter(Header, Constants);
288-
break;
289-
case llvm::to_underlying(dxbc::RootParameterType::SRV):
290-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
291-
case llvm::to_underlying(dxbc::RootParameterType::CBV):
291+
} else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(Param.Data)){
292+
auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
293+
292294
if (RS.Version == 1) {
293295
dxbc::RST0::v0::RootDescriptor Descriptor;
294-
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
295-
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
296+
auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
297+
Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
298+
Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
296299
RS.ParametersContainer.addParameter(Header, Descriptor);
297300
} else {
298301
dxbc::RST0::v1::RootDescriptor Descriptor;
299-
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
300-
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
301-
Descriptor.Flags = Param.Descriptor.getEncodedFlags();
302+
Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
303+
Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
304+
Descriptor.Flags = DescriptorYaml.getEncodedFlags();
302305
RS.ParametersContainer.addParameter(Header, Descriptor);
303306
}
304-
break;
305-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
307+
}else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(Param.Data)) {
306308
mcdxbc::DescriptorTable Table;
307-
for (const auto &R : Param.Table.Ranges) {
309+
auto TableYaml = std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
310+
311+
for (const auto &R : TableYaml.Ranges) {
308312
if (RS.Version == 1) {
309313
dxbc::RST0::v0::DescriptorRange Range;
310314
Range.RangeType = R.RangeType;
@@ -327,8 +331,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
327331
}
328332
}
329333
RS.ParametersContainer.addParameter(Header, Table);
330-
} break;
331-
default:
334+
} else {
332335
// Handling invalid parameter type edge case
333336
RS.ParametersContainer.addInfo(Header, -1);
334337
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

+41-23
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,29 @@ DXContainerYAML::RootSignatureYamlDesc::create(
7676
return std::move(E);
7777

7878
auto Constants = *ConstantsOrErr;
79-
80-
NewP.Constants.Num32BitValues = Constants.Num32BitValues;
81-
NewP.Constants.ShaderRegister = Constants.ShaderRegister;
82-
NewP.Constants.RegisterSpace = Constants.RegisterSpace;
79+
RootConstantsYaml ConstantYaml;
80+
ConstantYaml.Num32BitValues = Constants.Num32BitValues;
81+
ConstantYaml.ShaderRegister = Constants.ShaderRegister;
82+
ConstantYaml.RegisterSpace = Constants.RegisterSpace;
83+
NewP.Data = ConstantYaml;
8384
} else if (auto *RDV =
8485
dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
8586
llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
8687
RDV->read(Version);
8788
if (Error E = DescriptorOrErr.takeError())
8889
return std::move(E);
8990
auto Descriptor = *DescriptorOrErr;
90-
NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
91-
NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
91+
RootDescriptorYaml YamlDescriptor;
92+
YamlDescriptor.ShaderRegister = Descriptor.ShaderRegister;
93+
YamlDescriptor.RegisterSpace = Descriptor.RegisterSpace;
9294
if (Version > 1) {
9395
#define ROOT_DESCRIPTOR_FLAG(Num, Val) \
94-
NewP.Descriptor.Val = \
96+
YamlDescriptor.Val = \
9597
(Descriptor.Flags & \
9698
llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
9799
#include "llvm/BinaryFormat/DXContainerConstants.def"
98100
}
101+
NewP.Data = YamlDescriptor;
99102
} else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
100103
dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
101104
llvm::Expected<
@@ -104,8 +107,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
104107
if (Error E = TableOrErr.takeError())
105108
return std::move(E);
106109
auto Table = *TableOrErr;
107-
NewP.Table.NumRanges = Table.NumRanges;
108-
NewP.Table.RangesOffset = Table.RangesOffset;
110+
DescriptorTableYaml YamlTable;
111+
YamlTable.NumRanges = Table.NumRanges;
112+
YamlTable.RangesOffset = Table.RangesOffset;
109113

110114
for (const auto &R : Table) {
111115
DescriptorRangeYaml NewR;
@@ -117,8 +121,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
117121
NewR.RegisterSpace = R.RegisterSpace;
118122
NewR.RangeType = R.RangeType;
119123

120-
NewP.Table.Ranges.push_back(NewR);
124+
YamlTable.Ranges.push_back(NewR);
121125
}
126+
NewP.Data = YamlTable;
122127
} else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
123128
dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
124129
llvm::Expected<
@@ -127,8 +132,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
127132
if (Error E = TableOrErr.takeError())
128133
return std::move(E);
129134
auto Table = *TableOrErr;
130-
NewP.Table.NumRanges = Table.NumRanges;
131-
NewP.Table.RangesOffset = Table.RangesOffset;
135+
DescriptorTableYaml YamlTable;
136+
YamlTable.NumRanges = Table.NumRanges;
137+
YamlTable.RangesOffset = Table.RangesOffset;
132138

133139
for (const auto &R : Table) {
134140
DescriptorRangeYaml NewR;
@@ -143,8 +149,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
143149
NewR.Val = \
144150
(R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
145151
#include "llvm/BinaryFormat/DXContainerConstants.def"
146-
NewP.Table.Ranges.push_back(NewR);
152+
YamlTable.Ranges.push_back(NewR);
147153
}
154+
NewP.Data = YamlTable;
148155
}
149156

150157
RootSigDesc.Parameters.push_back(NewP);
@@ -394,18 +401,29 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
394401
IO.mapRequired("ShaderVisibility", P.Visibility);
395402

396403
switch (P.Type) {
397-
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
398-
IO.mapRequired("Constants", P.Constants);
399-
break;
404+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
405+
DXContainerYAML::RootConstantsYaml Constants;
406+
if(IO.outputting())
407+
Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
408+
IO.mapRequired("Constants", Constants);
409+
P.Data = Constants;
410+
} break;
400411
case llvm::to_underlying(dxbc::RootParameterType::CBV):
401412
case llvm::to_underlying(dxbc::RootParameterType::SRV):
402-
case llvm::to_underlying(dxbc::RootParameterType::UAV):
403-
IO.mapRequired("Descriptor", P.Descriptor);
404-
break;
405-
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
406-
IO.mapRequired("Table", P.Table);
407-
break;
408-
break;
413+
case llvm::to_underlying(dxbc::RootParameterType::UAV):{
414+
DXContainerYAML::RootDescriptorYaml Descriptor;
415+
if(IO.outputting())
416+
Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
417+
IO.mapRequired("Descriptor", Descriptor);
418+
P.Data = Descriptor;
419+
} break;
420+
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
421+
DXContainerYAML::DescriptorTableYaml Table;
422+
if(IO.outputting())
423+
Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
424+
IO.mapRequired("Table", Table);
425+
P.Data = Table;
426+
} break;
409427
}
410428
}
411429

llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml

-11
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ Parts:
3737
ShaderRegister: 31
3838
RegisterSpace: 32
3939
DATA_STATIC_WHILE_SET_AT_EXECUTE: true
40-
- ParameterType: 0 # SRV
41-
ShaderVisibility: 3 # Domain
42-
Table:
43-
NumRanges: 1
44-
Ranges:
45-
- RangeType: 0
46-
NumDescriptors: 41
47-
BaseShaderRegister: 42
48-
RegisterSpace: 43
49-
OffsetInDescriptorsFromTableStart: -1
50-
DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
5140
AllowInputAssemblerInputLayout: true
5241
DenyGeometryShaderRootAccess: true
5342

0 commit comments

Comments
 (0)