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
119 switch (Value) {
120#define FILTER(Num, Val) case llvm::to_underlying(dxbc::SamplerFilter::Val):
121#include "llvm/BinaryFormat/DXContainerConstants.def"
122 return true;
123 }
124 return false;
125}
126
127// Values allowed here:
128// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax
130 switch (Address) {
131#define TEXTURE_ADDRESS_MODE(Num, Val) \
132 case llvm::to_underlying(dxbc::TextureAddressMode::Val):
133#include "llvm/BinaryFormat/DXContainerConstants.def"
134 return true;
135 }
136 return false;
137}
138
139bool verifyMipLODBias(float MipLODBias) {
140 return MipLODBias >= -16.f && MipLODBias <= 15.99f;
141}
142
143bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
144 return MaxAnisotropy <= 16u;
145}
146
147bool verifyComparisonFunc(uint32_t ComparisonFunc) {
148 switch (ComparisonFunc) {
149#define COMPARISON_FUNC(Num, Val) \
150 case llvm::to_underlying(dxbc::ComparisonFunc::Val):
151#include "llvm/BinaryFormat/DXContainerConstants.def"
152 return true;
153 }
154 return false;
155}
156
157bool verifyBorderColor(uint32_t BorderColor) {
158 switch (BorderColor) {
159#define STATIC_BORDER_COLOR(Num, Val) \
160 case llvm::to_underlying(dxbc::StaticBorderColor::Val):
161#include "llvm/BinaryFormat/DXContainerConstants.def"
162 return true;
163 }
164 return false;
165}
166
167bool verifyLOD(float LOD) { return !std::isnan(LOD); }
168
172
174 return Offset <= std::numeric_limits<uint32_t>::max();
175}
176
178 assert(0 < Size && "Must be a non-empty range");
181
182 return uint64_t(Offset) + uint64_t(Size) - 1;
183}
184
185} // namespace rootsig
186} // namespace hlsl
187} // 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 Value Representation.
Definition Value.h:75
LLVM_ABI bool verifyAddress(uint32_t Address)
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 verifyComparisonFunc(uint32_t ComparisonFunc)
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type, dxbc::DescriptorRangeFlags FlagsVal)
LLVM_ABI bool verifySamplerFilter(uint32_t Value)
LLVM_ABI bool verifyVersion(uint32_t Version)
LLVM_ABI bool verifyRootFlag(uint32_t Flags)
LLVM_ABI bool verifyLOD(float LOD)
LLVM_ABI bool verifyBorderColor(uint32_t BorderColor)
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.