1 /*
2 * Copyright © 2018 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include "nir_test.h"
25
26 namespace {
27
28 class nir_core_test : public nir_test {
29 protected:
nir_core_test()30 nir_core_test()
31 : nir_test::nir_test("nir_core_test")
32 {
33 }
34
35 bool shader_contains_def(nir_def *def);
36 };
37
38 struct contains_def_state {
39 nir_def *def;
40 bool found;
41 };
42
43 static bool
contains_def_cb(nir_def * def,void * _state)44 contains_def_cb(nir_def *def, void *_state)
45 {
46 struct contains_def_state *state = (struct contains_def_state *)_state;
47 if (def == state->def)
48 state->found = true;
49
50 return true;
51 }
52
53 bool
shader_contains_def(nir_def * def)54 nir_core_test::shader_contains_def(nir_def *def)
55 {
56 nir_foreach_block(block, b->impl) {
57 nir_foreach_instr(instr, block) {
58 struct contains_def_state state = {
59 def, false
60 };
61 nir_foreach_def(instr, contains_def_cb, &state);
62 if (state.found)
63 return true;
64 }
65 }
66 return false;
67 }
68
TEST_F(nir_core_test,nir_instr_free_and_dce_test)69 TEST_F(nir_core_test, nir_instr_free_and_dce_test)
70 {
71 nir_def *zero = nir_imm_int(b, 0);
72 nir_def *one = nir_imm_int(b, 1);
73 nir_def *add01 = nir_iadd(b, zero, one);
74 nir_def *add11 = nir_iadd(b, one, one);
75
76 nir_cursor c = nir_instr_free_and_dce(add01->parent_instr);
77 ASSERT_FALSE(shader_contains_def(add01));
78 ASSERT_TRUE(shader_contains_def(add11));
79 ASSERT_FALSE(shader_contains_def(zero));
80 ASSERT_TRUE(shader_contains_def(one));
81
82 ASSERT_TRUE(nir_cursors_equal(c, nir_before_instr(add11->parent_instr)));
83
84 nir_validate_shader(b->shader, "after remove_and_dce");
85 }
86
TEST_F(nir_core_test,nir_instr_free_and_dce_all_test)87 TEST_F(nir_core_test, nir_instr_free_and_dce_all_test)
88 {
89 nir_def *one = nir_imm_int(b, 1);
90 nir_def *add = nir_iadd(b, one, one);
91
92 nir_cursor c = nir_instr_free_and_dce(add->parent_instr);
93 ASSERT_FALSE(shader_contains_def(add));
94 ASSERT_FALSE(shader_contains_def(one));
95
96 ASSERT_TRUE(nir_cursors_equal(c, nir_before_block(nir_start_block(b->impl))));
97
98 nir_validate_shader(b->shader, "after remove_and_dce");
99 }
100
TEST_F(nir_core_test,nir_instr_free_and_dce_multiple_src_test)101 TEST_F(nir_core_test, nir_instr_free_and_dce_multiple_src_test)
102 {
103 nir_def *one = nir_imm_int(b, 1);
104 nir_def *add = nir_iadd(b, one, one);
105
106 /* This risks triggering removing add multiple times, which can segfault in
107 * nir_instr_remove for instructions with srcs. */
108 nir_def *add2 = nir_iadd(b, add, add);
109
110 nir_cursor c = nir_instr_free_and_dce(add2->parent_instr);
111 ASSERT_FALSE(shader_contains_def(add2));
112 ASSERT_FALSE(shader_contains_def(add));
113 ASSERT_FALSE(shader_contains_def(one));
114
115 ASSERT_TRUE(nir_cursors_equal(c, nir_before_block(nir_start_block(b->impl))));
116
117 nir_validate_shader(b->shader, "after remove_and_dce");
118 }
119
120 }
121