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:
48 struct BindingRange {
52 };
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
69 };
70
71private:
72 BindingSpaces SRVSpaces{dxil::ResourceClass::SRV};
73 BindingSpaces UAVSpaces{dxil::ResourceClass::UAV};
74 BindingSpaces CBufferSpaces{dxil::ResourceClass::CBuffer};
75 BindingSpaces SamplerSpaces{dxil::ResourceClass::Sampler};
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
111 uint32_t UpperBound, const void *Cookie)
113 Cookie(Cookie) {}
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
136 bool isBound(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound,
137 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 false;
144 --It;
145 return It->RC == RC && It->Space == Space && It->LowerBound <= LowerBound &&
146 It->UpperBound >= UpperBound;
147 }
148};
149
150/// Builder class for creating a /c BindingInfo.
152private:
153 SmallVector<Binding> Bindings;
154
155public:
157 uint32_t UpperBound, const void *Cookie) {
158 Bindings.emplace_back(RC, Space, LowerBound, UpperBound, Cookie);
159 }
160 /// Calculate the binding info - \c ReportOverlap will be called once for each
161 /// overlapping binding.
163 llvm::function_ref<void(const BindingInfoBuilder &Builder,
164 const Binding &Overlapping)>
165 ReportOverlap);
166
167 /// Calculate the binding info - \c HasOverlap will be set to indicate whether
168 /// there are any overlapping bindings.
170 HasOverlap = false;
172 [&HasOverlap](auto, auto) { HasOverlap = true; });
173 }
174
176 assert(std::is_sorted(Bindings.begin(), Bindings.end()) &&
177 "takeBoundRegs should only be called after calculateBindingInfo");
178 return BoundRegs(std::move(Bindings));
179 }
180
181 /// For use in the \c ReportOverlap callback of \c calculateBindingInfo -
182 /// finds a binding that the \c ReportedBinding overlaps with.
183 LLVM_ABI const Binding &findOverlapping(const Binding &ReportedBinding) const;
184};
185
186} // namespace hlsl
187} // namespace llvm
188
189#endif // LLVM_FRONTEND_HLSL_HLSLBINDING_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Size
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
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An efficient, type-erasing, non-owning reference to a callable.
Builder class for creating a /c BindingInfo.
Definition: HLSLBinding.h:151
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)
Definition: HLSLBinding.h:156
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.
Definition: HLSLBinding.cpp:69
LLVM_ABI BoundRegs takeBoundRegs()
Definition: HLSLBinding.h:175
BindingInfo calculateBindingInfo(bool &HasOverlap)
Calculate the binding info - HasOverlap will be set to indicate whether there are any overlapping bin...
Definition: HLSLBinding.h:169
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
LLVM_ABI std::optional< uint32_t > findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size)
Definition: HLSLBinding.cpp:16
BindingSpaces & getBindingSpaces(dxil::ResourceClass RC)
Definition: HLSLBinding.h:78
bool isBound(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound) const
Definition: HLSLBinding.h:136
BoundRegs(SmallVector< Binding > &&Bindings)
Definition: HLSLBinding.h:134
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ResourceClass
Definition: DXILABI.h:26
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:2026
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
BindingRange(uint32_t LB, uint32_t UB)
Definition: HLSLBinding.h:51
LLVM_ABI RegisterSpace & getOrInsertSpace(uint32_t Space)
Definition: HLSLBinding.cpp:24
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)
Definition: HLSLBinding.cpp:36
bool operator==(const Binding &RHS) const
Definition: HLSLBinding.h:117
dxil::ResourceClass RC
Definition: HLSLBinding.h:104
bool operator<(const Binding &RHS) const
Definition: HLSLBinding.h:124
bool isUnbounded() const
Definition: HLSLBinding.h:115
Binding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound, const void *Cookie)
Definition: HLSLBinding.h:110
const void * Cookie
Definition: HLSLBinding.h:108
bool operator!=(const Binding &RHS) const
Definition: HLSLBinding.h:122