https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/143422
>From 26c7ccab57c53cffd545277555f131fae50520d9 Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Wed, 4 Jun 2025 21:42:20 +0000 Subject: [PATCH 1/6] adding metadata support for static samplers --- llvm/lib/MC/DXContainerRootSignature.cpp | 42 +++++---- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 89 +++++++++++++++++++ llvm/lib/Target/DirectX/DXILRootSignature.h | 1 + .../RootSignature-StaticSamplers.ll | 42 +++++++++ 4 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 6c71823a51f85..d67babf13a432 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; @@ -71,12 +72,16 @@ void RootSignatureDesc::write(raw_ostream &OS) const { BOS.reserveExtraSpace(getSize()); const uint32_t NumParameters = ParametersContainer.size(); - + const uint32_t NumSamplers = StaticSamplers.size(); support::endian::write(BOS, Version, llvm::endianness::little); support::endian::write(BOS, NumParameters, llvm::endianness::little); support::endian::write(BOS, RootParameterOffset, llvm::endianness::little); - support::endian::write(BOS, NumStaticSamplers, llvm::endianness::little); - support::endian::write(BOS, StaticSamplersOffset, llvm::endianness::little); + support::endian::write(BOS, NumSamplers, llvm::endianness::little); + uint32_t SSO = StaticSamplersOffset; + if (NumSamplers > 0) + SSO = writePlaceholder(BOS); + else + support::endian::write(BOS, SSO, llvm::endianness::little); support::endian::write(BOS, Flags, llvm::endianness::little); SmallVector<uint32_t> ParamsOffsets; @@ -142,20 +147,23 @@ void RootSignatureDesc::write(raw_ostream &OS) const { } } } - for (const auto &S : StaticSamplers) { - support::endian::write(BOS, S.Filter, llvm::endianness::little); - support::endian::write(BOS, S.AddressU, llvm::endianness::little); - support::endian::write(BOS, S.AddressV, llvm::endianness::little); - support::endian::write(BOS, S.AddressW, llvm::endianness::little); - support::endian::write(BOS, S.MipLODBias, llvm::endianness::little); - support::endian::write(BOS, S.MaxAnisotropy, llvm::endianness::little); - support::endian::write(BOS, S.ComparisonFunc, llvm::endianness::little); - support::endian::write(BOS, S.BorderColor, llvm::endianness::little); - support::endian::write(BOS, S.MinLOD, llvm::endianness::little); - support::endian::write(BOS, S.MaxLOD, llvm::endianness::little); - support::endian::write(BOS, S.ShaderRegister, llvm::endianness::little); - support::endian::write(BOS, S.RegisterSpace, llvm::endianness::little); - support::endian::write(BOS, S.ShaderVisibility, llvm::endianness::little); + if (NumSamplers > 0) { + rewriteOffsetToCurrentByte(BOS, SSO); + for (const auto &S : StaticSamplers) { + support::endian::write(BOS, S.Filter, llvm::endianness::little); + support::endian::write(BOS, S.AddressU, llvm::endianness::little); + support::endian::write(BOS, S.AddressV, llvm::endianness::little); + support::endian::write(BOS, S.AddressW, llvm::endianness::little); + support::endian::write(BOS, S.MipLODBias, llvm::endianness::little); + support::endian::write(BOS, S.MaxAnisotropy, llvm::endianness::little); + support::endian::write(BOS, S.ComparisonFunc, llvm::endianness::little); + support::endian::write(BOS, S.BorderColor, llvm::endianness::little); + support::endian::write(BOS, S.MinLOD, llvm::endianness::little); + support::endian::write(BOS, S.MaxLOD, llvm::endianness::little); + support::endian::write(BOS, S.ShaderRegister, llvm::endianness::little); + support::endian::write(BOS, S.RegisterSpace, llvm::endianness::little); + support::endian::write(BOS, S.ShaderVisibility, llvm::endianness::little); + } } assert(Storage.size() == getSize()); OS.write(Storage.data(), Storage.size()); diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 38bf9e008f1fe..8c85042bb7307 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "DXILRootSignature.h" #include "DirectX.h" +#include "llvm/ADT/APFloat.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/DXILMetadataAnalysis.h" @@ -55,6 +56,13 @@ static std::optional<uint32_t> extractMdIntValue(MDNode *Node, return std::nullopt; } +static std::optional<APFloat> extractMdFloatValue(MDNode *Node, + unsigned int OpId) { + if (auto *CI = mdconst::dyn_extract<ConstantFP>(Node->getOperand(OpId).get())) + return CI->getValue(); + return std::nullopt; +} + static std::optional<StringRef> extractMdStringValue(MDNode *Node, unsigned int OpId) { MDString *NodeText = dyn_cast<MDString>(Node->getOperand(OpId)); @@ -258,6 +266,81 @@ static bool parseDescriptorTable(LLVMContext *Ctx, return false; } +static bool parseStaticSampler(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, + MDNode *StaticSamplerNode) { + if (StaticSamplerNode->getNumOperands() != 14) + return reportError(Ctx, "Invalid format for Static Sampler"); + + dxbc::RTS0::v1::StaticSampler Sampler; + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 1)) + Sampler.Filter = *Val; + else + return reportError(Ctx, "Invalid value for Filter"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 2)) + Sampler.AddressU = *Val; + else + return reportError(Ctx, "Invalid value for AddressU"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 3)) + Sampler.AddressV = *Val; + else + return reportError(Ctx, "Invalid value for AddressV"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 4)) + Sampler.AddressW = *Val; + else + return reportError(Ctx, "Invalid value for AddressW"); + + if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 5)) + Sampler.MipLODBias = Val->convertToFloat(); + else + return reportError(Ctx, "Invalid value for MipLODBias"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 6)) + Sampler.MaxAnisotropy = *Val; + else + return reportError(Ctx, "Invalid value for MaxAnisotropy"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 7)) + Sampler.ComparisonFunc = *Val; + else + return reportError(Ctx, "Invalid value for ComparisonFunc "); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 8)) + Sampler.BorderColor = *Val; + else + return reportError(Ctx, "Invalid value for ComparisonFunc "); + + if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 9)) + Sampler.MinLOD = Val->convertToFloat(); + else + return reportError(Ctx, "Invalid value for MinLOD"); + + if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 10)) + Sampler.MaxLOD = Val->convertToFloat(); + else + return reportError(Ctx, "Invalid value for MaxLOD"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 11)) + Sampler.ShaderRegister = *Val; + else + return reportError(Ctx, "Invalid value for ShaderRegister"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 12)) + Sampler.RegisterSpace = *Val; + else + return reportError(Ctx, "Invalid value for RegisterSpace"); + + if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 13)) + Sampler.ShaderVisibility = *Val; + else + return reportError(Ctx, "Invalid value for ShaderVisibility"); + + RSD.StaticSamplers.push_back(Sampler); + return false; +} + static bool parseRootSignatureElement(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *Element) { @@ -273,6 +356,7 @@ static bool parseRootSignatureElement(LLVMContext *Ctx, .Case("RootSRV", RootSignatureElementKind::SRV) .Case("RootUAV", RootSignatureElementKind::UAV) .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable) + .Case("StaticSampler", RootSignatureElementKind::StaticSamplers) .Default(RootSignatureElementKind::Error); switch (ElementKind) { @@ -287,6 +371,8 @@ static bool parseRootSignatureElement(LLVMContext *Ctx, return parseRootDescriptors(Ctx, RSD, Element, ElementKind); case RootSignatureElementKind::DescriptorTable: return parseDescriptorTable(Ctx, RSD, Element); + case RootSignatureElementKind::StaticSamplers: + return parseStaticSampler(Ctx, RSD, Element); case RootSignatureElementKind::Error: return reportError(Ctx, "Invalid Root Signature Element: " + *ElementText); } @@ -531,6 +617,9 @@ analyzeModule(Module &M) { // offset will always equal to the header size. RSD.RootParameterOffset = sizeof(dxbc::RTS0::v1::RootSignatureHeader); + // static sampler offset is calculated when writting dxcontainer. + RSD.StaticSamplersOffset = 0u; + if (parse(Ctx, RSD, RootElementListNode) || validate(Ctx, RSD)) { return RSDMap; } diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h b/llvm/lib/Target/DirectX/DXILRootSignature.h index b45cebc15fd39..be5cc78bc6bdf 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.h +++ b/llvm/lib/Target/DirectX/DXILRootSignature.h @@ -32,6 +32,7 @@ enum class RootSignatureElementKind { UAV = 4, CBV = 5, DescriptorTable = 6, + StaticSamplers = 7 }; class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> { friend AnalysisInfoMixin<RootSignatureAnalysis>; diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll new file mode 100644 index 0000000000000..35a8fb53248f9 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll @@ -0,0 +1,42 @@ +; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s +; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC + +target triple = "dxil-unknown-shadermodel6.0-compute" + +; CHECK: @dx.rts0 = private constant [76 x i8] c"{{.*}}", section "RTS0", align 4 + +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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x40403999A0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } + +; DXC: - Name: RTS0 +; DXC-NEXT: Size: 76 +; DXC-NEXT: RootSignature: +; DXC-NEXT: Version: 2 +; DXC-NEXT: NumRootParameters: 0 +; DXC-NEXT: RootParametersOffset: 24 +; DXC-NEXT: NumStaticSamplers: 1 +; DXC-NEXT: StaticSamplersOffset: 24 +; DXC-NEXT: Parameters: [] +; DXC-NEXT: Samplers: +; DXC-NEXT: - Filter: 4 +; DXC-NEXT: AddressU: 2 +; DXC-NEXT: AddressV: 3 +; DXC-NEXT: AddressW: 5 +; DXC-NEXT: MipLODBias: 32.45 +; DXC-NEXT: MaxAnisotropy: 9 +; DXC-NEXT: ComparisonFunc: 3 +; DXC-NEXT: BorderColor: 2 +; DXC-NEXT: MinLOD: -128 +; DXC-NEXT: MaxLOD: 128 +; DXC-NEXT: ShaderRegister: 42 +; DXC-NEXT: RegisterSpace: 0 +; DXC-NEXT: ShaderVisibility: 0 >From 7ecbc9c4012a6de74285d606cd7c0e51ae9fe453 Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Thu, 5 Jun 2025 00:59:42 +0000 Subject: [PATCH 2/6] add validations --- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 168 ++++++++++++++++++ .../RootSignature-StaticSamplers.ll | 4 +- 2 files changed, 170 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 8c85042bb7307..cbee2b1fb347d 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -13,6 +13,7 @@ #include "DXILRootSignature.h" #include "DirectX.h" #include "llvm/ADT/APFloat.h" +#include "llvm/ADT/STLForwardCompat.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/DXILMetadataAnalysis.h" @@ -28,6 +29,7 @@ #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" +#include <cmath> #include <cstdint> #include <optional> #include <utility> @@ -487,6 +489,127 @@ static bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, return false; } +static bool verifySamplerFilter(uint32_t Filter) { + switch (Filter) { + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_MIP_POINT): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MIN_POINT_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_POINT_MAG_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_LINEAR_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MIN_LINEAR_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::ANISOTROPIC): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_POINT_MAG_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_LINEAR_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::COMPARISON_ANISOTROPIC): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_POINT_MAG_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_LINEAR_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MINIMUM_ANISOTROPIC): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_POINT_MAG_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_LINEAR_MAG_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_LINEAR_MIP_POINT): + case llvm::to_underlying( + dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_MIP_LINEAR): + case llvm::to_underlying(dxbc::StaticSamplerFilter::MAXIMUM_ANISOTROPIC): + return true; + } + return false; +} + +// Values allowed here: +// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax +static bool verifyAddress(uint32_t Address) { + switch (Address) { + case llvm::to_underlying(dxbc::TextureAddressMode::Border): + case llvm::to_underlying(dxbc::TextureAddressMode::Clamp): + case llvm::to_underlying(dxbc::TextureAddressMode::Mirror): + case llvm::to_underlying(dxbc::TextureAddressMode::MirrorOnce): + case llvm::to_underlying(dxbc::TextureAddressMode::Wrap): + return true; + } + + return false; +} + +static bool verifyMipLODBias(float MipLODBias) { + return MipLODBias >= -16.f && MipLODBias <= 16.f; +} + +static bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) { + return MaxAnisotropy <= 16u; +} + +static bool verifyComparisonFunc(uint32_t ComparisonFunc) { + switch (ComparisonFunc) { + case llvm::to_underlying(dxbc::SamplersComparisonFunction::Never): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::Less): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::Equal): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::LessEqual): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::Greater): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::NotEqual): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::GreaterEqual): + case llvm::to_underlying(dxbc::SamplersComparisonFunction::Always): + return true; + } + return false; +} + +static bool verifyBorderColor(uint32_t BorderColor) { + switch (BorderColor) { + case llvm::to_underlying(dxbc::SamplersBorderColor::TransparentBlack): + case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlack): + case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhite): + case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlackUint): + case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhiteUint): + return true; + } + return false; +} + +static bool verifyLOD(float LOD) { return !std::isnan(LOD); } + static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) { if (!verifyVersion(RSD.Version)) { @@ -544,6 +667,51 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) { } } + for (const dxbc::RTS0::v1::StaticSampler &Sampler : RSD.StaticSamplers) { + if (!verifySamplerFilter(Sampler.Filter)) + return reportValueError(Ctx, "Filter", Sampler.Filter); + + if (!verifyAddress(Sampler.AddressU)) + return reportValueError(Ctx, "AddressU", Sampler.AddressU); + + if (!verifyAddress(Sampler.AddressV)) + return reportValueError(Ctx, "AddressU", Sampler.AddressV); + + if (!verifyAddress(Sampler.AddressW)) + return reportValueError(Ctx, "AddressU", Sampler.AddressW); + + if (!verifyMipLODBias(Sampler.MipLODBias)) + return reportValueError(Ctx, "MipLODBias", Sampler.MipLODBias); + + if (!verifyMaxAnisotropy(Sampler.MaxAnisotropy)) + return reportValueError(Ctx, "MaxAnisotropy", Sampler.MaxAnisotropy); + + if (!verifyComparisonFunc(Sampler.ComparisonFunc)) + return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc); + + if (!verifyComparisonFunc(Sampler.ComparisonFunc)) + return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc); + + if (!verifyBorderColor(Sampler.BorderColor)) + return reportValueError(Ctx, "BorderColor ", Sampler.BorderColor); + + if (!verifyLOD(Sampler.MinLOD)) + return reportValueError(Ctx, "MinLOD ", Sampler.MinLOD); + + if (!verifyLOD(Sampler.MaxLOD)) + return reportValueError(Ctx, "MaxLOD ", Sampler.MaxLOD); + + if (!verifyRegisterValue(Sampler.ShaderRegister)) + return reportValueError(Ctx, "ShaderRegister", Sampler.ShaderRegister); + + if (!verifyRegisterSpace(Sampler.RegisterSpace)) + return reportValueError(Ctx, "RegisterSpace", Sampler.RegisterSpace); + + if (!dxbc::isValidShaderVisibility(Sampler.ShaderVisibility)) + return reportValueError(Ctx, "ShaderVisibility", + Sampler.ShaderVisibility); + } + return false; } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll index 35a8fb53248f9..77965064a4228 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll @@ -15,7 +15,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !dx.rootsignatures = !{!2} ; list of function/root signature pairs !2 = !{ ptr @main, !3 } ; function, root signature !3 = !{ !5 } ; list of root signature elements -!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x40403999A0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } ; DXC: - Name: RTS0 ; DXC-NEXT: Size: 76 @@ -31,7 +31,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ; DXC-NEXT: AddressU: 2 ; DXC-NEXT: AddressV: 3 ; DXC-NEXT: AddressW: 5 -; DXC-NEXT: MipLODBias: 32.45 +; DXC-NEXT: MipLODBias: 1.425 ; DXC-NEXT: MaxAnisotropy: 9 ; DXC-NEXT: ComparisonFunc: 3 ; DXC-NEXT: BorderColor: 2 >From 0f9cc14d357a701010631adf3efee2ead9ee79fe Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Fri, 6 Jun 2025 06:54:07 +0000 Subject: [PATCH 3/6] adding tests --- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 15 ++++++--------- ...tSignature-DescriptorTable-Invalid-Flag.ll | 2 +- .../RootSignature-DescriptorTable.ll | 6 +++--- .../ContainerData/RootSignature-Parameters.ll | 6 +++--- ...gnature-StaticSamplers-Invalid-AddressU.ll | 19 +++++++++++++++++++ ...gnature-StaticSamplers-Invalid-AddressV.ll | 19 +++++++++++++++++++ ...gnature-StaticSamplers-Invalid-AddressW.ll | 19 +++++++++++++++++++ ...ture-StaticSamplers-Invalid-BorderColor.ll | 19 +++++++++++++++++++ ...e-StaticSamplers-Invalid-ComparisonFunc.ll | 19 +++++++++++++++++++ ...Signature-StaticSamplers-Invalid-Filter.ll | 19 +++++++++++++++++++ ...re-StaticSamplers-Invalid-MaxAnisotropy.ll | 19 +++++++++++++++++++ ...Signature-StaticSamplers-Invalid-MaxLod.ll | 19 +++++++++++++++++++ ...Signature-StaticSamplers-Invalid-MinLod.ll | 19 +++++++++++++++++++ ...ature-StaticSamplers-Invalid-MinLopBias.ll | 19 +++++++++++++++++++ ...re-StaticSamplers-Invalid-RegisterSpace.ll | 19 +++++++++++++++++++ ...e-StaticSamplers-Invalid-ShaderRegister.ll | 19 +++++++++++++++++++ ...StaticSamplers-Invalid-ShaderVisibility.ll | 19 +++++++++++++++++++ 17 files changed, 260 insertions(+), 16 deletions(-) create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressU.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressV.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressW.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-BorderColor.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ComparisonFunc.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Filter.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxAnisotropy.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxLod.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLod.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLopBias.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-RegisterSpace.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderRegister.ll create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderVisibility.ll diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index cbee2b1fb347d..7edcea437d38a 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -411,7 +411,7 @@ static bool verifyRegisterValue(uint32_t RegisterValue) { // This Range is reserverved, therefore invalid, according to the spec // https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#all-the-values-should-be-legal static bool verifyRegisterSpace(uint32_t RegisterSpace) { - return !(RegisterSpace >= 0xFFFFFFF0 && RegisterSpace < 0xFFFFFFFF); + return !(RegisterSpace >= 0xFFFFFFF0 && RegisterSpace <= 0xFFFFFFFF); } static bool verifyDescriptorFlag(uint32_t Flags) { return (Flags & ~0xE) == 0; } @@ -675,10 +675,10 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) { return reportValueError(Ctx, "AddressU", Sampler.AddressU); if (!verifyAddress(Sampler.AddressV)) - return reportValueError(Ctx, "AddressU", Sampler.AddressV); + return reportValueError(Ctx, "AddressV", Sampler.AddressV); if (!verifyAddress(Sampler.AddressW)) - return reportValueError(Ctx, "AddressU", Sampler.AddressW); + return reportValueError(Ctx, "AddressW", Sampler.AddressW); if (!verifyMipLODBias(Sampler.MipLODBias)) return reportValueError(Ctx, "MipLODBias", Sampler.MipLODBias); @@ -689,17 +689,14 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) { if (!verifyComparisonFunc(Sampler.ComparisonFunc)) return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc); - if (!verifyComparisonFunc(Sampler.ComparisonFunc)) - return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc); - if (!verifyBorderColor(Sampler.BorderColor)) - return reportValueError(Ctx, "BorderColor ", Sampler.BorderColor); + return reportValueError(Ctx, "BorderColor", Sampler.BorderColor); if (!verifyLOD(Sampler.MinLOD)) - return reportValueError(Ctx, "MinLOD ", Sampler.MinLOD); + return reportValueError(Ctx, "MinLOD", Sampler.MinLOD); if (!verifyLOD(Sampler.MaxLOD)) - return reportValueError(Ctx, "MaxLOD ", Sampler.MaxLOD); + return reportValueError(Ctx, "MaxLOD", Sampler.MaxLOD); if (!verifyRegisterValue(Sampler.ShaderRegister)) return reportValueError(Ctx, "ShaderRegister", Sampler.ShaderRegister); diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-Flag.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-Flag.ll index 37d87986aa25f..76e1d7f4573b7 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-Flag.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-Invalid-Flag.ll @@ -16,5 +16,5 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !2 = !{ ptr @main, !3 } ; function, root signature !3 = !{ !5 } ; list of root signature elements !5 = !{ !"DescriptorTable", i32 0, !6, !7 } -!6 = !{ !"SRV", i32 0, i32 0, i32 -1, i32 -1, i32 22 } +!6 = !{ !"SRV", i32 0, i32 1, i32 0, i32 -1, i32 22 } !7 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable.ll index af4acfd2736d9..04eff9f872e4c 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable.ll @@ -16,7 +16,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !2 = !{ ptr @main, !3 } ; function, root signature !3 = !{ !5 } ; list of root signature elements !5 = !{ !"DescriptorTable", i32 0, !6, !7 } -!6 = !{ !"SRV", i32 0, i32 0, i32 -1, i32 -1, i32 4 } +!6 = !{ !"SRV", i32 0, i32 1, i32 0, i32 -1, i32 4 } !7 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } ; DXC: - Name: RTS0 @@ -36,8 +36,8 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ; DXC-NEXT: Ranges: ; DXC-NEXT: - RangeType: 0 ; DXC-NEXT: NumDescriptors: 0 -; DXC-NEXT: BaseShaderRegister: 0 -; DXC-NEXT: RegisterSpace: 4294967295 +; DXC-NEXT: BaseShaderRegister: 1 +; DXC-NEXT: RegisterSpace: 0 ; DXC-NEXT: OffsetInDescriptorsFromTableStart: 4294967295 ; DXC-NEXT: DATA_VOLATILE: true ; DXC-NEXT: - RangeType: 1 diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll index 28768e252d85a..df2bb18c87b2e 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Parameters.ll @@ -17,7 +17,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !5 = !{ !"RootConstants", i32 0, i32 1, i32 2, i32 3 } !6 = !{ !"RootSRV", i32 1, i32 4, i32 5, i32 6 } !7 = !{ !"DescriptorTable", i32 0, !8, !9 } -!8 = !{ !"SRV", i32 0, i32 0, i32 -1, i32 -1, i32 4 } +!8 = !{ !"SRV", i32 0, i32 1, i32 0, i32 -1, i32 4 } !9 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 2 } ;CHECK-LABEL: Definition for 'main': @@ -39,8 +39,8 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } ;CHECK-NEXT: Shader Visibility: 0 ;CHECK-NEXT: NumRanges: 2 ;CHECK-NEXT: - Range Type: 0 -;CHECK-NEXT: Register Space: 4294967295 -;CHECK-NEXT: Base Shader Register: 0 +;CHECK-NEXT: Register Space: 0 +;CHECK-NEXT: Base Shader Register: 1 ;CHECK-NEXT: Num Descriptors: 0 ;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295 ;CHECK-NEXT: Flags: 4 diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressU.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressU.ll new file mode 100644 index 0000000000000..124803e36a450 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressU.ll @@ -0,0 +1,19 @@ +; 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 value for AddressU: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 666, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressV.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressV.ll new file mode 100644 index 0000000000000..e2c5de1c36a0c --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressV.ll @@ -0,0 +1,19 @@ +; 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 value for AddressV: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 666, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressW.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressW.ll new file mode 100644 index 0000000000000..08fc8fa9e5093 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-AddressW.ll @@ -0,0 +1,19 @@ +; 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 value for AddressW: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 666, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-BorderColor.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-BorderColor.ll new file mode 100644 index 0000000000000..83b31594b2bec --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-BorderColor.ll @@ -0,0 +1,19 @@ +; 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 value for BorderColor: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 666, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ComparisonFunc.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ComparisonFunc.ll new file mode 100644 index 0000000000000..b9cd1d9093325 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ComparisonFunc.ll @@ -0,0 +1,19 @@ +; 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 value for ComparisonFunc: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 666, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Filter.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Filter.ll new file mode 100644 index 0000000000000..303dc5defaf52 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Filter.ll @@ -0,0 +1,19 @@ +; 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 value for Filter: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 666, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxAnisotropy.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxAnisotropy.ll new file mode 100644 index 0000000000000..084d6b326a67f --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxAnisotropy.ll @@ -0,0 +1,19 @@ +; 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 value for MaxAnisotropy: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 666, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxLod.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxLod.ll new file mode 100644 index 0000000000000..b4d33197ca150 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MaxLod.ll @@ -0,0 +1,19 @@ +; 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 value for MaxLOD: 0 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 0x7FF8000000000000, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLod.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLod.ll new file mode 100644 index 0000000000000..97fa803f4c165 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLod.ll @@ -0,0 +1,19 @@ +; 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 value for MinLOD: 0 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float 0x7FF8000000000000, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLopBias.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLopBias.ll new file mode 100644 index 0000000000000..cd4079d8edd74 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-MinLopBias.ll @@ -0,0 +1,19 @@ +; 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 value for MipLODBias: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 6.660000e+02, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-RegisterSpace.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-RegisterSpace.ll new file mode 100644 index 0000000000000..eeea8821afd07 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-RegisterSpace.ll @@ -0,0 +1,19 @@ +; 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 value for RegisterSpace: 4294967280 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 4294967280, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderRegister.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderRegister.ll new file mode 100644 index 0000000000000..6750cdc1b77fa --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderRegister.ll @@ -0,0 +1,19 @@ +; 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 value for ShaderRegister: 4294967295 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 4294967295, i32 0, i32 0 } diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderVisibility.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderVisibility.ll new file mode 100644 index 0000000000000..4db55cf9c4b89 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-ShaderVisibility.ll @@ -0,0 +1,19 @@ +; 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 value for ShaderVisibility: 666 +; 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 } ; function, root signature +!3 = !{ !5 } ; list of root signature elements +!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 666 } >From 6a1b0b120ee9bf51c3cf1daf7347a3ab22162e3c Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Fri, 6 Jun 2025 18:35:08 +0000 Subject: [PATCH 4/6] addressing comments --- .../BinaryFormat/DXContainerConstants.def | 10 +- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 98 +++++++++---------- 2 files changed, 52 insertions(+), 56 deletions(-) diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def index fa66450c563c4..a65453f78d85a 100644 --- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def +++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def @@ -100,11 +100,11 @@ DESCRIPTOR_RANGE_FLAG(16, DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) // DESCRIPTOR_RANGE(value, name). #ifdef DESCRIPTOR_RANGE - DESCRIPTOR_RANGE(4, ERROR) - DESCRIPTOR_RANGE(0, SRV) - DESCRIPTOR_RANGE(1, UAV) - DESCRIPTOR_RANGE(2, CBV) - DESCRIPTOR_RANGE(3, Sampler) +DESCRIPTOR_RANGE(4, ERROR) +DESCRIPTOR_RANGE(0, SRV) +DESCRIPTOR_RANGE(1, UAV) +DESCRIPTOR_RANGE(2, CBV) +DESCRIPTOR_RANGE(3, Sampler) DESCRIPTOR_RANGE(0, NONE) #undef DESCRIPTOR_RANGE #endif // DESCRIPTOR_RANGE diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 7edcea437d38a..56343eb48bea2 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -428,63 +428,59 @@ static bool verifyRangeType(uint32_t Type) { return false; } +template <typename... FlagTypes> +static bool isFlagSet(uint32_t Flags, FlagTypes... FlagsToCheck) { + return ((Flags & llvm::to_underlying(FlagsToCheck)) | ...) == Flags; +} + static bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, uint32_t Flags) { - if (Version == 1 && - Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)) - return Flags == 0; - - if (Version == 2 && - Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)) { - switch (Flags) { - case 0: - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_VOLATILE): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag:: - DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS): - return true; + using namespace dxbc; + + bool IsSampler = (Type == llvm::to_underlying(DescriptorRangeType::Sampler)); + + if (Version == 1) { + if (IsSampler) { + return Flags == 0; } - return false; + return Flags == + llvm::to_underlying(DescriptorRangeFlag::DESCRIPTORS_VOLATILE); } - if (Version == 1 && - Type != llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)) - return Flags == - llvm::to_underlying(dxbc::DescriptorRangeFlag::DESCRIPTORS_VOLATILE); - - if (Version == 2 && - Type != llvm::to_underlying(dxbc::DescriptorRangeType::Sampler)) { - switch (Flags) { - case 0: - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DESCRIPTORS_VOLATILE): - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_VOLATILE): - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_STATIC): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag:: - DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS): - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DESCRIPTORS_VOLATILE) | - llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_VOLATILE): - case llvm::to_underlying(dxbc::DescriptorRangeFlag::DESCRIPTORS_VOLATILE) | - llvm::to_underlying( - dxbc::DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag:: - DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) | - llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_VOLATILE): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag:: - DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) | - llvm::to_underlying(dxbc::DescriptorRangeFlag::DATA_STATIC): - case llvm::to_underlying( - dxbc::DescriptorRangeFlag:: - DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) | - llvm::to_underlying( - dxbc::DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE): - return true; + if (Version == 2) { + if (IsSampler) { + return Flags == 0 || + isFlagSet(Flags, DescriptorRangeFlag::DATA_VOLATILE) || + isFlagSet(Flags, + DescriptorRangeFlag:: + DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS); } - return false; + // Valid flag combinations for non-sampler Version 2 + return Flags == 0 || + isFlagSet(Flags, DescriptorRangeFlag::DESCRIPTORS_VOLATILE) || + isFlagSet(Flags, DescriptorRangeFlag::DATA_VOLATILE) || + isFlagSet(Flags, DescriptorRangeFlag::DATA_STATIC) || + isFlagSet(Flags, + DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE) || + isFlagSet(Flags, + DescriptorRangeFlag:: + DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) || + isFlagSet(Flags, DescriptorRangeFlag::DESCRIPTORS_VOLATILE, + DescriptorRangeFlag::DATA_VOLATILE) || + isFlagSet(Flags, DescriptorRangeFlag::DESCRIPTORS_VOLATILE, + DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE) || + isFlagSet(Flags, + DescriptorRangeFlag:: + DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS, + DescriptorRangeFlag::DATA_VOLATILE) || + isFlagSet(Flags, + DescriptorRangeFlag:: + DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS, + DescriptorRangeFlag::DATA_STATIC) || + isFlagSet(Flags, + DescriptorRangeFlag:: + DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS, + DescriptorRangeFlag::DATA_STATIC_WHILE_SET_AT_EXECUTE); } return false; } >From fb6ac268f7855dd986b77a73369ab02fbbf0db12 Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Mon, 9 Jun 2025 18:44:50 +0000 Subject: [PATCH 5/6] fix --- llvm/lib/MC/DXContainerRootSignature.cpp | 1 - llvm/lib/Target/DirectX/DXILRootSignature.cpp | 3 --- 2 files changed, 4 deletions(-) diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index d67babf13a432..77d648a739b87 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; diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 56343eb48bea2..be84c28bb4c7b 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -12,8 +12,6 @@ //===----------------------------------------------------------------------===// #include "DXILRootSignature.h" #include "DirectX.h" -#include "llvm/ADT/APFloat.h" -#include "llvm/ADT/STLForwardCompat.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/DXILMetadataAnalysis.h" @@ -29,7 +27,6 @@ #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" -#include <cmath> #include <cstdint> #include <optional> #include <utility> >From 0b4111a212fddf37293c65e6431026579c97e5e1 Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Mon, 9 Jun 2025 23:41:26 +0000 Subject: [PATCH 6/6] remove diff --- diff | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 diff diff --git a/diff b/diff deleted file mode 100644 index 0facb5edaa5fb..0000000000000 --- a/diff +++ /dev/null @@ -1,34 +0,0 @@ -diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h -index 08949e39716d..725c405b77e5 100644 ---- a/llvm/include/llvm/BinaryFormat/DXContainer.h -+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h -@@ -164,7 +164,7 @@ enum class RootDescriptorFlag : uint32_t { - #include "DXContainerConstants.def" - }; - --#define DESCRIPTOR_RANGE_FLAG(Num, Val) Val = 1ull << Num, -+#define DESCRIPTOR_RANGE_FLAG(Num, Val) Val = Num, - enum class DescriptorRangeFlag : uint32_t { - #include "DXContainerConstants.def" - }; -diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def -index 501ef0c31cdd..c4895ee8ed65 100644 ---- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def -+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def -@@ -90,11 +90,11 @@ ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC) - #ifdef DESCRIPTOR_RANGE_FLAG - - DESCRIPTOR_RANGE_FLAG(0, NONE) --DESCRIPTOR_RANGE_FLAG(1, DESCRIPTORS_VOLATILE) --DESCRIPTOR_RANGE_FLAG(2, DATA_VOLATILE) --DESCRIPTOR_RANGE_FLAG(3, DATA_STATIC_WHILE_SET_AT_EXECUTE) --DESCRIPTOR_RANGE_FLAG(4, DATA_STATIC) --DESCRIPTOR_RANGE_FLAG(16, DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) -+DESCRIPTOR_RANGE_FLAG(0x1, DESCRIPTORS_VOLATILE) -+DESCRIPTOR_RANGE_FLAG(0x2, DATA_VOLATILE) -+DESCRIPTOR_RANGE_FLAG(0x4, DATA_STATIC_WHILE_SET_AT_EXECUTE) -+DESCRIPTOR_RANGE_FLAG(0x8, DATA_STATIC) -+DESCRIPTOR_RANGE_FLAG(0x10000, DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS) - #undef DESCRIPTOR_RANGE_FLAG - #endif // DESCRIPTOR_RANGE_FLAG - _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits