-
Notifications
You must be signed in to change notification settings - Fork 14k
[NFC] Refactoring DXContainerYaml Root Parameter representation #138318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NFC] Refactoring DXContainerYaml Root Parameter representation #138318
Conversation
@llvm/pr-subscribers-objectyaml @llvm/pr-subscribers-backend-directx Author: None (joaosaffran) ChangesThis PR removes the union usage from Full diff: https://github.com/llvm/llvm-project/pull/138318.diff 4 Files Affected:
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 738108cc3d1be..d8d025fe4294b 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -26,6 +26,7 @@
#include <cstdint>
#include <optional>
#include <string>
+#include <variant>
#include <vector>
namespace llvm {
@@ -112,97 +113,23 @@ struct DescriptorTableYaml {
SmallVector<DescriptorRangeYaml> Ranges;
};
+
+using ParameterData = std::variant<
+RootConstantsYaml,
+RootDescriptorYaml,
+DescriptorTableYaml
+>;
+
struct RootParameterYamlDesc {
uint32_t Type;
uint32_t Visibility;
uint32_t Offset;
+ ParameterData Data;
+
RootParameterYamlDesc(){};
RootParameterYamlDesc(uint32_t T) : Type(T) {
- switch (T) {
-
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
- Constants = RootConstantsYaml();
- break;
- case llvm::to_underlying(dxbc::RootParameterType::CBV):
- case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- Descriptor = RootDescriptorYaml();
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
- Table = DescriptorTableYaml();
- break;
- }
- }
-
- ~RootParameterYamlDesc() {
- switch (Type) {
-
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
- Constants.~RootConstantsYaml();
- break;
- case llvm::to_underlying(dxbc::RootParameterType::CBV):
- case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- Descriptor.~RootDescriptorYaml();
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
- Table.~DescriptorTableYaml();
- break;
- }
+
}
-
- RootParameterYamlDesc(const RootParameterYamlDesc &Other)
- : Type(Other.Type), Visibility(Other.Visibility), Offset(Other.Offset) {
- // Initialize the appropriate union member based on Type
- switch (Type) {
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
- // Placement new to construct the union member
- new (&Constants) RootConstantsYaml(Other.Constants);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::CBV):
- case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
- new (&Table) DescriptorTableYaml(Other.Table);
- break;
- }
- }
-
- RootParameterYamlDesc &operator=(const RootParameterYamlDesc &other) {
- if (this != &other) {
- // First, destroy the current union member
- this->~RootParameterYamlDesc();
-
- // Copy the basic members
- Type = other.Type;
- Visibility = other.Visibility;
- Offset = other.Offset;
-
- // Initialize the new union member based on the Type from 'other'
- switch (Type) {
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
- new (&Constants) RootConstantsYaml(other.Constants);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::CBV):
- case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- new (&Descriptor) RootDescriptorYaml(other.Descriptor);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
- new (&Table) DescriptorTableYaml(other.Table);
- break;
- }
- }
- return *this;
- }
-
- union {
- RootConstantsYaml Constants;
- RootDescriptorYaml Descriptor;
- DescriptorTableYaml Table;
- };
};
struct RootSignatureYamlDesc {
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 6336a42c8e4ae..c2c0188a959b0 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -15,11 +15,13 @@
#include "llvm/BinaryFormat/DXContainer.h"
#include "llvm/MC/DXContainerPSVInfo.h"
#include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/ObjectYAML/DXContainerYAML.h"
#include "llvm/ObjectYAML/ObjectYAML.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
+#include <variant>
using namespace llvm;
@@ -278,33 +280,35 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
Param.Offset};
- switch (Param.Type) {
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+ if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){
+ auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
+
dxbc::RootConstants Constants;
- Constants.Num32BitValues = Param.Constants.Num32BitValues;
- Constants.RegisterSpace = Param.Constants.RegisterSpace;
- Constants.ShaderRegister = Param.Constants.ShaderRegister;
+ Constants.Num32BitValues = ConstantYaml.Num32BitValues;
+ Constants.RegisterSpace = ConstantYaml.RegisterSpace;
+ Constants.ShaderRegister = ConstantYaml.ShaderRegister;
RS.ParametersContainer.addParameter(Header, Constants);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- case llvm::to_underlying(dxbc::RootParameterType::CBV):
+ } else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(Param.Data)){
+ auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+
if (RS.Version == 1) {
dxbc::RST0::v0::RootDescriptor Descriptor;
- Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
- Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+ auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+ Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
+ Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
RS.ParametersContainer.addParameter(Header, Descriptor);
} else {
dxbc::RST0::v1::RootDescriptor Descriptor;
- Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
- Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
- Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+ Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
+ Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
+ Descriptor.Flags = DescriptorYaml.getEncodedFlags();
RS.ParametersContainer.addParameter(Header, Descriptor);
}
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+ }else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(Param.Data)) {
mcdxbc::DescriptorTable Table;
- for (const auto &R : Param.Table.Ranges) {
+ auto TableYaml = std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
+
+ for (const auto &R : TableYaml.Ranges) {
if (RS.Version == 1) {
dxbc::RST0::v0::DescriptorRange Range;
Range.RangeType = R.RangeType;
@@ -327,8 +331,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
}
}
RS.ParametersContainer.addParameter(Header, Table);
- } break;
- default:
+ } else {
// Handling invalid parameter type edge case
RS.ParametersContainer.addInfo(Header, -1);
}
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 528fef59b46e4..3fea73c0da447 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -76,10 +76,11 @@ DXContainerYAML::RootSignatureYamlDesc::create(
return std::move(E);
auto Constants = *ConstantsOrErr;
-
- NewP.Constants.Num32BitValues = Constants.Num32BitValues;
- NewP.Constants.ShaderRegister = Constants.ShaderRegister;
- NewP.Constants.RegisterSpace = Constants.RegisterSpace;
+ RootConstantsYaml ConstantYaml;
+ ConstantYaml.Num32BitValues = Constants.Num32BitValues;
+ ConstantYaml.ShaderRegister = Constants.ShaderRegister;
+ ConstantYaml.RegisterSpace = Constants.RegisterSpace;
+ NewP.Data = ConstantYaml;
} else if (auto *RDV =
dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
@@ -87,15 +88,17 @@ DXContainerYAML::RootSignatureYamlDesc::create(
if (Error E = DescriptorOrErr.takeError())
return std::move(E);
auto Descriptor = *DescriptorOrErr;
- NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
- NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
+ RootDescriptorYaml YamlDescriptor;
+ YamlDescriptor.ShaderRegister = Descriptor.ShaderRegister;
+ YamlDescriptor.RegisterSpace = Descriptor.RegisterSpace;
if (Version > 1) {
#define ROOT_DESCRIPTOR_FLAG(Num, Val) \
- NewP.Descriptor.Val = \
+ YamlDescriptor.Val = \
(Descriptor.Flags & \
llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
#include "llvm/BinaryFormat/DXContainerConstants.def"
}
+ NewP.Data = YamlDescriptor;
} else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
llvm::Expected<
@@ -104,8 +107,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
if (Error E = TableOrErr.takeError())
return std::move(E);
auto Table = *TableOrErr;
- NewP.Table.NumRanges = Table.NumRanges;
- NewP.Table.RangesOffset = Table.RangesOffset;
+ DescriptorTableYaml YamlTable;
+ YamlTable.NumRanges = Table.NumRanges;
+ YamlTable.RangesOffset = Table.RangesOffset;
for (const auto &R : Table) {
DescriptorRangeYaml NewR;
@@ -117,8 +121,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
NewR.RegisterSpace = R.RegisterSpace;
NewR.RangeType = R.RangeType;
- NewP.Table.Ranges.push_back(NewR);
+ YamlTable.Ranges.push_back(NewR);
}
+ NewP.Data = YamlTable;
} else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
llvm::Expected<
@@ -127,8 +132,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
if (Error E = TableOrErr.takeError())
return std::move(E);
auto Table = *TableOrErr;
- NewP.Table.NumRanges = Table.NumRanges;
- NewP.Table.RangesOffset = Table.RangesOffset;
+ DescriptorTableYaml YamlTable;
+ YamlTable.NumRanges = Table.NumRanges;
+ YamlTable.RangesOffset = Table.RangesOffset;
for (const auto &R : Table) {
DescriptorRangeYaml NewR;
@@ -143,8 +149,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
NewR.Val = \
(R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
#include "llvm/BinaryFormat/DXContainerConstants.def"
- NewP.Table.Ranges.push_back(NewR);
+ YamlTable.Ranges.push_back(NewR);
}
+ NewP.Data = YamlTable;
}
RootSigDesc.Parameters.push_back(NewP);
@@ -394,18 +401,29 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
IO.mapRequired("ShaderVisibility", P.Visibility);
switch (P.Type) {
- case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
- IO.mapRequired("Constants", P.Constants);
- break;
+ case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
+ DXContainerYAML::RootConstantsYaml Constants;
+ if(IO.outputting())
+ Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
+ IO.mapRequired("Constants", Constants);
+ P.Data = Constants;
+ } break;
case llvm::to_underlying(dxbc::RootParameterType::CBV):
case llvm::to_underlying(dxbc::RootParameterType::SRV):
- case llvm::to_underlying(dxbc::RootParameterType::UAV):
- IO.mapRequired("Descriptor", P.Descriptor);
- break;
- case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
- IO.mapRequired("Table", P.Table);
- break;
- break;
+ case llvm::to_underlying(dxbc::RootParameterType::UAV):{
+ DXContainerYAML::RootDescriptorYaml Descriptor;
+ if(IO.outputting())
+ Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
+ IO.mapRequired("Descriptor", Descriptor);
+ P.Data = Descriptor;
+ } break;
+ case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+ DXContainerYAML::DescriptorTableYaml Table;
+ if(IO.outputting())
+ Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
+ IO.mapRequired("Table", Table);
+ P.Data = Table;
+ } break;
}
}
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index d680bf09ab730..debb459c3944e 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -37,17 +37,6 @@ Parts:
ShaderRegister: 31
RegisterSpace: 32
DATA_STATIC_WHILE_SET_AT_EXECUTE: true
- - ParameterType: 0 # SRV
- ShaderVisibility: 3 # Domain
- Table:
- NumRanges: 1
- Ranges:
- - RangeType: 0
- NumDescriptors: 41
- BaseShaderRegister: 42
- RegisterSpace: 43
- OffsetInDescriptorsFromTableStart: -1
- DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
AllowInputAssemblerInputLayout: true
DenyGeometryShaderRootAccess: true
|
llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
Outdated
Show resolved
Hide resolved
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- llvm/include/llvm/ObjectYAML/DXContainerYAML.h llvm/lib/ObjectYAML/DXContainerEmitter.cpp llvm/lib/ObjectYAML/DXContainerYAML.cpp View the diff from clang-format here.diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 3af1da552..a21b7a5ce 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct RootParameterHeaderYaml {
uint32_t Visibility;
uint32_t Offset;
- RootParameterHeaderYaml(){};
+ RootParameterHeaderYaml() {};
RootParameterHeaderYaml(uint32_t T) : Type(T) {}
};
@@ -103,7 +103,7 @@ struct RootParameterLocationYaml {
RootParameterHeaderYaml Header;
std::optional<size_t> IndexInSignature;
- RootParameterLocationYaml(){};
+ RootParameterLocationYaml() {};
explicit RootParameterLocationYaml(RootParameterHeaderYaml Header)
: Header(Header) {}
};
|
if (IO.outputting()) | ||
Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this do? Is this actually a NFC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DXContainerYaml
handles both serialization and deserialization of yaml files. IO.outputting()
is a way for me to know which one is happening. Since parameter data is using a variant, the yaml serialization doesn't have a native way of handling that. Therefore, this is a custom logic to read and write the correct parameters data to and from YAML. I think this is still a NFC, since I am just keeping the existing functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Should we only assign P.Data = Constants
when we are not outputting then? Otherwise, it seems we would overwrite it with garbage.
e655315
to
aabd424
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly to the discussion in #137284, I'm not really convinced that std::variant
makes sense here. These two types (RootConstantsYaml and RootDescriptorYaml) are relatively small and similarly sized (12 bytes each), so a strongly typed union like std::variant seems fairly reasonable, but consider what we get once we add Descriptor Table handling - that object contains a SmallVector of objects that are 28 bytes large each, as well as a couple of other fields. Even if the SmallVector's inline storage is only a single element, that's on the order of 5 times the size of the other two variant types, which means most of the objects in the vector of ParameterData will have 4 times as much wasted space as they do actual data.
On the other hand, if we put something like a std::optional<size_t> IndexInSignature
here, we can store each type of data contiguously, and it doesn't even really change the amount of complexity overall. The one thing that will be a bit messier is the mapping
function for RootParameterYamlDesc, but we can use MappingContextTraits
there in order to pass along the RootSignatureYamlDesc
as a "Context" there, and then define some lazy accessor helpers in RootSignatureYamlDesc, like:
template <typename T>
T &getOrInsertImpl(RootParameterYamlDesc &ParamDesc,
SmallVectorImpl<T> &Container) {
if (!ParamDesc.IndexInSignature) {
ParamDesc.IndexInSignature = Container.size();
Container.emplace_back();
}
return Container[*ParamDesc.IndexInSignature];
}
RootConstantsYaml &getOrInsertConstants(RootParameterYamlDesc &ParamDesc) {
return getOrInsertImpl(ParamDesc, Constants);
}
RootDescriptorYaml &getOrInsertDescriptor(RootParameterYamlDesc &ParamDesc) {
return getOrInsertImpl(ParamDesc, Descriptors);
}
if (!P.RootSignature.has_value()) | ||
continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a funny whitespace change here, please double check.
auto Header = dxbc::RootParameterHeader{ | ||
L.Header.Type, L.Header.Visibility, L.Header.Offset}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be clearer.
auto Header = dxbc::RootParameterHeader{ | |
L.Header.Type, L.Header.Visibility, L.Header.Offset}; | |
dxbc::RootParameterHeader Header{ | |
L.Header.Type, L.Header.Visibility, L.Header.Offset}; |
DXContainerYAML::RootConstantsYaml ConstantYaml = | ||
P.RootSignature->Parameters.getOrInsertConstants(L); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make a copy here? This should be a reference.
FYI this didn't pass the code formatter before merging |
…#138318) This PR removes the union usage from `DXContainerYaml` Root Parameters representation, it uses variant instead. closes: [llvm#139585](llvm#139585) --------- Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
This PR removes the union usage from
DXContainerYaml
Root Parameters representation, it uses variant instead.closes: #139585