fa13c4076dcc0c803d6fa007db236b36bae09340
[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 mov->address = address;
641
642 array_insert(ctx->ir->indirects, mov);
643
644 return mov;
645 }
646
647 static struct ir3_instruction *
648 create_collect(struct ir3_block *block, struct ir3_instruction **arr,
649 unsigned arrsz)
650 {
651 struct ir3_instruction *collect;
652
653 if (arrsz == 0)
654 return NULL;
655
656 collect = ir3_instr_create2(block, -1, OPC_META_FI, 1 + arrsz);
657 ir3_reg_create(collect, 0, 0); /* dst */
658 for (unsigned i = 0; i < arrsz; i++)
659 ir3_reg_create(collect, 0, IR3_REG_SSA)->instr = arr[i];
660
661 return collect;
662 }
663
664 static struct ir3_instruction *
665 create_indirect_load(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
666 struct ir3_instruction *address, struct ir3_instruction *collect)
667 {
668 struct ir3_block *block = ctx->block;
669 struct ir3_instruction *mov;
670 struct ir3_register *src;
671
672 mov = ir3_instr_create(block, 1, 0);
673 mov->cat1.src_type = TYPE_U32;
674 mov->cat1.dst_type = TYPE_U32;
675 ir3_reg_create(mov, 0, 0);
676 src = ir3_reg_create(mov, 0, IR3_REG_SSA | IR3_REG_RELATIV);
677 src->instr = collect;
678 src->size = arrsz;
679 src->offset = n;
680 mov->address = address;
681
682 array_insert(ctx->ir->indirects, mov);
683
684 return mov;
685 }
686
687 static struct ir3_instruction *
688 create_indirect_store(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
689 struct ir3_instruction *src, struct ir3_instruction *address,
690 struct ir3_instruction *collect)
691 {
692 struct ir3_block *block = ctx->block;
693 struct ir3_instruction *mov;
694 struct ir3_register *dst;
695
696 mov = ir3_instr_create(block, 1, 0);
697 mov->cat1.src_type = TYPE_U32;
698 mov->cat1.dst_type = TYPE_U32;
699 dst = ir3_reg_create(mov, 0, IR3_REG_RELATIV);
700 dst->size = arrsz;
701 dst->offset = n;
702 ir3_reg_create(mov, 0, IR3_REG_SSA)->instr = src;
703 mov->address = address;
704 mov->fanin = collect;
705
706 array_insert(ctx->ir->indirects, mov);
707
708 return mov;
709 }
710
711 static struct ir3_instruction *
712 create_input(struct ir3_block *block, struct ir3_instruction *instr,
713 unsigned n)
714 {
715 struct ir3_instruction *in;
716
717 in = ir3_instr_create(block, -1, OPC_META_INPUT);
718 in->inout.block = block;
719 ir3_reg_create(in, n, 0);
720 if (instr)
721 ir3_reg_create(in, 0, IR3_REG_SSA)->instr = instr;
722
723 return in;
724 }
725
726 static struct ir3_instruction *
727 create_frag_input(struct ir3_compile *ctx, unsigned n, bool use_ldlv)
728 {
729 struct ir3_block *block = ctx->block;
730 struct ir3_instruction *instr;
731 struct ir3_instruction *inloc = create_immed(block, n);
732
733 if (use_ldlv) {
734 instr = ir3_LDLV(block, inloc, 0, create_immed(block, 1), 0);
735 instr->cat6.type = TYPE_U32;
736 instr->cat6.iim_val = 1;
737 } else {
738 instr = ir3_BARY_F(block, inloc, 0, ctx->frag_pos, 0);
739 instr->regs[2]->wrmask = 0x3;
740 }
741
742 return instr;
743 }
744
745 static struct ir3_instruction *
746 create_frag_coord(struct ir3_compile *ctx, unsigned comp)
747 {
748 struct ir3_block *block = ctx->block;
749 struct ir3_instruction *instr;
750
751 compile_assert(ctx, !ctx->frag_coord[comp]);
752
753 ctx->frag_coord[comp] = create_input(ctx->block, NULL, 0);
754
755 switch (comp) {
756 case 0: /* .x */
757 case 1: /* .y */
758 /* for frag_coord, we get unsigned values.. we need
759 * to subtract (integer) 8 and divide by 16 (right-
760 * shift by 4) then convert to float:
761 *
762 * sub.s tmp, src, 8
763 * shr.b tmp, tmp, 4
764 * mov.u32f32 dst, tmp
765 *
766 */
767 instr = ir3_SUB_S(block, ctx->frag_coord[comp], 0,
768 create_immed(block, 8), 0);
769 instr = ir3_SHR_B(block, instr, 0,
770 create_immed(block, 4), 0);
771 instr = ir3_COV(block, instr, TYPE_U32, TYPE_F32);
772
773 return instr;
774 case 2: /* .z */
775 case 3: /* .w */
776 default:
777 /* seems that we can use these as-is: */
778 return ctx->frag_coord[comp];
779 }
780 }
781
782 static struct ir3_instruction *
783 create_frag_face(struct ir3_compile *ctx, unsigned comp)
784 {
785 struct ir3_block *block = ctx->block;
786 struct ir3_instruction *instr;
787
788 switch (comp) {
789 case 0: /* .x */
790 compile_assert(ctx, !ctx->frag_face);
791
792 ctx->frag_face = create_input(block, NULL, 0);
793 ctx->frag_face->regs[0]->flags |= IR3_REG_HALF;
794
795 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
796 * positive vs negative float.. and piglit further seems to
797 * expect -1.0 or 1.0:
798 *
799 * mul.s tmp, hr0.x, 2
800 * add.s tmp, tmp, 1
801 * mov.s32f32, dst, tmp
802 *
803 */
804 instr = ir3_MUL_S(block, ctx->frag_face, 0,
805 create_immed(block, 2), 0);
806 instr = ir3_ADD_S(block, instr, 0,
807 create_immed(block, 1), 0);
808 instr = ir3_COV(block, instr, TYPE_S32, TYPE_F32);
809
810 return instr;
811 case 1: /* .y */
812 case 2: /* .z */
813 return create_immed(block, fui(0.0));
814 default:
815 case 3: /* .w */
816 return create_immed(block, fui(1.0));
817 }
818 }
819
820 /* helper for instructions that produce multiple consecutive scalar
821 * outputs which need to have a split/fanout meta instruction inserted
822 */
823 static void
824 split_dest(struct ir3_block *block, struct ir3_instruction **dst,
825 struct ir3_instruction *src, unsigned n)
826 {
827 struct ir3_instruction *prev = NULL;
828 for (int i = 0, j = 0; i < n; i++) {
829 struct ir3_instruction *split =
830 ir3_instr_create(block, -1, OPC_META_FO);
831 ir3_reg_create(split, 0, IR3_REG_SSA);
832 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = src;
833 split->fo.off = i;
834
835 if (prev) {
836 split->cp.left = prev;
837 split->cp.left_cnt++;
838 prev->cp.right = split;
839 prev->cp.right_cnt++;
840 }
841 prev = split;
842
843 if (src->regs[0]->wrmask & (1 << i))
844 dst[j++] = split;
845 }
846 }
847
848 /*
849 * Adreno uses uint rather than having dedicated bool type,
850 * which (potentially) requires some conversion, in particular
851 * when using output of an bool instr to int input, or visa
852 * versa.
853 *
854 * | Adreno | NIR |
855 * -------+---------+-------+-
856 * true | 1 | ~0 |
857 * false | 0 | 0 |
858 *
859 * To convert from an adreno bool (uint) to nir, use:
860 *
861 * absneg.s dst, (neg)src
862 *
863 * To convert back in the other direction:
864 *
865 * absneg.s dst, (abs)arc
866 *
867 * The CP step can clean up the absneg.s that cancel each other
868 * out, and with a slight bit of extra cleverness (to recognize
869 * the instructions which produce either a 0 or 1) can eliminate
870 * the absneg.s's completely when an instruction that wants
871 * 0/1 consumes the result. For example, when a nir 'bcsel'
872 * consumes the result of 'feq'. So we should be able to get by
873 * without a boolean resolve step, and without incuring any
874 * extra penalty in instruction count.
875 */
876
877 /* NIR bool -> native (adreno): */
878 static struct ir3_instruction *
879 ir3_b2n(struct ir3_block *block, struct ir3_instruction *instr)
880 {
881 return ir3_ABSNEG_S(block, instr, IR3_REG_SABS);
882 }
883
884 /* native (adreno) -> NIR bool: */
885 static struct ir3_instruction *
886 ir3_n2b(struct ir3_block *block, struct ir3_instruction *instr)
887 {
888 return ir3_ABSNEG_S(block, instr, IR3_REG_SNEG);
889 }
890
891 /*
892 * alu/sfu instructions:
893 */
894
895 static void
896 emit_alu(struct ir3_compile *ctx, nir_alu_instr *alu)
897 {
898 const nir_op_info *info = &nir_op_infos[alu->op];
899 struct ir3_instruction **dst, *src[info->num_inputs];
900 struct ir3_block *b = ctx->block;
901
902 dst = get_dst(ctx, &alu->dest.dest, MAX2(info->output_size, 1));
903
904 /* Vectors are special in that they have non-scalarized writemasks,
905 * and just take the first swizzle channel for each argument in
906 * order into each writemask channel.
907 */
908 if ((alu->op == nir_op_vec2) ||
909 (alu->op == nir_op_vec3) ||
910 (alu->op == nir_op_vec4)) {
911
912 for (int i = 0; i < info->num_inputs; i++) {
913 nir_alu_src *asrc = &alu->src[i];
914
915 compile_assert(ctx, !asrc->abs);
916 compile_assert(ctx, !asrc->negate);
917
918 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[0]];
919 if (!src[i])
920 src[i] = create_immed(ctx->block, 0);
921 dst[i] = ir3_MOV(b, src[i], TYPE_U32);
922 }
923
924 return;
925 }
926
927 /* General case: We can just grab the one used channel per src. */
928 for (int i = 0; i < info->num_inputs; i++) {
929 unsigned chan = ffs(alu->dest.write_mask) - 1;
930 nir_alu_src *asrc = &alu->src[i];
931
932 compile_assert(ctx, !asrc->abs);
933 compile_assert(ctx, !asrc->negate);
934
935 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[chan]];
936
937 compile_assert(ctx, src[i]);
938 }
939
940 switch (alu->op) {
941 case nir_op_f2i:
942 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_S32);
943 break;
944 case nir_op_f2u:
945 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_U32);
946 break;
947 case nir_op_i2f:
948 dst[0] = ir3_COV(b, src[0], TYPE_S32, TYPE_F32);
949 break;
950 case nir_op_u2f:
951 dst[0] = ir3_COV(b, src[0], TYPE_U32, TYPE_F32);
952 break;
953 case nir_op_imov:
954 dst[0] = ir3_MOV(b, src[0], TYPE_S32);
955 break;
956 case nir_op_fmov:
957 dst[0] = ir3_MOV(b, src[0], TYPE_F32);
958 break;
959 case nir_op_f2b:
960 dst[0] = ir3_CMPS_F(b, src[0], 0, create_immed(b, fui(0.0)), 0);
961 dst[0]->cat2.condition = IR3_COND_NE;
962 dst[0] = ir3_n2b(b, dst[0]);
963 break;
964 case nir_op_b2f:
965 dst[0] = ir3_COV(b, ir3_b2n(b, src[0]), TYPE_U32, TYPE_F32);
966 break;
967 case nir_op_b2i:
968 dst[0] = ir3_b2n(b, src[0]);
969 break;
970 case nir_op_i2b:
971 dst[0] = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
972 dst[0]->cat2.condition = IR3_COND_NE;
973 dst[0] = ir3_n2b(b, dst[0]);
974 break;
975
976 case nir_op_fneg:
977 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FNEG);
978 break;
979 case nir_op_fabs:
980 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FABS);
981 break;
982 case nir_op_fmax:
983 dst[0] = ir3_MAX_F(b, src[0], 0, src[1], 0);
984 break;
985 case nir_op_fmin:
986 dst[0] = ir3_MIN_F(b, src[0], 0, src[1], 0);
987 break;
988 case nir_op_fmul:
989 dst[0] = ir3_MUL_F(b, src[0], 0, src[1], 0);
990 break;
991 case nir_op_fadd:
992 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], 0);
993 break;
994 case nir_op_fsub:
995 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], IR3_REG_FNEG);
996 break;
997 case nir_op_ffma:
998 dst[0] = ir3_MAD_F32(b, src[0], 0, src[1], 0, src[2], 0);
999 break;
1000 case nir_op_fddx:
1001 dst[0] = ir3_DSX(b, src[0], 0);
1002 dst[0]->cat5.type = TYPE_F32;
1003 break;
1004 case nir_op_fddy:
1005 dst[0] = ir3_DSY(b, src[0], 0);
1006 dst[0]->cat5.type = TYPE_F32;
1007 break;
1008 break;
1009 case nir_op_flt:
1010 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1011 dst[0]->cat2.condition = IR3_COND_LT;
1012 dst[0] = ir3_n2b(b, dst[0]);
1013 break;
1014 case nir_op_fge:
1015 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1016 dst[0]->cat2.condition = IR3_COND_GE;
1017 dst[0] = ir3_n2b(b, dst[0]);
1018 break;
1019 case nir_op_feq:
1020 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1021 dst[0]->cat2.condition = IR3_COND_EQ;
1022 dst[0] = ir3_n2b(b, dst[0]);
1023 break;
1024 case nir_op_fne:
1025 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1026 dst[0]->cat2.condition = IR3_COND_NE;
1027 dst[0] = ir3_n2b(b, dst[0]);
1028 break;
1029 case nir_op_fceil:
1030 dst[0] = ir3_CEIL_F(b, src[0], 0);
1031 break;
1032 case nir_op_ffloor:
1033 dst[0] = ir3_FLOOR_F(b, src[0], 0);
1034 break;
1035 case nir_op_ftrunc:
1036 dst[0] = ir3_TRUNC_F(b, src[0], 0);
1037 break;
1038 case nir_op_fround_even:
1039 dst[0] = ir3_RNDNE_F(b, src[0], 0);
1040 break;
1041 case nir_op_fsign:
1042 dst[0] = ir3_SIGN_F(b, src[0], 0);
1043 break;
1044
1045 case nir_op_fsin:
1046 dst[0] = ir3_SIN(b, src[0], 0);
1047 break;
1048 case nir_op_fcos:
1049 dst[0] = ir3_COS(b, src[0], 0);
1050 break;
1051 case nir_op_frsq:
1052 dst[0] = ir3_RSQ(b, src[0], 0);
1053 break;
1054 case nir_op_frcp:
1055 dst[0] = ir3_RCP(b, src[0], 0);
1056 break;
1057 case nir_op_flog2:
1058 dst[0] = ir3_LOG2(b, src[0], 0);
1059 break;
1060 case nir_op_fexp2:
1061 dst[0] = ir3_EXP2(b, src[0], 0);
1062 break;
1063 case nir_op_fsqrt:
1064 dst[0] = ir3_SQRT(b, src[0], 0);
1065 break;
1066
1067 case nir_op_iabs:
1068 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SABS);
1069 break;
1070 case nir_op_iadd:
1071 dst[0] = ir3_ADD_U(b, src[0], 0, src[1], 0);
1072 break;
1073 case nir_op_iand:
1074 dst[0] = ir3_AND_B(b, src[0], 0, src[1], 0);
1075 break;
1076 case nir_op_imax:
1077 dst[0] = ir3_MAX_S(b, src[0], 0, src[1], 0);
1078 break;
1079 case nir_op_umax:
1080 dst[0] = ir3_MAX_U(b, src[0], 0, src[1], 0);
1081 break;
1082 case nir_op_imin:
1083 dst[0] = ir3_MIN_S(b, src[0], 0, src[1], 0);
1084 break;
1085 case nir_op_umin:
1086 dst[0] = ir3_MIN_U(b, src[0], 0, src[1], 0);
1087 break;
1088 case nir_op_imul:
1089 /*
1090 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
1091 * mull.u tmp0, a, b ; mul low, i.e. al * bl
1092 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
1093 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
1094 */
1095 dst[0] = ir3_MADSH_M16(b, src[1], 0, src[0], 0,
1096 ir3_MADSH_M16(b, src[0], 0, src[1], 0,
1097 ir3_MULL_U(b, src[0], 0, src[1], 0), 0), 0);
1098 break;
1099 case nir_op_ineg:
1100 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SNEG);
1101 break;
1102 case nir_op_inot:
1103 dst[0] = ir3_NOT_B(b, src[0], 0);
1104 break;
1105 case nir_op_ior:
1106 dst[0] = ir3_OR_B(b, src[0], 0, src[1], 0);
1107 break;
1108 case nir_op_ishl:
1109 dst[0] = ir3_SHL_B(b, src[0], 0, src[1], 0);
1110 break;
1111 case nir_op_ishr:
1112 dst[0] = ir3_ASHR_B(b, src[0], 0, src[1], 0);
1113 break;
1114 case nir_op_isign: {
1115 /* maybe this would be sane to lower in nir.. */
1116 struct ir3_instruction *neg, *pos;
1117
1118 neg = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1119 neg->cat2.condition = IR3_COND_LT;
1120
1121 pos = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1122 pos->cat2.condition = IR3_COND_GT;
1123
1124 dst[0] = ir3_SUB_U(b, pos, 0, neg, 0);
1125
1126 break;
1127 }
1128 case nir_op_isub:
1129 dst[0] = ir3_SUB_U(b, src[0], 0, src[1], 0);
1130 break;
1131 case nir_op_ixor:
1132 dst[0] = ir3_XOR_B(b, src[0], 0, src[1], 0);
1133 break;
1134 case nir_op_ushr:
1135 dst[0] = ir3_SHR_B(b, src[0], 0, src[1], 0);
1136 break;
1137 case nir_op_ilt:
1138 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1139 dst[0]->cat2.condition = IR3_COND_LT;
1140 dst[0] = ir3_n2b(b, dst[0]);
1141 break;
1142 case nir_op_ige:
1143 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1144 dst[0]->cat2.condition = IR3_COND_GE;
1145 dst[0] = ir3_n2b(b, dst[0]);
1146 break;
1147 case nir_op_ieq:
1148 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1149 dst[0]->cat2.condition = IR3_COND_EQ;
1150 dst[0] = ir3_n2b(b, dst[0]);
1151 break;
1152 case nir_op_ine:
1153 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1154 dst[0]->cat2.condition = IR3_COND_NE;
1155 dst[0] = ir3_n2b(b, dst[0]);
1156 break;
1157 case nir_op_ult:
1158 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1159 dst[0]->cat2.condition = IR3_COND_LT;
1160 dst[0] = ir3_n2b(b, dst[0]);
1161 break;
1162 case nir_op_uge:
1163 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1164 dst[0]->cat2.condition = IR3_COND_GE;
1165 dst[0] = ir3_n2b(b, dst[0]);
1166 break;
1167
1168 case nir_op_bcsel:
1169 dst[0] = ir3_SEL_B32(b, src[1], 0, ir3_b2n(b, src[0]), 0, src[2], 0);
1170 break;
1171
1172 default:
1173 compile_error(ctx, "Unhandled ALU op: %s\n",
1174 nir_op_infos[alu->op].name);
1175 break;
1176 }
1177 }
1178
1179 /* handles direct/indirect UBO reads: */
1180 static void
1181 emit_intrinsic_load_ubo(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1182 struct ir3_instruction **dst)
1183 {
1184 struct ir3_block *b = ctx->block;
1185 struct ir3_instruction *addr, *src0, *src1;
1186 /* UBO addresses are the first driver params: */
1187 unsigned ubo = regid(ctx->so->first_driver_param, 0);
1188 unsigned off = intr->const_index[0];
1189
1190 /* First src is ubo index, which could either be an immed or not: */
1191 src0 = get_src(ctx, &intr->src[0])[0];
1192 if (is_same_type_mov(src0) &&
1193 (src0->regs[1]->flags & IR3_REG_IMMED)) {
1194 addr = create_uniform(ctx, ubo + src0->regs[1]->iim_val);
1195 } else {
1196 addr = create_uniform_indirect(ctx, ubo, get_addr(ctx, src0));
1197 }
1198
1199 if (intr->intrinsic == nir_intrinsic_load_ubo_indirect) {
1200 /* For load_ubo_indirect, second src is indirect offset: */
1201 src1 = get_src(ctx, &intr->src[1])[0];
1202
1203 /* and add offset to addr: */
1204 addr = ir3_ADD_S(b, addr, 0, src1, 0);
1205 }
1206
1207 /* if offset is to large to encode in the ldg, split it out: */
1208 if ((off + (intr->num_components * 4)) > 1024) {
1209 /* split out the minimal amount to improve the odds that
1210 * cp can fit the immediate in the add.s instruction:
1211 */
1212 unsigned off2 = off + (intr->num_components * 4) - 1024;
1213 addr = ir3_ADD_S(b, addr, 0, create_immed(b, off2), 0);
1214 off -= off2;
1215 }
1216
1217 for (int i = 0; i < intr->num_components; i++) {
1218 struct ir3_instruction *load =
1219 ir3_LDG(b, addr, 0, create_immed(b, 1), 0);
1220 load->cat6.type = TYPE_U32;
1221 load->cat6.offset = off + i * 4; /* byte offset */
1222 dst[i] = load;
1223 }
1224 }
1225
1226 /* handles array reads: */
1227 static void
1228 emit_intrinisic_load_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1229 struct ir3_instruction **dst)
1230 {
1231 nir_deref_var *dvar = intr->variables[0];
1232 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1233 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1234
1235 compile_assert(ctx, dvar->deref.child &&
1236 (dvar->deref.child->deref_type == nir_deref_type_array));
1237
1238 switch (darr->deref_array_type) {
1239 case nir_deref_array_type_direct:
1240 /* direct access does not require anything special: */
1241 for (int i = 0; i < intr->num_components; i++) {
1242 unsigned n = darr->base_offset * 4 + i;
1243 compile_assert(ctx, n < arr->length);
1244 dst[i] = arr->arr[n];
1245 }
1246 break;
1247 case nir_deref_array_type_indirect: {
1248 /* for indirect, we need to collect all the array elements: */
1249 struct ir3_instruction *collect =
1250 create_collect(ctx->block, arr->arr, arr->length);
1251 struct ir3_instruction *addr =
1252 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1253 for (int i = 0; i < intr->num_components; i++) {
1254 unsigned n = darr->base_offset * 4 + i;
1255 compile_assert(ctx, n < arr->length);
1256 dst[i] = create_indirect_load(ctx, arr->length, n, addr, collect);
1257 }
1258 break;
1259 }
1260 default:
1261 compile_error(ctx, "Unhandled load deref type: %u\n",
1262 darr->deref_array_type);
1263 break;
1264 }
1265 }
1266
1267 /* handles array writes: */
1268 static void
1269 emit_intrinisic_store_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1270 {
1271 nir_deref_var *dvar = intr->variables[0];
1272 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1273 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1274 struct ir3_instruction **src;
1275
1276 compile_assert(ctx, dvar->deref.child &&
1277 (dvar->deref.child->deref_type == nir_deref_type_array));
1278
1279 src = get_src(ctx, &intr->src[0]);
1280
1281 switch (darr->deref_array_type) {
1282 case nir_deref_array_type_direct:
1283 /* direct access does not require anything special: */
1284 for (int i = 0; i < intr->num_components; i++) {
1285 unsigned n = darr->base_offset * 4 + i;
1286 compile_assert(ctx, n < arr->length);
1287 arr->arr[n] = src[i];
1288 }
1289 break;
1290 case nir_deref_array_type_indirect: {
1291 /* for indirect, create indirect-store and fan that out: */
1292 struct ir3_instruction *collect =
1293 create_collect(ctx->block, arr->arr, arr->length);
1294 struct ir3_instruction *addr =
1295 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1296 for (int i = 0; i < intr->num_components; i++) {
1297 struct ir3_instruction *store;
1298 unsigned n = darr->base_offset * 4 + i;
1299 compile_assert(ctx, n < arr->length);
1300
1301 store = create_indirect_store(ctx, arr->length,
1302 n, src[i], addr, collect);
1303
1304 store->fanin->fi.aid = arr->aid;
1305
1306 /* TODO: probably split this out to be used for
1307 * store_output_indirect? or move this into
1308 * create_indirect_store()?
1309 */
1310 for (int j = i; j < arr->length; j += intr->num_components) {
1311 struct ir3_instruction *split;
1312
1313 split = ir3_instr_create(ctx->block, -1, OPC_META_FO);
1314 split->fo.off = j;
1315 ir3_reg_create(split, 0, 0);
1316 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = store;
1317
1318 arr->arr[j] = split;
1319 }
1320 }
1321 /* fixup fanout/split neighbors: */
1322 for (int i = 0; i < arr->length; i++) {
1323 arr->arr[i]->cp.right = (i < (arr->length - 1)) ?
1324 arr->arr[i+1] : NULL;
1325 arr->arr[i]->cp.left = (i > 0) ?
1326 arr->arr[i-1] : NULL;
1327 }
1328 break;
1329 }
1330 default:
1331 compile_error(ctx, "Unhandled store deref type: %u\n",
1332 darr->deref_array_type);
1333 break;
1334 }
1335 }
1336
1337 static void add_sysval_input(struct ir3_compile *ctx, unsigned name,
1338 struct ir3_instruction *instr)
1339 {
1340 struct ir3_shader_variant *so = ctx->so;
1341 unsigned r = regid(so->inputs_count, 0);
1342 unsigned n = so->inputs_count++;
1343
1344 so->inputs[n].semantic = ir3_semantic_name(name, 0);
1345 so->inputs[n].compmask = 1;
1346 so->inputs[n].regid = r;
1347 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
1348 so->total_in++;
1349
1350 ctx->ir->ninputs = MAX2(ctx->ir->ninputs, r + 1);
1351 ctx->ir->inputs[r] = instr;
1352 }
1353
1354 static void
1355 emit_intrinisic(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1356 {
1357 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1358 struct ir3_instruction **dst, **src;
1359 struct ir3_block *b = ctx->block;
1360 unsigned idx = intr->const_index[0];
1361
1362 if (info->has_dest) {
1363 dst = get_dst(ctx, &intr->dest, intr->num_components);
1364 } else {
1365 dst = NULL;
1366 }
1367
1368 switch (intr->intrinsic) {
1369 case nir_intrinsic_load_uniform:
1370 for (int i = 0; i < intr->num_components; i++) {
1371 unsigned n = idx * 4 + i;
1372 dst[i] = create_uniform(ctx, n);
1373 }
1374 break;
1375 case nir_intrinsic_load_uniform_indirect:
1376 src = get_src(ctx, &intr->src[0]);
1377 for (int i = 0; i < intr->num_components; i++) {
1378 unsigned n = idx * 4 + i;
1379 dst[i] = create_uniform_indirect(ctx, n,
1380 get_addr(ctx, src[0]));
1381 }
1382 /* NOTE: if relative addressing is used, we set constlen in
1383 * the compiler (to worst-case value) since we don't know in
1384 * the assembler what the max addr reg value can be:
1385 */
1386 ctx->so->constlen = ctx->s->num_uniforms;
1387 break;
1388 case nir_intrinsic_load_ubo:
1389 case nir_intrinsic_load_ubo_indirect:
1390 emit_intrinsic_load_ubo(ctx, intr, dst);
1391 break;
1392 case nir_intrinsic_load_input:
1393 for (int i = 0; i < intr->num_components; i++) {
1394 unsigned n = idx * 4 + i;
1395 dst[i] = ctx->ir->inputs[n];
1396 }
1397 break;
1398 case nir_intrinsic_load_input_indirect:
1399 src = get_src(ctx, &intr->src[0]);
1400 struct ir3_instruction *collect =
1401 create_collect(b, ctx->ir->inputs, ctx->ir->ninputs);
1402 struct ir3_instruction *addr = get_addr(ctx, src[0]);
1403 for (int i = 0; i < intr->num_components; i++) {
1404 unsigned n = idx * 4 + i;
1405 dst[i] = create_indirect_load(ctx, ctx->ir->ninputs,
1406 n, addr, collect);
1407 }
1408 break;
1409 case nir_intrinsic_load_var:
1410 emit_intrinisic_load_var(ctx, intr, dst);
1411 break;
1412 case nir_intrinsic_store_var:
1413 emit_intrinisic_store_var(ctx, intr);
1414 break;
1415 case nir_intrinsic_store_output:
1416 src = get_src(ctx, &intr->src[0]);
1417 for (int i = 0; i < intr->num_components; i++) {
1418 unsigned n = idx * 4 + i;
1419 ctx->ir->outputs[n] = src[i];
1420 }
1421 break;
1422 case nir_intrinsic_load_base_vertex:
1423 if (!ctx->basevertex) {
1424 /* first four vec4 sysval's reserved for UBOs: */
1425 unsigned r = regid(ctx->so->first_driver_param + 4, 0);
1426 ctx->basevertex = create_uniform(ctx, r);
1427 add_sysval_input(ctx, TGSI_SEMANTIC_BASEVERTEX,
1428 ctx->basevertex);
1429 }
1430 dst[0] = ctx->basevertex;
1431 break;
1432 case nir_intrinsic_load_vertex_id_zero_base:
1433 if (!ctx->vertex_id) {
1434 ctx->vertex_id = create_input(ctx->block, NULL, 0);
1435 add_sysval_input(ctx, TGSI_SEMANTIC_VERTEXID_NOBASE,
1436 ctx->vertex_id);
1437 }
1438 dst[0] = ctx->vertex_id;
1439 break;
1440 case nir_intrinsic_load_instance_id:
1441 if (!ctx->instance_id) {
1442 ctx->instance_id = create_input(ctx->block, NULL, 0);
1443 add_sysval_input(ctx, TGSI_SEMANTIC_INSTANCEID,
1444 ctx->instance_id);
1445 }
1446 dst[0] = ctx->instance_id;
1447 break;
1448 case nir_intrinsic_discard_if:
1449 case nir_intrinsic_discard: {
1450 struct ir3_instruction *cond, *kill;
1451
1452 if (intr->intrinsic == nir_intrinsic_discard_if) {
1453 /* conditional discard: */
1454 src = get_src(ctx, &intr->src[0]);
1455 cond = ir3_b2n(b, src[0]);
1456 } else {
1457 /* unconditional discard: */
1458 cond = create_immed(b, 1);
1459 }
1460
1461 /* NOTE: only cmps.*.* can write p0.x: */
1462 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1463 cond->cat2.condition = IR3_COND_NE;
1464
1465 /* condition always goes in predicate register: */
1466 cond->regs[0]->num = regid(REG_P0, 0);
1467
1468 kill = ir3_KILL(b, cond, 0);
1469 array_insert(ctx->ir->predicates, kill);
1470
1471 ctx->kill[ctx->kill_count++] = kill;
1472 ctx->so->has_kill = true;
1473
1474 break;
1475 }
1476 default:
1477 compile_error(ctx, "Unhandled intrinsic type: %s\n",
1478 nir_intrinsic_infos[intr->intrinsic].name);
1479 break;
1480 }
1481 }
1482
1483 static void
1484 emit_load_const(struct ir3_compile *ctx, nir_load_const_instr *instr)
1485 {
1486 struct ir3_instruction **dst = get_dst_ssa(ctx, &instr->def,
1487 instr->def.num_components);
1488 for (int i = 0; i < instr->def.num_components; i++)
1489 dst[i] = create_immed(ctx->block, instr->value.u[i]);
1490 }
1491
1492 static void
1493 emit_undef(struct ir3_compile *ctx, nir_ssa_undef_instr *undef)
1494 {
1495 struct ir3_instruction **dst = get_dst_ssa(ctx, &undef->def,
1496 undef->def.num_components);
1497 /* backend doesn't want undefined instructions, so just plug
1498 * in 0.0..
1499 */
1500 for (int i = 0; i < undef->def.num_components; i++)
1501 dst[i] = create_immed(ctx->block, fui(0.0));
1502 }
1503
1504 /*
1505 * texture fetch/sample instructions:
1506 */
1507
1508 static void
1509 tex_info(nir_tex_instr *tex, unsigned *flagsp, unsigned *coordsp)
1510 {
1511 unsigned coords, flags = 0;
1512
1513 /* note: would use tex->coord_components.. except txs.. also,
1514 * since array index goes after shadow ref, we don't want to
1515 * count it:
1516 */
1517 switch (tex->sampler_dim) {
1518 case GLSL_SAMPLER_DIM_1D:
1519 case GLSL_SAMPLER_DIM_BUF:
1520 coords = 1;
1521 break;
1522 case GLSL_SAMPLER_DIM_2D:
1523 case GLSL_SAMPLER_DIM_RECT:
1524 case GLSL_SAMPLER_DIM_EXTERNAL:
1525 case GLSL_SAMPLER_DIM_MS:
1526 coords = 2;
1527 break;
1528 case GLSL_SAMPLER_DIM_3D:
1529 case GLSL_SAMPLER_DIM_CUBE:
1530 coords = 3;
1531 flags |= IR3_INSTR_3D;
1532 break;
1533 default:
1534 unreachable("bad sampler_dim");
1535 }
1536
1537 if (tex->is_shadow)
1538 flags |= IR3_INSTR_S;
1539
1540 if (tex->is_array)
1541 flags |= IR3_INSTR_A;
1542
1543 *flagsp = flags;
1544 *coordsp = coords;
1545 }
1546
1547 static void
1548 emit_tex(struct ir3_compile *ctx, nir_tex_instr *tex)
1549 {
1550 struct ir3_block *b = ctx->block;
1551 struct ir3_instruction **dst, *sam, *src0[12], *src1[4];
1552 struct ir3_instruction **coord, *lod, *compare, *proj, **off, **ddx, **ddy;
1553 bool has_bias = false, has_lod = false, has_proj = false, has_off = false;
1554 unsigned i, coords, flags;
1555 unsigned nsrc0 = 0, nsrc1 = 0;
1556 type_t type;
1557 opc_t opc = 0;
1558
1559 coord = off = ddx = ddy = NULL;
1560 lod = proj = compare = NULL;
1561
1562 /* TODO: might just be one component for gathers? */
1563 dst = get_dst(ctx, &tex->dest, 4);
1564
1565 for (unsigned i = 0; i < tex->num_srcs; i++) {
1566 switch (tex->src[i].src_type) {
1567 case nir_tex_src_coord:
1568 coord = get_src(ctx, &tex->src[i].src);
1569 break;
1570 case nir_tex_src_bias:
1571 lod = get_src(ctx, &tex->src[i].src)[0];
1572 has_bias = true;
1573 break;
1574 case nir_tex_src_lod:
1575 lod = get_src(ctx, &tex->src[i].src)[0];
1576 has_lod = true;
1577 break;
1578 case nir_tex_src_comparitor: /* shadow comparator */
1579 compare = get_src(ctx, &tex->src[i].src)[0];
1580 break;
1581 case nir_tex_src_projector:
1582 proj = get_src(ctx, &tex->src[i].src)[0];
1583 has_proj = true;
1584 break;
1585 case nir_tex_src_offset:
1586 off = get_src(ctx, &tex->src[i].src);
1587 has_off = true;
1588 break;
1589 case nir_tex_src_ddx:
1590 ddx = get_src(ctx, &tex->src[i].src);
1591 break;
1592 case nir_tex_src_ddy:
1593 ddy = get_src(ctx, &tex->src[i].src);
1594 break;
1595 default:
1596 compile_error(ctx, "Unhandled NIR tex serc type: %d\n",
1597 tex->src[i].src_type);
1598 return;
1599 }
1600 }
1601
1602 switch (tex->op) {
1603 case nir_texop_tex: opc = OPC_SAM; break;
1604 case nir_texop_txb: opc = OPC_SAMB; break;
1605 case nir_texop_txl: opc = OPC_SAML; break;
1606 case nir_texop_txd: opc = OPC_SAMGQ; break;
1607 case nir_texop_txf: opc = OPC_ISAML; break;
1608 case nir_texop_txf_ms:
1609 case nir_texop_txs:
1610 case nir_texop_lod:
1611 case nir_texop_tg4:
1612 case nir_texop_query_levels:
1613 compile_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
1614 return;
1615 }
1616
1617 tex_info(tex, &flags, &coords);
1618
1619 /* scale up integer coords for TXF based on the LOD */
1620 if (ctx->unminify_coords && (opc == OPC_ISAML)) {
1621 assert(has_lod);
1622 for (i = 0; i < coords; i++)
1623 coord[i] = ir3_SHL_B(b, coord[i], 0, lod, 0);
1624 }
1625
1626 /*
1627 * lay out the first argument in the proper order:
1628 * - actual coordinates first
1629 * - shadow reference
1630 * - array index
1631 * - projection w
1632 * - starting at offset 4, dpdx.xy, dpdy.xy
1633 *
1634 * bias/lod go into the second arg
1635 */
1636
1637 /* insert tex coords: */
1638 for (i = 0; i < coords; i++)
1639 src0[nsrc0++] = coord[i];
1640
1641 if (coords == 1) {
1642 /* hw doesn't do 1d, so we treat it as 2d with
1643 * height of 1, and patch up the y coord.
1644 * TODO: y coord should be (int)0 in some cases..
1645 */
1646 src0[nsrc0++] = create_immed(b, fui(0.5));
1647 }
1648
1649 if (tex->is_shadow)
1650 src0[nsrc0++] = compare;
1651
1652 if (tex->is_array)
1653 src0[nsrc0++] = coord[coords];
1654
1655 if (has_proj) {
1656 src0[nsrc0++] = proj;
1657 flags |= IR3_INSTR_P;
1658 }
1659
1660 /* pad to 4, then ddx/ddy: */
1661 if (tex->op == nir_texop_txd) {
1662 while (nsrc0 < 4)
1663 src0[nsrc0++] = create_immed(b, fui(0.0));
1664 for (i = 0; i < coords; i++)
1665 src0[nsrc0++] = ddx[i];
1666 if (coords < 2)
1667 src0[nsrc0++] = create_immed(b, fui(0.0));
1668 for (i = 0; i < coords; i++)
1669 src0[nsrc0++] = ddy[i];
1670 if (coords < 2)
1671 src0[nsrc0++] = create_immed(b, fui(0.0));
1672 }
1673
1674 /*
1675 * second argument (if applicable):
1676 * - offsets
1677 * - lod
1678 * - bias
1679 */
1680 if (has_off | has_lod | has_bias) {
1681 if (has_off) {
1682 for (i = 0; i < coords; i++)
1683 src1[nsrc1++] = off[i];
1684 if (coords < 2)
1685 src1[nsrc1++] = create_immed(b, fui(0.0));
1686 flags |= IR3_INSTR_O;
1687 }
1688
1689 if (has_lod | has_bias)
1690 src1[nsrc1++] = lod;
1691 }
1692
1693 switch (tex->dest_type) {
1694 case nir_type_invalid:
1695 case nir_type_float:
1696 type = TYPE_F32;
1697 break;
1698 case nir_type_int:
1699 type = TYPE_S32;
1700 break;
1701 case nir_type_unsigned:
1702 case nir_type_bool:
1703 type = TYPE_U32;
1704 break;
1705 default:
1706 unreachable("bad dest_type");
1707 }
1708
1709 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_XYZW,
1710 flags, tex->sampler_index, tex->sampler_index,
1711 create_collect(b, src0, nsrc0),
1712 create_collect(b, src1, nsrc1));
1713
1714 split_dest(b, dst, sam, 4);
1715 }
1716
1717 static void
1718 emit_tex_query_levels(struct ir3_compile *ctx, nir_tex_instr *tex)
1719 {
1720 struct ir3_block *b = ctx->block;
1721 struct ir3_instruction **dst, *sam;
1722
1723 dst = get_dst(ctx, &tex->dest, 1);
1724
1725 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, TGSI_WRITEMASK_Z, 0,
1726 tex->sampler_index, tex->sampler_index, NULL, NULL);
1727
1728 /* even though there is only one component, since it ends
1729 * up in .z rather than .x, we need a split_dest()
1730 */
1731 split_dest(b, dst, sam, 3);
1732
1733 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1734 * the value in TEX_CONST_0 is zero-based.
1735 */
1736 if (ctx->levels_add_one)
1737 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
1738 }
1739
1740 static void
1741 emit_tex_txs(struct ir3_compile *ctx, nir_tex_instr *tex)
1742 {
1743 struct ir3_block *b = ctx->block;
1744 struct ir3_instruction **dst, *sam, *lod;
1745 unsigned flags, coords;
1746
1747 tex_info(tex, &flags, &coords);
1748
1749 dst = get_dst(ctx, &tex->dest, 4);
1750
1751 compile_assert(ctx, tex->num_srcs == 1);
1752 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
1753
1754 lod = get_src(ctx, &tex->src[0].src)[0];
1755
1756 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, TGSI_WRITEMASK_XYZW, flags,
1757 tex->sampler_index, tex->sampler_index, lod, NULL);
1758
1759 split_dest(b, dst, sam, 4);
1760
1761 /* Array size actually ends up in .w rather than .z. This doesn't
1762 * matter for miplevel 0, but for higher mips the value in z is
1763 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1764 * returned, which means that we have to add 1 to it for arrays.
1765 */
1766 if (tex->is_array) {
1767 if (ctx->levels_add_one) {
1768 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
1769 } else {
1770 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
1771 }
1772 }
1773 }
1774
1775 static void
1776 emit_phi(struct ir3_compile *ctx, nir_phi_instr *nphi)
1777 {
1778 struct ir3_instruction *phi, **dst;
1779
1780 /* NOTE: phi's should be lowered to scalar at this point */
1781 compile_assert(ctx, nphi->dest.ssa.num_components == 1);
1782
1783 dst = get_dst(ctx, &nphi->dest, 1);
1784
1785 phi = ir3_instr_create2(ctx->block, -1, OPC_META_PHI,
1786 1 + exec_list_length(&nphi->srcs));
1787 ir3_reg_create(phi, 0, 0); /* dst */
1788 phi->phi.nphi = nphi;
1789
1790 dst[0] = phi;
1791 }
1792
1793 /* phi instructions are left partially constructed. We don't resolve
1794 * their srcs until the end of the block, since (eg. loops) one of
1795 * the phi's srcs might be defined after the phi due to back edges in
1796 * the CFG.
1797 */
1798 static void
1799 resolve_phis(struct ir3_compile *ctx, struct ir3_block *block)
1800 {
1801 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1802 nir_phi_instr *nphi;
1803
1804 /* phi's only come at start of block: */
1805 if (!(is_meta(instr) && (instr->opc == OPC_META_PHI)))
1806 break;
1807
1808 if (!instr->phi.nphi)
1809 break;
1810
1811 nphi = instr->phi.nphi;
1812 instr->phi.nphi = NULL;
1813
1814 foreach_list_typed(nir_phi_src, nsrc, node, &nphi->srcs) {
1815 struct ir3_instruction *src = get_src(ctx, &nsrc->src)[0];
1816 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
1817 }
1818 }
1819
1820 resolve_array_phis(ctx, block);
1821 }
1822
1823 static void
1824 emit_jump(struct ir3_compile *ctx, nir_jump_instr *jump)
1825 {
1826 switch (jump->type) {
1827 case nir_jump_break:
1828 case nir_jump_continue:
1829 /* I *think* we can simply just ignore this, and use the
1830 * successor block link to figure out where we need to
1831 * jump to for break/continue
1832 */
1833 break;
1834 default:
1835 compile_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
1836 break;
1837 }
1838 }
1839
1840 static void
1841 emit_instr(struct ir3_compile *ctx, nir_instr *instr)
1842 {
1843 switch (instr->type) {
1844 case nir_instr_type_alu:
1845 emit_alu(ctx, nir_instr_as_alu(instr));
1846 break;
1847 case nir_instr_type_intrinsic:
1848 emit_intrinisic(ctx, nir_instr_as_intrinsic(instr));
1849 break;
1850 case nir_instr_type_load_const:
1851 emit_load_const(ctx, nir_instr_as_load_const(instr));
1852 break;
1853 case nir_instr_type_ssa_undef:
1854 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1855 break;
1856 case nir_instr_type_tex: {
1857 nir_tex_instr *tex = nir_instr_as_tex(instr);
1858 /* couple tex instructions get special-cased:
1859 */
1860 switch (tex->op) {
1861 case nir_texop_txs:
1862 emit_tex_txs(ctx, tex);
1863 break;
1864 case nir_texop_query_levels:
1865 emit_tex_query_levels(ctx, tex);
1866 break;
1867 default:
1868 emit_tex(ctx, tex);
1869 break;
1870 }
1871 break;
1872 }
1873 case nir_instr_type_phi:
1874 emit_phi(ctx, nir_instr_as_phi(instr));
1875 break;
1876 case nir_instr_type_jump:
1877 emit_jump(ctx, nir_instr_as_jump(instr));
1878 break;
1879 case nir_instr_type_call:
1880 case nir_instr_type_parallel_copy:
1881 compile_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
1882 break;
1883 }
1884 }
1885
1886 static struct ir3_block *
1887 get_block(struct ir3_compile *ctx, nir_block *nblock)
1888 {
1889 struct ir3_block *block;
1890 struct hash_entry *entry;
1891 entry = _mesa_hash_table_search(ctx->block_ht, nblock);
1892 if (entry)
1893 return entry->data;
1894
1895 block = ir3_block_create(ctx->ir);
1896 block->nblock = nblock;
1897 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
1898
1899 return block;
1900 }
1901
1902 static void
1903 emit_block(struct ir3_compile *ctx, nir_block *nblock)
1904 {
1905 struct ir3_block *block = get_block(ctx, nblock);
1906
1907 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
1908 if (nblock->successors[i]) {
1909 block->successors[i] =
1910 get_block(ctx, nblock->successors[i]);
1911 }
1912 }
1913
1914 ctx->block = block;
1915 list_addtail(&block->node, &ctx->ir->block_list);
1916
1917 nir_foreach_instr(nblock, instr) {
1918 emit_instr(ctx, instr);
1919 if (ctx->error)
1920 return;
1921 }
1922 }
1923
1924 static void emit_cf_list(struct ir3_compile *ctx, struct exec_list *list);
1925
1926 static void
1927 emit_if(struct ir3_compile *ctx, nir_if *nif)
1928 {
1929 struct ir3_instruction *condition = get_src(ctx, &nif->condition)[0];
1930
1931 ctx->block->condition =
1932 get_predicate(ctx, ir3_b2n(condition->block, condition));
1933
1934 emit_cf_list(ctx, &nif->then_list);
1935 emit_cf_list(ctx, &nif->else_list);
1936 }
1937
1938 static void
1939 emit_loop(struct ir3_compile *ctx, nir_loop *nloop)
1940 {
1941 emit_cf_list(ctx, &nloop->body);
1942 }
1943
1944 static void
1945 emit_cf_list(struct ir3_compile *ctx, struct exec_list *list)
1946 {
1947 foreach_list_typed(nir_cf_node, node, node, list) {
1948 switch (node->type) {
1949 case nir_cf_node_block:
1950 emit_block(ctx, nir_cf_node_as_block(node));
1951 break;
1952 case nir_cf_node_if:
1953 emit_if(ctx, nir_cf_node_as_if(node));
1954 break;
1955 case nir_cf_node_loop:
1956 emit_loop(ctx, nir_cf_node_as_loop(node));
1957 break;
1958 case nir_cf_node_function:
1959 compile_error(ctx, "TODO\n");
1960 break;
1961 }
1962 }
1963 }
1964
1965 static void
1966 emit_function(struct ir3_compile *ctx, nir_function_impl *impl)
1967 {
1968 emit_cf_list(ctx, &impl->body);
1969 emit_block(ctx, impl->end_block);
1970
1971 /* at this point, we should have a single empty block,
1972 * into which we emit the 'end' instruction.
1973 */
1974 compile_assert(ctx, list_empty(&ctx->block->instr_list));
1975 ir3_END(ctx->block);
1976 }
1977
1978 static void
1979 setup_input(struct ir3_compile *ctx, nir_variable *in)
1980 {
1981 struct ir3_shader_variant *so = ctx->so;
1982 unsigned array_len = MAX2(glsl_get_length(in->type), 1);
1983 unsigned ncomp = glsl_get_components(in->type);
1984 /* XXX: map loc slots to semantics */
1985 unsigned semantic_name = in->data.location;
1986 unsigned semantic_index = in->data.index;
1987 unsigned n = in->data.driver_location;
1988
1989 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1990 semantic_name, semantic_index, array_len,
1991 ncomp, n);
1992
1993 so->inputs[n].semantic =
1994 ir3_semantic_name(semantic_name, semantic_index);
1995 so->inputs[n].compmask = (1 << ncomp) - 1;
1996 so->inputs[n].inloc = ctx->next_inloc;
1997 so->inputs[n].interpolate = 0;
1998 so->inputs_count = MAX2(so->inputs_count, n + 1);
1999
2000 /* the fdN_program_emit() code expects tgsi consts here, so map
2001 * things back to tgsi for now:
2002 */
2003 switch (in->data.interpolation) {
2004 case INTERP_QUALIFIER_FLAT:
2005 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
2006 break;
2007 case INTERP_QUALIFIER_NOPERSPECTIVE:
2008 so->inputs[n].interpolate = TGSI_INTERPOLATE_LINEAR;
2009 break;
2010 case INTERP_QUALIFIER_SMOOTH:
2011 so->inputs[n].interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
2012 break;
2013 }
2014
2015 for (int i = 0; i < ncomp; i++) {
2016 struct ir3_instruction *instr = NULL;
2017 unsigned idx = (n * 4) + i;
2018
2019 if (ctx->so->type == SHADER_FRAGMENT) {
2020 if (semantic_name == TGSI_SEMANTIC_POSITION) {
2021 so->inputs[n].bary = false;
2022 so->frag_coord = true;
2023 instr = create_frag_coord(ctx, i);
2024 } else if (semantic_name == TGSI_SEMANTIC_FACE) {
2025 so->inputs[n].bary = false;
2026 so->frag_face = true;
2027 instr = create_frag_face(ctx, i);
2028 } else {
2029 bool use_ldlv = false;
2030
2031 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2032 * from the semantic name:
2033 */
2034 if ((in->data.interpolation == INTERP_QUALIFIER_NONE) &&
2035 ((semantic_name == TGSI_SEMANTIC_COLOR) ||
2036 (semantic_name == TGSI_SEMANTIC_BCOLOR)))
2037 so->inputs[n].interpolate = TGSI_INTERPOLATE_COLOR;
2038
2039 if (ctx->flat_bypass) {
2040 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2041 * from the semantic name:
2042 */
2043 switch (so->inputs[n].interpolate) {
2044 case TGSI_INTERPOLATE_COLOR:
2045 if (!ctx->so->key.rasterflat)
2046 break;
2047 /* fallthrough */
2048 case TGSI_INTERPOLATE_CONSTANT:
2049 use_ldlv = true;
2050 break;
2051 }
2052 }
2053
2054 so->inputs[n].bary = true;
2055
2056 instr = create_frag_input(ctx,
2057 so->inputs[n].inloc + i - 8, use_ldlv);
2058 }
2059 } else {
2060 instr = create_input(ctx->block, NULL, idx);
2061 }
2062
2063 ctx->ir->inputs[idx] = instr;
2064 }
2065
2066 if (so->inputs[n].bary || (ctx->so->type == SHADER_VERTEX)) {
2067 ctx->next_inloc += ncomp;
2068 so->total_in += ncomp;
2069 }
2070 }
2071
2072 static void
2073 setup_output(struct ir3_compile *ctx, nir_variable *out)
2074 {
2075 struct ir3_shader_variant *so = ctx->so;
2076 unsigned array_len = MAX2(glsl_get_length(out->type), 1);
2077 unsigned ncomp = glsl_get_components(out->type);
2078 /* XXX: map loc slots to semantics */
2079 unsigned semantic_name = out->data.location;
2080 unsigned semantic_index = out->data.index;
2081 unsigned n = out->data.driver_location;
2082 unsigned comp = 0;
2083
2084 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
2085 semantic_name, semantic_index, array_len,
2086 ncomp, n);
2087
2088 if (ctx->so->type == SHADER_VERTEX) {
2089 switch (semantic_name) {
2090 case TGSI_SEMANTIC_POSITION:
2091 so->writes_pos = true;
2092 break;
2093 case TGSI_SEMANTIC_PSIZE:
2094 so->writes_psize = true;
2095 break;
2096 case TGSI_SEMANTIC_COLOR:
2097 case TGSI_SEMANTIC_BCOLOR:
2098 case TGSI_SEMANTIC_GENERIC:
2099 case TGSI_SEMANTIC_FOG:
2100 case TGSI_SEMANTIC_TEXCOORD:
2101 break;
2102 default:
2103 compile_error(ctx, "unknown VS semantic name: %s\n",
2104 tgsi_semantic_names[semantic_name]);
2105 }
2106 } else {
2107 switch (semantic_name) {
2108 case TGSI_SEMANTIC_POSITION:
2109 comp = 2; /* tgsi will write to .z component */
2110 so->writes_pos = true;
2111 break;
2112 case TGSI_SEMANTIC_COLOR:
2113 if (semantic_index == -1) {
2114 semantic_index = 0;
2115 so->color0_mrt = 1;
2116 }
2117 break;
2118 default:
2119 compile_error(ctx, "unknown FS semantic name: %s\n",
2120 tgsi_semantic_names[semantic_name]);
2121 }
2122 }
2123
2124 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2125
2126 so->outputs[n].semantic =
2127 ir3_semantic_name(semantic_name, semantic_index);
2128 so->outputs[n].regid = regid(n, comp);
2129 so->outputs_count = MAX2(so->outputs_count, n + 1);
2130
2131 for (int i = 0; i < ncomp; i++) {
2132 unsigned idx = (n * 4) + i;
2133
2134 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2135 }
2136 }
2137
2138 static void
2139 emit_instructions(struct ir3_compile *ctx)
2140 {
2141 unsigned ninputs, noutputs;
2142 nir_function_impl *fxn = NULL;
2143
2144 /* Find the main function: */
2145 nir_foreach_overload(ctx->s, overload) {
2146 compile_assert(ctx, strcmp(overload->function->name, "main") == 0);
2147 compile_assert(ctx, overload->impl);
2148 fxn = overload->impl;
2149 break;
2150 }
2151
2152 ninputs = exec_list_length(&ctx->s->inputs) * 4;
2153 noutputs = exec_list_length(&ctx->s->outputs) * 4;
2154
2155 /* we need to allocate big enough outputs array so that
2156 * we can stuff the kill's at the end. Likewise for vtx
2157 * shaders, we need to leave room for sysvals:
2158 */
2159 if (ctx->so->type == SHADER_FRAGMENT) {
2160 noutputs += ARRAY_SIZE(ctx->kill);
2161 } else if (ctx->so->type == SHADER_VERTEX) {
2162 ninputs += 8;
2163 }
2164
2165 ctx->ir = ir3_create(ctx->compiler, ninputs, noutputs);
2166
2167 /* Create inputs in first block: */
2168 ctx->block = get_block(ctx, fxn->start_block);
2169 ctx->in_block = ctx->block;
2170 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2171
2172 if (ctx->so->type == SHADER_FRAGMENT) {
2173 ctx->ir->noutputs -= ARRAY_SIZE(ctx->kill);
2174 } else if (ctx->so->type == SHADER_VERTEX) {
2175 ctx->ir->ninputs -= 8;
2176 }
2177
2178 /* for fragment shader, we have a single input register (usually
2179 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2180 */
2181 if (ctx->so->type == SHADER_FRAGMENT) {
2182 // TODO maybe a helper for fi since we need it a few places..
2183 struct ir3_instruction *instr;
2184 instr = ir3_instr_create(ctx->block, -1, OPC_META_FI);
2185 ir3_reg_create(instr, 0, 0);
2186 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.x */
2187 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.y */
2188 ctx->frag_pos = instr;
2189 }
2190
2191 /* Setup inputs: */
2192 foreach_list_typed(nir_variable, var, node, &ctx->s->inputs) {
2193 setup_input(ctx, var);
2194 }
2195
2196 /* Setup outputs: */
2197 foreach_list_typed(nir_variable, var, node, &ctx->s->outputs) {
2198 setup_output(ctx, var);
2199 }
2200
2201 /* Setup variables (which should only be arrays): */
2202 foreach_list_typed(nir_variable, var, node, &ctx->s->globals) {
2203 declare_var(ctx, var);
2204 }
2205
2206 /* And emit the body: */
2207 ctx->impl = fxn;
2208 emit_function(ctx, fxn);
2209
2210 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2211 resolve_phis(ctx, block);
2212 }
2213 }
2214
2215 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2216 * for a fragment shader are just bary.f instructions. The *actual* inputs
2217 * from the hw perspective are the frag_pos and optionally frag_coord and
2218 * frag_face.
2219 */
2220 static void
2221 fixup_frag_inputs(struct ir3_compile *ctx)
2222 {
2223 struct ir3_shader_variant *so = ctx->so;
2224 struct ir3 *ir = ctx->ir;
2225 struct ir3_instruction **inputs;
2226 struct ir3_instruction *instr;
2227 int n, regid = 0;
2228
2229 ir->ninputs = 0;
2230
2231 n = 4; /* always have frag_pos */
2232 n += COND(so->frag_face, 4);
2233 n += COND(so->frag_coord, 4);
2234
2235 inputs = ir3_alloc(ctx->ir, n * (sizeof(struct ir3_instruction *)));
2236
2237 if (so->frag_face) {
2238 /* this ultimately gets assigned to hr0.x so doesn't conflict
2239 * with frag_coord/frag_pos..
2240 */
2241 inputs[ir->ninputs++] = ctx->frag_face;
2242 ctx->frag_face->regs[0]->num = 0;
2243
2244 /* remaining channels not used, but let's avoid confusing
2245 * other parts that expect inputs to come in groups of vec4
2246 */
2247 inputs[ir->ninputs++] = NULL;
2248 inputs[ir->ninputs++] = NULL;
2249 inputs[ir->ninputs++] = NULL;
2250 }
2251
2252 /* since we don't know where to set the regid for frag_coord,
2253 * we have to use r0.x for it. But we don't want to *always*
2254 * use r1.x for frag_pos as that could increase the register
2255 * footprint on simple shaders:
2256 */
2257 if (so->frag_coord) {
2258 ctx->frag_coord[0]->regs[0]->num = regid++;
2259 ctx->frag_coord[1]->regs[0]->num = regid++;
2260 ctx->frag_coord[2]->regs[0]->num = regid++;
2261 ctx->frag_coord[3]->regs[0]->num = regid++;
2262
2263 inputs[ir->ninputs++] = ctx->frag_coord[0];
2264 inputs[ir->ninputs++] = ctx->frag_coord[1];
2265 inputs[ir->ninputs++] = ctx->frag_coord[2];
2266 inputs[ir->ninputs++] = ctx->frag_coord[3];
2267 }
2268
2269 /* we always have frag_pos: */
2270 so->pos_regid = regid;
2271
2272 /* r0.x */
2273 instr = create_input(ctx->in_block, NULL, ir->ninputs);
2274 instr->regs[0]->num = regid++;
2275 inputs[ir->ninputs++] = instr;
2276 ctx->frag_pos->regs[1]->instr = instr;
2277
2278 /* r0.y */
2279 instr = create_input(ctx->in_block, NULL, ir->ninputs);
2280 instr->regs[0]->num = regid++;
2281 inputs[ir->ninputs++] = instr;
2282 ctx->frag_pos->regs[2]->instr = instr;
2283
2284 ir->inputs = inputs;
2285 }
2286
2287 int
2288 ir3_compile_shader_nir(struct ir3_compiler *compiler,
2289 struct ir3_shader_variant *so,
2290 const struct tgsi_token *tokens,
2291 struct ir3_shader_key key)
2292 {
2293 struct ir3_compile *ctx;
2294 struct ir3 *ir;
2295 struct ir3_instruction **inputs;
2296 unsigned i, j, actual_in;
2297 int ret = 0, max_bary;
2298
2299 assert(!so->ir);
2300
2301 ctx = compile_init(compiler, so, tokens);
2302 if (!ctx) {
2303 DBG("INIT failed!");
2304 ret = -1;
2305 goto out;
2306 }
2307
2308 emit_instructions(ctx);
2309
2310 if (ctx->error) {
2311 DBG("EMIT failed!");
2312 ret = -1;
2313 goto out;
2314 }
2315
2316 ir = so->ir = ctx->ir;
2317
2318 /* keep track of the inputs from TGSI perspective.. */
2319 inputs = ir->inputs;
2320
2321 /* but fixup actual inputs for frag shader: */
2322 if (so->type == SHADER_FRAGMENT)
2323 fixup_frag_inputs(ctx);
2324
2325 /* at this point, for binning pass, throw away unneeded outputs: */
2326 if (key.binning_pass) {
2327 for (i = 0, j = 0; i < so->outputs_count; i++) {
2328 unsigned name = sem2name(so->outputs[i].semantic);
2329 unsigned idx = sem2idx(so->outputs[i].semantic);
2330
2331 /* throw away everything but first position/psize */
2332 if ((idx == 0) && ((name == TGSI_SEMANTIC_POSITION) ||
2333 (name == TGSI_SEMANTIC_PSIZE))) {
2334 if (i != j) {
2335 so->outputs[j] = so->outputs[i];
2336 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
2337 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
2338 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
2339 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
2340 }
2341 j++;
2342 }
2343 }
2344 so->outputs_count = j;
2345 ir->noutputs = j * 4;
2346 }
2347
2348 /* if we want half-precision outputs, mark the output registers
2349 * as half:
2350 */
2351 if (key.half_precision) {
2352 for (i = 0; i < ir->noutputs; i++) {
2353 struct ir3_instruction *out = ir->outputs[i];
2354 if (!out)
2355 continue;
2356 out->regs[0]->flags |= IR3_REG_HALF;
2357 /* output could be a fanout (ie. texture fetch output)
2358 * in which case we need to propagate the half-reg flag
2359 * up to the definer so that RA sees it:
2360 */
2361 if (is_meta(out) && (out->opc == OPC_META_FO)) {
2362 out = out->regs[1]->instr;
2363 out->regs[0]->flags |= IR3_REG_HALF;
2364 }
2365
2366 if (out->category == 1) {
2367 out->cat1.dst_type = half_type(out->cat1.dst_type);
2368 }
2369 }
2370 }
2371
2372 /* at this point, we want the kill's in the outputs array too,
2373 * so that they get scheduled (since they have no dst).. we've
2374 * already ensured that the array is big enough in push_block():
2375 */
2376 if (so->type == SHADER_FRAGMENT) {
2377 for (i = 0; i < ctx->kill_count; i++)
2378 ir->outputs[ir->noutputs++] = ctx->kill[i];
2379 }
2380
2381 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2382 printf("BEFORE CP:\n");
2383 ir3_print(ir);
2384 }
2385
2386 ir3_cp(ir);
2387
2388 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2389 printf("BEFORE GROUPING:\n");
2390 ir3_print(ir);
2391 }
2392
2393 /* Group left/right neighbors, inserting mov's where needed to
2394 * solve conflicts:
2395 */
2396 ir3_group(ir);
2397
2398 ir3_depth(ir);
2399
2400 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2401 printf("AFTER DEPTH:\n");
2402 ir3_print(ir);
2403 }
2404
2405 ret = ir3_sched(ir);
2406 if (ret) {
2407 DBG("SCHED failed!");
2408 goto out;
2409 }
2410
2411 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2412 printf("AFTER SCHED:\n");
2413 ir3_print(ir);
2414 }
2415
2416 ret = ir3_ra(ir, so->type, so->frag_coord, so->frag_face);
2417 if (ret) {
2418 DBG("RA failed!");
2419 goto out;
2420 }
2421
2422 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2423 printf("AFTER RA:\n");
2424 ir3_print(ir);
2425 }
2426
2427 ir3_legalize(ir, &so->has_samp, &max_bary);
2428
2429 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2430 printf("AFTER LEGALIZE:\n");
2431 ir3_print(ir);
2432 }
2433
2434 /* fixup input/outputs: */
2435 for (i = 0; i < so->outputs_count; i++) {
2436 so->outputs[i].regid = ir->outputs[i*4]->regs[0]->num;
2437 /* preserve hack for depth output.. tgsi writes depth to .z,
2438 * but what we give the hw is the scalar register:
2439 */
2440 if ((so->type == SHADER_FRAGMENT) &&
2441 (sem2name(so->outputs[i].semantic) == TGSI_SEMANTIC_POSITION))
2442 so->outputs[i].regid += 2;
2443 }
2444
2445 /* Note that some or all channels of an input may be unused: */
2446 actual_in = 0;
2447 for (i = 0; i < so->inputs_count; i++) {
2448 unsigned j, regid = ~0, compmask = 0;
2449 so->inputs[i].ncomp = 0;
2450 for (j = 0; j < 4; j++) {
2451 struct ir3_instruction *in = inputs[(i*4) + j];
2452 if (in) {
2453 compmask |= (1 << j);
2454 regid = in->regs[0]->num - j;
2455 actual_in++;
2456 so->inputs[i].ncomp++;
2457 }
2458 }
2459 so->inputs[i].regid = regid;
2460 so->inputs[i].compmask = compmask;
2461 }
2462
2463 /* fragment shader always gets full vec4's even if it doesn't
2464 * fetch all components, but vertex shader we need to update
2465 * with the actual number of components fetch, otherwise thing
2466 * will hang due to mismaptch between VFD_DECODE's and
2467 * TOTALATTRTOVS
2468 */
2469 if (so->type == SHADER_VERTEX)
2470 so->total_in = actual_in;
2471 else
2472 so->total_in = align(max_bary + 1, 4);
2473
2474 out:
2475 if (ret) {
2476 if (so->ir)
2477 ir3_destroy(so->ir);
2478 so->ir = NULL;
2479 }
2480 compile_free(ctx);
2481
2482 return ret;
2483 }