2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 #include <gtest/gtest.h>
25 #include "ir_array_refcount.h"
27 class array_refcount_test
: public ::testing::Test
{
30 virtual void TearDown();
35 * glsl_type for a vec4[3][4][5].
37 * The exceptionally verbose name is picked because it matches the syntax
38 * of http://cdecl.org/.
40 const glsl_type
*array_3_of_array_4_of_array_5_of_vec4
;
43 * Wrapper to access private member "bits" of ir_array_refcount_entry
45 * The test class is a friend to ir_array_refcount_entry, but the
46 * individual tests are not part of the class. Since the friendliness of
47 * the test class does not extend to the tests, provide a wrapper.
49 const BITSET_WORD
*get_bits(const ir_array_refcount_entry
&entry
)
55 * Wrapper to access private member "num_bits" of ir_array_refcount_entry
57 * The test class is a friend to ir_array_refcount_entry, but the
58 * individual tests are not part of the class. Since the friendliness of
59 * the test class does not extend to the tests, provide a wrapper.
61 unsigned get_num_bits(const ir_array_refcount_entry
&entry
)
63 return entry
.num_bits
;
67 * Wrapper to access private member "array_depth" of ir_array_refcount_entry
69 * The test class is a friend to ir_array_refcount_entry, but the
70 * individual tests are not part of the class. Since the friendliness of
71 * the test class does not extend to the tests, provide a wrapper.
73 unsigned get_array_depth(const ir_array_refcount_entry
&entry
)
75 return entry
.array_depth
;
80 array_refcount_test::SetUp()
82 mem_ctx
= ralloc_context(NULL
);
84 /* The type of vec4 x[3][4][5]; */
85 const glsl_type
*const array_5_of_vec4
=
86 glsl_type::get_array_instance(glsl_type::vec4_type
, 5);
87 const glsl_type
*const array_4_of_array_5_of_vec4
=
88 glsl_type::get_array_instance(array_5_of_vec4
, 4);
89 array_3_of_array_4_of_array_5_of_vec4
=
90 glsl_type::get_array_instance(array_4_of_array_5_of_vec4
, 3);
94 array_refcount_test::TearDown()
100 TEST_F(array_refcount_test
, ir_array_refcount_entry_initial_state_for_scalar
)
102 ir_variable
*const var
=
103 new(mem_ctx
) ir_variable(glsl_type::int_type
, "a", ir_var_auto
);
105 ir_array_refcount_entry
entry(var
);
107 ASSERT_NE((void *)0, get_bits(entry
));
108 EXPECT_FALSE(entry
.is_referenced
);
109 EXPECT_EQ(1, get_num_bits(entry
));
110 EXPECT_EQ(0, get_array_depth(entry
));
111 EXPECT_FALSE(entry
.is_linearized_index_referenced(0));
114 TEST_F(array_refcount_test
, ir_array_refcount_entry_initial_state_for_vector
)
116 ir_variable
*const var
=
117 new(mem_ctx
) ir_variable(glsl_type::vec4_type
, "a", ir_var_auto
);
119 ir_array_refcount_entry
entry(var
);
121 ASSERT_NE((void *)0, get_bits(entry
));
122 EXPECT_FALSE(entry
.is_referenced
);
123 EXPECT_EQ(1, get_num_bits(entry
));
124 EXPECT_EQ(0, get_array_depth(entry
));
125 EXPECT_FALSE(entry
.is_linearized_index_referenced(0));
128 TEST_F(array_refcount_test
, ir_array_refcount_entry_initial_state_for_matrix
)
130 ir_variable
*const var
=
131 new(mem_ctx
) ir_variable(glsl_type::mat4_type
, "a", ir_var_auto
);
133 ir_array_refcount_entry
entry(var
);
135 ASSERT_NE((void *)0, get_bits(entry
));
136 EXPECT_FALSE(entry
.is_referenced
);
137 EXPECT_EQ(1, get_num_bits(entry
));
138 EXPECT_EQ(0, get_array_depth(entry
));
139 EXPECT_FALSE(entry
.is_linearized_index_referenced(0));
142 TEST_F(array_refcount_test
, ir_array_refcount_entry_initial_state_for_array
)
144 ir_variable
*const var
=
145 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
148 const unsigned total_elements
= var
->type
->arrays_of_arrays_size();
150 ir_array_refcount_entry
entry(var
);
152 ASSERT_NE((void *)0, get_bits(entry
));
153 EXPECT_FALSE(entry
.is_referenced
);
154 EXPECT_EQ(total_elements
, get_num_bits(entry
));
155 EXPECT_EQ(3, get_array_depth(entry
));
157 for (unsigned i
= 0; i
< total_elements
; i
++)
158 EXPECT_FALSE(entry
.is_linearized_index_referenced(i
)) << "index = " << i
;
161 TEST_F(array_refcount_test
, mark_array_elements_referenced_simple
)
163 ir_variable
*const var
=
164 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
167 const unsigned total_elements
= var
->type
->arrays_of_arrays_size();
169 ir_array_refcount_entry
entry(var
);
171 static const array_deref_range dr
[] = {
172 { 0, 5 }, { 1, 4 }, { 2, 3 }
174 const unsigned accessed_element
= 0 + (1 * 5) + (2 * 4 * 5);
176 entry
.mark_array_elements_referenced(dr
, 3);
178 for (unsigned i
= 0; i
< total_elements
; i
++)
179 EXPECT_EQ(i
== accessed_element
, entry
.is_linearized_index_referenced(i
));
182 TEST_F(array_refcount_test
, mark_array_elements_referenced_whole_first_array
)
184 ir_variable
*const var
=
185 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
189 ir_array_refcount_entry
entry(var
);
191 static const array_deref_range dr
[] = {
192 { 0, 5 }, { 1, 4 }, { 3, 3 }
195 entry
.mark_array_elements_referenced(dr
, 3);
197 for (unsigned i
= 0; i
< 3; i
++) {
198 for (unsigned j
= 0; j
< 4; j
++) {
199 for (unsigned k
= 0; k
< 5; k
++) {
200 const bool accessed
= (j
== 1) && (k
== 0);
201 const unsigned linearized_index
= k
+ (j
* 5) + (i
* 4 * 5);
204 entry
.is_linearized_index_referenced(linearized_index
));
210 TEST_F(array_refcount_test
, mark_array_elements_referenced_whole_second_array
)
212 ir_variable
*const var
=
213 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
217 ir_array_refcount_entry
entry(var
);
219 static const array_deref_range dr
[] = {
220 { 0, 5 }, { 4, 4 }, { 1, 3 }
223 entry
.mark_array_elements_referenced(dr
, 3);
225 for (unsigned i
= 0; i
< 3; i
++) {
226 for (unsigned j
= 0; j
< 4; j
++) {
227 for (unsigned k
= 0; k
< 5; k
++) {
228 const bool accessed
= (i
== 1) && (k
== 0);
229 const unsigned linearized_index
= k
+ (j
* 5) + (i
* 4 * 5);
232 entry
.is_linearized_index_referenced(linearized_index
));
238 TEST_F(array_refcount_test
, mark_array_elements_referenced_whole_third_array
)
240 ir_variable
*const var
=
241 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
245 ir_array_refcount_entry
entry(var
);
247 static const array_deref_range dr
[] = {
248 { 5, 5 }, { 2, 4 }, { 1, 3 }
251 entry
.mark_array_elements_referenced(dr
, 3);
253 for (unsigned i
= 0; i
< 3; i
++) {
254 for (unsigned j
= 0; j
< 4; j
++) {
255 for (unsigned k
= 0; k
< 5; k
++) {
256 const bool accessed
= (i
== 1) && (j
== 2);
257 const unsigned linearized_index
= k
+ (j
* 5) + (i
* 4 * 5);
260 entry
.is_linearized_index_referenced(linearized_index
));
266 TEST_F(array_refcount_test
, mark_array_elements_referenced_whole_first_and_third_arrays
)
268 ir_variable
*const var
=
269 new(mem_ctx
) ir_variable(array_3_of_array_4_of_array_5_of_vec4
,
273 ir_array_refcount_entry
entry(var
);
275 static const array_deref_range dr
[] = {
276 { 5, 5 }, { 3, 4 }, { 3, 3 }
279 entry
.mark_array_elements_referenced(dr
, 3);
281 for (unsigned i
= 0; i
< 3; i
++) {
282 for (unsigned j
= 0; j
< 4; j
++) {
283 for (unsigned k
= 0; k
< 5; k
++) {
284 const bool accessed
= (j
== 3);
285 const unsigned linearized_index
= k
+ (j
* 5) + (i
* 4 * 5);
288 entry
.is_linearized_index_referenced(linearized_index
));