LLVM 22.0.0git
RootSignatureValidations.cpp
Go to the documentation of this file.
1//===- HLSLRootSignatureValidations.cpp - HLSL Root Signature helpers -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file contains helpers for working with HLSL Root Signatures.
10///
11//===----------------------------------------------------------------------===//
12
14
15#include <cmath>
16
17namespace llvm {
18namespace hlsl {
19namespace rootsig {
20
21bool verifyRootFlag(uint32_t Flags) { return (Flags & ~0xfff) == 0; }
22
23bool verifyVersion(uint32_t Version) { return (Version == 1 || Version == 2); }
24
25bool verifyRegisterValue(uint32_t RegisterValue) {
26 return RegisterValue != ~0U;
27}
28
29// This Range is reserverved, therefore invalid, according to the spec
30// https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#all-the-values-should-be-legal
31bool verifyRegisterSpace(uint32_t RegisterSpace) {
32 return !(RegisterSpace >= 0xFFFFFFF0);
33}
34
36 using FlagT = dxbc::RootDescriptorFlags;
37 FlagT Flags = FlagT(FlagsVal);
38 if (Version == 1)
39 return Flags == FlagT::DataVolatile;
40
41 assert(Version == 2 && "Provided invalid root signature version");
42
43 // The data-specific flags are mutually exclusive.
44 FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
45 FlagT::DataStaticWhileSetAtExecute;
46
47 if (popcount(llvm::to_underlying(Flags & DataFlags)) > 1)
48 return false;
49
50 // Only a data flag or no flags is valid
51 return (Flags | DataFlags) == DataFlags;
52}
53
56 using FlagT = dxbc::DescriptorRangeFlags;
57
58 const bool IsSampler = (Type == dxil::ResourceClass::Sampler);
59
60 if (Version == 1) {
61 // Since the metadata is unversioned, we expect to explicitly see the values
62 // that map to the version 1 behaviour here.
63 if (IsSampler)
64 return Flags == FlagT::DescriptorsVolatile;
65 return Flags == (FlagT::DataVolatile | FlagT::DescriptorsVolatile);
66 }
67
68 // The data-specific flags are mutually exclusive.
69 FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
70 FlagT::DataStaticWhileSetAtExecute;
71
72 if (popcount(llvm::to_underlying(Flags & DataFlags)) > 1)
73 return false;
74
75 // The descriptor-specific flags are mutually exclusive.
76 FlagT DescriptorFlags = FlagT::DescriptorsStaticKeepingBufferBoundsChecks |
77 FlagT::DescriptorsVolatile;
78 if (popcount(llvm::to_underlying(Flags & DescriptorFlags)) > 1)
79 return false;
80
81 // For volatile descriptors, DATA_is never valid.
82 if ((Flags & FlagT::DescriptorsVolatile) == FlagT::DescriptorsVolatile) {
83 FlagT Mask = FlagT::DescriptorsVolatile;
84 if (!IsSampler) {
85 Mask |= FlagT::DataVolatile;
86 Mask |= FlagT::DataStaticWhileSetAtExecute;
87 }
88 return (Flags & ~Mask) == FlagT::None;
89 }
90
91 // For "KEEPING_BUFFER_BOUNDS_CHECKS" descriptors,
92 // the other data-specific flags may all be set.
93 if ((Flags & FlagT::DescriptorsStaticKeepingBufferBoundsChecks) ==
94 FlagT::DescriptorsStaticKeepingBufferBoundsChecks) {
95 FlagT Mask = FlagT::DescriptorsStaticKeepingBufferBoundsChecks;
96 if (!IsSampler) {
97 Mask |= FlagT::DataVolatile;
98 Mask |= FlagT::DataStatic;
99 Mask |= FlagT::DataStaticWhileSetAtExecute;
100 }
101 return (Flags & ~Mask) == FlagT::None;
102 }
103
104 // When no descriptor flag is set, any data flag is allowed.
105 FlagT Mask = FlagT::None;
106 if (!IsSampler) {
107 Mask |= FlagT::DataVolatile;
108 Mask |= FlagT::DataStaticWhileSetAtExecute;
109 Mask |= FlagT::DataStatic;
110 }
111 return (Flags & ~Mask) == FlagT::None;
112}
113
114bool verifyNumDescriptors(uint32_t NumDescriptors) {
115 return NumDescriptors > 0;
116}
117
118bool verifyMipLODBias(float MipLODBias) {
119 return MipLODBias >= -16.f && MipLODBias <= 15.99f;
120}
121
122bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
123 return MaxAnisotropy <= 16u;
124}
125
126bool verifyLOD(float LOD) { return !std::isnan(LOD); }
127
131
133 return Offset <= std::numeric_limits<uint32_t>::max();
134}
135
137 assert(0 < Size && "Must be a non-empty range");
140
141 return uint64_t(Offset) + uint64_t(Size) - 1;
142}
143
144} // namespace rootsig
145} // namespace hlsl
146} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal)
static const uint32_t NumDescriptorsUnbounded
LLVM_ABI uint64_t computeRangeBound(uint32_t Offset, uint32_t Size)
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace)
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type, dxbc::DescriptorRangeFlags FlagsVal)
LLVM_ABI bool verifyVersion(uint32_t Version)
LLVM_ABI bool verifyRootFlag(uint32_t Flags)
LLVM_ABI bool verifyLOD(float LOD)
LLVM_ABI bool verifyNoOverflowedOffset(uint64_t Offset)
LLVM_ABI bool verifyMipLODBias(float MipLODBias)
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors)
LLVM_ABI bool verifyMaxAnisotropy(uint32_t MaxAnisotropy)
LLVM_ABI bool verifyBoundOffset(uint32_t Offset)
LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:307
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.