2 * Copyright (C) 2020 Collabora Ltd.
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:
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
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
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 #include "util/half_float.h"
30 #include "bifrost/disassemble.h"
32 /* Instruction packing tests */
35 bit_test_single(struct panfrost_device
*dev
,
38 bool fma
, enum bit_debug debug
)
40 /* First, simulate the instruction */
41 struct bit_state s
= { 0 };
42 memcpy(s
.r
, input
, 16);
43 bit_step(&s
, ins
, fma
);
45 /* Next, wrap it up and pack it */
47 bi_instruction ldubo
= {
48 .type
= BI_LOAD_UNIFORM
,
57 .dest
= BIR_INDEX_REGISTER
| 0,
58 .dest_type
= nir_type_uint32
,
62 bi_instruction ldva
= {
63 .type
= BI_LOAD_VAR_ADDRESS
,
64 .writemask
= (1 << 12) - 1,
65 .dest
= BIR_INDEX_REGISTER
| 32,
66 .dest_type
= nir_type_uint32
,
69 BIR_INDEX_REGISTER
| 61,
70 BIR_INDEX_REGISTER
| 62,
84 BIR_INDEX_REGISTER
| 0,
85 ldva
.dest
, ldva
.dest
+ 1, ldva
.dest
+ 2,
89 nir_type_uint32
, nir_type_uint32
, nir_type_uint32
,
94 bi_context
*ctx
= rzalloc(NULL
, bi_context
);
95 ctx
->stage
= MESA_SHADER_VERTEX
;
97 bi_block
*blk
= rzalloc(ctx
, bi_block
);
98 blk
->scheduled
= true;
100 blk
->base
.predecessors
= _mesa_set_create(blk
,
102 _mesa_key_pointer_equal
);
104 list_inithead(&ctx
->blocks
);
105 list_addtail(&blk
->base
.link
, &ctx
->blocks
);
106 list_inithead(&blk
->clauses
);
108 bi_clause
*clauses
[4] = {
109 rzalloc(ctx
, bi_clause
),
110 rzalloc(ctx
, bi_clause
),
111 rzalloc(ctx
, bi_clause
),
112 rzalloc(ctx
, bi_clause
)
115 for (unsigned i
= 0; i
< 4; ++i
) {
116 clauses
[i
]->bundle_count
= 1;
117 list_addtail(&clauses
[i
]->link
, &blk
->clauses
);
118 clauses
[i
]->scoreboard_id
= (i
& 1);
121 clauses
[i
]->dependencies
= 1 << (~i
& 1);
122 clauses
[i
]->data_register_write_barrier
= true;
126 clauses
[0]->bundles
[0].add
= &ldubo
;
127 clauses
[0]->clause_type
= BIFROST_CLAUSE_UBO
;
130 clauses
[1]->bundles
[0].fma
= ins
;
132 clauses
[1]->bundles
[0].add
= ins
;
134 clauses
[0]->constant_count
= 1;
135 clauses
[1]->constant_count
= 1;
136 clauses
[1]->constants
[0] = ins
->constant
.u64
;
138 clauses
[2]->bundles
[0].add
= &ldva
;
139 clauses
[3]->bundles
[0].add
= &st
;
141 clauses
[2]->clause_type
= BIFROST_CLAUSE_UBO
;
142 clauses
[3]->clause_type
= BIFROST_CLAUSE_SSBO_STORE
;
144 panfrost_program prog
;
145 bi_pack(ctx
, &prog
.compiled
);
147 bool succ
= bit_vertex(dev
, prog
, input
, 16, NULL
, 0,
150 if (debug
>= BIT_DEBUG_ALL
|| (!succ
&& debug
>= BIT_DEBUG_FAIL
)) {
151 bi_print_shader(ctx
, stderr
);
152 disassemble_bifrost(stderr
, prog
.compiled
.data
, prog
.compiled
.size
, true);
158 /* Utilities for generating tests */
161 bit_generate_vector(uint32_t *mem
)
163 for (unsigned i
= 0; i
< 4; ++i
)
167 static bi_instruction
168 bit_ins(enum bi_class C
, unsigned argc
, nir_alu_type base
, unsigned size
)
170 nir_alu_type T
= base
| size
;
172 bi_instruction ins
= {
174 .dest
= BIR_INDEX_REGISTER
| 0,
178 for (unsigned i
= 0; i
< argc
; ++i
) {
179 ins
.src
[i
] = BIR_INDEX_REGISTER
| i
;
180 ins
.src_types
[i
] = T
;
186 /* Tests all 64 combinations of floating point modifiers for a given
187 * instruction / floating-type / test type */
190 bit_fmod_helper(struct panfrost_device
*dev
,
191 enum bi_class c
, unsigned size
, bool fma
,
192 uint32_t *input
, enum bit_debug debug
)
194 bi_instruction ins
= bit_ins(c
, 2, nir_type_float
, size
);
196 for (unsigned outmod
= 0; outmod
< 4; ++outmod
) {
197 for (unsigned inmod
= 0; inmod
< 16; ++inmod
) {
199 ins
.src_abs
[0] = (inmod
& 0x1);
200 ins
.src_abs
[1] = (inmod
& 0x2);
201 ins
.src_neg
[0] = (inmod
& 0x4);
202 ins
.src_neg
[1] = (inmod
& 0x8);
204 /* Skip over tests that cannot run on FMA */
205 if (fma
&& (size
== 16) && ins
.src_abs
[0] && ins
.src_abs
[1])
208 if (!bit_test_single(dev
, &ins
, input
, fma
, debug
)) {
209 fprintf(stderr
, "FAIL: fmod.%s%u.%s%s.%u\n",
213 outmod
? bi_output_mod_name(outmod
) : ".none",
221 bit_fma_helper(struct panfrost_device
*dev
,
222 unsigned size
, uint32_t *input
, enum bit_debug debug
)
224 bi_instruction ins
= bit_ins(BI_FMA
, 3, nir_type_float
, size
);
226 for (unsigned outmod
= 0; outmod
< 4; ++outmod
) {
227 for (unsigned inmod
= 0; inmod
< 8; ++inmod
) {
229 ins
.src_neg
[0] = (inmod
& 0x1);
230 ins
.src_neg
[1] = (inmod
& 0x2);
231 ins
.src_neg
[2] = (inmod
& 0x4);
233 if (!bit_test_single(dev
, &ins
, input
, true, debug
)) {
234 fprintf(stderr
, "FAIL: fma%u%s.%u\n",
236 outmod
? bi_output_mod_name(outmod
) : ".none",
244 bit_csel_helper(struct panfrost_device
*dev
,
245 unsigned size
, uint32_t *input
, enum bit_debug debug
)
247 bi_instruction ins
= bit_ins(BI_CSEL
, 4, nir_type_uint
, size
);
249 /* SCHEDULER: We can only read 3 registers at once. */
250 ins
.src
[2] = ins
.src
[0];
252 for (enum bi_cond cond
= BI_COND_LT
; cond
<= BI_COND_NE
; ++cond
) {
253 ins
.csel_cond
= cond
;
255 if (!bit_test_single(dev
, &ins
, input
, true, debug
)) {
256 fprintf(stderr
, "FAIL: csel%u.%s\n",
257 size
, bi_cond_name(cond
));
263 bit_special_helper(struct panfrost_device
*dev
,
264 unsigned size
, uint32_t *input
, enum bit_debug debug
)
266 bi_instruction ins
= bit_ins(BI_SPECIAL
, 1, nir_type_float
, size
);
268 for (enum bi_special_op op
= BI_SPECIAL_FRCP
; op
<= BI_SPECIAL_FRSQ
; ++op
) {
269 for (unsigned c
= 0; c
< ((size
== 16) ? 2 : 1); ++c
) {
271 ins
.swizzle
[0][0] = c
;
273 if (!bit_test_single(dev
, &ins
, input
, false, debug
)) {
274 fprintf(stderr
, "FAIL: special%u.%s\n",
275 size
, bi_special_op_name(op
));
282 bit_fmod(struct panfrost_device
*dev
, enum bit_debug debug
)
284 float input32
[4] = { 0.8, 1.7, 0.0, 0.0 };
286 uint32_t input16
[4] = {
287 _mesa_float_to_half(input32
[0]) | (_mesa_float_to_half(-1.2) << 16),
288 _mesa_float_to_half(input32
[1]) | (_mesa_float_to_half(0.9) << 16),
292 for (unsigned sz
= 16; sz
<= 32; sz
*= 2) {
294 (sz
== 16) ? input16
:
295 (uint32_t *) input32
;
297 bit_fmod_helper(dev
, BI_ADD
, sz
, true, input
, debug
);
302 bit_fma(struct panfrost_device
*dev
, enum bit_debug debug
)
304 float input32
[4] = { 0.2, 1.6, -3.5, 0.0 };
306 uint32_t input16
[4] = {
307 _mesa_float_to_half(input32
[0]) | (_mesa_float_to_half(-1.8) << 16),
308 _mesa_float_to_half(input32
[1]) | (_mesa_float_to_half(0.6) << 16),
309 _mesa_float_to_half(input32
[1]) | (_mesa_float_to_half(16.2) << 16),
313 for (unsigned sz
= 16; sz
<= 32; sz
*= 2) {
315 (sz
== 16) ? input16
:
316 (uint32_t *) input32
;
318 bit_fma_helper(dev
, sz
, input
, debug
);
323 bit_csel(struct panfrost_device
*dev
, enum bit_debug debug
)
325 float input32
[4] = { 0.2, 1.6, -3.5, 3.0 };
327 uint32_t input16
[4] = {
328 _mesa_float_to_half(input32
[0]) | (_mesa_float_to_half(-1.8) << 16),
329 _mesa_float_to_half(input32
[1]) | (_mesa_float_to_half(0.6) << 16),
330 _mesa_float_to_half(input32
[1]) | (_mesa_float_to_half(16.2) << 16),
331 _mesa_float_to_half(input32
[2]) | (_mesa_float_to_half(4.9) << 16),
334 for (unsigned sz
= 32; sz
<= 32; sz
*= 2) {
336 (sz
== 16) ? input16
:
337 (uint32_t *) input32
;
339 bit_csel_helper(dev
, sz
, input
, debug
);
344 bit_special(struct panfrost_device
*dev
, enum bit_debug debug
)
346 float input32
[4] = { 0.9 };
347 uint32_t input16
[4] = { _mesa_float_to_half(input32
[0]) | (_mesa_float_to_half(0.2) << 16) };
349 for (unsigned sz
= 16; sz
<= 32; sz
*= 2) {
351 (sz
== 16) ? input16
:
352 (uint32_t *) input32
;
354 bit_special_helper(dev
, sz
, input
, debug
);