1/*
2 * Copyright (C) 2021 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 "va_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   uint64_t _value = va_pack_instr(instr); \
32   if (_value != expected) { \
33      fprintf(stderr, "Got %" PRIx64 ", expected %" PRIx64 "\n", _value, (uint64_t) expected); \
34      bi_print_instr(instr, stderr); \
35      fprintf(stderr, "\n"); \
36      ADD_FAILURE(); \
37   } \
38} while(0)
39
40class ValhallPacking : public testing::Test {
41protected:
42   ValhallPacking() {
43      mem_ctx = ralloc_context(NULL);
44      b = bit_builder(mem_ctx);
45
46      zero = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 0), false);
47      one = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 8), false);
48      n4567 = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 4), true);
49   }
50
51   ~ValhallPacking() {
52      ralloc_free(mem_ctx);
53   }
54
55   void *mem_ctx;
56   bi_builder *b;
57   bi_index zero, one, n4567;
58};
59
60TEST_F(ValhallPacking, Moves) {
61   CASE(bi_mov_i32_to(b, bi_register(1), bi_register(2)),
62         0x0091c10000000002ULL);
63   CASE(bi_mov_i32_to(b, bi_register(1), bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 5), false)),
64         0x0091c1000000008aULL);
65}
66
67TEST_F(ValhallPacking, Fadd) {
68   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1), bi_register(2)),
69         0x00a4c00000000201ULL);
70   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1), bi_abs(bi_register(2))),
71         0x00a4c02000000201ULL);
72   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1), bi_neg(bi_register(2))),
73         0x00a4c01000000201ULL);
74
75   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_swz_16(bi_register(1), false, false),
76                         bi_swz_16(bi_register(0), true, true)),
77         0x00a5c0000c000001ULL);
78
79   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(1), bi_register(0)),
80         0x00a5c00028000001ULL);
81
82   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(1),
83                         bi_swz_16(bi_register(0), true, false)),
84         0x00a5c00024000001ULL);
85
86   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_discard(bi_abs(bi_register(0))),
87                         bi_neg(zero)),
88         0x00a5c0902800c040ULL);
89
90   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1),
91                       zero),
92         0x00a4c0000000c001ULL);
93
94   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1),
95                       bi_neg(zero)),
96         0x00a4c0100000c001ULL);
97
98   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1),
99                       bi_half(bi_register(0), true)),
100         0x00a4c00008000001ULL);
101
102   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(1),
103                       bi_half(bi_register(0), false)),
104         0x00a4c00004000001ULL);
105}
106
107TEST_F(ValhallPacking, Clper) {
108   CASE(bi_clper_i32_to(b, bi_register(0), bi_register(0), bi_byte(n4567, 0),
109                        BI_INACTIVE_RESULT_F1, BI_LANE_OP_NONE, BI_SUBGROUP_SUBGROUP16),
110         0x00a0c030128fc900);
111}
112
113TEST_F(ValhallPacking, Clamps) {
114   bi_instr *I = bi_fadd_f32_to(b, bi_register(0), bi_register(1),
115                                bi_neg(bi_abs(bi_register(2))));
116   CASE(I, 0x00a4c03000000201ULL);
117
118   I->clamp = BI_CLAMP_CLAMP_M1_1;
119   CASE(I, 0x00a4c03200000201ULL);
120}
121
122TEST_F(ValhallPacking, Misc) {
123   CASE(bi_fma_f32_to(b, bi_register(1), bi_discard(bi_register(1)),
124                         bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 4), false),
125                         bi_neg(zero)),
126         0x00b2c10400c08841ULL);
127
128   CASE(bi_fround_f32_to(b, bi_register(2), bi_discard(bi_neg(bi_register(2))),
129                         BI_ROUND_RTN),
130         0x0090c240800d0042ULL);
131
132   CASE(bi_fround_v2f16_to(b, bi_half(bi_register(0), false), bi_register(0),
133                         BI_ROUND_RTN),
134         0x00904000a00f0000ULL);
135
136   CASE(bi_fround_v2f16_to(b, bi_half(bi_register(0), false),
137                           bi_swz_16(bi_register(1), true, false), BI_ROUND_RTN),
138         0x00904000900f0001ULL);
139}
140
141TEST_F(ValhallPacking, FaddImm) {
142   CASE(bi_fadd_imm_f32_to(b, bi_register(2), bi_discard(bi_register(2)), 0x4847C6C0),
143         0x0114C24847C6C042ULL);
144
145   CASE(bi_fadd_imm_v2f16_to(b, bi_register(2), bi_discard(bi_register(2)), 0x70AC6784),
146         0x0115C270AC678442ULL);
147}
148
149TEST_F(ValhallPacking, Comparions) {
150   bi_instr *I =
151      bi_icmp_v2s16_to(b, bi_register(2),
152            bi_discard(bi_swz_16(bi_register(3), true, false)),
153            bi_discard(bi_swz_16(bi_register(2), true, false)),
154            BI_CMPF_GT,
155            BI_RESULT_TYPE_M1);
156   I->src[2] = zero; // TODO: model in the IR
157
158   CASE(I, 0x00f9c21184c04243);
159
160   I->op = BI_OPCODE_FCMP_V2F16;
161   I->src[1] = bi_discard(bi_swz_16(bi_register(2), false, false));
162   CASE(I, 0x00f5c20190c04243);
163}
164
165TEST_F(ValhallPacking, Conversions) {
166   CASE(bi_v2s16_to_v2f16_to(b, bi_register(2), bi_discard(bi_register(2))),
167         0x0090c22000070042);
168}
169
170TEST_F(ValhallPacking, BranchzI16) {
171   bi_instr *I = bi_branchz_i16(b, bi_half(bi_register(2), false), bi_null(), BI_CMPF_EQ);
172   I->branch_offset = 1;
173   CASE(I, 0x001fc03000000102);
174}
175
176TEST_F(ValhallPacking, BranchzI16Backwards) {
177   bi_instr *I = bi_branchz_i16(b, zero, bi_null(), BI_CMPF_EQ);
178   I->branch_offset = -8;
179   CASE(I, 0x001fc017fffff8c0);
180}
181
182TEST_F(ValhallPacking, Blend) {
183   CASE(bi_blend_to(b, bi_null(), bi_register(0), bi_register(60),
184                       bi_fau(BIR_FAU_BLEND_0, false),
185                       bi_fau(BIR_FAU_BLEND_0, true),
186                       bi_null(), BI_REGISTER_FORMAT_F16, 2, 0),
187        0x007f4004333c00f0);
188}
189
190TEST_F(ValhallPacking, Mux) {
191   CASE(bi_mux_i32_to(b, bi_register(0), bi_discard(bi_register(0)),
192                      bi_discard(bi_register(4)),
193                      bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 0), false), BI_MUX_BIT),
194        0x00b8c00300804440ull);
195}
196
197TEST_F(ValhallPacking, AtestFP16) {
198   bi_instr *I = bi_atest_to(b, bi_register(60), bi_register(60),
199         bi_half(bi_register(1), true));
200   I->src[2] = bi_fau(BIR_FAU_ATEST_PARAM, false);
201
202   CASE(I, 0x007dbc0208ea013c);
203}
204
205TEST_F(ValhallPacking, AtestFP32) {
206   bi_instr *I = bi_atest_to(b, bi_register(60), bi_register(60), one);
207   I->src[2] = bi_fau(BIR_FAU_ATEST_PARAM, false);
208   CASE(I, 0x007dbc0200ead03c);
209}
210
211TEST_F(ValhallPacking, Transcendentals) {
212   CASE(bi_frexpm_f32_to(b, bi_register(1), bi_register(0), false, true),
213        0x0099c10001000000);
214
215   CASE(bi_frexpe_f32_to(b, bi_register(0), bi_discard(bi_register(0)), false, true),
216        0x0099c00001020040);
217
218   CASE(bi_frsq_f32_to(b, bi_register(2), bi_register(1)),
219        0x009cc20000020001);
220
221   CASE(bi_fma_rscale_f32_to(b, bi_register(0), bi_discard(bi_register(1)), bi_discard(bi_register(2)), bi_neg(zero), bi_discard(bi_register(0)), BI_SPECIAL_LEFT),
222        0x0162c00440c04241);
223}
224
225TEST_F(ValhallPacking, Csel) {
226   CASE(bi_csel_u32_to(b, bi_register(1), bi_discard(bi_register(2)),
227                       bi_discard(bi_register(3)),
228                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), false),
229                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), true),
230                       BI_CMPF_EQ),
231        0x0150c10085844342);
232
233   CASE(bi_csel_u32_to(b, bi_register(1), bi_discard(bi_register(2)),
234                       bi_discard(bi_register(3)),
235                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), false),
236                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), true),
237                       BI_CMPF_LT),
238        0x0150c10485844342);
239
240   CASE(bi_csel_s32_to(b, bi_register(1), bi_discard(bi_register(2)),
241                       bi_discard(bi_register(3)),
242                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), false),
243                       bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 2), true),
244                       BI_CMPF_LT),
245        0x0158c10485844342);
246}
247
248TEST_F(ValhallPacking, LdAttrImm) {
249   bi_instr *I = bi_ld_attr_imm_to(b, bi_register(0),
250                                   bi_discard(bi_register(60)),
251                                   bi_discard(bi_register(61)),
252                                   BI_REGISTER_FORMAT_F16, BI_VECSIZE_V4, 1);
253   I->table = 1;
254
255   CASE(I, 0x0066800433117d7c);
256}
257
258TEST_F(ValhallPacking, LdVarBufImmF16) {
259   CASE(bi_ld_var_buf_imm_f16_to(b, bi_register(2), bi_register(61),
260                                 BI_REGISTER_FORMAT_F16, BI_SAMPLE_CENTER,
261                                 BI_SOURCE_FORMAT_F16,
262                                 BI_UPDATE_RETRIEVE, BI_VECSIZE_V4, 0),
263        0x005d82143300003d);
264
265   CASE(bi_ld_var_buf_imm_f16_to(b, bi_register(0), bi_register(61),
266                                 BI_REGISTER_FORMAT_F16, BI_SAMPLE_SAMPLE,
267                                 BI_SOURCE_FORMAT_F16,
268                                 BI_UPDATE_STORE, BI_VECSIZE_V4, 0),
269         0x005d80843300003d);
270
271   CASE(bi_ld_var_buf_imm_f16_to(b, bi_register(0), bi_register(61),
272                                 BI_REGISTER_FORMAT_F16, BI_SAMPLE_CENTROID,
273                                 BI_SOURCE_FORMAT_F16,
274                                 BI_UPDATE_STORE, BI_VECSIZE_V4, 8),
275         0x005d80443308003d);
276}
277
278TEST_F(ValhallPacking, LeaBufImm) {
279   CASE(bi_lea_buf_imm_to(b, bi_register(4), bi_discard(bi_register(59))),
280        0x005e840400000d7b);
281}
282
283TEST_F(ValhallPacking, StoreSegment) {
284   CASE(bi_store_i96(b, bi_register(0), bi_discard(bi_register(4)),
285                        bi_discard(bi_register(5)), BI_SEG_VARY, 0),
286        0x0061400632000044);
287}
288
289TEST_F(ValhallPacking, Convert16To32) {
290   CASE(bi_u16_to_u32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), false, false))),
291         0x0090c20000140077);
292
293   CASE(bi_u16_to_u32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), true, false))),
294         0x0090c20010140077);
295
296   CASE(bi_u16_to_f32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), false, false))),
297         0x0090c20000150077);
298
299   CASE(bi_u16_to_f32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), true, false))),
300         0x0090c20010150077);
301
302   CASE(bi_s16_to_s32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), false, false))),
303         0x0090c20000040077);
304
305   CASE(bi_s16_to_s32_to(b, bi_register(2), bi_discard(bi_swz_16(bi_register(55), true, false))),
306         0x0090c20010040077);
307}
308
309TEST_F(ValhallPacking, Swizzle8) {
310   bi_instr *I = bi_icmp_v4u8_to(b, bi_register(1), bi_byte(bi_register(0), 0),
311                                 zero, BI_CMPF_NE, BI_RESULT_TYPE_I1);
312   I->src[2] = zero; // TODO: model in the IR
313
314   CASE(I, 0x00f2c14300c0c000);
315}
316
317TEST_F(ValhallPacking, FauPage1) {
318   CASE(bi_mov_i32_to(b, bi_register(1), bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 32), false)),
319         0x0291c10000000080ULL);
320}
321
322TEST_F(ValhallPacking, LdTileV3F16) {
323   CASE(bi_ld_tile_to(b, bi_register(4), bi_discard(bi_register(0)),
324                         bi_register(60), bi_register(3),
325                         BI_REGISTER_FORMAT_F16, BI_VECSIZE_V3),
326        0x0078840423033c40);
327}
328