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. */
28void
29vir_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
147static void
148vir_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_VPM:
186                fprintf(stderr, "vpm%d.%d",
187                        reg.index / 4, reg.index % 4);
188                break;
189
190        case QFILE_TEMP:
191                fprintf(stderr, "t%d", reg.index);
192                break;
193        }
194}
195
196static void
197vir_dump_sig_addr(const struct v3d_device_info *devinfo,
198                  const struct v3d_qpu_instr *instr)
199{
200        if (devinfo->ver < 41)
201                return;
202
203        if (!instr->sig_magic)
204                fprintf(stderr, ".rf%d", instr->sig_addr);
205        else {
206                const char *name =
207                         v3d_qpu_magic_waddr_name(devinfo, instr->sig_addr);
208                if (name)
209                        fprintf(stderr, ".%s", name);
210                else
211                        fprintf(stderr, ".UNKNOWN%d", instr->sig_addr);
212        }
213}
214
215static void
216vir_dump_sig(struct v3d_compile *c, struct qinst *inst)
217{
218        struct v3d_qpu_sig *sig = &inst->qpu.sig;
219
220        if (sig->thrsw)
221                fprintf(stderr, "; thrsw");
222        if (sig->ldvary) {
223                fprintf(stderr, "; ldvary");
224                vir_dump_sig_addr(c->devinfo, &inst->qpu);
225        }
226        if (sig->ldvpm)
227                fprintf(stderr, "; ldvpm");
228        if (sig->ldtmu) {
229                fprintf(stderr, "; ldtmu");
230                vir_dump_sig_addr(c->devinfo, &inst->qpu);
231        }
232        if (sig->ldtlb) {
233                fprintf(stderr, "; ldtlb");
234                vir_dump_sig_addr(c->devinfo, &inst->qpu);
235        }
236        if (sig->ldtlbu) {
237                fprintf(stderr, "; ldtlbu");
238                vir_dump_sig_addr(c->devinfo, &inst->qpu);
239        }
240        if (sig->ldunif)
241                fprintf(stderr, "; ldunif");
242        if (sig->ldunifrf) {
243                fprintf(stderr, "; ldunifrf");
244                vir_dump_sig_addr(c->devinfo, &inst->qpu);
245        }
246        if (sig->ldunifa)
247                fprintf(stderr, "; ldunifa");
248        if (sig->ldunifarf) {
249                fprintf(stderr, "; ldunifarf");
250                vir_dump_sig_addr(c->devinfo, &inst->qpu);
251        }
252        if (sig->wrtmuc)
253                fprintf(stderr, "; wrtmuc");
254}
255
256static void
257vir_dump_alu(struct v3d_compile *c, struct qinst *inst)
258{
259        struct v3d_qpu_instr *instr = &inst->qpu;
260        int nsrc = vir_get_nsrc(inst);
261        enum v3d_qpu_input_unpack unpack[2];
262
263        if (inst->qpu.alu.add.op != V3D_QPU_A_NOP) {
264                fprintf(stderr, "%s", v3d_qpu_add_op_name(instr->alu.add.op));
265                fprintf(stderr, "%s", v3d_qpu_cond_name(instr->flags.ac));
266                fprintf(stderr, "%s", v3d_qpu_pf_name(instr->flags.apf));
267                fprintf(stderr, "%s", v3d_qpu_uf_name(instr->flags.auf));
268                fprintf(stderr, " ");
269
270                vir_print_reg(c, inst, inst->dst);
271                fprintf(stderr, "%s", v3d_qpu_pack_name(instr->alu.add.output_pack));
272
273                unpack[0] = instr->alu.add.a_unpack;
274                unpack[1] = instr->alu.add.b_unpack;
275        } else {
276                fprintf(stderr, "%s", v3d_qpu_mul_op_name(instr->alu.mul.op));
277                fprintf(stderr, "%s", v3d_qpu_cond_name(instr->flags.mc));
278                fprintf(stderr, "%s", v3d_qpu_pf_name(instr->flags.mpf));
279                fprintf(stderr, "%s", v3d_qpu_uf_name(instr->flags.muf));
280                fprintf(stderr, " ");
281
282                vir_print_reg(c, inst, inst->dst);
283                fprintf(stderr, "%s", v3d_qpu_pack_name(instr->alu.mul.output_pack));
284
285                unpack[0] = instr->alu.mul.a_unpack;
286                unpack[1] = instr->alu.mul.b_unpack;
287        }
288
289        for (int i = 0; i < nsrc; i++) {
290                fprintf(stderr, ", ");
291                vir_print_reg(c, inst, inst->src[i]);
292                fprintf(stderr, "%s", v3d_qpu_unpack_name(unpack[i]));
293        }
294
295        vir_dump_sig(c, inst);
296}
297
298void
299vir_dump_inst(struct v3d_compile *c, struct qinst *inst)
300{
301        struct v3d_qpu_instr *instr = &inst->qpu;
302
303        switch (inst->qpu.type) {
304        case V3D_QPU_INSTR_TYPE_ALU:
305                vir_dump_alu(c, inst);
306                break;
307        case V3D_QPU_INSTR_TYPE_BRANCH:
308                fprintf(stderr, "b");
309                if (instr->branch.ub)
310                        fprintf(stderr, "u");
311
312                fprintf(stderr, "%s",
313                        v3d_qpu_branch_cond_name(instr->branch.cond));
314                fprintf(stderr, "%s", v3d_qpu_msfign_name(instr->branch.msfign));
315
316                switch (instr->branch.bdi) {
317                case V3D_QPU_BRANCH_DEST_ABS:
318                        fprintf(stderr, "  zero_addr+0x%08x", instr->branch.offset);
319                        break;
320
321                case V3D_QPU_BRANCH_DEST_REL:
322                        fprintf(stderr, "  %d", instr->branch.offset);
323                        break;
324
325                case V3D_QPU_BRANCH_DEST_LINK_REG:
326                        fprintf(stderr, "  lri");
327                        break;
328
329                case V3D_QPU_BRANCH_DEST_REGFILE:
330                        fprintf(stderr, "  rf%d", instr->branch.raddr_a);
331                        break;
332                }
333
334                if (instr->branch.ub) {
335                        switch (instr->branch.bdu) {
336                        case V3D_QPU_BRANCH_DEST_ABS:
337                                fprintf(stderr, ", a:unif");
338                                break;
339
340                        case V3D_QPU_BRANCH_DEST_REL:
341                                fprintf(stderr, ", r:unif");
342                                break;
343
344                        case V3D_QPU_BRANCH_DEST_LINK_REG:
345                                fprintf(stderr, ", lri");
346                                break;
347
348                        case V3D_QPU_BRANCH_DEST_REGFILE:
349                                fprintf(stderr, ", rf%d", instr->branch.raddr_a);
350                                break;
351                        }
352                }
353                break;
354        }
355
356        if (vir_has_uniform(inst)) {
357                fprintf(stderr, " (");
358                vir_dump_uniform(c->uniform_contents[inst->uniform],
359                                 c->uniform_data[inst->uniform]);
360                fprintf(stderr, ")");
361        }
362}
363
364void
365vir_dump(struct v3d_compile *c)
366{
367        int ip = 0;
368        int pressure = 0;
369
370        vir_for_each_block(block, c) {
371                fprintf(stderr, "BLOCK %d:\n", block->index);
372                vir_for_each_inst(inst, block) {
373                        if (c->live_intervals_valid) {
374                                for (int i = 0; i < c->num_temps; i++) {
375                                        if (c->temp_start[i] == ip)
376                                                pressure++;
377                                }
378
379                                fprintf(stderr, "P%4d ", pressure);
380
381                                bool first = true;
382
383                                for (int i = 0; i < c->num_temps; i++) {
384                                        if (c->temp_start[i] != ip)
385                                                continue;
386
387                                        if (first) {
388                                                first = false;
389                                        } else {
390                                                fprintf(stderr, ", ");
391                                        }
392                                        if (BITSET_TEST(c->spillable, i))
393                                                fprintf(stderr, "S%4d", i);
394                                        else
395                                                fprintf(stderr, "U%4d", i);
396                                }
397
398                                if (first)
399                                        fprintf(stderr, "      ");
400                                else
401                                        fprintf(stderr, " ");
402                        }
403
404                        if (c->live_intervals_valid) {
405                                bool first = true;
406
407                                for (int i = 0; i < c->num_temps; i++) {
408                                        if (c->temp_end[i] != ip)
409                                                continue;
410
411                                        if (first) {
412                                                first = false;
413                                        } else {
414                                                fprintf(stderr, ", ");
415                                        }
416                                        fprintf(stderr, "E%4d", i);
417                                        pressure--;
418                                }
419
420                                if (first)
421                                        fprintf(stderr, "      ");
422                                else
423                                        fprintf(stderr, " ");
424                        }
425
426                        vir_dump_inst(c, inst);
427                        fprintf(stderr, "\n");
428                        ip++;
429                }
430                if (block->successors[1]) {
431                        fprintf(stderr, "-> BLOCK %d, %d\n",
432                                block->successors[0]->index,
433                                block->successors[1]->index);
434                } else if (block->successors[0]) {
435                        fprintf(stderr, "-> BLOCK %d\n",
436                                block->successors[0]->index);
437                }
438        }
439}
440