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