[llvm-branch-commits] [llvm] [NFC] Refactoring DXContainerYaml Root Parameter representation (PR #138318)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri May 2 11:07:17 PDT 2025
https://github.com/joaosaffran created https://github.com/llvm/llvm-project/pull/138318
This PR removes the union usage from `DXContainerYaml` Root Parameters representation, it uses variant instead.
>From bd516ff7c29a49ff53e9ba776a1d7a15245d52d9 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 00:42:56 +0000
Subject: [PATCH] refactoring root signature dxcontainer yaml representation
---
.../include/llvm/ObjectYAML/DXContainerYAML.h | 95 +++----------------
llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 41 ++++----
llvm/lib/ObjectYAML/DXContainerYAML.cpp | 64 ++++++++-----
.../RootSignature-MultipleParameters.yaml | 11 ---
4 files changed, 74 insertions(+), 137 deletions(-)
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
More information about the llvm-branch-commits
mailing list