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 "compiler.h"
25 #include "bi_test.h"
26 #include "bi_builder.h"
27
28 #define CASE(instr, expected) do { \
29 bool _unsupported = false; \
30 uint32_t _value = bi_fold_constant(instr, &_unsupported); \
31 if (_unsupported) { \
32 fprintf(stderr, "Constant folding failed:\n"); \
33 bi_print_instr(instr, stderr); \
34 fprintf(stderr, "\n"); \
35 } else if (_value == (expected)) { \
36 nr_pass++; \
37 } else { \
38 fprintf(stderr, "Got %" PRIx32 ", expected %" PRIx32 "\n", _value, expected); \
39 bi_print_instr(instr, stderr); \
40 fprintf(stderr, "\n"); \
41 nr_fail++; \
42 } \
43 } while(0)
44
45 #define NEGCASE(instr) do { \
46 bool _unsupported = false; \
47 bi_fold_constant(instr, &_unsupported); \
48 if (_unsupported) { \
49 nr_pass++; \
50 } else { \
51 fprintf(stderr, "Should not have constant folded:\n"); \
52 bi_print_instr(instr, stderr); \
53 fprintf(stderr, "\n"); \
54 nr_fail++; \
55 } \
56 } while(0)
57
58 int
main(int argc,const char ** argv)59 main(int argc, const char **argv)
60 {
61 unsigned nr_fail = 0, nr_pass = 0;
62 void *ralloc_ctx = ralloc_context(NULL);
63 bi_builder *b = bit_builder(ralloc_ctx);
64 bi_index zero = bi_fau(BIR_FAU_IMMEDIATE | 0, false);
65 bi_index reg = bi_register(0);
66
67 /* Swizzles should be constant folded */
68 CASE(bi_swz_v2i16_to(b, reg, bi_imm_u32(0xCAFEBABE)), 0xCAFEBABE);
69 CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)),
70 0xBABEBABE);
71 CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, false)),
72 0xBABECAFE);
73 CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true)),
74 0xCAFECAFE);
75
76 /* Vector constructions should be constant folded */
77 CASE(bi_mkvec_v2i16_to(b, reg, bi_imm_u16(0xCAFE), bi_imm_u16(0xBABE)), 0xBABECAFE);
78 CASE(bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
79 bi_imm_u16(0xBABE)), 0xBABECAFE);
80 CASE(bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
81 bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)), 0xBABECAFE);
82
83 {
84 bi_index u32 = bi_imm_u32(0xCAFEBABE);
85
86 bi_index a = bi_byte(u32, 0); /* 0xBE */
87 bi_index c = bi_byte(u32, 2); /* 0xFE */
88
89 CASE(bi_mkvec_v4i8_to(b, reg, a, a, a, a), 0xBEBEBEBE);
90 CASE(bi_mkvec_v4i8_to(b, reg, a, c, a, c), 0xFEBEFEBE);
91 CASE(bi_mkvec_v4i8_to(b, reg, c, a, c, a), 0xBEFEBEFE);
92 CASE(bi_mkvec_v4i8_to(b, reg, c, c, c, c), 0xFEFEFEFE);
93 }
94
95 /* Limited shifts required for texturing */
96 CASE(bi_lshift_or_i32_to(b, reg, bi_imm_u32(0xCAFE), bi_imm_u32(0xA0000), bi_imm_u8(4)), (0xCAFE << 4) | 0xA0000);
97 NEGCASE(bi_lshift_or_i32_to(b, reg, bi_imm_u32(0xCAFE), bi_not(bi_imm_u32(0xA0000)), bi_imm_u8(4)));
98 NEGCASE(bi_lshift_or_i32_to(b, reg, bi_not(bi_imm_u32(0xCAFE)), bi_imm_u32(0xA0000), bi_imm_u8(4)));
99 {
100 bi_instr *I = bi_lshift_or_i32_to(b, reg, bi_imm_u32(0xCAFE), bi_imm_u32(0xA0000), bi_imm_u8(4));
101 I->not_result = true;
102 NEGCASE(I);
103 }
104
105 /* Limited rounding needed for texturing */
106 CASE(bi_f32_to_u32_to(b, reg, bi_imm_f32(15.0), BI_ROUND_NONE), 15);
107 CASE(bi_f32_to_u32_to(b, reg, bi_imm_f32(15.9), BI_ROUND_NONE), 15);
108 CASE(bi_f32_to_u32_to(b, reg, bi_imm_f32(-20.4), BI_ROUND_NONE), 0);
109 NEGCASE(bi_f32_to_u32_to(b, reg, bi_imm_f32(-20.4), BI_ROUND_RTP));
110 NEGCASE(bi_f32_to_u32_to(b, reg, bi_imm_f32(-20.4), BI_ROUND_RTZ));
111
112 /* Instructions with non-constant sources cannot be constant folded */
113 NEGCASE(bi_swz_v2i16_to(b, reg, bi_temp(b->shader)));
114 NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_temp(b->shader)));
115 NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_imm_u32(0xDEADBEEF)));
116 NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_imm_u32(0xDEADBEEF), bi_temp(b->shader)));
117
118 /* Other operations should not be constant folded */
119 NEGCASE(bi_fma_f32_to(b, reg, zero, zero, zero, BI_ROUND_NONE));
120 NEGCASE(bi_fadd_f32_to(b, reg, zero, zero, BI_ROUND_NONE));
121
122 ralloc_free(ralloc_ctx);
123 TEST_END(nr_pass, nr_fail);
124 }
125