1 /*
2 * Copyright © 2013 Rob Clark <robdclark@gmail.com>
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 OPC(6, OPC_SHFL, shfl),
392
393 OPC(6, OPC_SPILL_MACRO, spill.macro),
394 OPC(6, OPC_RELOAD_MACRO, reload.macro),
395
396 OPC(7, OPC_BAR, bar),
397 OPC(7, OPC_FENCE, fence),
398 OPC(7, OPC_LOCK, lock),
399 OPC(7, OPC_UNLOCK, unlock),
400 /* clang-format on */
401 #undef OPC
402 };
403
404 const char *
disasm_a3xx_instr_name(opc_t opc)405 disasm_a3xx_instr_name(opc_t opc)
406 {
407 if (opc_cat(opc) == OPC_META)
408 return "??meta??";
409 return opcs[opc].name;
410 }
411
412 static void
disasm_field_cb(void * d,const char * field_name,struct isa_decode_value * val)413 disasm_field_cb(void *d, const char *field_name, struct isa_decode_value *val)
414 {
415 struct disasm_ctx *ctx = d;
416
417 if (!strcmp(field_name, "NAME")) {
418 if (!strcmp("nop", val->str)) {
419 if (ctx->has_end) {
420 ctx->nop_count++;
421 if (ctx->nop_count > 3) {
422 ctx->options->stop = true;
423 }
424 }
425 ctx->stats->nops += 1 + ctx->last.repeat;
426 } else {
427 ctx->nop_count = 0;
428 }
429
430 if (!strcmp("end", val->str)) {
431 ctx->has_end = true;
432 ctx->nop_count = 0;
433 } else if (!strcmp("chsh", val->str)) {
434 ctx->options->stop = true;
435 } else if (!strcmp("bary.f", val->str)) {
436 ctx->stats->last_baryf = ctx->cur_n;
437 }
438 } else if (!strcmp(field_name, "REPEAT")) {
439 ctx->extra_cycles += val->num;
440 ctx->stats->instrs_per_cat[ctx->cur_opc_cat] += val->num;
441 ctx->last.repeat = val->num;
442 } else if (!strcmp(field_name, "NOP")) {
443 ctx->extra_cycles += val->num;
444 ctx->stats->instrs_per_cat[0] += val->num;
445 ctx->stats->nops += val->num;
446 ctx->last.nop = val->num;
447 } else if (!strcmp(field_name, "SY")) {
448 ctx->stats->sy += val->num;
449 } else if (!strcmp(field_name, "SS")) {
450 ctx->stats->ss += val->num;
451 ctx->last.ss = !!val->num;
452 } else if (!strcmp(field_name, "CONST")) {
453 ctx->reg.num = val->num;
454 ctx->reg.file = FILE_CONST;
455 } else if (!strcmp(field_name, "GPR")) {
456 /* don't count GPR regs r48.x (shared) or higher: */
457 if (val->num < 48) {
458 ctx->reg.num = val->num;
459 ctx->reg.file = FILE_GPR;
460 }
461 } else if (!strcmp(field_name, "SRC_R") || !strcmp(field_name, "SRC1_R") ||
462 !strcmp(field_name, "SRC2_R") || !strcmp(field_name, "SRC3_R")) {
463 ctx->reg.r = val->num;
464 } else if (!strcmp(field_name, "DST")) {
465 /* Dest register is always repeated
466 *
467 * Note that this doesn't really properly handle instructions
468 * that write multiple components.. the old disasm didn't handle
469 * that case either.
470 */
471 ctx->reg.r = true;
472 } else if (strstr(field_name, "HALF")) {
473 ctx->reg.half = val->num;
474 } else if (!strcmp(field_name, "SWIZ")) {
475 unsigned num = (ctx->reg.num << 2) | val->num;
476 if (ctx->reg.r)
477 num += ctx->last.repeat;
478
479 if (ctx->reg.file == FILE_CONST) {
480 ctx->stats->constlen = MAX2(ctx->stats->constlen, num);
481 } else if (ctx->reg.file == FILE_GPR) {
482 if (ctx->reg.half) {
483 ctx->stats->halfreg = MAX2(ctx->stats->halfreg, num);
484 } else {
485 ctx->stats->fullreg = MAX2(ctx->stats->fullreg, num);
486 }
487 }
488
489 memset(&ctx->reg, 0, sizeof(ctx->reg));
490 }
491 }
492
493 /**
494 * Handle stat updates dealt with at the end of instruction decoding,
495 * ie. before beginning of next instruction
496 */
497 static void
disasm_handle_last(struct disasm_ctx * ctx)498 disasm_handle_last(struct disasm_ctx *ctx)
499 {
500 if (ctx->last.ss) {
501 ctx->stats->sstall += ctx->sfu_delay;
502 ctx->sfu_delay = 0;
503 }
504
505 if (ctx->cur_opc_cat == 4) {
506 ctx->sfu_delay = 10;
507 } else {
508 int n = MIN2(ctx->sfu_delay, 1 + ctx->last.repeat + ctx->last.nop);
509 ctx->sfu_delay -= n;
510 }
511
512 memset(&ctx->last, 0, sizeof(ctx->last));
513 }
514
515 static void
disasm_instr_cb(void * d,unsigned n,void * instr)516 disasm_instr_cb(void *d, unsigned n, void *instr)
517 {
518 struct disasm_ctx *ctx = d;
519 uint32_t *dwords = (uint32_t *)instr;
520 uint64_t val = dwords[1];
521 val = val << 32;
522 val |= dwords[0];
523
524 unsigned opc_cat = val >> 61;
525
526 /* There are some cases where we can get instr_cb called multiple
527 * times per instruction (like when we need an extra line for branch
528 * target labels), don't update stats in these cases:
529 */
530 if (n != ctx->cur_n) {
531 if (n > 0) {
532 disasm_handle_last(ctx);
533 }
534 ctx->stats->instrs_per_cat[opc_cat]++;
535 ctx->cur_n = n;
536
537 /* mov vs cov stats are a bit harder to fish out of the field
538 * names, because current ir3-cat1.xml doesn't use {NAME} for
539 * this distinction. So for now just handle this case with
540 * some hand-coded parsing:
541 */
542 if (opc_cat == 1) {
543 unsigned opc = (val >> 57) & 0x3;
544 unsigned src_type = (val >> 50) & 0x7;
545 unsigned dst_type = (val >> 46) & 0x7;
546
547 if (opc == 0) {
548 if (src_type == dst_type) {
549 ctx->stats->mov_count++;
550 } else {
551 ctx->stats->cov_count++;
552 }
553 }
554 }
555 }
556
557 ctx->cur_opc_cat = opc_cat;
558
559 if (debug & PRINT_RAW) {
560 fprintf(ctx->out, "%s:%d:%04d:%04d[%08xx_%08xx] ", levels[ctx->level],
561 opc_cat, n, ctx->extra_cycles + n, dwords[1], dwords[0]);
562 }
563 }
564
565 int
disasm_a3xx_stat(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id,struct shader_stats * stats)566 disasm_a3xx_stat(uint32_t *dwords, int sizedwords, int level, FILE *out,
567 unsigned gpu_id, struct shader_stats *stats)
568 {
569 struct isa_decode_options decode_options = {
570 .gpu_id = gpu_id,
571 .show_errors = true,
572 .max_errors = 5,
573 .branch_labels = true,
574 .field_cb = disasm_field_cb,
575 .pre_instr_cb = disasm_instr_cb,
576 };
577 struct disasm_ctx ctx = {
578 .out = out,
579 .level = level,
580 .options = &decode_options,
581 .stats = stats,
582 .cur_n = -1,
583 };
584
585 memset(stats, 0, sizeof(*stats));
586
587 decode_options.cbdata = &ctx;
588
589 ir3_isa_disasm(dwords, sizedwords * 4, out, &decode_options);
590
591 disasm_handle_last(&ctx);
592
593 if (debug & PRINT_STATS)
594 print_stats(&ctx);
595
596 return 0;
597 }
598
599 void
disasm_a3xx_set_debug(enum debug_t d)600 disasm_a3xx_set_debug(enum debug_t d)
601 {
602 debug = d;
603 }
604
605 #include <setjmp.h>
606
607 static bool jmp_env_valid;
608 static jmp_buf jmp_env;
609
610 void
ir3_assert_handler(const char * expr,const char * file,int line,const char * func)611 ir3_assert_handler(const char *expr, const char *file, int line,
612 const char *func)
613 {
614 mesa_loge("%s:%u: %s: Assertion `%s' failed.", file, line, func, expr);
615 if (jmp_env_valid)
616 longjmp(jmp_env, 1);
617 abort();
618 }
619
620 #define TRY(x) \
621 do { \
622 assert(!jmp_env_valid); \
623 if (setjmp(jmp_env) == 0) { \
624 jmp_env_valid = true; \
625 x; \
626 } \
627 jmp_env_valid = false; \
628 } while (0)
629
630 int
disasm_a3xx(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id)631 disasm_a3xx(uint32_t *dwords, int sizedwords, int level, FILE *out,
632 unsigned gpu_id)
633 {
634 struct shader_stats stats;
635 return disasm_a3xx_stat(dwords, sizedwords, level, out, gpu_id, &stats);
636 }
637
638 int
try_disasm_a3xx(uint32_t * dwords,int sizedwords,int level,FILE * out,unsigned gpu_id)639 try_disasm_a3xx(uint32_t *dwords, int sizedwords, int level, FILE *out,
640 unsigned gpu_id)
641 {
642 struct shader_stats stats;
643 int ret = -1;
644 TRY(ret = disasm_a3xx_stat(dwords, sizedwords, level, out, gpu_id, &stats));
645 return ret;
646 }
647