Debian Codegraph @

src/panfrost/compiler/bifrost/bi_test.h

raw
1 /*
2 * Copyright (C) 2020-2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef __BI_TEST_H
7 #define __BI_TEST_H
8
9 #include <inttypes.h>
10 #include <stdio.h>
11 #include "compiler.h"
12
13 /* Helper to generate a bi_builder suitable for creating test instructions */
14 static inline bi_block *
15 bit_block(bi_context *ctx)
16 {
17 bi_block *blk = rzalloc(ctx, bi_block);
18
19 util_dynarray_init(&blk->predecessors, blk);
20 list_addtail(&blk->link, &ctx->blocks);
21 list_inithead(&blk->instructions);
22
23 blk->index = ctx->num_blocks++;
24
25 return blk;
26 }
27
28 static inline bi_builder *
29 bit_builder(void *memctx)
30 {
31 bi_context *ctx = rzalloc(memctx, bi_context);
32 list_inithead(&ctx->blocks);
33 ctx->inputs = rzalloc(memctx, struct pan_compile_inputs);
34
35 bi_block *blk = bit_block(ctx);
36
37 bi_builder *b = rzalloc(memctx, bi_builder);
38 b->shader = ctx;
39 b->cursor = bi_after_block(blk);
40 return b;
41 }
42
43 /* Helper to compare for logical equality of instructions. Need to skip over
44 * the pointers, guaranteed to be first. After that we can compare raw data.
45 */
46 static inline bool
47 bit_instr_equal(bi_instr *A, bi_instr *B)
48 {
49 size_t skip = sizeof(struct list_head) + 2 * sizeof(bi_index *);
50
51 if (memcmp((uint8_t *)A + skip, (uint8_t *)B + skip,
52 sizeof(bi_instr) - skip))
53 return false;
54
55 if (memcmp(A->dest, B->dest, sizeof(bi_index) * A->nr_dests))
56 return false;
57
58 if (memcmp(A->src, B->src, sizeof(bi_index) * A->nr_srcs))
59 return false;
60
61 return true;
62 }
63
64 static inline bool
65 bit_block_equal(bi_block *A, bi_block *B)
66 {
67 if (list_length(&A->instructions) != list_length(&B->instructions))
68 return false;
69
70 list_pair_for_each_entry(bi_instr, insA, insB, &A->instructions,
71 &B->instructions, link) {
72 if (!bit_instr_equal(insA, insB))
73 return false;
74 }
75
76 return true;
77 }
78
79 static inline bool
80 bit_shader_equal(bi_context *A, bi_context *B)
81 {
82 if (list_length(&A->blocks) != list_length(&B->blocks))
83 return false;
84
85 list_pair_for_each_entry(bi_block, blockA, blockB, &A->blocks, &B->blocks,
86 link) {
87 if (!bit_block_equal(blockA, blockB))
88 return false;
89 }
90
91 return true;
92 }
93
94 #define ASSERT_SHADER_EQUAL(A, B) \
95 if (!bit_shader_equal(A, B)) { \
96 ADD_FAILURE(); \
97 fprintf(stderr, "Pass produced unexpected results"); \
98 fprintf(stderr, " Actual:\n"); \
99 bi_print_shader(A, stderr); \
100 fprintf(stderr, " Expected:\n"); \
101 bi_print_shader(B, stderr); \
102 fprintf(stderr, "\n"); \
103 }
104
105 #define INSTRUCTION_CASE(instr, expected, pass) \
106 do { \
107 bi_builder *A = bit_builder(mem_ctx); \
108 bi_builder *B = bit_builder(mem_ctx); \
109 { \
110 bi_builder *b = A; \
111 instr; \
112 } \
113 { \
114 bi_builder *b = B; \
115 expected; \
116 } \
117 pass(A->shader); \
118 ASSERT_SHADER_EQUAL(A->shader, B->shader); \
119 } while (0)
120
121 #endif