[llvm-branch-commits] [llvm] [DirectX] Making sure we always parse, validate and verify Flags (PR #162171)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Oct 6 15:05:58 PDT 2025
https://github.com/joaosaffran created https://github.com/llvm/llvm-project/pull/162171
This PR makes a few changes to make sure that Root Signature Flags are always parsed validated and verified, this includes if you use a version that doesn't support flags. The logic already existed, this PR just makes sure it is always executed.
Closes: [#161579](https://github.com/llvm/llvm-project/issues/161579)
>From 02c956d5589e887922da23d058678c2d688a6b2a Mon Sep 17 00:00:00 2001
From: Joao Saffran <joaosaffranllvm at gmail.com>
Date: Mon, 6 Oct 2025 10:05:46 -0700
Subject: [PATCH 1/2] start changes
---
.../Frontend/HLSL/RootSignatureMetadata.cpp | 22 ++++++-------------
.../HLSL/RootSignatureValidations.cpp | 10 ++++++---
...gnature-RootDescriptor-Invalid-Flags_V1.ll | 18 +++++++++++++++
3 files changed, 32 insertions(+), 18 deletions(-)
create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootDescriptor-Invalid-Flags_V1.ll
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
index 7ed4ec0dc3457..5f93a48e796ba 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
@@ -28,7 +28,7 @@ template <typename T>
void formatImpl(raw_string_ostream &Buff,
std::integral_constant<RSErrorKind, RSErrorKind::Validation>,
StringRef ParamName, T Value) {
- Buff << "Invalid value for: " << ParamName << ":";
+ Buff << "Invalid value for " << ParamName << ": ";
if constexpr (std::is_same_v<std::decay_t<T>, std::nullptr_t>) {
Buff << "nullptr";
} else {
@@ -385,12 +385,6 @@ Error MetadataParser::parseRootDescriptors(
return createRSError(RSErrorKind::InvalidMetadataValue,
StringRef("RegisterSpace"));
- if (RSD.Version == 1) {
- RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor);
- return Error::success();
- }
- assert(RSD.Version > 1);
-
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 4))
Descriptor.Flags = *Val;
else
@@ -738,14 +732,12 @@ Error MetadataParser::validateRootSignature(
StringRef("RegisterSpace"),
Descriptor.RegisterSpace));
- if (RSD.Version > 1) {
- if (!hlsl::rootsig::verifyRootDescriptorFlag(RSD.Version,
- Descriptor.Flags))
- DeferredErrs = joinErrors(
- std::move(DeferredErrs),
- createRSError(RSErrorKind::Validation,
- StringRef("RootDescriptorFlag"), Descriptor.Flags));
- }
+ if (!hlsl::rootsig::verifyRootDescriptorFlag(RSD.Version,
+ Descriptor.Flags))
+ DeferredErrs = joinErrors(
+ std::move(DeferredErrs),
+ createRSError(RSErrorKind::Validation,
+ StringRef("RootDescriptorFlag"), Descriptor.Flags));
break;
}
case dxbc::RootParameterType::DescriptorTable: {
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
index 8a2b03d9ede8b..bed35c0015604 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
@@ -35,13 +35,17 @@ bool verifyRegisterSpace(uint32_t RegisterSpace) {
}
bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal) {
+ assert((Version <= 3) && "Provided invalid root signature version");
+ uint32_t LargestValue = llvm::to_underlying(
+ dxbc::RootDescriptorFlags::LLVM_BITMASK_LARGEST_ENUMERATOR);
+ if (FlagsVal >= NextPowerOf2(LargestValue))
+ return false;
+
using FlagT = dxbc::RootDescriptorFlags;
FlagT Flags = FlagT(FlagsVal);
if (Version == 1)
return Flags == FlagT::DataVolatile;
- assert((Version <= 3) && "Provided invalid root signature version");
-
// The data-specific flags are mutually exclusive.
FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
FlagT::DataStaticWhileSetAtExecute;
@@ -114,6 +118,7 @@ bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type,
}
bool verifyStaticSamplerFlags(uint32_t Version, uint32_t FlagsNumber) {
+ assert(Version == 3 && "Provided invalid root signature version");
uint32_t LargestValue = llvm::to_underlying(
dxbc::StaticSamplerFlags::LLVM_BITMASK_LARGEST_ENUMERATOR);
if (FlagsNumber >= NextPowerOf2(LargestValue))
@@ -123,7 +128,6 @@ bool verifyStaticSamplerFlags(uint32_t Version, uint32_t FlagsNumber) {
if (Version <= 2)
return Flags == dxbc::StaticSamplerFlags::None;
- assert(Version == 3 && "Provided invalid root signature version");
dxbc::StaticSamplerFlags Mask =
dxbc::StaticSamplerFlags::NonNormalizedCoordinates |
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootDescriptor-Invalid-Flags_V1.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootDescriptor-Invalid-Flags_V1.ll
new file mode 100644
index 0000000000000..610ce4f4617d0
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootDescriptor-Invalid-Flags_V1.ll
@@ -0,0 +1,18 @@
+; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s
+; On Version 1, the only valid flag is DataVolatile (2).
+target triple = "dxil-unknown-shadermodel6.0-compute"
+
+
+; CHECK: error: Invalid value for RootDescriptorFlag: 4
+; 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 1 } ; function, root signature
+!3 = !{ !5 } ; list of root signature elements
+!5 = !{ !"RootCBV", i32 0, i32 1, i32 2, i32 4 }
>From b5fdd5e85c56bd149c03f1fa60b18f83d8e674bc Mon Sep 17 00:00:00 2001
From: Joao Saffran <joaosaffranllvm at gmail.com>
Date: Mon, 6 Oct 2025 11:27:53 -0700
Subject: [PATCH 2/2] removing the enforcing while parsing
---
.../Frontend/HLSL/RootSignatureMetadata.cpp | 6 ------
.../HLSL/RootSignatureValidations.cpp | 2 +-
...ignature-StaticSamplers-Invalid-Flag_V1.ll | 19 +++++++++++++++++++
3 files changed, 20 insertions(+), 7 deletions(-)
create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Flag_V1.ll
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
index a14a3ba8d1191..f0f3bce88ba3a 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
@@ -583,12 +583,6 @@ Error MetadataParser::parseStaticSampler(mcdxbc::RootSignatureDesc &RSD,
return Error(std::move(E));
Sampler.ShaderVisibility = *Visibility;
- if (RSD.Version < 3) {
- RSD.StaticSamplers.push_back(Sampler);
- return Error::success();
- }
- assert(RSD.Version >= 3);
-
if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 14))
Sampler.Flags = *Val;
else
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
index bed35c0015604..65b431aa66944 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
@@ -118,7 +118,7 @@ bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type,
}
bool verifyStaticSamplerFlags(uint32_t Version, uint32_t FlagsNumber) {
- assert(Version == 3 && "Provided invalid root signature version");
+ assert(Version <= 3 && "Provided invalid root signature version");
uint32_t LargestValue = llvm::to_underlying(
dxbc::StaticSamplerFlags::LLVM_BITMASK_LARGEST_ENUMERATOR);
if (FlagsNumber >= NextPowerOf2(LargestValue))
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Flag_V1.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Flag_V1.ll
new file mode 100644
index 0000000000000..76b60b82db852
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers-Invalid-Flag_V1.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 Static Sampler Flag: 1
+; 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 1 } ; 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 0, i32 1 }
More information about the llvm-branch-commits
mailing list