LLVM 23.0.0git
HLSLRootSignature.cpp
Go to the documentation of this file.
1//===- HLSLRootSignature.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
17
18namespace llvm {
19namespace hlsl {
20namespace rootsig {
21
22template <typename T>
24 EnumStrings<T> Flags) {
25 bool FlagSet = false;
26 unsigned Remaining = llvm::to_underlying(Value);
27 while (Remaining) {
28 unsigned Bit = 1u << llvm::countr_zero(Remaining);
29 if (Remaining & Bit) {
30 if (FlagSet)
31 OS << " | ";
32
33 StringRef MaybeFlag = Flags.toString(T(Bit));
34 if (!MaybeFlag.empty())
35 OS << MaybeFlag;
36 else
37 OS << "invalid: " << Bit;
38
39 FlagSet = true;
40 }
41 Remaining &= ~Bit;
42 }
43
44 if (!FlagSet)
45 OS << "None";
46 return OS;
47}
48
50 constexpr EnumStringDef<RegisterType> RegisterNameDefs[] = {
51 {{"b"}, RegisterType::BReg},
52 {{"t"}, RegisterType::TReg},
53 {{"u"}, RegisterType::UReg},
54 {{"s"}, RegisterType::SReg},
55 };
56 static constexpr auto RegisterNames = BUILD_ENUM_STRINGS(RegisterNameDefs);
57 OS << EnumStrings(RegisterNames).toString(Reg.ViewType) << Reg.Number;
58 return OS;
59}
60
62 const llvm::dxbc::ShaderVisibility &Visibility) {
63 OS << dxbc::getShaderVisibility().toString(Visibility);
64
65 return OS;
66}
67
70 OS << dxbc::getSamplerFilters().toString(Filter);
71
72 return OS;
73}
74
77 OS << dxbc::getTextureAddressModes().toString(Address);
78
79 return OS;
80}
81
83 const dxbc::ComparisonFunc &CompFunc) {
84 OS << dxbc::getComparisonFuncs().toString(CompFunc);
85
86 return OS;
87}
88
90 const dxbc::StaticBorderColor &BorderColor) {
91 OS << dxbc::getStaticBorderColors().toString(BorderColor);
92
93 return OS;
94}
95
99 return OS;
100}
101
103 const dxbc::RootDescriptorFlags &Flags) {
105
106 return OS;
107}
108
112
113 return OS;
114}
115
117 const llvm::dxbc::StaticSamplerFlags &Flags) {
119
120 return OS;
121}
122
124 OS << "RootFlags(";
125 printFlags(OS, Flags, dxbc::getRootFlags());
126 OS << ")";
127
128 return OS;
129}
130
132 OS << "RootConstants(num32BitConstants = " << Constants.Num32BitConstants
133 << ", " << Constants.Reg << ", space = " << Constants.Space
134 << ", visibility = " << Constants.Visibility << ")";
135
136 return OS;
137}
138
140 OS << "DescriptorTable(numClauses = " << Table.NumClauses
141 << ", visibility = " << Table.Visibility << ")";
142
143 return OS;
144}
145
147 OS << Clause.Type << "(" << Clause.Reg << ", numDescriptors = ";
148 if (Clause.NumDescriptors == NumDescriptorsUnbounded)
149 OS << "unbounded";
150 else
151 OS << Clause.NumDescriptors;
152 OS << ", space = " << Clause.Space << ", offset = ";
154 OS << "DescriptorTableOffsetAppend";
155 else
156 OS << Clause.Offset;
157 OS << ", flags = " << Clause.Flags << ")";
158
159 return OS;
160}
161
163 OS << "Root" << Descriptor.Type << "(" << Descriptor.Reg
164 << ", space = " << Descriptor.Space
165 << ", visibility = " << Descriptor.Visibility
166 << ", flags = " << Descriptor.Flags << ")";
167
168 return OS;
169}
170
172 OS << "StaticSampler(" << Sampler.Reg << ", filter = " << Sampler.Filter
173 << ", addressU = " << Sampler.AddressU
174 << ", addressV = " << Sampler.AddressV
175 << ", addressW = " << Sampler.AddressW
176 << ", mipLODBias = " << Sampler.MipLODBias
177 << ", maxAnisotropy = " << Sampler.MaxAnisotropy
178 << ", comparisonFunc = " << Sampler.CompFunc
179 << ", borderColor = " << Sampler.BorderColor
180 << ", minLOD = " << Sampler.MinLOD << ", maxLOD = " << Sampler.MaxLOD
181 << ", space = " << Sampler.Space << ", visibility = " << Sampler.Visibility
182 << ", flags = " << Sampler.Flags << ")";
183 return OS;
184}
185
186namespace {
187
188// We use the OverloadVisit with std::visit to ensure the compiler catches if a
189// new RootElement variant type is added but it's operator<< isn't handled.
190template <class... Ts> struct OverloadedVisit : Ts... {
191 using Ts::operator()...;
192};
193template <class... Ts> OverloadedVisit(Ts...) -> OverloadedVisit<Ts...>;
194
195} // namespace
196
198 const auto Visitor = OverloadedVisit{
199 [&OS](const dxbc::RootFlags &Flags) { OS << Flags; },
200 [&OS](const RootConstants &Constants) { OS << Constants; },
201 [&OS](const RootDescriptor &Descriptor) { OS << Descriptor; },
202 [&OS](const DescriptorTableClause &Clause) { OS << Clause; },
203 [&OS](const DescriptorTable &Table) { OS << Table; },
204 [&OS](const StaticSampler &Sampler) { OS << Sampler; },
205 };
206 std::visit(Visitor, Element);
207 return OS;
208}
209
211 OS << " RootElements" << interleaved(Elements, ", ", "{", "}");
212}
213
214} // namespace rootsig
215} // namespace hlsl
216} // namespace llvm
#define BUILD_ENUM_STRINGS(Tab)
Definition Enum.h:120
Register Reg
#define T
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:54
LLVM_ABI EnumStrings< TextureAddressMode, 1 > getTextureAddressModes()
LLVM_ABI EnumStrings< DescriptorRangeFlags, 1 > getDescriptorRangeFlags()
LLVM_ABI EnumStrings< RootDescriptorFlags, 1 > getRootDescriptorFlags()
LLVM_ABI EnumStrings< SamplerFilter, 1 > getSamplerFilters()
LLVM_ABI EnumStrings< StaticBorderColor, 1 > getStaticBorderColors()
LLVM_ABI EnumStrings< ShaderVisibility, 1 > getShaderVisibility()
LLVM_ABI EnumStrings< ComparisonFunc, 1 > getComparisonFuncs()
LLVM_ABI EnumStrings< RootFlags, 1 > getRootFlags()
LLVM_ABI EnumStrings< StaticSamplerFlags, 1 > getStaticSamplerFlags()
LLVM_ABI StringRef getResourceClassName(ResourceClass RC)
Definition DXILABI.cpp:21
static const uint32_t NumDescriptorsUnbounded
static const uint32_t DescriptorTableOffsetAppend
LLVM_ABI void dumpRootElements(raw_ostream &OS, ArrayRef< RootElement > Elements)
static raw_ostream & printFlags(raw_ostream &OS, const T Value, EnumStrings< T > Flags)
std::variant< dxbc::RootFlags, RootConstants, RootDescriptor, DescriptorTable, DescriptorTableClause, StaticSampler > RootElement
Models RootElement : RootFlags | RootConstants | RootParam | DescriptorTable | DescriptorTableClause ...
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const dxbc::RootFlags &Flags)
The following contains the serialization interface for root elements.
This is an optimization pass for GlobalISel generic memory operations.
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
EnumStrings(const EnumStringsStorage< T, NumStrs, N, StrLen > &) -> EnumStrings< T, NumStrs >
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
Compile-time data representation of enum entries.
Definition Enum.h:47