• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* 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 link, guaranteed to be first. After that we can compare raw data. */
66 static inline bool
bit_instr_equal(bi_instr * A,bi_instr * B)67 bit_instr_equal(bi_instr *A, bi_instr *B)
68 {
69    return memcmp((uint8_t *) A    + sizeof(struct list_head),
70                  (uint8_t *) B    + sizeof(struct list_head),
71                  sizeof(bi_instr) - sizeof(struct list_head)) == 0;
72 }
73 
74 static inline bool
bit_block_equal(bi_block * A,bi_block * B)75 bit_block_equal(bi_block *A, bi_block *B)
76 {
77    if (list_length(&A->instructions) != list_length(&B->instructions))
78       return false;
79 
80    list_pair_for_each_entry(bi_instr, insA, insB,
81                             &A->instructions, &B->instructions, link) {
82       if (!bit_instr_equal(insA, insB))
83          return false;
84    }
85 
86    return true;
87 }
88 
89 static inline bool
bit_shader_equal(bi_context * A,bi_context * B)90 bit_shader_equal(bi_context *A, bi_context *B)
91 {
92    if (list_length(&A->blocks) != list_length(&B->blocks))
93       return false;
94 
95    list_pair_for_each_entry(bi_block, blockA, blockB,
96                             &A->blocks, &B->blocks, link) {
97       if (!bit_block_equal(blockA, blockB))
98          return false;
99    }
100 
101    return true;
102 }
103 
104 #define ASSERT_SHADER_EQUAL(A, B) \
105    if (!bit_shader_equal(A, B)) { \
106       ADD_FAILURE(); \
107       fprintf(stderr, "Pass produced unexpected results"); \
108       fprintf(stderr, "  Actual:\n"); \
109       bi_print_shader(A, stderr); \
110       fprintf(stderr, " Expected:\n"); \
111       bi_print_shader(B, stderr); \
112       fprintf(stderr, "\n"); \
113    } \
114 
115 #define INSTRUCTION_CASE(instr, expected, pass) do { \
116    bi_builder *A = bit_builder(mem_ctx); \
117    bi_builder *B = bit_builder(mem_ctx); \
118    { \
119       bi_builder *b = A; \
120       instr; \
121    } \
122    { \
123       bi_builder *b = B; \
124       expected; \
125    } \
126    pass(A->shader); \
127    ASSERT_SHADER_EQUAL(A->shader, B->shader); \
128 } while(0)
129 
130 #endif
131