1 /*
2 * Copyright © 2014 Rob Clark <robclark@freedesktop.org>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <robclark@freedesktop.org>
7 */
8
9 #include <math.h>
10 #include "util/half_float.h"
11 #include "util/u_math.h"
12
13 #include "ir3.h"
14 #include "ir3_compiler.h"
15 #include "ir3_shader.h"
16
17 #define swap(a, b) \
18 do { \
19 __typeof(a) __tmp = (a); \
20 (a) = (b); \
21 (b) = __tmp; \
22 } while (0)
23
24 /*
25 * Copy Propagate:
26 */
27
28 struct ir3_cp_ctx {
29 struct ir3 *shader;
30 struct ir3_shader_variant *so;
31 bool progress;
32 };
33
34 /* is it a type preserving mov, with ok flags?
35 *
36 * @instr: the mov to consider removing
37 * @dst_instr: the instruction consuming the mov (instr)
38 *
39 * TODO maybe drop allow_flags since this is only false when dst is
40 * NULL (ie. outputs)
41 */
42 static bool
is_eligible_mov(struct ir3_instruction * instr,struct ir3_instruction * dst_instr,bool allow_flags)43 is_eligible_mov(struct ir3_instruction *instr,
44 struct ir3_instruction *dst_instr, bool allow_flags)
45 {
46 if (is_same_type_mov(instr)) {
47 struct ir3_register *dst = instr->dsts[0];
48 struct ir3_register *src = instr->srcs[0];
49 struct ir3_instruction *src_instr = ssa(src);
50
51 /* only if mov src is SSA (not const/immed): */
52 if (!src_instr)
53 return false;
54
55 /* no indirect: */
56 if (dst->flags & IR3_REG_RELATIV)
57 return false;
58 if (src->flags & IR3_REG_RELATIV)
59 return false;
60
61 if (src->flags & IR3_REG_ARRAY)
62 return false;
63
64 if (!allow_flags)
65 if (src->flags & (IR3_REG_FABS | IR3_REG_FNEG | IR3_REG_SABS |
66 IR3_REG_SNEG | IR3_REG_BNOT))
67 return false;
68
69 return true;
70 }
71 return false;
72 }
73
74 /* propagate register flags from src to dst.. negates need special
75 * handling to cancel each other out.
76 */
77 static void
combine_flags(unsigned * dstflags,struct ir3_instruction * src)78 combine_flags(unsigned *dstflags, struct ir3_instruction *src)
79 {
80 unsigned srcflags = src->srcs[0]->flags;
81
82 /* if what we are combining into already has (abs) flags,
83 * we can drop (neg) from src:
84 */
85 if (*dstflags & IR3_REG_FABS)
86 srcflags &= ~IR3_REG_FNEG;
87 if (*dstflags & IR3_REG_SABS)
88 srcflags &= ~IR3_REG_SNEG;
89
90 if (srcflags & IR3_REG_FABS)
91 *dstflags |= IR3_REG_FABS;
92 if (srcflags & IR3_REG_SABS)
93 *dstflags |= IR3_REG_SABS;
94 if (srcflags & IR3_REG_FNEG)
95 *dstflags ^= IR3_REG_FNEG;
96 if (srcflags & IR3_REG_SNEG)
97 *dstflags ^= IR3_REG_SNEG;
98 if (srcflags & IR3_REG_BNOT)
99 *dstflags ^= IR3_REG_BNOT;
100
101 *dstflags &= ~(IR3_REG_SSA | IR3_REG_SHARED);
102 *dstflags |= srcflags & IR3_REG_SSA;
103 *dstflags |= srcflags & IR3_REG_CONST;
104 *dstflags |= srcflags & IR3_REG_IMMED;
105 *dstflags |= srcflags & IR3_REG_RELATIV;
106 *dstflags |= srcflags & IR3_REG_ARRAY;
107 *dstflags |= srcflags & IR3_REG_SHARED;
108
109 /* if src of the src is boolean we can drop the (abs) since we know
110 * the source value is already a postitive integer. This cleans
111 * up the absnegs that get inserted when converting between nir and
112 * native boolean (see ir3_b2n/n2b)
113 */
114 struct ir3_instruction *srcsrc = ssa(src->srcs[0]);
115 if (srcsrc && is_bool(srcsrc))
116 *dstflags &= ~IR3_REG_SABS;
117 }
118
119 /* Tries lowering an immediate register argument to a const buffer access by
120 * adding to the list of immediates to be pushed to the const buffer when
121 * switching to this shader.
122 */
123 static bool
lower_immed(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr,unsigned n,struct ir3_register * reg,unsigned new_flags)124 lower_immed(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr, unsigned n,
125 struct ir3_register *reg, unsigned new_flags)
126 {
127 if (ctx->shader->compiler->load_shader_consts_via_preamble)
128 return false;
129
130 if (!(new_flags & IR3_REG_IMMED))
131 return false;
132
133 new_flags &= ~IR3_REG_IMMED;
134 new_flags |= IR3_REG_CONST;
135
136 if (!ir3_valid_flags(instr, n, new_flags))
137 return false;
138
139 reg = ir3_reg_clone(ctx->shader, reg);
140
141 /* Half constant registers seems to handle only 32-bit values
142 * within floating-point opcodes. So convert back to 32-bit values.
143 */
144 bool f_opcode =
145 (is_cat2_float(instr->opc) || is_cat3_float(instr->opc)) ? true : false;
146 if (f_opcode && (new_flags & IR3_REG_HALF))
147 reg->uim_val = fui(_mesa_half_to_float(reg->uim_val));
148
149 /* in some cases, there are restrictions on (abs)/(neg) plus const..
150 * so just evaluate those and clear the flags:
151 */
152 if (new_flags & IR3_REG_SABS) {
153 reg->iim_val = abs(reg->iim_val);
154 new_flags &= ~IR3_REG_SABS;
155 }
156
157 if (new_flags & IR3_REG_FABS) {
158 reg->fim_val = fabs(reg->fim_val);
159 new_flags &= ~IR3_REG_FABS;
160 }
161
162 if (new_flags & IR3_REG_SNEG) {
163 reg->iim_val = -reg->iim_val;
164 new_flags &= ~IR3_REG_SNEG;
165 }
166
167 if (new_flags & IR3_REG_FNEG) {
168 reg->fim_val = -reg->fim_val;
169 new_flags &= ~IR3_REG_FNEG;
170 }
171
172 reg->num = ir3_const_find_imm(ctx->so, reg->uim_val);
173
174 if (reg->num == INVALID_CONST_REG) {
175 /* Don't modify the const state for the binning variant. */
176 if (ctx->so->binning_pass)
177 return false;
178
179 reg->num = ir3_const_add_imm(ctx->so, reg->uim_val);
180
181 if (reg->num == INVALID_CONST_REG)
182 return false;
183 }
184
185 reg->flags = new_flags;
186
187 instr->srcs[n] = reg;
188
189 return true;
190 }
191
192 static void
unuse(struct ir3_instruction * instr)193 unuse(struct ir3_instruction *instr)
194 {
195 assert(instr->use_count > 0);
196
197 if (--instr->use_count == 0) {
198 struct ir3_block *block = instr->block;
199
200 instr->barrier_class = 0;
201 instr->barrier_conflict = 0;
202
203 /* we don't want to remove anything in keeps (which could
204 * be things like array store's)
205 */
206 for (unsigned i = 0; i < block->keeps_count; i++) {
207 assert(block->keeps[i] != instr);
208 }
209 }
210 }
211
212 /* Try to swap src n of instr using new_flags with src swap_n. */
213 static bool
try_swap_two_srcs(struct ir3_instruction * instr,unsigned n,unsigned new_flags,unsigned swap_n)214 try_swap_two_srcs(struct ir3_instruction *instr, unsigned n, unsigned new_flags,
215 unsigned swap_n)
216 {
217 /* NOTE: pre-swap first two src's before valid_flags(),
218 * which might try to dereference the n'th src:
219 */
220 swap(instr->srcs[swap_n], instr->srcs[n]);
221
222 bool valid_swap =
223 /* can we propagate mov if we move 2nd src to first? */
224 ir3_valid_flags(instr, swap_n, new_flags) &&
225 /* and does first src fit in second slot? */
226 ir3_valid_flags(instr, n, instr->srcs[n]->flags);
227
228 if (!valid_swap) {
229 /* put things back the way they were: */
230 swap(instr->srcs[swap_n], instr->srcs[n]);
231 } else {
232 /* otherwise leave things swapped */
233 instr->cat3.swapped = true;
234 }
235
236 return valid_swap;
237 }
238
239 /**
240 * Handles the special case of the 2nd src (n == 1) to "normal" mad
241 * instructions, which cannot reference a constant. See if it is
242 * possible to swap the 1st and 2nd sources.
243 * The same case is handled for sad but since it's 3-src commutative, we can
244 * also try to swap the 2nd src with the 3rd. In addition, we can try to swap
245 * either the 1st or 3rd srcs with the 2nd which may be useful since only the
246 * 2nd src supports (neg).
247 */
248 static bool
try_swap_cat3_two_srcs(struct ir3_instruction * instr,unsigned n,unsigned new_flags)249 try_swap_cat3_two_srcs(struct ir3_instruction *instr, unsigned n,
250 unsigned new_flags)
251 {
252 if (!(is_mad(instr->opc) && n == 1) && !is_sad(instr->opc))
253 return false;
254
255 /* If we've already tried, nothing more to gain.. we will only
256 * have previously swapped if the original 2nd src was const or
257 * immed. So swapping back won't improve anything and could
258 * result in an infinite "progress" loop.
259 */
260 if (instr->cat3.swapped)
261 return false;
262
263 /* cat3 doesn't encode immediate, but we can lower immediate
264 * to const if that helps:
265 */
266 if (new_flags & IR3_REG_IMMED) {
267 new_flags &= ~IR3_REG_IMMED;
268 new_flags |= IR3_REG_CONST;
269 }
270
271 /* If the reason we couldn't fold without swapping is something
272 * other than const source, then swapping won't help:
273 */
274 if (!(new_flags & (IR3_REG_CONST | IR3_REG_SHARED | IR3_REG_SNEG)))
275 return false;
276
277 if (n == 1) {
278 /* Both mad and sad support swapping srcs 2 and 1. */
279 if (try_swap_two_srcs(instr, n, new_flags, 0)) {
280 return true;
281 }
282
283 /* sad also supports swapping srcs 2 and 3. */
284 if (is_sad(instr->opc) && try_swap_two_srcs(instr, n, new_flags, 2)) {
285 return true;
286 }
287 }
288
289 /* sad also supports swapping srcs 1 or 3 with 2. */
290 return is_sad(instr->opc) && try_swap_two_srcs(instr, n, new_flags, 1);
291 }
292
293 /**
294 * Handle cp for a given src register. This additionally handles
295 * the cases of collapsing immedate/const (which replace the src
296 * register with a non-ssa src) or collapsing mov's from relative
297 * src (which needs to also fixup the address src reference by the
298 * instruction).
299 */
300 static bool
reg_cp(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr,struct ir3_register * reg,unsigned n)301 reg_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr,
302 struct ir3_register *reg, unsigned n)
303 {
304 struct ir3_instruction *src = ssa(reg);
305
306 if (is_eligible_mov(src, instr, true)) {
307 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
308 struct ir3_register *src_reg = src->srcs[0];
309 unsigned new_flags = reg->flags;
310
311 combine_flags(&new_flags, src);
312
313 if (ir3_valid_flags(instr, n, new_flags)) {
314 if (new_flags & IR3_REG_ARRAY) {
315 assert(!(reg->flags & IR3_REG_ARRAY));
316 reg->array = src_reg->array;
317 }
318 reg->flags = new_flags;
319 reg->def = src_reg->def;
320
321 instr->barrier_class |= src->barrier_class;
322 instr->barrier_conflict |= src->barrier_conflict;
323
324 unuse(src);
325 reg->def->instr->use_count++;
326
327 return true;
328 } else if (try_swap_cat3_two_srcs(instr, n, new_flags)) {
329 return true;
330 }
331 } else if ((is_same_type_mov(src) || is_const_mov(src)) &&
332 /* cannot collapse const/immed/etc into control flow: */
333 opc_cat(instr->opc) != 0) {
334 /* immed/const/etc cases, which require some special handling: */
335 struct ir3_register *src_reg = src->srcs[0];
336 unsigned new_flags = reg->flags;
337
338 if (src_reg->flags & IR3_REG_ARRAY)
339 return false;
340
341 combine_flags(&new_flags, src);
342
343 if (!ir3_valid_flags(instr, n, new_flags)) {
344 /* See if lowering an immediate to const would help. */
345 if (lower_immed(ctx, instr, n, src_reg, new_flags))
346 return true;
347
348 /* special case for "normal" mad instructions, we can
349 * try swapping the first two args if that fits better.
350 *
351 * the "plain" MAD's (ie. the ones that don't shift first
352 * src prior to multiply) can swap their first two srcs if
353 * src[0] is !CONST and src[1] is CONST:
354 */
355 if (try_swap_cat3_two_srcs(instr, n, new_flags)) {
356 return true;
357 } else {
358 return false;
359 }
360 }
361
362 /* Here we handle the special case of mov from
363 * CONST and/or RELATIV. These need to be handled
364 * specially, because in the case of move from CONST
365 * there is no src ir3_instruction so we need to
366 * replace the ir3_register. And in the case of
367 * RELATIV we need to handle the address register
368 * dependency.
369 */
370 if (src_reg->flags & IR3_REG_CONST) {
371 /* an instruction cannot reference two different
372 * address registers:
373 */
374 if ((src_reg->flags & IR3_REG_RELATIV) &&
375 conflicts(instr->address, reg->def->instr->address))
376 return false;
377
378 /* These macros expand to a mov in an if statement */
379 if ((src_reg->flags & IR3_REG_RELATIV) &&
380 is_subgroup_cond_mov_macro(instr))
381 return false;
382
383 /* This seems to be a hw bug, or something where the timings
384 * just somehow don't work out. This restriction may only
385 * apply if the first src is also CONST.
386 */
387 if ((opc_cat(instr->opc) == 3) && (n == 2) &&
388 (src_reg->flags & IR3_REG_RELATIV) && (src_reg->array.offset == 0))
389 return false;
390
391 /* When narrowing constant from 32b to 16b, it seems
392 * to work only for float. So we should do this only with
393 * float opcodes.
394 */
395 if (src->cat1.dst_type == TYPE_F16) {
396 /* TODO: should we have a way to tell phi/collect to use a
397 * float move so that this is legal?
398 */
399 if (is_meta(instr))
400 return false;
401 if (instr->opc == OPC_MOV && !type_float(instr->cat1.src_type))
402 return false;
403 if (!is_cat2_float(instr->opc) && !is_cat3_float(instr->opc))
404 return false;
405 } else if (src->cat1.dst_type == TYPE_U16 || src->cat1.dst_type == TYPE_S16) {
406 /* Since we set CONSTANT_DEMOTION_ENABLE, a float reference of
407 * what was a U16 value read from the constbuf would incorrectly
408 * do 32f->16f conversion, when we want to read a 16f value.
409 */
410 if (is_cat2_float(instr->opc) || is_cat3_float(instr->opc))
411 return false;
412 if (instr->opc == OPC_MOV && type_float(instr->cat1.src_type))
413 return false;
414 }
415
416 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
417 src_reg->flags = new_flags;
418 instr->srcs[n] = src_reg;
419
420 if (src_reg->flags & IR3_REG_RELATIV)
421 ir3_instr_set_address(instr, reg->def->instr->address->def->instr);
422
423 return true;
424 }
425
426 if (src_reg->flags & IR3_REG_IMMED) {
427 int32_t iim_val = src_reg->iim_val;
428
429 assert((opc_cat(instr->opc) == 1) ||
430 (opc_cat(instr->opc) == 2) ||
431 (is_cat3_alt(instr->opc) && (n == 0 || n == 2)) ||
432 (opc_cat(instr->opc) == 6) ||
433 is_meta(instr) ||
434 (instr->opc == OPC_ISAM && (n == 1 || n == 2)) ||
435 (is_mad(instr->opc) && (n == 0)));
436
437 if ((opc_cat(instr->opc) == 2) &&
438 !ir3_cat2_int(instr->opc)) {
439 iim_val = ir3_flut(src_reg);
440 if (iim_val < 0) {
441 /* Fall back to trying to load the immediate as a const: */
442 return lower_immed(ctx, instr, n, src_reg, new_flags);
443 }
444 }
445
446 if (new_flags & IR3_REG_SABS)
447 iim_val = abs(iim_val);
448
449 if (new_flags & IR3_REG_SNEG)
450 iim_val = -iim_val;
451
452 if (new_flags & IR3_REG_BNOT)
453 iim_val = ~iim_val;
454
455 if (ir3_valid_flags(instr, n, new_flags) &&
456 ir3_valid_immediate(instr, iim_val)) {
457 new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);
458 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
459 src_reg->flags = new_flags;
460 src_reg->iim_val = iim_val;
461 instr->srcs[n] = src_reg;
462
463 return true;
464 } else {
465 /* Fall back to trying to load the immediate as a const: */
466 return lower_immed(ctx, instr, n, src_reg, new_flags);
467 }
468 }
469 }
470
471 return false;
472 }
473
474 /* Handle special case of eliminating output mov, and similar cases where
475 * there isn't a normal "consuming" instruction. In this case we cannot
476 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
477 * be eliminated)
478 */
479 static struct ir3_instruction *
eliminate_output_mov(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr)480 eliminate_output_mov(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
481 {
482 if (is_eligible_mov(instr, NULL, false)) {
483 struct ir3_register *reg = instr->srcs[0];
484 if (!(reg->flags & IR3_REG_ARRAY)) {
485 struct ir3_instruction *src_instr = ssa(reg);
486 assert(src_instr);
487 ctx->progress = true;
488 return src_instr;
489 }
490 }
491 return instr;
492 }
493
494 /**
495 * Find instruction src's which are mov's that can be collapsed, replacing
496 * the mov dst with the mov src
497 */
498 static void
instr_cp(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr)499 instr_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
500 {
501 if (instr->srcs_count == 0)
502 return;
503
504 if (ir3_instr_check_mark(instr))
505 return;
506
507 /* walk down the graph from each src: */
508 bool progress;
509 do {
510 progress = false;
511 foreach_src_n (reg, n, instr) {
512 struct ir3_instruction *src = ssa(reg);
513
514 if (!src)
515 continue;
516
517 instr_cp(ctx, src);
518
519 /* TODO non-indirect access we could figure out which register
520 * we actually want and allow cp..
521 */
522 if ((reg->flags & IR3_REG_ARRAY) && src->opc != OPC_META_PHI)
523 continue;
524
525 /* Don't CP absneg into meta instructions, that won't end well: */
526 if (is_meta(instr) &&
527 (src->opc == OPC_ABSNEG_F || src->opc == OPC_ABSNEG_S))
528 continue;
529
530 /* Don't CP mova and mova1 into their users */
531 if (writes_addr0(src) || writes_addr1(src))
532 continue;
533
534 progress |= reg_cp(ctx, instr, reg, n);
535 ctx->progress |= progress;
536 }
537 } while (progress);
538
539 /* After folding a mov's source we may wind up with a type-converting mov
540 * of an immediate. This happens e.g. with texture descriptors, since we
541 * narrow the descriptor (which may be a constant) to a half-reg in ir3.
542 * By converting the immediate in-place to the destination type, we can
543 * turn the mov into a same-type mov so that it can be further propagated.
544 */
545 if (instr->opc == OPC_MOV && (instr->srcs[0]->flags & IR3_REG_IMMED) &&
546 instr->cat1.src_type != instr->cat1.dst_type &&
547 /* Only do uint types for now, until we generate other types of
548 * mov's during instruction selection.
549 */
550 full_type(instr->cat1.src_type) == TYPE_U32 &&
551 full_type(instr->cat1.dst_type) == TYPE_U32) {
552 uint32_t uimm = instr->srcs[0]->uim_val;
553 if (instr->cat1.dst_type == TYPE_U16)
554 uimm &= 0xffff;
555 instr->srcs[0]->uim_val = uimm;
556 if (instr->dsts[0]->flags & IR3_REG_HALF)
557 instr->srcs[0]->flags |= IR3_REG_HALF;
558 else
559 instr->srcs[0]->flags &= ~IR3_REG_HALF;
560 instr->cat1.src_type = instr->cat1.dst_type;
561 ctx->progress = true;
562 }
563
564 /* Handle converting a sam.s2en (taking samp/tex idx params via register)
565 * into a normal sam (encoding immediate samp/tex idx) if they are
566 * immediate. This saves some instructions and regs in the common case
567 * where we know samp/tex at compile time. This needs to be done in the
568 * frontend for bindless tex, though, so don't replicate it here.
569 */
570 if (is_tex(instr) && (instr->flags & IR3_INSTR_S2EN) &&
571 !(instr->flags & IR3_INSTR_B) &&
572 !(ir3_shader_debug & IR3_DBG_FORCES2EN)) {
573 /* The first src will be a collect, if both of it's
574 * two sources are mov from imm, then we can
575 */
576 struct ir3_instruction *samp_tex = ssa(instr->srcs[0]);
577
578 assert(samp_tex->opc == OPC_META_COLLECT);
579
580 struct ir3_register *tex = samp_tex->srcs[0];
581 struct ir3_register *samp = samp_tex->srcs[1];
582
583 if ((samp->flags & IR3_REG_IMMED) && (tex->flags & IR3_REG_IMMED) &&
584 (samp->iim_val < 16) && (tex->iim_val < 16)) {
585 instr->flags &= ~IR3_INSTR_S2EN;
586 instr->cat5.samp = samp->iim_val;
587 instr->cat5.tex = tex->iim_val;
588
589 /* shuffle around the regs to remove the first src: */
590 instr->srcs_count--;
591 for (unsigned i = 0; i < instr->srcs_count; i++) {
592 instr->srcs[i] = instr->srcs[i + 1];
593 }
594
595 ctx->progress = true;
596 }
597 }
598 }
599
600 bool
ir3_cp(struct ir3 * ir,struct ir3_shader_variant * so)601 ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so)
602 {
603 struct ir3_cp_ctx ctx = {
604 .shader = ir,
605 .so = so,
606 };
607
608 /* This is a bit annoying, and probably wouldn't be necessary if we
609 * tracked a reverse link from producing instruction to consumer.
610 * But we need to know when we've eliminated the last consumer of
611 * a mov, so we need to do a pass to first count consumers of a
612 * mov.
613 */
614 foreach_block (block, &ir->block_list) {
615 foreach_instr (instr, &block->instr_list) {
616
617 /* by the way, we don't account for false-dep's, so the CP
618 * pass should always happen before false-dep's are inserted
619 */
620 assert(instr->deps_count == 0);
621
622 foreach_ssa_src (src, instr) {
623 src->use_count++;
624 }
625 }
626 }
627
628 ir3_clear_mark(ir);
629
630 foreach_block (block, &ir->block_list) {
631 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
632 if (terminator)
633 instr_cp(&ctx, terminator);
634
635 for (unsigned i = 0; i < block->keeps_count; i++) {
636 instr_cp(&ctx, block->keeps[i]);
637 block->keeps[i] = eliminate_output_mov(&ctx, block->keeps[i]);
638 }
639 }
640
641 return ctx.progress;
642 }
643