1 /*
2 * Copyright (C) 2022 Collabora, Ltd.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25 #include "bi_test.h"
26 #include "bi_builder.h"
27
28 #include <gtest/gtest.h>
29
30 #define CASE(instr, expected) do { \
31 bi_builder *A = bit_builder(mem_ctx); \
32 bi_builder *B = bit_builder(mem_ctx); \
33 A->shader->info.bifrost = rzalloc(mem_ctx, struct bifrost_shader_info); \
34 B->shader->info.bifrost = rzalloc(mem_ctx, struct bifrost_shader_info); \
35 { \
36 bi_builder *b = A; \
37 bi_index u = bi_temp(b->shader); \
38 UNUSED bi_index v = bi_temp(b->shader); \
39 UNUSED bi_index w = bi_temp(b->shader); \
40 instr; \
41 } \
42 { \
43 bi_builder *b = B; \
44 bi_index u = bi_temp(b->shader); \
45 UNUSED bi_index v = bi_temp(b->shader); \
46 UNUSED bi_index w = bi_temp(b->shader); \
47 expected; \
48 } \
49 bi_opt_message_preload(A->shader); \
50 if (!bit_shader_equal(A->shader, B->shader)) { \
51 ADD_FAILURE(); \
52 fprintf(stderr, "Optimization produce unexpected result"); \
53 fprintf(stderr, " Actual:\n"); \
54 bi_print_shader(A->shader, stderr); \
55 fprintf(stderr, "Expected:\n"); \
56 bi_print_shader(B->shader, stderr); \
57 fprintf(stderr, "\n"); \
58 } \
59 } while(0)
60
61 #define NEGCASE(instr) CASE(instr, instr)
62
63 class MessagePreload : public testing::Test {
64 protected:
MessagePreload()65 MessagePreload() {
66 mem_ctx = ralloc_context(NULL);
67
68 x = bi_register(16);
69 y = bi_register(32);
70
71 }
72
~MessagePreload()73 ~MessagePreload() {
74 ralloc_free(mem_ctx);
75 }
76
77 void *mem_ctx;
78
79 bi_index reg, x, y;
80
preload_moves(bi_builder * b,bi_index dest,int count,int idx)81 static void preload_moves(bi_builder *b, bi_index dest, int count, int idx)
82 {
83 bi_instr *I = bi_collect_i32_to(b, dest);
84 I->nr_srcs = count;
85
86 b->cursor = bi_before_block(bi_start_block(&b->shader->blocks));
87 for (int i = 0; i < count; ++i)
88 I->src[i] = bi_mov_i32(b, bi_register(idx*4 + i));
89
90 b->cursor = bi_after_instr(I);
91 }
92 };
93
94
TEST_F(MessagePreload,PreloadLdVarSample)95 TEST_F(MessagePreload, PreloadLdVarSample)
96 {
97 CASE({
98 bi_ld_var_imm_to(b, u, bi_register(61), BI_REGISTER_FORMAT_F32,
99 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 0);
100 }, {
101 preload_moves(b, u, 4, 0);
102 });
103 }
104
TEST_F(MessagePreload,PreloadLdVarLdVar)105 TEST_F(MessagePreload, PreloadLdVarLdVar)
106 {
107 CASE({
108 bi_ld_var_imm_to(b, u, bi_register(61), BI_REGISTER_FORMAT_F32,
109 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 2);
110 bi_ld_var_imm_to(b, v, bi_register(61), BI_REGISTER_FORMAT_F32,
111 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 1);
112 }, {
113 preload_moves(b, u, 4, 0);
114 preload_moves(b, v, 4, 1);
115 });
116 }
117
TEST_F(MessagePreload,MaxTwoMessages)118 TEST_F(MessagePreload, MaxTwoMessages)
119 {
120 CASE({
121 bi_ld_var_imm_to(b, u, bi_register(61), BI_REGISTER_FORMAT_F32,
122 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 2);
123 bi_ld_var_imm_to(b, v, bi_register(61), BI_REGISTER_FORMAT_F32,
124 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 1);
125 bi_ld_var_imm_to(b, w, bi_register(61), BI_REGISTER_FORMAT_F32,
126 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 0);
127 },
128 {
129 preload_moves(b, u, 4, 0);
130 preload_moves(b, v, 4, 1);
131 bi_ld_var_imm_to(b, w, bi_register(61), BI_REGISTER_FORMAT_F32,
132 BI_SAMPLE_SAMPLE, BI_UPDATE_STORE, BI_VECSIZE_V4, 0);
133 });
134
135 CASE({
136 bi_var_tex_f32_to(b, u, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
137 bi_var_tex_f16_to(b, v, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 1, 2);
138 bi_var_tex_f16_to(b, w, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 3, 3);
139 }, {
140 preload_moves(b, u, 4, 0);
141 preload_moves(b, v, 2, 1);
142 bi_var_tex_f16_to(b, w, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 3, 3);
143 });
144 }
145
TEST_F(MessagePreload,PreloadVartexF16)146 TEST_F(MessagePreload, PreloadVartexF16)
147 {
148 CASE({
149 bi_var_tex_f16_to(b, u, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
150 }, {
151 preload_moves(b, u, 2, 0);
152 });
153 }
154
TEST_F(MessagePreload,PreloadVartexF32)155 TEST_F(MessagePreload, PreloadVartexF32)
156 {
157 CASE({
158 bi_var_tex_f32_to(b, u, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
159 }, {
160 preload_moves(b, u, 4, 0);
161 });
162 }
163
TEST_F(MessagePreload,PreloadVartexF32VartexF16)164 TEST_F(MessagePreload, PreloadVartexF32VartexF16)
165 {
166 CASE({
167 bi_var_tex_f32_to(b, u, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
168 bi_var_tex_f16_to(b, v, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 1, 2);
169 }, {
170 preload_moves(b, u, 4, 0);
171 preload_moves(b, v, 2, 1);
172 });
173 }
174
TEST_F(MessagePreload,PreloadVartexLodModes)175 TEST_F(MessagePreload, PreloadVartexLodModes)
176 {
177 CASE({
178 bi_var_tex_f32_to(b, u, true, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
179 bi_var_tex_f32_to(b, v, false, BI_SAMPLE_CENTER, BI_UPDATE_STORE, 0, 0);
180 }, {
181 preload_moves(b, u, 4, 0);
182 preload_moves(b, v, 4, 1);
183 });
184 }
185