1 /*
2 * Copyright (c) 2020 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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 * Authors:
24 * Christian Gmeiner <[email protected]>
25 */
26
27 #include <gtest/gtest.h>
28 #include "nir.h"
29 #include "nir_builder.h"
30
31 class nir_opt_peephole_select_test : public ::testing::Test {
32 protected:
33 nir_opt_peephole_select_test();
34 ~nir_opt_peephole_select_test();
35
36 nir_builder bld;
37 };
38
nir_opt_peephole_select_test()39 nir_opt_peephole_select_test::nir_opt_peephole_select_test()
40 {
41 glsl_type_singleton_init_or_ref();
42
43 static const nir_shader_compiler_options options = { };
44 bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "peephole test");
45 }
46
~nir_opt_peephole_select_test()47 nir_opt_peephole_select_test::~nir_opt_peephole_select_test()
48 {
49 ralloc_free(bld.shader);
50 glsl_type_singleton_decref();
51 }
52
TEST_F(nir_opt_peephole_select_test,opt_load_ubo_no_speculate)53 TEST_F(nir_opt_peephole_select_test, opt_load_ubo_no_speculate)
54 {
55 /* Tests that opt_peephole_select does not optimize ubo loads:
56 *
57 * impl main {
58 * block b0: // preds:
59 * 32 %0 = load_const (0x00000001)
60 * 32 %1 = load_const (0x00000002)
61 * 32 %2 = load_const (0x0000000a = 10)
62 * 1 %3 = ieq %0 (0x1), %1 (0x2)
63 * // succs: b1 b2
64 * if %3 {
65 * block b1: // preds: b0
66 * 32 %4 = @load_ubo (%0 (0x1), %2 (0xa)) (access=0, align_mul=16, align_offset=0, range_base=16, range=16)
67 * // succs: b3
68 * } else {
69 * block b2: // preds: b0
70 * 32 %5 = @load_ubo (%0 (0x1), %2 (0xa)) (access=0, align_mul=16, align_offset=0, range_base=16, range=16)
71 * // succs: b3
72 * }
73 * block b3: // preds: b1 b2, succs: b4
74 * block b4:
75 * }
76 */
77 nir_function *main = nir_shader_get_function_for_name(bld.shader, "main");
78
79 nir_def *one = nir_imm_int(&bld, 1);
80 nir_def *two = nir_imm_int(&bld, 2);
81 nir_def *ten = nir_imm_int(&bld, 10);
82
83 nir_def *cmp_result = nir_ieq(&bld, one, two);
84 nir_push_if(&bld, cmp_result);
85
86 nir_load_ubo(&bld, 1, 32, one, ten, (gl_access_qualifier)0, 16, 0, 16, 16);
87
88 nir_push_else(&bld, NULL);
89
90 nir_load_ubo(&bld, 1, 32, one, ten, (gl_access_qualifier)0, 16, 0, 16, 16);
91
92 nir_pop_if(&bld, NULL);
93
94 nir_index_blocks(main->impl);
95 EXPECT_EQ(main->impl->num_blocks, 4);
96
97 ASSERT_FALSE(nir_opt_peephole_select(bld.shader, 16, true, true));
98 nir_validate_shader(bld.shader, NULL);
99
100 nir_index_blocks(main->impl);
101 EXPECT_EQ(main->impl->num_blocks, 4);
102 }
103
TEST_F(nir_opt_peephole_select_test,opt_load_ubo_speculate)104 TEST_F(nir_opt_peephole_select_test, opt_load_ubo_speculate)
105 {
106 /* Tests that opt_peephole_select correctly optimizes speculative ubo loads:
107 *
108 * impl main {
109 * block b0: // preds:
110 * 32 %0 = load_const (0x00000001)
111 * 32 %1 = load_const (0x00000002)
112 * 32 %2 = load_const (0x0000000a = 10)
113 * 1 %3 = ieq %0 (0x1), %1 (0x2)
114 * // succs: b1 b2
115 * if %3 {
116 * block b1: // preds: b0
117 * 32 %4 = @load_ubo (%0 (0x1), %2 (0xa)) (access=4096, align_mul=16, align_offset=0, range_base=16, range=16)
118 * // succs: b3
119 * } else {
120 * block b2: // preds: b0
121 * 32 %5 = @load_ubo (%0 (0x1), %2 (0xa)) (access=4096, align_mul=16, align_offset=0, range_base=16, range=16)
122 * // succs: b3
123 * }
124 * block b3: // preds: b1 b2, succs: b4
125 * block b4:
126 *}
127 */
128 nir_function *main = nir_shader_get_function_for_name(bld.shader, "main");
129
130 nir_def *one = nir_imm_int(&bld, 1);
131 nir_def *two = nir_imm_int(&bld, 2);
132 nir_def *ten = nir_imm_int(&bld, 10);
133
134 nir_def *cmp_result = nir_ieq(&bld, one, two);
135 nir_push_if(&bld, cmp_result);
136
137 nir_load_ubo(&bld, 1, 32, one, ten, (gl_access_qualifier)ACCESS_CAN_SPECULATE, 16, 0, 16, 16);
138
139 nir_push_else(&bld, NULL);
140
141 nir_load_ubo(&bld, 1, 32, one, ten, (gl_access_qualifier)ACCESS_CAN_SPECULATE, 16, 0, 16, 16);
142
143 nir_pop_if(&bld, NULL);
144
145 nir_index_blocks(main->impl);
146 EXPECT_EQ(main->impl->num_blocks, 4);
147
148 ASSERT_TRUE(nir_opt_peephole_select(bld.shader, 16, true, true));
149 nir_validate_shader(bld.shader, NULL);
150
151 nir_index_blocks(main->impl);
152 EXPECT_EQ(main->impl->num_blocks, 1);
153 }
154