pan/bi: Add preliminary LOAD_UNIFORM implementation
[mesa.git] / src / panfrost / bifrost / bi_print.c
1 /*
2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "bi_print.h"
28
29 const char *
30 bi_output_mod_name(enum bifrost_outmod mod)
31 {
32 switch (mod) {
33 case BIFROST_NONE: return "";
34 case BIFROST_POS: return ".pos";
35 case BIFROST_SAT_SIGNED: return ".sat_signed";
36 case BIFROST_SAT: return ".sat";
37 default: return "invalid";
38 }
39 }
40
41 const char *
42 bi_minmax_mode_name(enum bifrost_minmax_mode mod)
43 {
44 switch (mod) {
45 case BIFROST_MINMAX_NONE: return "";
46 case BIFROST_NAN_WINS: return ".nan_wins";
47 case BIFROST_SRC1_WINS: return ".src1_wins";
48 case BIFROST_SRC0_WINS: return ".src0_wins";
49 default: return "invalid";
50 }
51 }
52
53 const char *
54 bi_round_mode_name(enum bifrost_roundmode mod)
55 {
56 switch (mod) {
57 case BIFROST_RTE: return "";
58 case BIFROST_RTP: return ".rtp";
59 case BIFROST_RTN: return ".rtn";
60 case BIFROST_RTZ: return ".rtz";
61 default: return "invalid";
62 }
63 }
64
65 const char *
66 bi_csel_cond_name(enum bifrost_csel_cond cond)
67 {
68 switch (cond) {
69 case BIFROST_FEQ_F: return "feq.f";
70 case BIFROST_FGT_F: return "fgt.f";
71 case BIFROST_FGE_F: return "fge.f";
72 case BIFROST_IEQ_F: return "ieq.f";
73 case BIFROST_IGT_I: return "igt.i";
74 case BIFROST_IGE_I: return "uge.i";
75 case BIFROST_UGT_I: return "ugt.i";
76 case BIFROST_UGE_I: return "uge.i";
77 default: return "invalid";
78 }
79 }
80
81 const char *
82 bi_interp_mode_name(enum bifrost_interp_mode mode)
83 {
84 switch (mode) {
85 case BIFROST_INTERP_PER_FRAG: return ".per_frag";
86 case BIFROST_INTERP_CENTROID: return ".centroid";
87 case BIFROST_INTERP_DEFAULT: return "";
88 case BIFROST_INTERP_EXPLICIT: return ".explicit";
89 default: return ".unknown";
90 }
91 }
92
93 const char *
94 bi_ldst_type_name(enum bifrost_ldst_type type)
95 {
96 switch (type) {
97 case BIFROST_LDST_F16: return "f16";
98 case BIFROST_LDST_F32: return "f32";
99 case BIFROST_LDST_I32: return "i32";
100 case BIFROST_LDST_U32: return "u32";
101 default: return "invalid";
102 }
103 }
104
105 /* The remaining functions in this file are for IR-internal
106 * structures; the disassembler doesn't use them */
107
108 static const char *
109 bi_class_name(enum bi_class cl)
110 {
111 switch (cl) {
112 case BI_ADD: return "add";
113 case BI_ATEST: return "atest";
114 case BI_BRANCH: return "branch";
115 case BI_CMP: return "cmp";
116 case BI_BLEND: return "blend";
117 case BI_BITWISE: return "bitwise";
118 case BI_CONVERT: return "convert";
119 case BI_CSEL: return "csel";
120 case BI_DISCARD: return "discard";
121 case BI_EXTRACT: return "extract";
122 case BI_FMA: return "fma";
123 case BI_FREXP: return "frexp";
124 case BI_LOAD: return "load";
125 case BI_LOAD_UNIFORM: return "load_uniform";
126 case BI_LOAD_ATTR: return "load_attr";
127 case BI_LOAD_VAR: return "load_var";
128 case BI_LOAD_VAR_ADDRESS: return "load_var_address";
129 case BI_MAKE_VEC: return "make_vec";
130 case BI_MINMAX: return "minmax";
131 case BI_MOV: return "mov";
132 case BI_SHIFT: return "shift";
133 case BI_STORE: return "store";
134 case BI_STORE_VAR: return "store_var";
135 case BI_SPECIAL: return "special";
136 case BI_SWIZZLE: return "swizzle";
137 case BI_TEX: return "tex";
138 case BI_ROUND: return "round";
139 default: return "unknown_class";
140 }
141 }
142
143 static void
144 bi_print_index(FILE *fp, bi_instruction *ins, unsigned index)
145 {
146 if (!index)
147 fprintf(fp, "_");
148 else if (index & BIR_INDEX_REGISTER)
149 fprintf(fp, "br%u", index & ~BIR_INDEX_REGISTER);
150 else if (index & BIR_INDEX_UNIFORM)
151 fprintf(fp, "u%u", index & ~BIR_INDEX_UNIFORM);
152 else if (index & BIR_INDEX_CONSTANT)
153 fprintf(fp, "#0x%" PRIx64, ins->constant.u64);
154 else if (index & BIR_INDEX_ZERO)
155 fprintf(fp, "#0");
156 else if (index & BIR_IS_REG)
157 fprintf(fp, "r%u", index >> 1);
158 else
159 fprintf(fp, "%u", (index >> 1) - 1);
160 }
161
162 static void
163 bi_print_src(FILE *fp, bi_instruction *ins, unsigned s)
164 {
165 unsigned src = ins->src[s];
166 bool mods = bi_has_source_mods(ins);
167 bool abs = ins->src_abs[s] && mods;
168 bool neg = ins->src_neg[s] && mods;
169
170 if (neg)
171 fprintf(fp, "-");
172
173 if (abs)
174 fprintf(fp, "abs(");
175
176 bi_print_index(fp, ins, src);
177
178 if (abs)
179 fprintf(fp, ")");
180 }
181
182 /* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
183
184 static void
185 bi_print_alu_type(nir_alu_type t, FILE *fp)
186 {
187 unsigned size = nir_alu_type_get_type_size(t);
188 nir_alu_type base = nir_alu_type_get_base_type(t);
189
190 switch (base) {
191 case nir_type_int:
192 fprintf(fp, ".i");
193 break;
194 case nir_type_uint:
195 fprintf(fp, ".u");
196 break;
197 case nir_type_bool:
198 fprintf(fp, ".b");
199 break;
200 case nir_type_float:
201 fprintf(fp, ".f");
202 break;
203 default:
204 fprintf(fp, ".unknown");
205 break;
206 }
207
208 fprintf(fp, "%u", size);
209 }
210
211 static void
212 bi_print_swizzle(bi_instruction *ins, FILE *fp)
213 {
214 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
215 unsigned count = 32 / size;
216 assert(size == 8 || size == 16);
217
218 fprintf(fp, ".");
219
220 for (unsigned u = 0; u < count; ++u) {
221 assert(ins->swizzle[u] < size);
222 fputc("xyzw"[ins->swizzle[u]], fp);
223 }
224 }
225
226 static const char *
227 bi_bitwise_op_name(enum bi_bitwise_op op)
228 {
229 switch (op) {
230 case BI_BITWISE_AND: return "and";
231 case BI_BITWISE_OR: return "or";
232 case BI_BITWISE_XOR: return "xor";
233 default: return "invalid";
234 }
235 }
236
237 static void
238 bi_print_load(struct bi_load *load, FILE *fp)
239 {
240 fprintf(fp, ".loc%u", load->location);
241
242 if (load->channels != 1)
243 fprintf(fp, ".v%u", load->channels);
244 }
245
246 static void
247 bi_print_load_vary(struct bi_load_vary *load, FILE *fp)
248 {
249 bi_print_load(&load->load, fp);
250 fprintf(fp, "%s", bi_interp_mode_name(load->interp_mode));
251
252 if (load->reuse)
253 fprintf(fp, ".reuse");
254
255 if (load->flat)
256 fprintf(fp, ".flat");
257 }
258
259 static const char *
260 bi_cond_name(enum bi_cond cond)
261 {
262 switch (cond) {
263 case BI_COND_ALWAYS: return ".always";
264 case BI_COND_LT: return ".lt";
265 case BI_COND_LE: return ".le";
266 case BI_COND_GE: return ".ge";
267 case BI_COND_GT: return ".gt";
268 case BI_COND_EQ: return ".eq";
269 case BI_COND_NE: return ".ne";
270 default: return "invalid";
271 }
272 }
273
274 static void
275 bi_print_branch(struct bi_branch *branch, FILE *fp)
276 {
277 fprintf(fp, "%s", bi_cond_name(branch->cond));
278 }
279
280 void
281 bi_print_instruction(bi_instruction *ins, FILE *fp)
282 {
283 if (ins->type == BI_MINMAX)
284 fprintf(fp, "%s", ins->op.minmax == BI_MINMAX_MIN ? "min" : "max");
285 else if (ins->type == BI_BITWISE)
286 fprintf(fp, "%s", bi_bitwise_op_name(ins->op.bitwise));
287 else if (ins->type == BI_ROUND)
288 fprintf(fp, ins->op.round == BI_ROUND_MODE ? "roundMode": "round");
289 else
290 fprintf(fp, "%s", bi_class_name(ins->type));
291
292 if (ins->type == BI_MINMAX)
293 fprintf(fp, "%s", bi_minmax_mode_name(ins->minmax));
294 else if (ins->type == BI_LOAD_ATTR || ins->type == BI_LOAD_VAR_ADDRESS || ins->type == BI_LOAD_UNIFORM)
295 bi_print_load(&ins->load, fp);
296 else if (ins->type == BI_LOAD_VAR)
297 bi_print_load_vary(&ins->load_vary, fp);
298 else if (ins->type == BI_BRANCH)
299 bi_print_branch(&ins->branch, fp);
300 else if (ins->type == BI_CSEL)
301 fprintf(fp, "%s", bi_cond_name(ins->csel_cond));
302 else if (ins->type == BI_BLEND)
303 fprintf(fp, ".loc%u", ins->blend_location);
304
305 if (ins->dest)
306 bi_print_alu_type(ins->dest_type, fp);
307
308 if (bi_has_outmod(ins))
309 fprintf(fp, "%s", bi_output_mod_name(ins->outmod));
310
311 if (bi_class_props[ins->type] & BI_ROUNDMODE)
312 fprintf(fp, "%s", bi_round_mode_name(ins->roundmode));
313
314 fprintf(fp, " ");
315 bi_print_index(fp, ins, ins->dest);
316 fprintf(fp, ", ");
317
318 bi_foreach_src(ins, s) {
319 bi_print_src(fp, ins, s);
320
321 if (bi_is_src_swizzled(ins, s))
322 bi_print_swizzle(ins, fp);
323
324 bool is_convert = ins->type == BI_CONVERT && s == 0;
325 bool is_branch = ins->type == BI_BRANCH && s < 2 && ins->branch.cond != BI_COND_ALWAYS;
326 bool is_csel = ins->type == BI_CSEL && s < 2;
327
328 if (is_convert || is_branch || is_csel)
329 bi_print_alu_type(ins->src_types[s], fp);
330
331 if (s < BIR_SRC_COUNT)
332 fprintf(fp, ", ");
333 }
334
335 if (ins->type == BI_BRANCH) {
336 if (ins->branch.target)
337 fprintf(fp, "-> block%u", ins->branch.target->name);
338 else
339 fprintf(fp, "-> blockhole");
340 }
341
342 fprintf(fp, "\n");
343 }
344
345 void
346 bi_print_bundle(bi_bundle *bundle, FILE *fp)
347 {
348 bi_instruction *ins[2] = { bundle->fma, bundle->add };
349
350 for (unsigned i = 0; i < 2; ++i) {
351 if (ins[i])
352 bi_print_instruction(ins[i], fp);
353 else
354 fprintf(fp, "nop\n");
355 }
356 }
357
358 void
359 bi_print_clause(bi_clause *clause, FILE *fp)
360 {
361 fprintf(fp, "\tid(%u)", clause->scoreboard_id);
362
363 if (clause->dependencies) {
364 fprintf(fp, ", next-wait(");
365
366 for (unsigned i = 0; i < 8; ++i) {
367 if (clause->dependencies & (1 << i))
368 fprintf(fp, "%u ", i);
369 }
370
371 fprintf(fp, ")");
372 }
373
374 if (!clause->back_to_back)
375 fprintf(fp, " nbb %s", clause->branch_conditional ? "branch-cond" : "branch-uncond");
376
377 if (clause->data_register_write_barrier)
378 fprintf(fp, " drwb");
379
380 fprintf(fp, "\n");
381
382 if (clause->instruction_count) {
383 assert(!clause->bundle_count);
384
385 for (unsigned i = 0; i < clause->instruction_count; ++i)
386 bi_print_instruction(clause->instructions[i], fp);
387 } else {
388 assert(clause->bundle_count);
389
390 for (unsigned i = 0; i < clause->bundle_count; ++i)
391 bi_print_bundle(&clause->bundles[i], fp);
392 }
393
394 if (clause->constant_count) {
395 for (unsigned i = 0; i < clause->constant_count; ++i)
396 fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
397
398 fprintf(fp, "\n");
399 }
400 }
401
402 void
403 bi_print_block(bi_block *block, FILE *fp)
404 {
405 fprintf(fp, "block%u {\n", block->name);
406
407 if (block->scheduled) {
408 bi_foreach_clause_in_block(block, clause)
409 bi_print_clause(clause, fp);
410 } else {
411 bi_foreach_instr_in_block(block, ins)
412 bi_print_instruction(ins, fp);
413 }
414
415 fprintf(fp, "}");
416
417 if (block->successors[0]) {
418 fprintf(fp, " -> ");
419
420 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
421 if (block->successors[i])
422 fprintf(fp, "block%u ", block->successors[i]->name);
423 }
424 }
425
426 if (block->predecessors->entries) {
427 fprintf(fp, " from");
428
429 bi_foreach_predecessor(block, pred)
430 fprintf(fp, " block%u", pred->name);
431 }
432
433 fprintf(fp, "\n\n");
434 }
435
436 void
437 bi_print_shader(bi_context *ctx, FILE *fp)
438 {
439 bi_foreach_block(ctx, block)
440 bi_print_block(block, fp);
441 }