Skip to content

[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

Open
wants to merge 4 commits into
base: users/joaosaffran/138315
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 7 additions & 87 deletions llvm/include/llvm/ObjectYAML/DXContainerYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdint>
#include <optional>
#include <string>
#include <variant>
#include <vector>

namespace llvm {
Expand Down Expand Up @@ -112,98 +113,17 @@ struct DescriptorTableYaml {
SmallVector<DescriptorRangeYaml> Ranges;
};

using ParameterData =
std::variant<RootConstantsYaml, RootDescriptorYaml, DescriptorTableYaml>;

struct RootParameterYamlDesc {
uint32_t Type;
uint32_t Visibility;
uint32_t Offset;
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;
}
}
ParameterData Data;

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;
}

// ToDo: Fix this (Already have a follow up PR with it)
union {
RootConstantsYaml Constants;
RootDescriptorYaml Descriptor;
};
DescriptorTableYaml Table;
RootParameterYamlDesc(){};
RootParameterYamlDesc(uint32_t T) : Type(T) {}
};

struct RootSignatureYamlDesc {
Expand Down
48 changes: 28 additions & 20 deletions llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -278,33 +280,40 @@ 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;
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();
RS.ParametersContainer.addParameter(Header, Descriptor);
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;
Expand All @@ -327,8 +336,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);
}
Expand Down
82 changes: 50 additions & 32 deletions llvm/lib/ObjectYAML/DXContainerYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,40 @@ 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 =
RDV->read(Version);
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::v1::DescriptorRange>>(&ParamView)) {
dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
llvm::Expected<
object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
TableOrErr = TDV->read();
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;
Expand All @@ -116,22 +120,21 @@ DXContainerYAML::RootSignatureYamlDesc::create(
NewR.BaseShaderRegister = R.BaseShaderRegister;
NewR.RegisterSpace = R.RegisterSpace;
NewR.RangeType = R.RangeType;
#define DESCRIPTOR_RANGE_FLAG(Num, Val) \
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;
} else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
llvm::Expected<
object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
TableOrErr = TDV->read();
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;
Expand All @@ -142,9 +145,13 @@ DXContainerYAML::RootSignatureYamlDesc::create(
NewR.BaseShaderRegister = R.BaseShaderRegister;
NewR.RegisterSpace = R.RegisterSpace;
NewR.RangeType = R.RangeType;

NewP.Table.Ranges.push_back(NewR);
#define DESCRIPTOR_RANGE_FLAG(Num, Val) \
NewR.Val = \
(R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
#include "llvm/BinaryFormat/DXContainerConstants.def"
YamlTable.Ranges.push_back(NewR);
}
NewP.Data = YamlTable;
}

RootSigDesc.Parameters.push_back(NewP);
Expand Down Expand Up @@ -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;
}
}

Expand Down
Loading