1 /*
2 * Copyright © 2016-2017 Broadcom
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 "broadcom/common/v3d_device_info.h"
25 #include "v3d_compiler.h"
26
27 /* Prints a human-readable description of the uniform reference. */
28 void
vir_dump_uniform(enum quniform_contents contents,uint32_t data)29 vir_dump_uniform(enum quniform_contents contents,
30 uint32_t data)
31 {
32 static const char *quniform_names[] = {
33 [QUNIFORM_LINE_WIDTH] = "line_width",
34 [QUNIFORM_AA_LINE_WIDTH] = "aa_line_width",
35 [QUNIFORM_VIEWPORT_X_SCALE] = "vp_x_scale",
36 [QUNIFORM_VIEWPORT_Y_SCALE] = "vp_y_scale",
37 [QUNIFORM_VIEWPORT_Z_OFFSET] = "vp_z_offset",
38 [QUNIFORM_VIEWPORT_Z_SCALE] = "vp_z_scale",
39 [QUNIFORM_SHARED_OFFSET] = "shared_offset",
40 };
41
42 switch (contents) {
43 case QUNIFORM_CONSTANT:
44 fprintf(stderr, "0x%08x / %f", data, uif(data));
45 break;
46
47 case QUNIFORM_UNIFORM:
48 fprintf(stderr, "push[%d]", data);
49 break;
50
51 case QUNIFORM_TEXTURE_CONFIG_P1:
52 fprintf(stderr, "tex[%d].p1", data);
53 break;
54
55 case QUNIFORM_TMU_CONFIG_P0:
56 fprintf(stderr, "tex[%d].p0 | 0x%x",
57 v3d_unit_data_get_unit(data),
58 v3d_unit_data_get_offset(data));
59 break;
60
61 case QUNIFORM_TMU_CONFIG_P1:
62 fprintf(stderr, "tex[%d].p1 | 0x%x",
63 v3d_unit_data_get_unit(data),
64 v3d_unit_data_get_offset(data));
65 break;
66
67 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
68 fprintf(stderr, "img[%d].p0 | 0x%x",
69 v3d_unit_data_get_unit(data),
70 v3d_unit_data_get_offset(data));
71 break;
72
73 case QUNIFORM_TEXTURE_WIDTH:
74 fprintf(stderr, "tex[%d].width", data);
75 break;
76 case QUNIFORM_TEXTURE_HEIGHT:
77 fprintf(stderr, "tex[%d].height", data);
78 break;
79 case QUNIFORM_TEXTURE_DEPTH:
80 fprintf(stderr, "tex[%d].depth", data);
81 break;
82 case QUNIFORM_TEXTURE_ARRAY_SIZE:
83 fprintf(stderr, "tex[%d].array_size", data);
84 break;
85 case QUNIFORM_TEXTURE_LEVELS:
86 fprintf(stderr, "tex[%d].levels", data);
87 break;
88
89 case QUNIFORM_IMAGE_WIDTH:
90 fprintf(stderr, "img[%d].width", data);
91 break;
92 case QUNIFORM_IMAGE_HEIGHT:
93 fprintf(stderr, "img[%d].height", data);
94 break;
95 case QUNIFORM_IMAGE_DEPTH:
96 fprintf(stderr, "img[%d].depth", data);
97 break;
98 case QUNIFORM_IMAGE_ARRAY_SIZE:
99 fprintf(stderr, "img[%d].array_size", data);
100 break;
101
102 case QUNIFORM_SPILL_OFFSET:
103 fprintf(stderr, "spill_offset");
104 break;
105
106 case QUNIFORM_SPILL_SIZE_PER_THREAD:
107 fprintf(stderr, "spill_size_per_thread");
108 break;
109
110 case QUNIFORM_UBO_ADDR:
111 fprintf(stderr, "ubo[%d]+0x%x",
112 v3d_unit_data_get_unit(data),
113 v3d_unit_data_get_offset(data));
114 break;
115
116 case QUNIFORM_SSBO_OFFSET:
117 fprintf(stderr, "ssbo[%d]", data);
118 break;
119
120 case QUNIFORM_GET_SSBO_SIZE:
121 fprintf(stderr, "ssbo_size[%d]", data);
122 break;
123
124 case QUNIFORM_GET_UBO_SIZE:
125 fprintf(stderr, "ubo_size[%d]", data);
126 break;
127
128 case QUNIFORM_NUM_WORK_GROUPS:
129 fprintf(stderr, "num_wg.%c", data < 3 ? "xyz"[data] : '?');
130 break;
131
132 default:
133 if (quniform_contents_is_texture_p0(contents)) {
134 fprintf(stderr, "tex[%d].p0: 0x%08x",
135 contents - QUNIFORM_TEXTURE_CONFIG_P0_0,
136 data);
137 } else if (contents < ARRAY_SIZE(quniform_names) &&
138 quniform_names[contents]) {
139 fprintf(stderr, "%s",
140 quniform_names[contents]);
141 } else {
142 fprintf(stderr, "%d / 0x%08x", contents, data);
143 }
144 }
145 }
146
147 static void
vir_print_reg(struct v3d_compile * c,const struct qinst * inst,struct qreg reg)148 vir_print_reg(struct v3d_compile *c, const struct qinst *inst,
149 struct qreg reg)
150 {
151 switch (reg.file) {
152
153 case QFILE_NULL:
154 fprintf(stderr, "null");
155 break;
156
157 case QFILE_LOAD_IMM:
158 fprintf(stderr, "0x%08x (%f)", reg.index, uif(reg.index));
159 break;
160
161 case QFILE_REG:
162 fprintf(stderr, "rf%d", reg.index);
163 break;
164
165 case QFILE_MAGIC:
166 fprintf(stderr, "%s",
167 v3d_qpu_magic_waddr_name(c->devinfo, reg.index));
168 break;
169
170 case QFILE_SMALL_IMM: {
171 uint32_t unpacked;
172 bool ok = v3d_qpu_small_imm_unpack(c->devinfo,
173 inst->qpu.raddr_b,
174 &unpacked);
175 assert(ok); (void) ok;
176
177 int8_t *p = (int8_t *)&inst->qpu.raddr_b;
178 if (*p >= -16 && *p <= 15)
179 fprintf(stderr, "%d", unpacked);
180 else
181 fprintf(stderr, "%f", uif(unpacked));
182 break;
183 }
184
185 case QFILE_TEMP:
186 fprintf(stderr, "t%d", reg.index);
187 break;
188 }
189 }
190
191 static void
vir_dump_sig_addr(const struct v3d_device_info * devinfo,const struct v3d_qpu_instr * instr)192 vir_dump_sig_addr(const struct v3d_device_info *devinfo,
193 const struct v3d_qpu_instr *instr)
194 {
195 if (!instr->sig_magic)
196 fprintf(stderr, ".rf%d", instr->sig_addr);
197 else {
198 const char *name =
199 v3d_qpu_magic_waddr_name(devinfo, instr->sig_addr);
200 if (name)
201 fprintf(stderr, ".%s", name);
202 else
203 fprintf(stderr, ".UNKNOWN%d", instr->sig_addr);
204 }
205 }
206
207 static void
vir_dump_sig(struct v3d_compile * c,struct qinst * inst)208 vir_dump_sig(struct v3d_compile *c, struct qinst *inst)
209 {
210 struct v3d_qpu_sig *sig = &inst->qpu.sig;
211
212 if (sig->thrsw)
213 fprintf(stderr, "; thrsw");
214 if (sig->ldvary) {
215 fprintf(stderr, "; ldvary");
216 vir_dump_sig_addr(c->devinfo, &inst->qpu);
217 }
218 if (sig->ldvpm)
219 fprintf(stderr, "; ldvpm");
220 if (sig->ldtmu) {
221 fprintf(stderr, "; ldtmu");
222 vir_dump_sig_addr(c->devinfo, &inst->qpu);
223 }
224 if (sig->ldtlb) {
225 fprintf(stderr, "; ldtlb");
226 vir_dump_sig_addr(c->devinfo, &inst->qpu);
227 }
228 if (sig->ldtlbu) {
229 fprintf(stderr, "; ldtlbu");
230 vir_dump_sig_addr(c->devinfo, &inst->qpu);
231 }
232 if (sig->ldunif)
233 fprintf(stderr, "; ldunif");
234 if (sig->ldunifrf) {
235 fprintf(stderr, "; ldunifrf");
236 vir_dump_sig_addr(c->devinfo, &inst->qpu);
237 }
238 if (sig->ldunifa)
239 fprintf(stderr, "; ldunifa");
240 if (sig->ldunifarf) {
241 fprintf(stderr, "; ldunifarf");
242 vir_dump_sig_addr(c->devinfo, &inst->qpu);
243 }
244 if (sig->wrtmuc)
245 fprintf(stderr, "; wrtmuc");
246 }
247
248 static void
vir_dump_alu(struct v3d_compile * c,struct qinst * inst)249 vir_dump_alu(struct v3d_compile *c, struct qinst *inst)
250 {
251 struct v3d_qpu_instr *instr = &inst->qpu;
252 int nsrc = vir_get_nsrc(inst);
253 enum v3d_qpu_input_unpack unpack[2];
254
255 if (inst->qpu.alu.add.op != V3D_QPU_A_NOP) {
256 fprintf(stderr, "%s", v3d_qpu_add_op_name(instr->alu.add.op));
257 fprintf(stderr, "%s", v3d_qpu_cond_name(instr->flags.ac));
258 fprintf(stderr, "%s", v3d_qpu_pf_name(instr->flags.apf));
259 fprintf(stderr, "%s", v3d_qpu_uf_name(instr->flags.auf));
260 fprintf(stderr, " ");
261
262 vir_print_reg(c, inst, inst->dst);
263 fprintf(stderr, "%s", v3d_qpu_pack_name(instr->alu.add.output_pack));
264
265 unpack[0] = instr->alu.add.a.unpack;
266 unpack[1] = instr->alu.add.b.unpack;
267 } else {
268 fprintf(stderr, "%s", v3d_qpu_mul_op_name(instr->alu.mul.op));
269 fprintf(stderr, "%s", v3d_qpu_cond_name(instr->flags.mc));
270 fprintf(stderr, "%s", v3d_qpu_pf_name(instr->flags.mpf));
271 fprintf(stderr, "%s", v3d_qpu_uf_name(instr->flags.muf));
272 fprintf(stderr, " ");
273
274 vir_print_reg(c, inst, inst->dst);
275 fprintf(stderr, "%s", v3d_qpu_pack_name(instr->alu.mul.output_pack));
276
277 unpack[0] = instr->alu.mul.a.unpack;
278 unpack[1] = instr->alu.mul.b.unpack;
279 }
280
281 for (int i = 0; i < nsrc; i++) {
282 fprintf(stderr, ", ");
283 vir_print_reg(c, inst, inst->src[i]);
284 fprintf(stderr, "%s", v3d_qpu_unpack_name(unpack[i]));
285 }
286
287 vir_dump_sig(c, inst);
288 }
289
290 void
vir_dump_inst(struct v3d_compile * c,struct qinst * inst)291 vir_dump_inst(struct v3d_compile *c, struct qinst *inst)
292 {
293 struct v3d_qpu_instr *instr = &inst->qpu;
294
295 switch (inst->qpu.type) {
296 case V3D_QPU_INSTR_TYPE_ALU:
297 vir_dump_alu(c, inst);
298 break;
299 case V3D_QPU_INSTR_TYPE_BRANCH:
300 fprintf(stderr, "b");
301 if (instr->branch.ub)
302 fprintf(stderr, "u");
303
304 fprintf(stderr, "%s",
305 v3d_qpu_branch_cond_name(instr->branch.cond));
306 fprintf(stderr, "%s", v3d_qpu_msfign_name(instr->branch.msfign));
307
308 switch (instr->branch.bdi) {
309 case V3D_QPU_BRANCH_DEST_ABS:
310 fprintf(stderr, " zero_addr+0x%08x", instr->branch.offset);
311 break;
312
313 case V3D_QPU_BRANCH_DEST_REL:
314 fprintf(stderr, " %d", instr->branch.offset);
315 break;
316
317 case V3D_QPU_BRANCH_DEST_LINK_REG:
318 fprintf(stderr, " lri");
319 break;
320
321 case V3D_QPU_BRANCH_DEST_REGFILE:
322 fprintf(stderr, " rf%d", instr->branch.raddr_a);
323 break;
324 }
325
326 if (instr->branch.ub) {
327 switch (instr->branch.bdu) {
328 case V3D_QPU_BRANCH_DEST_ABS:
329 fprintf(stderr, ", a:unif");
330 break;
331
332 case V3D_QPU_BRANCH_DEST_REL:
333 fprintf(stderr, ", r:unif");
334 break;
335
336 case V3D_QPU_BRANCH_DEST_LINK_REG:
337 fprintf(stderr, ", lri");
338 break;
339
340 case V3D_QPU_BRANCH_DEST_REGFILE:
341 fprintf(stderr, ", rf%d", instr->branch.raddr_a);
342 break;
343 }
344 }
345 break;
346 }
347
348 if (vir_has_uniform(inst)) {
349 fprintf(stderr, " (");
350 vir_dump_uniform(c->uniform_contents[inst->uniform],
351 c->uniform_data[inst->uniform]);
352 fprintf(stderr, ")");
353 }
354 }
355
356 void
vir_dump(struct v3d_compile * c)357 vir_dump(struct v3d_compile *c)
358 {
359 int ip = 0;
360 int pressure = 0;
361
362 vir_for_each_block(block, c) {
363 fprintf(stderr, "BLOCK %d:\n", block->index);
364 vir_for_each_inst(inst, block) {
365 if (c->live_intervals_valid) {
366 for (int i = 0; i < c->num_temps; i++) {
367 if (c->temp_start[i] == ip)
368 pressure++;
369 }
370
371 fprintf(stderr, "P%4d ", pressure);
372
373 bool first = true;
374
375 for (int i = 0; i < c->num_temps; i++) {
376 if (c->temp_start[i] != ip)
377 continue;
378
379 if (first) {
380 first = false;
381 } else {
382 fprintf(stderr, ", ");
383 }
384 if (BITSET_TEST(c->spillable, i))
385 fprintf(stderr, "S%4d", i);
386 else
387 fprintf(stderr, "U%4d", i);
388 }
389
390 if (first)
391 fprintf(stderr, " ");
392 else
393 fprintf(stderr, " ");
394 }
395
396 if (c->live_intervals_valid) {
397 bool first = true;
398
399 for (int i = 0; i < c->num_temps; i++) {
400 if (c->temp_end[i] != ip)
401 continue;
402
403 if (first) {
404 first = false;
405 } else {
406 fprintf(stderr, ", ");
407 }
408 fprintf(stderr, "E%4d", i);
409 pressure--;
410 }
411
412 if (first)
413 fprintf(stderr, " ");
414 else
415 fprintf(stderr, " ");
416 }
417
418 vir_dump_inst(c, inst);
419 fprintf(stderr, "\n");
420 ip++;
421 }
422 if (block->successors[1]) {
423 fprintf(stderr, "-> BLOCK %d, %d\n",
424 block->successors[0]->index,
425 block->successors[1]->index);
426 } else if (block->successors[0]) {
427 fprintf(stderr, "-> BLOCK %d\n",
428 block->successors[0]->index);
429 }
430 }
431 }
432