freedreno/ir3: Use regid() helper when setting up precolor regs
[mesa.git] / src / freedreno / ir3 / ir3_compiler_nir.c
1 /*
2 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <stdarg.h>
28
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_math.h"
32
33 #include "ir3_compiler.h"
34 #include "ir3_image.h"
35 #include "ir3_shader.h"
36 #include "ir3_nir.h"
37
38 #include "instr-a3xx.h"
39 #include "ir3.h"
40 #include "ir3_context.h"
41
42
43 static struct ir3_instruction *
44 create_indirect_load(struct ir3_context *ctx, unsigned arrsz, int n,
45 struct ir3_instruction *address, struct ir3_instruction *collect)
46 {
47 struct ir3_block *block = ctx->block;
48 struct ir3_instruction *mov;
49 struct ir3_register *src;
50
51 mov = ir3_instr_create(block, OPC_MOV);
52 mov->cat1.src_type = TYPE_U32;
53 mov->cat1.dst_type = TYPE_U32;
54 ir3_reg_create(mov, 0, 0);
55 src = ir3_reg_create(mov, 0, IR3_REG_SSA | IR3_REG_RELATIV);
56 src->instr = collect;
57 src->size = arrsz;
58 src->array.offset = n;
59
60 ir3_instr_set_address(mov, address);
61
62 return mov;
63 }
64
65 static struct ir3_instruction *
66 create_input_compmask(struct ir3_context *ctx, unsigned n, unsigned compmask)
67 {
68 struct ir3_instruction *in;
69
70 in = ir3_instr_create(ctx->in_block, OPC_META_INPUT);
71 in->input.sysval = ~0;
72 ir3_reg_create(in, n, 0);
73
74 in->regs[0]->wrmask = compmask;
75
76 return in;
77 }
78
79 static struct ir3_instruction *
80 create_input(struct ir3_context *ctx, unsigned n)
81 {
82 return create_input_compmask(ctx, n, 0x1);
83 }
84
85 static struct ir3_instruction *
86 create_frag_input(struct ir3_context *ctx, bool use_ldlv, unsigned n)
87 {
88 struct ir3_block *block = ctx->block;
89 struct ir3_instruction *instr;
90 /* packed inloc is fixed up later: */
91 struct ir3_instruction *inloc = create_immed(block, n);
92
93 if (use_ldlv) {
94 instr = ir3_LDLV(block, inloc, 0, create_immed(block, 1), 0);
95 instr->cat6.type = TYPE_U32;
96 instr->cat6.iim_val = 1;
97 } else {
98 instr = ir3_BARY_F(block, inloc, 0, ctx->ij_pixel, 0);
99 instr->regs[2]->wrmask = 0x3;
100 }
101
102 return instr;
103 }
104
105 static struct ir3_instruction *
106 create_driver_param(struct ir3_context *ctx, enum ir3_driver_param dp)
107 {
108 /* first four vec4 sysval's reserved for UBOs: */
109 /* NOTE: dp is in scalar, but there can be >4 dp components: */
110 struct ir3_const_state *const_state = &ctx->so->shader->const_state;
111 unsigned n = const_state->offsets.driver_param;
112 unsigned r = regid(n + dp / 4, dp % 4);
113 return create_uniform(ctx->block, r);
114 }
115
116 /*
117 * Adreno uses uint rather than having dedicated bool type,
118 * which (potentially) requires some conversion, in particular
119 * when using output of an bool instr to int input, or visa
120 * versa.
121 *
122 * | Adreno | NIR |
123 * -------+---------+-------+-
124 * true | 1 | ~0 |
125 * false | 0 | 0 |
126 *
127 * To convert from an adreno bool (uint) to nir, use:
128 *
129 * absneg.s dst, (neg)src
130 *
131 * To convert back in the other direction:
132 *
133 * absneg.s dst, (abs)arc
134 *
135 * The CP step can clean up the absneg.s that cancel each other
136 * out, and with a slight bit of extra cleverness (to recognize
137 * the instructions which produce either a 0 or 1) can eliminate
138 * the absneg.s's completely when an instruction that wants
139 * 0/1 consumes the result. For example, when a nir 'bcsel'
140 * consumes the result of 'feq'. So we should be able to get by
141 * without a boolean resolve step, and without incuring any
142 * extra penalty in instruction count.
143 */
144
145 /* NIR bool -> native (adreno): */
146 static struct ir3_instruction *
147 ir3_b2n(struct ir3_block *block, struct ir3_instruction *instr)
148 {
149 return ir3_ABSNEG_S(block, instr, IR3_REG_SABS);
150 }
151
152 /* native (adreno) -> NIR bool: */
153 static struct ir3_instruction *
154 ir3_n2b(struct ir3_block *block, struct ir3_instruction *instr)
155 {
156 return ir3_ABSNEG_S(block, instr, IR3_REG_SNEG);
157 }
158
159 /*
160 * alu/sfu instructions:
161 */
162
163 static struct ir3_instruction *
164 create_cov(struct ir3_context *ctx, struct ir3_instruction *src,
165 unsigned src_bitsize, nir_op op)
166 {
167 type_t src_type, dst_type;
168
169 switch (op) {
170 case nir_op_f2f32:
171 case nir_op_f2f16_rtne:
172 case nir_op_f2f16_rtz:
173 case nir_op_f2f16:
174 case nir_op_f2i32:
175 case nir_op_f2i16:
176 case nir_op_f2i8:
177 case nir_op_f2u32:
178 case nir_op_f2u16:
179 case nir_op_f2u8:
180 switch (src_bitsize) {
181 case 32:
182 src_type = TYPE_F32;
183 break;
184 case 16:
185 src_type = TYPE_F16;
186 break;
187 default:
188 ir3_context_error(ctx, "invalid src bit size: %u", src_bitsize);
189 }
190 break;
191
192 case nir_op_i2f32:
193 case nir_op_i2f16:
194 case nir_op_i2i32:
195 case nir_op_i2i16:
196 case nir_op_i2i8:
197 switch (src_bitsize) {
198 case 32:
199 src_type = TYPE_S32;
200 break;
201 case 16:
202 src_type = TYPE_S16;
203 break;
204 case 8:
205 src_type = TYPE_S8;
206 break;
207 default:
208 ir3_context_error(ctx, "invalid src bit size: %u", src_bitsize);
209 }
210 break;
211
212 case nir_op_u2f32:
213 case nir_op_u2f16:
214 case nir_op_u2u32:
215 case nir_op_u2u16:
216 case nir_op_u2u8:
217 switch (src_bitsize) {
218 case 32:
219 src_type = TYPE_U32;
220 break;
221 case 16:
222 src_type = TYPE_U16;
223 break;
224 case 8:
225 src_type = TYPE_U8;
226 break;
227 default:
228 ir3_context_error(ctx, "invalid src bit size: %u", src_bitsize);
229 }
230 break;
231
232 default:
233 ir3_context_error(ctx, "invalid conversion op: %u", op);
234 }
235
236 switch (op) {
237 case nir_op_f2f32:
238 case nir_op_i2f32:
239 case nir_op_u2f32:
240 dst_type = TYPE_F32;
241 break;
242
243 case nir_op_f2f16_rtne:
244 case nir_op_f2f16_rtz:
245 case nir_op_f2f16:
246 /* TODO how to handle rounding mode? */
247 case nir_op_i2f16:
248 case nir_op_u2f16:
249 dst_type = TYPE_F16;
250 break;
251
252 case nir_op_f2i32:
253 case nir_op_i2i32:
254 dst_type = TYPE_S32;
255 break;
256
257 case nir_op_f2i16:
258 case nir_op_i2i16:
259 dst_type = TYPE_S16;
260 break;
261
262 case nir_op_f2i8:
263 case nir_op_i2i8:
264 dst_type = TYPE_S8;
265 break;
266
267 case nir_op_f2u32:
268 case nir_op_u2u32:
269 dst_type = TYPE_U32;
270 break;
271
272 case nir_op_f2u16:
273 case nir_op_u2u16:
274 dst_type = TYPE_U16;
275 break;
276
277 case nir_op_f2u8:
278 case nir_op_u2u8:
279 dst_type = TYPE_U8;
280 break;
281
282 default:
283 ir3_context_error(ctx, "invalid conversion op: %u", op);
284 }
285
286 return ir3_COV(ctx->block, src, src_type, dst_type);
287 }
288
289 static void
290 emit_alu(struct ir3_context *ctx, nir_alu_instr *alu)
291 {
292 const nir_op_info *info = &nir_op_infos[alu->op];
293 struct ir3_instruction **dst, *src[info->num_inputs];
294 unsigned bs[info->num_inputs]; /* bit size */
295 struct ir3_block *b = ctx->block;
296 unsigned dst_sz, wrmask;
297 type_t dst_type = nir_dest_bit_size(alu->dest.dest) < 32 ?
298 TYPE_U16 : TYPE_U32;
299
300 if (alu->dest.dest.is_ssa) {
301 dst_sz = alu->dest.dest.ssa.num_components;
302 wrmask = (1 << dst_sz) - 1;
303 } else {
304 dst_sz = alu->dest.dest.reg.reg->num_components;
305 wrmask = alu->dest.write_mask;
306 }
307
308 dst = ir3_get_dst(ctx, &alu->dest.dest, dst_sz);
309
310 /* Vectors are special in that they have non-scalarized writemasks,
311 * and just take the first swizzle channel for each argument in
312 * order into each writemask channel.
313 */
314 if ((alu->op == nir_op_vec2) ||
315 (alu->op == nir_op_vec3) ||
316 (alu->op == nir_op_vec4)) {
317
318 for (int i = 0; i < info->num_inputs; i++) {
319 nir_alu_src *asrc = &alu->src[i];
320
321 compile_assert(ctx, !asrc->abs);
322 compile_assert(ctx, !asrc->negate);
323
324 src[i] = ir3_get_src(ctx, &asrc->src)[asrc->swizzle[0]];
325 if (!src[i])
326 src[i] = create_immed_typed(ctx->block, 0, dst_type);
327 dst[i] = ir3_MOV(b, src[i], dst_type);
328 }
329
330 ir3_put_dst(ctx, &alu->dest.dest);
331 return;
332 }
333
334 /* We also get mov's with more than one component for mov's so
335 * handle those specially:
336 */
337 if (alu->op == nir_op_mov) {
338 nir_alu_src *asrc = &alu->src[0];
339 struct ir3_instruction *const *src0 = ir3_get_src(ctx, &asrc->src);
340
341 for (unsigned i = 0; i < dst_sz; i++) {
342 if (wrmask & (1 << i)) {
343 dst[i] = ir3_MOV(b, src0[asrc->swizzle[i]], dst_type);
344 } else {
345 dst[i] = NULL;
346 }
347 }
348
349 ir3_put_dst(ctx, &alu->dest.dest);
350 return;
351 }
352
353 /* General case: We can just grab the one used channel per src. */
354 for (int i = 0; i < info->num_inputs; i++) {
355 unsigned chan = ffs(alu->dest.write_mask) - 1;
356 nir_alu_src *asrc = &alu->src[i];
357
358 compile_assert(ctx, !asrc->abs);
359 compile_assert(ctx, !asrc->negate);
360
361 src[i] = ir3_get_src(ctx, &asrc->src)[asrc->swizzle[chan]];
362 bs[i] = nir_src_bit_size(asrc->src);
363
364 compile_assert(ctx, src[i]);
365 }
366
367 switch (alu->op) {
368 case nir_op_f2f32:
369 case nir_op_f2f16_rtne:
370 case nir_op_f2f16_rtz:
371 case nir_op_f2f16:
372 case nir_op_f2i32:
373 case nir_op_f2i16:
374 case nir_op_f2i8:
375 case nir_op_f2u32:
376 case nir_op_f2u16:
377 case nir_op_f2u8:
378 case nir_op_i2f32:
379 case nir_op_i2f16:
380 case nir_op_i2i32:
381 case nir_op_i2i16:
382 case nir_op_i2i8:
383 case nir_op_u2f32:
384 case nir_op_u2f16:
385 case nir_op_u2u32:
386 case nir_op_u2u16:
387 case nir_op_u2u8:
388 dst[0] = create_cov(ctx, src[0], bs[0], alu->op);
389 break;
390 case nir_op_fquantize2f16:
391 dst[0] = create_cov(ctx,
392 create_cov(ctx, src[0], 32, nir_op_f2f16),
393 16, nir_op_f2f32);
394 break;
395 case nir_op_f2b32:
396 dst[0] = ir3_CMPS_F(b, src[0], 0, create_immed(b, fui(0.0)), 0);
397 dst[0]->cat2.condition = IR3_COND_NE;
398 dst[0] = ir3_n2b(b, dst[0]);
399 break;
400 case nir_op_b2f16:
401 dst[0] = ir3_COV(b, ir3_b2n(b, src[0]), TYPE_U32, TYPE_F16);
402 break;
403 case nir_op_b2f32:
404 dst[0] = ir3_COV(b, ir3_b2n(b, src[0]), TYPE_U32, TYPE_F32);
405 break;
406 case nir_op_b2i8:
407 case nir_op_b2i16:
408 case nir_op_b2i32:
409 dst[0] = ir3_b2n(b, src[0]);
410 break;
411 case nir_op_i2b32:
412 dst[0] = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
413 dst[0]->cat2.condition = IR3_COND_NE;
414 dst[0] = ir3_n2b(b, dst[0]);
415 break;
416
417 case nir_op_fneg:
418 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FNEG);
419 break;
420 case nir_op_fabs:
421 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FABS);
422 break;
423 case nir_op_fmax:
424 dst[0] = ir3_MAX_F(b, src[0], 0, src[1], 0);
425 break;
426 case nir_op_fmin:
427 dst[0] = ir3_MIN_F(b, src[0], 0, src[1], 0);
428 break;
429 case nir_op_fsat:
430 /* if there is just a single use of the src, and it supports
431 * (sat) bit, we can just fold the (sat) flag back to the
432 * src instruction and create a mov. This is easier for cp
433 * to eliminate.
434 *
435 * TODO probably opc_cat==4 is ok too
436 */
437 if (alu->src[0].src.is_ssa &&
438 (list_length(&alu->src[0].src.ssa->uses) == 1) &&
439 ((opc_cat(src[0]->opc) == 2) || (opc_cat(src[0]->opc) == 3))) {
440 src[0]->flags |= IR3_INSTR_SAT;
441 dst[0] = ir3_MOV(b, src[0], dst_type);
442 } else {
443 /* otherwise generate a max.f that saturates.. blob does
444 * similar (generating a cat2 mov using max.f)
445 */
446 dst[0] = ir3_MAX_F(b, src[0], 0, src[0], 0);
447 dst[0]->flags |= IR3_INSTR_SAT;
448 }
449 break;
450 case nir_op_fmul:
451 dst[0] = ir3_MUL_F(b, src[0], 0, src[1], 0);
452 break;
453 case nir_op_fadd:
454 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], 0);
455 break;
456 case nir_op_fsub:
457 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], IR3_REG_FNEG);
458 break;
459 case nir_op_ffma:
460 dst[0] = ir3_MAD_F32(b, src[0], 0, src[1], 0, src[2], 0);
461 break;
462 case nir_op_fddx:
463 case nir_op_fddx_coarse:
464 dst[0] = ir3_DSX(b, src[0], 0);
465 dst[0]->cat5.type = TYPE_F32;
466 break;
467 case nir_op_fddy:
468 case nir_op_fddy_coarse:
469 dst[0] = ir3_DSY(b, src[0], 0);
470 dst[0]->cat5.type = TYPE_F32;
471 break;
472 break;
473 case nir_op_flt32:
474 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
475 dst[0]->cat2.condition = IR3_COND_LT;
476 dst[0] = ir3_n2b(b, dst[0]);
477 break;
478 case nir_op_fge32:
479 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
480 dst[0]->cat2.condition = IR3_COND_GE;
481 dst[0] = ir3_n2b(b, dst[0]);
482 break;
483 case nir_op_feq32:
484 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
485 dst[0]->cat2.condition = IR3_COND_EQ;
486 dst[0] = ir3_n2b(b, dst[0]);
487 break;
488 case nir_op_fne32:
489 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
490 dst[0]->cat2.condition = IR3_COND_NE;
491 dst[0] = ir3_n2b(b, dst[0]);
492 break;
493 case nir_op_fceil:
494 dst[0] = ir3_CEIL_F(b, src[0], 0);
495 break;
496 case nir_op_ffloor:
497 dst[0] = ir3_FLOOR_F(b, src[0], 0);
498 break;
499 case nir_op_ftrunc:
500 dst[0] = ir3_TRUNC_F(b, src[0], 0);
501 break;
502 case nir_op_fround_even:
503 dst[0] = ir3_RNDNE_F(b, src[0], 0);
504 break;
505 case nir_op_fsign:
506 dst[0] = ir3_SIGN_F(b, src[0], 0);
507 break;
508
509 case nir_op_fsin:
510 dst[0] = ir3_SIN(b, src[0], 0);
511 break;
512 case nir_op_fcos:
513 dst[0] = ir3_COS(b, src[0], 0);
514 break;
515 case nir_op_frsq:
516 dst[0] = ir3_RSQ(b, src[0], 0);
517 break;
518 case nir_op_frcp:
519 dst[0] = ir3_RCP(b, src[0], 0);
520 break;
521 case nir_op_flog2:
522 dst[0] = ir3_LOG2(b, src[0], 0);
523 break;
524 case nir_op_fexp2:
525 dst[0] = ir3_EXP2(b, src[0], 0);
526 break;
527 case nir_op_fsqrt:
528 dst[0] = ir3_SQRT(b, src[0], 0);
529 break;
530
531 case nir_op_iabs:
532 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SABS);
533 break;
534 case nir_op_iadd:
535 dst[0] = ir3_ADD_U(b, src[0], 0, src[1], 0);
536 break;
537 case nir_op_iand:
538 dst[0] = ir3_AND_B(b, src[0], 0, src[1], 0);
539 break;
540 case nir_op_imax:
541 dst[0] = ir3_MAX_S(b, src[0], 0, src[1], 0);
542 break;
543 case nir_op_umax:
544 dst[0] = ir3_MAX_U(b, src[0], 0, src[1], 0);
545 break;
546 case nir_op_imin:
547 dst[0] = ir3_MIN_S(b, src[0], 0, src[1], 0);
548 break;
549 case nir_op_umin:
550 dst[0] = ir3_MIN_U(b, src[0], 0, src[1], 0);
551 break;
552 case nir_op_umul_low:
553 dst[0] = ir3_MULL_U(b, src[0], 0, src[1], 0);
554 break;
555 case nir_op_imadsh_mix16:
556 dst[0] = ir3_MADSH_M16(b, src[0], 0, src[1], 0, src[2], 0);
557 break;
558 case nir_op_imad24_ir3:
559 dst[0] = ir3_MAD_S24(b, src[0], 0, src[1], 0, src[2], 0);
560 break;
561 case nir_op_imul24:
562 dst[0] = ir3_MUL_S24(b, src[0], 0, src[1], 0);
563 break;
564 case nir_op_ineg:
565 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SNEG);
566 break;
567 case nir_op_inot:
568 dst[0] = ir3_NOT_B(b, src[0], 0);
569 break;
570 case nir_op_ior:
571 dst[0] = ir3_OR_B(b, src[0], 0, src[1], 0);
572 break;
573 case nir_op_ishl:
574 dst[0] = ir3_SHL_B(b, src[0], 0, src[1], 0);
575 break;
576 case nir_op_ishr:
577 dst[0] = ir3_ASHR_B(b, src[0], 0, src[1], 0);
578 break;
579 case nir_op_isub:
580 dst[0] = ir3_SUB_U(b, src[0], 0, src[1], 0);
581 break;
582 case nir_op_ixor:
583 dst[0] = ir3_XOR_B(b, src[0], 0, src[1], 0);
584 break;
585 case nir_op_ushr:
586 dst[0] = ir3_SHR_B(b, src[0], 0, src[1], 0);
587 break;
588 case nir_op_ilt32:
589 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
590 dst[0]->cat2.condition = IR3_COND_LT;
591 dst[0] = ir3_n2b(b, dst[0]);
592 break;
593 case nir_op_ige32:
594 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
595 dst[0]->cat2.condition = IR3_COND_GE;
596 dst[0] = ir3_n2b(b, dst[0]);
597 break;
598 case nir_op_ieq32:
599 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
600 dst[0]->cat2.condition = IR3_COND_EQ;
601 dst[0] = ir3_n2b(b, dst[0]);
602 break;
603 case nir_op_ine32:
604 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
605 dst[0]->cat2.condition = IR3_COND_NE;
606 dst[0] = ir3_n2b(b, dst[0]);
607 break;
608 case nir_op_ult32:
609 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
610 dst[0]->cat2.condition = IR3_COND_LT;
611 dst[0] = ir3_n2b(b, dst[0]);
612 break;
613 case nir_op_uge32:
614 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
615 dst[0]->cat2.condition = IR3_COND_GE;
616 dst[0] = ir3_n2b(b, dst[0]);
617 break;
618
619 case nir_op_b32csel: {
620 struct ir3_instruction *cond = ir3_b2n(b, src[0]);
621 compile_assert(ctx, bs[1] == bs[2]);
622 /* the boolean condition is 32b even if src[1] and src[2] are
623 * half-precision, but sel.b16 wants all three src's to be the
624 * same type.
625 */
626 if (bs[1] < 32)
627 cond = ir3_COV(b, cond, TYPE_U32, TYPE_U16);
628 dst[0] = ir3_SEL_B32(b, src[1], 0, cond, 0, src[2], 0);
629 break;
630 }
631 case nir_op_bit_count: {
632 // TODO, we need to do this 16b at a time on a5xx+a6xx.. need to
633 // double check on earlier gen's. Once half-precision support is
634 // in place, this should probably move to a NIR lowering pass:
635 struct ir3_instruction *hi, *lo;
636
637 hi = ir3_COV(b, ir3_SHR_B(b, src[0], 0, create_immed(b, 16), 0),
638 TYPE_U32, TYPE_U16);
639 lo = ir3_COV(b, src[0], TYPE_U32, TYPE_U16);
640
641 hi = ir3_CBITS_B(b, hi, 0);
642 lo = ir3_CBITS_B(b, lo, 0);
643
644 // TODO maybe the builders should default to making dst half-precision
645 // if the src's were half precision, to make this less awkward.. otoh
646 // we should probably just do this lowering in NIR.
647 hi->regs[0]->flags |= IR3_REG_HALF;
648 lo->regs[0]->flags |= IR3_REG_HALF;
649
650 dst[0] = ir3_ADD_S(b, hi, 0, lo, 0);
651 dst[0]->regs[0]->flags |= IR3_REG_HALF;
652 dst[0] = ir3_COV(b, dst[0], TYPE_U16, TYPE_U32);
653 break;
654 }
655 case nir_op_ifind_msb: {
656 struct ir3_instruction *cmp;
657 dst[0] = ir3_CLZ_S(b, src[0], 0);
658 cmp = ir3_CMPS_S(b, dst[0], 0, create_immed(b, 0), 0);
659 cmp->cat2.condition = IR3_COND_GE;
660 dst[0] = ir3_SEL_B32(b,
661 ir3_SUB_U(b, create_immed(b, 31), 0, dst[0], 0), 0,
662 cmp, 0, dst[0], 0);
663 break;
664 }
665 case nir_op_ufind_msb:
666 dst[0] = ir3_CLZ_B(b, src[0], 0);
667 dst[0] = ir3_SEL_B32(b,
668 ir3_SUB_U(b, create_immed(b, 31), 0, dst[0], 0), 0,
669 src[0], 0, dst[0], 0);
670 break;
671 case nir_op_find_lsb:
672 dst[0] = ir3_BFREV_B(b, src[0], 0);
673 dst[0] = ir3_CLZ_B(b, dst[0], 0);
674 break;
675 case nir_op_bitfield_reverse:
676 dst[0] = ir3_BFREV_B(b, src[0], 0);
677 break;
678
679 default:
680 ir3_context_error(ctx, "Unhandled ALU op: %s\n",
681 nir_op_infos[alu->op].name);
682 break;
683 }
684
685 ir3_put_dst(ctx, &alu->dest.dest);
686 }
687
688 /* handles direct/indirect UBO reads: */
689 static void
690 emit_intrinsic_load_ubo(struct ir3_context *ctx, nir_intrinsic_instr *intr,
691 struct ir3_instruction **dst)
692 {
693 struct ir3_block *b = ctx->block;
694 struct ir3_instruction *base_lo, *base_hi, *addr, *src0, *src1;
695 /* UBO addresses are the first driver params, but subtract 2 here to
696 * account for nir_lower_uniforms_to_ubo rebasing the UBOs such that UBO 0
697 * is the uniforms: */
698 struct ir3_const_state *const_state = &ctx->so->shader->const_state;
699 unsigned ubo = regid(const_state->offsets.ubo, 0) - 2;
700 const unsigned ptrsz = ir3_pointer_size(ctx->compiler);
701
702 int off = 0;
703
704 /* First src is ubo index, which could either be an immed or not: */
705 src0 = ir3_get_src(ctx, &intr->src[0])[0];
706 if (is_same_type_mov(src0) &&
707 (src0->regs[1]->flags & IR3_REG_IMMED)) {
708 base_lo = create_uniform(b, ubo + (src0->regs[1]->iim_val * ptrsz));
709 base_hi = create_uniform(b, ubo + (src0->regs[1]->iim_val * ptrsz) + 1);
710 } else {
711 base_lo = create_uniform_indirect(b, ubo, ir3_get_addr(ctx, src0, ptrsz));
712 base_hi = create_uniform_indirect(b, ubo + 1, ir3_get_addr(ctx, src0, ptrsz));
713
714 /* NOTE: since relative addressing is used, make sure constlen is
715 * at least big enough to cover all the UBO addresses, since the
716 * assembler won't know what the max address reg is.
717 */
718 ctx->so->constlen = MAX2(ctx->so->constlen,
719 const_state->offsets.ubo + (ctx->s->info.num_ubos * ptrsz));
720 }
721
722 /* note: on 32bit gpu's base_hi is ignored and DCE'd */
723 addr = base_lo;
724
725 if (nir_src_is_const(intr->src[1])) {
726 off += nir_src_as_uint(intr->src[1]);
727 } else {
728 /* For load_ubo_indirect, second src is indirect offset: */
729 src1 = ir3_get_src(ctx, &intr->src[1])[0];
730
731 /* and add offset to addr: */
732 addr = ir3_ADD_S(b, addr, 0, src1, 0);
733 }
734
735 /* if offset is to large to encode in the ldg, split it out: */
736 if ((off + (intr->num_components * 4)) > 1024) {
737 /* split out the minimal amount to improve the odds that
738 * cp can fit the immediate in the add.s instruction:
739 */
740 unsigned off2 = off + (intr->num_components * 4) - 1024;
741 addr = ir3_ADD_S(b, addr, 0, create_immed(b, off2), 0);
742 off -= off2;
743 }
744
745 if (ptrsz == 2) {
746 struct ir3_instruction *carry;
747
748 /* handle 32b rollover, ie:
749 * if (addr < base_lo)
750 * base_hi++
751 */
752 carry = ir3_CMPS_U(b, addr, 0, base_lo, 0);
753 carry->cat2.condition = IR3_COND_LT;
754 base_hi = ir3_ADD_S(b, base_hi, 0, carry, 0);
755
756 addr = ir3_create_collect(ctx, (struct ir3_instruction*[]){ addr, base_hi }, 2);
757 }
758
759 for (int i = 0; i < intr->num_components; i++) {
760 struct ir3_instruction *load =
761 ir3_LDG(b, addr, 0, create_immed(b, 1), 0, /* num components */
762 create_immed(b, off + i * 4), 0);
763 load->cat6.type = TYPE_U32;
764 dst[i] = load;
765 }
766 }
767
768 /* src[] = { block_index } */
769 static void
770 emit_intrinsic_ssbo_size(struct ir3_context *ctx, nir_intrinsic_instr *intr,
771 struct ir3_instruction **dst)
772 {
773 /* SSBO size stored as a const starting at ssbo_sizes: */
774 struct ir3_const_state *const_state = &ctx->so->shader->const_state;
775 unsigned blk_idx = nir_src_as_uint(intr->src[0]);
776 unsigned idx = regid(const_state->offsets.ssbo_sizes, 0) +
777 const_state->ssbo_size.off[blk_idx];
778
779 debug_assert(const_state->ssbo_size.mask & (1 << blk_idx));
780
781 dst[0] = create_uniform(ctx->block, idx);
782 }
783
784 /* src[] = { offset }. const_index[] = { base } */
785 static void
786 emit_intrinsic_load_shared(struct ir3_context *ctx, nir_intrinsic_instr *intr,
787 struct ir3_instruction **dst)
788 {
789 struct ir3_block *b = ctx->block;
790 struct ir3_instruction *ldl, *offset;
791 unsigned base;
792
793 offset = ir3_get_src(ctx, &intr->src[0])[0];
794 base = nir_intrinsic_base(intr);
795
796 ldl = ir3_LDL(b, offset, 0,
797 create_immed(b, intr->num_components), 0,
798 create_immed(b, base), 0);
799
800 ldl->cat6.type = utype_dst(intr->dest);
801 ldl->regs[0]->wrmask = MASK(intr->num_components);
802
803 ldl->barrier_class = IR3_BARRIER_SHARED_R;
804 ldl->barrier_conflict = IR3_BARRIER_SHARED_W;
805
806 ir3_split_dest(b, dst, ldl, 0, intr->num_components);
807 }
808
809 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
810 static void
811 emit_intrinsic_store_shared(struct ir3_context *ctx, nir_intrinsic_instr *intr)
812 {
813 struct ir3_block *b = ctx->block;
814 struct ir3_instruction *stl, *offset;
815 struct ir3_instruction * const *value;
816 unsigned base, wrmask;
817
818 value = ir3_get_src(ctx, &intr->src[0]);
819 offset = ir3_get_src(ctx, &intr->src[1])[0];
820
821 base = nir_intrinsic_base(intr);
822 wrmask = nir_intrinsic_write_mask(intr);
823
824 /* Combine groups of consecutive enabled channels in one write
825 * message. We use ffs to find the first enabled channel and then ffs on
826 * the bit-inverse, down-shifted writemask to determine the length of
827 * the block of enabled bits.
828 *
829 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
830 */
831 while (wrmask) {
832 unsigned first_component = ffs(wrmask) - 1;
833 unsigned length = ffs(~(wrmask >> first_component)) - 1;
834
835 stl = ir3_STL(b, offset, 0,
836 ir3_create_collect(ctx, &value[first_component], length), 0,
837 create_immed(b, length), 0);
838 stl->cat6.dst_offset = first_component + base;
839 stl->cat6.type = utype_src(intr->src[0]);
840 stl->barrier_class = IR3_BARRIER_SHARED_W;
841 stl->barrier_conflict = IR3_BARRIER_SHARED_R | IR3_BARRIER_SHARED_W;
842
843 array_insert(b, b->keeps, stl);
844
845 /* Clear the bits in the writemask that we just wrote, then try
846 * again to see if more channels are left.
847 */
848 wrmask &= (15 << (first_component + length));
849 }
850 }
851
852 /* src[] = { offset }. const_index[] = { base } */
853 static void
854 emit_intrinsic_load_shared_ir3(struct ir3_context *ctx, nir_intrinsic_instr *intr,
855 struct ir3_instruction **dst)
856 {
857 struct ir3_block *b = ctx->block;
858 struct ir3_instruction *load, *offset;
859 unsigned base;
860
861 offset = ir3_get_src(ctx, &intr->src[0])[0];
862 base = nir_intrinsic_base(intr);
863
864 load = ir3_LDLW(b, offset, 0,
865 create_immed(b, intr->num_components), 0,
866 create_immed(b, base), 0);
867
868 load->cat6.type = utype_dst(intr->dest);
869 load->regs[0]->wrmask = MASK(intr->num_components);
870
871 load->barrier_class = IR3_BARRIER_SHARED_R;
872 load->barrier_conflict = IR3_BARRIER_SHARED_W;
873
874 ir3_split_dest(b, dst, load, 0, intr->num_components);
875 }
876
877 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
878 static void
879 emit_intrinsic_store_shared_ir3(struct ir3_context *ctx, nir_intrinsic_instr *intr)
880 {
881 struct ir3_block *b = ctx->block;
882 struct ir3_instruction *store, *offset;
883 struct ir3_instruction * const *value;
884 unsigned base, wrmask;
885
886 value = ir3_get_src(ctx, &intr->src[0]);
887 offset = ir3_get_src(ctx, &intr->src[1])[0];
888
889 base = nir_intrinsic_base(intr);
890 wrmask = nir_intrinsic_write_mask(intr);
891
892 /* Combine groups of consecutive enabled channels in one write
893 * message. We use ffs to find the first enabled channel and then ffs on
894 * the bit-inverse, down-shifted writemask to determine the length of
895 * the block of enabled bits.
896 *
897 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
898 */
899 while (wrmask) {
900 unsigned first_component = ffs(wrmask) - 1;
901 unsigned length = ffs(~(wrmask >> first_component)) - 1;
902
903 store = ir3_STLW(b, offset, 0,
904 ir3_create_collect(ctx, &value[first_component], length), 0,
905 create_immed(b, length), 0);
906
907 store->cat6.dst_offset = first_component + base;
908 store->cat6.type = utype_src(intr->src[0]);
909 store->barrier_class = IR3_BARRIER_SHARED_W;
910 store->barrier_conflict = IR3_BARRIER_SHARED_R | IR3_BARRIER_SHARED_W;
911
912 array_insert(b, b->keeps, store);
913
914 /* Clear the bits in the writemask that we just wrote, then try
915 * again to see if more channels are left.
916 */
917 wrmask &= (15 << (first_component + length));
918 }
919 }
920
921 /*
922 * CS shared variable atomic intrinsics
923 *
924 * All of the shared variable atomic memory operations read a value from
925 * memory, compute a new value using one of the operations below, write the
926 * new value to memory, and return the original value read.
927 *
928 * All operations take 2 sources except CompSwap that takes 3. These
929 * sources represent:
930 *
931 * 0: The offset into the shared variable storage region that the atomic
932 * operation will operate on.
933 * 1: The data parameter to the atomic function (i.e. the value to add
934 * in shared_atomic_add, etc).
935 * 2: For CompSwap only: the second data parameter.
936 */
937 static struct ir3_instruction *
938 emit_intrinsic_atomic_shared(struct ir3_context *ctx, nir_intrinsic_instr *intr)
939 {
940 struct ir3_block *b = ctx->block;
941 struct ir3_instruction *atomic, *src0, *src1;
942 type_t type = TYPE_U32;
943
944 src0 = ir3_get_src(ctx, &intr->src[0])[0]; /* offset */
945 src1 = ir3_get_src(ctx, &intr->src[1])[0]; /* value */
946
947 switch (intr->intrinsic) {
948 case nir_intrinsic_shared_atomic_add:
949 atomic = ir3_ATOMIC_ADD(b, src0, 0, src1, 0);
950 break;
951 case nir_intrinsic_shared_atomic_imin:
952 atomic = ir3_ATOMIC_MIN(b, src0, 0, src1, 0);
953 type = TYPE_S32;
954 break;
955 case nir_intrinsic_shared_atomic_umin:
956 atomic = ir3_ATOMIC_MIN(b, src0, 0, src1, 0);
957 break;
958 case nir_intrinsic_shared_atomic_imax:
959 atomic = ir3_ATOMIC_MAX(b, src0, 0, src1, 0);
960 type = TYPE_S32;
961 break;
962 case nir_intrinsic_shared_atomic_umax:
963 atomic = ir3_ATOMIC_MAX(b, src0, 0, src1, 0);
964 break;
965 case nir_intrinsic_shared_atomic_and:
966 atomic = ir3_ATOMIC_AND(b, src0, 0, src1, 0);
967 break;
968 case nir_intrinsic_shared_atomic_or:
969 atomic = ir3_ATOMIC_OR(b, src0, 0, src1, 0);
970 break;
971 case nir_intrinsic_shared_atomic_xor:
972 atomic = ir3_ATOMIC_XOR(b, src0, 0, src1, 0);
973 break;
974 case nir_intrinsic_shared_atomic_exchange:
975 atomic = ir3_ATOMIC_XCHG(b, src0, 0, src1, 0);
976 break;
977 case nir_intrinsic_shared_atomic_comp_swap:
978 /* for cmpxchg, src1 is [ui]vec2(data, compare): */
979 src1 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
980 ir3_get_src(ctx, &intr->src[2])[0],
981 src1,
982 }, 2);
983 atomic = ir3_ATOMIC_CMPXCHG(b, src0, 0, src1, 0);
984 break;
985 default:
986 unreachable("boo");
987 }
988
989 atomic->cat6.iim_val = 1;
990 atomic->cat6.d = 1;
991 atomic->cat6.type = type;
992 atomic->barrier_class = IR3_BARRIER_SHARED_W;
993 atomic->barrier_conflict = IR3_BARRIER_SHARED_R | IR3_BARRIER_SHARED_W;
994
995 /* even if nothing consume the result, we can't DCE the instruction: */
996 array_insert(b, b->keeps, atomic);
997
998 return atomic;
999 }
1000
1001 /* TODO handle actual indirect/dynamic case.. which is going to be weird
1002 * to handle with the image_mapping table..
1003 */
1004 static struct ir3_instruction *
1005 get_image_samp_tex_src(struct ir3_context *ctx, nir_intrinsic_instr *intr)
1006 {
1007 unsigned slot = ir3_get_image_slot(nir_src_as_deref(intr->src[0]));
1008 unsigned tex_idx = ir3_image_to_tex(&ctx->so->image_mapping, slot);
1009 struct ir3_instruction *texture, *sampler;
1010
1011 texture = create_immed_typed(ctx->block, tex_idx, TYPE_U16);
1012 sampler = create_immed_typed(ctx->block, tex_idx, TYPE_U16);
1013
1014 return ir3_create_collect(ctx, (struct ir3_instruction*[]){
1015 sampler,
1016 texture,
1017 }, 2);
1018 }
1019
1020 /* src[] = { deref, coord, sample_index }. const_index[] = {} */
1021 static void
1022 emit_intrinsic_load_image(struct ir3_context *ctx, nir_intrinsic_instr *intr,
1023 struct ir3_instruction **dst)
1024 {
1025 struct ir3_block *b = ctx->block;
1026 const nir_variable *var = nir_intrinsic_get_var(intr, 0);
1027 struct ir3_instruction *samp_tex = get_image_samp_tex_src(ctx, intr);
1028 struct ir3_instruction *sam;
1029 struct ir3_instruction * const *src0 = ir3_get_src(ctx, &intr->src[1]);
1030 struct ir3_instruction *coords[4];
1031 unsigned flags, ncoords = ir3_get_image_coords(var, &flags);
1032 type_t type = ir3_get_image_type(var);
1033
1034 /* hmm, this seems a bit odd, but it is what blob does and (at least
1035 * a5xx) just faults on bogus addresses otherwise:
1036 */
1037 if (flags & IR3_INSTR_3D) {
1038 flags &= ~IR3_INSTR_3D;
1039 flags |= IR3_INSTR_A;
1040 }
1041
1042 for (unsigned i = 0; i < ncoords; i++)
1043 coords[i] = src0[i];
1044
1045 if (ncoords == 1)
1046 coords[ncoords++] = create_immed(b, 0);
1047
1048 sam = ir3_SAM(b, OPC_ISAM, type, 0b1111, flags,
1049 samp_tex, ir3_create_collect(ctx, coords, ncoords), NULL);
1050
1051 sam->barrier_class = IR3_BARRIER_IMAGE_R;
1052 sam->barrier_conflict = IR3_BARRIER_IMAGE_W;
1053
1054 ir3_split_dest(b, dst, sam, 0, 4);
1055 }
1056
1057 static void
1058 emit_intrinsic_image_size(struct ir3_context *ctx, nir_intrinsic_instr *intr,
1059 struct ir3_instruction **dst)
1060 {
1061 struct ir3_block *b = ctx->block;
1062 const nir_variable *var = nir_intrinsic_get_var(intr, 0);
1063 struct ir3_instruction *samp_tex = get_image_samp_tex_src(ctx, intr);
1064 struct ir3_instruction *sam, *lod;
1065 unsigned flags, ncoords = ir3_get_image_coords(var, &flags);
1066
1067 lod = create_immed(b, 0);
1068 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, 0b1111, flags,
1069 samp_tex, lod, NULL);
1070
1071 /* Array size actually ends up in .w rather than .z. This doesn't
1072 * matter for miplevel 0, but for higher mips the value in z is
1073 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1074 * returned, which means that we have to add 1 to it for arrays for
1075 * a3xx.
1076 *
1077 * Note use a temporary dst and then copy, since the size of the dst
1078 * array that is passed in is based on nir's understanding of the
1079 * result size, not the hardware's
1080 */
1081 struct ir3_instruction *tmp[4];
1082
1083 ir3_split_dest(b, tmp, sam, 0, 4);
1084
1085 /* get_size instruction returns size in bytes instead of texels
1086 * for imageBuffer, so we need to divide it by the pixel size
1087 * of the image format.
1088 *
1089 * TODO: This is at least true on a5xx. Check other gens.
1090 */
1091 enum glsl_sampler_dim dim =
1092 glsl_get_sampler_dim(glsl_without_array(var->type));
1093 if (dim == GLSL_SAMPLER_DIM_BUF) {
1094 /* Since all the possible values the divisor can take are
1095 * power-of-two (4, 8, or 16), the division is implemented
1096 * as a shift-right.
1097 * During shader setup, the log2 of the image format's
1098 * bytes-per-pixel should have been emitted in 2nd slot of
1099 * image_dims. See ir3_shader::emit_image_dims().
1100 */
1101 struct ir3_const_state *const_state = &ctx->so->shader->const_state;
1102 unsigned cb = regid(const_state->offsets.image_dims, 0) +
1103 const_state->image_dims.off[var->data.driver_location];
1104 struct ir3_instruction *aux = create_uniform(b, cb + 1);
1105
1106 tmp[0] = ir3_SHR_B(b, tmp[0], 0, aux, 0);
1107 }
1108
1109 for (unsigned i = 0; i < ncoords; i++)
1110 dst[i] = tmp[i];
1111
1112 if (flags & IR3_INSTR_A) {
1113 if (ctx->compiler->levels_add_one) {
1114 dst[ncoords-1] = ir3_ADD_U(b, tmp[3], 0, create_immed(b, 1), 0);
1115 } else {
1116 dst[ncoords-1] = ir3_MOV(b, tmp[3], TYPE_U32);
1117 }
1118 }
1119 }
1120
1121 static void
1122 emit_intrinsic_barrier(struct ir3_context *ctx, nir_intrinsic_instr *intr)
1123 {
1124 struct ir3_block *b = ctx->block;
1125 struct ir3_instruction *barrier;
1126
1127 switch (intr->intrinsic) {
1128 case nir_intrinsic_barrier:
1129 barrier = ir3_BAR(b);
1130 barrier->cat7.g = true;
1131 barrier->cat7.l = true;
1132 barrier->flags = IR3_INSTR_SS | IR3_INSTR_SY;
1133 barrier->barrier_class = IR3_BARRIER_EVERYTHING;
1134 break;
1135 case nir_intrinsic_memory_barrier:
1136 barrier = ir3_FENCE(b);
1137 barrier->cat7.g = true;
1138 barrier->cat7.r = true;
1139 barrier->cat7.w = true;
1140 barrier->cat7.l = true;
1141 barrier->barrier_class = IR3_BARRIER_IMAGE_W |
1142 IR3_BARRIER_BUFFER_W;
1143 barrier->barrier_conflict =
1144 IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W |
1145 IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
1146 break;
1147 case nir_intrinsic_memory_barrier_atomic_counter:
1148 case nir_intrinsic_memory_barrier_buffer:
1149 barrier = ir3_FENCE(b);
1150 barrier->cat7.g = true;
1151 barrier->cat7.r = true;
1152 barrier->cat7.w = true;
1153 barrier->barrier_class = IR3_BARRIER_BUFFER_W;
1154 barrier->barrier_conflict = IR3_BARRIER_BUFFER_R |
1155 IR3_BARRIER_BUFFER_W;
1156 break;
1157 case nir_intrinsic_memory_barrier_image:
1158 // TODO double check if this should have .g set
1159 barrier = ir3_FENCE(b);
1160 barrier->cat7.g = true;
1161 barrier->cat7.r = true;
1162 barrier->cat7.w = true;
1163 barrier->barrier_class = IR3_BARRIER_IMAGE_W;
1164 barrier->barrier_conflict = IR3_BARRIER_IMAGE_R |
1165 IR3_BARRIER_IMAGE_W;
1166 break;
1167 case nir_intrinsic_memory_barrier_shared:
1168 barrier = ir3_FENCE(b);
1169 barrier->cat7.g = true;
1170 barrier->cat7.l = true;
1171 barrier->cat7.r = true;
1172 barrier->cat7.w = true;
1173 barrier->barrier_class = IR3_BARRIER_SHARED_W;
1174 barrier->barrier_conflict = IR3_BARRIER_SHARED_R |
1175 IR3_BARRIER_SHARED_W;
1176 break;
1177 case nir_intrinsic_group_memory_barrier:
1178 barrier = ir3_FENCE(b);
1179 barrier->cat7.g = true;
1180 barrier->cat7.l = true;
1181 barrier->cat7.r = true;
1182 barrier->cat7.w = true;
1183 barrier->barrier_class = IR3_BARRIER_SHARED_W |
1184 IR3_BARRIER_IMAGE_W |
1185 IR3_BARRIER_BUFFER_W;
1186 barrier->barrier_conflict =
1187 IR3_BARRIER_SHARED_R | IR3_BARRIER_SHARED_W |
1188 IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W |
1189 IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
1190 break;
1191 default:
1192 unreachable("boo");
1193 }
1194
1195 /* make sure barrier doesn't get DCE'd */
1196 array_insert(b, b->keeps, barrier);
1197 }
1198
1199 static void add_sysval_input_compmask(struct ir3_context *ctx,
1200 gl_system_value slot, unsigned compmask,
1201 struct ir3_instruction *instr)
1202 {
1203 struct ir3_shader_variant *so = ctx->so;
1204 unsigned r = regid(so->inputs_count, 0);
1205 unsigned n = so->inputs_count++;
1206
1207 assert(instr->opc == OPC_META_INPUT);
1208 instr->input.sysval = slot;
1209
1210 so->inputs[n].sysval = true;
1211 so->inputs[n].slot = slot;
1212 so->inputs[n].compmask = compmask;
1213 so->inputs[n].regid = r;
1214 so->inputs[n].interpolate = INTERP_MODE_FLAT;
1215 so->total_in++;
1216
1217 ctx->ir->ninputs = MAX2(ctx->ir->ninputs, r + 1);
1218 ctx->ir->inputs[r] = instr;
1219 }
1220
1221 static void add_sysval_input(struct ir3_context *ctx, gl_system_value slot,
1222 struct ir3_instruction *instr)
1223 {
1224 add_sysval_input_compmask(ctx, slot, 0x1, instr);
1225 }
1226
1227 static struct ir3_instruction *
1228 get_barycentric_centroid(struct ir3_context *ctx)
1229 {
1230 if (!ctx->ij_centroid) {
1231 struct ir3_instruction *xy[2];
1232 struct ir3_instruction *ij;
1233
1234 ij = create_input_compmask(ctx, 0, 0x3);
1235 ir3_split_dest(ctx->block, xy, ij, 0, 2);
1236
1237 ctx->ij_centroid = ir3_create_collect(ctx, xy, 2);
1238
1239 add_sysval_input_compmask(ctx,
1240 SYSTEM_VALUE_BARYCENTRIC_CENTROID,
1241 0x3, ij);
1242 }
1243
1244 return ctx->ij_centroid;
1245 }
1246
1247 static struct ir3_instruction *
1248 get_barycentric_sample(struct ir3_context *ctx)
1249 {
1250 if (!ctx->ij_sample) {
1251 struct ir3_instruction *xy[2];
1252 struct ir3_instruction *ij;
1253
1254 ij = create_input_compmask(ctx, 0, 0x3);
1255 ir3_split_dest(ctx->block, xy, ij, 0, 2);
1256
1257 ctx->ij_sample = ir3_create_collect(ctx, xy, 2);
1258
1259 add_sysval_input_compmask(ctx,
1260 SYSTEM_VALUE_BARYCENTRIC_SAMPLE,
1261 0x3, ij);
1262 }
1263
1264 return ctx->ij_sample;
1265 }
1266
1267 static struct ir3_instruction *
1268 get_barycentric_pixel(struct ir3_context *ctx)
1269 {
1270 /* TODO when tgsi_to_nir supports "new-style" FS inputs switch
1271 * this to create ij_pixel only on demand:
1272 */
1273 return ctx->ij_pixel;
1274 }
1275
1276 static struct ir3_instruction *
1277 get_frag_coord(struct ir3_context *ctx)
1278 {
1279 if (!ctx->frag_coord) {
1280 struct ir3_block *b = ctx->block;
1281 struct ir3_instruction *xyzw[4];
1282 struct ir3_instruction *hw_frag_coord;
1283
1284 hw_frag_coord = create_input_compmask(ctx, 0, 0xf);
1285 ir3_split_dest(ctx->block, xyzw, hw_frag_coord, 0, 4);
1286
1287 /* for frag_coord.xy, we get unsigned values.. we need
1288 * to subtract (integer) 8 and divide by 16 (right-
1289 * shift by 4) then convert to float:
1290 *
1291 * sub.s tmp, src, 8
1292 * shr.b tmp, tmp, 4
1293 * mov.u32f32 dst, tmp
1294 *
1295 */
1296 for (int i = 0; i < 2; i++) {
1297 xyzw[i] = ir3_SUB_S(b, xyzw[i], 0,
1298 create_immed(b, 8), 0);
1299 xyzw[i] = ir3_SHR_B(b, xyzw[i], 0,
1300 create_immed(b, 4), 0);
1301 xyzw[i] = ir3_COV(b, xyzw[i], TYPE_U32, TYPE_F32);
1302 }
1303
1304 ctx->frag_coord = ir3_create_collect(ctx, xyzw, 4);
1305
1306 add_sysval_input_compmask(ctx,
1307 SYSTEM_VALUE_FRAG_COORD,
1308 0xf, hw_frag_coord);
1309
1310 ctx->so->frag_coord = true;
1311 }
1312
1313 return ctx->frag_coord;
1314 }
1315
1316 static void
1317 emit_intrinsic(struct ir3_context *ctx, nir_intrinsic_instr *intr)
1318 {
1319 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1320 struct ir3_instruction **dst;
1321 struct ir3_instruction * const *src;
1322 struct ir3_block *b = ctx->block;
1323 int idx, comp;
1324
1325 if (info->has_dest) {
1326 unsigned n = nir_intrinsic_dest_components(intr);
1327 dst = ir3_get_dst(ctx, &intr->dest, n);
1328 } else {
1329 dst = NULL;
1330 }
1331
1332 const unsigned primitive_param = ctx->so->shader->const_state.offsets.primitive_param * 4;
1333 const unsigned primitive_map = ctx->so->shader->const_state.offsets.primitive_map * 4;
1334
1335 switch (intr->intrinsic) {
1336 case nir_intrinsic_load_uniform:
1337 idx = nir_intrinsic_base(intr);
1338 if (nir_src_is_const(intr->src[0])) {
1339 idx += nir_src_as_uint(intr->src[0]);
1340 for (int i = 0; i < intr->num_components; i++) {
1341 dst[i] = create_uniform_typed(b, idx + i,
1342 nir_dest_bit_size(intr->dest) < 32 ? TYPE_F16 : TYPE_F32);
1343 }
1344 } else {
1345 src = ir3_get_src(ctx, &intr->src[0]);
1346 for (int i = 0; i < intr->num_components; i++) {
1347 dst[i] = create_uniform_indirect(b, idx + i,
1348 ir3_get_addr(ctx, src[0], 1));
1349 }
1350 /* NOTE: if relative addressing is used, we set
1351 * constlen in the compiler (to worst-case value)
1352 * since we don't know in the assembler what the max
1353 * addr reg value can be:
1354 */
1355 ctx->so->constlen = MAX2(ctx->so->constlen,
1356 ctx->so->shader->ubo_state.size / 16);
1357 }
1358 break;
1359
1360 case nir_intrinsic_load_vs_primitive_stride_ir3:
1361 dst[0] = create_uniform(b, primitive_param + 0);
1362 break;
1363 case nir_intrinsic_load_vs_vertex_stride_ir3:
1364 dst[0] = create_uniform(b, primitive_param + 1);
1365 break;
1366 case nir_intrinsic_load_hs_patch_stride_ir3:
1367 dst[0] = create_uniform(b, primitive_param + 2);
1368 break;
1369 case nir_intrinsic_load_patch_vertices_in:
1370 dst[0] = create_uniform(b, primitive_param + 3);
1371 break;
1372 case nir_intrinsic_load_tess_param_base_ir3:
1373 dst[0] = create_uniform(b, primitive_param + 4);
1374 dst[1] = create_uniform(b, primitive_param + 5);
1375 break;
1376 case nir_intrinsic_load_tess_factor_base_ir3:
1377 dst[0] = create_uniform(b, primitive_param + 6);
1378 dst[1] = create_uniform(b, primitive_param + 7);
1379 break;
1380
1381 case nir_intrinsic_load_primitive_location_ir3:
1382 idx = nir_intrinsic_driver_location(intr);
1383 dst[0] = create_uniform(b, primitive_map + idx);
1384 break;
1385
1386 case nir_intrinsic_load_gs_header_ir3:
1387 dst[0] = ctx->gs_header;
1388 break;
1389 case nir_intrinsic_load_tcs_header_ir3:
1390 dst[0] = ctx->tcs_header;
1391 break;
1392
1393 case nir_intrinsic_load_primitive_id:
1394 dst[0] = ctx->primitive_id;
1395 break;
1396
1397 case nir_intrinsic_load_tess_coord:
1398 if (!ctx->tess_coord) {
1399 ctx->tess_coord = create_input_compmask(ctx, 0, 0x3);
1400 add_sysval_input_compmask(ctx, SYSTEM_VALUE_TESS_COORD,
1401 0x3, ctx->tess_coord);
1402 }
1403 ir3_split_dest(b, dst, ctx->tess_coord, 0, 2);
1404
1405 /* Unused, but ir3_put_dst() below wants to free something */
1406 dst[2] = create_immed(b, 0);
1407 break;
1408
1409 case nir_intrinsic_end_patch_ir3:
1410 assert(ctx->so->type == MESA_SHADER_TESS_CTRL);
1411 struct ir3_instruction *end = ir3_ENDPATCH(b);
1412 array_insert(b, b->keeps, end);
1413
1414 end->barrier_class = IR3_BARRIER_EVERYTHING;
1415 end->barrier_conflict = IR3_BARRIER_EVERYTHING;
1416 break;
1417
1418 case nir_intrinsic_store_global_ir3: {
1419 struct ir3_instruction *value, *addr, *offset;
1420
1421 addr = ir3_create_collect(ctx, (struct ir3_instruction*[]){
1422 ir3_get_src(ctx, &intr->src[1])[0],
1423 ir3_get_src(ctx, &intr->src[1])[1]
1424 }, 2);
1425
1426 offset = ir3_get_src(ctx, &intr->src[2])[0];
1427
1428 value = ir3_create_collect(ctx, ir3_get_src(ctx, &intr->src[0]),
1429 intr->num_components);
1430
1431 struct ir3_instruction *stg =
1432 ir3_STG_G(ctx->block, addr, 0, value, 0,
1433 create_immed(ctx->block, intr->num_components), 0, offset, 0);
1434 stg->cat6.type = TYPE_U32;
1435 stg->cat6.iim_val = 1;
1436
1437 array_insert(b, b->keeps, stg);
1438
1439 stg->barrier_class = IR3_BARRIER_BUFFER_W;
1440 stg->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
1441 break;
1442 }
1443
1444 case nir_intrinsic_load_global_ir3: {
1445 struct ir3_instruction *addr, *offset;
1446
1447 addr = ir3_create_collect(ctx, (struct ir3_instruction*[]){
1448 ir3_get_src(ctx, &intr->src[0])[0],
1449 ir3_get_src(ctx, &intr->src[0])[1]
1450 }, 2);
1451
1452 offset = ir3_get_src(ctx, &intr->src[1])[0];
1453
1454 struct ir3_instruction *load =
1455 ir3_LDG(b, addr, 0, create_immed(ctx->block, intr->num_components),
1456 0, offset, 0);
1457 load->cat6.type = TYPE_U32;
1458 load->regs[0]->wrmask = MASK(intr->num_components);
1459
1460 load->barrier_class = IR3_BARRIER_BUFFER_R;
1461 load->barrier_conflict = IR3_BARRIER_BUFFER_W;
1462
1463 ir3_split_dest(b, dst, load, 0, intr->num_components);
1464 break;
1465 }
1466
1467 case nir_intrinsic_load_ubo:
1468 emit_intrinsic_load_ubo(ctx, intr, dst);
1469 break;
1470 case nir_intrinsic_load_frag_coord:
1471 ir3_split_dest(b, dst, get_frag_coord(ctx), 0, 4);
1472 break;
1473 case nir_intrinsic_load_sample_pos_from_id: {
1474 /* NOTE: blob seems to always use TYPE_F16 and then cov.f16f32,
1475 * but that doesn't seem necessary.
1476 */
1477 struct ir3_instruction *offset =
1478 ir3_RGETPOS(b, ir3_get_src(ctx, &intr->src[0])[0], 0);
1479 offset->regs[0]->wrmask = 0x3;
1480 offset->cat5.type = TYPE_F32;
1481
1482 ir3_split_dest(b, dst, offset, 0, 2);
1483
1484 break;
1485 }
1486 case nir_intrinsic_load_size_ir3:
1487 if (!ctx->ij_size) {
1488 ctx->ij_size = create_input(ctx, 0);
1489
1490 add_sysval_input(ctx, SYSTEM_VALUE_BARYCENTRIC_SIZE,
1491 ctx->ij_size);
1492 }
1493 dst[0] = ctx->ij_size;
1494 break;
1495 case nir_intrinsic_load_barycentric_centroid:
1496 ir3_split_dest(b, dst, get_barycentric_centroid(ctx), 0, 2);
1497 break;
1498 case nir_intrinsic_load_barycentric_sample:
1499 if (ctx->so->key.msaa) {
1500 ir3_split_dest(b, dst, get_barycentric_sample(ctx), 0, 2);
1501 } else {
1502 ir3_split_dest(b, dst, get_barycentric_pixel(ctx), 0, 2);
1503 }
1504 break;
1505 case nir_intrinsic_load_barycentric_pixel:
1506 ir3_split_dest(b, dst, get_barycentric_pixel(ctx), 0, 2);
1507 break;
1508 case nir_intrinsic_load_interpolated_input:
1509 idx = nir_intrinsic_base(intr);
1510 comp = nir_intrinsic_component(intr);
1511 src = ir3_get_src(ctx, &intr->src[0]);
1512 if (nir_src_is_const(intr->src[1])) {
1513 struct ir3_instruction *coord = ir3_create_collect(ctx, src, 2);
1514 idx += nir_src_as_uint(intr->src[1]);
1515 for (int i = 0; i < intr->num_components; i++) {
1516 unsigned inloc = idx * 4 + i + comp;
1517 if (ctx->so->inputs[idx].bary &&
1518 !ctx->so->inputs[idx].use_ldlv) {
1519 dst[i] = ir3_BARY_F(b, create_immed(b, inloc), 0, coord, 0);
1520 } else {
1521 /* for non-varyings use the pre-setup input, since
1522 * that is easier than mapping things back to a
1523 * nir_variable to figure out what it is.
1524 */
1525 dst[i] = ctx->ir->inputs[inloc];
1526 }
1527 }
1528 } else {
1529 ir3_context_error(ctx, "unhandled");
1530 }
1531 break;
1532 case nir_intrinsic_load_input:
1533 idx = nir_intrinsic_base(intr);
1534 comp = nir_intrinsic_component(intr);
1535 if (nir_src_is_const(intr->src[0])) {
1536 idx += nir_src_as_uint(intr->src[0]);
1537 for (int i = 0; i < intr->num_components; i++) {
1538 unsigned n = idx * 4 + i + comp;
1539 dst[i] = ctx->ir->inputs[n];
1540 compile_assert(ctx, ctx->ir->inputs[n]);
1541 }
1542 } else {
1543 src = ir3_get_src(ctx, &intr->src[0]);
1544 struct ir3_instruction *collect =
1545 ir3_create_collect(ctx, ctx->ir->inputs, ctx->ir->ninputs);
1546 struct ir3_instruction *addr = ir3_get_addr(ctx, src[0], 4);
1547 for (int i = 0; i < intr->num_components; i++) {
1548 unsigned n = idx * 4 + i + comp;
1549 dst[i] = create_indirect_load(ctx, ctx->ir->ninputs,
1550 n, addr, collect);
1551 }
1552 }
1553 break;
1554 /* All SSBO intrinsics should have been lowered by 'lower_io_offsets'
1555 * pass and replaced by an ir3-specifc version that adds the
1556 * dword-offset in the last source.
1557 */
1558 case nir_intrinsic_load_ssbo_ir3:
1559 ctx->funcs->emit_intrinsic_load_ssbo(ctx, intr, dst);
1560 break;
1561 case nir_intrinsic_store_ssbo_ir3:
1562 if ((ctx->so->type == MESA_SHADER_FRAGMENT) &&
1563 !ctx->s->info.fs.early_fragment_tests)
1564 ctx->so->no_earlyz = true;
1565 ctx->funcs->emit_intrinsic_store_ssbo(ctx, intr);
1566 break;
1567 case nir_intrinsic_get_buffer_size:
1568 emit_intrinsic_ssbo_size(ctx, intr, dst);
1569 break;
1570 case nir_intrinsic_ssbo_atomic_add_ir3:
1571 case nir_intrinsic_ssbo_atomic_imin_ir3:
1572 case nir_intrinsic_ssbo_atomic_umin_ir3:
1573 case nir_intrinsic_ssbo_atomic_imax_ir3:
1574 case nir_intrinsic_ssbo_atomic_umax_ir3:
1575 case nir_intrinsic_ssbo_atomic_and_ir3:
1576 case nir_intrinsic_ssbo_atomic_or_ir3:
1577 case nir_intrinsic_ssbo_atomic_xor_ir3:
1578 case nir_intrinsic_ssbo_atomic_exchange_ir3:
1579 case nir_intrinsic_ssbo_atomic_comp_swap_ir3:
1580 if ((ctx->so->type == MESA_SHADER_FRAGMENT) &&
1581 !ctx->s->info.fs.early_fragment_tests)
1582 ctx->so->no_earlyz = true;
1583 dst[0] = ctx->funcs->emit_intrinsic_atomic_ssbo(ctx, intr);
1584 break;
1585 case nir_intrinsic_load_shared:
1586 emit_intrinsic_load_shared(ctx, intr, dst);
1587 break;
1588 case nir_intrinsic_store_shared:
1589 emit_intrinsic_store_shared(ctx, intr);
1590 break;
1591 case nir_intrinsic_shared_atomic_add:
1592 case nir_intrinsic_shared_atomic_imin:
1593 case nir_intrinsic_shared_atomic_umin:
1594 case nir_intrinsic_shared_atomic_imax:
1595 case nir_intrinsic_shared_atomic_umax:
1596 case nir_intrinsic_shared_atomic_and:
1597 case nir_intrinsic_shared_atomic_or:
1598 case nir_intrinsic_shared_atomic_xor:
1599 case nir_intrinsic_shared_atomic_exchange:
1600 case nir_intrinsic_shared_atomic_comp_swap:
1601 dst[0] = emit_intrinsic_atomic_shared(ctx, intr);
1602 break;
1603 case nir_intrinsic_image_deref_load:
1604 emit_intrinsic_load_image(ctx, intr, dst);
1605 break;
1606 case nir_intrinsic_image_deref_store:
1607 if ((ctx->so->type == MESA_SHADER_FRAGMENT) &&
1608 !ctx->s->info.fs.early_fragment_tests)
1609 ctx->so->no_earlyz = true;
1610 ctx->funcs->emit_intrinsic_store_image(ctx, intr);
1611 break;
1612 case nir_intrinsic_image_deref_size:
1613 emit_intrinsic_image_size(ctx, intr, dst);
1614 break;
1615 case nir_intrinsic_image_deref_atomic_add:
1616 case nir_intrinsic_image_deref_atomic_imin:
1617 case nir_intrinsic_image_deref_atomic_umin:
1618 case nir_intrinsic_image_deref_atomic_imax:
1619 case nir_intrinsic_image_deref_atomic_umax:
1620 case nir_intrinsic_image_deref_atomic_and:
1621 case nir_intrinsic_image_deref_atomic_or:
1622 case nir_intrinsic_image_deref_atomic_xor:
1623 case nir_intrinsic_image_deref_atomic_exchange:
1624 case nir_intrinsic_image_deref_atomic_comp_swap:
1625 if ((ctx->so->type == MESA_SHADER_FRAGMENT) &&
1626 !ctx->s->info.fs.early_fragment_tests)
1627 ctx->so->no_earlyz = true;
1628 dst[0] = ctx->funcs->emit_intrinsic_atomic_image(ctx, intr);
1629 break;
1630 case nir_intrinsic_barrier:
1631 case nir_intrinsic_memory_barrier:
1632 case nir_intrinsic_group_memory_barrier:
1633 case nir_intrinsic_memory_barrier_atomic_counter:
1634 case nir_intrinsic_memory_barrier_buffer:
1635 case nir_intrinsic_memory_barrier_image:
1636 case nir_intrinsic_memory_barrier_shared:
1637 emit_intrinsic_barrier(ctx, intr);
1638 /* note that blk ptr no longer valid, make that obvious: */
1639 b = NULL;
1640 break;
1641 case nir_intrinsic_store_output:
1642 idx = nir_intrinsic_base(intr);
1643 comp = nir_intrinsic_component(intr);
1644 compile_assert(ctx, nir_src_is_const(intr->src[1]));
1645 idx += nir_src_as_uint(intr->src[1]);
1646
1647 src = ir3_get_src(ctx, &intr->src[0]);
1648 for (int i = 0; i < intr->num_components; i++) {
1649 unsigned n = idx * 4 + i + comp;
1650 ctx->ir->outputs[n] = src[i];
1651 }
1652 break;
1653 case nir_intrinsic_load_base_vertex:
1654 case nir_intrinsic_load_first_vertex:
1655 if (!ctx->basevertex) {
1656 ctx->basevertex = create_driver_param(ctx, IR3_DP_VTXID_BASE);
1657 add_sysval_input(ctx, SYSTEM_VALUE_FIRST_VERTEX, ctx->basevertex);
1658 }
1659 dst[0] = ctx->basevertex;
1660 break;
1661 case nir_intrinsic_load_vertex_id_zero_base:
1662 case nir_intrinsic_load_vertex_id:
1663 if (!ctx->vertex_id) {
1664 gl_system_value sv = (intr->intrinsic == nir_intrinsic_load_vertex_id) ?
1665 SYSTEM_VALUE_VERTEX_ID : SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
1666 ctx->vertex_id = create_input(ctx, 0);
1667 add_sysval_input(ctx, sv, ctx->vertex_id);
1668 }
1669 dst[0] = ctx->vertex_id;
1670 break;
1671 case nir_intrinsic_load_instance_id:
1672 if (!ctx->instance_id) {
1673 ctx->instance_id = create_input(ctx, 0);
1674 add_sysval_input(ctx, SYSTEM_VALUE_INSTANCE_ID,
1675 ctx->instance_id);
1676 }
1677 dst[0] = ctx->instance_id;
1678 break;
1679 case nir_intrinsic_load_sample_id:
1680 ctx->so->per_samp = true;
1681 /* fall-thru */
1682 case nir_intrinsic_load_sample_id_no_per_sample:
1683 if (!ctx->samp_id) {
1684 ctx->samp_id = create_input(ctx, 0);
1685 ctx->samp_id->regs[0]->flags |= IR3_REG_HALF;
1686 add_sysval_input(ctx, SYSTEM_VALUE_SAMPLE_ID,
1687 ctx->samp_id);
1688 }
1689 dst[0] = ir3_COV(b, ctx->samp_id, TYPE_U16, TYPE_U32);
1690 break;
1691 case nir_intrinsic_load_sample_mask_in:
1692 if (!ctx->samp_mask_in) {
1693 ctx->samp_mask_in = create_input(ctx, 0);
1694 add_sysval_input(ctx, SYSTEM_VALUE_SAMPLE_MASK_IN,
1695 ctx->samp_mask_in);
1696 }
1697 dst[0] = ctx->samp_mask_in;
1698 break;
1699 case nir_intrinsic_load_user_clip_plane:
1700 idx = nir_intrinsic_ucp_id(intr);
1701 for (int i = 0; i < intr->num_components; i++) {
1702 unsigned n = idx * 4 + i;
1703 dst[i] = create_driver_param(ctx, IR3_DP_UCP0_X + n);
1704 }
1705 break;
1706 case nir_intrinsic_load_front_face:
1707 if (!ctx->frag_face) {
1708 ctx->so->frag_face = true;
1709 ctx->frag_face = create_input(ctx, 0);
1710 add_sysval_input(ctx, SYSTEM_VALUE_FRONT_FACE, ctx->frag_face);
1711 ctx->frag_face->regs[0]->flags |= IR3_REG_HALF;
1712 }
1713 /* for fragface, we get -1 for back and 0 for front. However this is
1714 * the inverse of what nir expects (where ~0 is true).
1715 */
1716 dst[0] = ir3_COV(b, ctx->frag_face, TYPE_S16, TYPE_S32);
1717 dst[0] = ir3_NOT_B(b, dst[0], 0);
1718 break;
1719 case nir_intrinsic_load_local_invocation_id:
1720 if (!ctx->local_invocation_id) {
1721 ctx->local_invocation_id = create_input_compmask(ctx, 0, 0x7);
1722 add_sysval_input_compmask(ctx, SYSTEM_VALUE_LOCAL_INVOCATION_ID,
1723 0x7, ctx->local_invocation_id);
1724 }
1725 ir3_split_dest(b, dst, ctx->local_invocation_id, 0, 3);
1726 break;
1727 case nir_intrinsic_load_work_group_id:
1728 if (!ctx->work_group_id) {
1729 ctx->work_group_id = create_input_compmask(ctx, 0, 0x7);
1730 add_sysval_input_compmask(ctx, SYSTEM_VALUE_WORK_GROUP_ID,
1731 0x7, ctx->work_group_id);
1732 ctx->work_group_id->regs[0]->flags |= IR3_REG_HIGH;
1733 }
1734 ir3_split_dest(b, dst, ctx->work_group_id, 0, 3);
1735 break;
1736 case nir_intrinsic_load_num_work_groups:
1737 for (int i = 0; i < intr->num_components; i++) {
1738 dst[i] = create_driver_param(ctx, IR3_DP_NUM_WORK_GROUPS_X + i);
1739 }
1740 break;
1741 case nir_intrinsic_load_local_group_size:
1742 for (int i = 0; i < intr->num_components; i++) {
1743 dst[i] = create_driver_param(ctx, IR3_DP_LOCAL_GROUP_SIZE_X + i);
1744 }
1745 break;
1746 case nir_intrinsic_discard_if:
1747 case nir_intrinsic_discard: {
1748 struct ir3_instruction *cond, *kill;
1749
1750 if (intr->intrinsic == nir_intrinsic_discard_if) {
1751 /* conditional discard: */
1752 src = ir3_get_src(ctx, &intr->src[0]);
1753 cond = ir3_b2n(b, src[0]);
1754 } else {
1755 /* unconditional discard: */
1756 cond = create_immed(b, 1);
1757 }
1758
1759 /* NOTE: only cmps.*.* can write p0.x: */
1760 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1761 cond->cat2.condition = IR3_COND_NE;
1762
1763 /* condition always goes in predicate register: */
1764 cond->regs[0]->num = regid(REG_P0, 0);
1765
1766 kill = ir3_KILL(b, cond, 0);
1767 array_insert(ctx->ir, ctx->ir->predicates, kill);
1768
1769 array_insert(b, b->keeps, kill);
1770 ctx->so->no_earlyz = true;
1771
1772 break;
1773 }
1774
1775 case nir_intrinsic_cond_end_ir3: {
1776 struct ir3_instruction *cond, *kill;
1777
1778 src = ir3_get_src(ctx, &intr->src[0]);
1779 cond = ir3_b2n(b, src[0]);
1780
1781 /* NOTE: only cmps.*.* can write p0.x: */
1782 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1783 cond->cat2.condition = IR3_COND_NE;
1784
1785 /* condition always goes in predicate register: */
1786 cond->regs[0]->num = regid(REG_P0, 0);
1787
1788 kill = ir3_CONDEND(b, cond, 0);
1789
1790 kill->barrier_class = IR3_BARRIER_EVERYTHING;
1791 kill->barrier_conflict = IR3_BARRIER_EVERYTHING;
1792
1793 array_insert(ctx->ir, ctx->ir->predicates, kill);
1794 array_insert(b, b->keeps, kill);
1795 break;
1796 }
1797
1798 case nir_intrinsic_load_shared_ir3:
1799 emit_intrinsic_load_shared_ir3(ctx, intr, dst);
1800 break;
1801 case nir_intrinsic_store_shared_ir3:
1802 emit_intrinsic_store_shared_ir3(ctx, intr);
1803 break;
1804 default:
1805 ir3_context_error(ctx, "Unhandled intrinsic type: %s\n",
1806 nir_intrinsic_infos[intr->intrinsic].name);
1807 break;
1808 }
1809
1810 if (info->has_dest)
1811 ir3_put_dst(ctx, &intr->dest);
1812 }
1813
1814 static void
1815 emit_load_const(struct ir3_context *ctx, nir_load_const_instr *instr)
1816 {
1817 struct ir3_instruction **dst = ir3_get_dst_ssa(ctx, &instr->def,
1818 instr->def.num_components);
1819
1820 if (instr->def.bit_size < 32) {
1821 for (int i = 0; i < instr->def.num_components; i++)
1822 dst[i] = create_immed_typed(ctx->block,
1823 instr->value[i].u16,
1824 TYPE_U16);
1825 } else {
1826 for (int i = 0; i < instr->def.num_components; i++)
1827 dst[i] = create_immed_typed(ctx->block,
1828 instr->value[i].u32,
1829 TYPE_U32);
1830 }
1831
1832 }
1833
1834 static void
1835 emit_undef(struct ir3_context *ctx, nir_ssa_undef_instr *undef)
1836 {
1837 struct ir3_instruction **dst = ir3_get_dst_ssa(ctx, &undef->def,
1838 undef->def.num_components);
1839 type_t type = (undef->def.bit_size < 32) ? TYPE_U16 : TYPE_U32;
1840
1841 /* backend doesn't want undefined instructions, so just plug
1842 * in 0.0..
1843 */
1844 for (int i = 0; i < undef->def.num_components; i++)
1845 dst[i] = create_immed_typed(ctx->block, fui(0.0), type);
1846 }
1847
1848 /*
1849 * texture fetch/sample instructions:
1850 */
1851
1852 static void
1853 tex_info(nir_tex_instr *tex, unsigned *flagsp, unsigned *coordsp)
1854 {
1855 unsigned coords, flags = 0;
1856
1857 /* note: would use tex->coord_components.. except txs.. also,
1858 * since array index goes after shadow ref, we don't want to
1859 * count it:
1860 */
1861 switch (tex->sampler_dim) {
1862 case GLSL_SAMPLER_DIM_1D:
1863 case GLSL_SAMPLER_DIM_BUF:
1864 coords = 1;
1865 break;
1866 case GLSL_SAMPLER_DIM_2D:
1867 case GLSL_SAMPLER_DIM_RECT:
1868 case GLSL_SAMPLER_DIM_EXTERNAL:
1869 case GLSL_SAMPLER_DIM_MS:
1870 coords = 2;
1871 break;
1872 case GLSL_SAMPLER_DIM_3D:
1873 case GLSL_SAMPLER_DIM_CUBE:
1874 coords = 3;
1875 flags |= IR3_INSTR_3D;
1876 break;
1877 default:
1878 unreachable("bad sampler_dim");
1879 }
1880
1881 if (tex->is_shadow && tex->op != nir_texop_lod)
1882 flags |= IR3_INSTR_S;
1883
1884 if (tex->is_array && tex->op != nir_texop_lod)
1885 flags |= IR3_INSTR_A;
1886
1887 *flagsp = flags;
1888 *coordsp = coords;
1889 }
1890
1891 /* Gets the sampler/texture idx as a hvec2. Which could either be dynamic
1892 * or immediate (in which case it will get lowered later to a non .s2en
1893 * version of the tex instruction which encode tex/samp as immediates:
1894 */
1895 static struct ir3_instruction *
1896 get_tex_samp_tex_src(struct ir3_context *ctx, nir_tex_instr *tex)
1897 {
1898 int texture_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_offset);
1899 int sampler_idx = nir_tex_instr_src_index(tex, nir_tex_src_sampler_offset);
1900 struct ir3_instruction *texture, *sampler;
1901
1902 if (texture_idx >= 0) {
1903 texture = ir3_get_src(ctx, &tex->src[texture_idx].src)[0];
1904 texture = ir3_COV(ctx->block, texture, TYPE_U32, TYPE_U16);
1905 } else {
1906 /* TODO what to do for dynamic case? I guess we only need the
1907 * max index for astc srgb workaround so maybe not a problem
1908 * to worry about if we don't enable indirect samplers for
1909 * a4xx?
1910 */
1911 ctx->max_texture_index = MAX2(ctx->max_texture_index, tex->texture_index);
1912 texture = create_immed_typed(ctx->block, tex->texture_index, TYPE_U16);
1913 }
1914
1915 if (sampler_idx >= 0) {
1916 sampler = ir3_get_src(ctx, &tex->src[sampler_idx].src)[0];
1917 sampler = ir3_COV(ctx->block, sampler, TYPE_U32, TYPE_U16);
1918 } else {
1919 sampler = create_immed_typed(ctx->block, tex->sampler_index, TYPE_U16);
1920 }
1921
1922 return ir3_create_collect(ctx, (struct ir3_instruction*[]){
1923 sampler,
1924 texture,
1925 }, 2);
1926 }
1927
1928 static void
1929 emit_tex(struct ir3_context *ctx, nir_tex_instr *tex)
1930 {
1931 struct ir3_block *b = ctx->block;
1932 struct ir3_instruction **dst, *sam, *src0[12], *src1[4];
1933 struct ir3_instruction * const *coord, * const *off, * const *ddx, * const *ddy;
1934 struct ir3_instruction *lod, *compare, *proj, *sample_index;
1935 bool has_bias = false, has_lod = false, has_proj = false, has_off = false;
1936 unsigned i, coords, flags, ncomp;
1937 unsigned nsrc0 = 0, nsrc1 = 0;
1938 type_t type;
1939 opc_t opc = 0;
1940
1941 ncomp = nir_dest_num_components(tex->dest);
1942
1943 coord = off = ddx = ddy = NULL;
1944 lod = proj = compare = sample_index = NULL;
1945
1946 dst = ir3_get_dst(ctx, &tex->dest, ncomp);
1947
1948 for (unsigned i = 0; i < tex->num_srcs; i++) {
1949 switch (tex->src[i].src_type) {
1950 case nir_tex_src_coord:
1951 coord = ir3_get_src(ctx, &tex->src[i].src);
1952 break;
1953 case nir_tex_src_bias:
1954 lod = ir3_get_src(ctx, &tex->src[i].src)[0];
1955 has_bias = true;
1956 break;
1957 case nir_tex_src_lod:
1958 lod = ir3_get_src(ctx, &tex->src[i].src)[0];
1959 has_lod = true;
1960 break;
1961 case nir_tex_src_comparator: /* shadow comparator */
1962 compare = ir3_get_src(ctx, &tex->src[i].src)[0];
1963 break;
1964 case nir_tex_src_projector:
1965 proj = ir3_get_src(ctx, &tex->src[i].src)[0];
1966 has_proj = true;
1967 break;
1968 case nir_tex_src_offset:
1969 off = ir3_get_src(ctx, &tex->src[i].src);
1970 has_off = true;
1971 break;
1972 case nir_tex_src_ddx:
1973 ddx = ir3_get_src(ctx, &tex->src[i].src);
1974 break;
1975 case nir_tex_src_ddy:
1976 ddy = ir3_get_src(ctx, &tex->src[i].src);
1977 break;
1978 case nir_tex_src_ms_index:
1979 sample_index = ir3_get_src(ctx, &tex->src[i].src)[0];
1980 break;
1981 case nir_tex_src_texture_offset:
1982 case nir_tex_src_sampler_offset:
1983 /* handled in get_tex_samp_src() */
1984 break;
1985 default:
1986 ir3_context_error(ctx, "Unhandled NIR tex src type: %d\n",
1987 tex->src[i].src_type);
1988 return;
1989 }
1990 }
1991
1992 switch (tex->op) {
1993 case nir_texop_tex_prefetch:
1994 compile_assert(ctx, !has_bias);
1995 compile_assert(ctx, !has_lod);
1996 compile_assert(ctx, !compare);
1997 compile_assert(ctx, !has_proj);
1998 compile_assert(ctx, !has_off);
1999 compile_assert(ctx, !ddx);
2000 compile_assert(ctx, !ddy);
2001 compile_assert(ctx, !sample_index);
2002 compile_assert(ctx, nir_tex_instr_src_index(tex, nir_tex_src_texture_offset) < 0);
2003 compile_assert(ctx, nir_tex_instr_src_index(tex, nir_tex_src_sampler_offset) < 0);
2004
2005 if (ctx->so->num_sampler_prefetch < IR3_MAX_SAMPLER_PREFETCH) {
2006 opc = OPC_META_TEX_PREFETCH;
2007 ctx->so->num_sampler_prefetch++;
2008 break;
2009 }
2010 /* fallthru */
2011 case nir_texop_tex: opc = has_lod ? OPC_SAML : OPC_SAM; break;
2012 case nir_texop_txb: opc = OPC_SAMB; break;
2013 case nir_texop_txl: opc = OPC_SAML; break;
2014 case nir_texop_txd: opc = OPC_SAMGQ; break;
2015 case nir_texop_txf: opc = OPC_ISAML; break;
2016 case nir_texop_lod: opc = OPC_GETLOD; break;
2017 case nir_texop_tg4:
2018 /* NOTE: a4xx might need to emulate gather w/ txf (this is
2019 * what blob does, seems gather is broken?), and a3xx did
2020 * not support it (but probably could also emulate).
2021 */
2022 switch (tex->component) {
2023 case 0: opc = OPC_GATHER4R; break;
2024 case 1: opc = OPC_GATHER4G; break;
2025 case 2: opc = OPC_GATHER4B; break;
2026 case 3: opc = OPC_GATHER4A; break;
2027 }
2028 break;
2029 case nir_texop_txf_ms_fb:
2030 case nir_texop_txf_ms: opc = OPC_ISAMM; break;
2031 default:
2032 ir3_context_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
2033 return;
2034 }
2035
2036 tex_info(tex, &flags, &coords);
2037
2038 /*
2039 * lay out the first argument in the proper order:
2040 * - actual coordinates first
2041 * - shadow reference
2042 * - array index
2043 * - projection w
2044 * - starting at offset 4, dpdx.xy, dpdy.xy
2045 *
2046 * bias/lod go into the second arg
2047 */
2048
2049 /* insert tex coords: */
2050 for (i = 0; i < coords; i++)
2051 src0[i] = coord[i];
2052
2053 nsrc0 = i;
2054
2055 /* scale up integer coords for TXF based on the LOD */
2056 if (ctx->compiler->unminify_coords && (opc == OPC_ISAML)) {
2057 assert(has_lod);
2058 for (i = 0; i < coords; i++)
2059 src0[i] = ir3_SHL_B(b, src0[i], 0, lod, 0);
2060 }
2061
2062 if (coords == 1) {
2063 /* hw doesn't do 1d, so we treat it as 2d with
2064 * height of 1, and patch up the y coord.
2065 */
2066 if (is_isam(opc)) {
2067 src0[nsrc0++] = create_immed(b, 0);
2068 } else {
2069 src0[nsrc0++] = create_immed(b, fui(0.5));
2070 }
2071 }
2072
2073 if (tex->is_shadow && tex->op != nir_texop_lod)
2074 src0[nsrc0++] = compare;
2075
2076 if (tex->is_array && tex->op != nir_texop_lod) {
2077 struct ir3_instruction *idx = coord[coords];
2078
2079 /* the array coord for cube arrays needs 0.5 added to it */
2080 if (ctx->compiler->array_index_add_half && !is_isam(opc))
2081 idx = ir3_ADD_F(b, idx, 0, create_immed(b, fui(0.5)), 0);
2082
2083 src0[nsrc0++] = idx;
2084 }
2085
2086 if (has_proj) {
2087 src0[nsrc0++] = proj;
2088 flags |= IR3_INSTR_P;
2089 }
2090
2091 /* pad to 4, then ddx/ddy: */
2092 if (tex->op == nir_texop_txd) {
2093 while (nsrc0 < 4)
2094 src0[nsrc0++] = create_immed(b, fui(0.0));
2095 for (i = 0; i < coords; i++)
2096 src0[nsrc0++] = ddx[i];
2097 if (coords < 2)
2098 src0[nsrc0++] = create_immed(b, fui(0.0));
2099 for (i = 0; i < coords; i++)
2100 src0[nsrc0++] = ddy[i];
2101 if (coords < 2)
2102 src0[nsrc0++] = create_immed(b, fui(0.0));
2103 }
2104
2105 /* NOTE a3xx (and possibly a4xx?) might be different, using isaml
2106 * with scaled x coord according to requested sample:
2107 */
2108 if (opc == OPC_ISAMM) {
2109 if (ctx->compiler->txf_ms_with_isaml) {
2110 /* the samples are laid out in x dimension as
2111 * 0 1 2 3
2112 * x_ms = (x << ms) + sample_index;
2113 */
2114 struct ir3_instruction *ms;
2115 ms = create_immed(b, (ctx->samples >> (2 * tex->texture_index)) & 3);
2116
2117 src0[0] = ir3_SHL_B(b, src0[0], 0, ms, 0);
2118 src0[0] = ir3_ADD_U(b, src0[0], 0, sample_index, 0);
2119
2120 opc = OPC_ISAML;
2121 } else {
2122 src0[nsrc0++] = sample_index;
2123 }
2124 }
2125
2126 /*
2127 * second argument (if applicable):
2128 * - offsets
2129 * - lod
2130 * - bias
2131 */
2132 if (has_off | has_lod | has_bias) {
2133 if (has_off) {
2134 unsigned off_coords = coords;
2135 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
2136 off_coords--;
2137 for (i = 0; i < off_coords; i++)
2138 src1[nsrc1++] = off[i];
2139 if (off_coords < 2)
2140 src1[nsrc1++] = create_immed(b, fui(0.0));
2141 flags |= IR3_INSTR_O;
2142 }
2143
2144 if (has_lod | has_bias)
2145 src1[nsrc1++] = lod;
2146 }
2147
2148 switch (tex->dest_type) {
2149 case nir_type_invalid:
2150 case nir_type_float:
2151 type = TYPE_F32;
2152 break;
2153 case nir_type_int:
2154 type = TYPE_S32;
2155 break;
2156 case nir_type_uint:
2157 case nir_type_bool:
2158 type = TYPE_U32;
2159 break;
2160 default:
2161 unreachable("bad dest_type");
2162 }
2163
2164 if (opc == OPC_GETLOD)
2165 type = TYPE_S32;
2166
2167 struct ir3_instruction *samp_tex;
2168
2169 if (tex->op == nir_texop_txf_ms_fb) {
2170 /* only expect a single txf_ms_fb per shader: */
2171 compile_assert(ctx, !ctx->so->fb_read);
2172 compile_assert(ctx, ctx->so->type == MESA_SHADER_FRAGMENT);
2173
2174 ctx->so->fb_read = true;
2175 samp_tex = ir3_create_collect(ctx, (struct ir3_instruction*[]){
2176 create_immed_typed(ctx->block, ctx->so->num_samp, TYPE_U16),
2177 create_immed_typed(ctx->block, ctx->so->num_samp, TYPE_U16),
2178 }, 2);
2179
2180 ctx->so->num_samp++;
2181 } else {
2182 samp_tex = get_tex_samp_tex_src(ctx, tex);
2183 }
2184
2185 struct ir3_instruction *col0 = ir3_create_collect(ctx, src0, nsrc0);
2186 struct ir3_instruction *col1 = ir3_create_collect(ctx, src1, nsrc1);
2187
2188 if (opc == OPC_META_TEX_PREFETCH) {
2189 int idx = nir_tex_instr_src_index(tex, nir_tex_src_coord);
2190
2191 compile_assert(ctx, tex->src[idx].src.is_ssa);
2192
2193 sam = ir3_META_TEX_PREFETCH(b);
2194 ir3_reg_create(sam, 0, 0)->wrmask = MASK(ncomp); /* dst */
2195 sam->prefetch.input_offset =
2196 ir3_nir_coord_offset(tex->src[idx].src.ssa);
2197 sam->prefetch.tex = tex->texture_index;
2198 sam->prefetch.samp = tex->sampler_index;
2199 } else {
2200 sam = ir3_SAM(b, opc, type, MASK(ncomp), flags,
2201 samp_tex, col0, col1);
2202 }
2203
2204 if ((ctx->astc_srgb & (1 << tex->texture_index)) && !nir_tex_instr_is_query(tex)) {
2205 assert(opc != OPC_META_TEX_PREFETCH);
2206
2207 /* only need first 3 components: */
2208 sam->regs[0]->wrmask = 0x7;
2209 ir3_split_dest(b, dst, sam, 0, 3);
2210
2211 /* we need to sample the alpha separately with a non-ASTC
2212 * texture state:
2213 */
2214 sam = ir3_SAM(b, opc, type, 0b1000, flags,
2215 samp_tex, col0, col1);
2216
2217 array_insert(ctx->ir, ctx->ir->astc_srgb, sam);
2218
2219 /* fixup .w component: */
2220 ir3_split_dest(b, &dst[3], sam, 3, 1);
2221 } else {
2222 /* normal (non-workaround) case: */
2223 ir3_split_dest(b, dst, sam, 0, ncomp);
2224 }
2225
2226 /* GETLOD returns results in 4.8 fixed point */
2227 if (opc == OPC_GETLOD) {
2228 struct ir3_instruction *factor = create_immed(b, fui(1.0 / 256));
2229
2230 compile_assert(ctx, tex->dest_type == nir_type_float);
2231 for (i = 0; i < 2; i++) {
2232 dst[i] = ir3_MUL_F(b, ir3_COV(b, dst[i], TYPE_S32, TYPE_F32), 0,
2233 factor, 0);
2234 }
2235 }
2236
2237 ir3_put_dst(ctx, &tex->dest);
2238 }
2239
2240 static void
2241 emit_tex_info(struct ir3_context *ctx, nir_tex_instr *tex, unsigned idx)
2242 {
2243 struct ir3_block *b = ctx->block;
2244 struct ir3_instruction **dst, *sam;
2245
2246 dst = ir3_get_dst(ctx, &tex->dest, 1);
2247
2248 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, 1 << idx, 0,
2249 get_tex_samp_tex_src(ctx, tex), NULL, NULL);
2250
2251 /* even though there is only one component, since it ends
2252 * up in .y/.z/.w rather than .x, we need a split_dest()
2253 */
2254 if (idx)
2255 ir3_split_dest(b, dst, sam, 0, idx + 1);
2256
2257 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
2258 * the value in TEX_CONST_0 is zero-based.
2259 */
2260 if (ctx->compiler->levels_add_one)
2261 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
2262
2263 ir3_put_dst(ctx, &tex->dest);
2264 }
2265
2266 static void
2267 emit_tex_txs(struct ir3_context *ctx, nir_tex_instr *tex)
2268 {
2269 struct ir3_block *b = ctx->block;
2270 struct ir3_instruction **dst, *sam;
2271 struct ir3_instruction *lod;
2272 unsigned flags, coords;
2273
2274 tex_info(tex, &flags, &coords);
2275
2276 /* Actually we want the number of dimensions, not coordinates. This
2277 * distinction only matters for cubes.
2278 */
2279 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
2280 coords = 2;
2281
2282 dst = ir3_get_dst(ctx, &tex->dest, 4);
2283
2284 compile_assert(ctx, tex->num_srcs == 1);
2285 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
2286
2287 lod = ir3_get_src(ctx, &tex->src[0].src)[0];
2288
2289 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, 0b1111, flags,
2290 get_tex_samp_tex_src(ctx, tex), lod, NULL);
2291
2292 ir3_split_dest(b, dst, sam, 0, 4);
2293
2294 /* Array size actually ends up in .w rather than .z. This doesn't
2295 * matter for miplevel 0, but for higher mips the value in z is
2296 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
2297 * returned, which means that we have to add 1 to it for arrays.
2298 */
2299 if (tex->is_array) {
2300 if (ctx->compiler->levels_add_one) {
2301 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
2302 } else {
2303 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
2304 }
2305 }
2306
2307 ir3_put_dst(ctx, &tex->dest);
2308 }
2309
2310 static void
2311 emit_jump(struct ir3_context *ctx, nir_jump_instr *jump)
2312 {
2313 switch (jump->type) {
2314 case nir_jump_break:
2315 case nir_jump_continue:
2316 case nir_jump_return:
2317 /* I *think* we can simply just ignore this, and use the
2318 * successor block link to figure out where we need to
2319 * jump to for break/continue
2320 */
2321 break;
2322 default:
2323 ir3_context_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
2324 break;
2325 }
2326 }
2327
2328 static void
2329 emit_instr(struct ir3_context *ctx, nir_instr *instr)
2330 {
2331 switch (instr->type) {
2332 case nir_instr_type_alu:
2333 emit_alu(ctx, nir_instr_as_alu(instr));
2334 break;
2335 case nir_instr_type_deref:
2336 /* ignored, handled as part of the intrinsic they are src to */
2337 break;
2338 case nir_instr_type_intrinsic:
2339 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
2340 break;
2341 case nir_instr_type_load_const:
2342 emit_load_const(ctx, nir_instr_as_load_const(instr));
2343 break;
2344 case nir_instr_type_ssa_undef:
2345 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
2346 break;
2347 case nir_instr_type_tex: {
2348 nir_tex_instr *tex = nir_instr_as_tex(instr);
2349 /* couple tex instructions get special-cased:
2350 */
2351 switch (tex->op) {
2352 case nir_texop_txs:
2353 emit_tex_txs(ctx, tex);
2354 break;
2355 case nir_texop_query_levels:
2356 emit_tex_info(ctx, tex, 2);
2357 break;
2358 case nir_texop_texture_samples:
2359 emit_tex_info(ctx, tex, 3);
2360 break;
2361 default:
2362 emit_tex(ctx, tex);
2363 break;
2364 }
2365 break;
2366 }
2367 case nir_instr_type_jump:
2368 emit_jump(ctx, nir_instr_as_jump(instr));
2369 break;
2370 case nir_instr_type_phi:
2371 /* we have converted phi webs to regs in NIR by now */
2372 ir3_context_error(ctx, "Unexpected NIR instruction type: %d\n", instr->type);
2373 break;
2374 case nir_instr_type_call:
2375 case nir_instr_type_parallel_copy:
2376 ir3_context_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
2377 break;
2378 }
2379 }
2380
2381 static struct ir3_block *
2382 get_block(struct ir3_context *ctx, const nir_block *nblock)
2383 {
2384 struct ir3_block *block;
2385 struct hash_entry *hentry;
2386
2387 hentry = _mesa_hash_table_search(ctx->block_ht, nblock);
2388 if (hentry)
2389 return hentry->data;
2390
2391 block = ir3_block_create(ctx->ir);
2392 block->nblock = nblock;
2393 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
2394
2395 block->predecessors = _mesa_pointer_set_create(block);
2396 set_foreach(nblock->predecessors, sentry) {
2397 _mesa_set_add(block->predecessors, get_block(ctx, sentry->key));
2398 }
2399
2400 return block;
2401 }
2402
2403 static void
2404 emit_block(struct ir3_context *ctx, nir_block *nblock)
2405 {
2406 struct ir3_block *block = get_block(ctx, nblock);
2407
2408 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
2409 if (nblock->successors[i]) {
2410 block->successors[i] =
2411 get_block(ctx, nblock->successors[i]);
2412 }
2413 }
2414
2415 ctx->block = block;
2416 list_addtail(&block->node, &ctx->ir->block_list);
2417
2418 /* re-emit addr register in each block if needed: */
2419 for (int i = 0; i < ARRAY_SIZE(ctx->addr_ht); i++) {
2420 _mesa_hash_table_destroy(ctx->addr_ht[i], NULL);
2421 ctx->addr_ht[i] = NULL;
2422 }
2423
2424 nir_foreach_instr(instr, nblock) {
2425 ctx->cur_instr = instr;
2426 emit_instr(ctx, instr);
2427 ctx->cur_instr = NULL;
2428 if (ctx->error)
2429 return;
2430 }
2431 }
2432
2433 static void emit_cf_list(struct ir3_context *ctx, struct exec_list *list);
2434
2435 static void
2436 emit_if(struct ir3_context *ctx, nir_if *nif)
2437 {
2438 struct ir3_instruction *condition = ir3_get_src(ctx, &nif->condition)[0];
2439
2440 ctx->block->condition =
2441 ir3_get_predicate(ctx, ir3_b2n(condition->block, condition));
2442
2443 emit_cf_list(ctx, &nif->then_list);
2444 emit_cf_list(ctx, &nif->else_list);
2445 }
2446
2447 static void
2448 emit_loop(struct ir3_context *ctx, nir_loop *nloop)
2449 {
2450 emit_cf_list(ctx, &nloop->body);
2451 ctx->so->loops++;
2452 }
2453
2454 static void
2455 stack_push(struct ir3_context *ctx)
2456 {
2457 ctx->stack++;
2458 ctx->max_stack = MAX2(ctx->max_stack, ctx->stack);
2459 }
2460
2461 static void
2462 stack_pop(struct ir3_context *ctx)
2463 {
2464 compile_assert(ctx, ctx->stack > 0);
2465 ctx->stack--;
2466 }
2467
2468 static void
2469 emit_cf_list(struct ir3_context *ctx, struct exec_list *list)
2470 {
2471 foreach_list_typed(nir_cf_node, node, node, list) {
2472 switch (node->type) {
2473 case nir_cf_node_block:
2474 emit_block(ctx, nir_cf_node_as_block(node));
2475 break;
2476 case nir_cf_node_if:
2477 stack_push(ctx);
2478 emit_if(ctx, nir_cf_node_as_if(node));
2479 stack_pop(ctx);
2480 break;
2481 case nir_cf_node_loop:
2482 stack_push(ctx);
2483 emit_loop(ctx, nir_cf_node_as_loop(node));
2484 stack_pop(ctx);
2485 break;
2486 case nir_cf_node_function:
2487 ir3_context_error(ctx, "TODO\n");
2488 break;
2489 }
2490 }
2491 }
2492
2493 /* emit stream-out code. At this point, the current block is the original
2494 * (nir) end block, and nir ensures that all flow control paths terminate
2495 * into the end block. We re-purpose the original end block to generate
2496 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
2497 * block holding stream-out write instructions, followed by the new end
2498 * block:
2499 *
2500 * blockOrigEnd {
2501 * p0.x = (vtxcnt < maxvtxcnt)
2502 * // succs: blockStreamOut, blockNewEnd
2503 * }
2504 * blockStreamOut {
2505 * ... stream-out instructions ...
2506 * // succs: blockNewEnd
2507 * }
2508 * blockNewEnd {
2509 * }
2510 */
2511 static void
2512 emit_stream_out(struct ir3_context *ctx)
2513 {
2514 struct ir3 *ir = ctx->ir;
2515 struct ir3_stream_output_info *strmout =
2516 &ctx->so->shader->stream_output;
2517 struct ir3_block *orig_end_block, *stream_out_block, *new_end_block;
2518 struct ir3_instruction *vtxcnt, *maxvtxcnt, *cond;
2519 struct ir3_instruction *bases[IR3_MAX_SO_BUFFERS];
2520
2521 /* create vtxcnt input in input block at top of shader,
2522 * so that it is seen as live over the entire duration
2523 * of the shader:
2524 */
2525 vtxcnt = create_input(ctx, 0);
2526 add_sysval_input(ctx, SYSTEM_VALUE_VERTEX_CNT, vtxcnt);
2527
2528 maxvtxcnt = create_driver_param(ctx, IR3_DP_VTXCNT_MAX);
2529
2530 /* at this point, we are at the original 'end' block,
2531 * re-purpose this block to stream-out condition, then
2532 * append stream-out block and new-end block
2533 */
2534 orig_end_block = ctx->block;
2535
2536 // TODO these blocks need to update predecessors..
2537 // maybe w/ store_global intrinsic, we could do this
2538 // stuff in nir->nir pass
2539
2540 stream_out_block = ir3_block_create(ir);
2541 list_addtail(&stream_out_block->node, &ir->block_list);
2542
2543 new_end_block = ir3_block_create(ir);
2544 list_addtail(&new_end_block->node, &ir->block_list);
2545
2546 orig_end_block->successors[0] = stream_out_block;
2547 orig_end_block->successors[1] = new_end_block;
2548 stream_out_block->successors[0] = new_end_block;
2549
2550 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
2551 cond = ir3_CMPS_S(ctx->block, vtxcnt, 0, maxvtxcnt, 0);
2552 cond->regs[0]->num = regid(REG_P0, 0);
2553 cond->cat2.condition = IR3_COND_LT;
2554
2555 /* condition goes on previous block to the conditional,
2556 * since it is used to pick which of the two successor
2557 * paths to take:
2558 */
2559 orig_end_block->condition = cond;
2560
2561 /* switch to stream_out_block to generate the stream-out
2562 * instructions:
2563 */
2564 ctx->block = stream_out_block;
2565
2566 /* Calculate base addresses based on vtxcnt. Instructions
2567 * generated for bases not used in following loop will be
2568 * stripped out in the backend.
2569 */
2570 for (unsigned i = 0; i < IR3_MAX_SO_BUFFERS; i++) {
2571 struct ir3_const_state *const_state = &ctx->so->shader->const_state;
2572 unsigned stride = strmout->stride[i];
2573 struct ir3_instruction *base, *off;
2574
2575 base = create_uniform(ctx->block, regid(const_state->offsets.tfbo, i));
2576
2577 /* 24-bit should be enough: */
2578 off = ir3_MUL_U24(ctx->block, vtxcnt, 0,
2579 create_immed(ctx->block, stride * 4), 0);
2580
2581 bases[i] = ir3_ADD_S(ctx->block, off, 0, base, 0);
2582 }
2583
2584 /* Generate the per-output store instructions: */
2585 for (unsigned i = 0; i < strmout->num_outputs; i++) {
2586 for (unsigned j = 0; j < strmout->output[i].num_components; j++) {
2587 unsigned c = j + strmout->output[i].start_component;
2588 struct ir3_instruction *base, *out, *stg;
2589
2590 base = bases[strmout->output[i].output_buffer];
2591 out = ctx->ir->outputs[regid(strmout->output[i].register_index, c)];
2592
2593 stg = ir3_STG(ctx->block, base, 0, out, 0,
2594 create_immed(ctx->block, 1), 0);
2595 stg->cat6.type = TYPE_U32;
2596 stg->cat6.dst_offset = (strmout->output[i].dst_offset + j) * 4;
2597
2598 array_insert(ctx->block, ctx->block->keeps, stg);
2599 }
2600 }
2601
2602 /* and finally switch to the new_end_block: */
2603 ctx->block = new_end_block;
2604 }
2605
2606 static void
2607 emit_function(struct ir3_context *ctx, nir_function_impl *impl)
2608 {
2609 nir_metadata_require(impl, nir_metadata_block_index);
2610
2611 compile_assert(ctx, ctx->stack == 0);
2612
2613 emit_cf_list(ctx, &impl->body);
2614 emit_block(ctx, impl->end_block);
2615
2616 compile_assert(ctx, ctx->stack == 0);
2617
2618 /* at this point, we should have a single empty block,
2619 * into which we emit the 'end' instruction.
2620 */
2621 compile_assert(ctx, list_is_empty(&ctx->block->instr_list));
2622
2623 /* If stream-out (aka transform-feedback) enabled, emit the
2624 * stream-out instructions, followed by a new empty block (into
2625 * which the 'end' instruction lands).
2626 *
2627 * NOTE: it is done in this order, rather than inserting before
2628 * we emit end_block, because NIR guarantees that all blocks
2629 * flow into end_block, and that end_block has no successors.
2630 * So by re-purposing end_block as the first block of stream-
2631 * out, we guarantee that all exit paths flow into the stream-
2632 * out instructions.
2633 */
2634 if ((ctx->compiler->gpu_id < 500) &&
2635 (ctx->so->shader->stream_output.num_outputs > 0) &&
2636 !ctx->so->binning_pass) {
2637 debug_assert(ctx->so->type == MESA_SHADER_VERTEX);
2638 emit_stream_out(ctx);
2639 }
2640
2641 /* Vertex shaders in a tessellation or geometry pipeline treat END as a
2642 * NOP and has an epilogue that writes the VS outputs to local storage, to
2643 * be read by the HS. Then it resets execution mask (chmask) and chains
2644 * to the next shader (chsh).
2645 */
2646 if ((ctx->so->type == MESA_SHADER_VERTEX &&
2647 (ctx->so->key.has_gs || ctx->so->key.tessellation)) ||
2648 (ctx->so->type == MESA_SHADER_TESS_EVAL && ctx->so->key.has_gs)) {
2649 struct ir3_instruction *chmask =
2650 ir3_CHMASK(ctx->block);
2651 chmask->barrier_class = IR3_BARRIER_EVERYTHING;
2652 chmask->barrier_conflict = IR3_BARRIER_EVERYTHING;
2653
2654 struct ir3_instruction *chsh =
2655 ir3_CHSH(ctx->block);
2656 chsh->barrier_class = IR3_BARRIER_EVERYTHING;
2657 chsh->barrier_conflict = IR3_BARRIER_EVERYTHING;
2658 } else {
2659 ir3_END(ctx->block);
2660 }
2661 }
2662
2663 static void
2664 setup_input(struct ir3_context *ctx, nir_variable *in)
2665 {
2666 struct ir3_shader_variant *so = ctx->so;
2667 unsigned ncomp = glsl_get_components(in->type);
2668 unsigned n = in->data.driver_location;
2669 unsigned frac = in->data.location_frac;
2670 unsigned slot = in->data.location;
2671
2672 /* Inputs are loaded using ldlw or ldg for these stages. */
2673 if (ctx->so->type == MESA_SHADER_TESS_CTRL ||
2674 ctx->so->type == MESA_SHADER_TESS_EVAL ||
2675 ctx->so->type == MESA_SHADER_GEOMETRY)
2676 return;
2677
2678 /* skip unread inputs, we could end up with (for example), unsplit
2679 * matrix/etc inputs in the case they are not read, so just silently
2680 * skip these.
2681 */
2682 if (ncomp > 4)
2683 return;
2684
2685 so->inputs[n].slot = slot;
2686 so->inputs[n].compmask = (1 << (ncomp + frac)) - 1;
2687 so->inputs_count = MAX2(so->inputs_count, n + 1);
2688 so->inputs[n].interpolate = in->data.interpolation;
2689
2690 if (ctx->so->type == MESA_SHADER_FRAGMENT) {
2691
2692 /* if any varyings have 'sample' qualifer, that triggers us
2693 * to run in per-sample mode:
2694 */
2695 so->per_samp |= in->data.sample;
2696
2697 for (int i = 0; i < ncomp; i++) {
2698 struct ir3_instruction *instr = NULL;
2699 unsigned idx = (n * 4) + i + frac;
2700
2701 if (slot == VARYING_SLOT_POS) {
2702 ir3_context_error(ctx, "fragcoord should be a sysval!\n");
2703 } else if (slot == VARYING_SLOT_PNTC) {
2704 /* see for example st_nir_fixup_varying_slots().. this is
2705 * maybe a bit mesa/st specific. But we need things to line
2706 * up for this in fdN_program:
2707 * unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
2708 * if (emit->sprite_coord_enable & texmask) {
2709 * ...
2710 * }
2711 */
2712 so->inputs[n].slot = VARYING_SLOT_VAR8;
2713 so->inputs[n].bary = true;
2714 instr = create_frag_input(ctx, false, idx);
2715 } else {
2716 /* detect the special case for front/back colors where
2717 * we need to do flat vs smooth shading depending on
2718 * rast state:
2719 */
2720 if (in->data.interpolation == INTERP_MODE_NONE) {
2721 switch (slot) {
2722 case VARYING_SLOT_COL0:
2723 case VARYING_SLOT_COL1:
2724 case VARYING_SLOT_BFC0:
2725 case VARYING_SLOT_BFC1:
2726 so->inputs[n].rasterflat = true;
2727 break;
2728 default:
2729 break;
2730 }
2731 }
2732
2733 if (ctx->compiler->flat_bypass) {
2734 if ((so->inputs[n].interpolate == INTERP_MODE_FLAT) ||
2735 (so->inputs[n].rasterflat && ctx->so->key.rasterflat))
2736 so->inputs[n].use_ldlv = true;
2737 }
2738
2739 so->inputs[n].bary = true;
2740
2741 instr = create_frag_input(ctx, so->inputs[n].use_ldlv, idx);
2742 }
2743
2744 compile_assert(ctx, idx < ctx->ir->ninputs);
2745
2746 ctx->ir->inputs[idx] = instr;
2747 }
2748 } else if (ctx->so->type == MESA_SHADER_VERTEX) {
2749 for (int i = 0; i < ncomp; i++) {
2750 unsigned idx = (n * 4) + i + frac;
2751 compile_assert(ctx, idx < ctx->ir->ninputs);
2752 ctx->ir->inputs[idx] = create_input(ctx, idx);
2753 }
2754 } else {
2755 ir3_context_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2756 }
2757
2758 if (so->inputs[n].bary || (ctx->so->type == MESA_SHADER_VERTEX)) {
2759 so->total_in += ncomp;
2760 }
2761 }
2762
2763 /* Initially we assign non-packed inloc's for varyings, as we don't really
2764 * know up-front which components will be unused. After all the compilation
2765 * stages we scan the shader to see which components are actually used, and
2766 * re-pack the inlocs to eliminate unneeded varyings.
2767 */
2768 static void
2769 pack_inlocs(struct ir3_context *ctx)
2770 {
2771 struct ir3_shader_variant *so = ctx->so;
2772 uint8_t used_components[so->inputs_count];
2773
2774 memset(used_components, 0, sizeof(used_components));
2775
2776 /*
2777 * First Step: scan shader to find which bary.f/ldlv remain:
2778 */
2779
2780 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2781 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
2782 if (is_input(instr)) {
2783 unsigned inloc = instr->regs[1]->iim_val;
2784 unsigned i = inloc / 4;
2785 unsigned j = inloc % 4;
2786
2787 compile_assert(ctx, instr->regs[1]->flags & IR3_REG_IMMED);
2788 compile_assert(ctx, i < so->inputs_count);
2789
2790 used_components[i] |= 1 << j;
2791 } else if (instr->opc == OPC_META_TEX_PREFETCH) {
2792 for (int n = 0; n < 2; n++) {
2793 unsigned inloc = instr->prefetch.input_offset + n;
2794 unsigned i = inloc / 4;
2795 unsigned j = inloc % 4;
2796
2797 compile_assert(ctx, i < so->inputs_count);
2798
2799 used_components[i] |= 1 << j;
2800 }
2801 }
2802 }
2803 }
2804
2805 /*
2806 * Second Step: reassign varying inloc/slots:
2807 */
2808
2809 unsigned actual_in = 0;
2810 unsigned inloc = 0;
2811
2812 for (unsigned i = 0; i < so->inputs_count; i++) {
2813 unsigned compmask = 0, maxcomp = 0;
2814
2815 so->inputs[i].inloc = inloc;
2816 so->inputs[i].bary = false;
2817
2818 for (unsigned j = 0; j < 4; j++) {
2819 if (!(used_components[i] & (1 << j)))
2820 continue;
2821
2822 compmask |= (1 << j);
2823 actual_in++;
2824 maxcomp = j + 1;
2825
2826 /* at this point, since used_components[i] mask is only
2827 * considering varyings (ie. not sysvals) we know this
2828 * is a varying:
2829 */
2830 so->inputs[i].bary = true;
2831 }
2832
2833 if (so->inputs[i].bary) {
2834 so->varying_in++;
2835 so->inputs[i].compmask = (1 << maxcomp) - 1;
2836 inloc += maxcomp;
2837 }
2838 }
2839
2840 /*
2841 * Third Step: reassign packed inloc's:
2842 */
2843
2844 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2845 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
2846 if (is_input(instr)) {
2847 unsigned inloc = instr->regs[1]->iim_val;
2848 unsigned i = inloc / 4;
2849 unsigned j = inloc % 4;
2850
2851 instr->regs[1]->iim_val = so->inputs[i].inloc + j;
2852 }
2853 }
2854 }
2855 }
2856
2857 static void
2858 setup_output(struct ir3_context *ctx, nir_variable *out)
2859 {
2860 struct ir3_shader_variant *so = ctx->so;
2861 unsigned ncomp = glsl_get_components(out->type);
2862 unsigned n = out->data.driver_location;
2863 unsigned frac = out->data.location_frac;
2864 unsigned slot = out->data.location;
2865 unsigned comp = 0;
2866
2867 if (ctx->so->type == MESA_SHADER_FRAGMENT) {
2868 switch (slot) {
2869 case FRAG_RESULT_DEPTH:
2870 comp = 2; /* tgsi will write to .z component */
2871 so->writes_pos = true;
2872 break;
2873 case FRAG_RESULT_COLOR:
2874 so->color0_mrt = 1;
2875 break;
2876 case FRAG_RESULT_SAMPLE_MASK:
2877 so->writes_smask = true;
2878 break;
2879 default:
2880 if (slot >= FRAG_RESULT_DATA0)
2881 break;
2882 ir3_context_error(ctx, "unknown FS output name: %s\n",
2883 gl_frag_result_name(slot));
2884 }
2885 } else if (ctx->so->type == MESA_SHADER_VERTEX ||
2886 ctx->so->type == MESA_SHADER_TESS_EVAL ||
2887 ctx->so->type == MESA_SHADER_GEOMETRY) {
2888 switch (slot) {
2889 case VARYING_SLOT_POS:
2890 so->writes_pos = true;
2891 break;
2892 case VARYING_SLOT_PSIZ:
2893 so->writes_psize = true;
2894 break;
2895 case VARYING_SLOT_PRIMITIVE_ID:
2896 case VARYING_SLOT_LAYER:
2897 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3:
2898 debug_assert(ctx->so->type == MESA_SHADER_GEOMETRY);
2899 /* fall through */
2900 case VARYING_SLOT_COL0:
2901 case VARYING_SLOT_COL1:
2902 case VARYING_SLOT_BFC0:
2903 case VARYING_SLOT_BFC1:
2904 case VARYING_SLOT_FOGC:
2905 case VARYING_SLOT_CLIP_DIST0:
2906 case VARYING_SLOT_CLIP_DIST1:
2907 case VARYING_SLOT_CLIP_VERTEX:
2908 break;
2909 default:
2910 if (slot >= VARYING_SLOT_VAR0)
2911 break;
2912 if ((VARYING_SLOT_TEX0 <= slot) && (slot <= VARYING_SLOT_TEX7))
2913 break;
2914 ir3_context_error(ctx, "unknown %s shader output name: %s\n",
2915 _mesa_shader_stage_to_string(ctx->so->type),
2916 gl_varying_slot_name(slot));
2917 }
2918 } else if (ctx->so->type == MESA_SHADER_TESS_CTRL) {
2919 /* output lowered to buffer writes. */
2920 return;
2921 } else {
2922 ir3_context_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2923 }
2924
2925 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2926
2927 so->outputs[n].slot = slot;
2928 so->outputs[n].regid = regid(n, comp);
2929 so->outputs_count = MAX2(so->outputs_count, n + 1);
2930
2931 for (int i = 0; i < ncomp; i++) {
2932 unsigned idx = (n * 4) + i + frac;
2933 compile_assert(ctx, idx < ctx->ir->noutputs);
2934 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2935 }
2936
2937 /* if varying packing doesn't happen, we could end up in a situation
2938 * with "holes" in the output, and since the per-generation code that
2939 * sets up varying linkage registers doesn't expect to have more than
2940 * one varying per vec4 slot, pad the holes.
2941 *
2942 * Note that this should probably generate a performance warning of
2943 * some sort.
2944 */
2945 for (int i = 0; i < frac; i++) {
2946 unsigned idx = (n * 4) + i;
2947 if (!ctx->ir->outputs[idx]) {
2948 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2949 }
2950 }
2951 }
2952
2953 static int
2954 max_drvloc(struct exec_list *vars)
2955 {
2956 int drvloc = -1;
2957 nir_foreach_variable(var, vars) {
2958 drvloc = MAX2(drvloc, (int)var->data.driver_location);
2959 }
2960 return drvloc;
2961 }
2962
2963 static const unsigned max_sysvals[] = {
2964 [MESA_SHADER_VERTEX] = 16,
2965 [MESA_SHADER_TESS_CTRL] = 16,
2966 [MESA_SHADER_TESS_EVAL] = 16,
2967 [MESA_SHADER_GEOMETRY] = 16,
2968 [MESA_SHADER_FRAGMENT] = 24, // TODO
2969 [MESA_SHADER_COMPUTE] = 16, // TODO how many do we actually need?
2970 [MESA_SHADER_KERNEL] = 16, // TODO how many do we actually need?
2971 };
2972
2973 static void
2974 emit_instructions(struct ir3_context *ctx)
2975 {
2976 unsigned ninputs, noutputs;
2977 nir_function_impl *fxn = nir_shader_get_entrypoint(ctx->s);
2978
2979 ninputs = (max_drvloc(&ctx->s->inputs) + 1) * 4;
2980 noutputs = (max_drvloc(&ctx->s->outputs) + 1) * 4;
2981
2982 /* we need to leave room for sysvals:
2983 */
2984 ninputs += max_sysvals[ctx->so->type];
2985 if (ctx->so->type == MESA_SHADER_VERTEX ||
2986 ctx->so->type == MESA_SHADER_TESS_EVAL)
2987 noutputs += 8; /* gs or tess header + primitive_id */
2988
2989 ctx->ir = ir3_create(ctx->compiler, ctx->so->type, ninputs, noutputs);
2990
2991 /* Create inputs in first block: */
2992 ctx->block = get_block(ctx, nir_start_block(fxn));
2993 ctx->in_block = ctx->block;
2994 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2995
2996 ninputs -= max_sysvals[ctx->so->type];
2997
2998 /* Tesselation shaders always need primitive ID for indexing the
2999 * BO. Geometry shaders don't always need it but when they do it has be
3000 * delivered and unclobbered in the VS. To make things easy, we always
3001 * make room for it in VS/DS.
3002 */
3003 bool has_tess = ctx->so->key.tessellation != IR3_TESS_NONE;
3004 bool has_gs = ctx->so->key.has_gs;
3005 switch (ctx->so->type) {
3006 case MESA_SHADER_VERTEX:
3007 if (has_tess) {
3008 ctx->tcs_header = create_input(ctx, 0);
3009 ctx->primitive_id = create_input(ctx, 0);
3010 } else if (has_gs) {
3011 ctx->gs_header = create_input(ctx, 0);
3012 ctx->primitive_id = create_input(ctx, 0);
3013 }
3014 break;
3015 case MESA_SHADER_TESS_CTRL:
3016 ctx->tcs_header = create_input(ctx, 0);
3017 ctx->primitive_id = create_input(ctx, 0);
3018 break;
3019 case MESA_SHADER_TESS_EVAL:
3020 if (has_gs)
3021 ctx->gs_header = create_input(ctx, 0);
3022 ctx->primitive_id = create_input(ctx, 0);
3023 break;
3024 case MESA_SHADER_GEOMETRY:
3025 ctx->gs_header = create_input(ctx, 0);
3026 ctx->primitive_id = create_input(ctx, 0);
3027 break;
3028 default:
3029 break;
3030 }
3031
3032 /* for fragment shader, the vcoord input register is used as the
3033 * base for bary.f varying fetch instrs:
3034 *
3035 * TODO defer creating ctx->ij_pixel and corresponding sysvals
3036 * until emit_intrinsic when we know they are actually needed.
3037 * For now, we defer creating ctx->ij_centroid, etc, since we
3038 * only need ij_pixel for "old style" varying inputs (ie.
3039 * tgsi_to_nir)
3040 */
3041 struct ir3_instruction *vcoord = NULL;
3042 if (ctx->so->type == MESA_SHADER_FRAGMENT) {
3043 struct ir3_instruction *xy[2];
3044
3045 vcoord = create_input_compmask(ctx, 0, 0x3);
3046 ir3_split_dest(ctx->block, xy, vcoord, 0, 2);
3047
3048 ctx->ij_pixel = ir3_create_collect(ctx, xy, 2);
3049 }
3050
3051 /* Setup inputs: */
3052 nir_foreach_variable(var, &ctx->s->inputs) {
3053 setup_input(ctx, var);
3054 }
3055
3056 /* Defer add_sysval_input() stuff until after setup_inputs(),
3057 * because sysvals need to be appended after varyings:
3058 */
3059 if (vcoord) {
3060 add_sysval_input_compmask(ctx, SYSTEM_VALUE_BARYCENTRIC_PIXEL,
3061 0x3, vcoord);
3062 }
3063
3064 if (ctx->primitive_id)
3065 add_sysval_input(ctx, SYSTEM_VALUE_PRIMITIVE_ID, ctx->primitive_id);
3066 if (ctx->gs_header)
3067 add_sysval_input(ctx, SYSTEM_VALUE_GS_HEADER_IR3, ctx->gs_header);
3068 if (ctx->tcs_header)
3069 add_sysval_input(ctx, SYSTEM_VALUE_TCS_HEADER_IR3, ctx->tcs_header);
3070
3071 /* Setup outputs: */
3072 nir_foreach_variable(var, &ctx->s->outputs) {
3073 setup_output(ctx, var);
3074 }
3075
3076 /* Set up the shared system values as outputs for the vertex and tess eval
3077 * shaders so they don't clobber them for the next shader in the pipeline.
3078 */
3079 if (ctx->so->type == MESA_SHADER_VERTEX ||
3080 (has_gs && ctx->so->type == MESA_SHADER_TESS_EVAL)) {
3081 struct ir3_shader_variant *so = ctx->so;
3082 if (ctx->primitive_id) {
3083 unsigned n = so->outputs_count++;
3084 so->outputs[n].slot = VARYING_SLOT_PRIMITIVE_ID;
3085 so->outputs[n].regid = regid(n, 0);
3086 ctx->ir->outputs[n * 4] = ctx->primitive_id;
3087
3088 compile_assert(ctx, n * 4 < ctx->ir->noutputs);
3089 }
3090
3091 if (ctx->gs_header) {
3092 unsigned n = so->outputs_count++;
3093 so->outputs[n].slot = VARYING_SLOT_GS_HEADER_IR3;
3094 so->outputs[n].regid = regid(n, 0);
3095 ctx->ir->outputs[n * 4] = ctx->gs_header;
3096
3097 compile_assert(ctx, n * 4 < ctx->ir->noutputs);
3098 }
3099
3100 if (ctx->tcs_header) {
3101 unsigned n = so->outputs_count++;
3102 so->outputs[n].slot = VARYING_SLOT_TCS_HEADER_IR3;
3103 so->outputs[n].regid = regid(n, 0);
3104 ctx->ir->outputs[n * 4] = ctx->tcs_header;
3105
3106 compile_assert(ctx, n * 4 < ctx->ir->noutputs);
3107 }
3108 }
3109
3110 /* Find # of samplers: */
3111 nir_foreach_variable(var, &ctx->s->uniforms) {
3112 ctx->so->num_samp += glsl_type_get_sampler_count(var->type);
3113 /* just assume that we'll be reading from images.. if it
3114 * is write-only we don't have to count it, but not sure
3115 * if there is a good way to know?
3116 */
3117 ctx->so->num_samp += glsl_type_get_image_count(var->type);
3118 }
3119
3120 /* NOTE: need to do something more clever when we support >1 fxn */
3121 nir_foreach_register(reg, &fxn->registers) {
3122 ir3_declare_array(ctx, reg);
3123 }
3124 /* And emit the body: */
3125 ctx->impl = fxn;
3126 emit_function(ctx, fxn);
3127 }
3128
3129 /* from NIR perspective, we actually have varying inputs. But the varying
3130 * inputs, from an IR standpoint, are just bary.f/ldlv instructions. The
3131 * only actual inputs are the sysvals.
3132 */
3133 static void
3134 fixup_frag_inputs(struct ir3_context *ctx)
3135 {
3136 struct ir3_shader_variant *so = ctx->so;
3137 struct ir3 *ir = ctx->ir;
3138 unsigned i = 0;
3139
3140 /* sysvals should appear at the end of the inputs, drop everything else: */
3141 while ((i < so->inputs_count) && !so->inputs[i].sysval)
3142 i++;
3143
3144 /* at IR level, inputs are always blocks of 4 scalars: */
3145 i *= 4;
3146
3147 ir->inputs = &ir->inputs[i];
3148 ir->ninputs -= i;
3149 }
3150
3151 /* Fixup tex sampler state for astc/srgb workaround instructions. We
3152 * need to assign the tex state indexes for these after we know the
3153 * max tex index.
3154 */
3155 static void
3156 fixup_astc_srgb(struct ir3_context *ctx)
3157 {
3158 struct ir3_shader_variant *so = ctx->so;
3159 /* indexed by original tex idx, value is newly assigned alpha sampler
3160 * state tex idx. Zero is invalid since there is at least one sampler
3161 * if we get here.
3162 */
3163 unsigned alt_tex_state[16] = {0};
3164 unsigned tex_idx = ctx->max_texture_index + 1;
3165 unsigned idx = 0;
3166
3167 so->astc_srgb.base = tex_idx;
3168
3169 for (unsigned i = 0; i < ctx->ir->astc_srgb_count; i++) {
3170 struct ir3_instruction *sam = ctx->ir->astc_srgb[i];
3171
3172 compile_assert(ctx, sam->cat5.tex < ARRAY_SIZE(alt_tex_state));
3173
3174 if (alt_tex_state[sam->cat5.tex] == 0) {
3175 /* assign new alternate/alpha tex state slot: */
3176 alt_tex_state[sam->cat5.tex] = tex_idx++;
3177 so->astc_srgb.orig_idx[idx++] = sam->cat5.tex;
3178 so->astc_srgb.count++;
3179 }
3180
3181 sam->cat5.tex = alt_tex_state[sam->cat5.tex];
3182 }
3183 }
3184
3185 static void
3186 fixup_binning_pass(struct ir3_context *ctx)
3187 {
3188 struct ir3_shader_variant *so = ctx->so;
3189 struct ir3 *ir = ctx->ir;
3190 unsigned i, j;
3191
3192 for (i = 0, j = 0; i < so->outputs_count; i++) {
3193 unsigned slot = so->outputs[i].slot;
3194
3195 /* throw away everything but first position/psize */
3196 if ((slot == VARYING_SLOT_POS) || (slot == VARYING_SLOT_PSIZ)) {
3197 if (i != j) {
3198 so->outputs[j] = so->outputs[i];
3199 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
3200 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
3201 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
3202 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
3203 }
3204 j++;
3205 }
3206 }
3207 so->outputs_count = j;
3208 ir->noutputs = j * 4;
3209 }
3210
3211 static void
3212 collect_tex_prefetches(struct ir3_context *ctx, struct ir3 *ir)
3213 {
3214 unsigned idx = 0;
3215
3216 /* Collect sampling instructions eligible for pre-dispatch. */
3217 list_for_each_entry(struct ir3_block, block, &ir->block_list, node) {
3218 list_for_each_entry_safe(struct ir3_instruction, instr,
3219 &block->instr_list, node) {
3220 if (instr->opc == OPC_META_TEX_PREFETCH) {
3221 assert(idx < ARRAY_SIZE(ctx->so->sampler_prefetch));
3222 struct ir3_sampler_prefetch *fetch =
3223 &ctx->so->sampler_prefetch[idx];
3224 idx++;
3225
3226 fetch->cmd = IR3_SAMPLER_PREFETCH_CMD;
3227 fetch->wrmask = instr->regs[0]->wrmask;
3228 fetch->tex_id = instr->prefetch.tex;
3229 fetch->samp_id = instr->prefetch.samp;
3230 fetch->dst = instr->regs[0]->num;
3231 fetch->src = instr->prefetch.input_offset;
3232
3233 ctx->so->total_in =
3234 MAX2(ctx->so->total_in, instr->prefetch.input_offset + 2);
3235
3236 /* Disable half precision until supported. */
3237 fetch->half_precision = 0x0;
3238
3239 /* Remove the prefetch placeholder instruction: */
3240 list_delinit(&instr->node);
3241 }
3242 }
3243 }
3244 }
3245
3246 int
3247 ir3_compile_shader_nir(struct ir3_compiler *compiler,
3248 struct ir3_shader_variant *so)
3249 {
3250 struct ir3_context *ctx;
3251 struct ir3 *ir;
3252 struct ir3_instruction **inputs;
3253 unsigned i;
3254 int ret = 0, max_bary;
3255
3256 assert(!so->ir);
3257
3258 ctx = ir3_context_init(compiler, so);
3259 if (!ctx) {
3260 DBG("INIT failed!");
3261 ret = -1;
3262 goto out;
3263 }
3264
3265 emit_instructions(ctx);
3266
3267 if (ctx->error) {
3268 DBG("EMIT failed!");
3269 ret = -1;
3270 goto out;
3271 }
3272
3273 ir = so->ir = ctx->ir;
3274
3275 /* keep track of the inputs from TGSI perspective.. */
3276 inputs = ir->inputs;
3277
3278 /* but fixup actual inputs for frag shader: */
3279 if (so->type == MESA_SHADER_FRAGMENT)
3280 fixup_frag_inputs(ctx);
3281
3282 /* at this point, for binning pass, throw away unneeded outputs: */
3283 if (so->binning_pass && (ctx->compiler->gpu_id < 600))
3284 fixup_binning_pass(ctx);
3285
3286 /* if we want half-precision outputs, mark the output registers
3287 * as half:
3288 */
3289 if (so->key.half_precision) {
3290 for (i = 0; i < ir->noutputs; i++) {
3291 struct ir3_instruction *out = ir->outputs[i];
3292
3293 if (!out)
3294 continue;
3295
3296 /* if frag shader writes z, that needs to be full precision: */
3297 if (so->outputs[i/4].slot == FRAG_RESULT_DEPTH)
3298 continue;
3299
3300 out->regs[0]->flags |= IR3_REG_HALF;
3301 /* output could be a fanout (ie. texture fetch output)
3302 * in which case we need to propagate the half-reg flag
3303 * up to the definer so that RA sees it:
3304 */
3305 if (out->opc == OPC_META_FO) {
3306 out = out->regs[1]->instr;
3307 out->regs[0]->flags |= IR3_REG_HALF;
3308 }
3309
3310 if (out->opc == OPC_MOV) {
3311 out->cat1.dst_type = half_type(out->cat1.dst_type);
3312 }
3313 }
3314 }
3315
3316 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3317 printf("BEFORE CP:\n");
3318 ir3_print(ir);
3319 }
3320
3321 ir3_cp(ir, so);
3322
3323 /* at this point, for binning pass, throw away unneeded outputs:
3324 * Note that for a6xx and later, we do this after ir3_cp to ensure
3325 * that the uniform/constant layout for BS and VS matches, so that
3326 * we can re-use same VS_CONST state group.
3327 */
3328 if (so->binning_pass && (ctx->compiler->gpu_id >= 600))
3329 fixup_binning_pass(ctx);
3330
3331 /* for a6xx+, binning and draw pass VS use same VBO state, so we
3332 * need to make sure not to remove any inputs that are used by
3333 * the nonbinning VS.
3334 */
3335 if (ctx->compiler->gpu_id >= 600 && so->binning_pass &&
3336 so->type == MESA_SHADER_VERTEX) {
3337 for (int i = 0; i < ir->ninputs; i++) {
3338 struct ir3_instruction *in = ir->inputs[i];
3339
3340 if (!in)
3341 continue;
3342
3343 unsigned n = i / 4;
3344 unsigned c = i % 4;
3345
3346 debug_assert(n < so->nonbinning->inputs_count);
3347
3348 if (so->nonbinning->inputs[n].sysval)
3349 continue;
3350
3351 /* be sure to keep inputs, even if only used in VS */
3352 if (so->nonbinning->inputs[n].compmask & (1 << c))
3353 array_insert(in->block, in->block->keeps, in);
3354 }
3355 }
3356
3357 /* Insert mov if there's same instruction for each output.
3358 * eg. dEQP-GLES31.functional.shaders.opaque_type_indexing.sampler.const_expression.vertex.sampler2dshadow
3359 */
3360 for (int i = ir->noutputs - 1; i >= 0; i--) {
3361 if (!ir->outputs[i])
3362 continue;
3363 for (unsigned j = 0; j < i; j++) {
3364 if (ir->outputs[i] == ir->outputs[j]) {
3365 ir->outputs[i] =
3366 ir3_MOV(ir->outputs[i]->block, ir->outputs[i], TYPE_F32);
3367 }
3368 }
3369 }
3370
3371 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3372 printf("BEFORE GROUPING:\n");
3373 ir3_print(ir);
3374 }
3375
3376 ir3_sched_add_deps(ir);
3377
3378 /* Group left/right neighbors, inserting mov's where needed to
3379 * solve conflicts:
3380 */
3381 ir3_group(ir);
3382
3383 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3384 printf("AFTER GROUPING:\n");
3385 ir3_print(ir);
3386 }
3387
3388 ir3_depth(ir, so);
3389
3390 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3391 printf("AFTER DEPTH:\n");
3392 ir3_print(ir);
3393 }
3394
3395 /* do Sethi–Ullman numbering before scheduling: */
3396 ir3_sun(ir);
3397
3398 ret = ir3_sched(ir);
3399 if (ret) {
3400 DBG("SCHED failed!");
3401 goto out;
3402 }
3403
3404 if (compiler->gpu_id >= 600) {
3405 ir3_a6xx_fixup_atomic_dests(ir, so);
3406 }
3407
3408 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3409 printf("AFTER SCHED:\n");
3410 ir3_print(ir);
3411 }
3412
3413 /* Pre-assign VS inputs on a6xx+ binning pass shader, to align
3414 * with draw pass VS, so binning and draw pass can both use the
3415 * same VBO state.
3416 *
3417 * Note that VS inputs are expected to be full precision.
3418 */
3419 bool pre_assign_inputs = (ir->compiler->gpu_id >= 600) &&
3420 (ir->type == MESA_SHADER_VERTEX) &&
3421 so->binning_pass;
3422
3423 if (pre_assign_inputs) {
3424 for (unsigned i = 0; i < ir->ninputs; i++) {
3425 struct ir3_instruction *instr = ir->inputs[i];
3426
3427 if (!instr)
3428 continue;
3429
3430 unsigned n = i / 4;
3431 unsigned c = i % 4;
3432 unsigned regid = so->nonbinning->inputs[n].regid + c;
3433
3434 instr->regs[0]->num = regid;
3435 }
3436
3437 ret = ir3_ra(so, ir->inputs, ir->ninputs);
3438 } else if (ctx->tcs_header) {
3439 /* We need to have these values in the same registers between VS and TCS
3440 * since the VS chains to TCS and doesn't get the sysvals redelivered.
3441 */
3442
3443 ctx->tcs_header->regs[0]->num = regid(0, 0);
3444 ctx->primitive_id->regs[0]->num = regid(0, 1);
3445 struct ir3_instruction *precolor[] = { ctx->tcs_header, ctx->primitive_id };
3446 ret = ir3_ra(so, precolor, ARRAY_SIZE(precolor));
3447 } else if (ctx->gs_header) {
3448 /* We need to have these values in the same registers between producer
3449 * (VS or DS) and GS since the producer chains to GS and doesn't get
3450 * the sysvals redelivered.
3451 */
3452
3453 ctx->gs_header->regs[0]->num = regid(0, 0);
3454 ctx->primitive_id->regs[0]->num = regid(0, 1);
3455 struct ir3_instruction *precolor[] = { ctx->gs_header, ctx->primitive_id };
3456 ret = ir3_ra(so, precolor, ARRAY_SIZE(precolor));
3457 } else if (so->num_sampler_prefetch) {
3458 assert(so->type == MESA_SHADER_FRAGMENT);
3459 struct ir3_instruction *precolor[2];
3460 int idx = 0;
3461
3462 for (unsigned i = 0; i < ir->ninputs; i++) {
3463 struct ir3_instruction *instr = ctx->ir->inputs[i];
3464
3465 if (!instr)
3466 continue;
3467
3468 if (instr->input.sysval != SYSTEM_VALUE_BARYCENTRIC_PIXEL)
3469 continue;
3470
3471 assert(idx < ARRAY_SIZE(precolor));
3472
3473 precolor[idx] = instr;
3474 instr->regs[0]->num = idx;
3475
3476 idx++;
3477 }
3478 ret = ir3_ra(so, precolor, idx);
3479 } else {
3480 ret = ir3_ra(so, NULL, 0);
3481 }
3482
3483 if (ret) {
3484 DBG("RA failed!");
3485 goto out;
3486 }
3487
3488 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3489 printf("AFTER RA:\n");
3490 ir3_print(ir);
3491 }
3492
3493 if (so->type == MESA_SHADER_FRAGMENT)
3494 pack_inlocs(ctx);
3495
3496 /* fixup input/outputs: */
3497 for (i = 0; i < so->outputs_count; i++) {
3498 /* sometimes we get outputs that don't write the .x coord, like:
3499 *
3500 * decl_var shader_out INTERP_MODE_NONE float Color (VARYING_SLOT_VAR9.z, 1, 0)
3501 *
3502 * Presumably the result of varying packing and then eliminating
3503 * some unneeded varyings? Just skip head to the first valid
3504 * component of the output.
3505 */
3506 for (unsigned j = 0; j < 4; j++) {
3507 struct ir3_instruction *instr = ir->outputs[(i*4) + j];
3508 if (instr) {
3509 so->outputs[i].regid = instr->regs[0]->num;
3510 so->outputs[i].half = !!(instr->regs[0]->flags & IR3_REG_HALF);
3511 break;
3512 }
3513 }
3514 }
3515
3516 /* Note that some or all channels of an input may be unused: */
3517 for (i = 0; i < so->inputs_count; i++) {
3518 unsigned j, reg = regid(63,0);
3519 bool half = false;
3520 for (j = 0; j < 4; j++) {
3521 struct ir3_instruction *in = inputs[(i*4) + j];
3522
3523 if (!in)
3524 continue;
3525
3526 if (in->flags & IR3_INSTR_UNUSED)
3527 continue;
3528
3529 reg = in->regs[0]->num - j;
3530 if (half) {
3531 compile_assert(ctx, in->regs[0]->flags & IR3_REG_HALF);
3532 } else {
3533 half = !!(in->regs[0]->flags & IR3_REG_HALF);
3534 }
3535 }
3536 so->inputs[i].regid = reg;
3537 so->inputs[i].half = half;
3538 }
3539
3540 if (ctx->astc_srgb)
3541 fixup_astc_srgb(ctx);
3542
3543 /* We need to do legalize after (for frag shader's) the "bary.f"
3544 * offsets (inloc) have been assigned.
3545 */
3546 ir3_legalize(ir, &so->has_ssbo, &so->need_pixlod, &max_bary);
3547
3548 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
3549 printf("AFTER LEGALIZE:\n");
3550 ir3_print(ir);
3551 }
3552
3553 /* Set (ss)(sy) on first TCS and GEOMETRY instructions, since we don't
3554 * know what we might have to wait on when coming in from VS chsh.
3555 */
3556 if (so->type == MESA_SHADER_TESS_CTRL ||
3557 so->type == MESA_SHADER_GEOMETRY ) {
3558 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
3559 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
3560 instr->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
3561 break;
3562 }
3563 }
3564 }
3565
3566 so->branchstack = ctx->max_stack;
3567
3568 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
3569 if (so->type == MESA_SHADER_FRAGMENT)
3570 so->total_in = max_bary + 1;
3571
3572 so->max_sun = ir->max_sun;
3573
3574 /* Collect sampling instructions eligible for pre-dispatch. */
3575 collect_tex_prefetches(ctx, ir);
3576
3577 out:
3578 if (ret) {
3579 if (so->ir)
3580 ir3_destroy(so->ir);
3581 so->ir = NULL;
3582 }
3583 ir3_context_free(ctx);
3584
3585 return ret;
3586 }