Skip to content

[NFC] Updating RTS0 namespace to contain all elements related to it's representation #141173

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 33 commits into from
May 27, 2025
Merged
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
35 changes: 19 additions & 16 deletions llvm/include/llvm/BinaryFormat/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ struct ProgramSignatureElement {

static_assert(sizeof(ProgramSignatureElement) == 32,
"ProgramSignatureElement is misaligned");

namespace RTS0 {
namespace v1 {
struct RootDescriptor {
Expand All @@ -595,23 +596,7 @@ struct RootDescriptor {
sys::swapByteOrder(RegisterSpace);
}
};
} // namespace v1

namespace v2 {
struct RootDescriptor : public v1::RootDescriptor {
uint32_t Flags;

RootDescriptor() = default;
explicit RootDescriptor(v1::RootDescriptor &Base)
: v1::RootDescriptor(Base), Flags(0u) {}

void swapBytes() {
v1::RootDescriptor::swapBytes();
sys::swapByteOrder(Flags);
}
};
} // namespace v2
} // namespace RTS0
// following dx12 naming
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
struct RootConstants {
Expand Down Expand Up @@ -655,6 +640,24 @@ struct RootSignatureHeader {
sys::swapByteOrder(Flags);
}
};
} // namespace v1

namespace v2 {
struct RootDescriptor : public v1::RootDescriptor {
uint32_t Flags;

RootDescriptor() = default;
explicit RootDescriptor(v1::RootDescriptor &Base)
: v1::RootDescriptor(Base), Flags(0u) {}

void swapBytes() {
v1::RootDescriptor::swapBytes();
sys::swapByteOrder(Flags);
}
};
} // namespace v2
} // namespace RTS0

} // namespace dxbc
} // namespace llvm

Expand Down
20 changes: 10 additions & 10 deletions llvm/include/llvm/MC/DXContainerRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ class raw_ostream;
namespace mcdxbc {

struct RootParameterInfo {
dxbc::RootParameterHeader Header;
dxbc::RTS0::v1::RootParameterHeader Header;
size_t Location;

RootParameterInfo() = default;

RootParameterInfo(dxbc::RootParameterHeader Header, size_t Location)
RootParameterInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location)
: Header(Header), Location(Location) {}
};

