From a4d77d734011cdd651dd23a67ef5be75b649a59a Mon Sep 17 00:00:00 2001 From: Joao Saffran Date: Wed, 20 Aug 2025 14:48:32 -0700 Subject: [PATCH] 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 85dc5eb2b42f..47a4874721db 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 54747b46ccc6..f74c9775cb3f 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 acb83e54b92e..fde32a1fff59 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 28664434f933..85b45323cee0 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 Ranges; - SmallVector::const_iterator begin() const { + SmallVector Ranges; + SmallVector::const_iterator begin() const { return Ranges.begin(); } - SmallVector::const_iterator end() const { + SmallVector::const_iterator end() const { return Ranges.end(); } }; diff --git a/llvm/lib/BinaryFormat/DXContainer.cpp b/llvm/lib/BinaryFormat/DXContainer.cpp index f79db506962b..36d10d0b6307 100644 --- a/llvm/lib/BinaryFormat/DXContainer.cpp +++ b/llvm/lib/BinaryFormat/DXContainer.cpp @@ -149,16 +149,6 @@ ArrayRef> dxbc::getRootParameterTypes() { return ArrayRef(RootParameterTypes); } -#define DESCRIPTOR_RANGE(Val, Enum) {#Enum, DescriptorRangeType::Enum}, - -static const EnumEntry DescriptorRangeTypes[] = { -#include "llvm/BinaryFormat/DXContainerConstants.def" -}; - -ArrayRef> dxbc::getDescriptorRangeTypes() { - return ArrayRef(DescriptorRangeTypes); -} - #define SEMANTIC_KIND(Val, Enum) {#Enum, PSV::SemanticKind::Enum}, static const EnumEntry SemanticKindNames[] = { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index a5e66e73a5dc..770a6d1638e3 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("Descriptor Range"); - mcdxbc::DescriptorRange Range; + dxbc::RTS0::v2::DescriptorRange Range; std::optional ElementText = extractMdStringValue(RangeDescriptorNode, 0); @@ -333,11 +333,17 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table, if (!ElementText.has_value()) return make_error("Descriptor Range"); - Range.RangeType = StringSwitch(*ElementText) - .Case("CBV", dxbc::DescriptorRangeType::CBV) - .Case("SRV", dxbc::DescriptorRangeType::SRV) - .Case("UAV", dxbc::DescriptorRangeType::UAV) - .Case("Sampler", dxbc::DescriptorRangeType::Sampler); + Range.RangeType = + StringSwitch(*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("Invalid Descriptor Range type.", + RangeDescriptorNode); if (std::optional 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>( + "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 5dbeede778b0..72308a3de5fd 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 a749f4779245..a51821e196cb 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 c6f79621c39d..5557443a3db9 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 296e11f3ad5e..b05b8ee699ef 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 000000000000..4a65a53f9f72 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-RangeType.ll @@ -0,0 +1,20 @@ +; RUN: not opt -passes='print' %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 6c6739d6ed39..742fea14f5af 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