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