struct RootParametersContainer {
SmallVector<RootParameterInfo> ParametersInfo;

SmallVector<dxbc::RootConstants> Constants;
SmallVector<dxbc::RTS0::v1::RootConstants> Constants;
SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors;

void addInfo(dxbc::RootParameterHeader Header, size_t Location) {
void addInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location) {
ParametersInfo.push_back(RootParameterInfo(Header, Location));
}

void addParameter(dxbc::RootParameterHeader Header,
dxbc::RootConstants Constant) {
void addParameter(dxbc::RTS0::v1::RootParameterHeader Header,
dxbc::RTS0::v1::RootConstants Constant) {
addInfo(Header, Constants.size());
Constants.push_back(Constant);
}

void addInvalidParameter(dxbc::RootParameterHeader Header) {
void addInvalidParameter(dxbc::RTS0::v1::RootParameterHeader Header) {
addInfo(Header, -1);
}

void addParameter(dxbc::RootParameterHeader Header,
void addParameter(dxbc::RTS0::v1::RootParameterHeader Header,
dxbc::RTS0::v2::RootDescriptor Descriptor) {
addInfo(Header, Descriptors.size());
Descriptors.push_back(Descriptor);
Expand All @@ -57,12 +57,12 @@ struct RootParametersContainer {
return {Info.Header.ParameterType, Info.Location};
}

const dxbc::RootParameterHeader &getHeader(size_t Location) const {
const dxbc::RTS0::v1::RootParameterHeader &getHeader(size_t Location) const {
const RootParameterInfo &Info = ParametersInfo[Location];
return Info.Header;
}

const dxbc::RootConstants &getConstant(size_t Index) const {
const dxbc::RTS0::v1::RootConstants &getConstant(size_t Index) const {
return Constants[Index];
}

Expand Down
17 changes: 9 additions & 8 deletions llvm/include/llvm/Object/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ template <typename T> struct ViewArray {

namespace DirectX {
struct RootParameterView {
const dxbc::RootParameterHeader &Header;
const dxbc::RTS0::v1::RootParameterHeader &Header;
StringRef ParamData;

RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
RootParameterView(const dxbc::RTS0::v1::RootParameterHeader &H, StringRef P)
: Header(H), ParamData(P) {}

template <typename T> Expected<T> readParameter() {
Expand All @@ -147,8 +147,8 @@ struct RootConstantView : RootParameterView {
(uint32_t)dxbc::RootParameterType::Constants32Bit;
}

llvm::Expected<dxbc::RootConstants> read() {
return readParameter<dxbc::RootConstants>();
llvm::Expected<dxbc::RTS0::v1::RootConstants> read() {
return readParameter<dxbc::RTS0::v1::RootConstants>();
}
};

Expand Down Expand Up @@ -189,10 +189,11 @@ class RootSignature {
uint32_t NumStaticSamplers;
uint32_t StaticSamplersOffset;
uint32_t Flags;
ViewArray<dxbc::RootParameterHeader> ParametersHeaders;
ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
StringRef PartData;

using param_header_iterator = ViewArray<dxbc::RootParameterHeader>::iterator;
using param_header_iterator =
ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;

public:
RootSignature(StringRef PD) : PartData(PD) {}
Expand All @@ -210,15 +211,15 @@ class RootSignature {
uint32_t getFlags() const { return Flags; }

llvm::Expected<RootParameterView>
getParameter(const dxbc::RootParameterHeader &Header) const {
getParameter(const dxbc::RTS0::v1::RootParameterHeader &Header) const {
size_t DataSize;

if (!dxbc::isValidParameterType(Header.ParameterType))
return parseFailed("invalid parameter type");

switch (static_cast<dxbc::RootParameterType>(Header.ParameterType)) {
case dxbc::RootParameterType::Constants32Bit:
DataSize = sizeof(dxbc::RootConstants);
DataSize = sizeof(dxbc::RTS0::v1::RootConstants);
break;
case dxbc::RootParameterType::CBV:
case dxbc::RootParameterType::SRV:
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/MC/DXContainerRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
}

size_t RootSignatureDesc::getSize() const {
size_t Size = sizeof(dxbc::RootSignatureHeader) +
ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
size_t Size =
sizeof(dxbc::RTS0::v1::RootSignatureHeader) +
ParametersContainer.size() * sizeof(dxbc::RTS0::v1::RootParameterHeader);

for (const RootParameterInfo &I : ParametersContainer) {
switch (I.Header.ParameterType) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
Size += sizeof(dxbc::RootConstants);
Size += sizeof(dxbc::RTS0::v1::RootConstants);
break;
case llvm::to_underlying(dxbc::RootParameterType::CBV):
case llvm::to_underlying(dxbc::RootParameterType::SRV):
Expand Down Expand Up @@ -81,7 +82,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
switch (Type) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
const dxbc::RootConstants &Constants =
const dxbc::RTS0::v1::RootConstants &Constants =
ParametersContainer.getConstant(Loc);
support::endian::write(BOS, Constants.ShaderRegister,
llvm::endianness::little);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Object/DXContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ Error DirectX::RootSignature::parse() {
Current += sizeof(uint32_t);

ParametersHeaders.Data = PartData.substr(
RootParametersOffset, NumParameters * sizeof(dxbc::RootParameterHeader));
RootParametersOffset,
NumParameters * sizeof(dxbc::RTS0::v1::RootParameterHeader));

return Error::success();
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {

for (DXContainerYAML::RootParameterLocationYaml &L :
P.RootSignature->Parameters.Locations) {
dxbc::RootParameterHeader Header{L.Header.Type, L.Header.Visibility,
dxbc::RTS0::v1::RootParameterHeader Header{L.Header.Type, L.Header.Visibility,
L.Header.Offset};

switch (L.Header.Type) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
const DXContainerYAML::RootConstantsYaml &ConstantYaml =
P.RootSignature->Parameters.getOrInsertConstants(L);
dxbc::RootConstants Constants;
dxbc::RTS0::v1::RootConstants Constants;
Constants.Num32BitValues = ConstantYaml.Num32BitValues;
Constants.RegisterSpace = ConstantYaml.RegisterSpace;
Constants.ShaderRegister = ConstantYaml.ShaderRegister;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/ObjectYAML/DXContainerYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
RootSigDesc.RootParametersOffset = Data.getRootParametersOffset();

uint32_t Flags = Data.getFlags();
for (const dxbc::RootParameterHeader &PH : Data.param_headers()) {
for (const dxbc::RTS0::v1::RootParameterHeader &PH : Data.param_headers()) {

if (!dxbc::isValidParameterType(PH.ParameterType))
return createStringError(std::errc::invalid_argument,
Expand All @@ -70,7 +70,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
object::DirectX::RootParameterView ParamView = ParamViewOrErr.get();

if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&ParamView)) {
llvm::Expected<dxbc::RootConstants> ConstantsOrErr = RCV->read();
llvm::Expected<dxbc::RTS0::v1::RootConstants> ConstantsOrErr =
RCV->read();
if (Error E = ConstantsOrErr.takeError())
return std::move(E);

Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Target/DirectX/DXILRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
if (RootConstantNode->getNumOperands() != 5)
return reportError(Ctx, "Invalid format for RootConstants Element");

dxbc::RootParameterHeader Header;
dxbc::RTS0::v1::RootParameterHeader Header;
// The parameter offset doesn't matter here - we recalculate it during
// serialization Header.ParameterOffset = 0;
Header.ParameterType =
Expand All @@ -86,7 +86,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
else
return reportError(Ctx, "Invalid value for ShaderVisibility");

dxbc::RootConstants Constants;
dxbc::RTS0::v1::RootConstants Constants;
if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
Constants.ShaderRegister = *Val;
else
Expand Down Expand Up @@ -247,7 +247,7 @@ analyzeModule(Module &M) {
// Clang emits the root signature data in dxcontainer following a specific
// sequence. First the header, then the root parameters. So the header
// offset will always equal to the header size.
RSD.RootParameterOffset = sizeof(dxbc::RootSignatureHeader);
RSD.RootParameterOffset = sizeof(dxbc::RTS0::v1::RootSignatureHeader);

if (parse(Ctx, RSD, RootElementListNode) || validate(Ctx, RSD)) {
return RSDMap;
Expand Down Expand Up @@ -296,7 +296,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
for (size_t I = 0; I < RS.ParametersContainer.size(); I++) {
const auto &[Type, Loc] =
RS.ParametersContainer.getTypeAndLocForParameter(I);
const dxbc::RootParameterHeader Header =
const dxbc::RTS0::v1::RootParameterHeader Header =
RS.ParametersContainer.getHeader(I);

OS << indent(Space) << "- Parameter Type: " << Type << "\n";
Expand All @@ -305,7 +305,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,

switch (Type) {
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
const dxbc::RootConstants &Constants =
const dxbc::RTS0::v1::RootConstants &Constants =
RS.ParametersContainer.getConstant(Loc);
OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
<< "\n";
Expand Down
Loading