freedreno/ir3: fix indirects tracking
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_compiler_nir.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <stdarg.h>
30
31 #include "pipe/p_state.h"
32 #include "util/u_string.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "tgsi/tgsi_lowering.h"
36 #include "tgsi/tgsi_strings.h"
37
38 #include "nir/tgsi_to_nir.h"
39 #include "glsl/shader_enums.h"
40
41 #include "freedreno_util.h"
42
43 #include "ir3_compiler.h"
44 #include "ir3_shader.h"
45 #include "ir3_nir.h"
46
47 #include "instr-a3xx.h"
48 #include "ir3.h"
49
50
51 struct ir3_compile {
52 struct ir3_compiler *compiler;
53
54 const struct tgsi_token *tokens;
55 struct nir_shader *s;
56
57 struct ir3 *ir;
58 struct ir3_shader_variant *so;
59
60 struct ir3_block *block; /* the current block */
61 struct ir3_block *in_block; /* block created for shader inputs */
62
63 nir_function_impl *impl;
64
65 /* For fragment shaders, from the hw perspective the only
66 * actual input is r0.xy position register passed to bary.f.
67 * But TGSI doesn't know that, it still declares things as
68 * IN[] registers. So we do all the input tracking normally
69 * and fix things up after compile_instructions()
70 *
71 * NOTE that frag_pos is the hardware position (possibly it
72 * is actually an index or tag or some such.. it is *not*
73 * values that can be directly used for gl_FragCoord..)
74 */
75 struct ir3_instruction *frag_pos, *frag_face, *frag_coord[4];
76
77 /* For vertex shaders, keep track of the system values sources */
78 struct ir3_instruction *vertex_id, *basevertex, *instance_id;
79
80 /* mapping from nir_register to defining instruction: */
81 struct hash_table *def_ht;
82
83 /* mapping from nir_variable to ir3_array: */
84 struct hash_table *var_ht;
85 unsigned num_arrays;
86
87 /* a common pattern for indirect addressing is to request the
88 * same address register multiple times. To avoid generating
89 * duplicate instruction sequences (which our backend does not
90 * try to clean up, since that should be done as the NIR stage)
91 * we cache the address value generated for a given src value:
92 */
93 struct hash_table *addr_ht;
94
95 /* maps nir_block to ir3_block, mostly for the purposes of
96 * figuring out the blocks successors
97 */
98 struct hash_table *block_ht;
99
100 /* for calculating input/output positions/linkages: */
101 unsigned next_inloc;
102
103 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
104 * so we need to use ldlv.u32 to load the varying directly:
105 */
106 bool flat_bypass;
107
108 /* on a3xx, we need to add one to # of array levels:
109 */
110 bool levels_add_one;
111
112 /* on a3xx, we need to scale up integer coords for isaml based
113 * on LoD:
114 */
115 bool unminify_coords;
116
117 /* for looking up which system value is which */
118 unsigned sysval_semantics[8];
119
120 /* list of kill instructions: */
121 struct ir3_instruction *kill[16];
122 unsigned int kill_count;
123
124 /* set if we encounter something we can't handle yet, so we
125 * can bail cleanly and fallback to TGSI compiler f/e
126 */
127 bool error;
128 };
129
130
131 static struct ir3_instruction * create_immed(struct ir3_block *block, uint32_t val);
132 static struct ir3_block * get_block(struct ir3_compile *ctx, nir_block *nblock);
133
134 static struct nir_shader *to_nir(const struct tgsi_token *tokens)
135 {
136 struct nir_shader_compiler_options options = {
137 .lower_fpow = true,
138 .lower_fsat = true,
139 .lower_scmp = true,
140 .lower_flrp = true,
141 .native_integers = true,
142 };
143 bool progress;
144
145 struct nir_shader *s = tgsi_to_nir(tokens, &options);
146
147 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
148 debug_printf("----------------------\n");
149 nir_print_shader(s, stdout);
150 debug_printf("----------------------\n");
151 }
152
153 nir_opt_global_to_local(s);
154 nir_convert_to_ssa(s);
155 nir_lower_idiv(s);
156
157 do {
158 progress = false;
159
160 nir_lower_vars_to_ssa(s);
161 nir_lower_alu_to_scalar(s);
162 nir_lower_phis_to_scalar(s);
163
164 progress |= nir_copy_prop(s);
165 progress |= nir_opt_dce(s);
166 progress |= nir_opt_cse(s);
167 progress |= ir3_nir_lower_if_else(s);
168 progress |= nir_opt_algebraic(s);
169 progress |= nir_opt_constant_folding(s);
170
171 } while (progress);
172
173 nir_remove_dead_variables(s);
174 nir_validate_shader(s);
175
176 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
177 debug_printf("----------------------\n");
178 nir_print_shader(s, stdout);
179 debug_printf("----------------------\n");
180 }
181
182 return s;
183 }
184
185 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
186 static const struct tgsi_token *
187 lower_tgsi(struct ir3_compile *ctx, const struct tgsi_token *tokens,
188 struct ir3_shader_variant *so)
189 {
190 struct tgsi_shader_info info;
191 struct tgsi_lowering_config lconfig = {
192 .color_two_side = so->key.color_two_side,
193 .lower_FRC = true,
194 };
195
196 switch (so->type) {
197 case SHADER_FRAGMENT:
198 case SHADER_COMPUTE:
199 lconfig.saturate_s = so->key.fsaturate_s;
200 lconfig.saturate_t = so->key.fsaturate_t;
201 lconfig.saturate_r = so->key.fsaturate_r;
202 break;
203 case SHADER_VERTEX:
204 lconfig.saturate_s = so->key.vsaturate_s;
205 lconfig.saturate_t = so->key.vsaturate_t;
206 lconfig.saturate_r = so->key.vsaturate_r;
207 break;
208 }
209
210 if (ctx->compiler->gpu_id >= 400) {
211 /* a4xx seems to have *no* sam.p */
212 lconfig.lower_TXP = ~0; /* lower all txp */
213 } else {
214 /* a3xx just needs to avoid sam.p for 3d tex */
215 lconfig.lower_TXP = (1 << TGSI_TEXTURE_3D);
216 }
217
218 return tgsi_transform_lowering(&lconfig, tokens, &info);
219 }
220
221 static struct ir3_compile *
222 compile_init(struct ir3_compiler *compiler,
223 struct ir3_shader_variant *so,
224 const struct tgsi_token *tokens)
225 {
226 struct ir3_compile *ctx = rzalloc(NULL, struct ir3_compile);
227 const struct tgsi_token *lowered_tokens;
228
229 if (compiler->gpu_id >= 400) {
230 /* need special handling for "flat" */
231 ctx->flat_bypass = true;
232 ctx->levels_add_one = false;
233 ctx->unminify_coords = false;
234 } else {
235 /* no special handling for "flat" */
236 ctx->flat_bypass = false;
237 ctx->levels_add_one = true;
238 ctx->unminify_coords = true;
239 }
240
241 ctx->compiler = compiler;
242 ctx->ir = so->ir;
243 ctx->so = so;
244 ctx->next_inloc = 8;
245 ctx->def_ht = _mesa_hash_table_create(ctx,
246 _mesa_hash_pointer, _mesa_key_pointer_equal);
247 ctx->var_ht = _mesa_hash_table_create(ctx,
248 _mesa_hash_pointer, _mesa_key_pointer_equal);
249 ctx->addr_ht = _mesa_hash_table_create(ctx,
250 _mesa_hash_pointer, _mesa_key_pointer_equal);
251 ctx->block_ht = _mesa_hash_table_create(ctx,
252 _mesa_hash_pointer, _mesa_key_pointer_equal);
253
254 lowered_tokens = lower_tgsi(ctx, tokens, so);
255 if (!lowered_tokens)
256 lowered_tokens = tokens;
257 ctx->s = to_nir(lowered_tokens);
258
259 if (lowered_tokens != tokens)
260 free((void *)lowered_tokens);
261
262 so->first_driver_param = so->first_immediate = ctx->s->num_uniforms;
263
264 /* one (vec4) slot for vertex id base: */
265 if (so->type == SHADER_VERTEX)
266 so->first_immediate++;
267
268 /* reserve 4 (vec4) slots for ubo base addresses: */
269 so->first_immediate += 4;
270
271 return ctx;
272 }
273
274 static void
275 compile_error(struct ir3_compile *ctx, const char *format, ...)
276 {
277 va_list ap;
278 va_start(ap, format);
279 _debug_vprintf(format, ap);
280 va_end(ap);
281 nir_print_shader(ctx->s, stdout);
282 ctx->error = true;
283 debug_assert(0);
284 }
285
286 #define compile_assert(ctx, cond) do { \
287 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
288 } while (0)
289
290 static void
291 compile_free(struct ir3_compile *ctx)
292 {
293 ralloc_free(ctx);
294 }
295
296 /* global per-array information: */
297 struct ir3_array {
298 unsigned length, aid;
299 };
300
301 /* per-block array state: */
302 struct ir3_array_value {
303 /* TODO drop length/aid, and just have ptr back to ir3_array */
304 unsigned length, aid;
305 /* initial array element values are phi's, other than for the
306 * entry block. The phi src's get added later in a resolve step
307 * after we have visited all the blocks, to account for back
308 * edges in the cfg.
309 */
310 struct ir3_instruction **phis;
311 /* current array element values (as block is processed). When
312 * the array phi's are resolved, it will contain the array state
313 * at exit of block, so successor blocks can use it to add their
314 * phi srcs.
315 */
316 struct ir3_instruction *arr[];
317 };
318
319 /* track array assignments per basic block. When an array is read
320 * outside of the same basic block, we can use NIR's dominance-frontier
321 * information to figure out where phi nodes are needed.
322 */
323 struct ir3_nir_block_data {
324 unsigned foo;
325 /* indexed by array-id (aid): */
326 struct ir3_array_value *arrs[];
327 };
328
329 static struct ir3_nir_block_data *
330 get_block_data(struct ir3_compile *ctx, struct ir3_block *block)
331 {
332 if (!block->bd) {
333 struct ir3_nir_block_data *bd = ralloc_size(ctx, sizeof(*bd) +
334 ((ctx->num_arrays + 1) * sizeof(bd->arrs[0])));
335 block->bd = bd;
336 }
337 return block->bd;
338 }
339
340 static void
341 declare_var(struct ir3_compile *ctx, nir_variable *var)
342 {
343 unsigned length = glsl_get_length(var->type) * 4; /* always vec4, at least with ttn */
344 struct ir3_array *arr = ralloc(ctx, struct ir3_array);
345 arr->length = length;
346 arr->aid = ++ctx->num_arrays;
347 _mesa_hash_table_insert(ctx->var_ht, var, arr);
348 }
349
350 static nir_block *
351 nir_block_pred(nir_block *block)
352 {
353 assert(block->predecessors->entries < 2);
354 if (block->predecessors->entries == 0)
355 return NULL;
356 return (nir_block *)_mesa_set_next_entry(block->predecessors, NULL)->key;
357 }
358
359 static struct ir3_array_value *
360 get_var(struct ir3_compile *ctx, nir_variable *var)
361 {
362 struct hash_entry *entry = _mesa_hash_table_search(ctx->var_ht, var);
363 struct ir3_block *block = ctx->block;
364 struct ir3_nir_block_data *bd = get_block_data(ctx, block);
365 struct ir3_array *arr = entry->data;
366
367 if (!bd->arrs[arr->aid]) {
368 struct ir3_array_value *av = ralloc_size(bd, sizeof(*av) +
369 (arr->length * sizeof(av->arr[0])));
370 struct ir3_array_value *defn = NULL;
371 nir_block *pred_block;
372
373 av->length = arr->length;
374 av->aid = arr->aid;
375
376 /* For loops, we have to consider that we have not visited some
377 * of the blocks who should feed into the phi (ie. back-edges in
378 * the cfg).. for example:
379 *
380 * loop {
381 * block { load_var; ... }
382 * if then block {} else block {}
383 * block { store_var; ... }
384 * if then block {} else block {}
385 * block {...}
386 * }
387 *
388 * We can skip the phi if we can chase the block predecessors
389 * until finding the block previously defining the array without
390 * crossing a block that has more than one predecessor.
391 *
392 * Otherwise create phi's and resolve them as a post-pass after
393 * all the blocks have been visited (to handle back-edges).
394 */
395
396 for (pred_block = block->nblock;
397 pred_block && (pred_block->predecessors->entries < 2) && !defn;
398 pred_block = nir_block_pred(pred_block)) {
399 struct ir3_block *pblock = get_block(ctx, pred_block);
400 struct ir3_nir_block_data *pbd = pblock->bd;
401 if (!pbd)
402 continue;
403 defn = pbd->arrs[arr->aid];
404 }
405
406 if (defn) {
407 /* only one possible definer: */
408 for (unsigned i = 0; i < arr->length; i++)
409 av->arr[i] = defn->arr[i];
410 } else if (pred_block) {
411 /* not the first block, and multiple potential definers: */
412 av->phis = ralloc_size(av, arr->length * sizeof(av->phis[0]));
413
414 for (unsigned i = 0; i < arr->length; i++) {
415 struct ir3_instruction *phi;
416
417 phi = ir3_instr_create2(block, -1, OPC_META_PHI,
418 1 + ctx->impl->num_blocks);
419 ir3_reg_create(phi, 0, 0); /* dst */
420
421 /* phi's should go at head of block: */
422 list_delinit(&phi->node);
423 list_add(&phi->node, &block->instr_list);
424
425 av->phis[i] = av->arr[i] = phi;
426 }
427 } else {
428 /* Some shaders end up reading array elements without
429 * first writing.. so initialize things to prevent null
430 * instr ptrs later:
431 */
432 for (unsigned i = 0; i < arr->length; i++)
433 av->arr[i] = create_immed(block, 0);
434 }
435
436 bd->arrs[arr->aid] = av;
437 }
438
439 return bd->arrs[arr->aid];
440 }
441
442 static void
443 add_array_phi_srcs(struct ir3_compile *ctx, nir_block *nblock,
444 struct ir3_array_value *av, BITSET_WORD *visited)
445 {
446 struct ir3_block *block;
447 struct ir3_nir_block_data *bd;
448
449 if (BITSET_TEST(visited, nblock->index))
450 return;
451
452 BITSET_SET(visited, nblock->index);
453
454 block = get_block(ctx, nblock);
455 bd = block->bd;
456
457 if (bd && bd->arrs[av->aid]) {
458 struct ir3_array_value *dav = bd->arrs[av->aid];
459 for (unsigned i = 0; i < av->length; i++) {
460 ir3_reg_create(av->phis[i], 0, IR3_REG_SSA)->instr =
461 dav->arr[i];
462 }
463 } else {
464 /* didn't find defn, recurse predecessors: */
465 struct set_entry *entry;
466 set_foreach(nblock->predecessors, entry) {
467 add_array_phi_srcs(ctx, (nir_block *)entry->key, av, visited);
468 }
469 }
470 }
471
472 static void
473 resolve_array_phis(struct ir3_compile *ctx, struct ir3_block *block)
474 {
475 struct ir3_nir_block_data *bd = block->bd;
476 unsigned bitset_words = BITSET_WORDS(ctx->impl->num_blocks);
477
478 if (!bd)
479 return;
480
481 /* TODO use nir dom_frontier to help us with this? */
482
483 for (unsigned i = 1; i <= ctx->num_arrays; i++) {
484 struct ir3_array_value *av = bd->arrs[i];
485 BITSET_WORD visited[bitset_words];
486 struct set_entry *entry;
487
488 if (!(av && av->phis))
489 continue;
490
491 memset(visited, 0, sizeof(visited));
492 set_foreach(block->nblock->predecessors, entry) {
493 add_array_phi_srcs(ctx, (nir_block *)entry->key, av, visited);
494 }
495 }
496 }
497
498 /* allocate a n element value array (to be populated by caller) and
499 * insert in def_ht
500 */
501 static struct ir3_instruction **
502 __get_dst(struct ir3_compile *ctx, void *key, unsigned n)
503 {
504 struct ir3_instruction **value =
505 ralloc_array(ctx->def_ht, struct ir3_instruction *, n);
506 _mesa_hash_table_insert(ctx->def_ht, key, value);
507 return value;
508 }
509
510 static struct ir3_instruction **
511 get_dst(struct ir3_compile *ctx, nir_dest *dst, unsigned n)
512 {
513 if (dst->is_ssa) {
514 return __get_dst(ctx, &dst->ssa, n);
515 } else {
516 return __get_dst(ctx, dst->reg.reg, n);
517 }
518 }
519
520 static struct ir3_instruction **
521 get_dst_ssa(struct ir3_compile *ctx, nir_ssa_def *dst, unsigned n)
522 {
523 return __get_dst(ctx, dst, n);
524 }
525
526 static struct ir3_instruction **
527 get_src(struct ir3_compile *ctx, nir_src *src)
528 {
529 struct hash_entry *entry;
530 if (src->is_ssa) {
531 entry = _mesa_hash_table_search(ctx->def_ht, src->ssa);
532 } else {
533 entry = _mesa_hash_table_search(ctx->def_ht, src->reg.reg);
534 }
535 compile_assert(ctx, entry);
536 return entry->data;
537 }
538
539 static struct ir3_instruction *
540 create_immed(struct ir3_block *block, uint32_t val)
541 {
542 struct ir3_instruction *mov;
543
544 mov = ir3_instr_create(block, 1, 0);
545 mov->cat1.src_type = TYPE_U32;
546 mov->cat1.dst_type = TYPE_U32;
547 ir3_reg_create(mov, 0, 0);
548 ir3_reg_create(mov, 0, IR3_REG_IMMED)->uim_val = val;
549
550 return mov;
551 }
552
553 static struct ir3_instruction *
554 create_addr(struct ir3_block *block, struct ir3_instruction *src)
555 {
556 struct ir3_instruction *instr, *immed;
557
558 /* TODO in at least some cases, the backend could probably be
559 * made clever enough to propagate IR3_REG_HALF..
560 */
561 instr = ir3_COV(block, src, TYPE_U32, TYPE_S16);
562 instr->regs[0]->flags |= IR3_REG_HALF;
563
564 immed = create_immed(block, 2);
565 immed->regs[0]->flags |= IR3_REG_HALF;
566
567 instr = ir3_SHL_B(block, instr, 0, immed, 0);
568 instr->regs[0]->flags |= IR3_REG_HALF;
569 instr->regs[1]->flags |= IR3_REG_HALF;
570
571 instr = ir3_MOV(block, instr, TYPE_S16);
572 instr->regs[0]->num = regid(REG_A0, 0);
573 instr->regs[0]->flags |= IR3_REG_HALF;
574 instr->regs[1]->flags |= IR3_REG_HALF;
575
576 return instr;
577 }
578
579 /* caches addr values to avoid generating multiple cov/shl/mova
580 * sequences for each use of a given NIR level src as address
581 */
582 static struct ir3_instruction *
583 get_addr(struct ir3_compile *ctx, struct ir3_instruction *src)
584 {
585 struct ir3_instruction *addr;
586 struct hash_entry *entry;
587 entry = _mesa_hash_table_search(ctx->addr_ht, src);
588 if (entry)
589 return entry->data;
590
591 /* TODO do we need to cache per block? */
592 addr = create_addr(ctx->block, src);
593 _mesa_hash_table_insert(ctx->addr_ht, src, addr);
594
595 return addr;
596 }
597
598 static struct ir3_instruction *
599 get_predicate(struct ir3_compile *ctx, struct ir3_instruction *src)
600 {
601 struct ir3_block *b = ctx->block;
602 struct ir3_instruction *cond;
603
604 /* NOTE: only cmps.*.* can write p0.x: */
605 cond = ir3_CMPS_S(b, src, 0, create_immed(b, 0), 0);
606 cond->cat2.condition = IR3_COND_NE;
607
608 /* condition always goes in predicate register: */
609 cond->regs[0]->num = regid(REG_P0, 0);
610
611 return cond;
612 }
613
614 static struct ir3_instruction *
615 create_uniform(struct ir3_compile *ctx, unsigned n)
616 {
617 struct ir3_instruction *mov;
618
619 mov = ir3_instr_create(ctx->block, 1, 0);
620 /* TODO get types right? */
621 mov->cat1.src_type = TYPE_F32;
622 mov->cat1.dst_type = TYPE_F32;
623 ir3_reg_create(mov, 0, 0);
624 ir3_reg_create(mov, n, IR3_REG_CONST);
625
626 return mov;
627 }
628
629 static struct ir3_instruction *
630 create_uniform_indirect(struct ir3_compile *ctx, unsigned n,
631 struct ir3_instruction *address)
632 {
633 struct ir3_instruction *mov;
634
635 mov = ir3_instr_create(ctx->block, 1, 0);
636 mov->cat1.src_type = TYPE_U32;
637 mov->cat1.dst_type = TYPE_U32;
638 ir3_reg_create(mov, 0, 0);
639 ir3_reg_create(mov, n, IR3_REG_CONST | IR3_REG_RELATIV);
640
641 ir3_instr_set_address(mov, address);
642
643 return mov;
644 }
645
646 static struct ir3_instruction *
647 create_collect(struct ir3_block *block, struct ir3_instruction **arr,
648 unsigned arrsz)
649 {
650 struct ir3_instruction *collect;
651
652 if (arrsz == 0)
653 return NULL;
654
655 collect = ir3_instr_create2(block, -1, OPC_META_FI, 1 + arrsz);
656 ir3_reg_create(collect, 0, 0); /* dst */
657 for (unsigned i = 0; i < arrsz; i++)
658 ir3_reg_create(collect, 0, IR3_REG_SSA)->instr = arr[i];
659
660 return collect;
661 }
662
663 static struct ir3_instruction *
664 create_indirect_load(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
665 struct ir3_instruction *address, struct ir3_instruction *collect)
666 {
667 struct ir3_block *block = ctx->block;
668 struct ir3_instruction *mov;
669 struct ir3_register *src;
670
671 mov = ir3_instr_create(block, 1, 0);
672 mov->cat1.src_type = TYPE_U32;
673 mov->cat1.dst_type = TYPE_U32;
674 ir3_reg_create(mov, 0, 0);
675 src = ir3_reg_create(mov, 0, IR3_REG_SSA | IR3_REG_RELATIV);
676 src->instr = collect;
677 src->size = arrsz;
678 src->offset = n;
679
680 ir3_instr_set_address(mov, address);
681
682 return mov;
683 }
684
685 static struct ir3_instruction *
686 create_indirect_store(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
687 struct ir3_instruction *src, struct ir3_instruction *address,
688 struct ir3_instruction *collect)
689 {
690 struct ir3_block *block = ctx->block;
691 struct ir3_instruction *mov;
692 struct ir3_register *dst;
693
694 mov = ir3_instr_create(block, 1, 0);
695 mov->cat1.src_type = TYPE_U32;
696 mov->cat1.dst_type = TYPE_U32;
697 dst = ir3_reg_create(mov, 0, IR3_REG_RELATIV);
698 dst->size = arrsz;
699 dst->offset = n;
700 ir3_reg_create(mov, 0, IR3_REG_SSA)->instr = src;
701 mov->fanin = collect;
702
703 ir3_instr_set_address(mov, address);
704
705 return mov;
706 }
707
708 static struct ir3_instruction *
709 create_input(struct ir3_block *block, struct ir3_instruction *instr,
710 unsigned n)
711 {
712 struct ir3_instruction *in;
713
714 in = ir3_instr_create(block, -1, OPC_META_INPUT);
715 in->inout.block = block;
716 ir3_reg_create(in, n, 0);
717 if (instr)
718 ir3_reg_create(in, 0, IR3_REG_SSA)->instr = instr;
719
720 return in;
721 }
722
723 static struct ir3_instruction *
724 create_frag_input(struct ir3_compile *ctx, unsigned n, bool use_ldlv)
725 {
726 struct ir3_block *block = ctx->block;
727 struct ir3_instruction *instr;
728 struct ir3_instruction *inloc = create_immed(block, n);
729
730 if (use_ldlv) {
731 instr = ir3_LDLV(block, inloc, 0, create_immed(block, 1), 0);
732 instr->cat6.type = TYPE_U32;
733 instr->cat6.iim_val = 1;
734 } else {
735 instr = ir3_BARY_F(block, inloc, 0, ctx->frag_pos, 0);
736 instr->regs[2]->wrmask = 0x3;
737 }
738
739 return instr;
740 }
741
742 static struct ir3_instruction *
743 create_frag_coord(struct ir3_compile *ctx, unsigned comp)
744 {
745 struct ir3_block *block = ctx->block;
746 struct ir3_instruction *instr;
747
748 compile_assert(ctx, !ctx->frag_coord[comp]);
749
750 ctx->frag_coord[comp] = create_input(ctx->block, NULL, 0);
751
752 switch (comp) {
753 case 0: /* .x */
754 case 1: /* .y */
755 /* for frag_coord, we get unsigned values.. we need
756 * to subtract (integer) 8 and divide by 16 (right-
757 * shift by 4) then convert to float:
758 *
759 * sub.s tmp, src, 8
760 * shr.b tmp, tmp, 4
761 * mov.u32f32 dst, tmp
762 *
763 */
764 instr = ir3_SUB_S(block, ctx->frag_coord[comp], 0,
765 create_immed(block, 8), 0);
766 instr = ir3_SHR_B(block, instr, 0,
767 create_immed(block, 4), 0);
768 instr = ir3_COV(block, instr, TYPE_U32, TYPE_F32);
769
770 return instr;
771 case 2: /* .z */
772 case 3: /* .w */
773 default:
774 /* seems that we can use these as-is: */
775 return ctx->frag_coord[comp];
776 }
777 }
778
779 static struct ir3_instruction *
780 create_frag_face(struct ir3_compile *ctx, unsigned comp)
781 {
782 struct ir3_block *block = ctx->block;
783 struct ir3_instruction *instr;
784
785 switch (comp) {
786 case 0: /* .x */
787 compile_assert(ctx, !ctx->frag_face);
788
789 ctx->frag_face = create_input(block, NULL, 0);
790 ctx->frag_face->regs[0]->flags |= IR3_REG_HALF;
791
792 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
793 * positive vs negative float.. and piglit further seems to
794 * expect -1.0 or 1.0:
795 *
796 * mul.s tmp, hr0.x, 2
797 * add.s tmp, tmp, 1
798 * mov.s32f32, dst, tmp
799 *
800 */
801 instr = ir3_MUL_S(block, ctx->frag_face, 0,
802 create_immed(block, 2), 0);
803 instr = ir3_ADD_S(block, instr, 0,
804 create_immed(block, 1), 0);
805 instr = ir3_COV(block, instr, TYPE_S32, TYPE_F32);
806
807 return instr;
808 case 1: /* .y */
809 case 2: /* .z */
810 return create_immed(block, fui(0.0));
811 default:
812 case 3: /* .w */
813 return create_immed(block, fui(1.0));
814 }
815 }
816
817 /* helper for instructions that produce multiple consecutive scalar
818 * outputs which need to have a split/fanout meta instruction inserted
819 */
820 static void
821 split_dest(struct ir3_block *block, struct ir3_instruction **dst,
822 struct ir3_instruction *src, unsigned n)
823 {
824 struct ir3_instruction *prev = NULL;
825 for (int i = 0, j = 0; i < n; i++) {
826 struct ir3_instruction *split =
827 ir3_instr_create(block, -1, OPC_META_FO);
828 ir3_reg_create(split, 0, IR3_REG_SSA);
829 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = src;
830 split->fo.off = i;
831
832 if (prev) {
833 split->cp.left = prev;
834 split->cp.left_cnt++;
835 prev->cp.right = split;
836 prev->cp.right_cnt++;
837 }
838 prev = split;
839
840 if (src->regs[0]->wrmask & (1 << i))
841 dst[j++] = split;
842 }
843 }
844
845 /*
846 * Adreno uses uint rather than having dedicated bool type,
847 * which (potentially) requires some conversion, in particular
848 * when using output of an bool instr to int input, or visa
849 * versa.
850 *
851 * | Adreno | NIR |
852 * -------+---------+-------+-
853 * true | 1 | ~0 |
854 * false | 0 | 0 |
855 *
856 * To convert from an adreno bool (uint) to nir, use:
857 *
858 * absneg.s dst, (neg)src
859 *
860 * To convert back in the other direction:
861 *
862 * absneg.s dst, (abs)arc
863 *
864 * The CP step can clean up the absneg.s that cancel each other
865 * out, and with a slight bit of extra cleverness (to recognize
866 * the instructions which produce either a 0 or 1) can eliminate
867 * the absneg.s's completely when an instruction that wants
868 * 0/1 consumes the result. For example, when a nir 'bcsel'
869 * consumes the result of 'feq'. So we should be able to get by
870 * without a boolean resolve step, and without incuring any
871 * extra penalty in instruction count.
872 */
873
874 /* NIR bool -> native (adreno): */
875 static struct ir3_instruction *
876 ir3_b2n(struct ir3_block *block, struct ir3_instruction *instr)
877 {
878 return ir3_ABSNEG_S(block, instr, IR3_REG_SABS);
879 }
880
881 /* native (adreno) -> NIR bool: */
882 static struct ir3_instruction *
883 ir3_n2b(struct ir3_block *block, struct ir3_instruction *instr)
884 {
885 return ir3_ABSNEG_S(block, instr, IR3_REG_SNEG);
886 }
887
888 /*
889 * alu/sfu instructions:
890 */
891
892 static void
893 emit_alu(struct ir3_compile *ctx, nir_alu_instr *alu)
894 {
895 const nir_op_info *info = &nir_op_infos[alu->op];
896 struct ir3_instruction **dst, *src[info->num_inputs];
897 struct ir3_block *b = ctx->block;
898
899 dst = get_dst(ctx, &alu->dest.dest, MAX2(info->output_size, 1));
900
901 /* Vectors are special in that they have non-scalarized writemasks,
902 * and just take the first swizzle channel for each argument in
903 * order into each writemask channel.
904 */
905 if ((alu->op == nir_op_vec2) ||
906 (alu->op == nir_op_vec3) ||
907 (alu->op == nir_op_vec4)) {
908
909 for (int i = 0; i < info->num_inputs; i++) {
910 nir_alu_src *asrc = &alu->src[i];
911
912 compile_assert(ctx, !asrc->abs);
913 compile_assert(ctx, !asrc->negate);
914
915 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[0]];
916 if (!src[i])
917 src[i] = create_immed(ctx->block, 0);
918 dst[i] = ir3_MOV(b, src[i], TYPE_U32);
919 }
920
921 return;
922 }
923
924 /* General case: We can just grab the one used channel per src. */
925 for (int i = 0; i < info->num_inputs; i++) {
926 unsigned chan = ffs(alu->dest.write_mask) - 1;
927 nir_alu_src *asrc = &alu->src[i];
928
929 compile_assert(ctx, !asrc->abs);
930 compile_assert(ctx, !asrc->negate);
931
932 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[chan]];
933
934 compile_assert(ctx, src[i]);
935 }
936
937 switch (alu->op) {
938 case nir_op_f2i:
939 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_S32);
940 break;
941 case nir_op_f2u:
942 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_U32);
943 break;
944 case nir_op_i2f:
945 dst[0] = ir3_COV(b, src[0], TYPE_S32, TYPE_F32);
946 break;
947 case nir_op_u2f:
948 dst[0] = ir3_COV(b, src[0], TYPE_U32, TYPE_F32);
949 break;
950 case nir_op_imov:
951 dst[0] = ir3_MOV(b, src[0], TYPE_S32);
952 break;
953 case nir_op_fmov:
954 dst[0] = ir3_MOV(b, src[0], TYPE_F32);
955 break;
956 case nir_op_f2b:
957 dst[0] = ir3_CMPS_F(b, src[0], 0, create_immed(b, fui(0.0)), 0);
958 dst[0]->cat2.condition = IR3_COND_NE;
959 dst[0] = ir3_n2b(b, dst[0]);
960 break;
961 case nir_op_b2f:
962 dst[0] = ir3_COV(b, ir3_b2n(b, src[0]), TYPE_U32, TYPE_F32);
963 break;
964 case nir_op_b2i:
965 dst[0] = ir3_b2n(b, src[0]);
966 break;
967 case nir_op_i2b:
968 dst[0] = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
969 dst[0]->cat2.condition = IR3_COND_NE;
970 dst[0] = ir3_n2b(b, dst[0]);
971 break;
972
973 case nir_op_fneg:
974 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FNEG);
975 break;
976 case nir_op_fabs:
977 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FABS);
978 break;
979 case nir_op_fmax:
980 dst[0] = ir3_MAX_F(b, src[0], 0, src[1], 0);
981 break;
982 case nir_op_fmin:
983 dst[0] = ir3_MIN_F(b, src[0], 0, src[1], 0);
984 break;
985 case nir_op_fmul:
986 dst[0] = ir3_MUL_F(b, src[0], 0, src[1], 0);
987 break;
988 case nir_op_fadd:
989 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], 0);
990 break;
991 case nir_op_fsub:
992 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], IR3_REG_FNEG);
993 break;
994 case nir_op_ffma:
995 dst[0] = ir3_MAD_F32(b, src[0], 0, src[1], 0, src[2], 0);
996 break;
997 case nir_op_fddx:
998 dst[0] = ir3_DSX(b, src[0], 0);
999 dst[0]->cat5.type = TYPE_F32;
1000 break;
1001 case nir_op_fddy:
1002 dst[0] = ir3_DSY(b, src[0], 0);
1003 dst[0]->cat5.type = TYPE_F32;
1004 break;
1005 break;
1006 case nir_op_flt:
1007 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1008 dst[0]->cat2.condition = IR3_COND_LT;
1009 dst[0] = ir3_n2b(b, dst[0]);
1010 break;
1011 case nir_op_fge:
1012 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1013 dst[0]->cat2.condition = IR3_COND_GE;
1014 dst[0] = ir3_n2b(b, dst[0]);
1015 break;
1016 case nir_op_feq:
1017 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1018 dst[0]->cat2.condition = IR3_COND_EQ;
1019 dst[0] = ir3_n2b(b, dst[0]);
1020 break;
1021 case nir_op_fne:
1022 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1023 dst[0]->cat2.condition = IR3_COND_NE;
1024 dst[0] = ir3_n2b(b, dst[0]);
1025 break;
1026 case nir_op_fceil:
1027 dst[0] = ir3_CEIL_F(b, src[0], 0);
1028 break;
1029 case nir_op_ffloor:
1030 dst[0] = ir3_FLOOR_F(b, src[0], 0);
1031 break;
1032 case nir_op_ftrunc:
1033 dst[0] = ir3_TRUNC_F(b, src[0], 0);
1034 break;
1035 case nir_op_fround_even:
1036 dst[0] = ir3_RNDNE_F(b, src[0], 0);
1037 break;
1038 case nir_op_fsign:
1039 dst[0] = ir3_SIGN_F(b, src[0], 0);
1040 break;
1041
1042 case nir_op_fsin:
1043 dst[0] = ir3_SIN(b, src[0], 0);
1044 break;
1045 case nir_op_fcos:
1046 dst[0] = ir3_COS(b, src[0], 0);
1047 break;
1048 case nir_op_frsq:
1049 dst[0] = ir3_RSQ(b, src[0], 0);
1050 break;
1051 case nir_op_frcp:
1052 dst[0] = ir3_RCP(b, src[0], 0);
1053 break;
1054 case nir_op_flog2:
1055 dst[0] = ir3_LOG2(b, src[0], 0);
1056 break;
1057 case nir_op_fexp2:
1058 dst[0] = ir3_EXP2(b, src[0], 0);
1059 break;
1060 case nir_op_fsqrt:
1061 dst[0] = ir3_SQRT(b, src[0], 0);
1062 break;
1063
1064 case nir_op_iabs:
1065 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SABS);
1066 break;
1067 case nir_op_iadd:
1068 dst[0] = ir3_ADD_U(b, src[0], 0, src[1], 0);
1069 break;
1070 case nir_op_iand:
1071 dst[0] = ir3_AND_B(b, src[0], 0, src[1], 0);
1072 break;
1073 case nir_op_imax:
1074 dst[0] = ir3_MAX_S(b, src[0], 0, src[1], 0);
1075 break;
1076 case nir_op_umax:
1077 dst[0] = ir3_MAX_U(b, src[0], 0, src[1], 0);
1078 break;
1079 case nir_op_imin:
1080 dst[0] = ir3_MIN_S(b, src[0], 0, src[1], 0);
1081 break;
1082 case nir_op_umin:
1083 dst[0] = ir3_MIN_U(b, src[0], 0, src[1], 0);
1084 break;
1085 case nir_op_imul:
1086 /*
1087 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
1088 * mull.u tmp0, a, b ; mul low, i.e. al * bl
1089 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
1090 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
1091 */
1092 dst[0] = ir3_MADSH_M16(b, src[1], 0, src[0], 0,
1093 ir3_MADSH_M16(b, src[0], 0, src[1], 0,
1094 ir3_MULL_U(b, src[0], 0, src[1], 0), 0), 0);
1095 break;
1096 case nir_op_ineg:
1097 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SNEG);
1098 break;
1099 case nir_op_inot:
1100 dst[0] = ir3_NOT_B(b, src[0], 0);
1101 break;
1102 case nir_op_ior:
1103 dst[0] = ir3_OR_B(b, src[0], 0, src[1], 0);
1104 break;
1105 case nir_op_ishl:
1106 dst[0] = ir3_SHL_B(b, src[0], 0, src[1], 0);
1107 break;
1108 case nir_op_ishr:
1109 dst[0] = ir3_ASHR_B(b, src[0], 0, src[1], 0);
1110 break;
1111 case nir_op_isign: {
1112 /* maybe this would be sane to lower in nir.. */
1113 struct ir3_instruction *neg, *pos;
1114
1115 neg = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1116 neg->cat2.condition = IR3_COND_LT;
1117
1118 pos = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1119 pos->cat2.condition = IR3_COND_GT;
1120
1121 dst[0] = ir3_SUB_U(b, pos, 0, neg, 0);
1122
1123 break;
1124 }
1125 case nir_op_isub:
1126 dst[0] = ir3_SUB_U(b, src[0], 0, src[1], 0);
1127 break;
1128 case nir_op_ixor:
1129 dst[0] = ir3_XOR_B(b, src[0], 0, src[1], 0);
1130 break;
1131 case nir_op_ushr:
1132 dst[0] = ir3_SHR_B(b, src[0], 0, src[1], 0);
1133 break;
1134 case nir_op_ilt:
1135 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1136 dst[0]->cat2.condition = IR3_COND_LT;
1137 dst[0] = ir3_n2b(b, dst[0]);
1138 break;
1139 case nir_op_ige:
1140 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1141 dst[0]->cat2.condition = IR3_COND_GE;
1142 dst[0] = ir3_n2b(b, dst[0]);
1143 break;
1144 case nir_op_ieq:
1145 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1146 dst[0]->cat2.condition = IR3_COND_EQ;
1147 dst[0] = ir3_n2b(b, dst[0]);
1148 break;
1149 case nir_op_ine:
1150 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1151 dst[0]->cat2.condition = IR3_COND_NE;
1152 dst[0] = ir3_n2b(b, dst[0]);
1153 break;
1154 case nir_op_ult:
1155 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1156 dst[0]->cat2.condition = IR3_COND_LT;
1157 dst[0] = ir3_n2b(b, dst[0]);
1158 break;
1159 case nir_op_uge:
1160 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1161 dst[0]->cat2.condition = IR3_COND_GE;
1162 dst[0] = ir3_n2b(b, dst[0]);
1163 break;
1164
1165 case nir_op_bcsel:
1166 dst[0] = ir3_SEL_B32(b, src[1], 0, ir3_b2n(b, src[0]), 0, src[2], 0);
1167 break;
1168
1169 default:
1170 compile_error(ctx, "Unhandled ALU op: %s\n",
1171 nir_op_infos[alu->op].name);
1172 break;
1173 }
1174 }
1175
1176 /* handles direct/indirect UBO reads: */
1177 static void
1178 emit_intrinsic_load_ubo(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1179 struct ir3_instruction **dst)
1180 {
1181 struct ir3_block *b = ctx->block;
1182 struct ir3_instruction *addr, *src0, *src1;
1183 /* UBO addresses are the first driver params: */
1184 unsigned ubo = regid(ctx->so->first_driver_param, 0);
1185 unsigned off = intr->const_index[0];
1186
1187 /* First src is ubo index, which could either be an immed or not: */
1188 src0 = get_src(ctx, &intr->src[0])[0];
1189 if (is_same_type_mov(src0) &&
1190 (src0->regs[1]->flags & IR3_REG_IMMED)) {
1191 addr = create_uniform(ctx, ubo + src0->regs[1]->iim_val);
1192 } else {
1193 addr = create_uniform_indirect(ctx, ubo, get_addr(ctx, src0));
1194 }
1195
1196 if (intr->intrinsic == nir_intrinsic_load_ubo_indirect) {
1197 /* For load_ubo_indirect, second src is indirect offset: */
1198 src1 = get_src(ctx, &intr->src[1])[0];
1199
1200 /* and add offset to addr: */
1201 addr = ir3_ADD_S(b, addr, 0, src1, 0);
1202 }
1203
1204 /* if offset is to large to encode in the ldg, split it out: */
1205 if ((off + (intr->num_components * 4)) > 1024) {
1206 /* split out the minimal amount to improve the odds that
1207 * cp can fit the immediate in the add.s instruction:
1208 */
1209 unsigned off2 = off + (intr->num_components * 4) - 1024;
1210 addr = ir3_ADD_S(b, addr, 0, create_immed(b, off2), 0);
1211 off -= off2;
1212 }
1213
1214 for (int i = 0; i < intr->num_components; i++) {
1215 struct ir3_instruction *load =
1216 ir3_LDG(b, addr, 0, create_immed(b, 1), 0);
1217 load->cat6.type = TYPE_U32;
1218 load->cat6.offset = off + i * 4; /* byte offset */
1219 dst[i] = load;
1220 }
1221 }
1222
1223 /* handles array reads: */
1224 static void
1225 emit_intrinisic_load_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1226 struct ir3_instruction **dst)
1227 {
1228 nir_deref_var *dvar = intr->variables[0];
1229 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1230 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1231
1232 compile_assert(ctx, dvar->deref.child &&
1233 (dvar->deref.child->deref_type == nir_deref_type_array));
1234
1235 switch (darr->deref_array_type) {
1236 case nir_deref_array_type_direct:
1237 /* direct access does not require anything special: */
1238 for (int i = 0; i < intr->num_components; i++) {
1239 unsigned n = darr->base_offset * 4 + i;
1240 compile_assert(ctx, n < arr->length);
1241 dst[i] = arr->arr[n];
1242 }
1243 break;
1244 case nir_deref_array_type_indirect: {
1245 /* for indirect, we need to collect all the array elements: */
1246 struct ir3_instruction *collect =
1247 create_collect(ctx->block, arr->arr, arr->length);
1248 struct ir3_instruction *addr =
1249 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1250 for (int i = 0; i < intr->num_components; i++) {
1251 unsigned n = darr->base_offset * 4 + i;
1252 compile_assert(ctx, n < arr->length);
1253 dst[i] = create_indirect_load(ctx, arr->length, n, addr, collect);
1254 }
1255 break;
1256 }
1257 default:
1258 compile_error(ctx, "Unhandled load deref type: %u\n",
1259 darr->deref_array_type);
1260 break;
1261 }
1262 }
1263
1264 /* handles array writes: */
1265 static void
1266 emit_intrinisic_store_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1267 {
1268 nir_deref_var *dvar = intr->variables[0];
1269 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1270 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1271 struct ir3_instruction **src;
1272
1273 compile_assert(ctx, dvar->deref.child &&
1274 (dvar->deref.child->deref_type == nir_deref_type_array));
1275
1276 src = get_src(ctx, &intr->src[0]);
1277
1278 switch (darr->deref_array_type) {
1279 case nir_deref_array_type_direct:
1280 /* direct access does not require anything special: */
1281 for (int i = 0; i < intr->num_components; i++) {
1282 unsigned n = darr->base_offset * 4 + i;
1283 compile_assert(ctx, n < arr->length);
1284 arr->arr[n] = src[i];
1285 }
1286 break;
1287 case nir_deref_array_type_indirect: {
1288 /* for indirect, create indirect-store and fan that out: */
1289 struct ir3_instruction *collect =
1290 create_collect(ctx->block, arr->arr, arr->length);
1291 struct ir3_instruction *addr =
1292 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1293 for (int i = 0; i < intr->num_components; i++) {
1294 struct ir3_instruction *store;
1295 unsigned n = darr->base_offset * 4 + i;
1296 compile_assert(ctx, n < arr->length);
1297
1298 store = create_indirect_store(ctx, arr->length,
1299 n, src[i], addr, collect);
1300
1301 store->fanin->fi.aid = arr->aid;
1302
1303 /* TODO: probably split this out to be used for
1304 * store_output_indirect? or move this into
1305 * create_indirect_store()?
1306 */
1307 for (int j = i; j < arr->length; j += intr->num_components) {
1308 struct ir3_instruction *split;
1309
1310 split = ir3_instr_create(ctx->block, -1, OPC_META_FO);
1311 split->fo.off = j;
1312 ir3_reg_create(split, 0, 0);
1313 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = store;
1314
1315 arr->arr[j] = split;
1316 }
1317 }
1318 /* fixup fanout/split neighbors: */
1319 for (int i = 0; i < arr->length; i++) {
1320 arr->arr[i]->cp.right = (i < (arr->length - 1)) ?
1321 arr->arr[i+1] : NULL;
1322 arr->arr[i]->cp.left = (i > 0) ?
1323 arr->arr[i-1] : NULL;
1324 }
1325 break;
1326 }
1327 default:
1328 compile_error(ctx, "Unhandled store deref type: %u\n",
1329 darr->deref_array_type);
1330 break;
1331 }
1332 }
1333
1334 static void add_sysval_input(struct ir3_compile *ctx, unsigned name,
1335 struct ir3_instruction *instr)
1336 {
1337 struct ir3_shader_variant *so = ctx->so;
1338 unsigned r = regid(so->inputs_count, 0);
1339 unsigned n = so->inputs_count++;
1340
1341 so->inputs[n].semantic = ir3_semantic_name(name, 0);
1342 so->inputs[n].compmask = 1;
1343 so->inputs[n].regid = r;
1344 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
1345 so->total_in++;
1346
1347 ctx->ir->ninputs = MAX2(ctx->ir->ninputs, r + 1);
1348 ctx->ir->inputs[r] = instr;
1349 }
1350
1351 static void
1352 emit_intrinisic(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1353 {
1354 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1355 struct ir3_instruction **dst, **src;
1356 struct ir3_block *b = ctx->block;
1357 unsigned idx = intr->const_index[0];
1358
1359 if (info->has_dest) {
1360 dst = get_dst(ctx, &intr->dest, intr->num_components);
1361 } else {
1362 dst = NULL;
1363 }
1364
1365 switch (intr->intrinsic) {
1366 case nir_intrinsic_load_uniform:
1367 for (int i = 0; i < intr->num_components; i++) {
1368 unsigned n = idx * 4 + i;
1369 dst[i] = create_uniform(ctx, n);
1370 }
1371 break;
1372 case nir_intrinsic_load_uniform_indirect:
1373 src = get_src(ctx, &intr->src[0]);
1374 for (int i = 0; i < intr->num_components; i++) {
1375 unsigned n = idx * 4 + i;
1376 dst[i] = create_uniform_indirect(ctx, n,
1377 get_addr(ctx, src[0]));
1378 }
1379 /* NOTE: if relative addressing is used, we set constlen in
1380 * the compiler (to worst-case value) since we don't know in
1381 * the assembler what the max addr reg value can be:
1382 */
1383 ctx->so->constlen = ctx->s->num_uniforms;
1384 break;
1385 case nir_intrinsic_load_ubo:
1386 case nir_intrinsic_load_ubo_indirect:
1387 emit_intrinsic_load_ubo(ctx, intr, dst);
1388 break;
1389 case nir_intrinsic_load_input:
1390 for (int i = 0; i < intr->num_components; i++) {
1391 unsigned n = idx * 4 + i;
1392 dst[i] = ctx->ir->inputs[n];
1393 }
1394 break;
1395 case nir_intrinsic_load_input_indirect:
1396 src = get_src(ctx, &intr->src[0]);
1397 struct ir3_instruction *collect =
1398 create_collect(b, ctx->ir->inputs, ctx->ir->ninputs);
1399 struct ir3_instruction *addr = get_addr(ctx, src[0]);
1400 for (int i = 0; i < intr->num_components; i++) {
1401 unsigned n = idx * 4 + i;
1402 dst[i] = create_indirect_load(ctx, ctx->ir->ninputs,
1403 n, addr, collect);
1404 }
1405 break;
1406 case nir_intrinsic_load_var:
1407 emit_intrinisic_load_var(ctx, intr, dst);
1408 break;
1409 case nir_intrinsic_store_var:
1410 emit_intrinisic_store_var(ctx, intr);
1411 break;
1412 case nir_intrinsic_store_output:
1413 src = get_src(ctx, &intr->src[0]);
1414 for (int i = 0; i < intr->num_components; i++) {
1415 unsigned n = idx * 4 + i;
1416 ctx->ir->outputs[n] = src[i];
1417 }
1418 break;
1419 case nir_intrinsic_load_base_vertex:
1420 if (!ctx->basevertex) {
1421 /* first four vec4 sysval's reserved for UBOs: */
1422 unsigned r = regid(ctx->so->first_driver_param + 4, 0);
1423 ctx->basevertex = create_uniform(ctx, r);
1424 add_sysval_input(ctx, TGSI_SEMANTIC_BASEVERTEX,
1425 ctx->basevertex);
1426 }
1427 dst[0] = ctx->basevertex;
1428 break;
1429 case nir_intrinsic_load_vertex_id_zero_base:
1430 if (!ctx->vertex_id) {
1431 ctx->vertex_id = create_input(ctx->block, NULL, 0);
1432 add_sysval_input(ctx, TGSI_SEMANTIC_VERTEXID_NOBASE,
1433 ctx->vertex_id);
1434 }
1435 dst[0] = ctx->vertex_id;
1436 break;
1437 case nir_intrinsic_load_instance_id:
1438 if (!ctx->instance_id) {
1439 ctx->instance_id = create_input(ctx->block, NULL, 0);
1440 add_sysval_input(ctx, TGSI_SEMANTIC_INSTANCEID,
1441 ctx->instance_id);
1442 }
1443 dst[0] = ctx->instance_id;
1444 break;
1445 case nir_intrinsic_discard_if:
1446 case nir_intrinsic_discard: {
1447 struct ir3_instruction *cond, *kill;
1448
1449 if (intr->intrinsic == nir_intrinsic_discard_if) {
1450 /* conditional discard: */
1451 src = get_src(ctx, &intr->src[0]);
1452 cond = ir3_b2n(b, src[0]);
1453 } else {
1454 /* unconditional discard: */
1455 cond = create_immed(b, 1);
1456 }
1457
1458 /* NOTE: only cmps.*.* can write p0.x: */
1459 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1460 cond->cat2.condition = IR3_COND_NE;
1461
1462 /* condition always goes in predicate register: */
1463 cond->regs[0]->num = regid(REG_P0, 0);
1464
1465 kill = ir3_KILL(b, cond, 0);
1466 array_insert(ctx->ir->predicates, kill);
1467
1468 ctx->kill[ctx->kill_count++] = kill;
1469 ctx->so->has_kill = true;
1470
1471 break;
1472 }
1473 default:
1474 compile_error(ctx, "Unhandled intrinsic type: %s\n",
1475 nir_intrinsic_infos[intr->intrinsic].name);
1476 break;
1477 }
1478 }
1479
1480 static void
1481 emit_load_const(struct ir3_compile *ctx, nir_load_const_instr *instr)
1482 {
1483 struct ir3_instruction **dst = get_dst_ssa(ctx, &instr->def,
1484 instr->def.num_components);
1485 for (int i = 0; i < instr->def.num_components; i++)
1486 dst[i] = create_immed(ctx->block, instr->value.u[i]);
1487 }
1488
1489 static void
1490 emit_undef(struct ir3_compile *ctx, nir_ssa_undef_instr *undef)
1491 {
1492 struct ir3_instruction **dst = get_dst_ssa(ctx, &undef->def,
1493 undef->def.num_components);
1494 /* backend doesn't want undefined instructions, so just plug
1495 * in 0.0..
1496 */
1497 for (int i = 0; i < undef->def.num_components; i++)
1498 dst[i] = create_immed(ctx->block, fui(0.0));
1499 }
1500
1501 /*
1502 * texture fetch/sample instructions:
1503 */
1504
1505 static void
1506 tex_info(nir_tex_instr *tex, unsigned *flagsp, unsigned *coordsp)
1507 {
1508 unsigned coords, flags = 0;
1509
1510 /* note: would use tex->coord_components.. except txs.. also,
1511 * since array index goes after shadow ref, we don't want to
1512 * count it:
1513 */
1514 switch (tex->sampler_dim) {
1515 case GLSL_SAMPLER_DIM_1D:
1516 case GLSL_SAMPLER_DIM_BUF:
1517 coords = 1;
1518 break;
1519 case GLSL_SAMPLER_DIM_2D:
1520 case GLSL_SAMPLER_DIM_RECT:
1521 case GLSL_SAMPLER_DIM_EXTERNAL:
1522 case GLSL_SAMPLER_DIM_MS:
1523 coords = 2;
1524 break;
1525 case GLSL_SAMPLER_DIM_3D:
1526 case GLSL_SAMPLER_DIM_CUBE:
1527 coords = 3;
1528 flags |= IR3_INSTR_3D;
1529 break;
1530 default:
1531 unreachable("bad sampler_dim");
1532 }
1533
1534 if (tex->is_shadow)
1535 flags |= IR3_INSTR_S;
1536
1537 if (tex->is_array)
1538 flags |= IR3_INSTR_A;
1539
1540 *flagsp = flags;
1541 *coordsp = coords;
1542 }
1543
1544 static void
1545 emit_tex(struct ir3_compile *ctx, nir_tex_instr *tex)
1546 {
1547 struct ir3_block *b = ctx->block;
1548 struct ir3_instruction **dst, *sam, *src0[12], *src1[4];
1549 struct ir3_instruction **coord, *lod, *compare, *proj, **off, **ddx, **ddy;
1550 bool has_bias = false, has_lod = false, has_proj = false, has_off = false;
1551 unsigned i, coords, flags;
1552 unsigned nsrc0 = 0, nsrc1 = 0;
1553 type_t type;
1554 opc_t opc = 0;
1555
1556 coord = off = ddx = ddy = NULL;
1557 lod = proj = compare = NULL;
1558
1559 /* TODO: might just be one component for gathers? */
1560 dst = get_dst(ctx, &tex->dest, 4);
1561
1562 for (unsigned i = 0; i < tex->num_srcs; i++) {
1563 switch (tex->src[i].src_type) {
1564 case nir_tex_src_coord:
1565 coord = get_src(ctx, &tex->src[i].src);
1566 break;
1567 case nir_tex_src_bias:
1568 lod = get_src(ctx, &tex->src[i].src)[0];
1569 has_bias = true;
1570 break;
1571 case nir_tex_src_lod:
1572 lod = get_src(ctx, &tex->src[i].src)[0];
1573 has_lod = true;
1574 break;
1575 case nir_tex_src_comparitor: /* shadow comparator */
1576 compare = get_src(ctx, &tex->src[i].src)[0];
1577 break;
1578 case nir_tex_src_projector:
1579 proj = get_src(ctx, &tex->src[i].src)[0];
1580 has_proj = true;
1581 break;
1582 case nir_tex_src_offset:
1583 off = get_src(ctx, &tex->src[i].src);
1584 has_off = true;
1585 break;
1586 case nir_tex_src_ddx:
1587 ddx = get_src(ctx, &tex->src[i].src);
1588 break;
1589 case nir_tex_src_ddy:
1590 ddy = get_src(ctx, &tex->src[i].src);
1591 break;
1592 default:
1593 compile_error(ctx, "Unhandled NIR tex serc type: %d\n",
1594 tex->src[i].src_type);
1595 return;
1596 }
1597 }
1598
1599 switch (tex->op) {
1600 case nir_texop_tex: opc = OPC_SAM; break;
1601 case nir_texop_txb: opc = OPC_SAMB; break;
1602 case nir_texop_txl: opc = OPC_SAML; break;
1603 case nir_texop_txd: opc = OPC_SAMGQ; break;
1604 case nir_texop_txf: opc = OPC_ISAML; break;
1605 case nir_texop_txf_ms:
1606 case nir_texop_txs:
1607 case nir_texop_lod:
1608 case nir_texop_tg4:
1609 case nir_texop_query_levels:
1610 compile_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
1611 return;
1612 }
1613
1614 tex_info(tex, &flags, &coords);
1615
1616 /* scale up integer coords for TXF based on the LOD */
1617 if (ctx->unminify_coords && (opc == OPC_ISAML)) {
1618 assert(has_lod);
1619 for (i = 0; i < coords; i++)
1620 coord[i] = ir3_SHL_B(b, coord[i], 0, lod, 0);
1621 }
1622
1623 /*
1624 * lay out the first argument in the proper order:
1625 * - actual coordinates first
1626 * - shadow reference
1627 * - array index
1628 * - projection w
1629 * - starting at offset 4, dpdx.xy, dpdy.xy
1630 *
1631 * bias/lod go into the second arg
1632 */
1633
1634 /* insert tex coords: */
1635 for (i = 0; i < coords; i++)
1636 src0[nsrc0++] = coord[i];
1637
1638 if (coords == 1) {
1639 /* hw doesn't do 1d, so we treat it as 2d with
1640 * height of 1, and patch up the y coord.
1641 * TODO: y coord should be (int)0 in some cases..
1642 */
1643 src0[nsrc0++] = create_immed(b, fui(0.5));
1644 }
1645
1646 if (tex->is_shadow)
1647 src0[nsrc0++] = compare;
1648
1649 if (tex->is_array)
1650 src0[nsrc0++] = coord[coords];
1651
1652 if (has_proj) {
1653 src0[nsrc0++] = proj;
1654 flags |= IR3_INSTR_P;
1655 }
1656
1657 /* pad to 4, then ddx/ddy: */
1658 if (tex->op == nir_texop_txd) {
1659 while (nsrc0 < 4)
1660 src0[nsrc0++] = create_immed(b, fui(0.0));
1661 for (i = 0; i < coords; i++)
1662 src0[nsrc0++] = ddx[i];
1663 if (coords < 2)
1664 src0[nsrc0++] = create_immed(b, fui(0.0));
1665 for (i = 0; i < coords; i++)
1666 src0[nsrc0++] = ddy[i];
1667 if (coords < 2)
1668 src0[nsrc0++] = create_immed(b, fui(0.0));
1669 }
1670
1671 /*
1672 * second argument (if applicable):
1673 * - offsets
1674 * - lod
1675 * - bias
1676 */
1677 if (has_off | has_lod | has_bias) {
1678 if (has_off) {
1679 for (i = 0; i < coords; i++)
1680 src1[nsrc1++] = off[i];
1681 if (coords < 2)
1682 src1[nsrc1++] = create_immed(b, fui(0.0));
1683 flags |= IR3_INSTR_O;
1684 }
1685
1686 if (has_lod | has_bias)
1687 src1[nsrc1++] = lod;
1688 }
1689
1690 switch (tex->dest_type) {
1691 case nir_type_invalid:
1692 case nir_type_float:
1693 type = TYPE_F32;
1694 break;
1695 case nir_type_int:
1696 type = TYPE_S32;
1697 break;
1698 case nir_type_unsigned:
1699 case nir_type_bool:
1700 type = TYPE_U32;
1701 break;
1702 default:
1703 unreachable("bad dest_type");
1704 }
1705
1706 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_XYZW,
1707 flags, tex->sampler_index, tex->sampler_index,
1708 create_collect(b, src0, nsrc0),
1709 create_collect(b, src1, nsrc1));
1710
1711 split_dest(b, dst, sam, 4);
1712 }
1713
1714 static void
1715 emit_tex_query_levels(struct ir3_compile *ctx, nir_tex_instr *tex)
1716 {
1717 struct ir3_block *b = ctx->block;
1718 struct ir3_instruction **dst, *sam;
1719
1720 dst = get_dst(ctx, &tex->dest, 1);
1721
1722 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, TGSI_WRITEMASK_Z, 0,
1723 tex->sampler_index, tex->sampler_index, NULL, NULL);
1724
1725 /* even though there is only one component, since it ends
1726 * up in .z rather than .x, we need a split_dest()
1727 */
1728 split_dest(b, dst, sam, 3);
1729
1730 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1731 * the value in TEX_CONST_0 is zero-based.
1732 */
1733 if (ctx->levels_add_one)
1734 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
1735 }
1736
1737 static void
1738 emit_tex_txs(struct ir3_compile *ctx, nir_tex_instr *tex)
1739 {
1740 struct ir3_block *b = ctx->block;
1741 struct ir3_instruction **dst, *sam, *lod;
1742 unsigned flags, coords;
1743
1744 tex_info(tex, &flags, &coords);
1745
1746 dst = get_dst(ctx, &tex->dest, 4);
1747
1748 compile_assert(ctx, tex->num_srcs == 1);
1749 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
1750
1751 lod = get_src(ctx, &tex->src[0].src)[0];
1752
1753 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, TGSI_WRITEMASK_XYZW, flags,
1754 tex->sampler_index, tex->sampler_index, lod, NULL);
1755
1756 split_dest(b, dst, sam, 4);
1757
1758 /* Array size actually ends up in .w rather than .z. This doesn't
1759 * matter for miplevel 0, but for higher mips the value in z is
1760 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1761 * returned, which means that we have to add 1 to it for arrays.
1762 */
1763 if (tex->is_array) {
1764 if (ctx->levels_add_one) {
1765 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
1766 } else {
1767 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
1768 }
1769 }
1770 }
1771
1772 static void
1773 emit_phi(struct ir3_compile *ctx, nir_phi_instr *nphi)
1774 {
1775 struct ir3_instruction *phi, **dst;
1776
1777 /* NOTE: phi's should be lowered to scalar at this point */
1778 compile_assert(ctx, nphi->dest.ssa.num_components == 1);
1779
1780 dst = get_dst(ctx, &nphi->dest, 1);
1781
1782 phi = ir3_instr_create2(ctx->block, -1, OPC_META_PHI,
1783 1 + exec_list_length(&nphi->srcs));
1784 ir3_reg_create(phi, 0, 0); /* dst */
1785 phi->phi.nphi = nphi;
1786
1787 dst[0] = phi;
1788 }
1789
1790 /* phi instructions are left partially constructed. We don't resolve
1791 * their srcs until the end of the block, since (eg. loops) one of
1792 * the phi's srcs might be defined after the phi due to back edges in
1793 * the CFG.
1794 */
1795 static void
1796 resolve_phis(struct ir3_compile *ctx, struct ir3_block *block)
1797 {
1798 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1799 nir_phi_instr *nphi;
1800
1801 /* phi's only come at start of block: */
1802 if (!(is_meta(instr) && (instr->opc == OPC_META_PHI)))
1803 break;
1804
1805 if (!instr->phi.nphi)
1806 break;
1807
1808 nphi = instr->phi.nphi;
1809 instr->phi.nphi = NULL;
1810
1811 foreach_list_typed(nir_phi_src, nsrc, node, &nphi->srcs) {
1812 struct ir3_instruction *src = get_src(ctx, &nsrc->src)[0];
1813 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
1814 }
1815 }
1816
1817 resolve_array_phis(ctx, block);
1818 }
1819
1820 static void
1821 emit_jump(struct ir3_compile *ctx, nir_jump_instr *jump)
1822 {
1823 switch (jump->type) {
1824 case nir_jump_break:
1825 case nir_jump_continue:
1826 /* I *think* we can simply just ignore this, and use the
1827 * successor block link to figure out where we need to
1828 * jump to for break/continue
1829 */
1830 break;
1831 default:
1832 compile_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
1833 break;
1834 }
1835 }
1836
1837 static void
1838 emit_instr(struct ir3_compile *ctx, nir_instr *instr)
1839 {
1840 switch (instr->type) {
1841 case nir_instr_type_alu:
1842 emit_alu(ctx, nir_instr_as_alu(instr));
1843 break;
1844 case nir_instr_type_intrinsic:
1845 emit_intrinisic(ctx, nir_instr_as_intrinsic(instr));
1846 break;
1847 case nir_instr_type_load_const:
1848 emit_load_const(ctx, nir_instr_as_load_const(instr));
1849 break;
1850 case nir_instr_type_ssa_undef:
1851 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1852 break;
1853 case nir_instr_type_tex: {
1854 nir_tex_instr *tex = nir_instr_as_tex(instr);
1855 /* couple tex instructions get special-cased:
1856 */
1857 switch (tex->op) {
1858 case nir_texop_txs:
1859 emit_tex_txs(ctx, tex);
1860 break;
1861 case nir_texop_query_levels:
1862 emit_tex_query_levels(ctx, tex);
1863 break;
1864 default:
1865 emit_tex(ctx, tex);
1866 break;
1867 }
1868 break;
1869 }
1870 case nir_instr_type_phi:
1871 emit_phi(ctx, nir_instr_as_phi(instr));
1872 break;
1873 case nir_instr_type_jump:
1874 emit_jump(ctx, nir_instr_as_jump(instr));
1875 break;
1876 case nir_instr_type_call:
1877 case nir_instr_type_parallel_copy:
1878 compile_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
1879 break;
1880 }
1881 }
1882
1883 static struct ir3_block *
1884 get_block(struct ir3_compile *ctx, nir_block *nblock)
1885 {
1886 struct ir3_block *block;
1887 struct hash_entry *entry;
1888 entry = _mesa_hash_table_search(ctx->block_ht, nblock);
1889 if (entry)
1890 return entry->data;
1891
1892 block = ir3_block_create(ctx->ir);
1893 block->nblock = nblock;
1894 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
1895
1896 return block;
1897 }
1898
1899 static void
1900 emit_block(struct ir3_compile *ctx, nir_block *nblock)
1901 {
1902 struct ir3_block *block = get_block(ctx, nblock);
1903
1904 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
1905 if (nblock->successors[i]) {
1906 block->successors[i] =
1907 get_block(ctx, nblock->successors[i]);
1908 }
1909 }
1910
1911 ctx->block = block;
1912 list_addtail(&block->node, &ctx->ir->block_list);
1913
1914 nir_foreach_instr(nblock, instr) {
1915 emit_instr(ctx, instr);
1916 if (ctx->error)
1917 return;
1918 }
1919 }
1920
1921 static void emit_cf_list(struct ir3_compile *ctx, struct exec_list *list);
1922
1923 static void
1924 emit_if(struct ir3_compile *ctx, nir_if *nif)
1925 {
1926 struct ir3_instruction *condition = get_src(ctx, &nif->condition)[0];
1927
1928 ctx->block->condition =
1929 get_predicate(ctx, ir3_b2n(condition->block, condition));
1930
1931 emit_cf_list(ctx, &nif->then_list);
1932 emit_cf_list(ctx, &nif->else_list);
1933 }
1934
1935 static void
1936 emit_loop(struct ir3_compile *ctx, nir_loop *nloop)
1937 {
1938 emit_cf_list(ctx, &nloop->body);
1939 }
1940
1941 static void
1942 emit_cf_list(struct ir3_compile *ctx, struct exec_list *list)
1943 {
1944 foreach_list_typed(nir_cf_node, node, node, list) {
1945 switch (node->type) {
1946 case nir_cf_node_block:
1947 emit_block(ctx, nir_cf_node_as_block(node));
1948 break;
1949 case nir_cf_node_if:
1950 emit_if(ctx, nir_cf_node_as_if(node));
1951 break;
1952 case nir_cf_node_loop:
1953 emit_loop(ctx, nir_cf_node_as_loop(node));
1954 break;
1955 case nir_cf_node_function:
1956 compile_error(ctx, "TODO\n");
1957 break;
1958 }
1959 }
1960 }
1961
1962 static void
1963 emit_function(struct ir3_compile *ctx, nir_function_impl *impl)
1964 {
1965 emit_cf_list(ctx, &impl->body);
1966 emit_block(ctx, impl->end_block);
1967
1968 /* at this point, we should have a single empty block,
1969 * into which we emit the 'end' instruction.
1970 */
1971 compile_assert(ctx, list_empty(&ctx->block->instr_list));
1972 ir3_END(ctx->block);
1973 }
1974
1975 static void
1976 setup_input(struct ir3_compile *ctx, nir_variable *in)
1977 {
1978 struct ir3_shader_variant *so = ctx->so;
1979 unsigned array_len = MAX2(glsl_get_length(in->type), 1);
1980 unsigned ncomp = glsl_get_components(in->type);
1981 /* XXX: map loc slots to semantics */
1982 unsigned semantic_name = in->data.location;
1983 unsigned semantic_index = in->data.index;
1984 unsigned n = in->data.driver_location;
1985
1986 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1987 semantic_name, semantic_index, array_len,
1988 ncomp, n);
1989
1990 so->inputs[n].semantic =
1991 ir3_semantic_name(semantic_name, semantic_index);
1992 so->inputs[n].compmask = (1 << ncomp) - 1;
1993 so->inputs[n].inloc = ctx->next_inloc;
1994 so->inputs[n].interpolate = 0;
1995 so->inputs_count = MAX2(so->inputs_count, n + 1);
1996
1997 /* the fdN_program_emit() code expects tgsi consts here, so map
1998 * things back to tgsi for now:
1999 */
2000 switch (in->data.interpolation) {
2001 case INTERP_QUALIFIER_FLAT:
2002 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
2003 break;
2004 case INTERP_QUALIFIER_NOPERSPECTIVE:
2005 so->inputs[n].interpolate = TGSI_INTERPOLATE_LINEAR;
2006 break;
2007 case INTERP_QUALIFIER_SMOOTH:
2008 so->inputs[n].interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
2009 break;
2010 }
2011
2012 for (int i = 0; i < ncomp; i++) {
2013 struct ir3_instruction *instr = NULL;
2014 unsigned idx = (n * 4) + i;
2015
2016 if (ctx->so->type == SHADER_FRAGMENT) {
2017 if (semantic_name == TGSI_SEMANTIC_POSITION) {
2018 so->inputs[n].bary = false;
2019 so->frag_coord = true;
2020 instr = create_frag_coord(ctx, i);
2021 } else if (semantic_name == TGSI_SEMANTIC_FACE) {
2022 so->inputs[n].bary = false;
2023 so->frag_face = true;
2024 instr = create_frag_face(ctx, i);
2025 } else {
2026 bool use_ldlv = false;
2027
2028 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2029 * from the semantic name:
2030 */
2031 if ((in->data.interpolation == INTERP_QUALIFIER_NONE) &&
2032 ((semantic_name == TGSI_SEMANTIC_COLOR) ||
2033 (semantic_name == TGSI_SEMANTIC_BCOLOR)))
2034 so->inputs[n].interpolate = TGSI_INTERPOLATE_COLOR;
2035
2036 if (ctx->flat_bypass) {
2037 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2038 * from the semantic name:
2039 */
2040 switch (so->inputs[n].interpolate) {
2041 case TGSI_INTERPOLATE_COLOR:
2042 if (!ctx->so->key.rasterflat)
2043 break;
2044 /* fallthrough */
2045 case TGSI_INTERPOLATE_CONSTANT:
2046 use_ldlv = true;
2047 break;
2048 }
2049 }
2050
2051 so->inputs[n].bary = true;
2052
2053 instr = create_frag_input(ctx,
2054 so->inputs[n].inloc + i - 8, use_ldlv);
2055 }
2056 } else {
2057 instr = create_input(ctx->block, NULL, idx);
2058 }
2059
2060 ctx->ir->inputs[idx] = instr;
2061 }
2062
2063 if (so->inputs[n].bary || (ctx->so->type == SHADER_VERTEX)) {
2064 ctx->next_inloc += ncomp;
2065 so->total_in += ncomp;
2066 }
2067 }
2068
2069 static void
2070 setup_output(struct ir3_compile *ctx, nir_variable *out)
2071 {
2072 struct ir3_shader_variant *so = ctx->so;
2073 unsigned array_len = MAX2(glsl_get_length(out->type), 1);
2074 unsigned ncomp = glsl_get_components(out->type);
2075 /* XXX: map loc slots to semantics */
2076 unsigned semantic_name = out->data.location;
2077 unsigned semantic_index = out->data.index;
2078 unsigned n = out->data.driver_location;
2079 unsigned comp = 0;
2080
2081 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
2082 semantic_name, semantic_index, array_len,
2083 ncomp, n);
2084
2085 if (ctx->so->type == SHADER_VERTEX) {
2086 switch (semantic_name) {
2087 case TGSI_SEMANTIC_POSITION:
2088 so->writes_pos = true;
2089 break;
2090 case TGSI_SEMANTIC_PSIZE:
2091 so->writes_psize = true;
2092 break;
2093 case TGSI_SEMANTIC_COLOR:
2094 case TGSI_SEMANTIC_BCOLOR:
2095 case TGSI_SEMANTIC_GENERIC:
2096 case TGSI_SEMANTIC_FOG:
2097 case TGSI_SEMANTIC_TEXCOORD:
2098 break;
2099 default:
2100 compile_error(ctx, "unknown VS semantic name: %s\n",
2101 tgsi_semantic_names[semantic_name]);
2102 }
2103 } else {
2104 switch (semantic_name) {
2105 case TGSI_SEMANTIC_POSITION:
2106 comp = 2; /* tgsi will write to .z component */
2107 so->writes_pos = true;
2108 break;
2109 case TGSI_SEMANTIC_COLOR:
2110 if (semantic_index == -1) {
2111 semantic_index = 0;
2112 so->color0_mrt = 1;
2113 }
2114 break;
2115 default:
2116 compile_error(ctx, "unknown FS semantic name: %s\n",
2117 tgsi_semantic_names[semantic_name]);
2118 }
2119 }
2120
2121 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2122
2123 so->outputs[n].semantic =
2124 ir3_semantic_name(semantic_name, semantic_index);
2125 so->outputs[n].regid = regid(n, comp);
2126 so->outputs_count = MAX2(so->outputs_count, n + 1);
2127
2128 for (int i = 0; i < ncomp; i++) {
2129 unsigned idx = (n * 4) + i;
2130
2131 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2132 }
2133 }
2134
2135 static void
2136 emit_instructions(struct ir3_compile *ctx)
2137 {
2138 unsigned ninputs, noutputs;
2139 nir_function_impl *fxn = NULL;
2140
2141 /* Find the main function: */
2142 nir_foreach_overload(ctx->s, overload) {
2143 compile_assert(ctx, strcmp(overload->function->name, "main") == 0);
2144 compile_assert(ctx, overload->impl);
2145 fxn = overload->impl;
2146 break;
2147 }
2148
2149 ninputs = exec_list_length(&ctx->s->inputs) * 4;
2150 noutputs = exec_list_length(&ctx->s->outputs) * 4;
2151
2152 /* we need to allocate big enough outputs array so that
2153 * we can stuff the kill's at the end. Likewise for vtx
2154 * shaders, we need to leave room for sysvals:
2155 */
2156 if (ctx->so->type == SHADER_FRAGMENT) {
2157 noutputs += ARRAY_SIZE(ctx->kill);
2158 } else if (ctx->so->type == SHADER_VERTEX) {
2159 ninputs += 8;
2160 }
2161
2162 ctx->ir = ir3_create(ctx->compiler, ninputs, noutputs);
2163
2164 /* Create inputs in first block: */
2165 ctx->block = get_block(ctx, fxn->start_block);
2166 ctx->in_block = ctx->block;
2167 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2168
2169 if (ctx->so->type == SHADER_FRAGMENT) {
2170 ctx->ir->noutputs -= ARRAY_SIZE(ctx->kill);
2171 } else if (ctx->so->type == SHADER_VERTEX) {
2172 ctx->ir->ninputs -= 8;
2173 }
2174
2175 /* for fragment shader, we have a single input register (usually
2176 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2177 */
2178 if (ctx->so->type == SHADER_FRAGMENT) {
2179 // TODO maybe a helper for fi since we need it a few places..
2180 struct ir3_instruction *instr;
2181 instr = ir3_instr_create(ctx->block, -1, OPC_META_FI);
2182 ir3_reg_create(instr, 0, 0);
2183 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.x */
2184 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.y */
2185 ctx->frag_pos = instr;
2186 }
2187
2188 /* Setup inputs: */
2189 foreach_list_typed(nir_variable, var, node, &ctx->s->inputs) {
2190 setup_input(ctx, var);
2191 }
2192
2193 /* Setup outputs: */
2194 foreach_list_typed(nir_variable, var, node, &ctx->s->outputs) {
2195 setup_output(ctx, var);
2196 }
2197
2198 /* Setup variables (which should only be arrays): */
2199 foreach_list_typed(nir_variable, var, node, &ctx->s->globals) {
2200 declare_var(ctx, var);
2201 }
2202
2203 /* And emit the body: */
2204 ctx->impl = fxn;
2205 emit_function(ctx, fxn);
2206
2207 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2208 resolve_phis(ctx, block);
2209 }
2210 }
2211
2212 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2213 * for a fragment shader are just bary.f instructions. The *actual* inputs
2214 * from the hw perspective are the frag_pos and optionally frag_coord and
2215 * frag_face.
2216 */
2217 static void
2218 fixup_frag_inputs(struct ir3_compile *ctx)
2219 {
2220 struct ir3_shader_variant *so = ctx->so;
2221 struct ir3 *ir = ctx->ir;
2222 struct ir3_instruction **inputs;
2223 struct ir3_instruction *instr;
2224 int n, regid = 0;
2225
2226 ir->ninputs = 0;
2227
2228 n = 4; /* always have frag_pos */
2229 n += COND(so->frag_face, 4);
2230 n += COND(so->frag_coord, 4);
2231
2232 inputs = ir3_alloc(ctx->ir, n * (sizeof(struct ir3_instruction *)));
2233
2234 if (so->frag_face) {
2235 /* this ultimately gets assigned to hr0.x so doesn't conflict
2236 * with frag_coord/frag_pos..
2237 */
2238 inputs[ir->ninputs++] = ctx->frag_face;
2239 ctx->frag_face->regs[0]->num = 0;
2240
2241 /* remaining channels not used, but let's avoid confusing
2242 * other parts that expect inputs to come in groups of vec4
2243 */
2244 inputs[ir->ninputs++] = NULL;
2245 inputs[ir->ninputs++] = NULL;
2246 inputs[ir->ninputs++] = NULL;
2247 }
2248
2249 /* since we don't know where to set the regid for frag_coord,
2250 * we have to use r0.x for it. But we don't want to *always*
2251 * use r1.x for frag_pos as that could increase the register
2252 * footprint on simple shaders:
2253 */
2254 if (so->frag_coord) {
2255 ctx->frag_coord[0]->regs[0]->num = regid++;
2256 ctx->frag_coord[1]->regs[0]->num = regid++;
2257 ctx->frag_coord[2]->regs[0]->num = regid++;
2258 ctx->frag_coord[3]->regs[0]->num = regid++;
2259
2260 inputs[ir->ninputs++] = ctx->frag_coord[0];
2261 inputs[ir->ninputs++] = ctx->frag_coord[1];
2262 inputs[ir->ninputs++] = ctx->frag_coord[2];
2263 inputs[ir->ninputs++] = ctx->frag_coord[3];
2264 }
2265
2266 /* we always have frag_pos: */
2267 so->pos_regid = regid;
2268
2269 /* r0.x */
2270 instr = create_input(ctx->in_block, NULL, ir->ninputs);
2271 instr->regs[0]->num = regid++;
2272 inputs[ir->ninputs++] = instr;
2273 ctx->frag_pos->regs[1]->instr = instr;
2274
2275 /* r0.y */
2276 instr = create_input(ctx->in_block, NULL, ir->ninputs);
2277 instr->regs[0]->num = regid++;
2278 inputs[ir->ninputs++] = instr;
2279 ctx->frag_pos->regs[2]->instr = instr;
2280
2281 ir->inputs = inputs;
2282 }
2283
2284 int
2285 ir3_compile_shader_nir(struct ir3_compiler *compiler,
2286 struct ir3_shader_variant *so,
2287 const struct tgsi_token *tokens,
2288 struct ir3_shader_key key)
2289 {
2290 struct ir3_compile *ctx;
2291 struct ir3 *ir;
2292 struct ir3_instruction **inputs;
2293 unsigned i, j, actual_in;
2294 int ret = 0, max_bary;
2295
2296 assert(!so->ir);
2297
2298 ctx = compile_init(compiler, so, tokens);
2299 if (!ctx) {
2300 DBG("INIT failed!");
2301 ret = -1;
2302 goto out;
2303 }
2304
2305 emit_instructions(ctx);
2306
2307 if (ctx->error) {
2308 DBG("EMIT failed!");
2309 ret = -1;
2310 goto out;
2311 }
2312
2313 ir = so->ir = ctx->ir;
2314
2315 /* keep track of the inputs from TGSI perspective.. */
2316 inputs = ir->inputs;
2317
2318 /* but fixup actual inputs for frag shader: */
2319 if (so->type == SHADER_FRAGMENT)
2320 fixup_frag_inputs(ctx);
2321
2322 /* at this point, for binning pass, throw away unneeded outputs: */
2323 if (key.binning_pass) {
2324 for (i = 0, j = 0; i < so->outputs_count; i++) {
2325 unsigned name = sem2name(so->outputs[i].semantic);
2326 unsigned idx = sem2idx(so->outputs[i].semantic);
2327
2328 /* throw away everything but first position/psize */
2329 if ((idx == 0) && ((name == TGSI_SEMANTIC_POSITION) ||
2330 (name == TGSI_SEMANTIC_PSIZE))) {
2331 if (i != j) {
2332 so->outputs[j] = so->outputs[i];
2333 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
2334 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
2335 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
2336 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
2337 }
2338 j++;
2339 }
2340 }
2341 so->outputs_count = j;
2342 ir->noutputs = j * 4;
2343 }
2344
2345 /* if we want half-precision outputs, mark the output registers
2346 * as half:
2347 */
2348 if (key.half_precision) {
2349 for (i = 0; i < ir->noutputs; i++) {
2350 struct ir3_instruction *out = ir->outputs[i];
2351 if (!out)
2352 continue;
2353 out->regs[0]->flags |= IR3_REG_HALF;
2354 /* output could be a fanout (ie. texture fetch output)
2355 * in which case we need to propagate the half-reg flag
2356 * up to the definer so that RA sees it:
2357 */
2358 if (is_meta(out) && (out->opc == OPC_META_FO)) {
2359 out = out->regs[1]->instr;
2360 out->regs[0]->flags |= IR3_REG_HALF;
2361 }
2362
2363 if (out->category == 1) {
2364 out->cat1.dst_type = half_type(out->cat1.dst_type);
2365 }
2366 }
2367 }
2368
2369 /* at this point, we want the kill's in the outputs array too,
2370 * so that they get scheduled (since they have no dst).. we've
2371 * already ensured that the array is big enough in push_block():
2372 */
2373 if (so->type == SHADER_FRAGMENT) {
2374 for (i = 0; i < ctx->kill_count; i++)
2375 ir->outputs[ir->noutputs++] = ctx->kill[i];
2376 }
2377
2378 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2379 printf("BEFORE CP:\n");
2380 ir3_print(ir);
2381 }
2382
2383 ir3_cp(ir);
2384
2385 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2386 printf("BEFORE GROUPING:\n");
2387 ir3_print(ir);
2388 }
2389
2390 /* Group left/right neighbors, inserting mov's where needed to
2391 * solve conflicts:
2392 */
2393 ir3_group(ir);
2394
2395 ir3_depth(ir);
2396
2397 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2398 printf("AFTER DEPTH:\n");
2399 ir3_print(ir);
2400 }
2401
2402 ret = ir3_sched(ir);
2403 if (ret) {
2404 DBG("SCHED failed!");
2405 goto out;
2406 }
2407
2408 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2409 printf("AFTER SCHED:\n");
2410 ir3_print(ir);
2411 }
2412
2413 ret = ir3_ra(ir, so->type, so->frag_coord, so->frag_face);
2414 if (ret) {
2415 DBG("RA failed!");
2416 goto out;
2417 }
2418
2419 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2420 printf("AFTER RA:\n");
2421 ir3_print(ir);
2422 }
2423
2424 ir3_legalize(ir, &so->has_samp, &max_bary);
2425
2426 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2427 printf("AFTER LEGALIZE:\n");
2428 ir3_print(ir);
2429 }
2430
2431 /* fixup input/outputs: */
2432 for (i = 0; i < so->outputs_count; i++) {
2433 so->outputs[i].regid = ir->outputs[i*4]->regs[0]->num;
2434 /* preserve hack for depth output.. tgsi writes depth to .z,
2435 * but what we give the hw is the scalar register:
2436 */
2437 if ((so->type == SHADER_FRAGMENT) &&
2438 (sem2name(so->outputs[i].semantic) == TGSI_SEMANTIC_POSITION))
2439 so->outputs[i].regid += 2;
2440 }
2441
2442 /* Note that some or all channels of an input may be unused: */
2443 actual_in = 0;
2444 for (i = 0; i < so->inputs_count; i++) {
2445 unsigned j, regid = ~0, compmask = 0;
2446 so->inputs[i].ncomp = 0;
2447 for (j = 0; j < 4; j++) {
2448 struct ir3_instruction *in = inputs[(i*4) + j];
2449 if (in) {
2450 compmask |= (1 << j);
2451 regid = in->regs[0]->num - j;
2452 actual_in++;
2453 so->inputs[i].ncomp++;
2454 }
2455 }
2456 so->inputs[i].regid = regid;
2457 so->inputs[i].compmask = compmask;
2458 }
2459
2460 /* fragment shader always gets full vec4's even if it doesn't
2461 * fetch all components, but vertex shader we need to update
2462 * with the actual number of components fetch, otherwise thing
2463 * will hang due to mismaptch between VFD_DECODE's and
2464 * TOTALATTRTOVS
2465 */
2466 if (so->type == SHADER_VERTEX)
2467 so->total_in = actual_in;
2468 else
2469 so->total_in = align(max_bary + 1, 4);
2470
2471 out:
2472 if (ret) {
2473 if (so->ir)
2474 ir3_destroy(so->ir);
2475 so->ir = NULL;
2476 }
2477 compile_free(ctx);
2478
2479 return ret;
2480 }