https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/154629
>From 31ec5e50ff36ba6d963443e8908f8fb58d7b89bf Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Mon, 18 Aug 2025 18:49:45 -0700 Subject: [PATCH 01/15] making parameter type and shader visibility use enums --- .../llvm/MC/DXContainerRootSignature.h | 27 ++++--- .../Frontend/HLSL/RootSignatureMetadata.cpp | 76 +++++++++++-------- llvm/lib/MC/DXContainerRootSignature.cpp | 20 ++--- llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 19 +++-- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 21 +++-- .../ContainerData/RootSignature-Parameters.ll | 60 +++++++-------- 6 files changed, 122 insertions(+), 101 deletions(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 3c7c886e79fc3..757ab8bc83129 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -19,13 +19,19 @@ namespace llvm { class raw_ostream; namespace mcdxbc { +struct RootParameterHeader { + dxbc::RootParameterType ParameterType; + dxbc::ShaderVisibility ShaderVisibility; + uint32_t ParameterOffset; +}; + struct RootParameterInfo { - dxbc::RTS0::v1::RootParameterHeader Header; + RootParameterHeader Header; size_t Location; RootParameterInfo() = default; - RootParameterInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location) + RootParameterInfo(RootParameterHeader Header, size_t Location) : Header(Header), Location(Location) {} }; @@ -46,39 +52,36 @@ struct RootParametersContainer { SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors; SmallVector<DescriptorTable> Tables; - void addInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location) { + void addInfo(RootParameterHeader Header, size_t Location) { ParametersInfo.push_back(RootParameterInfo(Header, Location)); } - void addParameter(dxbc::RTS0::v1::RootParameterHeader Header, + void addParameter(RootParameterHeader Header, dxbc::RTS0::v1::RootConstants Constant) { addInfo(Header, Constants.size()); Constants.push_back(Constant); } - void addInvalidParameter(dxbc::RTS0::v1::RootParameterHeader Header) { - addInfo(Header, -1); - } + void addInvalidParameter(RootParameterHeader Header) { addInfo(Header, -1); } - void addParameter(dxbc::RTS0::v1::RootParameterHeader Header, + void addParameter(RootParameterHeader Header, dxbc::RTS0::v2::RootDescriptor Descriptor) { addInfo(Header, Descriptors.size()); Descriptors.push_back(Descriptor); } - void addParameter(dxbc::RTS0::v1::RootParameterHeader Header, - DescriptorTable Table) { + void addParameter(RootParameterHeader Header, DescriptorTable Table) { addInfo(Header, Tables.size()); Tables.push_back(Table); } - std::pair<uint32_t, uint32_t> + std::pair<dxbc::RootParameterType, uint32_t> getTypeAndLocForParameter(uint32_t Location) const { const RootParameterInfo &Info = ParametersInfo[Location]; return {Info.Header.ParameterType, Info.Location}; } - const dxbc::RTS0::v1::RootParameterHeader &getHeader(size_t Location) const { + const RootParameterHeader &getHeader(size_t Location) const { const RootParameterInfo &Info = ParametersInfo[Location]; return Info.Header; } diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 157bfc665b207..8b9c0d24c34f3 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -12,9 +12,11 @@ //===----------------------------------------------------------------------===// #include "llvm/Frontend/HLSL/RootSignatureMetadata.h" +#include "llvm/BinaryFormat/DXContainer.h" #include "llvm/Frontend/HLSL/RootSignatureValidations.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Metadata.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ScopedPrinter.h" using namespace llvm; @@ -51,6 +53,17 @@ static std::optional<StringRef> extractMdStringValue(MDNode *Node, return NodeText->getString(); } +static Expected<dxbc::ShaderVisibility> +extractShaderVisibility(MDNode *Node, unsigned int OpId) { + if (std::optional<uint32_t> Val = extractMdIntValue(Node, OpId)) { + if (!dxbc::isValidShaderVisibility(*Val)) + return make_error<RootSignatureValidationError<uint32_t>>( + "ShaderVisibility", *Val); + return static_cast<dxbc::ShaderVisibility>(*Val); + } + return make_error<InvalidRSMetadataValue>("ShaderVisibility"); +} + namespace { // We use the OverloadVisit with std::visit to ensure the compiler catches if a @@ -224,15 +237,16 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, if (RootConstantNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("RootConstants Element"); - dxbc::RTS0::v1::RootParameterHeader Header; + mcdxbc::RootParameterHeader Header; // The parameter offset doesn't matter here - we recalculate it during // serialization Header.ParameterOffset = 0; - Header.ParameterType = to_underlying(dxbc::RootParameterType::Constants32Bit); + Header.ParameterType = dxbc::RootParameterType::Constants32Bit; - if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); + Expected<dxbc::ShaderVisibility> VisibilityOrErr = + extractShaderVisibility(RootConstantNode, 1); + if (auto E = VisibilityOrErr.takeError()) + return Error(std::move(E)); + Header.ShaderVisibility = *VisibilityOrErr; dxbc::RTS0::v1::RootConstants Constants; if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2)) @@ -266,26 +280,27 @@ Error MetadataParser::parseRootDescriptors( if (RootDescriptorNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("Root Descriptor Element"); - dxbc::RTS0::v1::RootParameterHeader Header; + mcdxbc::RootParameterHeader Header; switch (ElementKind) { case RootSignatureElementKind::SRV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::SRV); + Header.ParameterType = dxbc::RootParameterType::SRV; break; case RootSignatureElementKind::UAV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::UAV); + Header.ParameterType = dxbc::RootParameterType::UAV; break; case RootSignatureElementKind::CBV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::CBV); + Header.ParameterType = dxbc::RootParameterType::CBV; break; default: llvm_unreachable("invalid Root Descriptor kind"); break; } - if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); + Expected<dxbc::ShaderVisibility> VisibilityOrErr = + extractShaderVisibility(RootDescriptorNode, 1); + if (auto E = VisibilityOrErr.takeError()) + return Error(std::move(E)); + Header.ShaderVisibility = *VisibilityOrErr; dxbc::RTS0::v2::RootDescriptor Descriptor; if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2)) @@ -375,15 +390,16 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, if (NumOperands < 2) return make_error<InvalidRSMetadataFormat>("Descriptor Table"); - dxbc::RTS0::v1::RootParameterHeader Header; - if (std::optional<uint32_t> Val = extractMdIntValue(DescriptorTableNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); + mcdxbc::RootParameterHeader Header; + + Expected<dxbc::ShaderVisibility> VisibilityOrErr = + extractShaderVisibility(DescriptorTableNode, 1); + if (auto E = VisibilityOrErr.takeError()) + return Error(std::move(E)); + Header.ShaderVisibility = *VisibilityOrErr; mcdxbc::DescriptorTable Table; - Header.ParameterType = - to_underlying(dxbc::RootParameterType::DescriptorTable); + Header.ParameterType = dxbc::RootParameterType::DescriptorTable; for (unsigned int I = 2; I < NumOperands; I++) { MDNode *Element = dyn_cast<MDNode>(DescriptorTableNode->getOperand(I)); @@ -531,20 +547,14 @@ Error MetadataParser::validateRootSignature( } for (const mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) { - if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility)) - DeferredErrs = - joinErrors(std::move(DeferredErrs), - make_error<RootSignatureValidationError<uint32_t>>( - "ShaderVisibility", Info.Header.ShaderVisibility)); - - assert(dxbc::isValidParameterType(Info.Header.ParameterType) && - "Invalid value for ParameterType"); switch (Info.Header.ParameterType) { + case dxbc::RootParameterType::Constants32Bit: + break; - case to_underlying(dxbc::RootParameterType::CBV): - case to_underlying(dxbc::RootParameterType::UAV): - case to_underlying(dxbc::RootParameterType::SRV): { + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::UAV: + case dxbc::RootParameterType::SRV: { const dxbc::RTS0::v2::RootDescriptor &Descriptor = RSD.ParametersContainer.getRootDescriptor(Info.Location); if (!hlsl::rootsig::verifyRegisterValue(Descriptor.ShaderRegister)) @@ -569,7 +579,7 @@ Error MetadataParser::validateRootSignature( } break; } - case to_underlying(dxbc::RootParameterType::DescriptorTable): { + case dxbc::RootParameterType::DescriptorTable: { const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(Info.Location); for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) { diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 482280b5ef289..c7be78149a32b 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -36,19 +36,19 @@ size_t RootSignatureDesc::getSize() const { for (const RootParameterInfo &I : ParametersContainer) { switch (I.Header.ParameterType) { - case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): + case dxbc::RootParameterType::Constants32Bit: Size += sizeof(dxbc::RTS0::v1::RootConstants); break; - case llvm::to_underlying(dxbc::RootParameterType::CBV): - case llvm::to_underlying(dxbc::RootParameterType::SRV): - case llvm::to_underlying(dxbc::RootParameterType::UAV): + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::SRV: + case dxbc::RootParameterType::UAV: if (Version == 1) Size += sizeof(dxbc::RTS0::v1::RootDescriptor); else Size += sizeof(dxbc::RTS0::v2::RootDescriptor); break; - case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): + case dxbc::RootParameterType::DescriptorTable: const DescriptorTable &Table = ParametersContainer.getDescriptorTable(I.Location); @@ -98,7 +98,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]); const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I); switch (Type) { - case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): { + case dxbc::RootParameterType::Constants32Bit: { const dxbc::RTS0::v1::RootConstants &Constants = ParametersContainer.getConstant(Loc); support::endian::write(BOS, Constants.ShaderRegister, @@ -109,9 +109,9 @@ void RootSignatureDesc::write(raw_ostream &OS) const { llvm::endianness::little); break; } - case llvm::to_underlying(dxbc::RootParameterType::CBV): - case llvm::to_underlying(dxbc::RootParameterType::SRV): - case llvm::to_underlying(dxbc::RootParameterType::UAV): { + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::SRV: + case dxbc::RootParameterType::UAV: { const dxbc::RTS0::v2::RootDescriptor &Descriptor = ParametersContainer.getRootDescriptor(Loc); @@ -123,7 +123,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little); break; } - case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): { + case dxbc::RootParameterType::DescriptorTable: { const DescriptorTable &Table = ParametersContainer.getDescriptorTable(Loc); support::endian::write(BOS, (uint32_t)Table.Ranges.size(), diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp index 043b575a43b11..0eb73d4c0beb5 100644 --- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp +++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp @@ -275,11 +275,14 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { for (DXContainerYAML::RootParameterLocationYaml &L : P.RootSignature->Parameters.Locations) { - 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): { + mcdxbc::RootParameterHeader Header{ + static_cast<dxbc::RootParameterType>(L.Header.Type), + static_cast<dxbc::ShaderVisibility>(L.Header.Visibility), + L.Header.Offset}; + + switch (Header.ParameterType) { + case dxbc::RootParameterType::Constants32Bit: { const DXContainerYAML::RootConstantsYaml &ConstantYaml = P.RootSignature->Parameters.getOrInsertConstants(L); dxbc::RTS0::v1::RootConstants Constants; @@ -289,9 +292,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { RS.ParametersContainer.addParameter(Header, Constants); break; } - case llvm::to_underlying(dxbc::RootParameterType::CBV): - case llvm::to_underlying(dxbc::RootParameterType::SRV): - case llvm::to_underlying(dxbc::RootParameterType::UAV): { + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::SRV: + case dxbc::RootParameterType::UAV: { const DXContainerYAML::RootDescriptorYaml &DescriptorYaml = P.RootSignature->Parameters.getOrInsertDescriptor(L); @@ -303,7 +306,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { RS.ParametersContainer.addParameter(Header, Descriptor); break; } - case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): { + case dxbc::RootParameterType::DescriptorTable: { const DXContainerYAML::DescriptorTableYaml &TableYaml = P.RootSignature->Parameters.getOrInsertTable(L); mcdxbc::DescriptorTable Table; diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index a4f5086c2f428..b31f811d76bf9 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -27,6 +27,7 @@ #include "llvm/Pass.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" #include <cstdint> @@ -173,14 +174,18 @@ 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::RTS0::v1::RootParameterHeader Header = + const mcdxbc::RootParameterHeader Header = RS.ParametersContainer.getHeader(I); - OS << "- Parameter Type: " << Type << "\n" - << " Shader Visibility: " << Header.ShaderVisibility << "\n"; + OS << "- Parameter Type: " + << enumToStringRef(Type, dxbc::getRootParameterTypes()) << "\n" + << " Shader Visibility: " + << enumToStringRef(Header.ShaderVisibility, + dxbc::getShaderVisibility()) + << "\n"; switch (Type) { - case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): { + case dxbc::RootParameterType::Constants32Bit: { const dxbc::RTS0::v1::RootConstants &Constants = RS.ParametersContainer.getConstant(Loc); OS << " Register Space: " << Constants.RegisterSpace << "\n" @@ -188,9 +193,9 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, << " Num 32 Bit Values: " << Constants.Num32BitValues << "\n"; break; } - case llvm::to_underlying(dxbc::RootParameterType::CBV): - case llvm::to_underlying(dxbc::RootParameterType::UAV): - case llvm::to_underlying(dxbc::RootParameterType::SRV): { + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::UAV: + case dxbc::RootParameterType::SRV: { const dxbc::RTS0::v2::RootDescriptor &Descriptor = RS.ParametersContainer.getRootDescriptor(Loc); OS << " Register Space: " << Descriptor.RegisterSpace << "\n" @@ -199,7 +204,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, OS << " Flags: " << Descriptor.Flags << "\n"; break; } - case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): { + case dxbc::RootParameterType::DescriptorTable: { const mcdxbc::DescriptorTable &Table = RS.ParametersContainer.getDescriptorTable(Loc); OS << " NumRanges: " << Table.Ranges.size() << "\n"; diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index 6477ad397c32d..fd3dbb01cab11 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -21,34 +21,34 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !9 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } ;CHECK-LABEL: Definition for 'main': -;CHECK-NEXT: Flags: 0x000001 -;CHECK-NEXT: Version: 2 -;CHECK-NEXT: RootParametersOffset: 24 -;CHECK-NEXT: NumParameters: 3 -;CHECK-NEXT: - Parameter Type: 1 -;CHECK-NEXT: Shader Visibility: 0 -;CHECK-NEXT: Register Space: 2 -;CHECK-NEXT: Shader Register: 1 -;CHECK-NEXT: Num 32 Bit Values: 3 -;CHECK-NEXT: - Parameter Type: 3 -;CHECK-NEXT: Shader Visibility: 1 -;CHECK-NEXT: Register Space: 5 -;CHECK-NEXT: Shader Register: 4 +;CHECK-NEXT: Flags: 0x000001 +;CHECK-NEXT: Version: 2 +;CHECK-NEXT: RootParametersOffset: 24 +;CHECK-NEXT: NumParameters: 3 +;CHECK-NEXT: - Parameter Type: Constants32Bit +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: Register Space: 2 +;CHECK-NEXT: Shader Register: 1 +;CHECK-NEXT: Num 32 Bit Values: 3 +;CHECK-NEXT: - Parameter Type: SRV +;CHECK-NEXT: Shader Visibility: Vertex +;CHECK-NEXT: Register Space: 5 +;CHECK-NEXT: Shader Register: 4 +;CHECK-NEXT: Flags: 4 +;CHECK-NEXT: - Parameter Type: DescriptorTable +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: NumRanges: 2 +;CHECK-NEXT: - Range Type: 0 +;CHECK-NEXT: Register Space: 0 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 1 +;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 ;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Parameter Type: 0 -;CHECK-NEXT: Shader Visibility: 0 -;CHECK-NEXT: NumRanges: 2 -;CHECK-NEXT: - Range Type: 0 -;CHECK-NEXT: Register Space: 0 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 1 -;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 -;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Range Type: 1 -;CHECK-NEXT: Register Space: 10 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 5 -;CHECK-NEXT: Offset In Descriptors From Table Start: 5 -;CHECK-NEXT: Flags: 2 -;CHECK-NEXT: NumStaticSamplers: 0 -;CHECK-NEXT: StaticSamplersOffset: 0 +;CHECK-NEXT: - Range Type: 1 +;CHECK-NEXT: Register Space: 10 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 5 +;CHECK-NEXT: Offset In Descriptors From Table Start: 5 +;CHECK-NEXT: Flags: 2 +;CHECK-NEXT: NumStaticSamplers: 0 +;CHECK-NEXT: StaticSamplersOffset: 0 >From 1690a9c04dbdd4b04d272f0ab87eea97ff8b8f2d Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Mon, 18 Aug 2025 18:52:48 -0700 Subject: [PATCH 02/15] clean up --- llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp | 2 -- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 1 - 2 files changed, 3 deletions(-) diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 8b9c0d24c34f3..378f93359ddc4 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -12,11 +12,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Frontend/HLSL/RootSignatureMetadata.h" -#include "llvm/BinaryFormat/DXContainer.h" #include "llvm/Frontend/HLSL/RootSignatureValidations.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Metadata.h" -#include "llvm/Support/Error.h" #include "llvm/Support/ScopedPrinter.h" using namespace llvm; diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index b31f811d76bf9..3bc2e39544960 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -27,7 +27,6 @@ #include "llvm/Pass.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" #include <cstdint> >From f6f2e61d5d76c6b98ffc3c25edf964f61afa42a4 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 10:34:02 -0700 Subject: [PATCH 03/15] removing root parameter header from MC --- .../llvm/MC/DXContainerRootSignature.h | 41 +++++++++---------- .../Frontend/HLSL/RootSignatureMetadata.cpp | 29 ++++++------- llvm/lib/MC/DXContainerRootSignature.cpp | 16 ++++---- llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 27 ++++++------ .../DXILPostOptimizationValidation.cpp | 12 +++--- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 14 +++---- .../RootSignature-InvalidType.yaml | 29 ------------- .../RootSignature-InvalidVisibility.yaml | 33 --------------- 8 files changed, 64 insertions(+), 137 deletions(-) delete mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidType.yaml delete mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidVisibility.yaml diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 757ab8bc83129..980557a72ef84 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -11,7 +11,6 @@ #include "llvm/BinaryFormat/DXContainer.h" #include "llvm/Support/Compiler.h" -#include <cstdint> #include <limits> namespace llvm { @@ -26,13 +25,15 @@ struct RootParameterHeader { }; struct RootParameterInfo { - RootParameterHeader Header; + dxbc::RootParameterType Type; + dxbc::ShaderVisibility Visibility; size_t Location; RootParameterInfo() = default; - RootParameterInfo(RootParameterHeader Header, size_t Location) - : Header(Header), Location(Location) {} + RootParameterInfo(dxbc::RootParameterType Type, + dxbc::ShaderVisibility Visibility, size_t Location) + : Type(Type), Visibility(Visibility), Location(Location) {} }; struct DescriptorTable { @@ -52,38 +53,34 @@ struct RootParametersContainer { SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors; SmallVector<DescriptorTable> Tables; - void addInfo(RootParameterHeader Header, size_t Location) { - ParametersInfo.push_back(RootParameterInfo(Header, Location)); + void addInfo(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, + size_t Location) { + ParametersInfo.push_back(RootParameterInfo(Type, Visibility, Location)); } - void addParameter(RootParameterHeader Header, + void addParameter(dxbc::RootParameterType Type, + dxbc::ShaderVisibility Visibility, dxbc::RTS0::v1::RootConstants Constant) { - addInfo(Header, Constants.size()); + addInfo(Type, Visibility, Constants.size()); Constants.push_back(Constant); } - void addInvalidParameter(RootParameterHeader Header) { addInfo(Header, -1); } - - void addParameter(RootParameterHeader Header, + void addParameter(dxbc::RootParameterType Type, + dxbc::ShaderVisibility Visibility, dxbc::RTS0::v2::RootDescriptor Descriptor) { - addInfo(Header, Descriptors.size()); + addInfo(Type, Visibility, Descriptors.size()); Descriptors.push_back(Descriptor); } - void addParameter(RootParameterHeader Header, DescriptorTable Table) { - addInfo(Header, Tables.size()); + void addParameter(dxbc::RootParameterType Type, + dxbc::ShaderVisibility Visibility, DescriptorTable Table) { + addInfo(Type, Visibility, Tables.size()); Tables.push_back(Table); } - std::pair<dxbc::RootParameterType, uint32_t> - getTypeAndLocForParameter(uint32_t Location) const { - const RootParameterInfo &Info = ParametersInfo[Location]; - return {Info.Header.ParameterType, Info.Location}; - } - - const RootParameterHeader &getHeader(size_t Location) const { + const RootParameterInfo &getInfo(uint32_t Location) const { const RootParameterInfo &Info = ParametersInfo[Location]; - return Info.Header; + return Info; } const dxbc::RTS0::v1::RootConstants &getConstant(size_t Index) const { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 378f93359ddc4..2d69369d08ebb 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -236,15 +236,11 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, return make_error<InvalidRSMetadataFormat>("RootConstants Element"); mcdxbc::RootParameterHeader Header; - // The parameter offset doesn't matter here - we recalculate it during - // serialization Header.ParameterOffset = 0; - Header.ParameterType = dxbc::RootParameterType::Constants32Bit; Expected<dxbc::ShaderVisibility> VisibilityOrErr = extractShaderVisibility(RootConstantNode, 1); if (auto E = VisibilityOrErr.takeError()) return Error(std::move(E)); - Header.ShaderVisibility = *VisibilityOrErr; dxbc::RTS0::v1::RootConstants Constants; if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2)) @@ -262,7 +258,10 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, else return make_error<InvalidRSMetadataValue>("Num32BitValues"); - RSD.ParametersContainer.addParameter(Header, Constants); + // The parameter offset doesn't matter here - we recalculate it during + // serialization Header.ParameterOffset = 0; + RSD.ParametersContainer.addParameter(dxbc::RootParameterType::Constants32Bit, + *VisibilityOrErr, Constants); return Error::success(); } @@ -278,16 +277,16 @@ Error MetadataParser::parseRootDescriptors( if (RootDescriptorNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("Root Descriptor Element"); - mcdxbc::RootParameterHeader Header; + dxbc::RootParameterType Type; switch (ElementKind) { case RootSignatureElementKind::SRV: - Header.ParameterType = dxbc::RootParameterType::SRV; + Type = dxbc::RootParameterType::SRV; break; case RootSignatureElementKind::UAV: - Header.ParameterType = dxbc::RootParameterType::UAV; + Type = dxbc::RootParameterType::UAV; break; case RootSignatureElementKind::CBV: - Header.ParameterType = dxbc::RootParameterType::CBV; + Type = dxbc::RootParameterType::CBV; break; default: llvm_unreachable("invalid Root Descriptor kind"); @@ -298,7 +297,6 @@ Error MetadataParser::parseRootDescriptors( extractShaderVisibility(RootDescriptorNode, 1); if (auto E = VisibilityOrErr.takeError()) return Error(std::move(E)); - Header.ShaderVisibility = *VisibilityOrErr; dxbc::RTS0::v2::RootDescriptor Descriptor; if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2)) @@ -312,7 +310,7 @@ Error MetadataParser::parseRootDescriptors( return make_error<InvalidRSMetadataValue>("RegisterSpace"); if (RSD.Version == 1) { - RSD.ParametersContainer.addParameter(Header, Descriptor); + RSD.ParametersContainer.addParameter(Type, *VisibilityOrErr, Descriptor); return Error::success(); } assert(RSD.Version > 1); @@ -322,7 +320,7 @@ Error MetadataParser::parseRootDescriptors( else return make_error<InvalidRSMetadataValue>("Root Descriptor Flags"); - RSD.ParametersContainer.addParameter(Header, Descriptor); + RSD.ParametersContainer.addParameter(Type, *VisibilityOrErr, Descriptor); return Error::success(); } @@ -394,10 +392,8 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, extractShaderVisibility(DescriptorTableNode, 1); if (auto E = VisibilityOrErr.takeError()) return Error(std::move(E)); - Header.ShaderVisibility = *VisibilityOrErr; mcdxbc::DescriptorTable Table; - Header.ParameterType = dxbc::RootParameterType::DescriptorTable; for (unsigned int I = 2; I < NumOperands; I++) { MDNode *Element = dyn_cast<MDNode>(DescriptorTableNode->getOperand(I)); @@ -409,7 +405,8 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, return Err; } - RSD.ParametersContainer.addParameter(Header, Table); + RSD.ParametersContainer.addParameter(dxbc::RootParameterType::DescriptorTable, + *VisibilityOrErr, Table); return Error::success(); } @@ -546,7 +543,7 @@ Error MetadataParser::validateRootSignature( for (const mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) { - switch (Info.Header.ParameterType) { + switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: break; diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index c7be78149a32b..45ecd9a8d5e01 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -9,6 +9,7 @@ #include "llvm/MC/DXContainerRootSignature.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/EndianStream.h" +#include <cstdint> using namespace llvm; using namespace llvm::mcdxbc; @@ -35,7 +36,7 @@ size_t RootSignatureDesc::getSize() const { StaticSamplers.size() * sizeof(dxbc::RTS0::v1::StaticSampler); for (const RootParameterInfo &I : ParametersContainer) { - switch (I.Header.ParameterType) { + switch (I.Type) { case dxbc::RootParameterType::Constants32Bit: Size += sizeof(dxbc::RTS0::v1::RootConstants); break; @@ -84,11 +85,9 @@ void RootSignatureDesc::write(raw_ostream &OS) const { support::endian::write(BOS, Flags, llvm::endianness::little); SmallVector<uint32_t> ParamsOffsets; - for (const RootParameterInfo &P : ParametersContainer) { - support::endian::write(BOS, P.Header.ParameterType, - llvm::endianness::little); - support::endian::write(BOS, P.Header.ShaderVisibility, - llvm::endianness::little); + for (const RootParameterInfo &I : ParametersContainer) { + support::endian::write(BOS, I.Type, llvm::endianness::little); + support::endian::write(BOS, I.Visibility, llvm::endianness::little); ParamsOffsets.push_back(writePlaceholder(BOS)); } @@ -96,8 +95,9 @@ void RootSignatureDesc::write(raw_ostream &OS) const { assert(NumParameters == ParamsOffsets.size()); for (size_t I = 0; I < NumParameters; ++I) { rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]); - const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I); - switch (Type) { + const auto Info = ParametersContainer.getInfo(I); + const uint32_t &Loc = Info.Location; + switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: { const dxbc::RTS0::v1::RootConstants &Constants = ParametersContainer.getConstant(Loc); diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp index 0eb73d4c0beb5..b112c6f21ee5a 100644 --- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp +++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp @@ -276,20 +276,24 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { for (DXContainerYAML::RootParameterLocationYaml &L : P.RootSignature->Parameters.Locations) { - mcdxbc::RootParameterHeader Header{ - static_cast<dxbc::RootParameterType>(L.Header.Type), - static_cast<dxbc::ShaderVisibility>(L.Header.Visibility), - L.Header.Offset}; - - switch (Header.ParameterType) { + assert(dxbc::isValidParameterType(L.Header.Type) && + "invalid DXContainer YAML"); + assert(dxbc::isValidShaderVisibility(L.Header.Visibility) && + "invalid DXContainer YAML"); + dxbc::RootParameterType Type = dxbc::RootParameterType(L.Header.Type); + dxbc::ShaderVisibility Visibility = + dxbc::ShaderVisibility(L.Header.Visibility); + + switch (Type) { case dxbc::RootParameterType::Constants32Bit: { const DXContainerYAML::RootConstantsYaml &ConstantYaml = P.RootSignature->Parameters.getOrInsertConstants(L); dxbc::RTS0::v1::RootConstants Constants; + Constants.Num32BitValues = ConstantYaml.Num32BitValues; Constants.RegisterSpace = ConstantYaml.RegisterSpace; Constants.ShaderRegister = ConstantYaml.ShaderRegister; - RS.ParametersContainer.addParameter(Header, Constants); + RS.ParametersContainer.addParameter(Type, Visibility, Constants); break; } case dxbc::RootParameterType::CBV: @@ -303,7 +307,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister; if (RS.Version > 1) Descriptor.Flags = DescriptorYaml.getEncodedFlags(); - RS.ParametersContainer.addParameter(Header, Descriptor); + RS.ParametersContainer.addParameter(Type, Visibility, Descriptor); break; } case dxbc::RootParameterType::DescriptorTable: { @@ -323,14 +327,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { Range.Flags = R.getEncodedFlags(); Table.Ranges.push_back(Range); } - RS.ParametersContainer.addParameter(Header, Table); + RS.ParametersContainer.addParameter(Type, Visibility, Table); break; } - default: - // 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. - RS.ParametersContainer.addInvalidParameter(Header); } } diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index be2c7d1ddff3f..8738e3071f824 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -164,12 +164,12 @@ static void validateRootSignature(Module &M, for (const mcdxbc::RootParameterInfo &ParamInfo : RSD.ParametersContainer) { dxbc::ShaderVisibility ParamVisibility = - static_cast<dxbc::ShaderVisibility>(ParamInfo.Header.ShaderVisibility); + static_cast<dxbc::ShaderVisibility>(ParamInfo.Visibility); if (ParamVisibility != dxbc::ShaderVisibility::All && ParamVisibility != Visibility) continue; dxbc::RootParameterType ParamType = - static_cast<dxbc::RootParameterType>(ParamInfo.Header.ParameterType); + static_cast<dxbc::RootParameterType>(ParamInfo.Type); switch (ParamType) { case dxbc::RootParameterType::Constants32Bit: { dxbc::RTS0::v1::RootConstants Const = @@ -185,10 +185,10 @@ static void validateRootSignature(Module &M, case dxbc::RootParameterType::CBV: { dxbc::RTS0::v2::RootDescriptor Desc = RSD.ParametersContainer.getRootDescriptor(ParamInfo.Location); - Builder.trackBinding(toResourceClass(static_cast<dxbc::RootParameterType>( - ParamInfo.Header.ParameterType)), - Desc.RegisterSpace, Desc.ShaderRegister, - Desc.ShaderRegister, &ParamInfo); + Builder.trackBinding( + toResourceClass(static_cast<dxbc::RootParameterType>(ParamInfo.Type)), + Desc.RegisterSpace, Desc.ShaderRegister, Desc.ShaderRegister, + &ParamInfo); break; } diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 3bc2e39544960..159cf7eda2482 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -171,19 +171,15 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, << "RootParametersOffset: " << RS.RootParameterOffset << "\n" << "NumParameters: " << RS.ParametersContainer.size() << "\n"; for (size_t I = 0; I < RS.ParametersContainer.size(); I++) { - const auto &[Type, Loc] = - RS.ParametersContainer.getTypeAndLocForParameter(I); - const mcdxbc::RootParameterHeader Header = - RS.ParametersContainer.getHeader(I); + const auto &Info = RS.ParametersContainer.getInfo(I); OS << "- Parameter Type: " - << enumToStringRef(Type, dxbc::getRootParameterTypes()) << "\n" + << enumToStringRef(Info.Type, dxbc::getRootParameterTypes()) << "\n" << " Shader Visibility: " - << enumToStringRef(Header.ShaderVisibility, - dxbc::getShaderVisibility()) + << enumToStringRef(Info.Visibility, dxbc::getShaderVisibility()) << "\n"; - - switch (Type) { + const uint32_t &Loc = Info.Location; + switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: { const dxbc::RTS0::v1::RootConstants &Constants = RS.ParametersContainer.getConstant(Loc); diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidType.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidType.yaml deleted file mode 100644 index 091e70789d956..0000000000000 --- a/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidType.yaml +++ /dev/null @@ -1,29 +0,0 @@ -# RUN: yaml2obj %s -o %t -# RUN: not obj2yaml 2>&1 %t | FileCheck %s -DFILE=%t - -# CHECK: Error reading file: [[FILE]]: Invalid value for parameter type - - ---- !dxcontainer -Header: - Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ] - Version: - Major: 1 - Minor: 0 - PartCount: 1 - PartOffsets: [ 60 ] -Parts: - - Name: RTS0 - Size: 80 - RootSignature: - Version: 2 - NumRootParameters: 2 - RootParametersOffset: 24 - NumStaticSamplers: 0 - StaticSamplersOffset: 64 - Parameters: - - ParameterType: 255 # INVALID - ShaderVisibility: 2 # Hull - AllowInputAssemblerInputLayout: true - DenyGeometryShaderRootAccess: true diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidVisibility.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidVisibility.yaml deleted file mode 100644 index 1acaf6e4e08a4..0000000000000 --- a/llvm/test/ObjectYAML/DXContainer/RootSignature-InvalidVisibility.yaml +++ /dev/null @@ -1,33 +0,0 @@ -# RUN: yaml2obj %s -o %t -# RUN: not obj2yaml 2>&1 %t | FileCheck %s -DFILE=%t - -# CHECK: Error reading file: [[FILE]]: Invalid value for shader visibility - - ---- !dxcontainer -Header: - Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ] - Version: - Major: 1 - Minor: 0 - PartCount: 1 - PartOffsets: [ 60 ] -Parts: - - Name: RTS0 - Size: 80 - RootSignature: - Version: 2 - NumRootParameters: 2 - RootParametersOffset: 24 - NumStaticSamplers: 0 - StaticSamplersOffset: 64 - Parameters: - - ParameterType: 1 # Constants32Bit - ShaderVisibility: 255 # INVALID - Constants: - Num32BitValues: 21 - ShaderRegister: 22 - RegisterSpace: 23 - AllowInputAssemblerInputLayout: true - DenyGeometryShaderRootAccess: true >From 6539364fa7b218bfe4f6743727f9ce21ec10e562 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 10:45:49 -0700 Subject: [PATCH 04/15] clean up --- .../llvm/MC/DXContainerRootSignature.h | 1 + .../Frontend/HLSL/RootSignatureMetadata.cpp | 4 +- .../DXILPostOptimizationValidation.cpp | 12 ++-- .../ContainerData/RootSignature-Parameters.ll | 62 +++++++++---------- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 980557a72ef84..149de9fbd8b47 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -11,6 +11,7 @@ #include "llvm/BinaryFormat/DXContainer.h" #include "llvm/Support/Compiler.h" +#include <cstdint> #include <limits> namespace llvm { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 2d69369d08ebb..4dbcf05fa5668 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -57,7 +57,7 @@ extractShaderVisibility(MDNode *Node, unsigned int OpId) { if (!dxbc::isValidShaderVisibility(*Val)) return make_error<RootSignatureValidationError<uint32_t>>( "ShaderVisibility", *Val); - return static_cast<dxbc::ShaderVisibility>(*Val); + return dxbc::ShaderVisibility(*Val); } return make_error<InvalidRSMetadataValue>("ShaderVisibility"); } @@ -258,8 +258,6 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, else return make_error<InvalidRSMetadataValue>("Num32BitValues"); - // The parameter offset doesn't matter here - we recalculate it during - // serialization Header.ParameterOffset = 0; RSD.ParametersContainer.addParameter(dxbc::RootParameterType::Constants32Bit, *VisibilityOrErr, Constants); diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index 8738e3071f824..fc0afb9a0efdf 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -164,12 +164,11 @@ static void validateRootSignature(Module &M, for (const mcdxbc::RootParameterInfo &ParamInfo : RSD.ParametersContainer) { dxbc::ShaderVisibility ParamVisibility = - static_cast<dxbc::ShaderVisibility>(ParamInfo.Visibility); + dxbc::ShaderVisibility(ParamInfo.Visibility); if (ParamVisibility != dxbc::ShaderVisibility::All && ParamVisibility != Visibility) continue; - dxbc::RootParameterType ParamType = - static_cast<dxbc::RootParameterType>(ParamInfo.Type); + dxbc::RootParameterType ParamType = dxbc::RootParameterType(ParamInfo.Type); switch (ParamType) { case dxbc::RootParameterType::Constants32Bit: { dxbc::RTS0::v1::RootConstants Const = @@ -185,10 +184,9 @@ static void validateRootSignature(Module &M, case dxbc::RootParameterType::CBV: { dxbc::RTS0::v2::RootDescriptor Desc = RSD.ParametersContainer.getRootDescriptor(ParamInfo.Location); - Builder.trackBinding( - toResourceClass(static_cast<dxbc::RootParameterType>(ParamInfo.Type)), - Desc.RegisterSpace, Desc.ShaderRegister, Desc.ShaderRegister, - &ParamInfo); + Builder.trackBinding(toResourceClass(ParamInfo.Type), Desc.RegisterSpace, + Desc.ShaderRegister, Desc.ShaderRegister, + &ParamInfo); break; } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index fd3dbb01cab11..b6cc7b456bdc4 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -21,34 +21,34 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !9 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } ;CHECK-LABEL: Definition for 'main': -;CHECK-NEXT: Flags: 0x000001 -;CHECK-NEXT: Version: 2 -;CHECK-NEXT: RootParametersOffset: 24 -;CHECK-NEXT: NumParameters: 3 -;CHECK-NEXT: - Parameter Type: Constants32Bit -;CHECK-NEXT: Shader Visibility: All -;CHECK-NEXT: Register Space: 2 -;CHECK-NEXT: Shader Register: 1 -;CHECK-NEXT: Num 32 Bit Values: 3 -;CHECK-NEXT: - Parameter Type: SRV -;CHECK-NEXT: Shader Visibility: Vertex -;CHECK-NEXT: Register Space: 5 -;CHECK-NEXT: Shader Register: 4 -;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Parameter Type: DescriptorTable -;CHECK-NEXT: Shader Visibility: All -;CHECK-NEXT: NumRanges: 2 -;CHECK-NEXT: - Range Type: 0 -;CHECK-NEXT: Register Space: 0 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 1 -;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 -;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Range Type: 1 -;CHECK-NEXT: Register Space: 10 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 5 -;CHECK-NEXT: Offset In Descriptors From Table Start: 5 -;CHECK-NEXT: Flags: 2 -;CHECK-NEXT: NumStaticSamplers: 0 -;CHECK-NEXT: StaticSamplersOffset: 0 +;CHECK-NEXT: Flags: 0x000001 +;CHECK-NEXT: Version: 2 +;CHECK-NEXT: RootParametersOffset: 24 +;CHECK-NEXT: NumParameters: 3 +;CHECK-NEXT: - Parameter Type: Constants32Bit +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: Register Space: 2 +;CHECK-NEXT: Shader Register: 1 +;CHECK-NEXT: Num 32 Bit Values: 3 +;CHECK-NEXT: - Parameter Type: SRV +;CHECK-NEXT: Shader Visibility: Vertex +;CHECK-NEXT: Register Space: 5 +;CHECK-NEXT: Shader Register: 4 +;CHECK-NEXT: Flags: 4 +;CHECK-NEXT: - Parameter Type: DescriptorTable +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: NumRanges: 2 +;CHECK-NEXT: - Range Type: 0 +;CHECK-NEXT: Register Space: 0 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 1 +;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 +;CHECK-NEXT: Flags: 4 +;CHECK-NEXT: - Range Type: 1 +;CHECK-NEXT: Register Space: 10 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 5 +;CHECK-NEXT: Offset In Descriptors From Table Start: 5 +;CHECK-NEXT: Flags: 2 +;CHECK-NEXT: NumStaticSamplers: 0 +;CHECK-NEXT: StaticSamplersOffset: 0 >From 1d29111c4f5a298572f50dd19f9838610a4a3778 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 10:47:15 -0700 Subject: [PATCH 05/15] fix whitespace in test --- .../ContainerData/RootSignature-Parameters.ll | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index b6cc7b456bdc4..742fea14f5af6 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -25,30 +25,30 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ;CHECK-NEXT: Version: 2 ;CHECK-NEXT: RootParametersOffset: 24 ;CHECK-NEXT: NumParameters: 3 -;CHECK-NEXT: - Parameter Type: Constants32Bit -;CHECK-NEXT: Shader Visibility: All -;CHECK-NEXT: Register Space: 2 -;CHECK-NEXT: Shader Register: 1 -;CHECK-NEXT: Num 32 Bit Values: 3 -;CHECK-NEXT: - Parameter Type: SRV -;CHECK-NEXT: Shader Visibility: Vertex -;CHECK-NEXT: Register Space: 5 -;CHECK-NEXT: Shader Register: 4 -;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Parameter Type: DescriptorTable -;CHECK-NEXT: Shader Visibility: All -;CHECK-NEXT: NumRanges: 2 -;CHECK-NEXT: - Range Type: 0 -;CHECK-NEXT: Register Space: 0 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 1 -;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 -;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Range Type: 1 -;CHECK-NEXT: Register Space: 10 -;CHECK-NEXT: Base Shader Register: 1 -;CHECK-NEXT: Num Descriptors: 5 -;CHECK-NEXT: Offset In Descriptors From Table Start: 5 -;CHECK-NEXT: Flags: 2 +;CHECK-NEXT: - Parameter Type: Constants32Bit +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: Register Space: 2 +;CHECK-NEXT: Shader Register: 1 +;CHECK-NEXT: Num 32 Bit Values: 3 +;CHECK-NEXT: - Parameter Type: SRV +;CHECK-NEXT: Shader Visibility: Vertex +;CHECK-NEXT: Register Space: 5 +;CHECK-NEXT: Shader Register: 4 +;CHECK-NEXT: Flags: 4 +;CHECK-NEXT: - Parameter Type: DescriptorTable +;CHECK-NEXT: Shader Visibility: All +;CHECK-NEXT: NumRanges: 2 +;CHECK-NEXT: - Range Type: 0 +;CHECK-NEXT: Register Space: 0 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 1 +;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 +;CHECK-NEXT: Flags: 4 +;CHECK-NEXT: - Range Type: 1 +;CHECK-NEXT: Register Space: 10 +;CHECK-NEXT: Base Shader Register: 1 +;CHECK-NEXT: Num Descriptors: 5 +;CHECK-NEXT: Offset In Descriptors From Table Start: 5 +;CHECK-NEXT: Flags: 2 ;CHECK-NEXT: NumStaticSamplers: 0 ;CHECK-NEXT: StaticSamplersOffset: 0 >From 8eb82fdac3a0b7e632ffc644c369175aba9794c5 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 10:56:17 -0700 Subject: [PATCH 06/15] adding missing import --- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 159cf7eda2482..2ca2ecff5a55f 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -27,6 +27,7 @@ #include "llvm/Pass.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" #include <cstdint> >From d38c00d09a9fe8e9dbbb66dca57bc540516acdf0 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:33:07 -0700 Subject: [PATCH 07/15] remove unused --- llvm/include/llvm/MC/DXContainerRootSignature.h | 6 ------ llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp | 2 -- 2 files changed, 8 deletions(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 149de9fbd8b47..6460d70943911 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -19,12 +19,6 @@ namespace llvm { class raw_ostream; namespace mcdxbc { -struct RootParameterHeader { - dxbc::RootParameterType ParameterType; - dxbc::ShaderVisibility ShaderVisibility; - uint32_t ParameterOffset; -}; - struct RootParameterInfo { dxbc::RootParameterType Type; dxbc::ShaderVisibility Visibility; diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 4dbcf05fa5668..14001b7bd6428 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -235,8 +235,6 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, if (RootConstantNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("RootConstants Element"); - mcdxbc::RootParameterHeader Header; - Expected<dxbc::ShaderVisibility> VisibilityOrErr = extractShaderVisibility(RootConstantNode, 1); if (auto E = VisibilityOrErr.takeError()) >From 3b25b34cd95d917b665ae53df5a53ec77dfeadd9 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:35:05 -0700 Subject: [PATCH 08/15] save a copy --- llvm/include/llvm/MC/DXContainerRootSignature.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 6460d70943911..304daaad9cd5f 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -50,7 +50,7 @@ struct RootParametersContainer { void addInfo(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, size_t Location) { - ParametersInfo.push_back(RootParameterInfo(Type, Visibility, Location)); + ParametersInfo.emplace_back(Type, Visibility, Location); } void addParameter(dxbc::RootParameterType Type, >From 567a3d4efe867c88b49b6a2ecc614f205ffd1f1c Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:35:55 -0700 Subject: [PATCH 09/15] remove default constructor --- llvm/include/llvm/MC/DXContainerRootSignature.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 304daaad9cd5f..4db3f3458c808 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -24,8 +24,6 @@ struct RootParameterInfo { dxbc::ShaderVisibility Visibility; size_t Location; - RootParameterInfo() = default; - RootParameterInfo(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, size_t Location) : Type(Type), Visibility(Visibility), Location(Location) {} >From fb248daeef03d261ee3baff56f181fba997b3ea0 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:36:56 -0700 Subject: [PATCH 10/15] rename visibility --- .../Frontend/HLSL/RootSignatureMetadata.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 14001b7bd6428..610f889e8d7c6 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -235,9 +235,9 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, if (RootConstantNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("RootConstants Element"); - Expected<dxbc::ShaderVisibility> VisibilityOrErr = + Expected<dxbc::ShaderVisibility> Visibility = extractShaderVisibility(RootConstantNode, 1); - if (auto E = VisibilityOrErr.takeError()) + if (auto E = Visibility.takeError()) return Error(std::move(E)); dxbc::RTS0::v1::RootConstants Constants; @@ -257,7 +257,7 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, return make_error<InvalidRSMetadataValue>("Num32BitValues"); RSD.ParametersContainer.addParameter(dxbc::RootParameterType::Constants32Bit, - *VisibilityOrErr, Constants); + *Visibility, Constants); return Error::success(); } @@ -289,9 +289,9 @@ Error MetadataParser::parseRootDescriptors( break; } - Expected<dxbc::ShaderVisibility> VisibilityOrErr = + Expected<dxbc::ShaderVisibility> Visibility = extractShaderVisibility(RootDescriptorNode, 1); - if (auto E = VisibilityOrErr.takeError()) + if (auto E = Visibility.takeError()) return Error(std::move(E)); dxbc::RTS0::v2::RootDescriptor Descriptor; @@ -306,7 +306,7 @@ Error MetadataParser::parseRootDescriptors( return make_error<InvalidRSMetadataValue>("RegisterSpace"); if (RSD.Version == 1) { - RSD.ParametersContainer.addParameter(Type, *VisibilityOrErr, Descriptor); + RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor); return Error::success(); } assert(RSD.Version > 1); @@ -316,7 +316,7 @@ Error MetadataParser::parseRootDescriptors( else return make_error<InvalidRSMetadataValue>("Root Descriptor Flags"); - RSD.ParametersContainer.addParameter(Type, *VisibilityOrErr, Descriptor); + RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor); return Error::success(); } @@ -382,11 +382,9 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, if (NumOperands < 2) return make_error<InvalidRSMetadataFormat>("Descriptor Table"); - mcdxbc::RootParameterHeader Header; - - Expected<dxbc::ShaderVisibility> VisibilityOrErr = + Expected<dxbc::ShaderVisibility> Visibility = extractShaderVisibility(DescriptorTableNode, 1); - if (auto E = VisibilityOrErr.takeError()) + if (auto E = Visibility.takeError()) return Error(std::move(E)); mcdxbc::DescriptorTable Table; @@ -402,7 +400,7 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, } RSD.ParametersContainer.addParameter(dxbc::RootParameterType::DescriptorTable, - *VisibilityOrErr, Table); + *Visibility, Table); return Error::success(); } >From dc436d553be7e4b29530c9007f1c172ed72e7189 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:37:45 -0700 Subject: [PATCH 11/15] remove cstdint --- llvm/lib/MC/DXContainerRootSignature.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 45ecd9a8d5e01..27e715f16a4a1 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -9,7 +9,6 @@ #include "llvm/MC/DXContainerRootSignature.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/EndianStream.h" -#include <cstdint> using namespace llvm; using namespace llvm::mcdxbc; >From 8353fe0d0a6e5d8e1016c7d1b6b25a3b4b23f058 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Tue, 19 Aug 2025 16:38:47 -0700 Subject: [PATCH 12/15] remove Loc --- llvm/lib/MC/DXContainerRootSignature.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 27e715f16a4a1..14c9c8866bb24 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -95,11 +95,10 @@ void RootSignatureDesc::write(raw_ostream &OS) const { for (size_t I = 0; I < NumParameters; ++I) { rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]); const auto Info = ParametersContainer.getInfo(I); - const uint32_t &Loc = Info.Location; switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: { const dxbc::RTS0::v1::RootConstants &Constants = - ParametersContainer.getConstant(Loc); + ParametersContainer.getConstant(Info.Location); support::endian::write(BOS, Constants.ShaderRegister, llvm::endianness::little); support::endian::write(BOS, Constants.RegisterSpace, @@ -112,7 +111,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { case dxbc::RootParameterType::SRV: case dxbc::RootParameterType::UAV: { const dxbc::RTS0::v2::RootDescriptor &Descriptor = - ParametersContainer.getRootDescriptor(Loc); + ParametersContainer.getRootDescriptor(Info.Location); support::endian::write(BOS, Descriptor.ShaderRegister, llvm::endianness::little); @@ -124,7 +123,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { } case dxbc::RootParameterType::DescriptorTable: { const DescriptorTable &Table = - ParametersContainer.getDescriptorTable(Loc); + ParametersContainer.getDescriptorTable(Info.Location); support::endian::write(BOS, (uint32_t)Table.Ranges.size(), llvm::endianness::little); rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS)); >From f3ecd8ae8c4af29d23d8c82c6a9104b615c24473 Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Wed, 20 Aug 2025 10:15:37 -0700 Subject: [PATCH 13/15] removing dependency of Object --- .../llvm/MC/DXContainerRootSignature.h | 25 +++++++++++++------ .../Frontend/HLSL/RootSignatureMetadata.cpp | 6 ++--- llvm/lib/MC/DXContainerRootSignature.cpp | 4 +-- llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 4 +-- .../DXILPostOptimizationValidation.cpp | 4 +-- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 4 +-- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 4db3f3458c808..85b45323cee08 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -19,6 +19,18 @@ namespace llvm { class raw_ostream; namespace mcdxbc { +struct RootConstants { + uint32_t ShaderRegister; + uint32_t RegisterSpace; + uint32_t Num32BitValues; +}; + +struct RootDescriptor { + uint32_t ShaderRegister; + uint32_t RegisterSpace; + uint32_t Flags; +}; + struct RootParameterInfo { dxbc::RootParameterType Type; dxbc::ShaderVisibility Visibility; @@ -42,8 +54,8 @@ struct DescriptorTable { struct RootParametersContainer { SmallVector<RootParameterInfo> ParametersInfo; - SmallVector<dxbc::RTS0::v1::RootConstants> Constants; - SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors; + SmallVector<RootConstants> Constants; + SmallVector<RootDescriptor> Descriptors; SmallVector<DescriptorTable> Tables; void addInfo(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, @@ -52,15 +64,14 @@ struct RootParametersContainer { } void addParameter(dxbc::RootParameterType Type, - dxbc::ShaderVisibility Visibility, - dxbc::RTS0::v1::RootConstants Constant) { + dxbc::ShaderVisibility Visibility, RootConstants Constant) { addInfo(Type, Visibility, Constants.size()); Constants.push_back(Constant); } void addParameter(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, - dxbc::RTS0::v2::RootDescriptor Descriptor) { + RootDescriptor Descriptor) { addInfo(Type, Visibility, Descriptors.size()); Descriptors.push_back(Descriptor); } @@ -76,11 +87,11 @@ struct RootParametersContainer { return Info; } - const dxbc::RTS0::v1::RootConstants &getConstant(size_t Index) const { + const RootConstants &getConstant(size_t Index) const { return Constants[Index]; } - const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const { + const RootDescriptor &getRootDescriptor(size_t Index) const { return Descriptors[Index]; } diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 610f889e8d7c6..770a6d1638e3c 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -240,7 +240,7 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, if (auto E = Visibility.takeError()) return Error(std::move(E)); - dxbc::RTS0::v1::RootConstants Constants; + mcdxbc::RootConstants Constants; if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2)) Constants.ShaderRegister = *Val; else @@ -294,7 +294,7 @@ Error MetadataParser::parseRootDescriptors( if (auto E = Visibility.takeError()) return Error(std::move(E)); - dxbc::RTS0::v2::RootDescriptor Descriptor; + mcdxbc::RootDescriptor Descriptor; if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2)) Descriptor.ShaderRegister = *Val; else @@ -544,7 +544,7 @@ Error MetadataParser::validateRootSignature( case dxbc::RootParameterType::CBV: case dxbc::RootParameterType::UAV: case dxbc::RootParameterType::SRV: { - const dxbc::RTS0::v2::RootDescriptor &Descriptor = + const mcdxbc::RootDescriptor &Descriptor = RSD.ParametersContainer.getRootDescriptor(Info.Location); if (!hlsl::rootsig::verifyRegisterValue(Descriptor.ShaderRegister)) DeferredErrs = diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 14c9c8866bb24..94119d8e89ec9 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -97,7 +97,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { const auto Info = ParametersContainer.getInfo(I); switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: { - const dxbc::RTS0::v1::RootConstants &Constants = + const mcdxbc::RootConstants &Constants = ParametersContainer.getConstant(Info.Location); support::endian::write(BOS, Constants.ShaderRegister, llvm::endianness::little); @@ -110,7 +110,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { case dxbc::RootParameterType::CBV: case dxbc::RootParameterType::SRV: case dxbc::RootParameterType::UAV: { - const dxbc::RTS0::v2::RootDescriptor &Descriptor = + const mcdxbc::RootDescriptor &Descriptor = ParametersContainer.getRootDescriptor(Info.Location); support::endian::write(BOS, Descriptor.ShaderRegister, diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp index b112c6f21ee5a..a51821e196cb8 100644 --- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp +++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp @@ -288,7 +288,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { case dxbc::RootParameterType::Constants32Bit: { const DXContainerYAML::RootConstantsYaml &ConstantYaml = P.RootSignature->Parameters.getOrInsertConstants(L); - dxbc::RTS0::v1::RootConstants Constants; + mcdxbc::RootConstants Constants; Constants.Num32BitValues = ConstantYaml.Num32BitValues; Constants.RegisterSpace = ConstantYaml.RegisterSpace; @@ -302,7 +302,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { const DXContainerYAML::RootDescriptorYaml &DescriptorYaml = P.RootSignature->Parameters.getOrInsertDescriptor(L); - dxbc::RTS0::v2::RootDescriptor Descriptor; + mcdxbc::RootDescriptor Descriptor; Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace; Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister; if (RS.Version > 1) diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index fc0afb9a0efdf..5557443a3db93 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -171,7 +171,7 @@ static void validateRootSignature(Module &M, dxbc::RootParameterType ParamType = dxbc::RootParameterType(ParamInfo.Type); switch (ParamType) { case dxbc::RootParameterType::Constants32Bit: { - dxbc::RTS0::v1::RootConstants Const = + mcdxbc::RootConstants Const = RSD.ParametersContainer.getConstant(ParamInfo.Location); Builder.trackBinding(dxil::ResourceClass::CBuffer, Const.RegisterSpace, Const.ShaderRegister, Const.ShaderRegister, @@ -182,7 +182,7 @@ static void validateRootSignature(Module &M, case dxbc::RootParameterType::SRV: case dxbc::RootParameterType::UAV: case dxbc::RootParameterType::CBV: { - dxbc::RTS0::v2::RootDescriptor Desc = + mcdxbc::RootDescriptor Desc = RSD.ParametersContainer.getRootDescriptor(ParamInfo.Location); Builder.trackBinding(toResourceClass(ParamInfo.Type), Desc.RegisterSpace, Desc.ShaderRegister, Desc.ShaderRegister, diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 2ca2ecff5a55f..b05b8ee699ef6 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -182,7 +182,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, const uint32_t &Loc = Info.Location; switch (Info.Type) { case dxbc::RootParameterType::Constants32Bit: { - const dxbc::RTS0::v1::RootConstants &Constants = + const mcdxbc::RootConstants &Constants = RS.ParametersContainer.getConstant(Loc); OS << " Register Space: " << Constants.RegisterSpace << "\n" << " Shader Register: " << Constants.ShaderRegister << "\n" @@ -192,7 +192,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, case dxbc::RootParameterType::CBV: case dxbc::RootParameterType::UAV: case dxbc::RootParameterType::SRV: { - const dxbc::RTS0::v2::RootDescriptor &Descriptor = + const mcdxbc::RootDescriptor &Descriptor = RS.ParametersContainer.getRootDescriptor(Loc); OS << " Register Space: " << Descriptor.RegisterSpace << "\n" << " Shader Register: " << Descriptor.ShaderRegister << "\n"; >From 8c143ba0ce00b198244ff9f598c4ce905e9f10cd Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Wed, 20 Aug 2025 11:55:38 -0700 Subject: [PATCH 14/15] removing binary format descriptor range dependency --- clang/lib/Sema/SemaHLSL.cpp | 31 ++++++++++++++++--- llvm/include/llvm/BinaryFormat/DXContainer.h | 10 ++++++ .../Frontend/HLSL/RootSignatureValidations.h | 3 +- .../llvm/MC/DXContainerRootSignature.h | 15 +++++++-- llvm/lib/BinaryFormat/DXContainer.cpp | 10 ++++++ .../Frontend/HLSL/RootSignatureMetadata.cpp | 25 +++++---------- .../HLSL/RootSignatureValidations.cpp | 5 ++- llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 7 +++-- .../DXILPostOptimizationValidation.cpp | 2 +- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 7 +++-- ...ature-DescriptorTable-Invalid-RangeType.ll | 20 ------------ .../ContainerData/RootSignature-Parameters.ll | 4 +-- 12 files changed, 82 insertions(+), 57 deletions(-) delete mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 47a4874721db8..85dc5eb2b42f8 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -1268,11 +1268,34 @@ bool SemaHLSL::handleRootSignatureElements( // value ReportError(Loc, 1, 0xfffffffe); } + switch (Clause->Type) { - if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( - Version, llvm::to_underlying(Clause->Type), - llvm::to_underlying(Clause->Flags))) - ReportFlagError(Loc); + case llvm::dxil::ResourceClass::SRV: + if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( + Version, llvm::dxbc::DescriptorRangeType::SRV, + llvm::to_underlying(Clause->Flags))) + ReportFlagError(Loc); + break; + case llvm::dxil::ResourceClass::UAV: + if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( + Version, llvm::dxbc::DescriptorRangeType::UAV, + llvm::to_underlying(Clause->Flags))) + ReportFlagError(Loc); + break; + case llvm::dxil::ResourceClass::CBuffer: + if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( + Version, llvm::dxbc::DescriptorRangeType::CBV, + llvm::to_underlying(Clause->Flags))) + ReportFlagError(Loc); + break; + case llvm::dxil::ResourceClass::Sampler: + if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( + Version, llvm::dxbc::DescriptorRangeType::Sampler, + llvm::to_underlying(Clause->Flags))) + ReportFlagError(Loc); + break; + break; + } } } diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h index f74c9775cb3f3..54747b46ccc6c 100644 --- a/llvm/include/llvm/BinaryFormat/DXContainer.h +++ b/llvm/include/llvm/BinaryFormat/DXContainer.h @@ -209,6 +209,16 @@ inline bool isValidParameterType(uint32_t V) { return false; } +#define DESCRIPTOR_RANGE(Val, Enum) \ + case Val: \ + return true; +inline bool isValidRangeType(uint32_t V) { + switch (V) { +#include "DXContainerConstants.def" + } + return false; +} + #define SHADER_VISIBILITY(Val, Enum) Enum = Val, enum class ShaderVisibility : uint32_t { #include "DXContainerConstants.def" diff --git a/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h b/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h index fde32a1fff591..acb83e54b92ed 100644 --- a/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h +++ b/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h @@ -30,7 +30,8 @@ LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue); LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace); LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal); LLVM_ABI bool verifyRangeType(uint32_t Type); -LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, +LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, + dxbc::DescriptorRangeType Type, uint32_t FlagsVal); LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors); LLVM_ABI bool verifySamplerFilter(uint32_t Value); diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 85b45323cee08..28664434f933c 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -31,6 +31,15 @@ struct RootDescriptor { uint32_t Flags; }; +struct DescriptorRange { + dxbc::DescriptorRangeType RangeType; + uint32_t NumDescriptors; + uint32_t BaseShaderRegister; + uint32_t RegisterSpace; + uint32_t Flags; + uint32_t OffsetInDescriptorsFromTableStart; +}; + struct RootParameterInfo { dxbc::RootParameterType Type; dxbc::ShaderVisibility Visibility; @@ -42,11 +51,11 @@ struct RootParameterInfo { }; struct DescriptorTable { - SmallVector<dxbc::RTS0::v2::DescriptorRange> Ranges; - SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator begin() const { + SmallVector<DescriptorRange> Ranges; + SmallVector<DescriptorRange>::const_iterator begin() const { return Ranges.begin(); } - SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator end() const { + SmallVector<DescriptorRange>::const_iterator end() const { return Ranges.end(); } }; diff --git a/llvm/lib/BinaryFormat/DXContainer.cpp b/llvm/lib/BinaryFormat/DXContainer.cpp index 36d10d0b63078..f79db506962bc 100644 --- a/llvm/lib/BinaryFormat/DXContainer.cpp +++ b/llvm/lib/BinaryFormat/DXContainer.cpp @@ -149,6 +149,16 @@ ArrayRef<EnumEntry<RootParameterType>> dxbc::getRootParameterTypes() { return ArrayRef(RootParameterTypes); } +#define DESCRIPTOR_RANGE(Val, Enum) {#Enum, DescriptorRangeType::Enum}, + +static const EnumEntry<DescriptorRangeType> DescriptorRangeTypes[] = { +#include "llvm/BinaryFormat/DXContainerConstants.def" +}; + +ArrayRef<EnumEntry<DescriptorRangeType>> dxbc::getDescriptorRangeTypes() { + return ArrayRef(DescriptorRangeTypes); +} + #define SEMANTIC_KIND(Val, Enum) {#Enum, PSV::SemanticKind::Enum}, static const EnumEntry<PSV::SemanticKind> SemanticKindNames[] = { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 770a6d1638e3c..a5e66e73a5dc1 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -325,7 +325,7 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table, if (RangeDescriptorNode->getNumOperands() != 6) return make_error<InvalidRSMetadataFormat>("Descriptor Range"); - dxbc::RTS0::v2::DescriptorRange Range; + mcdxbc::DescriptorRange Range; std::optional<StringRef> ElementText = extractMdStringValue(RangeDescriptorNode, 0); @@ -333,17 +333,11 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table, if (!ElementText.has_value()) return make_error<InvalidRSMetadataFormat>("Descriptor Range"); - Range.RangeType = - StringSwitch<uint32_t>(*ElementText) - .Case("CBV", to_underlying(dxbc::DescriptorRangeType::CBV)) - .Case("SRV", to_underlying(dxbc::DescriptorRangeType::SRV)) - .Case("UAV", to_underlying(dxbc::DescriptorRangeType::UAV)) - .Case("Sampler", to_underlying(dxbc::DescriptorRangeType::Sampler)) - .Default(~0U); - - if (Range.RangeType == ~0U) - return make_error<GenericRSMetadataError>("Invalid Descriptor Range type.", - RangeDescriptorNode); + Range.RangeType = StringSwitch<dxbc::DescriptorRangeType>(*ElementText) + .Case("CBV", dxbc::DescriptorRangeType::CBV) + .Case("SRV", dxbc::DescriptorRangeType::SRV) + .Case("UAV", dxbc::DescriptorRangeType::UAV) + .Case("Sampler", dxbc::DescriptorRangeType::Sampler); if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 1)) Range.NumDescriptors = *Val; @@ -571,12 +565,7 @@ Error MetadataParser::validateRootSignature( case dxbc::RootParameterType::DescriptorTable: { const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(Info.Location); - for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) { - if (!hlsl::rootsig::verifyRangeType(Range.RangeType)) - DeferredErrs = - joinErrors(std::move(DeferredErrs), - make_error<RootSignatureValidationError<uint32_t>>( - "RangeType", Range.RangeType)); + for (const mcdxbc::DescriptorRange &Range : Table) { if (!hlsl::rootsig::verifyRegisterSpace(Range.RegisterSpace)) DeferredErrs = diff --git a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp index 72308a3de5fd4..5dbeede778b04 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp @@ -63,13 +63,12 @@ bool verifyRangeType(uint32_t Type) { return false; } -bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, +bool verifyDescriptorRangeFlag(uint32_t Version, dxbc::DescriptorRangeType Type, uint32_t FlagsVal) { using FlagT = dxbc::DescriptorRangeFlags; FlagT Flags = FlagT(FlagsVal); - const bool IsSampler = - (Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)); + const bool IsSampler = (Type == dxbc::DescriptorRangeType::Sampler); if (Version == 1) { // Since the metadata is unversioned, we expect to explicitly see the values diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp index a51821e196cb8..a749f47792453 100644 --- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp +++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp @@ -315,9 +315,10 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { P.RootSignature->Parameters.getOrInsertTable(L); mcdxbc::DescriptorTable Table; for (const auto &R : TableYaml.Ranges) { - - dxbc::RTS0::v2::DescriptorRange Range; - Range.RangeType = R.RangeType; + assert(dxbc::isValidRangeType(R.RangeType) && + "Invalid Descriptor Range Type"); + mcdxbc::DescriptorRange Range; + Range.RangeType = dxbc::DescriptorRangeType(R.RangeType); Range.NumDescriptors = R.NumDescriptors; Range.BaseShaderRegister = R.BaseShaderRegister; Range.RegisterSpace = R.RegisterSpace; diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index 5557443a3db93..c6f79621c39de 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -194,7 +194,7 @@ static void validateRootSignature(Module &M, const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(ParamInfo.Location); - for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) { + for (const mcdxbc::DescriptorRange &Range : Table.Ranges) { uint32_t UpperBound = Range.NumDescriptors == ~0U ? Range.BaseShaderRegister diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index b05b8ee699ef6..296e11f3ad5e9 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -205,8 +205,11 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, RS.ParametersContainer.getDescriptorTable(Loc); OS << " NumRanges: " << Table.Ranges.size() << "\n"; - for (const dxbc::RTS0::v2::DescriptorRange Range : Table) { - OS << " - Range Type: " << Range.RangeType << "\n" + for (const mcdxbc::DescriptorRange Range : Table) { + OS << " - Range Type: " + << enumToStringRef(Range.RangeType, + dxbc::getDescriptorRangeTypes()) + << "\n" << " Register Space: " << Range.RegisterSpace << "\n" << " Base Shader Register: " << Range.BaseShaderRegister << "\n" << " Num Descriptors: " << Range.NumDescriptors << "\n" diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll deleted file mode 100644 index 4a65a53f9f727..0000000000000 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll +++ /dev/null @@ -1,20 +0,0 @@ -; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s - -target triple = "dxil-unknown-shadermodel6.0-compute" - -; CHECK: error: Invalid Descriptor Range type -; CHECK-NOT: Root Signature Definitions - -define void @main() #0 { -entry: - ret void -} -attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } - - -!dx.rootsignatures = !{!2} ; list of function/root signature pairs -!2 = !{ ptr @main, !3, i32 2 } ; function, root signature -!3 = !{ !5 } ; list of root signature elements -!5 = !{ !"DescriptorTable", i32 0, !6, !7 } -!6 = !{ !"Invalid", i32 1, i32 0, i32 -1, i32 -1, i32 4 } -!7 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index 742fea14f5af6..6c6739d6ed390 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -38,13 +38,13 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ;CHECK-NEXT: - Parameter Type: DescriptorTable ;CHECK-NEXT: Shader Visibility: All ;CHECK-NEXT: NumRanges: 2 -;CHECK-NEXT: - Range Type: 0 +;CHECK-NEXT: - Range Type: SRV ;CHECK-NEXT: Register Space: 0 ;CHECK-NEXT: Base Shader Register: 1 ;CHECK-NEXT: Num Descriptors: 1 ;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 ;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Range Type: 1 +;CHECK-NEXT: - Range Type: UAV ;CHECK-NEXT: Register Space: 10 ;CHECK-NEXT: Base Shader Register: 1 ;CHECK-NEXT: Num Descriptors: 5 >From a4d77d734011cdd651dd23a67ef5be75b649a59a Mon Sep 17 00:00:00 2001 From: Joao Saffran <joaosaffranl...@gmail.com> Date: Wed, 20 Aug 2025 14:48:32 -0700 Subject: [PATCH 15/15] Revert "removing binary format descriptor range dependency" This reverts commit 8c143ba0ce00b198244ff9f598c4ce905e9f10cd. --- clang/lib/Sema/SemaHLSL.cpp | 31 +++---------------- llvm/include/llvm/BinaryFormat/DXContainer.h | 10 ------ .../Frontend/HLSL/RootSignatureValidations.h | 3 +- .../llvm/MC/DXContainerRootSignature.h | 15 ++------- llvm/lib/BinaryFormat/DXContainer.cpp | 10 ------ .../Frontend/HLSL/RootSignatureMetadata.cpp | 25 ++++++++++----- .../HLSL/RootSignatureValidations.cpp | 5 +-- llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 7 ++--- .../DXILPostOptimizationValidation.cpp | 2 +- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 7 ++--- ...ature-DescriptorTable-Invalid-RangeType.ll | 20 ++++++++++++ .../ContainerData/RootSignature-Parameters.ll | 4 +-- 12 files changed, 57 insertions(+), 82 deletions(-) create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 85dc5eb2b42f8..47a4874721db8 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -1268,34 +1268,11 @@ bool SemaHLSL::handleRootSignatureElements( // value ReportError(Loc, 1, 0xfffffffe); } - switch (Clause->Type) { - case llvm::dxil::ResourceClass::SRV: - if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( - Version, llvm::dxbc::DescriptorRangeType::SRV, - llvm::to_underlying(Clause->Flags))) - ReportFlagError(Loc); - break; - case llvm::dxil::ResourceClass::UAV: - if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( - Version, llvm::dxbc::DescriptorRangeType::UAV, - llvm::to_underlying(Clause->Flags))) - ReportFlagError(Loc); - break; - case llvm::dxil::ResourceClass::CBuffer: - if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( - Version, llvm::dxbc::DescriptorRangeType::CBV, - llvm::to_underlying(Clause->Flags))) - ReportFlagError(Loc); - break; - case llvm::dxil::ResourceClass::Sampler: - if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( - Version, llvm::dxbc::DescriptorRangeType::Sampler, - llvm::to_underlying(Clause->Flags))) - ReportFlagError(Loc); - break; - break; - } + if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag( + Version, llvm::to_underlying(Clause->Type), + llvm::to_underlying(Clause->Flags))) + ReportFlagError(Loc); } } diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h index 54747b46ccc6c..f74c9775cb3f3 100644 --- a/llvm/include/llvm/BinaryFormat/DXContainer.h +++ b/llvm/include/llvm/BinaryFormat/DXContainer.h @@ -209,16 +209,6 @@ inline bool isValidParameterType(uint32_t V) { return false; } -#define DESCRIPTOR_RANGE(Val, Enum) \ - case Val: \ - return true; -inline bool isValidRangeType(uint32_t V) { - switch (V) { -#include "DXContainerConstants.def" - } - return false; -} - #define SHADER_VISIBILITY(Val, Enum) Enum = Val, enum class ShaderVisibility : uint32_t { #include "DXContainerConstants.def" diff --git a/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h b/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h index acb83e54b92ed..fde32a1fff591 100644 --- a/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h +++ b/llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h @@ -30,8 +30,7 @@ LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue); LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace); LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal); LLVM_ABI bool verifyRangeType(uint32_t Type); -LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, - dxbc::DescriptorRangeType Type, +LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, uint32_t FlagsVal); LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors); LLVM_ABI bool verifySamplerFilter(uint32_t Value); diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 28664434f933c..85b45323cee08 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -31,15 +31,6 @@ struct RootDescriptor { uint32_t Flags; }; -struct DescriptorRange { - dxbc::DescriptorRangeType RangeType; - uint32_t NumDescriptors; - uint32_t BaseShaderRegister; - uint32_t RegisterSpace; - uint32_t Flags; - uint32_t OffsetInDescriptorsFromTableStart; -}; - struct RootParameterInfo { dxbc::RootParameterType Type; dxbc::ShaderVisibility Visibility; @@ -51,11 +42,11 @@ struct RootParameterInfo { }; struct DescriptorTable { - SmallVector<DescriptorRange> Ranges; - SmallVector<DescriptorRange>::const_iterator begin() const { + SmallVector<dxbc::RTS0::v2::DescriptorRange> Ranges; + SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator begin() const { return Ranges.begin(); } - SmallVector<DescriptorRange>::const_iterator end() const { + SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator end() const { return Ranges.end(); } }; diff --git a/llvm/lib/BinaryFormat/DXContainer.cpp b/llvm/lib/BinaryFormat/DXContainer.cpp index f79db506962bc..36d10d0b63078 100644 --- a/llvm/lib/BinaryFormat/DXContainer.cpp +++ b/llvm/lib/BinaryFormat/DXContainer.cpp @@ -149,16 +149,6 @@ ArrayRef<EnumEntry<RootParameterType>> dxbc::getRootParameterTypes() { return ArrayRef(RootParameterTypes); } -#define DESCRIPTOR_RANGE(Val, Enum) {#Enum, DescriptorRangeType::Enum}, - -static const EnumEntry<DescriptorRangeType> DescriptorRangeTypes[] = { -#include "llvm/BinaryFormat/DXContainerConstants.def" -}; - -ArrayRef<EnumEntry<DescriptorRangeType>> dxbc::getDescriptorRangeTypes() { - return ArrayRef(DescriptorRangeTypes); -} - #define SEMANTIC_KIND(Val, Enum) {#Enum, PSV::SemanticKind::Enum}, static const EnumEntry<PSV::SemanticKind> SemanticKindNames[] = { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index a5e66e73a5dc1..770a6d1638e3c 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -325,7 +325,7 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table, if (RangeDescriptorNode->getNumOperands() != 6) return make_error<InvalidRSMetadataFormat>("Descriptor Range"); - mcdxbc::DescriptorRange Range; + dxbc::RTS0::v2::DescriptorRange Range; std::optional<StringRef> ElementText = extractMdStringValue(RangeDescriptorNode, 0); @@ -333,11 +333,17 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table, if (!ElementText.has_value()) return make_error<InvalidRSMetadataFormat>("Descriptor Range"); - Range.RangeType = StringSwitch<dxbc::DescriptorRangeType>(*ElementText) - .Case("CBV", dxbc::DescriptorRangeType::CBV) - .Case("SRV", dxbc::DescriptorRangeType::SRV) - .Case("UAV", dxbc::DescriptorRangeType::UAV) - .Case("Sampler", dxbc::DescriptorRangeType::Sampler); + Range.RangeType = + StringSwitch<uint32_t>(*ElementText) + .Case("CBV", to_underlying(dxbc::DescriptorRangeType::CBV)) + .Case("SRV", to_underlying(dxbc::DescriptorRangeType::SRV)) + .Case("UAV", to_underlying(dxbc::DescriptorRangeType::UAV)) + .Case("Sampler", to_underlying(dxbc::DescriptorRangeType::Sampler)) + .Default(~0U); + + if (Range.RangeType == ~0U) + return make_error<GenericRSMetadataError>("Invalid Descriptor Range type.", + RangeDescriptorNode); if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 1)) Range.NumDescriptors = *Val; @@ -565,7 +571,12 @@ Error MetadataParser::validateRootSignature( case dxbc::RootParameterType::DescriptorTable: { const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(Info.Location); - for (const mcdxbc::DescriptorRange &Range : Table) { + for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) { + if (!hlsl::rootsig::verifyRangeType(Range.RangeType)) + DeferredErrs = + joinErrors(std::move(DeferredErrs), + make_error<RootSignatureValidationError<uint32_t>>( + "RangeType", Range.RangeType)); if (!hlsl::rootsig::verifyRegisterSpace(Range.RegisterSpace)) DeferredErrs = diff --git a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp index 5dbeede778b04..72308a3de5fd4 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp @@ -63,12 +63,13 @@ bool verifyRangeType(uint32_t Type) { return false; } -bool verifyDescriptorRangeFlag(uint32_t Version, dxbc::DescriptorRangeType Type, +bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, uint32_t FlagsVal) { using FlagT = dxbc::DescriptorRangeFlags; FlagT Flags = FlagT(FlagsVal); - const bool IsSampler = (Type == dxbc::DescriptorRangeType::Sampler); + const bool IsSampler = + (Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)); if (Version == 1) { // Since the metadata is unversioned, we expect to explicitly see the values diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp index a749f47792453..a51821e196cb8 100644 --- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp +++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp @@ -315,10 +315,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { P.RootSignature->Parameters.getOrInsertTable(L); mcdxbc::DescriptorTable Table; for (const auto &R : TableYaml.Ranges) { - assert(dxbc::isValidRangeType(R.RangeType) && - "Invalid Descriptor Range Type"); - mcdxbc::DescriptorRange Range; - Range.RangeType = dxbc::DescriptorRangeType(R.RangeType); + + dxbc::RTS0::v2::DescriptorRange Range; + Range.RangeType = R.RangeType; Range.NumDescriptors = R.NumDescriptors; Range.BaseShaderRegister = R.BaseShaderRegister; Range.RegisterSpace = R.RegisterSpace; diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index c6f79621c39de..5557443a3db93 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -194,7 +194,7 @@ static void validateRootSignature(Module &M, const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(ParamInfo.Location); - for (const mcdxbc::DescriptorRange &Range : Table.Ranges) { + for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) { uint32_t UpperBound = Range.NumDescriptors == ~0U ? Range.BaseShaderRegister diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 296e11f3ad5e9..b05b8ee699ef6 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -205,11 +205,8 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, RS.ParametersContainer.getDescriptorTable(Loc); OS << " NumRanges: " << Table.Ranges.size() << "\n"; - for (const mcdxbc::DescriptorRange Range : Table) { - OS << " - Range Type: " - << enumToStringRef(Range.RangeType, - dxbc::getDescriptorRangeTypes()) - << "\n" + for (const dxbc::RTS0::v2::DescriptorRange Range : Table) { + OS << " - Range Type: " << Range.RangeType << "\n" << " Register Space: " << Range.RegisterSpace << "\n" << " Base Shader Register: " << Range.BaseShaderRegister << "\n" << " Num Descriptors: " << Range.NumDescriptors << "\n" diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll new file mode 100644 index 0000000000000..4a65a53f9f727 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll @@ -0,0 +1,20 @@ +; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s + +target triple = "dxil-unknown-shadermodel6.0-compute" + +; CHECK: error: Invalid Descriptor Range type +; CHECK-NOT: Root Signature Definitions + +define void @main() #0 { +entry: + ret void +} +attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } + + +!dx.rootsignatures = !{!2} ; list of function/root signature pairs +!2 = !{ ptr @main, !3, i32 2 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"DescriptorTable", i32 0, !6, !7 } +!6 = !{ !"Invalid", i32 1, i32 0, i32 -1, i32 -1, i32 4 } +!7 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index 6c6739d6ed390..742fea14f5af6 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -38,13 +38,13 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ;CHECK-NEXT: - Parameter Type: DescriptorTable ;CHECK-NEXT: Shader Visibility: All ;CHECK-NEXT: NumRanges: 2 -;CHECK-NEXT: - Range Type: SRV +;CHECK-NEXT: - Range Type: 0 ;CHECK-NEXT: Register Space: 0 ;CHECK-NEXT: Base Shader Register: 1 ;CHECK-NEXT: Num Descriptors: 1 ;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 ;CHECK-NEXT: Flags: 4 -;CHECK-NEXT: - Range Type: UAV +;CHECK-NEXT: - Range Type: 1 ;CHECK-NEXT: Register Space: 10 ;CHECK-NEXT: Base Shader Register: 1 ;CHECK-NEXT: Num Descriptors: 5 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits