|
| 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 | + |
| 13 | +#include "llvm/Frontend/HLSL/RootSignatureValidations.h" |
| 14 | + |
| 15 | +namespace llvm { |
| 16 | +namespace hlsl { |
| 17 | +namespace rootsig { |
| 18 | + |
| 19 | +std::optional<const RangeInfo *> |
| 20 | +ResourceRange::getOverlapping(const RangeInfo &Info) const { |
| 21 | + MapT::const_iterator Interval = Intervals.find(Info.LowerBound); |
| 22 | + if (!Interval.valid() || Info.UpperBound < Interval.start()) |
| 23 | + return std::nullopt; |
| 24 | + return Interval.value(); |
| 25 | +} |
| 26 | + |
| 27 | +const RangeInfo *ResourceRange::lookup(uint32_t X) const { |
| 28 | + return Intervals.lookup(X, nullptr); |
| 29 | +} |
| 30 | + |
| 31 | +void ResourceRange::clear() { return Intervals.clear(); } |
| 32 | + |
| 33 | +std::optional<const RangeInfo *> ResourceRange::insert(const RangeInfo &Info) { |
| 34 | + uint32_t LowerBound = Info.LowerBound; |
| 35 | + uint32_t UpperBound = Info.UpperBound; |
| 36 | + |
| 37 | + std::optional<const RangeInfo *> Res = std::nullopt; |
| 38 | + MapT::iterator Interval = Intervals.begin(); |
| 39 | + |
| 40 | + while (true) { |
| 41 | + if (UpperBound < LowerBound) |
| 42 | + break; |
| 43 | + |
| 44 | + Interval.advanceTo(LowerBound); |
| 45 | + if (!Interval.valid()) // No interval found |
| 46 | + break; |
| 47 | + |
| 48 | + // Let Interval = [x;y] and [LowerBound;UpperBound] = [a;b] and note that |
| 49 | + // a <= y implicitly from Intervals.find(LowerBound) |
| 50 | + if (UpperBound < Interval.start()) |
| 51 | + break; // found interval does not overlap with inserted one |
| 52 | + |
| 53 | + if (!Res.has_value()) // Update to be the first found intersection |
| 54 | + Res = Interval.value(); |
| 55 | + |
| 56 | + if (Interval.start() <= LowerBound && UpperBound <= Interval.stop()) { |
| 57 | + // x <= a <= b <= y implies that [a;b] is covered by [x;y] |
| 58 | + // -> so we don't need to insert this, report an overlap |
| 59 | + return Res; |
| 60 | + } else if (LowerBound <= Interval.start() && |
| 61 | + Interval.stop() <= UpperBound) { |
| 62 | + // a <= x <= y <= b implies that [x;y] is covered by [a;b] |
| 63 | + // -> so remove the existing interval that we will cover with the |
| 64 | + // overwrite |
| 65 | + Interval.erase(); |
| 66 | + } else if (LowerBound < Interval.start() && UpperBound <= Interval.stop()) { |
| 67 | + // a < x <= b <= y implies that [a; x] is not covered but [x;b] is |
| 68 | + // -> so set b = x - 1 such that [a;x-1] is now the interval to insert |
| 69 | + UpperBound = Interval.start() - 1; |
| 70 | + } else if (Interval.start() <= LowerBound && Interval.stop() < UpperBound) { |
| 71 | + // a < x <= b <= y implies that [y; b] is not covered but [a;y] is |
| 72 | + // -> so set a = y + 1 such that [y+1;b] is now the interval to insert |
| 73 | + LowerBound = Interval.stop() + 1; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assert(LowerBound <= UpperBound && "Attempting to insert an empty interval"); |
| 78 | + Intervals.insert(LowerBound, UpperBound, &Info); |
| 79 | + return Res; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace rootsig |
| 83 | +} // namespace hlsl |
| 84 | +} // namespace llvm |
0 commit comments