1 /*
2 * Copyright (C) 2020-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 * Authors (Collabora):
24 * Alyssa Rosenzweig <[email protected]>
25 */
26
27 #ifndef __BI_TEST_H
28 #define __BI_TEST_H
29
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include "compiler.h"
33
34 /* Helper to generate a bi_builder suitable for creating test instructions */
35 static inline bi_block *
bit_block(bi_context * ctx)36 bit_block(bi_context *ctx)
37 {
38 bi_block *blk = rzalloc(ctx, bi_block);
39
40 util_dynarray_init(&blk->predecessors, blk);
41 list_addtail(&blk->link, &ctx->blocks);
42 list_inithead(&blk->instructions);
43
44 blk->index = ctx->num_blocks++;
45
46 return blk;
47 }
48
49 static inline bi_builder *
bit_builder(void * memctx)50 bit_builder(void *memctx)
51 {
52 bi_context *ctx = rzalloc(memctx, bi_context);
53 list_inithead(&ctx->blocks);
54 ctx->inputs = rzalloc(memctx, struct panfrost_compile_inputs);
55
56 bi_block *blk = bit_block(ctx);
57
58 bi_builder *b = rzalloc(memctx, bi_builder);
59 b->shader = ctx;
60 b->cursor = bi_after_block(blk);
61 return b;
62 }
63
64 /* Helper to compare for logical equality of instructions. Need to skip over
65 * the pointers, guaranteed to be first. After that we can compare raw data.
66 */
67 static inline bool
bit_instr_equal(bi_instr * A,bi_instr * B)68 bit_instr_equal(bi_instr *A, bi_instr *B)
69 {
70 size_t skip = sizeof(struct list_head) + 2 * sizeof(bi_index *);
71
72 if (memcmp((uint8_t *)A + skip, (uint8_t *)B + skip,
73 sizeof(bi_instr) - skip))
74 return false;
75
76 if (memcmp(A->dest, B->dest, sizeof(bi_index) * A->nr_dests))
77 return false;
78
79 if (memcmp(A->src, B->src, sizeof(bi_index) * A->nr_srcs))
80 return false;
81
82 return true;
83 }
84
85 static inline bool
bit_block_equal(bi_block * A,bi_block * B)86 bit_block_equal(bi_block *A, bi_block *B)
87 {
88 if (list_length(&A->instructions) != list_length(&B->instructions))
89 return false;
90
91 list_pair_for_each_entry(bi_instr, insA, insB, &A->instructions,
92 &B->instructions, link) {
93 if (!bit_instr_equal(insA, insB))
94 return false;
95 }
96
97 return true;
98 }
99
100 static inline bool
bit_shader_equal(bi_context * A,bi_context * B)101 bit_shader_equal(bi_context *A, bi_context *B)
102 {
103 if (list_length(&A->blocks) != list_length(&B->blocks))
104 return false;
105
106 list_pair_for_each_entry(bi_block, blockA, blockB, &A->blocks, &B->blocks,
107 link) {
108 if (!bit_block_equal(blockA, blockB))
109 return false;
110 }
111
112 return true;
113 }
114
115 #define ASSERT_SHADER_EQUAL(A, B) \
116 if (!bit_shader_equal(A, B)) { \
117 ADD_FAILURE(); \
118 fprintf(stderr, "Pass produced unexpected results"); \
119 fprintf(stderr, " Actual:\n"); \
120 bi_print_shader(A, stderr); \
121 fprintf(stderr, " Expected:\n"); \
122 bi_print_shader(B, stderr); \
123 fprintf(stderr, "\n"); \
124 }
125
126 #define INSTRUCTION_CASE(instr, expected, pass) \
127 do { \
128 bi_builder *A = bit_builder(mem_ctx); \
129 bi_builder *B = bit_builder(mem_ctx); \
130 { \
131 bi_builder *b = A; \
132 instr; \
133 } \
134 { \
135 bi_builder *b = B; \
136 expected; \
137 } \
138 pass(A->shader); \
139 ASSERT_SHADER_EQUAL(A->shader, B->shader); \
140 } while (0)
141
142 #endif
143