Skip to content

Commit 3a3b214

Browse files
joaosaffranjoaosaffran
andauthored
[DirectX] Making sure we always parse, validate and verify Flags (#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](llvm/llvm-project#161579) --------- Co-authored-by: joaosaffran <[email protected]>
1 parent 8a32e58 commit 3a3b214

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,6 @@ Error MetadataParser::parseRootDescriptors(
337337
else
338338
return makeRSError("Invalid value for RegisterSpace");
339339

340-
if (RSD.Version == 1) {
341-
RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor);
342-
return Error::success();
343-
}
344-
assert(RSD.Version > 1);
345-
346340
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 4))
347341
Descriptor.Flags = *Val;
348342
else
@@ -523,12 +517,6 @@ Error MetadataParser::parseStaticSampler(mcdxbc::RootSignatureDesc &RSD,
523517
return Error(std::move(E));
524518
Sampler.ShaderVisibility = *Visibility;
525519

526-
if (RSD.Version < 3) {
527-
RSD.StaticSamplers.push_back(Sampler);
528-
return Error::success();
529-
}
530-
assert(RSD.Version >= 3);
531-
532520
if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 14))
533521
Sampler.Flags = *Val;
534522
else
@@ -673,17 +661,15 @@ Error MetadataParser::validateRootSignature(
673661
makeRSError(formatv("Invalid value for RegisterSpace: {0}",
674662
Descriptor.RegisterSpace)));
675663

676-
if (RSD.Version > 1) {
677-
bool IsValidFlag =
678-
dxbc::isValidRootDesciptorFlags(Descriptor.Flags) &&
679-
hlsl::rootsig::verifyRootDescriptorFlag(
680-
RSD.Version, dxbc::RootDescriptorFlags(Descriptor.Flags));
681-
if (!IsValidFlag)
682-
DeferredErrs = joinErrors(
683-
std::move(DeferredErrs),
684-
makeRSError(formatv("Invalid value for RootDescriptorFlag: {0}",
685-
Descriptor.Flags)));
686-
}
664+
bool IsValidFlag =
665+
dxbc::isValidRootDesciptorFlags(Descriptor.Flags) &&
666+
hlsl::rootsig::verifyRootDescriptorFlag(
667+
RSD.Version, dxbc::RootDescriptorFlags(Descriptor.Flags));
668+
if (!IsValidFlag)
669+
DeferredErrs = joinErrors(
670+
std::move(DeferredErrs),
671+
makeRSError(formatv("Invalid value for RootDescriptorFlag: {0}",
672+
Descriptor.Flags)));
687673
break;
688674
}
689675
case dxbc::RootParameterType::DescriptorTable: {

llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ bool verifyRootDescriptorFlag(uint32_t Version,
4141
if (Version == 1)
4242
return Flags == FlagT::DataVolatile;
4343

44-
assert((Version <= 3) && "Provided invalid root signature version");
45-
4644
// The data-specific flags are mutually exclusive.
4745
FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
4846
FlagT::DataStaticWhileSetAtExecute;
@@ -118,8 +116,6 @@ bool verifyStaticSamplerFlags(uint32_t Version,
118116
if (Version <= 2)
119117
return Flags == dxbc::StaticSamplerFlags::None;
120118

121-
assert(Version == 3 && "Provided invalid root signature version");
122-
123119
dxbc::StaticSamplerFlags Mask =
124120
dxbc::StaticSamplerFlags::NonNormalizedCoordinates |
125121
dxbc::StaticSamplerFlags::UintBorderColor |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s
2+
; On Version 1, the only valid flag is DataVolatile (2).
3+
target triple = "dxil-unknown-shadermodel6.0-compute"
4+
5+
6+
; CHECK: error: Invalid value for RootDescriptorFlag: 4
7+
; CHECK-NOT: Root Signature Definitions
8+
define void @main() #0 {
9+
entry:
10+
ret void
11+
}
12+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
13+
14+
15+
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
16+
!2 = !{ ptr @main, !3, i32 1 } ; function, root signature
17+
!3 = !{ !5 } ; list of root signature elements
18+
!5 = !{ !"RootCBV", i32 0, i32 1, i32 2, i32 4 }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s
2+
3+
4+
target triple = "dxil-unknown-shadermodel6.0-compute"
5+
6+
; CHECK: error: Invalid value for Static Sampler Flag: 1
7+
; CHECK-NOT: Root Signature Definitions
8+
9+
define void @main() #0 {
10+
entry:
11+
ret void
12+
}
13+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
14+
15+
16+
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
17+
!2 = !{ ptr @main, !3, i32 1 } ; function, root signature
18+
!3 = !{ !5 } ; list of root signature elements
19+
!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 }

0 commit comments

Comments
 (0)