aco: flush denormals before fp16 fabs/fneg if needed
[mesa.git] / src / amd / compiler / aco_print_ir.cpp
1 #include "aco_ir.h"
2 #include "aco_builder.h"
3
4 #include "sid.h"
5 #include "ac_shader_util.h"
6
7 namespace aco {
8
9 static const char *reduce_ops[] = {
10 [iadd8] = "iadd8",
11 [iadd16] = "iadd16",
12 [iadd32] = "iadd32",
13 [iadd64] = "iadd64",
14 [imul8] = "imul8",
15 [imul16] = "imul16",
16 [imul32] = "imul32",
17 [imul64] = "imul64",
18 [fadd16] = "fadd16",
19 [fadd32] = "fadd32",
20 [fadd64] = "fadd64",
21 [fmul16] = "fmul16",
22 [fmul32] = "fmul32",
23 [fmul64] = "fmul64",
24 [imin8] = "imin8",
25 [imin16] = "imin16",
26 [imin32] = "imin32",
27 [imin64] = "imin64",
28 [imax8] = "imax8",
29 [imax16] = "imax16",
30 [imax32] = "imax32",
31 [imax64] = "imax64",
32 [umin8] = "umin8",
33 [umin16] = "umin16",
34 [umin32] = "umin32",
35 [umin64] = "umin64",
36 [umax8] = "umax8",
37 [umax16] = "umax16",
38 [umax32] = "umax32",
39 [umax64] = "umax64",
40 [fmin16] = "fmin16",
41 [fmin32] = "fmin32",
42 [fmin64] = "fmin64",
43 [fmax16] = "fmax16",
44 [fmax32] = "fmax32",
45 [fmax64] = "fmax64",
46 [iand8] = "iand8",
47 [iand16] = "iand16",
48 [iand32] = "iand32",
49 [iand64] = "iand64",
50 [ior8] = "ior8",
51 [ior16] = "ior16",
52 [ior32] = "ior32",
53 [ior64] = "ior64",
54 [ixor8] = "ixor8",
55 [ixor16] = "ixor16",
56 [ixor32] = "ixor32",
57 [ixor64] = "ixor64",
58 };
59
60 static void print_reg_class(const RegClass rc, FILE *output)
61 {
62 switch (rc) {
63 case RegClass::s1: fprintf(output, " s1: "); return;
64 case RegClass::s2: fprintf(output, " s2: "); return;
65 case RegClass::s3: fprintf(output, " s3: "); return;
66 case RegClass::s4: fprintf(output, " s4: "); return;
67 case RegClass::s6: fprintf(output, " s6: "); return;
68 case RegClass::s8: fprintf(output, " s8: "); return;
69 case RegClass::s16: fprintf(output, "s16: "); return;
70 case RegClass::v1: fprintf(output, " v1: "); return;
71 case RegClass::v2: fprintf(output, " v2: "); return;
72 case RegClass::v3: fprintf(output, " v3: "); return;
73 case RegClass::v4: fprintf(output, " v4: "); return;
74 case RegClass::v5: fprintf(output, " v5: "); return;
75 case RegClass::v6: fprintf(output, " v6: "); return;
76 case RegClass::v7: fprintf(output, " v7: "); return;
77 case RegClass::v8: fprintf(output, " v8: "); return;
78 case RegClass::v1b: fprintf(output, " v1b: "); return;
79 case RegClass::v2b: fprintf(output, " v2b: "); return;
80 case RegClass::v3b: fprintf(output, " v3b: "); return;
81 case RegClass::v4b: fprintf(output, " v4b: "); return;
82 case RegClass::v6b: fprintf(output, " v6b: "); return;
83 case RegClass::v8b: fprintf(output, " v8b: "); return;
84 case RegClass::v1_linear: fprintf(output, " v1: "); return;
85 case RegClass::v2_linear: fprintf(output, " v2: "); return;
86 }
87 }
88
89 void print_physReg(PhysReg reg, unsigned bytes, FILE *output)
90 {
91 if (reg == 124) {
92 fprintf(output, ":m0");
93 } else if (reg == 106) {
94 fprintf(output, ":vcc");
95 } else if (reg == 253) {
96 fprintf(output, ":scc");
97 } else if (reg == 126) {
98 fprintf(output, ":exec");
99 } else {
100 bool is_vgpr = reg / 256;
101 unsigned r = reg % 256;
102 unsigned size = DIV_ROUND_UP(bytes, 4);
103 fprintf(output, ":%c[%d", is_vgpr ? 'v' : 's', r);
104 if (size > 1)
105 fprintf(output, "-%d]", r + size -1);
106 else
107 fprintf(output, "]");
108 if (reg.byte() || bytes % 4)
109 fprintf(output, "[%d:%d]", reg.byte()*8, (reg.byte()+bytes) * 8);
110 }
111 }
112
113 static void print_constant(uint8_t reg, FILE *output)
114 {
115 if (reg >= 128 && reg <= 192) {
116 fprintf(output, "%d", reg - 128);
117 return;
118 } else if (reg >= 192 && reg <= 208) {
119 fprintf(output, "%d", 192 - reg);
120 return;
121 }
122
123 switch (reg) {
124 case 240:
125 fprintf(output, "0.5");
126 break;
127 case 241:
128 fprintf(output, "-0.5");
129 break;
130 case 242:
131 fprintf(output, "1.0");
132 break;
133 case 243:
134 fprintf(output, "-1.0");
135 break;
136 case 244:
137 fprintf(output, "2.0");
138 break;
139 case 245:
140 fprintf(output, "-2.0");
141 break;
142 case 246:
143 fprintf(output, "4.0");
144 break;
145 case 247:
146 fprintf(output, "-4.0");
147 break;
148 case 248:
149 fprintf(output, "1/(2*PI)");
150 break;
151 }
152 }
153
154 static void print_operand(const Operand *operand, FILE *output)
155 {
156 if (operand->isLiteral() || (operand->isConstant() && operand->bytes() == 1)) {
157 if (operand->bytes() == 1)
158 fprintf(output, "0x%.2x", operand->constantValue());
159 else if (operand->bytes() == 2)
160 fprintf(output, "0x%.4x", operand->constantValue());
161 else
162 fprintf(output, "0x%x", operand->constantValue());
163 } else if (operand->isConstant()) {
164 print_constant(operand->physReg().reg(), output);
165 } else if (operand->isUndefined()) {
166 print_reg_class(operand->regClass(), output);
167 fprintf(output, "undef");
168 } else {
169 if (operand->isLateKill())
170 fprintf(output, "(latekill)");
171
172 fprintf(output, "%%%d", operand->tempId());
173
174 if (operand->isFixed())
175 print_physReg(operand->physReg(), operand->bytes(), output);
176 }
177 }
178
179 static void print_definition(const Definition *definition, FILE *output)
180 {
181 print_reg_class(definition->regClass(), output);
182 if (definition->isPrecise())
183 fprintf(output, "(precise)");
184 fprintf(output, "%%%d", definition->tempId());
185
186 if (definition->isFixed())
187 print_physReg(definition->physReg(), definition->bytes(), output);
188 }
189
190 static void print_barrier_reorder(bool can_reorder, barrier_interaction barrier, FILE *output)
191 {
192 if (can_reorder)
193 fprintf(output, " reorder");
194
195 if (barrier & barrier_buffer)
196 fprintf(output, " buffer");
197 if (barrier & barrier_image)
198 fprintf(output, " image");
199 if (barrier & barrier_atomic)
200 fprintf(output, " atomic");
201 if (barrier & barrier_shared)
202 fprintf(output, " shared");
203 if (barrier & barrier_gs_data)
204 fprintf(output, " gs_data");
205 if (barrier & barrier_gs_sendmsg)
206 fprintf(output, " gs_sendmsg");
207 }
208
209 static void print_instr_format_specific(const Instruction *instr, FILE *output)
210 {
211 switch (instr->format) {
212 case Format::SOPK: {
213 const SOPK_instruction* sopk = static_cast<const SOPK_instruction*>(instr);
214 fprintf(output, " imm:%d", sopk->imm & 0x8000 ? (sopk->imm - 65536) : sopk->imm);
215 break;
216 }
217 case Format::SOPP: {
218 const SOPP_instruction* sopp = static_cast<const SOPP_instruction*>(instr);
219 uint16_t imm = sopp->imm;
220 switch (instr->opcode) {
221 case aco_opcode::s_waitcnt: {
222 /* we usually should check the chip class for vmcnt/lgkm, but
223 * insert_waitcnt() should fill it in regardless. */
224 unsigned vmcnt = (imm & 0xF) | ((imm & (0x3 << 14)) >> 10);
225 if (vmcnt != 63) fprintf(output, " vmcnt(%d)", vmcnt);
226 if (((imm >> 4) & 0x7) < 0x7) fprintf(output, " expcnt(%d)", (imm >> 4) & 0x7);
227 if (((imm >> 8) & 0x3F) < 0x3F) fprintf(output, " lgkmcnt(%d)", (imm >> 8) & 0x3F);
228 break;
229 }
230 case aco_opcode::s_endpgm:
231 case aco_opcode::s_endpgm_saved:
232 case aco_opcode::s_endpgm_ordered_ps_done:
233 case aco_opcode::s_wakeup:
234 case aco_opcode::s_barrier:
235 case aco_opcode::s_icache_inv:
236 case aco_opcode::s_ttracedata:
237 case aco_opcode::s_set_gpr_idx_off: {
238 break;
239 }
240 case aco_opcode::s_sendmsg: {
241 unsigned id = imm & sendmsg_id_mask;
242 switch (id) {
243 case sendmsg_none:
244 fprintf(output, " sendmsg(MSG_NONE)");
245 break;
246 case _sendmsg_gs:
247 fprintf(output, " sendmsg(gs%s%s, %u)",
248 imm & 0x10 ? ", cut" : "", imm & 0x20 ? ", emit" : "", imm >> 8);
249 break;
250 case _sendmsg_gs_done:
251 fprintf(output, " sendmsg(gs_done%s%s, %u)",
252 imm & 0x10 ? ", cut" : "", imm & 0x20 ? ", emit" : "", imm >> 8);
253 break;
254 case sendmsg_save_wave:
255 fprintf(output, " sendmsg(save_wave)");
256 break;
257 case sendmsg_stall_wave_gen:
258 fprintf(output, " sendmsg(stall_wave_gen)");
259 break;
260 case sendmsg_halt_waves:
261 fprintf(output, " sendmsg(halt_waves)");
262 break;
263 case sendmsg_ordered_ps_done:
264 fprintf(output, " sendmsg(ordered_ps_done)");
265 break;
266 case sendmsg_early_prim_dealloc:
267 fprintf(output, " sendmsg(early_prim_dealloc)");
268 break;
269 case sendmsg_gs_alloc_req:
270 fprintf(output, " sendmsg(gs_alloc_req)");
271 break;
272 }
273 break;
274 }
275 default: {
276 if (imm)
277 fprintf(output, " imm:%u", imm);
278 break;
279 }
280 }
281 if (sopp->block != -1)
282 fprintf(output, " block:BB%d", sopp->block);
283 break;
284 }
285 case Format::SMEM: {
286 const SMEM_instruction* smem = static_cast<const SMEM_instruction*>(instr);
287 if (smem->glc)
288 fprintf(output, " glc");
289 if (smem->dlc)
290 fprintf(output, " dlc");
291 if (smem->nv)
292 fprintf(output, " nv");
293 print_barrier_reorder(smem->can_reorder, smem->barrier, output);
294 break;
295 }
296 case Format::VINTRP: {
297 const Interp_instruction* vintrp = static_cast<const Interp_instruction*>(instr);
298 fprintf(output, " attr%d.%c", vintrp->attribute, "xyzw"[vintrp->component]);
299 break;
300 }
301 case Format::DS: {
302 const DS_instruction* ds = static_cast<const DS_instruction*>(instr);
303 if (ds->offset0)
304 fprintf(output, " offset0:%u", ds->offset0);
305 if (ds->offset1)
306 fprintf(output, " offset1:%u", ds->offset1);
307 if (ds->gds)
308 fprintf(output, " gds");
309 break;
310 }
311 case Format::MUBUF: {
312 const MUBUF_instruction* mubuf = static_cast<const MUBUF_instruction*>(instr);
313 if (mubuf->offset)
314 fprintf(output, " offset:%u", mubuf->offset);
315 if (mubuf->offen)
316 fprintf(output, " offen");
317 if (mubuf->idxen)
318 fprintf(output, " idxen");
319 if (mubuf->addr64)
320 fprintf(output, " addr64");
321 if (mubuf->glc)
322 fprintf(output, " glc");
323 if (mubuf->dlc)
324 fprintf(output, " dlc");
325 if (mubuf->slc)
326 fprintf(output, " slc");
327 if (mubuf->tfe)
328 fprintf(output, " tfe");
329 if (mubuf->lds)
330 fprintf(output, " lds");
331 if (mubuf->disable_wqm)
332 fprintf(output, " disable_wqm");
333 print_barrier_reorder(mubuf->can_reorder, mubuf->barrier, output);
334 break;
335 }
336 case Format::MIMG: {
337 const MIMG_instruction* mimg = static_cast<const MIMG_instruction*>(instr);
338 unsigned identity_dmask = !instr->definitions.empty() ?
339 (1 << instr->definitions[0].size()) - 1 :
340 0xf;
341 if ((mimg->dmask & identity_dmask) != identity_dmask)
342 fprintf(output, " dmask:%s%s%s%s",
343 mimg->dmask & 0x1 ? "x" : "",
344 mimg->dmask & 0x2 ? "y" : "",
345 mimg->dmask & 0x4 ? "z" : "",
346 mimg->dmask & 0x8 ? "w" : "");
347 switch (mimg->dim) {
348 case ac_image_1d:
349 fprintf(output, " 1d");
350 break;
351 case ac_image_2d:
352 fprintf(output, " 2d");
353 break;
354 case ac_image_3d:
355 fprintf(output, " 3d");
356 break;
357 case ac_image_cube:
358 fprintf(output, " cube");
359 break;
360 case ac_image_1darray:
361 fprintf(output, " 1darray");
362 break;
363 case ac_image_2darray:
364 fprintf(output, " 2darray");
365 break;
366 case ac_image_2dmsaa:
367 fprintf(output, " 2dmsaa");
368 break;
369 case ac_image_2darraymsaa:
370 fprintf(output, " 2darraymsaa");
371 break;
372 }
373 if (mimg->unrm)
374 fprintf(output, " unrm");
375 if (mimg->glc)
376 fprintf(output, " glc");
377 if (mimg->dlc)
378 fprintf(output, " dlc");
379 if (mimg->slc)
380 fprintf(output, " slc");
381 if (mimg->tfe)
382 fprintf(output, " tfe");
383 if (mimg->da)
384 fprintf(output, " da");
385 if (mimg->lwe)
386 fprintf(output, " lwe");
387 if (mimg->r128 || mimg->a16)
388 fprintf(output, " r128/a16");
389 if (mimg->d16)
390 fprintf(output, " d16");
391 if (mimg->disable_wqm)
392 fprintf(output, " disable_wqm");
393 print_barrier_reorder(mimg->can_reorder, mimg->barrier, output);
394 break;
395 }
396 case Format::EXP: {
397 const Export_instruction* exp = static_cast<const Export_instruction*>(instr);
398 unsigned identity_mask = exp->compressed ? 0x5 : 0xf;
399 if ((exp->enabled_mask & identity_mask) != identity_mask)
400 fprintf(output, " en:%c%c%c%c",
401 exp->enabled_mask & 0x1 ? 'r' : '*',
402 exp->enabled_mask & 0x2 ? 'g' : '*',
403 exp->enabled_mask & 0x4 ? 'b' : '*',
404 exp->enabled_mask & 0x8 ? 'a' : '*');
405 if (exp->compressed)
406 fprintf(output, " compr");
407 if (exp->done)
408 fprintf(output, " done");
409 if (exp->valid_mask)
410 fprintf(output, " vm");
411
412 if (exp->dest <= V_008DFC_SQ_EXP_MRT + 7)
413 fprintf(output, " mrt%d", exp->dest - V_008DFC_SQ_EXP_MRT);
414 else if (exp->dest == V_008DFC_SQ_EXP_MRTZ)
415 fprintf(output, " mrtz");
416 else if (exp->dest == V_008DFC_SQ_EXP_NULL)
417 fprintf(output, " null");
418 else if (exp->dest >= V_008DFC_SQ_EXP_POS && exp->dest <= V_008DFC_SQ_EXP_POS + 3)
419 fprintf(output, " pos%d", exp->dest - V_008DFC_SQ_EXP_POS);
420 else if (exp->dest >= V_008DFC_SQ_EXP_PARAM && exp->dest <= V_008DFC_SQ_EXP_PARAM + 31)
421 fprintf(output, " param%d", exp->dest - V_008DFC_SQ_EXP_PARAM);
422 break;
423 }
424 case Format::PSEUDO_BRANCH: {
425 const Pseudo_branch_instruction* branch = static_cast<const Pseudo_branch_instruction*>(instr);
426 /* Note: BB0 cannot be a branch target */
427 if (branch->target[0] != 0)
428 fprintf(output, " BB%d", branch->target[0]);
429 if (branch->target[1] != 0)
430 fprintf(output, ", BB%d", branch->target[1]);
431 break;
432 }
433 case Format::PSEUDO_REDUCTION: {
434 const Pseudo_reduction_instruction* reduce = static_cast<const Pseudo_reduction_instruction*>(instr);
435 fprintf(output, " op:%s", reduce_ops[reduce->reduce_op]);
436 if (reduce->cluster_size)
437 fprintf(output, " cluster_size:%u", reduce->cluster_size);
438 break;
439 }
440 case Format::FLAT:
441 case Format::GLOBAL:
442 case Format::SCRATCH: {
443 const FLAT_instruction* flat = static_cast<const FLAT_instruction*>(instr);
444 if (flat->offset)
445 fprintf(output, " offset:%u", flat->offset);
446 if (flat->glc)
447 fprintf(output, " glc");
448 if (flat->dlc)
449 fprintf(output, " dlc");
450 if (flat->slc)
451 fprintf(output, " slc");
452 if (flat->lds)
453 fprintf(output, " lds");
454 if (flat->nv)
455 fprintf(output, " nv");
456 if (flat->disable_wqm)
457 fprintf(output, " disable_wqm");
458 print_barrier_reorder(flat->can_reorder, flat->barrier, output);
459 break;
460 }
461 case Format::MTBUF: {
462 const MTBUF_instruction* mtbuf = static_cast<const MTBUF_instruction*>(instr);
463 fprintf(output, " dfmt:");
464 switch (mtbuf->dfmt) {
465 case V_008F0C_BUF_DATA_FORMAT_8: fprintf(output, "8"); break;
466 case V_008F0C_BUF_DATA_FORMAT_16: fprintf(output, "16"); break;
467 case V_008F0C_BUF_DATA_FORMAT_8_8: fprintf(output, "8_8"); break;
468 case V_008F0C_BUF_DATA_FORMAT_32: fprintf(output, "32"); break;
469 case V_008F0C_BUF_DATA_FORMAT_16_16: fprintf(output, "16_16"); break;
470 case V_008F0C_BUF_DATA_FORMAT_10_11_11: fprintf(output, "10_11_11"); break;
471 case V_008F0C_BUF_DATA_FORMAT_11_11_10: fprintf(output, "11_11_10"); break;
472 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2: fprintf(output, "10_10_10_2"); break;
473 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10: fprintf(output, "2_10_10_10"); break;
474 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8: fprintf(output, "8_8_8_8"); break;
475 case V_008F0C_BUF_DATA_FORMAT_32_32: fprintf(output, "32_32"); break;
476 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16: fprintf(output, "16_16_16_16"); break;
477 case V_008F0C_BUF_DATA_FORMAT_32_32_32: fprintf(output, "32_32_32"); break;
478 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32: fprintf(output, "32_32_32_32"); break;
479 case V_008F0C_BUF_DATA_FORMAT_RESERVED_15: fprintf(output, "reserved15"); break;
480 }
481 fprintf(output, " nfmt:");
482 switch (mtbuf->nfmt) {
483 case V_008F0C_BUF_NUM_FORMAT_UNORM: fprintf(output, "unorm"); break;
484 case V_008F0C_BUF_NUM_FORMAT_SNORM: fprintf(output, "snorm"); break;
485 case V_008F0C_BUF_NUM_FORMAT_USCALED: fprintf(output, "uscaled"); break;
486 case V_008F0C_BUF_NUM_FORMAT_SSCALED: fprintf(output, "sscaled"); break;
487 case V_008F0C_BUF_NUM_FORMAT_UINT: fprintf(output, "uint"); break;
488 case V_008F0C_BUF_NUM_FORMAT_SINT: fprintf(output, "sint"); break;
489 case V_008F0C_BUF_NUM_FORMAT_SNORM_OGL: fprintf(output, "snorm"); break;
490 case V_008F0C_BUF_NUM_FORMAT_FLOAT: fprintf(output, "float"); break;
491 }
492 if (mtbuf->offset)
493 fprintf(output, " offset:%u", mtbuf->offset);
494 if (mtbuf->offen)
495 fprintf(output, " offen");
496 if (mtbuf->idxen)
497 fprintf(output, " idxen");
498 if (mtbuf->glc)
499 fprintf(output, " glc");
500 if (mtbuf->dlc)
501 fprintf(output, " dlc");
502 if (mtbuf->slc)
503 fprintf(output, " slc");
504 if (mtbuf->tfe)
505 fprintf(output, " tfe");
506 if (mtbuf->disable_wqm)
507 fprintf(output, " disable_wqm");
508 print_barrier_reorder(mtbuf->can_reorder, mtbuf->barrier, output);
509 break;
510 }
511 case Format::VOP3P: {
512 if (static_cast<const VOP3P_instruction*>(instr)->clamp)
513 fprintf(output, " clamp");
514 break;
515 }
516 default: {
517 break;
518 }
519 }
520 if (instr->isVOP3()) {
521 const VOP3A_instruction* vop3 = static_cast<const VOP3A_instruction*>(instr);
522 switch (vop3->omod) {
523 case 1:
524 fprintf(output, " *2");
525 break;
526 case 2:
527 fprintf(output, " *4");
528 break;
529 case 3:
530 fprintf(output, " *0.5");
531 break;
532 }
533 if (vop3->clamp)
534 fprintf(output, " clamp");
535 if (vop3->opsel & (1 << 3))
536 fprintf(output, " opsel_hi");
537 } else if (instr->isDPP()) {
538 const DPP_instruction* dpp = static_cast<const DPP_instruction*>(instr);
539 if (dpp->dpp_ctrl <= 0xff) {
540 fprintf(output, " quad_perm:[%d,%d,%d,%d]",
541 dpp->dpp_ctrl & 0x3, (dpp->dpp_ctrl >> 2) & 0x3,
542 (dpp->dpp_ctrl >> 4) & 0x3, (dpp->dpp_ctrl >> 6) & 0x3);
543 } else if (dpp->dpp_ctrl >= 0x101 && dpp->dpp_ctrl <= 0x10f) {
544 fprintf(output, " row_shl:%d", dpp->dpp_ctrl & 0xf);
545 } else if (dpp->dpp_ctrl >= 0x111 && dpp->dpp_ctrl <= 0x11f) {
546 fprintf(output, " row_shr:%d", dpp->dpp_ctrl & 0xf);
547 } else if (dpp->dpp_ctrl >= 0x121 && dpp->dpp_ctrl <= 0x12f) {
548 fprintf(output, " row_ror:%d", dpp->dpp_ctrl & 0xf);
549 } else if (dpp->dpp_ctrl == dpp_wf_sl1) {
550 fprintf(output, " wave_shl:1");
551 } else if (dpp->dpp_ctrl == dpp_wf_rl1) {
552 fprintf(output, " wave_rol:1");
553 } else if (dpp->dpp_ctrl == dpp_wf_sr1) {
554 fprintf(output, " wave_shr:1");
555 } else if (dpp->dpp_ctrl == dpp_wf_rr1) {
556 fprintf(output, " wave_ror:1");
557 } else if (dpp->dpp_ctrl == dpp_row_mirror) {
558 fprintf(output, " row_mirror");
559 } else if (dpp->dpp_ctrl == dpp_row_half_mirror) {
560 fprintf(output, " row_half_mirror");
561 } else if (dpp->dpp_ctrl == dpp_row_bcast15) {
562 fprintf(output, " row_bcast:15");
563 } else if (dpp->dpp_ctrl == dpp_row_bcast31) {
564 fprintf(output, " row_bcast:31");
565 } else {
566 fprintf(output, " dpp_ctrl:0x%.3x", dpp->dpp_ctrl);
567 }
568 if (dpp->row_mask != 0xf)
569 fprintf(output, " row_mask:0x%.1x", dpp->row_mask);
570 if (dpp->bank_mask != 0xf)
571 fprintf(output, " bank_mask:0x%.1x", dpp->bank_mask);
572 if (dpp->bound_ctrl)
573 fprintf(output, " bound_ctrl:1");
574 } else if ((int)instr->format & (int)Format::SDWA) {
575 const SDWA_instruction* sdwa = static_cast<const SDWA_instruction*>(instr);
576 switch (sdwa->omod) {
577 case 1:
578 fprintf(output, " *2");
579 break;
580 case 2:
581 fprintf(output, " *4");
582 break;
583 case 3:
584 fprintf(output, " *0.5");
585 break;
586 }
587 if (sdwa->clamp)
588 fprintf(output, " clamp");
589 switch (sdwa->dst_sel & sdwa_asuint) {
590 case sdwa_udword:
591 break;
592 case sdwa_ubyte0:
593 case sdwa_ubyte1:
594 case sdwa_ubyte2:
595 case sdwa_ubyte3:
596 fprintf(output, " dst_sel:%sbyte%u", sdwa->dst_sel & sdwa_sext ? "s" : "u",
597 sdwa->dst_sel & sdwa_bytenum);
598 break;
599 case sdwa_uword0:
600 case sdwa_uword1:
601 fprintf(output, " dst_sel:%sword%u", sdwa->dst_sel & sdwa_sext ? "s" : "u",
602 sdwa->dst_sel & sdwa_wordnum);
603 break;
604 }
605 if (sdwa->dst_preserve)
606 fprintf(output, " dst_preserve");
607 }
608 }
609
610 void aco_print_instr(const Instruction *instr, FILE *output)
611 {
612 if (!instr->definitions.empty()) {
613 for (unsigned i = 0; i < instr->definitions.size(); ++i) {
614 print_definition(&instr->definitions[i], output);
615 if (i + 1 != instr->definitions.size())
616 fprintf(output, ", ");
617 }
618 fprintf(output, " = ");
619 }
620 fprintf(output, "%s", instr_info.name[(int)instr->opcode]);
621 if (instr->operands.size()) {
622 bool abs[instr->operands.size()];
623 bool neg[instr->operands.size()];
624 bool opsel[instr->operands.size()];
625 uint8_t sel[instr->operands.size()];
626 if ((int)instr->format & (int)Format::VOP3A) {
627 const VOP3A_instruction* vop3 = static_cast<const VOP3A_instruction*>(instr);
628 for (unsigned i = 0; i < instr->operands.size(); ++i) {
629 abs[i] = vop3->abs[i];
630 neg[i] = vop3->neg[i];
631 opsel[i] = vop3->opsel & (1 << i);
632 sel[i] = sdwa_udword;
633 }
634 } else if (instr->isDPP()) {
635 const DPP_instruction* dpp = static_cast<const DPP_instruction*>(instr);
636 for (unsigned i = 0; i < instr->operands.size(); ++i) {
637 abs[i] = i < 2 ? dpp->abs[i] : false;
638 neg[i] = i < 2 ? dpp->neg[i] : false;
639 opsel[i] = false;
640 sel[i] = sdwa_udword;
641 }
642 } else if (instr->isSDWA()) {
643 const SDWA_instruction* sdwa = static_cast<const SDWA_instruction*>(instr);
644 for (unsigned i = 0; i < instr->operands.size(); ++i) {
645 abs[i] = i < 2 ? sdwa->abs[i] : false;
646 neg[i] = i < 2 ? sdwa->neg[i] : false;
647 opsel[i] = false;
648 sel[i] = i < 2 ? sdwa->sel[i] : sdwa_udword;
649 }
650 } else {
651 for (unsigned i = 0; i < instr->operands.size(); ++i) {
652 abs[i] = false;
653 neg[i] = false;
654 opsel[i] = false;
655 sel[i] = sdwa_udword;
656 }
657 }
658 for (unsigned i = 0; i < instr->operands.size(); ++i) {
659 if (i)
660 fprintf(output, ", ");
661 else
662 fprintf(output, " ");
663
664 if (neg[i])
665 fprintf(output, "-");
666 if (abs[i])
667 fprintf(output, "|");
668 if (opsel[i])
669 fprintf(output, "hi(");
670 else if (sel[i] & sdwa_sext)
671 fprintf(output, "sext(");
672 print_operand(&instr->operands[i], output);
673 if (opsel[i] || (sel[i] & sdwa_sext))
674 fprintf(output, ")");
675 if (!(sel[i] & sdwa_isra)) {
676 if (sel[i] & sdwa_udword) {
677 /* print nothing */
678 } else if (sel[i] & sdwa_isword) {
679 unsigned index = sel[i] & sdwa_wordnum;
680 fprintf(output, "[%u:%u]", index * 16, index * 16 + 15);
681 } else {
682 unsigned index = sel[i] & sdwa_bytenum;
683 fprintf(output, "[%u:%u]", index * 8, index * 8 + 7);
684 }
685 }
686 if (abs[i])
687 fprintf(output, "|");
688
689 if (instr->format == Format::VOP3P) {
690 const VOP3P_instruction* vop3 = static_cast<const VOP3P_instruction*>(instr);
691 if ((vop3->opsel_lo & (1 << i)) || !(vop3->opsel_hi & (1 << i))) {
692 fprintf(output, ".%c%c",
693 vop3->opsel_lo & (1 << i) ? 'y' : 'x',
694 vop3->opsel_hi & (1 << i) ? 'y' : 'x');
695 }
696 if (vop3->neg_lo[i] && vop3->neg_hi[i])
697 fprintf(output, "*[-1,-1]");
698 else if (vop3->neg_lo[i])
699 fprintf(output, "*[-1,1]");
700 else if (vop3->neg_hi[i])
701 fprintf(output, "*[1,-1]");
702 }
703 }
704 }
705 print_instr_format_specific(instr, output);
706 }
707
708 static void print_block_kind(uint16_t kind, FILE *output)
709 {
710 if (kind & block_kind_uniform)
711 fprintf(output, "uniform, ");
712 if (kind & block_kind_top_level)
713 fprintf(output, "top-level, ");
714 if (kind & block_kind_loop_preheader)
715 fprintf(output, "loop-preheader, ");
716 if (kind & block_kind_loop_header)
717 fprintf(output, "loop-header, ");
718 if (kind & block_kind_loop_exit)
719 fprintf(output, "loop-exit, ");
720 if (kind & block_kind_continue)
721 fprintf(output, "continue, ");
722 if (kind & block_kind_break)
723 fprintf(output, "break, ");
724 if (kind & block_kind_continue_or_break)
725 fprintf(output, "continue_or_break, ");
726 if (kind & block_kind_discard)
727 fprintf(output, "discard, ");
728 if (kind & block_kind_branch)
729 fprintf(output, "branch, ");
730 if (kind & block_kind_merge)
731 fprintf(output, "merge, ");
732 if (kind & block_kind_invert)
733 fprintf(output, "invert, ");
734 if (kind & block_kind_uses_discard_if)
735 fprintf(output, "discard_if, ");
736 if (kind & block_kind_needs_lowering)
737 fprintf(output, "needs_lowering, ");
738 if (kind & block_kind_uses_demote)
739 fprintf(output, "uses_demote, ");
740 if (kind & block_kind_export_end)
741 fprintf(output, "export_end, ");
742 }
743
744 static void print_stage(Stage stage, FILE *output)
745 {
746 fprintf(output, "ACO shader stage: ");
747
748 if (stage == compute_cs)
749 fprintf(output, "compute_cs");
750 else if (stage == fragment_fs)
751 fprintf(output, "fragment_fs");
752 else if (stage == gs_copy_vs)
753 fprintf(output, "gs_copy_vs");
754 else if (stage == vertex_ls)
755 fprintf(output, "vertex_ls");
756 else if (stage == vertex_es)
757 fprintf(output, "vertex_es");
758 else if (stage == vertex_vs)
759 fprintf(output, "vertex_vs");
760 else if (stage == tess_control_hs)
761 fprintf(output, "tess_control_hs");
762 else if (stage == vertex_tess_control_hs)
763 fprintf(output, "vertex_tess_control_hs");
764 else if (stage == tess_eval_es)
765 fprintf(output, "tess_eval_es");
766 else if (stage == tess_eval_vs)
767 fprintf(output, "tess_eval_vs");
768 else if (stage == geometry_gs)
769 fprintf(output, "geometry_gs");
770 else if (stage == vertex_geometry_gs)
771 fprintf(output, "vertex_geometry_gs");
772 else if (stage == tess_eval_geometry_gs)
773 fprintf(output, "tess_eval_geometry_gs");
774 else if (stage == ngg_vertex_gs)
775 fprintf(output, "ngg_vertex_gs");
776 else if (stage == ngg_tess_eval_gs)
777 fprintf(output, "ngg_tess_eval_gs");
778 else if (stage == ngg_vertex_geometry_gs)
779 fprintf(output, "ngg_vertex_geometry_gs");
780 else if (stage == ngg_tess_eval_geometry_gs)
781 fprintf(output, "ngg_tess_eval_geometry_gs");
782 else
783 fprintf(output, "unknown");
784
785 fprintf(output, "\n");
786 }
787
788 void aco_print_block(const Block* block, FILE *output)
789 {
790 fprintf(output, "BB%d\n", block->index);
791 fprintf(output, "/* logical preds: ");
792 for (unsigned pred : block->logical_preds)
793 fprintf(output, "BB%d, ", pred);
794 fprintf(output, "/ linear preds: ");
795 for (unsigned pred : block->linear_preds)
796 fprintf(output, "BB%d, ", pred);
797 fprintf(output, "/ kind: ");
798 print_block_kind(block->kind, output);
799 fprintf(output, "*/\n");
800 for (auto const& instr : block->instructions) {
801 fprintf(output, "\t");
802 aco_print_instr(instr.get(), output);
803 fprintf(output, "\n");
804 }
805 }
806
807 void aco_print_program(const Program *program, FILE *output)
808 {
809 print_stage(program->stage, output);
810
811 for (Block const& block : program->blocks)
812 aco_print_block(&block, output);
813
814 if (program->constant_data.size()) {
815 fprintf(output, "\n/* constant data */\n");
816 for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
817 fprintf(output, "[%06d] ", i);
818 unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
819 for (unsigned j = 0; j < line_size; j += 4) {
820 unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
821 uint32_t v = 0;
822 memcpy(&v, &program->constant_data[i + j], size);
823 fprintf(output, " %08x", v);
824 }
825 fprintf(output, "\n");
826 }
827 }
828
829 fprintf(output, "\n");
830 }
831
832 }