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 <gtest/gtest.h>
25
26 #include "nir.h"
27 #include "nir_builder.h"
28
29 namespace {
30
31 class nir_core_test : public ::testing::Test {
32 protected:
33 nir_core_test();
34 ~nir_core_test();
35
36 bool shader_contains_def(nir_ssa_def *def);
37
38 nir_builder *b, _b;
39 };
40
nir_core_test()41 nir_core_test::nir_core_test()
42 {
43 glsl_type_singleton_init_or_ref();
44
45 static const nir_shader_compiler_options options = { };
46 _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "builder test");
47 b = &_b;
48 }
49
~nir_core_test()50 nir_core_test::~nir_core_test()
51 {
52 if (HasFailure()) {
53 printf("\nShader from the failed test:\n\n");
54 nir_print_shader(b->shader, stdout);
55 }
56
57 ralloc_free(b->shader);
58
59 glsl_type_singleton_decref();
60 }
61
62 struct contains_def_state {
63 nir_ssa_def *def;
64 bool found;
65 };
66
67 static bool
contains_def_cb(nir_ssa_def * def,void * _state)68 contains_def_cb(nir_ssa_def *def, void *_state)
69 {
70 struct contains_def_state *state = (struct contains_def_state *)_state;
71 if (def == state->def)
72 state->found = true;
73
74 return true;
75 }
76
77 bool
shader_contains_def(nir_ssa_def * def)78 nir_core_test::shader_contains_def(nir_ssa_def *def)
79 {
80 nir_foreach_block(block, b->impl) {
81 nir_foreach_instr(instr, block) {
82 struct contains_def_state state = {
83 def, false
84 };
85 nir_foreach_ssa_def(instr, contains_def_cb, &state);
86 if (state.found)
87 return true;
88 }
89 }
90 return false;
91 }
92
TEST_F(nir_core_test,nir_instr_free_and_dce_test)93 TEST_F(nir_core_test, nir_instr_free_and_dce_test)
94 {
95 nir_ssa_def *zero = nir_imm_int(b, 0);
96 nir_ssa_def *one = nir_imm_int(b, 1);
97 nir_ssa_def *add01 = nir_iadd(b, zero, one);
98 nir_ssa_def *add11 = nir_iadd(b, one, one);
99
100 nir_cursor c = nir_instr_free_and_dce(add01->parent_instr);
101 ASSERT_FALSE(shader_contains_def(add01));
102 ASSERT_TRUE(shader_contains_def(add11));
103 ASSERT_FALSE(shader_contains_def(zero));
104 ASSERT_TRUE(shader_contains_def(one));
105
106 ASSERT_TRUE(nir_cursors_equal(c, nir_before_instr(add11->parent_instr)));
107
108 nir_validate_shader(b->shader, "after remove_and_dce");
109 }
110
TEST_F(nir_core_test,nir_instr_free_and_dce_all_test)111 TEST_F(nir_core_test, nir_instr_free_and_dce_all_test)
112 {
113 nir_ssa_def *one = nir_imm_int(b, 1);
114 nir_ssa_def *add = nir_iadd(b, one, one);
115
116 nir_cursor c = nir_instr_free_and_dce(add->parent_instr);
117 ASSERT_FALSE(shader_contains_def(add));
118 ASSERT_FALSE(shader_contains_def(one));
119
120 ASSERT_TRUE(nir_cursors_equal(c, nir_before_block(nir_start_block(b->impl))));
121
122 nir_validate_shader(b->shader, "after remove_and_dce");
123 }
124
TEST_F(nir_core_test,nir_instr_free_and_dce_multiple_src_test)125 TEST_F(nir_core_test, nir_instr_free_and_dce_multiple_src_test)
126 {
127 nir_ssa_def *one = nir_imm_int(b, 1);
128 nir_ssa_def *add = nir_iadd(b, one, one);
129
130 /* This risks triggering removing add multiple times, which can segfault in
131 * nir_instr_remove for instructions with srcs. */
132 nir_ssa_def *add2 = nir_iadd(b, add, add);
133
134 nir_cursor c = nir_instr_free_and_dce(add2->parent_instr);
135 ASSERT_FALSE(shader_contains_def(add2));
136 ASSERT_FALSE(shader_contains_def(add));
137 ASSERT_FALSE(shader_contains_def(one));
138
139 ASSERT_TRUE(nir_cursors_equal(c, nir_before_block(nir_start_block(b->impl))));
140
141 nir_validate_shader(b->shader, "after remove_and_dce");
142 }
143
144 }
145