freedreno/ir3: Mark ir3_context_error() as NORETURN
[mesa.git] / src / freedreno / ir3 / ir3_context.c
1 /*
2 * Copyright (C) 2015-2018 Rob Clark <robclark@freedesktop.org>
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 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_math.h"
28
29 #include "ir3_compiler.h"
30 #include "ir3_context.h"
31 #include "ir3_image.h"
32 #include "ir3_shader.h"
33 #include "ir3_nir.h"
34
35 struct ir3_context *
36 ir3_context_init(struct ir3_compiler *compiler,
37 struct ir3_shader_variant *so)
38 {
39 struct ir3_context *ctx = rzalloc(NULL, struct ir3_context);
40
41 if (compiler->gpu_id >= 400) {
42 if (so->type == MESA_SHADER_VERTEX) {
43 ctx->astc_srgb = so->key.vastc_srgb;
44 } else if (so->type == MESA_SHADER_FRAGMENT) {
45 ctx->astc_srgb = so->key.fastc_srgb;
46 }
47
48 } else {
49 if (so->type == MESA_SHADER_VERTEX) {
50 ctx->samples = so->key.vsamples;
51 } else if (so->type == MESA_SHADER_FRAGMENT) {
52 ctx->samples = so->key.fsamples;
53 }
54 }
55
56 if (compiler->gpu_id >= 600) {
57 ctx->funcs = &ir3_a6xx_funcs;
58 } else if (compiler->gpu_id >= 400) {
59 ctx->funcs = &ir3_a4xx_funcs;
60 }
61
62 ctx->compiler = compiler;
63 ctx->so = so;
64 ctx->def_ht = _mesa_hash_table_create(ctx,
65 _mesa_hash_pointer, _mesa_key_pointer_equal);
66 ctx->block_ht = _mesa_hash_table_create(ctx,
67 _mesa_hash_pointer, _mesa_key_pointer_equal);
68
69 /* TODO: maybe generate some sort of bitmask of what key
70 * lowers vs what shader has (ie. no need to lower
71 * texture clamp lowering if no texture sample instrs)..
72 * although should be done further up the stack to avoid
73 * creating duplicate variants..
74 */
75
76 if (ir3_key_lowers_nir(&so->key)) {
77 nir_shader *s = nir_shader_clone(ctx, so->shader->nir);
78 ctx->s = ir3_optimize_nir(so->shader, s, &so->key);
79 } else {
80 /* fast-path for shader key that lowers nothing in NIR: */
81 ctx->s = nir_shader_clone(ctx, so->shader->nir);
82 }
83
84 /* this needs to be the last pass run, so do this here instead of
85 * in ir3_optimize_nir():
86 */
87 NIR_PASS_V(ctx->s, nir_lower_bool_to_int32);
88 NIR_PASS_V(ctx->s, nir_lower_locals_to_regs);
89 NIR_PASS_V(ctx->s, nir_convert_from_ssa, true);
90
91 if (ir3_shader_debug & IR3_DBG_DISASM) {
92 DBG("dump nir%dv%d: type=%d, k={cts=%u,hp=%u}",
93 so->shader->id, so->id, so->type,
94 so->key.color_two_side, so->key.half_precision);
95 nir_print_shader(ctx->s, stdout);
96 }
97
98 if (shader_debug_enabled(so->type)) {
99 fprintf(stderr, "NIR (final form) for %s shader:\n",
100 _mesa_shader_stage_to_string(so->type));
101 nir_print_shader(ctx->s, stderr);
102 }
103
104 ir3_nir_scan_driver_consts(ctx->s, &so->const_layout);
105
106 so->num_uniforms = ctx->s->num_uniforms;
107 so->num_ubos = ctx->s->info.num_ubos;
108
109 ir3_ibo_mapping_init(&so->image_mapping, ctx->s->info.num_textures);
110
111 /* Layout of constant registers, each section aligned to vec4. Note
112 * that pointer size (ubo, etc) changes depending on generation.
113 *
114 * user consts
115 * UBO addresses
116 * SSBO sizes
117 * if (vertex shader) {
118 * driver params (IR3_DP_*)
119 * if (stream_output.num_outputs > 0)
120 * stream-out addresses
121 * }
122 * immediates
123 *
124 * Immediates go last mostly because they are inserted in the CP pass
125 * after the nir -> ir3 frontend.
126 */
127 unsigned constoff = align(ctx->so->shader->ubo_state.size / 16, 4);
128 unsigned ptrsz = ir3_pointer_size(ctx);
129
130 memset(&so->constbase, ~0, sizeof(so->constbase));
131
132 if (so->num_ubos > 0) {
133 so->constbase.ubo = constoff;
134 constoff += align(ctx->s->info.num_ubos * ptrsz, 4) / 4;
135 }
136
137 if (so->const_layout.ssbo_size.count > 0) {
138 unsigned cnt = so->const_layout.ssbo_size.count;
139 so->constbase.ssbo_sizes = constoff;
140 constoff += align(cnt, 4) / 4;
141 }
142
143 if (so->const_layout.image_dims.count > 0) {
144 unsigned cnt = so->const_layout.image_dims.count;
145 so->constbase.image_dims = constoff;
146 constoff += align(cnt, 4) / 4;
147 }
148
149 unsigned num_driver_params = 0;
150 if (so->type == MESA_SHADER_VERTEX) {
151 num_driver_params = IR3_DP_VS_COUNT;
152 } else if (so->type == MESA_SHADER_COMPUTE) {
153 num_driver_params = IR3_DP_CS_COUNT;
154 }
155
156 so->constbase.driver_param = constoff;
157 constoff += align(num_driver_params, 4) / 4;
158
159 if ((so->type == MESA_SHADER_VERTEX) &&
160 (compiler->gpu_id < 500) &&
161 so->shader->stream_output.num_outputs > 0) {
162 so->constbase.tfbo = constoff;
163 constoff += align(IR3_MAX_SO_BUFFERS * ptrsz, 4) / 4;
164 }
165
166 so->constbase.immediate = constoff;
167
168 return ctx;
169 }
170
171 void
172 ir3_context_free(struct ir3_context *ctx)
173 {
174 ralloc_free(ctx);
175 }
176
177 /*
178 * Misc helpers
179 */
180
181 /* allocate a n element value array (to be populated by caller) and
182 * insert in def_ht
183 */
184 struct ir3_instruction **
185 ir3_get_dst_ssa(struct ir3_context *ctx, nir_ssa_def *dst, unsigned n)
186 {
187 struct ir3_instruction **value =
188 ralloc_array(ctx->def_ht, struct ir3_instruction *, n);
189 _mesa_hash_table_insert(ctx->def_ht, dst, value);
190 return value;
191 }
192
193 struct ir3_instruction **
194 ir3_get_dst(struct ir3_context *ctx, nir_dest *dst, unsigned n)
195 {
196 struct ir3_instruction **value;
197
198 if (dst->is_ssa) {
199 value = ir3_get_dst_ssa(ctx, &dst->ssa, n);
200 } else {
201 value = ralloc_array(ctx, struct ir3_instruction *, n);
202 }
203
204 /* NOTE: in non-ssa case, we don't really need to store last_dst
205 * but this helps us catch cases where put_dst() call is forgotten
206 */
207 compile_assert(ctx, !ctx->last_dst);
208 ctx->last_dst = value;
209 ctx->last_dst_n = n;
210
211 return value;
212 }
213
214 struct ir3_instruction * const *
215 ir3_get_src(struct ir3_context *ctx, nir_src *src)
216 {
217 if (src->is_ssa) {
218 struct hash_entry *entry;
219 entry = _mesa_hash_table_search(ctx->def_ht, src->ssa);
220 compile_assert(ctx, entry);
221 return entry->data;
222 } else {
223 nir_register *reg = src->reg.reg;
224 struct ir3_array *arr = ir3_get_array(ctx, reg);
225 unsigned num_components = arr->r->num_components;
226 struct ir3_instruction *addr = NULL;
227 struct ir3_instruction **value =
228 ralloc_array(ctx, struct ir3_instruction *, num_components);
229
230 if (src->reg.indirect)
231 addr = ir3_get_addr(ctx, ir3_get_src(ctx, src->reg.indirect)[0],
232 reg->num_components);
233
234 for (unsigned i = 0; i < num_components; i++) {
235 unsigned n = src->reg.base_offset * reg->num_components + i;
236 compile_assert(ctx, n < arr->length);
237 value[i] = ir3_create_array_load(ctx, arr, n, addr);
238 }
239
240 return value;
241 }
242 }
243
244 void
245 ir3_put_dst(struct ir3_context *ctx, nir_dest *dst)
246 {
247 unsigned bit_size = nir_dest_bit_size(*dst);
248
249 /* add extra mov if dst value is HIGH reg.. in some cases not all
250 * instructions can read from HIGH regs, in cases where they can
251 * ir3_cp will clean up the extra mov:
252 */
253 for (unsigned i = 0; i < ctx->last_dst_n; i++) {
254 if (!ctx->last_dst[i])
255 continue;
256 if (ctx->last_dst[i]->regs[0]->flags & IR3_REG_HIGH) {
257 ctx->last_dst[i] = ir3_MOV(ctx->block, ctx->last_dst[i], TYPE_U32);
258 }
259 }
260
261 if (bit_size < 32) {
262 for (unsigned i = 0; i < ctx->last_dst_n; i++) {
263 struct ir3_instruction *dst = ctx->last_dst[i];
264 dst->regs[0]->flags |= IR3_REG_HALF;
265 if (ctx->last_dst[i]->opc == OPC_META_FO)
266 dst->regs[1]->instr->regs[0]->flags |= IR3_REG_HALF;
267 }
268 }
269
270 if (!dst->is_ssa) {
271 nir_register *reg = dst->reg.reg;
272 struct ir3_array *arr = ir3_get_array(ctx, reg);
273 unsigned num_components = ctx->last_dst_n;
274 struct ir3_instruction *addr = NULL;
275
276 if (dst->reg.indirect)
277 addr = ir3_get_addr(ctx, ir3_get_src(ctx, dst->reg.indirect)[0],
278 reg->num_components);
279
280 for (unsigned i = 0; i < num_components; i++) {
281 unsigned n = dst->reg.base_offset * reg->num_components + i;
282 compile_assert(ctx, n < arr->length);
283 if (!ctx->last_dst[i])
284 continue;
285 ir3_create_array_store(ctx, arr, n, ctx->last_dst[i], addr);
286 }
287
288 ralloc_free(ctx->last_dst);
289 }
290
291 ctx->last_dst = NULL;
292 ctx->last_dst_n = 0;
293 }
294
295 struct ir3_instruction *
296 ir3_create_collect(struct ir3_context *ctx, struct ir3_instruction *const *arr,
297 unsigned arrsz)
298 {
299 struct ir3_block *block = ctx->block;
300 struct ir3_instruction *collect;
301
302 if (arrsz == 0)
303 return NULL;
304
305 unsigned flags = arr[0]->regs[0]->flags & IR3_REG_HALF;
306
307 collect = ir3_instr_create2(block, OPC_META_FI, 1 + arrsz);
308 ir3_reg_create(collect, 0, flags); /* dst */
309 for (unsigned i = 0; i < arrsz; i++) {
310 struct ir3_instruction *elem = arr[i];
311
312 /* Since arrays are pre-colored in RA, we can't assume that
313 * things will end up in the right place. (Ie. if a collect
314 * joins elements from two different arrays.) So insert an
315 * extra mov.
316 *
317 * We could possibly skip this if all the collected elements
318 * are contiguous elements in a single array.. not sure how
319 * likely that is to happen.
320 *
321 * Fixes a problem with glamor shaders, that in effect do
322 * something like:
323 *
324 * if (foo)
325 * texcoord = ..
326 * else
327 * texcoord = ..
328 * color = texture2D(tex, texcoord);
329 *
330 * In this case, texcoord will end up as nir registers (which
331 * translate to ir3 array's of length 1. And we can't assume
332 * the two (or more) arrays will get allocated in consecutive
333 * scalar registers.
334 *
335 */
336 if (elem->regs[0]->flags & IR3_REG_ARRAY) {
337 type_t type = (flags & IR3_REG_HALF) ? TYPE_U16 : TYPE_U32;
338 elem = ir3_MOV(block, elem, type);
339 }
340
341 compile_assert(ctx, (elem->regs[0]->flags & IR3_REG_HALF) == flags);
342 ir3_reg_create(collect, 0, IR3_REG_SSA | flags)->instr = elem;
343 }
344
345 collect->regs[0]->wrmask = MASK(arrsz);
346
347 return collect;
348 }
349
350 /* helper for instructions that produce multiple consecutive scalar
351 * outputs which need to have a split/fanout meta instruction inserted
352 */
353 void
354 ir3_split_dest(struct ir3_block *block, struct ir3_instruction **dst,
355 struct ir3_instruction *src, unsigned base, unsigned n)
356 {
357 struct ir3_instruction *prev = NULL;
358
359 if ((n == 1) && (src->regs[0]->wrmask == 0x1)) {
360 dst[0] = src;
361 return;
362 }
363
364 unsigned flags = src->regs[0]->flags & (IR3_REG_HALF | IR3_REG_HIGH);
365
366 for (int i = 0, j = 0; i < n; i++) {
367 struct ir3_instruction *split = ir3_instr_create(block, OPC_META_FO);
368 ir3_reg_create(split, 0, IR3_REG_SSA | flags);
369 ir3_reg_create(split, 0, IR3_REG_SSA | flags)->instr = src;
370 split->fo.off = i + base;
371
372 if (prev) {
373 split->cp.left = prev;
374 split->cp.left_cnt++;
375 prev->cp.right = split;
376 prev->cp.right_cnt++;
377 }
378 prev = split;
379
380 if (src->regs[0]->wrmask & (1 << (i + base)))
381 dst[j++] = split;
382 }
383 }
384
385 NORETURN void
386 ir3_context_error(struct ir3_context *ctx, const char *format, ...)
387 {
388 struct hash_table *errors = NULL;
389 va_list ap;
390 va_start(ap, format);
391 if (ctx->cur_instr) {
392 errors = _mesa_hash_table_create(NULL,
393 _mesa_hash_pointer,
394 _mesa_key_pointer_equal);
395 char *msg = ralloc_vasprintf(errors, format, ap);
396 _mesa_hash_table_insert(errors, ctx->cur_instr, msg);
397 } else {
398 _debug_vprintf(format, ap);
399 }
400 va_end(ap);
401 nir_print_shader_annotated(ctx->s, stdout, errors);
402 ralloc_free(errors);
403 ctx->error = true;
404 unreachable("");
405 }
406
407 static struct ir3_instruction *
408 create_addr(struct ir3_block *block, struct ir3_instruction *src, int align)
409 {
410 struct ir3_instruction *instr, *immed;
411
412 /* TODO in at least some cases, the backend could probably be
413 * made clever enough to propagate IR3_REG_HALF..
414 */
415 instr = ir3_COV(block, src, TYPE_U32, TYPE_S16);
416 instr->regs[0]->flags |= IR3_REG_HALF;
417
418 switch(align){
419 case 1:
420 /* src *= 1: */
421 break;
422 case 2:
423 /* src *= 2 => src <<= 1: */
424 immed = create_immed(block, 1);
425 immed->regs[0]->flags |= IR3_REG_HALF;
426
427 instr = ir3_SHL_B(block, instr, 0, immed, 0);
428 instr->regs[0]->flags |= IR3_REG_HALF;
429 instr->regs[1]->flags |= IR3_REG_HALF;
430 break;
431 case 3:
432 /* src *= 3: */
433 immed = create_immed(block, 3);
434 immed->regs[0]->flags |= IR3_REG_HALF;
435
436 instr = ir3_MULL_U(block, instr, 0, immed, 0);
437 instr->regs[0]->flags |= IR3_REG_HALF;
438 instr->regs[1]->flags |= IR3_REG_HALF;
439 break;
440 case 4:
441 /* src *= 4 => src <<= 2: */
442 immed = create_immed(block, 2);
443 immed->regs[0]->flags |= IR3_REG_HALF;
444
445 instr = ir3_SHL_B(block, instr, 0, immed, 0);
446 instr->regs[0]->flags |= IR3_REG_HALF;
447 instr->regs[1]->flags |= IR3_REG_HALF;
448 break;
449 default:
450 unreachable("bad align");
451 return NULL;
452 }
453
454 instr = ir3_MOV(block, instr, TYPE_S16);
455 instr->regs[0]->num = regid(REG_A0, 0);
456 instr->regs[0]->flags |= IR3_REG_HALF;
457 instr->regs[1]->flags |= IR3_REG_HALF;
458
459 return instr;
460 }
461
462 /* caches addr values to avoid generating multiple cov/shl/mova
463 * sequences for each use of a given NIR level src as address
464 */
465 struct ir3_instruction *
466 ir3_get_addr(struct ir3_context *ctx, struct ir3_instruction *src, int align)
467 {
468 struct ir3_instruction *addr;
469 unsigned idx = align - 1;
470
471 compile_assert(ctx, idx < ARRAY_SIZE(ctx->addr_ht));
472
473 if (!ctx->addr_ht[idx]) {
474 ctx->addr_ht[idx] = _mesa_hash_table_create(ctx,
475 _mesa_hash_pointer, _mesa_key_pointer_equal);
476 } else {
477 struct hash_entry *entry;
478 entry = _mesa_hash_table_search(ctx->addr_ht[idx], src);
479 if (entry)
480 return entry->data;
481 }
482
483 addr = create_addr(ctx->block, src, align);
484 _mesa_hash_table_insert(ctx->addr_ht[idx], src, addr);
485
486 return addr;
487 }
488
489 struct ir3_instruction *
490 ir3_get_predicate(struct ir3_context *ctx, struct ir3_instruction *src)
491 {
492 struct ir3_block *b = ctx->block;
493 struct ir3_instruction *cond;
494
495 /* NOTE: only cmps.*.* can write p0.x: */
496 cond = ir3_CMPS_S(b, src, 0, create_immed(b, 0), 0);
497 cond->cat2.condition = IR3_COND_NE;
498
499 /* condition always goes in predicate register: */
500 cond->regs[0]->num = regid(REG_P0, 0);
501
502 return cond;
503 }
504
505 /*
506 * Array helpers
507 */
508
509 void
510 ir3_declare_array(struct ir3_context *ctx, nir_register *reg)
511 {
512 struct ir3_array *arr = rzalloc(ctx, struct ir3_array);
513 arr->id = ++ctx->num_arrays;
514 /* NOTE: sometimes we get non array regs, for example for arrays of
515 * length 1. See fs-const-array-of-struct-of-array.shader_test. So
516 * treat a non-array as if it was an array of length 1.
517 *
518 * It would be nice if there was a nir pass to convert arrays of
519 * length 1 to ssa.
520 */
521 arr->length = reg->num_components * MAX2(1, reg->num_array_elems);
522 compile_assert(ctx, arr->length > 0);
523 arr->r = reg;
524 list_addtail(&arr->node, &ctx->ir->array_list);
525 }
526
527 struct ir3_array *
528 ir3_get_array(struct ir3_context *ctx, nir_register *reg)
529 {
530 list_for_each_entry (struct ir3_array, arr, &ctx->ir->array_list, node) {
531 if (arr->r == reg)
532 return arr;
533 }
534 ir3_context_error(ctx, "bogus reg: %s\n", reg->name);
535 return NULL;
536 }
537
538 /* relative (indirect) if address!=NULL */
539 struct ir3_instruction *
540 ir3_create_array_load(struct ir3_context *ctx, struct ir3_array *arr, int n,
541 struct ir3_instruction *address)
542 {
543 struct ir3_block *block = ctx->block;
544 struct ir3_instruction *mov;
545 struct ir3_register *src;
546
547 mov = ir3_instr_create(block, OPC_MOV);
548 mov->cat1.src_type = TYPE_U32;
549 mov->cat1.dst_type = TYPE_U32;
550 mov->barrier_class = IR3_BARRIER_ARRAY_R;
551 mov->barrier_conflict = IR3_BARRIER_ARRAY_W;
552 ir3_reg_create(mov, 0, 0);
553 src = ir3_reg_create(mov, 0, IR3_REG_ARRAY |
554 COND(address, IR3_REG_RELATIV));
555 src->instr = arr->last_write;
556 src->size = arr->length;
557 src->array.id = arr->id;
558 src->array.offset = n;
559
560 if (address)
561 ir3_instr_set_address(mov, address);
562
563 return mov;
564 }
565
566 /* relative (indirect) if address!=NULL */
567 void
568 ir3_create_array_store(struct ir3_context *ctx, struct ir3_array *arr, int n,
569 struct ir3_instruction *src, struct ir3_instruction *address)
570 {
571 struct ir3_block *block = ctx->block;
572 struct ir3_instruction *mov;
573 struct ir3_register *dst;
574
575 /* if not relative store, don't create an extra mov, since that
576 * ends up being difficult for cp to remove.
577 */
578 if (!address) {
579 dst = src->regs[0];
580
581 src->barrier_class |= IR3_BARRIER_ARRAY_W;
582 src->barrier_conflict |= IR3_BARRIER_ARRAY_R | IR3_BARRIER_ARRAY_W;
583
584 dst->flags |= IR3_REG_ARRAY;
585 dst->instr = arr->last_write;
586 dst->size = arr->length;
587 dst->array.id = arr->id;
588 dst->array.offset = n;
589
590 arr->last_write = src;
591
592 array_insert(block, block->keeps, src);
593
594 return;
595 }
596
597 mov = ir3_instr_create(block, OPC_MOV);
598 mov->cat1.src_type = TYPE_U32;
599 mov->cat1.dst_type = TYPE_U32;
600 mov->barrier_class = IR3_BARRIER_ARRAY_W;
601 mov->barrier_conflict = IR3_BARRIER_ARRAY_R | IR3_BARRIER_ARRAY_W;
602 dst = ir3_reg_create(mov, 0, IR3_REG_ARRAY |
603 COND(address, IR3_REG_RELATIV));
604 dst->instr = arr->last_write;
605 dst->size = arr->length;
606 dst->array.id = arr->id;
607 dst->array.offset = n;
608 ir3_reg_create(mov, 0, IR3_REG_SSA)->instr = src;
609
610 if (address)
611 ir3_instr_set_address(mov, address);
612
613 arr->last_write = mov;
614
615 /* the array store may only matter to something in an earlier
616 * block (ie. loops), but since arrays are not in SSA, depth
617 * pass won't know this.. so keep all array stores:
618 */
619 array_insert(block, block->keeps, mov);
620 }