|
| 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/STLFunctionalExtras.h" |
| 17 | +#include "llvm/ADT/SmallVector.h" |
| 18 | +#include "llvm/Support/DXILABI.h" |
| 19 | +#include "llvm/Support/ErrorHandling.h" |
| 20 | + |
| 21 | +namespace llvm { |
| 22 | +namespace hlsl { |
| 23 | + |
| 24 | +/// BindingInfo represents the ranges of bindings and free space for each |
| 25 | +/// `dxil::ResourceClass`. This can represent HLSL-level bindings as well as |
| 26 | +/// bindings described in root signatures, and can be used for analysis of |
| 27 | +/// overlapping or missing bindings as well as for finding space for implicit |
| 28 | +/// bindings. |
| 29 | +/// |
| 30 | +/// As an example, given these resource bindings: |
| 31 | +/// |
| 32 | +/// RWBuffer<float> A[10] : register(u3); |
| 33 | +/// RWBuffer<float> B[] : register(u5, space2) |
| 34 | +/// |
| 35 | +/// The binding info for UAV bindings should look like this: |
| 36 | +/// |
| 37 | +/// UAVSpaces { |
| 38 | +/// ResClass = ResourceClass::UAV, |
| 39 | +/// Spaces = { |
| 40 | +/// { Space = 0, FreeRanges = {{ 0, 2 }, { 13, UINT32_MAX }} }, |
| 41 | +/// { Space = 2, FreeRanges = {{ 0, 4 }} } |
| 42 | +/// } |
| 43 | +/// } |
| 44 | +class BindingInfo { |
| 45 | +public: |
| 46 | + struct BindingRange { |
| 47 | + uint32_t LowerBound; |
| 48 | + uint32_t UpperBound; |
| 49 | + BindingRange(uint32_t LB, uint32_t UB) : LowerBound(LB), UpperBound(UB) {} |
| 50 | + }; |
| 51 | + |
| 52 | + struct RegisterSpace { |
| 53 | + uint32_t Space; |
| 54 | + SmallVector<BindingRange> FreeRanges; |
| 55 | + RegisterSpace(uint32_t Space) : Space(Space) { |
| 56 | + FreeRanges.emplace_back(0, UINT32_MAX); |
| 57 | + } |
| 58 | + // Size == -1 means unbounded array |
| 59 | + LLVM_ABI std::optional<uint32_t> findAvailableBinding(int32_t Size); |
| 60 | + }; |
| 61 | + |
| 62 | + struct BindingSpaces { |
| 63 | + dxil::ResourceClass RC; |
| 64 | + llvm::SmallVector<RegisterSpace> Spaces; |
| 65 | + BindingSpaces(dxil::ResourceClass RC) : RC(RC) {} |
| 66 | + LLVM_ABI RegisterSpace &getOrInsertSpace(uint32_t Space); |
| 67 | + }; |
| 68 | + |
| 69 | +private: |
| 70 | + BindingSpaces SRVSpaces{dxil::ResourceClass::SRV}; |
| 71 | + BindingSpaces UAVSpaces{dxil::ResourceClass::UAV}; |
| 72 | + BindingSpaces CBufferSpaces{dxil::ResourceClass::CBuffer}; |
| 73 | + BindingSpaces SamplerSpaces{dxil::ResourceClass::Sampler}; |
| 74 | + |
| 75 | +public: |
| 76 | + BindingSpaces &getBindingSpaces(dxil::ResourceClass RC) { |
| 77 | + switch (RC) { |
| 78 | + case dxil::ResourceClass::SRV: |
| 79 | + return SRVSpaces; |
| 80 | + case dxil::ResourceClass::UAV: |
| 81 | + return UAVSpaces; |
| 82 | + case dxil::ResourceClass::CBuffer: |
| 83 | + return CBufferSpaces; |
| 84 | + case dxil::ResourceClass::Sampler: |
| 85 | + return SamplerSpaces; |
| 86 | + } |
| 87 | + |
| 88 | + llvm_unreachable("Invalid resource class"); |
| 89 | + } |
| 90 | + const BindingSpaces &getBindingSpaces(dxil::ResourceClass RC) const { |
| 91 | + return const_cast<BindingInfo *>(this)->getBindingSpaces(RC); |
| 92 | + } |
| 93 | + |
| 94 | + // Size == -1 means unbounded array |
| 95 | + LLVM_ABI std::optional<uint32_t> |
| 96 | + findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size); |
| 97 | + |
| 98 | + friend class BindingInfoBuilder; |
| 99 | +}; |
| 100 | + |
| 101 | +/// Builder class for creating a /c BindingInfo. |
| 102 | +class BindingInfoBuilder { |
| 103 | +public: |
| 104 | + struct Binding { |
| 105 | + dxil::ResourceClass RC; |
| 106 | + uint32_t Space; |
| 107 | + uint32_t LowerBound; |
| 108 | + uint32_t UpperBound; |
| 109 | + const void *Cookie; |
| 110 | + |
| 111 | + Binding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, |
| 112 | + uint32_t UpperBound, const void *Cookie) |
| 113 | + : RC(RC), Space(Space), LowerBound(LowerBound), UpperBound(UpperBound), |
| 114 | + Cookie(Cookie) {} |
| 115 | + |
| 116 | + bool isUnbounded() const { return UpperBound == ~0U; } |
| 117 | + |
| 118 | + bool operator==(const Binding &RHS) const { |
| 119 | + return std::tie(RC, Space, LowerBound, UpperBound, Cookie) == |
| 120 | + std::tie(RHS.RC, RHS.Space, RHS.LowerBound, RHS.UpperBound, |
| 121 | + RHS.Cookie); |
| 122 | + } |
| 123 | + bool operator!=(const Binding &RHS) const { return !(*this == RHS); } |
| 124 | + |
| 125 | + bool operator<(const Binding &RHS) const { |
| 126 | + return std::tie(RC, Space, LowerBound) < |
| 127 | + std::tie(RHS.RC, RHS.Space, RHS.LowerBound); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | +private: |
| 132 | + SmallVector<Binding> Bindings; |
| 133 | + |
| 134 | +public: |
| 135 | + void trackBinding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, |
| 136 | + uint32_t UpperBound, const void *Cookie) { |
| 137 | + Bindings.emplace_back(RC, Space, LowerBound, UpperBound, Cookie); |
| 138 | + } |
| 139 | + /// Calculate the binding info - \c ReportOverlap will be called once for each |
| 140 | + /// overlapping binding. |
| 141 | + BindingInfo calculateBindingInfo( |
| 142 | + llvm::function_ref<void(const BindingInfoBuilder &Builder, |
| 143 | + const Binding &Overlapping)> |
| 144 | + ReportOverlap); |
| 145 | + |
| 146 | + /// Calculate the binding info - \c HasOverlap will be set to indicate whether |
| 147 | + /// there are any overlapping bindings. |
| 148 | + BindingInfo calculateBindingInfo(bool &HasOverlap) { |
| 149 | + HasOverlap = false; |
| 150 | + return calculateBindingInfo( |
| 151 | + [&HasOverlap](auto, auto) { HasOverlap = true; }); |
| 152 | + } |
| 153 | + |
| 154 | + /// For use in the \c ReportOverlap callback of \c calculateBindingInfo - |
| 155 | + /// finds a binding that the \c ReportedBinding overlaps with. |
| 156 | + const Binding &findOverlapping(const Binding &ReportedBinding) const; |
| 157 | +}; |
| 158 | + |
| 159 | +} // namespace hlsl |
| 160 | +} // namespace llvm |
| 161 | + |
| 162 | +#endif // LLVM_FRONTEND_HLSL_HLSLBINDING_H |
0 commit comments