1/*
2 * Copyright (C) 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
24#include "va_compiler.h"
25#include "bi_test.h"
26#include "bi_builder.h"
27
28#include <gtest/gtest.h>
29
30#define CASE(instr, expected) do { \
31   if (va_validate_fau(instr) != expected) { \
32      fprintf(stderr, "Incorrect validation for:\n"); \
33      bi_print_instr(instr, stderr); \
34      fprintf(stderr, "\n"); \
35      ADD_FAILURE(); \
36   } \
37} while(0)
38
39#define VALID(instr) CASE(instr, true)
40#define INVALID(instr) CASE(instr, false)
41
42class ValidateFau : public testing::Test {
43protected:
44   ValidateFau() {
45      mem_ctx = ralloc_context(NULL);
46      b = bit_builder(mem_ctx);
47
48      zero = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 0), false);
49      imm1 = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 1), false);
50      imm2 = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 2), false);
51      unif = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 5), false);
52      unif_hi = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 5), true);
53      unif2 = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 6), false);
54      core_id = bi_fau(BIR_FAU_CORE_ID, false);
55      lane_id = bi_fau(BIR_FAU_LANE_ID, false);
56   }
57
58   ~ValidateFau() {
59      ralloc_free(mem_ctx);
60   }
61
62   void *mem_ctx;
63   bi_builder *b;
64   bi_index zero, imm1, imm2, unif, unif_hi, unif2, core_id, lane_id;
65};
66
67TEST_F(ValidateFau, One64BitUniformSlot)
68{
69   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(3),
70            unif));
71   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, unif));
72   VALID(bi_fma_f32_to(b, bi_register(1), unif, unif, unif_hi));
73   INVALID(bi_fma_f32_to(b, bi_register(1), unif, unif2, bi_register(1)));
74   INVALID(bi_fma_f32_to(b, bi_register(1), unif, unif2, unif_hi));
75
76   /* Crafted case that appears correct at first glance and was erronously
77    * marked as valid in early versions of the validator.
78    */
79   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2),
80                         bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 0), false),
81                         bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 1), true)));
82}
83
84TEST_F(ValidateFau, Combined64BitUniformsConstants)
85{
86   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, unif));
87   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), zero, unif));
88   VALID(bi_fma_f32_to(b, bi_register(1), zero, imm1, imm1));
89   INVALID(bi_fma_f32_to(b, bi_register(1), zero, unif_hi, unif));
90   INVALID(bi_fma_f32_to(b, bi_register(1), zero, imm1, imm2));
91}
92
93TEST_F(ValidateFau, UniformsOnlyInDefaultMode)
94{
95   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, lane_id));
96   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, core_id));
97}
98
99TEST_F(ValidateFau, SingleSpecialImmediate)
100{
101   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(2),
102            lane_id));
103   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(2),
104            core_id));
105   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), lane_id,
106            core_id));
107}
108
109TEST_F(ValidateFau, SmokeTests)
110{
111   VALID(bi_mov_i32_to(b, bi_register(1), bi_register(2)));
112   VALID(bi_mov_i32_to(b, bi_register(1), unif));
113   VALID(bi_fma_f32_to(b, bi_register(1), bi_discard(bi_register(1)),
114                        unif, bi_neg(zero)));
115}
116