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 MERCHANTAAGXLITY,
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 * LIAAGXLITY, 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 __AGX_TEST_H
28 #define __AGX_TEST_H
29
30 #include <stdio.h>
31 #include <inttypes.h>
32 #include "agx_compiler.h"
33 #include "agx_builder.h"
34
35 /* Helper to generate a agx_builder suitable for creating test instructions */
36 static inline agx_builder *
agx_test_builder(void * memctx)37 agx_test_builder(void *memctx)
38 {
39 agx_context *ctx = rzalloc(memctx, agx_context);
40 list_inithead(&ctx->blocks);
41
42 agx_block *blk = rzalloc(ctx, agx_block);
43 util_dynarray_init(&blk->predecessors, NULL);
44
45 list_addtail(&blk->link, &ctx->blocks);
46 list_inithead(&blk->instructions);
47
48 agx_builder *b = rzalloc(memctx, agx_builder);
49 b->shader = ctx;
50 b->cursor = agx_after_block(blk);
51
52 return b;
53 }
54
55 /* Helper to compare for logical equality of instructions. Need to compare the
56 * pointers, then compare raw data.
57 */
58 static inline bool
agx_instr_equal(agx_instr * A,agx_instr * B)59 agx_instr_equal(agx_instr *A, agx_instr *B)
60 {
61 unsigned pointers = sizeof(struct list_head) + sizeof(agx_index *);
62
63 if (A->nr_srcs != B->nr_srcs)
64 return false;
65
66 if (memcmp(A->src, B->src, A->nr_srcs * sizeof(agx_index)))
67 return false;
68
69 return memcmp((uint8_t *) A + pointers,
70 (uint8_t *) B + pointers,
71 sizeof(agx_instr) - pointers) == 0;
72 }
73
74 static inline bool
agx_block_equal(agx_block * A,agx_block * B)75 agx_block_equal(agx_block *A, agx_block *B)
76 {
77 if (list_length(&A->instructions) != list_length(&B->instructions))
78 return false;
79
80 list_pair_for_each_entry(agx_instr, insA, insB,
81 &A->instructions, &B->instructions, link) {
82 if (!agx_instr_equal(insA, insB))
83 return false;
84 }
85
86 return true;
87 }
88
89 static inline bool
agx_shader_equal(agx_context * A,agx_context * B)90 agx_shader_equal(agx_context *A, agx_context *B)
91 {
92 if (list_length(&A->blocks) != list_length(&B->blocks))
93 return false;
94
95 list_pair_for_each_entry(agx_block, blockA, blockB,
96 &A->blocks, &B->blocks, link) {
97 if (!agx_block_equal(blockA, blockB))
98 return false;
99 }
100
101 return true;
102 }
103
104 #define ASSERT_SHADER_EQUAL(A, B) \
105 if (!agx_shader_equal(A, B)) { \
106 ADD_FAILURE(); \
107 fprintf(stderr, "Pass produced unexpected results"); \
108 fprintf(stderr, " Actual:\n"); \
109 agx_print_shader(A, stderr); \
110 fprintf(stderr, " Expected:\n"); \
111 agx_print_shader(B, stderr); \
112 fprintf(stderr, "\n"); \
113 } \
114
115 #define INSTRUCTION_CASE(instr, expected, pass) do { \
116 agx_builder *A = agx_test_builder(mem_ctx); \
117 agx_builder *B = agx_test_builder(mem_ctx); \
118 { \
119 agx_builder *b = A; \
120 instr; \
121 } \
122 { \
123 agx_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