1 /*
2 * Copyright © 2020 Google, Inc.
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <err.h>
25 #include <stdio.h>
26
27 #include "ir3.h"
28 #include "ir3_assembler.h"
29 #include "ir3_shader.h"
30
31 /*
32 * A test for delay-slot calculation. Each test specifies ir3 assembly
33 * for one or more instructions and the last instruction that consumes
34 * the previously produced values. And the expected number of delay
35 * slots that would be needed before that last instruction. Any source
36 * registers in the last instruction which are not written in a previous
37 * instruction are not counted.
38 */
39
40 /* clang-format off */
41 #define TEST(n, ...) { # __VA_ARGS__, n }
42 /* clang-format on */
43
44 static const struct test {
45 const char *asmstr;
46 unsigned expected_delay;
47 } tests[] = {
48 /* clang-format off */
49 TEST(6,
50 add.f r0.x, r2.x, r2.y
51 rsq r0.x, r0.x
52 ),
53 TEST(3,
54 mov.f32f32 r0.x, c0.x
55 mov.f32f32 r0.y, c0.y
56 add.f r0.x, r0.x, r0.y
57 ),
58 TEST(2,
59 mov.f32f32 r0.x, c0.x
60 mov.f32f32 r0.y, c0.y
61 mov.f32f32 r0.z, c0.z
62 mad.f32 r0.x, r0.x, r0.y, r0.z
63 ),
64 TEST(0,
65 mov.f32f32 r0.x, c0.x
66 rcp r0.x, r0.y
67 add.f r0.x, r0.x, c0.x
68 ),
69 TEST(2,
70 mov.f32f32 r0.x, c0.x
71 mov.f32f32 r0.y, c0.y
72 (rpt1)add.f r0.x, (r)r0.x, (r)c0.x
73 ),
74 TEST(2,
75 (rpt1)mov.f32f32 r0.x, c0.x
76 (rpt1)add.f r0.x, (r)r0.x, (r)c0.x
77 ),
78 TEST(3,
79 mov.f32f32 r0.y, c0.y
80 mov.f32f32 r0.x, c0.x
81 (rpt1)add.f r0.x, (r)r0.x, (r)c0.x
82 ),
83 TEST(1,
84 (rpt2)mov.f32f32 r0.x, (r)c0.x
85 add.f r0.x, r0.x, c0.x
86 ),
87 TEST(2,
88 (rpt2)mov.f32f32 r0.x, (r)c0.x
89 add.f r0.x, r0.x, r0.y
90 ),
91 TEST(2,
92 (rpt1)mov.f32f32 r0.x, (r)c0.x
93 (rpt1)add.f r0.x, (r)r0.x, c0.x
94 ),
95 TEST(1,
96 (rpt1)mov.f32f32 r0.y, (r)c0.x
97 (rpt1)add.f r0.x, (r)r0.x, c0.x
98 ),
99 TEST(3,
100 (rpt1)mov.f32f32 r0.x, (r)c0.x
101 (rpt1)add.f r0.x, (r)r0.y, c0.x
102 ),
103 /* clang-format on */
104 };
105
106 static struct ir3_shader *
parse_asm(struct ir3_compiler *c, const char *asmstr)107 parse_asm(struct ir3_compiler *c, const char *asmstr)
108 {
109 struct ir3_kernel_info info = {};
110 FILE *in = fmemopen((void *)asmstr, strlen(asmstr), "r");
111 struct ir3_shader *shader = ir3_parse_asm(c, &info, in);
112
113 fclose(in);
114
115 if (!shader)
116 errx(-1, "assembler failed");
117
118 return shader;
119 }
120
121 /**
122 * ir3_delay_calc_* relies on the src/dst wrmask being correct even for ALU
123 * instructions, so this sets it here.
124 *
125 * Note that this is not clever enough to know how many src/dst there are
126 * for various tex/mem instructions. But the rules for tex consuming alu
127 * are the same as sfu consuming alu.
128 */
129 static void
fixup_wrmask(struct ir3 *ir)130 fixup_wrmask(struct ir3 *ir)
131 {
132 struct ir3_block *block = ir3_start_block(ir);
133
134 foreach_instr_safe (instr, &block->instr_list) {
135 instr->dsts[0]->wrmask = MASK(instr->repeat + 1);
136 foreach_src (reg, instr) {
137 if (reg->flags & (IR3_REG_CONST | IR3_REG_IMMED))
138 continue;
139
140 if (reg->flags & IR3_REG_R)
141 reg->wrmask = MASK(instr->repeat + 1);
142 else
143 reg->wrmask = 1;
144 }
145 }
146 }
147
148 int
main(int argc, char **argv)149 main(int argc, char **argv)
150 {
151 struct ir3_compiler *c;
152 int result = 0;
153
154 struct fd_dev_id dev_id = {
155 .gpu_id = 630,
156 };
157
158 c = ir3_compiler_create(NULL, &dev_id, &(struct ir3_compiler_options){});
159
160 for (int i = 0; i < ARRAY_SIZE(tests); i++) {
161 const struct test *test = &tests[i];
162 struct ir3_shader *shader = parse_asm(c, test->asmstr);
163 struct ir3 *ir = shader->variants->ir;
164
165 fixup_wrmask(ir);
166
167 ir3_debug_print(ir, "AFTER fixup_wrmask");
168
169 struct ir3_block *block =
170 list_first_entry(&ir->block_list, struct ir3_block, node);
171 struct ir3_instruction *last = NULL;
172
173 foreach_instr_rev (instr, &block->instr_list) {
174 if (is_meta(instr))
175 continue;
176 last = instr;
177 break;
178 }
179
180 /* The delay calc is expecting the instr to not yet be added to the
181 * block, so remove it from the block so that it doesn't get counted
182 * in the distance from assigner:
183 */
184 list_delinit(&last->node);
185
186 unsigned n = ir3_delay_calc(block, last, true);
187
188 if (n != test->expected_delay) {
189 printf("%d: FAIL: Expected delay %u, but got %u, for:\n%s\n", i,
190 test->expected_delay, n, test->asmstr);
191 result = -1;
192 } else {
193 printf("%d: PASS\n", i);
194 }
195
196 ir3_shader_destroy(shader);
197 }
198
199 ir3_compiler_destroy(c);
200
201 return result;
202 }
203