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