1 /*
2 * Copyright © 2013 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <assert.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <util/log.h>
14 #include <util/u_debug.h>
15
16 #include "freedreno/isa/ir3-isa.h"
17
18 #include "disasm.h"
19 #include "instr-a3xx.h"
20
21 static enum debug_t debug;
22
23 static const char *levels[] = {
24 "",
25 "\t",
26 "\t\t",
27 "\t\t\t",
28 "\t\t\t\t",
29 "\t\t\t\t\t",
30 "\t\t\t\t\t\t",
31 "\t\t\t\t\t\t\t",
32 "\t\t\t\t\t\t\t\t",
33 "\t\t\t\t\t\t\t\t\t",
34 "x",
35 "x",
36 "x",
37 "x",
38 "x",
39 "x",
40 };
41
42 struct disasm_ctx {
43 FILE *out;
44 struct isa_decode_options *options;
45 unsigned level;
46 unsigned extra_cycles;
47
48 /**
49 * nop_count/has_end used to detect the real end of shader. Since
50 * in some cases there can be a epilogue following an `end` we look
51 * for a sequence of `nop`s following the `end`
52 */
53 int nop_count; /* number of nop's since non-nop instruction: */
54 bool has_end; /* have we seen end instruction */
55
56 int cur_n; /* current instr # */
57 int cur_opc_cat; /* current opc_cat */
58
59 int sfu_delay;
60
61 /**
62 * State accumulated decoding fields of the current instruction,
63 * handled after decoding is complete (ie. at start of next instr)
64 */
65 struct {
66 bool ss;
67 uint8_t nop;
68 uint8_t repeat;
69 } last;
70
71 /**
72 * State accumulated decoding fields of src or dst register
73 */
74 struct {
75 bool half;
76 bool r;
77 enum {
78 FILE_GPR = 1,
79 FILE_CONST = 2,
80 } file;
81 unsigned num;
82 } reg;
83
84 struct shader_stats *stats;
85 };
86
87 static void
print_stats(struct disasm_ctx * ctx)88 print_stats(struct disasm_ctx *ctx)
89 {
90 if (ctx->options->gpu_id >= 600) {
91 /* handle MERGEREGS case.. this isn't *entirely* accurate, as
92 * you can have shader stages not using merged register file,
93 * but it is good enough for a guestimate:
94 */
95 unsigned n = (ctx->stats->halfreg + 1) / 2;
96
97 ctx->stats->halfreg = 0;
98 ctx->stats->fullreg = MAX2(ctx->stats->fullreg, n);
99 }
100
101 unsigned instructions = ctx->cur_n + ctx->extra_cycles + 1;
102
103 fprintf(ctx->out, "%sStats:\n", levels[ctx->level]);
104 fprintf(ctx->out,
105 "%s- shaderdb: %u instr, %u nops, %u non-nops, %u mov, %u cov\n",
106 levels[ctx->level], instructions, ctx->stats->nops,
107 instructions - ctx->stats->nops, ctx->stats->mov_count,
108 ctx->stats->cov_count);
109
110 fprintf(ctx->out,
111 "%s- shaderdb: %u last-baryf, %d half, %d full, %u constlen\n",
112 levels[ctx->level], ctx->stats->last_baryf,
113 DIV_ROUND_UP(ctx->stats->halfreg, 4),
114 DIV_ROUND_UP(ctx->stats->fullreg, 4),
115 DIV_ROUND_UP(ctx->stats->constlen, 4));
116
117 fprintf(
118 ctx->out,
119 "%s- shaderdb: %u cat0, %u cat1, %u cat2, %u cat3, %u cat4, %u cat5, %u cat6, %u cat7\n",
120 levels[ctx->level], ctx->stats->instrs_per_cat[0],
121 ctx->stats->instrs_per_cat[1], ctx->stats->instrs_per_cat[2],
122 ctx->stats->instrs_per_cat[3], ctx->stats->instrs_per_cat[4],
123 ctx->stats->instrs_per_cat[5], ctx->stats->instrs_per_cat[6],
124 ctx->stats->instrs_per_cat[7]);
125
126 fprintf(ctx->out, "%s- shaderdb: %u sstall, %u (ss), %u (sy)\n",
127 levels[ctx->level], ctx->stats->sstall, ctx->stats->ss,
128 ctx->stats->sy);
129 }
130
131 static const struct opc_info {
132 const char *name;
133 } opcs[1 << (3 + NOPC_BITS)] = {
134 #define OPC(cat, opc, name) [(opc)] = {#name}
135 /* clang-format off */
136 /* category 0: */
137 OPC(0, OPC_NOP, nop),
138 OPC(0, OPC_BR, br),
139 OPC(0, OPC_BRAC, brac),
140 OPC(0, OPC_BRAA, braa),
141 OPC(0, OPC_BRAO, brao),
142 OPC(0, OPC_BALL, ball),
143 OPC(0, OPC_BANY, bany),
144 OPC(0, OPC_JUMP, jump),
145 OPC(0, OPC_CALL, call),
146 OPC(0, OPC_RET, ret),
147 OPC(0, OPC_KILL, kill),
148 OPC(0, OPC_DEMOTE, demote),
149 OPC(0, OPC_END, end),
150 OPC(0, OPC_EMIT, emit),
151 OPC(0, OPC_CUT, cut),
152 OPC(0, OPC_CHMASK, chmask),
153 OPC(0, OPC_CHSH, chsh),
154 OPC(0, OPC_FLOW_REV, flow_rev),
155 OPC(0, OPC_PREDT, predt),
156 OPC(0, OPC_PREDF, predf),
157 OPC(0, OPC_PREDE, prede),
158 OPC(0, OPC_BKT, bkt),
159 OPC(0, OPC_STKS, stks),
160 OPC(0, OPC_STKR, stkr),
161 OPC(0, OPC_XSET, xset),
162 OPC(0, OPC_XCLR, xclr),
163 OPC(0, OPC_GETLAST, getlast),
164 OPC(0, OPC_GETONE, getone),
165 OPC(0, OPC_DBG, dbg),
166 OPC(0, OPC_SHPS, shps),
167 OPC(0, OPC_SHPE, shpe),
168
169 /* category 1: */
170 OPC(1, OPC_MOV, ),
171 OPC(1, OPC_MOVMSK, movmsk),
172 OPC(1, OPC_SWZ, swz),
173 OPC(1, OPC_SCT, sct),
174 OPC(1, OPC_GAT, gat),
175 OPC(1, OPC_BALLOT_MACRO, ballot.macro),
176 OPC(1, OPC_ANY_MACRO, any.macro),
177 OPC(1, OPC_ALL_MACRO, all.macro),
178 OPC(1, OPC_ELECT_MACRO, elect.macro),
179 OPC(1, OPC_READ_COND_MACRO, read_cond.macro),
180 OPC(1, OPC_READ_FIRST_MACRO, read_first.macro),
181 OPC(1, OPC_SCAN_MACRO, scan.macro),
182 OPC(1, OPC_SCAN_CLUSTERS_MACRO, scan_clusters.macro),
183 OPC(1, OPC_SHPS_MACRO, shps.macro),
184 OPC(1, OPC_PUSH_CONSTS_LOAD_MACRO, push_consts_load.macro),
185
186 /* category 2: */
187 OPC(2, OPC_ADD_F, add.f),
188 OPC(2, OPC_MIN_F, min.f),
189 OPC(2, OPC_MAX_F, max.f),
190 OPC(2, OPC_MUL_F, mul.f),
191 OPC(2, OPC_SIGN_F, sign.f),
192 OPC(2, OPC_CMPS_F, cmps.f),
193 OPC(2, OPC_ABSNEG_F, absneg.f),
194 OPC(2, OPC_CMPV_F, cmpv.f),
195 OPC(2, OPC_FLOOR_F, floor.f),
196 OPC(2, OPC_CEIL_F, ceil.f),
197 OPC(2, OPC_RNDNE_F, rndne.f),
198 OPC(2, OPC_RNDAZ_F, rndaz.f),
199 OPC(2, OPC_TRUNC_F, trunc.f),
200 OPC(2, OPC_ADD_U, add.u),
201 OPC(2, OPC_ADD_S, add.s),
202 OPC(2, OPC_SUB_U, sub.u),
203 OPC(2, OPC_SUB_S, sub.s),
204 OPC(2, OPC_CMPS_U, cmps.u),
205 OPC(2, OPC_CMPS_S, cmps.s),
206 OPC(2, OPC_MIN_U, min.u),
207 OPC(2, OPC_MIN_S, min.s),
208 OPC(2, OPC_MAX_U, max.u),
209 OPC(2, OPC_MAX_S, max.s),
210 OPC(2, OPC_ABSNEG_S, absneg.s),
211 OPC(2, OPC_AND_B, and.b),
212 OPC(2, OPC_OR_B, or.b),
213 OPC(2, OPC_NOT_B, not.b),
214 OPC(2, OPC_XOR_B, xor.b),
215 OPC(2, OPC_CMPV_U, cmpv.u),
216 OPC(2, OPC_CMPV_S, cmpv.s),
217 OPC(2, OPC_MUL_U24, mul.u24),
218 OPC(2, OPC_MUL_S24, mul.s24),
219 OPC(2, OPC_MULL_U, mull.u),
220 OPC(2, OPC_BFREV_B, bfrev.b),
221 OPC(2, OPC_CLZ_S, clz.s),
222 OPC(2, OPC_CLZ_B, clz.b),
223 OPC(2, OPC_SHL_B, shl.b),
224 OPC(2, OPC_SHR_B, shr.b),
225 OPC(2, OPC_ASHR_B, ashr.b),
226 OPC(2, OPC_BARY_F, bary.f),
227 OPC(2, OPC_MGEN_B, mgen.b),
228 OPC(2, OPC_GETBIT_B, getbit.b),
229 OPC(2, OPC_SETRM, setrm),
230 OPC(2, OPC_CBITS_B, cbits.b),
231 OPC(2, OPC_SHB, shb),
232 OPC(2, OPC_MSAD, msad),
233 OPC(2, OPC_FLAT_B, flat.b),
234
235 /* category 3: */
236 OPC(3, OPC_MAD_U16, mad.u16),
237 OPC(3, OPC_MADSH_U16, madsh.u16),
238 OPC(3, OPC_MAD_S16, mad.s16),
239 OPC(3, OPC_MADSH_M16, madsh.m16),
240 OPC(3, OPC_MAD_U24, mad.u24),
241 OPC(3, OPC_MAD_S24, mad.s24),
242 OPC(3, OPC_MAD_F16, mad.f16),
243 OPC(3, OPC_MAD_F32, mad.f32),
244 OPC(3, OPC_SEL_B16, sel.b16),
245 OPC(3, OPC_SEL_B32, sel.b32),
246 OPC(3, OPC_SEL_S16, sel.s16),
247 OPC(3, OPC_SEL_S32, sel.s32),
248 OPC(3, OPC_SEL_F16, sel.f16),
249 OPC(3, OPC_SEL_F32, sel.f32),
250 OPC(3, OPC_SAD_S16, sad.s16),
251 OPC(3, OPC_SAD_S32, sad.s32),
252 OPC(3, OPC_SHRM, shrm),
253 OPC(3, OPC_SHLM, shlm),
254 OPC(3, OPC_SHRG, shrg),
255 OPC(3, OPC_SHLG, shlg),
256 OPC(3, OPC_ANDG, andg),
257 OPC(3, OPC_DP2ACC, dp2acc),
258 OPC(3, OPC_DP4ACC, dp4acc),
259 OPC(3, OPC_WMM, wmm),
260 OPC(3, OPC_WMM_ACCU, wmm.accu),
261
262 /* category 4: */
263 OPC(4, OPC_RCP, rcp),
264 OPC(4, OPC_RSQ, rsq),
265 OPC(4, OPC_LOG2, log2),
266 OPC(4, OPC_EXP2, exp2),
267 OPC(4, OPC_SIN, sin),
268 OPC(4, OPC_COS, cos),
269 OPC(4, OPC_SQRT, sqrt),
270 OPC(4, OPC_HRSQ, hrsq),
271 OPC(4, OPC_HLOG2, hlog2),
272 OPC(4, OPC_HEXP2, hexp2),
273
274 /* category 5: */
275 OPC(5, OPC_ISAM, isam),
276 OPC(5, OPC_ISAML, isaml),
277 OPC(5, OPC_ISAMM, isamm),
278 OPC(5, OPC_SAM, sam),
279 OPC(5, OPC_SAMB, samb),
280 OPC(5, OPC_SAML, saml),
281 OPC(5, OPC_SAMGQ, samgq),
282 OPC(5, OPC_GETLOD, getlod),
283 OPC(5, OPC_CONV, conv),
284 OPC(5, OPC_CONVM, convm),
285 OPC(5, OPC_GETSIZE, getsize),
286 OPC(5, OPC_GETBUF, getbuf),
287 OPC(5, OPC_GETPOS, getpos),
288 OPC(5, OPC_GETINFO, getinfo),
289 OPC(5, OPC_DSX, dsx),
290 OPC(5, OPC_DSY, dsy),
291 OPC(5, OPC_GATHER4R, gather4r),
292 OPC(5, OPC_GATHER4G, gather4g),
293 OPC(5, OPC_GATHER4B, gather4b),
294 OPC(5, OPC_GATHER4A, gather4a),
295 OPC(5, OPC_SAMGP0, samgp0),
296 OPC(5, OPC_SAMGP1, samgp1),
297 OPC(5, OPC_SAMGP2, samgp2),
298 OPC(5, OPC_SAMGP3, samgp3),
299 OPC(5, OPC_DSXPP_1, dsxpp.1),
300 OPC(5, OPC_DSYPP_1, dsypp.1),
301 OPC(5, OPC_RGETPOS, rgetpos),
302 OPC(5, OPC_RGETINFO, rgetinfo),
303 OPC(5, OPC_BRCST_ACTIVE, brcst.active),
304 OPC(5, OPC_QUAD_SHUFFLE_BRCST, quad_shuffle.brcst),
305 OPC(5, OPC_QUAD_SHUFFLE_HORIZ, quad_shuffle.horiz),
306 OPC(5, OPC_QUAD_SHUFFLE_VERT, quad_shuffle.vert),
307 OPC(5, OPC_QUAD_SHUFFLE_DIAG, quad_shuffle.diag),
308 OPC(5, OPC_TCINV, tcinv),
309 /* macros are needed here for ir3_print */
310 OPC(5, OPC_DSXPP_MACRO, dsxpp.macro),
311 OPC(5, OPC_DSYPP_MACRO, dsypp.macro),
312
313
314 /* category 6: */
315 OPC(6, OPC_LDG, ldg),
316 OPC(6, OPC_LDG_A, ldg.a),
317 OPC(6, OPC_LDL, ldl),
318 OPC(6, OPC_LDP, ldp),
319 OPC(6, OPC_STG, stg),
320 OPC(6, OPC_STG_A, stg.a),
321 OPC(6, OPC_STL, stl),
322 OPC(6, OPC_STP, stp),
323 OPC(6, OPC_LDIB, ldib),
324 OPC(6, OPC_G2L, g2l),
325 OPC(6, OPC_L2G, l2g),
326 OPC(6, OPC_PREFETCH, prefetch),
327 OPC(6, OPC_LDLW, ldlw),
328 OPC(6, OPC_STLW, stlw),
329 OPC(6, OPC_RESFMT, resfmt),
330 OPC(6, OPC_RESINFO, resinfo),
331 OPC(6, OPC_ATOMIC_ADD, atomic.add),
332 OPC(6, OPC_ATOMIC_SUB, atomic.sub),
333 OPC(6, OPC_ATOMIC_XCHG, atomic.xchg),
334 OPC(6, OPC_ATOMIC_INC, atomic.inc),
335 OPC(6, OPC_ATOMIC_DEC, atomic.dec),
336 OPC(6, OPC_ATOMIC_CMPXCHG, atomic.cmpxchg),
337 OPC(6, OPC_ATOMIC_MIN, atomic.min),
338 OPC(6, OPC_ATOMIC_MAX, atomic.max),
339 OPC(6, OPC_ATOMIC_AND, atomic.and),
340 OPC(6, OPC_ATOMIC_OR, atomic.or),
341 OPC(6, OPC_ATOMIC_XOR, atomic.xor),
342 OPC(6, OPC_ATOMIC_B_ADD, atomic.b.add),
343 OPC(6, OPC_ATOMIC_B_SUB, atomic.b.sub),
344 OPC(6, OPC_ATOMIC_B_XCHG, atomic.b.xchg),
345 OPC(6, OPC_ATOMIC_B_INC, atomic.b.inc),
346 OPC(6, OPC_ATOMIC_B_DEC, atomic.b.dec),
347 OPC(6, OPC_ATOMIC_B_CMPXCHG, atomic.b.cmpxchg),
348 OPC(6, OPC_ATOMIC_B_MIN, atomic.b.min),
349 OPC(6, OPC_ATOMIC_B_MAX, atomic.b.max),
350 OPC(6, OPC_ATOMIC_B_AND, atomic.b.and),
351 OPC(6, OPC_ATOMIC_B_OR, atomic.b.or),
352 OPC(6, OPC_ATOMIC_B_XOR, atomic.b.xor),
353 OPC(6, OPC_ATOMIC_S_ADD, atomic.s.add),
354 OPC(6, OPC_ATOMIC_S_SUB, atomic.s.sub),
355 OPC(6, OPC_ATOMIC_S_XCHG, atomic.s.xchg),
356 OPC(6, OPC_ATOMIC_S_INC, atomic.s.inc),
357 OPC(6, OPC_ATOMIC_S_DEC, atomic.s.dec),
358 OPC(6, OPC_ATOMIC_S_CMPXCHG, atomic.s.cmpxchg),
359 OPC(6, OPC_ATOMIC_S_MIN, atomic.s.min),
360 OPC(6, OPC_ATOMIC_S_MAX, atomic.s.max),
361 OPC(6, OPC_ATOMIC_S_AND, atomic.s.and),
362 OPC(6, OPC_ATOMIC_S_OR, atomic.s.or),
363 OPC(6, OPC_ATOMIC_S_XOR, atomic.s.xor),
364 OPC(6, OPC_ATOMIC_G_ADD, atomic.g.add),
365 OPC(6, OPC_ATOMIC_G_SUB, atomic.g.sub),
366 OPC(6, OPC_ATOMIC_G_XCHG, atomic.g.xchg),
367 OPC(6, OPC_ATOMIC_G_INC, atomic.g.inc),
368 OPC(6, OPC_ATOMIC_G_DEC, atomic.g.dec),
369 OPC(6, OPC_ATOMIC_G_CMPXCHG, atomic.g.cmpxchg),
370 OPC(6, OPC_ATOMIC_G_MIN, atomic.g.min),
371 OPC(6, OPC_ATOMIC_G_MAX, atomic.g.max),
372 OPC(6, OPC_ATOMIC_G_AND, atomic.g.and),
373 OPC(6, OPC_ATOMIC_G_OR, atomic.g.or),
374 OPC(6, OPC_ATOMIC_G_XOR, atomic.g.xor),
375 OPC(6, OPC_LDGB, ldgb),
376 OPC(6, OPC_STGB, stgb),
377 OPC(6, OPC_STIB, stib),
378 OPC(6, OPC_LDC, ldc),
379 OPC(6, OPC_LDLV, ldlv),
380 OPC(6, OPC_PIPR, pipr),
381 OPC(6, OPC_PIPC, pipc),
382 OPC(6, OPC_EMIT2, emit),
383 OPC(6, OPC_ENDLS, endls),
384 OPC(6, OPC_GETSPID, getspid),
385 OPC(6, OPC_GETWID, getwid),
386 OPC(6, OPC_GETFIBERID, getfiberid),
387 OPC(6, OPC_STC, stc),
388 OPC(6, OPC_STSC, stsc),
389 OPC(6, OPC_LDC_K, ldc.k),
390 OPC(6, OPC_LDG_K, ldg.k),
391
392 OPC(6, OPC_SPILL_MACRO, spill.macro),
393 OPC(6, OPC_RELOAD_MACRO, reload.macro),
394
395 OPC(7, OPC_BAR, bar),
396 OPC(7, OPC_FENCE, fence),
397 OPC(7, OPC_LOCK, lock),
398 OPC(7, OPC_UNLOCK, unlock),
399 /* clang-format on */
400 #undef OPC
401 };
402
403 const char *
disasm_a3xx_instr_name(opc_t opc)404 disasm_a3xx_instr_name(opc_t opc)
405 {
406 if (opc_cat(opc) == OPC_META)
407 return "??meta??";
408 return opcs[opc].name;
409 }
410
411 static void
disasm_field_cb(void * d,const char * field_name,struct isa_decode_value * val)412 disasm_field_cb(void *d, const char *field_name, struct isa_decode_value *val)
413 {
414 struct disasm_ctx *ctx = d;
415
416 if (!strcmp(field_name, "NAME")) {
417 if (!strcmp("nop", val->str)) {
418 if (ctx->has_end) {
419 ctx->nop_count++;
420 if (ctx->nop_count > 3) {
421 ctx->options->stop = true;
422 }
423 }
424 ctx->stats->nops += 1 + ctx->last.repeat;
425 } else {
426 ctx->nop_count = 0;
427 }
428
429 if (!strcmp("end", val->str)) {
430 ctx->has_end = true;
431 ctx->nop_count = 0;
432 } else if (!strcmp("chsh", val->str)) {
433 ctx->options->stop = true;
434 } else if (!strcmp("bary.f", val->str)) {
435 ctx->stats->last_baryf = ctx->cur_n;
436 }
437 } else if (!strcmp(field_name, "REPEAT")) {
438 ctx->extra_cycles += val->num;
439 ctx->stats->instrs_per_cat[ctx->cur_opc_cat] += val->num;
440 ctx->last.repeat = val->num;
441 } else if (!strcmp(field_name, "NOP")) {
442 ctx->extra_cycles += val->num;
443 ctx->stats->instrs_per_cat[0] += val->num;
444 ctx->stats->nops += val->num;
445 ctx->last.nop = val->num;
446 } else if (!strcmp(field_name, "SY")) {
447 ctx->stats->sy += val->num;
448 } else if (!strcmp(field_name, "SS")) {
449 ctx->stats->ss += val->num;
450 ctx->last.ss = !!val->num;
451 } else if (!strcmp(field_name, "CONST")) {
452 ctx->reg.num = val->num;
453 ctx->reg.file = FILE_CONST;
454 } else if (!strcmp(field_name, "GPR")) {
455 /* don't count GPR regs r48.x (shared) or higher: */
456 if (val->num < 48) {
457 ctx->reg.num = val->num;
458 ctx->reg.file = FILE_GPR;
459 }
460 } else if (!strcmp(field_name, "SRC_R") || !strcmp(field_name, "SRC1_R") ||
461 !strcmp(field_name, "SRC2_R") || !strcmp(field_name, "SRC3_R")) {
462 ctx->reg.r = val->num;
463 } else if (!strcmp(field_name, "DST")) {
464 /* Dest register is always repeated
465 *
466 * Note that this doesn't really properly handle instructions
467 * that write multiple components.. the old disasm didn't handle
468 * that case either.
469 */
470 ctx->reg.r = true;
471 } else if (strstr(field_name, "HALF")) {
472 ctx->reg.half = val->num;
473 } else if (!strcmp(field_name, "SWIZ")) {
474 unsigned num = (ctx->reg.num << 2) | val->num;
475 if (ctx->reg.r)
476 num += ctx->last.repeat;
477
478 if (ctx->reg.file == FILE_CONST) {
479 ctx->stats->constlen = MAX2(ctx->stats->constlen, num);
480 } else if (ctx->reg.file == FILE_GPR) {
481 if (ctx->reg.half) {
482 ctx->stats->halfreg = MAX2(ctx->stats->halfreg, num);
483 } else {
484 ctx->stats->fullreg = MAX2(ctx->stats->fullreg, num);
485 }
486 }
487
488 memset(&ctx->reg, 0, sizeof(ctx->reg));
489 }
490 }
491
492 /**
493 * Handle stat updates dealt with at the end of instruction decoding,
494 * ie. before beginning of next instruction
495 */
496 static void
disasm_handle_last(struct disasm_ctx * ctx)497 disasm_handle_last(struct disasm_ctx *ctx)
498 {
499 if (ctx->last.ss) {
500 ctx->stats->sstall += ctx->sfu_delay;
501 ctx->sfu_delay = 0;
502 }
503
504 if (ctx->cur_opc_cat == 4) {
505 ctx->sfu_delay = 10;
506 } else {
507 int n = MIN2(ctx->sfu_delay, 1 + ctx->last.repeat + ctx->last.nop);
508 ctx->sfu_delay -= n;
509 }
510
511 memset(&ctx->last, 0, sizeof(ctx->last));
512 }
513
514 static void
disasm_instr_cb(void * d,unsigned n,void * instr)515 disasm_instr_cb(void *d, unsigned n, void *instr)
516 {
517 struct disasm_ctx *ctx = d;
518 uint32_t *dwords = (uint32_t *)instr;
519 uint64_t val = dwords[1];
520 val = val << 32;
521 val |= dwords[0];
522
523 unsigned opc_cat = val >> 61;
524
525 /* There are some cases where we can get instr_cb called multiple
526 * times per instruction (like when we need an extra line for branch
527 * target labels), don't update stats in these cases:
528 */
529 if (n != ctx->cur_n) {
530 if (n > 0) {
531 disasm_handle_last(ctx);
532 }
533 ctx->stats->instrs_per_cat[opc_cat]++;
534 ctx->cur_n = n;
535
536 /* mov vs cov stats are a bit harder to fish out of the field
537 * names, because current ir3-cat1.xml doesn't use {NAME} for
538 * this distinction. So for now just handle this case with
539 * some hand-coded parsing:
540 */
541 if (opc_cat == 1) {
542 unsigned opc = (val >> 57) & 0x3;
543 unsigned src_type = (val >> 50) & 0x7;
544 unsigned dst_type = (val >> 46) & 0x7;
545
546 if (opc == 0) {
547 if (src_type == dst_type) {
548 ctx->stats->mov_count++;
549 } else {
550 ctx->stats->cov_count++;
551 }
552 }
553 }
554 }
555
556 ctx->cur_opc_cat = opc_cat;
557
558 if (debug & PRINT_RAW) {
559 fprintf(ctx->out, "%s:%d:%04d:%04d[%08xx_%08xx] ", levels[ctx->level],
560 opc_cat, n, ctx->extra_cycles + n, dwords[1], dwords[0]);
561 }
562 }
563
564 int
disasm_a3xx_stat(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id,struct shader_stats * stats)565 disasm_a3xx_stat(uint32_t *dwords, int sizedwords, int level, FILE *out,
566 unsigned gpu_id, struct shader_stats *stats)
567 {
568 struct isa_decode_options decode_options = {
569 .gpu_id = gpu_id,
570 .show_errors = true,
571 .max_errors = 5,
572 .branch_labels = true,
573 .field_cb = disasm_field_cb,
574 .pre_instr_cb = disasm_instr_cb,
575 };
576 struct disasm_ctx ctx = {
577 .out = out,
578 .level = level,
579 .options = &decode_options,
580 .stats = stats,
581 .cur_n = -1,
582 };
583
584 memset(stats, 0, sizeof(*stats));
585
586 decode_options.cbdata = &ctx;
587
588 ir3_isa_disasm(dwords, sizedwords * 4, out, &decode_options);
589
590 disasm_handle_last(&ctx);
591
592 if (debug & PRINT_STATS)
593 print_stats(&ctx);
594
595 return 0;
596 }
597
598 void
disasm_a3xx_set_debug(enum debug_t d)599 disasm_a3xx_set_debug(enum debug_t d)
600 {
601 debug = d;
602 }
603
604 #include <setjmp.h>
605
606 static bool jmp_env_valid;
607 static jmp_buf jmp_env;
608
609 void
ir3_assert_handler(const char * expr,const char * file,int line,const char * func)610 ir3_assert_handler(const char *expr, const char *file, int line,
611 const char *func)
612 {
613 mesa_loge("%s:%u: %s: Assertion `%s' failed.", file, line, func, expr);
614 if (jmp_env_valid)
615 longjmp(jmp_env, 1);
616 abort();
617 }
618
619 #define TRY(x) \
620 do { \
621 assert(!jmp_env_valid); \
622 if (setjmp(jmp_env) == 0) { \
623 jmp_env_valid = true; \
624 x; \
625 } \
626 jmp_env_valid = false; \
627 } while (0)
628
629 int
disasm_a3xx(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id)630 disasm_a3xx(uint32_t *dwords, int sizedwords, int level, FILE *out,
631 unsigned gpu_id)
632 {
633 struct shader_stats stats;
634 return disasm_a3xx_stat(dwords, sizedwords, level, out, gpu_id, &stats);
635 }
636
637 int
try_disasm_a3xx(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id)638 try_disasm_a3xx(uint32_t *dwords, int sizedwords, int level, FILE *out,
639 unsigned gpu_id)
640 {
641 struct shader_stats stats;
642 int ret = -1;
643 TRY(ret = disasm_a3xx_stat(dwords, sizedwords, level, out, gpu_id, &stats));
644 return ret;
645 }
646