1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2020-2021 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors (Collabora):
24bf215546Sopenharmony_ci *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef __BI_TEST_H
28bf215546Sopenharmony_ci#define __BI_TEST_H
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdio.h>
31bf215546Sopenharmony_ci#include <inttypes.h>
32bf215546Sopenharmony_ci#include "compiler.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/* Helper to generate a bi_builder suitable for creating test instructions */
35bf215546Sopenharmony_cistatic inline bi_block *
36bf215546Sopenharmony_cibit_block(bi_context *ctx)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci        bi_block *blk = rzalloc(ctx, bi_block);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci        util_dynarray_init(&blk->predecessors, blk);
41bf215546Sopenharmony_ci        list_addtail(&blk->link, &ctx->blocks);
42bf215546Sopenharmony_ci        list_inithead(&blk->instructions);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci        blk->index = ctx->num_blocks++;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci        return blk;
47bf215546Sopenharmony_ci}
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistatic inline bi_builder *
50bf215546Sopenharmony_cibit_builder(void *memctx)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci        bi_context *ctx = rzalloc(memctx, bi_context);
53bf215546Sopenharmony_ci        list_inithead(&ctx->blocks);
54bf215546Sopenharmony_ci        ctx->inputs = rzalloc(memctx, struct panfrost_compile_inputs);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci        bi_block *blk = bit_block(ctx);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci        bi_builder *b = rzalloc(memctx, bi_builder);
59bf215546Sopenharmony_ci        b->shader = ctx;
60bf215546Sopenharmony_ci        b->cursor = bi_after_block(blk);
61bf215546Sopenharmony_ci        return b;
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/* Helper to compare for logical equality of instructions. Need to skip over
65bf215546Sopenharmony_ci * the link, guaranteed to be first. After that we can compare raw data. */
66bf215546Sopenharmony_cistatic inline bool
67bf215546Sopenharmony_cibit_instr_equal(bi_instr *A, bi_instr *B)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   return memcmp((uint8_t *) A    + sizeof(struct list_head),
70bf215546Sopenharmony_ci                 (uint8_t *) B    + sizeof(struct list_head),
71bf215546Sopenharmony_ci                 sizeof(bi_instr) - sizeof(struct list_head)) == 0;
72bf215546Sopenharmony_ci}
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_cistatic inline bool
75bf215546Sopenharmony_cibit_block_equal(bi_block *A, bi_block *B)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   if (list_length(&A->instructions) != list_length(&B->instructions))
78bf215546Sopenharmony_ci      return false;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   list_pair_for_each_entry(bi_instr, insA, insB,
81bf215546Sopenharmony_ci                            &A->instructions, &B->instructions, link) {
82bf215546Sopenharmony_ci      if (!bit_instr_equal(insA, insB))
83bf215546Sopenharmony_ci         return false;
84bf215546Sopenharmony_ci   }
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   return true;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic inline bool
90bf215546Sopenharmony_cibit_shader_equal(bi_context *A, bi_context *B)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   if (list_length(&A->blocks) != list_length(&B->blocks))
93bf215546Sopenharmony_ci      return false;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   list_pair_for_each_entry(bi_block, blockA, blockB,
96bf215546Sopenharmony_ci                            &A->blocks, &B->blocks, link) {
97bf215546Sopenharmony_ci      if (!bit_block_equal(blockA, blockB))
98bf215546Sopenharmony_ci         return false;
99bf215546Sopenharmony_ci   }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   return true;
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci#define ASSERT_SHADER_EQUAL(A, B) \
105bf215546Sopenharmony_ci   if (!bit_shader_equal(A, B)) { \
106bf215546Sopenharmony_ci      ADD_FAILURE(); \
107bf215546Sopenharmony_ci      fprintf(stderr, "Pass produced unexpected results"); \
108bf215546Sopenharmony_ci      fprintf(stderr, "  Actual:\n"); \
109bf215546Sopenharmony_ci      bi_print_shader(A, stderr); \
110bf215546Sopenharmony_ci      fprintf(stderr, " Expected:\n"); \
111bf215546Sopenharmony_ci      bi_print_shader(B, stderr); \
112bf215546Sopenharmony_ci      fprintf(stderr, "\n"); \
113bf215546Sopenharmony_ci   } \
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci#define INSTRUCTION_CASE(instr, expected, pass) do { \
116bf215546Sopenharmony_ci   bi_builder *A = bit_builder(mem_ctx); \
117bf215546Sopenharmony_ci   bi_builder *B = bit_builder(mem_ctx); \
118bf215546Sopenharmony_ci   { \
119bf215546Sopenharmony_ci      bi_builder *b = A; \
120bf215546Sopenharmony_ci      instr; \
121bf215546Sopenharmony_ci   } \
122bf215546Sopenharmony_ci   { \
123bf215546Sopenharmony_ci      bi_builder *b = B; \
124bf215546Sopenharmony_ci      expected; \
125bf215546Sopenharmony_ci   } \
126bf215546Sopenharmony_ci   pass(A->shader); \
127bf215546Sopenharmony_ci   ASSERT_SHADER_EQUAL(A->shader, B->shader); \
128bf215546Sopenharmony_ci} while(0)
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci#endif
131