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

Merged
merged 32 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from 28 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
23 changes: 5 additions & 18 deletions llvm/include/llvm/ObjectYAML/DXContainerYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,16 @@ struct RootDescriptorYaml {
#include "llvm/BinaryFormat/DXContainerConstants.def"
};

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

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

union {
RootConstantsYaml Constants;
RootDescriptorYaml Descriptor;
};
RootParameterYamlDesc() {};
RootParameterYamlDesc(uint32_t T) : Type(T) {}
};

struct RootSignatureYamlDesc {
Expand Down
30 changes: 14 additions & 16 deletions llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,26 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;

for (const auto &Param : P.RootSignature->Parameters) {
dxbc::RootParameterHeader Header{Param.Type, Param.Visibility,
Param.Offset};
auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
Param.Offset};

switch (Param.Type) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
if (auto *ConstantYaml =
std::get_if<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 (auto *DescriptorYaml =
std::get_if<DXContainerYAML::RootDescriptorYaml>(
&Param.Data)) {
dxbc::RTS0::v2::RootDescriptor Descriptor;
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
Descriptor.RegisterSpace = DescriptorYaml->RegisterSpace;
Descriptor.ShaderRegister = DescriptorYaml->ShaderRegister;
if (RS.Version > 1)
Descriptor.Flags = Param.Descriptor.getEncodedFlags();
Descriptor.Flags = DescriptorYaml->getEncodedFlags();
RS.ParametersContainer.addParameter(Header, Descriptor);
break;
default:
} else {
// Handling invalid parameter type edge case. We intentionally let
// obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
// for that to be used as a testing tool more effectively.
Expand Down
37 changes: 24 additions & 13 deletions llvm/lib/ObjectYAML/DXContainerYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,29 @@ 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::RTS0::v2::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;
}

RootSigDesc.Parameters.push_back(NewP);
Expand Down Expand Up @@ -316,14 +319,22 @@ 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);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@inbelic inbelic May 23, 2025

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.

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::UAV): {
DXContainerYAML::RootDescriptorYaml Descriptor;
if (IO.outputting())
Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
IO.mapRequired("Descriptor", Descriptor);
P.Data = Descriptor;
} break;
}
}

Expand Down
Loading