Debian Codegraph @

src/panfrost/compiler/bifrost/test/test-constant-fold.cpp

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "bi_test.h"
8 #include "compiler.h"
9
10 #include <gtest/gtest.h>
11
12 static std::string
13 to_string(const bi_instr *I)
14 {
15 char *cstr = NULL;
16 size_t size = 0;
17 FILE *f = open_memstream(&cstr, &size);
18 bi_print_instr(I, f);
19 fclose(f);
20 auto str = std::string(cstr);
21 free(cstr);
22 return str;
23 }
24
25 static testing::AssertionResult
26 constant_fold_pred(const char *I_expr, const char *expected_expr, bi_instr *I,
27 uint32_t expected)
28 {
29 bool unsupported = false;
30 uint32_t v = bi_fold_constant(I, &unsupported);
31 if (unsupported) {
32 return testing::AssertionFailure()
33 << "Constant fold unsupported for instruction \n\n"
34 << " " << to_string(I);
35 } else if (v != expected) {
36 return testing::AssertionFailure()
37 << "Unexpected result when constant folding instruction\n\n"
38 << " " << to_string(I) << "\n"
39 << " Actual: " << v << "\n"
40 << "Expected: " << expected << "\n";
41 } else {
42 return testing::AssertionSuccess();
43 }
44 }
45
46 #define EXPECT_FOLD(i, e) EXPECT_PRED_FORMAT2(constant_fold_pred, i, e)
47
48 static testing::AssertionResult
49 not_constant_fold_pred(const char *I_expr, bi_instr *I)
50 {
51 bool unsupported = false;
52 uint32_t v = bi_fold_constant(I, &unsupported);
53 if (unsupported) {
54 return testing::AssertionSuccess();
55 } else {
56 return testing::AssertionFailure()
57 << "Instruction\n\n"
58 << " " << to_string(I) << "\n"
59 << "shouldn't have constant folded, but folded to: " << v;
60 }
61 }
62
63 #define EXPECT_NOT_FOLD(i) EXPECT_PRED_FORMAT1(not_constant_fold_pred, i)
64
65 class ConstantFold : public testing::Test {
66 protected:
67 ConstantFold()
68 {
69 mem_ctx = ralloc_context(NULL);
70 b = bit_builder(mem_ctx);
71 }
72 ~ConstantFold()
73 {
74 ralloc_free(mem_ctx);
75 }
76
77 void *mem_ctx;
78 bi_builder *b;
79 };
80
81 TEST_F(ConstantFold, Swizzles)
82 {
83 bi_index reg = bi_register(0);
84
85 EXPECT_FOLD(bi_swz_v2i16_to(b, reg, bi_imm_u32(0xCAFEBABE)), 0xCAFEBABE);
86
87 EXPECT_FOLD(
88 bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)),
89 0xBABEBABE);
90
91 EXPECT_FOLD(
92 bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, false)),
93 0xBABECAFE);
94
95 EXPECT_FOLD(
96 bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true)),
97 0xCAFECAFE);
98 }
99
100 TEST_F(ConstantFold, VectorConstructions2i16)
101 {
102 bi_index reg = bi_register(0);
103
104 EXPECT_FOLD(
105 bi_mkvec_v2i16_to(b, reg, bi_imm_u16(0xCAFE), bi_imm_u16(0xBABE)),
106 0xBABECAFE);
107
108 EXPECT_FOLD(
109 bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
110 bi_imm_u16(0xBABE)),
111 0xBABECAFE);
112
113 EXPECT_FOLD(
114 bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
115 bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)),
116 0xBABECAFE);
117 }
118
119 TEST_F(ConstantFold, VectorConstructions4i8)
120 {
121 bi_index reg = bi_register(0);
122 bi_index u32 = bi_imm_u32(0xCAFEBABE);
123
124 bi_index a = bi_byte(u32, 0); /* 0xBE */
125 bi_index c = bi_byte(u32, 2); /* 0xFE */
126
127 EXPECT_FOLD(bi_mkvec_v4i8_to(b, reg, a, a, a, a), 0xBEBEBEBE);
128 EXPECT_FOLD(bi_mkvec_v4i8_to(b, reg, a, c, a, c), 0xFEBEFEBE);
129 EXPECT_FOLD(bi_mkvec_v4i8_to(b, reg, c, a, c, a), 0xBEFEBEFE);
130 EXPECT_FOLD(bi_mkvec_v4i8_to(b, reg, c, c, c, c), 0xFEFEFEFE);
131 }
132
133 TEST_F(ConstantFold, VectorConstructions2i8)
134 {
135 bi_index reg = bi_register(0);
136 bi_index u32 = bi_imm_u32(0xCAFEBABE);
137 bi_index rem = bi_imm_u32(0xABCD1234);
138
139 bi_index a = bi_byte(u32, 0); /* 0xBE */
140 bi_index B = bi_byte(u32, 1); /* 0xBA */
141 bi_index c = bi_byte(u32, 2); /* 0xFE */
142 bi_index d = bi_byte(u32, 3); /* 0xCA */
143
144 EXPECT_FOLD(bi_mkvec_v2i8_to(b, reg, a, B, rem), 0x1234BABE);
145 EXPECT_FOLD(bi_mkvec_v2i8_to(b, reg, a, d, rem), 0x1234CABE);
146 EXPECT_FOLD(bi_mkvec_v2i8_to(b, reg, c, d, rem), 0x1234CAFE);
147 EXPECT_FOLD(bi_mkvec_v2i8_to(b, reg, d, d, rem), 0x1234CACA);
148 }
149
150 TEST_F(ConstantFold, LimitedShiftsForTexturing)
151 {
152 bi_index reg = bi_register(0);
153
154 EXPECT_FOLD(bi_lshift_or_i32_to(b, reg, bi_imm_u32(0xCAFE),
155 bi_imm_u32(0xA0000), bi_imm_u8(4)),
156 (0xCAFE << 4) | 0xA0000);
157
158 EXPECT_NOT_FOLD(bi_lshift_or_i32_to(
159 b, reg, bi_imm_u32(0xCAFE), bi_not(bi_imm_u32(0xA0000)), bi_imm_u8(4)));
160
161 EXPECT_NOT_FOLD(bi_lshift_or_i32_to(b, reg, bi_not(bi_imm_u32(0xCAFE)),
162 bi_imm_u32(0xA0000), bi_imm_u8(4)));
163
164 bi_instr *I = bi_lshift_or_i32_to(b, reg, bi_imm_u32(0xCAFE),
165 bi_imm_u32(0xA0000), bi_imm_u8(4));
166 I->not_result = true;
167 EXPECT_NOT_FOLD(I);
168 }
169
170 TEST_F(ConstantFold, NonConstantSourcesCannotBeFolded)
171 {
172 bi_index reg = bi_register(0);
173
174 EXPECT_NOT_FOLD(bi_swz_v2i16_to(b, reg, bi_temp(b->shader)));
175 EXPECT_NOT_FOLD(
176 bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_temp(b->shader)));
177 EXPECT_NOT_FOLD(
178 bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_imm_u32(0xDEADBEEF)));
179 EXPECT_NOT_FOLD(
180 bi_mkvec_v2i16_to(b, reg, bi_imm_u32(0xDEADBEEF), bi_temp(b->shader)));
181 }
182
183 TEST_F(ConstantFold, OtherOperationsShouldNotFold)
184 {
185 bi_index zero = bi_fau(bir_fau(BIR_FAU_IMMEDIATE | 0), false);
186 bi_index reg = bi_register(0);
187
188 EXPECT_NOT_FOLD(bi_fma_f32_to(b, reg, zero, zero, zero));
189 EXPECT_NOT_FOLD(bi_fadd_f32_to(b, reg, zero, zero));
190 }