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