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
55 switch (Type) {
56 case llvm::to_underlying(dxbc::DescriptorRangeType::CBV):
57 case llvm::to_underlying(dxbc::DescriptorRangeType::SRV):
58 case llvm::to_underlying(dxbc::DescriptorRangeType::UAV):
59 case llvm::to_underlying(dxbc::DescriptorRangeType::Sampler):
60 return true;
61 };
62
63 return false;
64}
65
67 uint32_t FlagsVal) {
68 using FlagT = dxbc::DescriptorRangeFlags;
69 FlagT Flags = FlagT(FlagsVal);
70
71 const bool IsSampler =
72 (Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler));
73
74 if (Version == 1) {
75 // Since the metadata is unversioned, we expect to explicitly see the values
76 // that map to the version 1 behaviour here.
77 if (IsSampler)
78 return Flags == FlagT::DescriptorsVolatile;
79 return Flags == (FlagT::DataVolatile | FlagT::DescriptorsVolatile);
80 }
81
82 // The data-specific flags are mutually exclusive.
83 FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
84 FlagT::DataStaticWhileSetAtExecute;
85
86 if (popcount(llvm::to_underlying(Flags & DataFlags)) > 1)
87 return false;
88
89 // The descriptor-specific flags are mutually exclusive.
90 FlagT DescriptorFlags = FlagT::DescriptorsStaticKeepingBufferBoundsChecks |
91 FlagT::DescriptorsVolatile;
92 if (popcount(llvm::to_underlying(Flags & DescriptorFlags)) > 1)
93 return false;
94
95 // For volatile descriptors, DATA_is never valid.
96 if ((Flags & FlagT::DescriptorsVolatile) == FlagT::DescriptorsVolatile) {
97 FlagT Mask = FlagT::DescriptorsVolatile;
98 if (!IsSampler) {
99 Mask |= FlagT::DataVolatile;
100 Mask |= FlagT::DataStaticWhileSetAtExecute;
101 }
102 return (Flags & ~Mask) == FlagT::None;
103 }
104
105 // For "KEEPING_BUFFER_BOUNDS_CHECKS" descriptors,
106 // the other data-specific flags may all be set.
107 if ((Flags & FlagT::DescriptorsStaticKeepingBufferBoundsChecks) ==
108 FlagT::DescriptorsStaticKeepingBufferBoundsChecks) {
109 FlagT Mask = FlagT::DescriptorsStaticKeepingBufferBoundsChecks;
110 if (!IsSampler) {
111 Mask |= FlagT::DataVolatile;
112 Mask |= FlagT::DataStatic;
113 Mask |= FlagT::DataStaticWhileSetAtExecute;
114 }
115 return (Flags & ~Mask) == FlagT::None;
116 }
117
118 // When no descriptor flag is set, any data flag is allowed.
119 FlagT Mask = FlagT::None;
120 if (!IsSampler) {
121 Mask |= FlagT::DataVolatile;
122 Mask |= FlagT::DataStaticWhileSetAtExecute;
123 Mask |= FlagT::DataStatic;
124 }
125 return (Flags & ~Mask) == FlagT::None;
126}
127
128bool verifyNumDescriptors(uint32_t NumDescriptors) {
129 return NumDescriptors > 0;
130}
131
133 switch (Value) {
134#define FILTER(Num, Val) case llvm::to_underlying(dxbc::SamplerFilter::Val):
135#include "llvm/BinaryFormat/DXContainerConstants.def"
136 return true;
137 }
138 return false;
139}
140
141// Values allowed here:
142// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax
144 switch (Address) {
145#define TEXTURE_ADDRESS_MODE(Num, Val) \
146 case llvm::to_underlying(dxbc::TextureAddressMode::Val):
147#include "llvm/BinaryFormat/DXContainerConstants.def"
148 return true;
149 }
150 return false;
151}
152
153bool verifyMipLODBias(float MipLODBias) {
154 return MipLODBias >= -16.f && MipLODBias <= 15.99f;
155}
156
157bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
158 return MaxAnisotropy <= 16u;
159}
160
161bool verifyComparisonFunc(uint32_t ComparisonFunc) {
162 switch (ComparisonFunc) {
163#define COMPARISON_FUNC(Num, Val) \
164 case llvm::to_underlying(dxbc::ComparisonFunc::Val):
165#include "llvm/BinaryFormat/DXContainerConstants.def"
166 return true;
167 }
168 return false;
169}
170
171bool verifyBorderColor(uint32_t BorderColor) {
172 switch (BorderColor) {
173#define STATIC_BORDER_COLOR(Num, Val) \
174 case llvm::to_underlying(dxbc::StaticBorderColor::Val):
175#include "llvm/BinaryFormat/DXContainerConstants.def"
176 return true;
177 }
178 return false;
179}
180
181bool verifyLOD(float LOD) { return !std::isnan(LOD); }
182
183} // namespace rootsig
184} // namespace hlsl
185} // 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)
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace)
LLVM_ABI bool verifyComparisonFunc(uint32_t ComparisonFunc)
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 verifyMipLODBias(float MipLODBias)
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors)
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type, uint32_t FlagsVal)
LLVM_ABI bool verifyMaxAnisotropy(uint32_t MaxAnisotropy)
LLVM_ABI bool verifyRangeType(uint32_t Type)
LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:307
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.