aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Frontend/HLSLRootSignatureRangesTest.cpp
blob: 0ef6fe84f0ec99b15067a97d0f0f6e5fcf43b226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===------ HLSLRootSignatureRangeTest.cpp - RootSignature Range tests ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Frontend/HLSL/HLSLRootSignatureUtils.h"
#include "gtest/gtest.h"

using namespace llvm::hlsl::rootsig;

namespace {

TEST(HLSLRootSignatureTest, NoOverlappingInsertTests) {
  // Ensures that there is never a reported overlap
  ResourceRange::MapT::Allocator Allocator;
  ResourceRange Range(Allocator);

  RangeInfo A;
  A.LowerBound = 0;
  A.UpperBound = 3;
  EXPECT_EQ(Range.insert(A), std::nullopt);

  RangeInfo B;
  B.LowerBound = 4;
  B.UpperBound = 7;
  EXPECT_EQ(Range.insert(B), std::nullopt);

  RangeInfo C;
  C.LowerBound = 10;
  C.UpperBound = RangeInfo::Unbounded;
  EXPECT_EQ(Range.insert(C), std::nullopt);

  // A = [0;3]
  EXPECT_EQ(Range.lookup(0), &A);
  EXPECT_EQ(Range.lookup(2), &A);
  EXPECT_EQ(Range.lookup(3), &A);

  // B = [4;7]
  EXPECT_EQ(Range.lookup(4), &B);
  EXPECT_EQ(Range.lookup(5), &B);
  EXPECT_EQ(Range.lookup(7), &B);

  EXPECT_EQ(Range.lookup(8), nullptr);
  EXPECT_EQ(Range.lookup(9), nullptr);

  // C = [10;unbounded]
  EXPECT_EQ(Range.lookup(10), &C);
  EXPECT_EQ(Range.lookup(42), &C);
  EXPECT_EQ(Range.lookup(98237423), &C);
  EXPECT_EQ(Range.lookup(RangeInfo::Unbounded), &C);
}

TEST(HLSLRootSignatureTest, SingleOverlappingInsertTests) {
  // Ensures that we correctly report an overlap when we insert a range that
  // overlaps with one other range but does not cover (replace) it
  ResourceRange::MapT::Allocator Allocator;
  ResourceRange Range(Allocator);

  RangeInfo A;
  A.LowerBound = 1;
  A.UpperBound = 5;
  EXPECT_EQ(Range.insert(A), std::nullopt);

  RangeInfo B;
  B.LowerBound = 0;
  B.UpperBound = 2;
  EXPECT_EQ(Range.insert(B).value(), &A);

  RangeInfo C;
  C.LowerBound = 4;
  C.UpperBound = RangeInfo::Unbounded;
  EXPECT_EQ(Range.insert(C).value(), &A);

  // A = [1;5]
  EXPECT_EQ(Range.lookup(1), &A);
  EXPECT_EQ(Range.lookup(2), &A);
  EXPECT_EQ(Range.lookup(3), &A);
  EXPECT_EQ(Range.lookup(4), &A);
  EXPECT_EQ(Range.lookup(5), &A);

  // B = [0;0]
  EXPECT_EQ(Range.lookup(0), &B);

  // C = [6; unbounded]
  EXPECT_EQ(Range.lookup(6), &C);
  EXPECT_EQ(Range.lookup(RangeInfo::Unbounded), &C);
}

TEST(HLSLRootSignatureTest, MultipleOverlappingInsertTests) {
  // Ensures that we correctly report an overlap when inserted range
  // overlaps more than one range and it does not cover (replace) either
  // range. In this case it will just fill in the interval between the two
  ResourceRange::MapT::Allocator Allocator;
  ResourceRange Range(Allocator);

  RangeInfo A;
  A.LowerBound = 0;
  A.UpperBound = 2;
  EXPECT_EQ(Range.insert(A), std::nullopt);

  RangeInfo B;
  B.LowerBound = 4;
  B.UpperBound = 6;
  EXPECT_EQ(Range.insert(B), std::nullopt);

  RangeInfo C;
  C.LowerBound = 1;
  C.UpperBound = 5;
  EXPECT_EQ(Range.insert(C).value(), &A);

  // A = [0;2]
  EXPECT_EQ(Range.lookup(0), &A);
  EXPECT_EQ(Range.lookup(1), &A);
  EXPECT_EQ(Range.lookup(2), &A);

  // B = [4;6]
  EXPECT_EQ(Range.lookup(4), &B);
  EXPECT_EQ(Range.lookup(5), &B);
  EXPECT_EQ(Range.lookup(6), &B);

  // C = [3;3]
  EXPECT_EQ(Range.lookup(3), &C);
}

TEST(HLSLRootSignatureTest, CoverInsertTests) {
  // Ensures that we correctly report an overlap when inserted range
  // covers one or more ranges
  ResourceRange::MapT::Allocator Allocator;
  ResourceRange Range(Allocator);

  RangeInfo A;
  A.LowerBound = 0;
  A.UpperBound = 2;
  EXPECT_EQ(Range.insert(A), std::nullopt);

  RangeInfo B;
  B.LowerBound = 4;
  B.UpperBound = 5;
  EXPECT_EQ(Range.insert(B), std::nullopt);

  // Covers B
  RangeInfo C;
  C.LowerBound = 4;
  C.UpperBound = 6;
  EXPECT_EQ(Range.insert(C).value(), &B);

  // A = [0;2]
  // C = [4;6] <- covers reference to B
  EXPECT_EQ(Range.lookup(0), &A);
  EXPECT_EQ(Range.lookup(1), &A);
  EXPECT_EQ(Range.lookup(2), &A);
  EXPECT_EQ(Range.lookup(3), nullptr);
  EXPECT_EQ(Range.lookup(4), &C);
  EXPECT_EQ(Range.lookup(5), &C);
  EXPECT_EQ(Range.lookup(6), &C);

  // Covers all other ranges
  RangeInfo D;
  D.LowerBound = 0;
  D.UpperBound = 7;
  EXPECT_EQ(Range.insert(D).value(), &A);

  // D = [0;7] <- Covers reference to A and C
  EXPECT_EQ(Range.lookup(0), &D);
  EXPECT_EQ(Range.lookup(1), &D);
  EXPECT_EQ(Range.lookup(2), &D);
  EXPECT_EQ(Range.lookup(3), &D);
  EXPECT_EQ(Range.lookup(4), &D);
  EXPECT_EQ(Range.lookup(5), &D);
  EXPECT_EQ(Range.lookup(6), &D);
  EXPECT_EQ(Range.lookup(7), &D);
}

} // namespace