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 <alyssa.rosenzweig@collabora.com>
25 */
26
27 #ifndef __BI_TEST_H
28 #define __BI_TEST_H
29
30 #include <stdio.h>
31 #include <inttypes.h>
32 #include "compiler.h"
33
34 /* Helpers for unit testing */
35 #define TEST_END(nr_pass, nr_fail) \
36 printf("Passed %u/%u tests.\n", nr_pass, nr_pass + nr_fail); \
37 return nr_fail ? 1 : 0;
38
39 /* Helper to generate a bi_builder suitable for creating test instructions */
40 static inline bi_builder *
bit_builder(void * memctx)41 bit_builder(void *memctx)
42 {
43 bi_context *ctx = rzalloc(memctx, bi_context);
44 list_inithead(&ctx->blocks);
45
46 bi_block *blk = rzalloc(ctx, bi_block);
47
48 blk->predecessors = _mesa_set_create(blk,
49 _mesa_hash_pointer,
50 _mesa_key_pointer_equal);
51
52 list_addtail(&blk->link, &ctx->blocks);
53 list_inithead(&blk->instructions);
54
55 bi_builder *b = rzalloc(memctx, bi_builder);
56 b->shader = ctx;
57 b->cursor = bi_after_block(blk);
58 return b;
59 }
60
61 /* Helper to compare for logical equality of instructions. Need to skip over
62 * the link, guaranteed to be first. After that we can compare raw data. */
63 static inline bool
bit_instr_equal(bi_instr * A,bi_instr * B)64 bit_instr_equal(bi_instr *A, bi_instr *B)
65 {
66 return memcmp((uint8_t *) A + sizeof(struct list_head),
67 (uint8_t *) B + sizeof(struct list_head),
68 sizeof(bi_instr) - sizeof(struct list_head)) == 0;
69 }
70
71 static inline bool
bit_block_equal(bi_block * A,bi_block * B)72 bit_block_equal(bi_block *A, bi_block *B)
73 {
74 if (list_length(&A->instructions) != list_length(&B->instructions))
75 return false;
76
77 list_pair_for_each_entry(bi_instr, insA, insB,
78 &A->instructions, &B->instructions, link) {
79 if (!bit_instr_equal(insA, insB))
80 return false;
81 }
82
83 return true;
84 }
85
86 static inline bool
bit_shader_equal(bi_context * A,bi_context * B)87 bit_shader_equal(bi_context *A, bi_context *B)
88 {
89 if (list_length(&A->blocks) != list_length(&B->blocks))
90 return false;
91
92 list_pair_for_each_entry(bi_block, blockA, blockB,
93 &A->blocks, &B->blocks, link) {
94 if (!bit_block_equal(blockA, blockB))
95 return false;
96 }
97
98 return true;
99 }
100
101 #define INSTRUCTION_CASE(instr, expected, CB) do { \
102 bi_instr *left = instr; \
103 bi_instr *right = expected; \
104 CB(b, left); \
105 if (bit_instr_equal(left, right)) { \
106 nr_pass++; \
107 } else { \
108 fprintf(stderr, "Incorrect optimization\n"); \
109 bi_print_instr(instr, stderr); \
110 bi_print_instr(left, stderr); \
111 bi_print_instr(right, stderr); \
112 fprintf(stderr, "\n"); \
113 nr_fail++; \
114 } \
115 } while(0)
116
117 #define BIT_ASSERT(condition) do { \
118 if (condition) { \
119 nr_pass++; \
120 } else { \
121 fprintf(stderr, "Assertion failed: %s\n", #condition); \
122 nr_fail++; \
123 } \
124 } while(0)
125
126 #endif
127