1 /*
2 * Copyright © 2014 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
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 /**
213 * Handles the special case of the 2nd src (n == 1) to "normal" mad
214 * instructions, which cannot reference a constant. See if it is
215 * possible to swap the 1st and 2nd sources.
216 */
217 static bool
try_swap_mad_two_srcs(struct ir3_instruction * instr,unsigned new_flags)218 try_swap_mad_two_srcs(struct ir3_instruction *instr, unsigned new_flags)
219 {
220 if (!is_mad(instr->opc))
221 return false;
222
223 /* If we've already tried, nothing more to gain.. we will only
224 * have previously swapped if the original 2nd src was const or
225 * immed. So swapping back won't improve anything and could
226 * result in an infinite "progress" loop.
227 */
228 if (instr->cat3.swapped)
229 return false;
230
231 /* cat3 doesn't encode immediate, but we can lower immediate
232 * to const if that helps:
233 */
234 if (new_flags & IR3_REG_IMMED) {
235 new_flags &= ~IR3_REG_IMMED;
236 new_flags |= IR3_REG_CONST;
237 }
238
239 /* If the reason we couldn't fold without swapping is something
240 * other than const source, then swapping won't help:
241 */
242 if (!(new_flags & (IR3_REG_CONST | IR3_REG_SHARED)))
243 return false;
244
245 instr->cat3.swapped = true;
246
247 /* NOTE: pre-swap first two src's before valid_flags(),
248 * which might try to dereference the n'th src:
249 */
250 swap(instr->srcs[0], instr->srcs[1]);
251
252 bool valid_swap =
253 /* can we propagate mov if we move 2nd src to first? */
254 ir3_valid_flags(instr, 0, new_flags) &&
255 /* and does first src fit in second slot? */
256 ir3_valid_flags(instr, 1, instr->srcs[1]->flags);
257
258 if (!valid_swap) {
259 /* put things back the way they were: */
260 swap(instr->srcs[0], instr->srcs[1]);
261 } /* otherwise leave things swapped */
262
263 return valid_swap;
264 }
265
266 /**
267 * Handle cp for a given src register. This additionally handles
268 * the cases of collapsing immedate/const (which replace the src
269 * register with a non-ssa src) or collapsing mov's from relative
270 * src (which needs to also fixup the address src reference by the
271 * instruction).
272 */
273 static bool
reg_cp(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr,struct ir3_register * reg,unsigned n)274 reg_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr,
275 struct ir3_register *reg, unsigned n)
276 {
277 struct ir3_instruction *src = ssa(reg);
278
279 if (is_eligible_mov(src, instr, true)) {
280 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
281 struct ir3_register *src_reg = src->srcs[0];
282 unsigned new_flags = reg->flags;
283
284 combine_flags(&new_flags, src);
285
286 if (ir3_valid_flags(instr, n, new_flags)) {
287 if (new_flags & IR3_REG_ARRAY) {
288 assert(!(reg->flags & IR3_REG_ARRAY));
289 reg->array = src_reg->array;
290 }
291 reg->flags = new_flags;
292 reg->def = src_reg->def;
293
294 instr->barrier_class |= src->barrier_class;
295 instr->barrier_conflict |= src->barrier_conflict;
296
297 unuse(src);
298 reg->def->instr->use_count++;
299
300 return true;
301 } else if (n == 1 && try_swap_mad_two_srcs(instr, new_flags)) {
302 return true;
303 }
304 } else if ((is_same_type_mov(src) || is_const_mov(src)) &&
305 /* cannot collapse const/immed/etc into control flow: */
306 opc_cat(instr->opc) != 0) {
307 /* immed/const/etc cases, which require some special handling: */
308 struct ir3_register *src_reg = src->srcs[0];
309 unsigned new_flags = reg->flags;
310
311 if (src_reg->flags & IR3_REG_ARRAY)
312 return false;
313
314 combine_flags(&new_flags, src);
315
316 if (!ir3_valid_flags(instr, n, new_flags)) {
317 /* See if lowering an immediate to const would help. */
318 if (lower_immed(ctx, instr, n, src_reg, new_flags))
319 return true;
320
321 /* special case for "normal" mad instructions, we can
322 * try swapping the first two args if that fits better.
323 *
324 * the "plain" MAD's (ie. the ones that don't shift first
325 * src prior to multiply) can swap their first two srcs if
326 * src[0] is !CONST and src[1] is CONST:
327 */
328 if ((n == 1) && try_swap_mad_two_srcs(instr, new_flags)) {
329 return true;
330 } else {
331 return false;
332 }
333 }
334
335 /* Here we handle the special case of mov from
336 * CONST and/or RELATIV. These need to be handled
337 * specially, because in the case of move from CONST
338 * there is no src ir3_instruction so we need to
339 * replace the ir3_register. And in the case of
340 * RELATIV we need to handle the address register
341 * dependency.
342 */
343 if (src_reg->flags & IR3_REG_CONST) {
344 /* an instruction cannot reference two different
345 * address registers:
346 */
347 if ((src_reg->flags & IR3_REG_RELATIV) &&
348 conflicts(instr->address, reg->def->instr->address))
349 return false;
350
351 /* These macros expand to a mov in an if statement */
352 if ((src_reg->flags & IR3_REG_RELATIV) &&
353 is_subgroup_cond_mov_macro(instr))
354 return false;
355
356 /* This seems to be a hw bug, or something where the timings
357 * just somehow don't work out. This restriction may only
358 * apply if the first src is also CONST.
359 */
360 if ((opc_cat(instr->opc) == 3) && (n == 2) &&
361 (src_reg->flags & IR3_REG_RELATIV) && (src_reg->array.offset == 0))
362 return false;
363
364 /* When narrowing constant from 32b to 16b, it seems
365 * to work only for float. So we should do this only with
366 * float opcodes.
367 */
368 if (src->cat1.dst_type == TYPE_F16) {
369 /* TODO: should we have a way to tell phi/collect to use a
370 * float move so that this is legal?
371 */
372 if (is_meta(instr))
373 return false;
374 if (instr->opc == OPC_MOV && !type_float(instr->cat1.src_type))
375 return false;
376 if (!is_cat2_float(instr->opc) && !is_cat3_float(instr->opc))
377 return false;
378 } else if (src->cat1.dst_type == TYPE_U16 || src->cat1.dst_type == TYPE_S16) {
379 /* Since we set CONSTANT_DEMOTION_ENABLE, a float reference of
380 * what was a U16 value read from the constbuf would incorrectly
381 * do 32f->16f conversion, when we want to read a 16f value.
382 */
383 if (is_cat2_float(instr->opc) || is_cat3_float(instr->opc))
384 return false;
385 if (instr->opc == OPC_MOV && type_float(instr->cat1.src_type))
386 return false;
387 }
388
389 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
390 src_reg->flags = new_flags;
391 instr->srcs[n] = src_reg;
392
393 if (src_reg->flags & IR3_REG_RELATIV)
394 ir3_instr_set_address(instr, reg->def->instr->address->def->instr);
395
396 return true;
397 }
398
399 if (src_reg->flags & IR3_REG_IMMED) {
400 int32_t iim_val = src_reg->iim_val;
401
402 assert((opc_cat(instr->opc) == 1) ||
403 (opc_cat(instr->opc) == 2) ||
404 (opc_cat(instr->opc) == 6) ||
405 is_meta(instr) ||
406 (instr->opc == OPC_ISAM && (n == 1 || n == 2)) ||
407 (is_mad(instr->opc) && (n == 0)));
408
409 if ((opc_cat(instr->opc) == 2) &&
410 !ir3_cat2_int(instr->opc)) {
411 iim_val = ir3_flut(src_reg);
412 if (iim_val < 0) {
413 /* Fall back to trying to load the immediate as a const: */
414 return lower_immed(ctx, instr, n, src_reg, new_flags);
415 }
416 }
417
418 if (new_flags & IR3_REG_SABS)
419 iim_val = abs(iim_val);
420
421 if (new_flags & IR3_REG_SNEG)
422 iim_val = -iim_val;
423
424 if (new_flags & IR3_REG_BNOT)
425 iim_val = ~iim_val;
426
427 if (ir3_valid_flags(instr, n, new_flags) &&
428 ir3_valid_immediate(instr, iim_val)) {
429 new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);
430 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
431 src_reg->flags = new_flags;
432 src_reg->iim_val = iim_val;
433 instr->srcs[n] = src_reg;
434
435 return true;
436 } else {
437 /* Fall back to trying to load the immediate as a const: */
438 return lower_immed(ctx, instr, n, src_reg, new_flags);
439 }
440 }
441 }
442
443 return false;
444 }
445
446 /* Handle special case of eliminating output mov, and similar cases where
447 * there isn't a normal "consuming" instruction. In this case we cannot
448 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
449 * be eliminated)
450 */
451 static struct ir3_instruction *
eliminate_output_mov(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr)452 eliminate_output_mov(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
453 {
454 if (is_eligible_mov(instr, NULL, false)) {
455 struct ir3_register *reg = instr->srcs[0];
456 if (!(reg->flags & IR3_REG_ARRAY)) {
457 struct ir3_instruction *src_instr = ssa(reg);
458 assert(src_instr);
459 ctx->progress = true;
460 return src_instr;
461 }
462 }
463 return instr;
464 }
465
466 /**
467 * Find instruction src's which are mov's that can be collapsed, replacing
468 * the mov dst with the mov src
469 */
470 static void
instr_cp(struct ir3_cp_ctx * ctx,struct ir3_instruction * instr)471 instr_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
472 {
473 if (instr->srcs_count == 0)
474 return;
475
476 if (ir3_instr_check_mark(instr))
477 return;
478
479 /* walk down the graph from each src: */
480 bool progress;
481 do {
482 progress = false;
483 foreach_src_n (reg, n, instr) {
484 struct ir3_instruction *src = ssa(reg);
485
486 if (!src)
487 continue;
488
489 instr_cp(ctx, src);
490
491 /* TODO non-indirect access we could figure out which register
492 * we actually want and allow cp..
493 */
494 if ((reg->flags & IR3_REG_ARRAY) && src->opc != OPC_META_PHI)
495 continue;
496
497 /* Don't CP absneg into meta instructions, that won't end well: */
498 if (is_meta(instr) &&
499 (src->opc == OPC_ABSNEG_F || src->opc == OPC_ABSNEG_S))
500 continue;
501
502 /* Don't CP mova and mova1 into their users */
503 if (writes_addr0(src) || writes_addr1(src))
504 continue;
505
506 progress |= reg_cp(ctx, instr, reg, n);
507 ctx->progress |= progress;
508 }
509 } while (progress);
510
511 /* After folding a mov's source we may wind up with a type-converting mov
512 * of an immediate. This happens e.g. with texture descriptors, since we
513 * narrow the descriptor (which may be a constant) to a half-reg in ir3.
514 * By converting the immediate in-place to the destination type, we can
515 * turn the mov into a same-type mov so that it can be further propagated.
516 */
517 if (instr->opc == OPC_MOV && (instr->srcs[0]->flags & IR3_REG_IMMED) &&
518 instr->cat1.src_type != instr->cat1.dst_type &&
519 /* Only do uint types for now, until we generate other types of
520 * mov's during instruction selection.
521 */
522 full_type(instr->cat1.src_type) == TYPE_U32 &&
523 full_type(instr->cat1.dst_type) == TYPE_U32) {
524 uint32_t uimm = instr->srcs[0]->uim_val;
525 if (instr->cat1.dst_type == TYPE_U16)
526 uimm &= 0xffff;
527 instr->srcs[0]->uim_val = uimm;
528 if (instr->dsts[0]->flags & IR3_REG_HALF)
529 instr->srcs[0]->flags |= IR3_REG_HALF;
530 else
531 instr->srcs[0]->flags &= ~IR3_REG_HALF;
532 instr->cat1.src_type = instr->cat1.dst_type;
533 ctx->progress = true;
534 }
535
536 /* Handle converting a sam.s2en (taking samp/tex idx params via register)
537 * into a normal sam (encoding immediate samp/tex idx) if they are
538 * immediate. This saves some instructions and regs in the common case
539 * where we know samp/tex at compile time. This needs to be done in the
540 * frontend for bindless tex, though, so don't replicate it here.
541 */
542 if (is_tex(instr) && (instr->flags & IR3_INSTR_S2EN) &&
543 !(instr->flags & IR3_INSTR_B) &&
544 !(ir3_shader_debug & IR3_DBG_FORCES2EN)) {
545 /* The first src will be a collect, if both of it's
546 * two sources are mov from imm, then we can
547 */
548 struct ir3_instruction *samp_tex = ssa(instr->srcs[0]);
549
550 assert(samp_tex->opc == OPC_META_COLLECT);
551
552 struct ir3_register *samp = samp_tex->srcs[0];
553 struct ir3_register *tex = samp_tex->srcs[1];
554
555 if ((samp->flags & IR3_REG_IMMED) && (tex->flags & IR3_REG_IMMED) &&
556 (samp->iim_val < 16) && (tex->iim_val < 16)) {
557 instr->flags &= ~IR3_INSTR_S2EN;
558 instr->cat5.samp = samp->iim_val;
559 instr->cat5.tex = tex->iim_val;
560
561 /* shuffle around the regs to remove the first src: */
562 instr->srcs_count--;
563 for (unsigned i = 0; i < instr->srcs_count; i++) {
564 instr->srcs[i] = instr->srcs[i + 1];
565 }
566
567 ctx->progress = true;
568 }
569 }
570 }
571
572 bool
ir3_cp(struct ir3 * ir,struct ir3_shader_variant * so)573 ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so)
574 {
575 struct ir3_cp_ctx ctx = {
576 .shader = ir,
577 .so = so,
578 };
579
580 /* This is a bit annoying, and probably wouldn't be necessary if we
581 * tracked a reverse link from producing instruction to consumer.
582 * But we need to know when we've eliminated the last consumer of
583 * a mov, so we need to do a pass to first count consumers of a
584 * mov.
585 */
586 foreach_block (block, &ir->block_list) {
587 foreach_instr (instr, &block->instr_list) {
588
589 /* by the way, we don't account for false-dep's, so the CP
590 * pass should always happen before false-dep's are inserted
591 */
592 assert(instr->deps_count == 0);
593
594 foreach_ssa_src (src, instr) {
595 src->use_count++;
596 }
597 }
598 }
599
600 ir3_clear_mark(ir);
601
602 foreach_block (block, &ir->block_list) {
603 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
604 if (terminator)
605 instr_cp(&ctx, terminator);
606
607 for (unsigned i = 0; i < block->keeps_count; i++) {
608 instr_cp(&ctx, block->keeps[i]);
609 block->keeps[i] = eliminate_output_mov(&ctx, block->keeps[i]);
610 }
611 }
612
613 return ctx.progress;
614 }
615