-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[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
base: users/joaosaffran/138315
Are you sure you want to change the base?
[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
|
@@ -37,17 +37,6 @@ Parts: | |||
ShaderRegister: 31 | |||
RegisterSpace: 32 | |||
DATA_STATIC_WHILE_SET_AT_EXECUTE: true | |||
- ParameterType: 0 # SRV |
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 is this removed? Is this somehow a result of switching to use std::variant?
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.
Need to rebase, that is why. It should go away on the next commit or two.
if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){ | ||
auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.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.
if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){ | |
auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.Data); | |
if (auto* ConstantYaml = std::get_if<DXContainerYAML::RootConstantsYaml>(&Param.Data)) { |
(If I remember how variant works correctly)
Saves some repetitive code, and also allows you to pull the data directly out of Param.Data
without making a copy of it.
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); |
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.
Isn't this doing something that was already done on line 292?
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.
True, missed that, thanks
bd516ff
to
2527580
Compare
This PR removes the union usage from
DXContainerYaml
Root Parameters representation, it uses variant instead.