Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
HLSLBinding.h
Go to the documentation of this file.
1//===- HLSLBinding.h - Representation for resource bindings in HLSL -------===//
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 objects to represent resource bindings.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_FRONTEND_HLSL_HLSLBINDING_H
14#define LLVM_FRONTEND_HLSL_HLSLBINDING_H
15
16#include "llvm/ADT/STLExtras.h"
22
23namespace llvm {
24namespace hlsl {
25
26/// BindingInfo represents the ranges of bindings and free space for each
27/// `dxil::ResourceClass`. This can represent HLSL-level bindings as well as
28/// bindings described in root signatures, and can be used for analysis of
29/// overlapping or missing bindings as well as for finding space for implicit
30/// bindings.
31///
32/// As an example, given these resource bindings:
33///
34/// RWBuffer<float> A[10] : register(u3);
35/// RWBuffer<float> B[] : register(u5, space2)
36///
37/// The binding info for UAV bindings should look like this:
38///
39/// UAVSpaces {
40/// ResClass = ResourceClass::UAV,
41/// Spaces = {
42/// { Space = 0u, FreeRanges = {{ 0u, 2u }, { 13u, ~0u }} },
43/// { Space = 2u, FreeRanges = {{ 0u, 4u }} }
44/// }
45/// }
47public:
53
58 FreeRanges.emplace_back(0, ~0u);
59 }
60 // Size == -1 means unbounded array
61 LLVM_ABI std::optional<uint32_t> findAvailableBinding(int32_t Size);
62 };
63
70
71private:
72 BindingSpaces SRVSpaces{dxil::ResourceClass::SRV};
73 BindingSpaces UAVSpaces{dxil::ResourceClass::UAV};
74 BindingSpaces CBufferSpaces{dxil::ResourceClass::CBuffer};
76
77public:
79 switch (RC) {
81 return SRVSpaces;
83 return UAVSpaces;
85 return CBufferSpaces;
87 return SamplerSpaces;
88 }
89
90 llvm_unreachable("Invalid resource class");
91 }
93 return const_cast<BindingInfo *>(this)->getBindingSpaces(RC);
94 }
95
96 // Size == -1 means unbounded array
97 LLVM_ABI std::optional<uint32_t>
99
100 friend class BindingInfoBuilder;
101};
102
103struct Binding {
108 const void *Cookie;
109
114
115 bool isUnbounded() const { return UpperBound == ~0U; }
116
117 bool operator==(const Binding &RHS) const {
118 return std::tie(RC, Space, LowerBound, UpperBound, Cookie) ==
119 std::tie(RHS.RC, RHS.Space, RHS.LowerBound, RHS.UpperBound,
120 RHS.Cookie);
121 }
122 bool operator!=(const Binding &RHS) const { return !(*this == RHS); }
123
124 bool operator<(const Binding &RHS) const {
125 return std::tie(RC, Space, LowerBound) <
126 std::tie(RHS.RC, RHS.Space, RHS.LowerBound);
127 }
128};
129
131 SmallVector<Binding> Bindings;
132
133public:
134 BoundRegs(SmallVector<Binding> &&Bindings) : Bindings(std::move(Bindings)) {}
135
137 uint32_t LowerBound, uint32_t UpperBound) const {
138 // UpperBound and Cookie are given dummy values, since they aren't
139 // interesting for operator<
140 const Binding *It =
141 llvm::upper_bound(Bindings, Binding{RC, Space, LowerBound, 0, nullptr});
142 if (It == Bindings.begin())
143 return nullptr;
144 --It;
145 if (It->RC == RC && It->Space == Space && It->LowerBound <= LowerBound &&
146 It->UpperBound >= UpperBound)
147 return It;
148 return nullptr;
149 }
150};
151
152/// Builder class for creating a /c BindingInfo.
154private:
155 SmallVector<Binding> Bindings;
156
157public:
159 uint32_t UpperBound, const void *Cookie) {
160 Bindings.emplace_back(RC, Space, LowerBound, UpperBound, Cookie);
161 }
162 /// Calculate the binding info - \c ReportOverlap will be called once for each
163 /// overlapping binding.
165 llvm::function_ref<void(const BindingInfoBuilder &Builder,
166 const Binding &Overlapping)>
167 ReportOverlap);
168
169 /// Calculate the binding info - \c HasOverlap will be set to indicate whether
170 /// there are any overlapping bindings.
172 HasOverlap = false;
174 [&HasOverlap](auto, auto) { HasOverlap = true; });
175 }
176
178 assert(std::is_sorted(Bindings.begin(), Bindings.end()) &&
179 "takeBoundRegs should only be called after calculateBindingInfo");
180 return BoundRegs(std::move(Bindings));
181 }
182
183 /// For use in the \c ReportOverlap callback of \c calculateBindingInfo -
184 /// finds a binding that the \c ReportedBinding overlaps with.
185 LLVM_ABI const Binding &findOverlapping(const Binding &ReportedBinding) const;
186};
187
188} // namespace hlsl
189} // namespace llvm
190
191#endif // LLVM_FRONTEND_HLSL_HLSLBINDING_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
Value * RHS
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An efficient, type-erasing, non-owning reference to a callable.
Builder class for creating a /c BindingInfo.
LLVM_ABI const Binding & findOverlapping(const Binding &ReportedBinding) const
For use in the ReportOverlap callback of calculateBindingInfo - finds a binding that the ReportedBind...
void trackBinding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound, const void *Cookie)
LLVM_ABI BindingInfo calculateBindingInfo(llvm::function_ref< void(const BindingInfoBuilder &Builder, const Binding &Overlapping)> ReportOverlap)
Calculate the binding info - ReportOverlap will be called once for each overlapping binding.
LLVM_ABI BoundRegs takeBoundRegs()
BindingInfo calculateBindingInfo(bool &HasOverlap)
Calculate the binding info - HasOverlap will be set to indicate whether there are any overlapping bin...
BindingInfo represents the ranges of bindings and free space for each dxil::ResourceClass.
Definition HLSLBinding.h:46
const BindingSpaces & getBindingSpaces(dxil::ResourceClass RC) const
Definition HLSLBinding.h:92
friend class BindingInfoBuilder
LLVM_ABI std::optional< uint32_t > findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size)
BindingSpaces & getBindingSpaces(dxil::ResourceClass RC)
Definition HLSLBinding.h:78
const Binding * findBoundReg(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound) const
BoundRegs(SmallVector< Binding > &&Bindings)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:1987
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1847
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
BindingRange(uint32_t LB, uint32_t UB)
Definition HLSLBinding.h:51
LLVM_ABI RegisterSpace & getOrInsertSpace(uint32_t Space)
llvm::SmallVector< RegisterSpace > Spaces
Definition HLSLBinding.h:66
BindingSpaces(dxil::ResourceClass RC)
Definition HLSLBinding.h:67
SmallVector< BindingRange > FreeRanges
Definition HLSLBinding.h:56
LLVM_ABI std::optional< uint32_t > findAvailableBinding(int32_t Size)
bool operator==(const Binding &RHS) const
dxil::ResourceClass RC
bool operator<(const Binding &RHS) const
bool isUnbounded() const
Binding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound, const void *Cookie)
const void * Cookie
bool operator!=(const Binding &RHS) const