Debian Codegraph @

src/asahi/compiler/agx_optimizer.c

raw
1 /*
2 * Copyright 2021 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/lut.h"
7 #include "util/macros.h"
8 #include "agx_builder.h"
9 #include "agx_compiler.h"
10 #include "agx_opcodes.h"
11
12 /* AGX peephole optimizer responsible for instruction combining. It operates in
13 * a forward direction and a backward direction, in each case traversing in
14 * source order. SSA means the forward pass satisfies the invariant:
15 *
16 * Every def is visited before any of its uses.
17 *
18 * Dually, the backend pass satisfies the invariant:
19 *
20 * Every use of a def is visited before the def.
21 *
22 * This means the forward pass can propagate modifiers forward, whereas the
23 * backwards pass propagates modifiers backward. Consider an example:
24 *
25 * 1 = fabs 0
26 * 2 = fround 1
27 * 3 = fsat 1
28 *
29 * The forwards pass would propagate the fabs to the fround (since we can
30 * lookup the fabs from the fround source and do the replacement). By contrast
31 * the backwards pass would propagate the fsat back to the fround (since when
32 * we see the fround we know it has only a single user, fsat). Propagatable
33 * instruction have natural directions (like pushforwards and pullbacks).
34 *
35 * We are careful to update the tracked state whenever we modify an instruction
36 * to ensure the passes are linear-time and converge in a single iteration.
37 *
38 * Size conversions are worth special discussion. Consider the snippet:
39 *
40 * 2 = fadd 0, 1
41 * 3 = f2f16 2
42 * 4 = fround 3
43 *
44 * A priori, we can move the f2f16 in either direction. But it's not equal --
45 * if we move it up to the fadd, we get FP16 for two instructions, whereas if
46 * we push it into the fround, we effectively get FP32 for two instructions. So
47 * f2f16 is backwards. Likewise, consider
48 *
49 * 2 = fadd 0, 1
50 * 3 = f2f32 1
51 * 4 = fround 3
52 *
53 * This time if we move f2f32 up to the fadd, we get FP32 for two, but if we
54 * move it down to the fround, we get FP16 to too. So f2f32 is backwards.
55 */
56
57 static bool
58 agx_is_fmov(agx_instr *def)
59 {
60 return (def->op == AGX_OPCODE_FADD || def->op == AGX_OPCODE_HADD) &&
61 agx_is_equiv(def->src[1], agx_negzero());
62 }
63
64 /* Compose floating-point modifiers with floating-point sources */
65
66 static agx_index
67 agx_compose_float_src(agx_index to, agx_index from)
68 {
69 if (to.abs) {
70 from.neg = false;
71 from.abs = true;
72 }
73
74 from.neg ^= to.neg;
75
76 return from;
77 }
78
79 static void
80 agx_optimizer_fmov(agx_instr **defs, agx_instr *ins)
81 {
82 agx_foreach_ssa_src(ins, s) {
83 agx_index src = ins->src[s];
84 agx_instr *def = defs[src.value];
85
86 if (def == NULL)
87 continue; /* happens for phis in loops */
88 if (!agx_is_fmov(def))
89 continue;
90 if (def->saturate)
91 continue;
92 if (ins->op == AGX_OPCODE_FCMPSEL && s >= 2)
93 continue;
94
95 /* We can fold f2f32 into 32-bit instructions, but we can't fold f2f16
96 * into 16-bit instructions, since the latter would implicitly promote to
97 * a 32-bit instruction which is not exact.
98 */
99 assert(def->src[0].size == AGX_SIZE_32 ||
100 def->src[0].size == AGX_SIZE_16);
101 assert(src.size == AGX_SIZE_32 || src.size == AGX_SIZE_16);
102
103 if (src.size == AGX_SIZE_16 && def->src[0].size == AGX_SIZE_32)
104 continue;
105
106 ins->src[s] = agx_compose_float_src(src, def->src[0]);
107 }
108 }
109
110 static bool
111 image_write_source_can_be_immediate(agx_instr *I, unsigned s)
112 {
113 bool block = I->op == AGX_OPCODE_BLOCK_IMAGE_STORE;
114 assert(I->op == AGX_OPCODE_IMAGE_WRITE || block);
115
116 /* LOD can always be immediate. Actually, it's just zero so far, we don't
117 * support nonzero LOD for images yet.
118 */
119 if (s == 2 && !block)
120 return true;
121
122 /* If the "bindless" source (source 3) is an immediate, it means we don't
123 * have a bindless image, instead we have a texture state index. We're
124 * allowed to have immediate texture state registers (source 4). However,
125 * we're not allowed to have immediate bindless offsets (also source 4).
126 */
127 unsigned base = block ? 0 : 3;
128 bool is_texture_state = (I->src[base].type == AGX_INDEX_IMMEDIATE);
129 if (s == (base + 1) && is_texture_state)
130 return true;
131
132 /* Otherwise, must be from a register */
133 return false;
134 }
135
136 static void
137 agx_optimizer_inline_imm(agx_instr **defs, agx_instr *I)
138 {
139 agx_foreach_ssa_src(I, s) {
140 agx_index src = I->src[s];
141 if (src.neg)
142 continue;
143
144 agx_instr *def = defs[src.value];
145 if (!def || def->op != AGX_OPCODE_MOV_IMM)
146 continue;
147
148 uint8_t value = def->imm;
149 uint16_t value_u16 = def->imm;
150
151 bool float_src = agx_is_float_src(I, s);
152
153 if (I->op == AGX_OPCODE_ST_TILE && s == 0)
154 continue;
155 if (I->op == AGX_OPCODE_ZS_EMIT && s != 0)
156 continue;
157 if (I->op == AGX_OPCODE_TEXTURE_SAMPLE && s != 4)
158 continue;
159 if ((I->op == AGX_OPCODE_DEVICE_STORE ||
160 I->op == AGX_OPCODE_LOCAL_STORE || I->op == AGX_OPCODE_ATOMIC ||
161 I->op == AGX_OPCODE_LOCAL_ATOMIC) &&
162 s != 2)
163 continue;
164 if (I->op == AGX_OPCODE_ST_VARY && s != 0)
165 continue;
166 if ((I->op == AGX_OPCODE_LOCAL_LOAD || I->op == AGX_OPCODE_DEVICE_LOAD ||
167 I->op == AGX_OPCODE_STACK_STORE) &&
168 s != 1)
169 continue;
170 if (I->op == AGX_OPCODE_SPLIT)
171 continue;
172
173 if ((I->op == AGX_OPCODE_IMAGE_WRITE ||
174 I->op == AGX_OPCODE_BLOCK_IMAGE_STORE) &&
175 !image_write_source_can_be_immediate(I, s))
176 continue;
177
178 if (float_src) {
179 bool fp16 = (def->dest[0].size == AGX_SIZE_16);
180 assert(fp16 || (def->dest[0].size == AGX_SIZE_32));
181
182 float f = fp16 ? _mesa_half_to_float(def->imm) : uif(def->imm);
183 if (!agx_minifloat_exact(f))
184 continue;
185
186 I->src[s] = agx_immediate_f(f);
187 } else if (value == def->imm) {
188 I->src[s] = agx_immediate(value);
189 } else if (value_u16 == def->imm && agx_allows_16bit_immediate(I)) {
190 I->src[s] = agx_abs(agx_immediate(value_u16));
191 } else if ((I->op == AGX_OPCODE_IADD || I->op == AGX_OPCODE_IMAD) &&
192 s == agx_negate_src_index(I)) {
193 unsigned bits = agx_size_align_16(def->dest[0].size) * 16;
194 uint64_t mask = BITFIELD64_MASK(bits);
195 uint64_t negated = (-def->imm) & mask;
196 value = negated;
197
198 /* Try to negate the immediate */
199 if (value == negated) {
200 I->src[s] = agx_neg(agx_immediate(value));
201 }
202 }
203 }
204 }
205
206 /*
207 * Fuse not into and/or/xor. Specifically, acts on not and fuses:
208 *
209 * not(and(x, y) -> nand(x, y)
210 * not(or(x, y) -> nor(x, y)
211 * not(xor(x, y) -> xnor(x, y)
212 */
213 static bool
214 agx_optimizer_not(agx_instr *I, agx_instr *use)
215 {
216 /* Check for bit op and use of not op */
217 if (I->op != AGX_OPCODE_BITOP || use->op != AGX_OPCODE_NOT)
218 return false;
219
220 /* Remap operation to the appropriate one */
221 I->truth_table ^= 0xF;
222 I->dest[0] = use->dest[0];
223
224 return true;
225 }
226
227 static bool
228 agx_optimizer_fmov_rev(agx_instr *I, agx_instr *use)
229 {
230 if (!agx_is_fmov(use))
231 return false;
232 if (use->src[0].neg || use->src[0].abs)
233 return false;
234
235 /* We can fold f2f16 into 32-bit instructions, but we can't fold f2f32 into
236 * 16-bit instructions, since the latter would implicitly promote to a 32-bit
237 * instruction which is not exact.
238 */
239 assert(use->dest[0].size == AGX_SIZE_32 || use->dest[0].size == AGX_SIZE_16);
240 assert(I->dest[0].size == AGX_SIZE_32 || I->dest[0].size == AGX_SIZE_16);
241
242 if (I->dest[0].size == AGX_SIZE_16 && use->dest[0].size == AGX_SIZE_32)
243 return false;
244
245 /* saturate(saturate(x)) = saturate(x) */
246 I->saturate |= use->saturate;
247 I->dest[0] = use->dest[0];
248 return true;
249 }
250
251 static bool
252 agx_icond_is_unsigned(enum agx_icond cond)
253 {
254 switch (cond) {
255 case AGX_ICOND_UEQ:
256 case AGX_ICOND_ULT:
257 case AGX_ICOND_UGT:
258 return true;
259
260 case AGX_ICOND_SEQ:
261 case AGX_ICOND_SLT:
262 case AGX_ICOND_SGT:
263 return false;
264 }
265
266 UNREACHABLE("invalid condition");
267 }
268
269 static bool
270 agx_supports_zext(agx_instr *I, unsigned s)
271 {
272 switch (I->op) {
273 case AGX_OPCODE_IADD:
274 case AGX_OPCODE_IMAD:
275 case AGX_OPCODE_INTL:
276 case AGX_OPCODE_FFS:
277 case AGX_OPCODE_BITREV:
278 case AGX_OPCODE_BFI:
279 case AGX_OPCODE_BFEIL:
280 case AGX_OPCODE_EXTR:
281 case AGX_OPCODE_BITOP:
282 case AGX_OPCODE_WHILE_ICMP:
283 case AGX_OPCODE_IF_ICMP:
284 case AGX_OPCODE_ELSE_ICMP:
285 case AGX_OPCODE_BREAK_IF_ICMP:
286 case AGX_OPCODE_ICMP_BALLOT:
287 case AGX_OPCODE_ICMP_QUAD_BALLOT:
288 return true;
289
290 case AGX_OPCODE_ICMP:
291 case AGX_OPCODE_ICMPSEL:
292 /* Only the comparisons can be extended, not the selection. And we can
293 * only zero-extend with unsigned comparison. Presumably the hardware
294 * sign-extends with signed comparisons but we don't handle that yet.
295 */
296 return (s < 2) && agx_icond_is_unsigned(I->icond);
297
298 default:
299 return false;
300 }
301 }
302
303 static void
304 agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I)
305 {
306 agx_foreach_ssa_src(I, s) {
307 agx_index src = I->src[s];
308 agx_instr *def = defs[src.value];
309
310 if (def == NULL)
311 continue; /* happens for phis in loops */
312 if (def->op != AGX_OPCODE_MOV)
313 continue;
314
315 /* At the moment, not all instructions support size conversions. Notably
316 * RA pseudo instructions don't handle size conversions. This should be
317 * refined in the future.
318 */
319 if (def->src[0].size != src.size &&
320 !(def->src[0].size < src.size && agx_supports_zext(I, s)))
321 continue;
322
323 /* Optimize split(64-bit uniform) so we can get better copyprop of the
324 * 32-bit uniform parts. This helps reduce moves with 64-bit uniforms.
325 */
326 if (I->op == AGX_OPCODE_SPLIT && def->src[0].type == AGX_INDEX_UNIFORM &&
327 src.size == AGX_SIZE_64 && I->dest[0].size == AGX_SIZE_32) {
328
329 assert(I->nr_dests == 2 && "decomposing a 64-bit scalar");
330 agx_builder b = agx_init_builder(ctx, agx_before_instr(I));
331
332 agx_index lo = def->src[0];
333 lo.size = AGX_SIZE_32;
334
335 agx_index hi = lo;
336 hi.value += 2 /* half of 64-bits = 32-bits = 2 x 16-bits */;
337
338 defs[I->dest[0].value] = agx_mov_to(&b, I->dest[0], lo);
339 defs[I->dest[1].value] = agx_mov_to(&b, I->dest[1], hi);
340
341 agx_remove_instruction(I);
342 continue;
343 }
344
345 /* Immediate inlining happens elsewhere */
346 if (def->src[0].type == AGX_INDEX_IMMEDIATE)
347 continue;
348
349 /* ALU instructions cannot take 64-bit */
350 if (def->src[0].size == AGX_SIZE_64 &&
351 !(I->op == AGX_OPCODE_DEVICE_LOAD && s == 0) &&
352 !(I->op == AGX_OPCODE_DEVICE_STORE && s == 1) &&
353 !(I->op == AGX_OPCODE_ATOMIC && s == 1))
354 continue;
355
356 agx_replace_src(I, s, def->src[0]);
357
358 /* If we are zero-extending into an instruction that distinguishes sign
359 * and zero extend, make sure we pick zero-extend.
360 */
361 if (def->src[0].size < src.size &&
362 (I->op == AGX_OPCODE_IMAD || I->op == AGX_OPCODE_IADD)) {
363
364 assert(agx_supports_zext(I, s));
365 I->src[s].abs = true;
366 }
367 }
368 }
369
370 /*
371 * Fuse conditions into if. Specifically, acts on if_icmp and fuses:
372 *
373 * if_icmp(cmp(x, y, *), 0, ne/eq) -> if_cmp(x, y, *)
374 */
375 static void
376 agx_optimizer_if_cmp(agx_instr **defs, agx_instr *I)
377 {
378 /* Check for unfused if */
379 if (!agx_is_equiv(I->src[1], agx_zero()) || I->icond != AGX_ICOND_UEQ ||
380 I->src[0].type != AGX_INDEX_NORMAL)
381 return;
382
383 /* Check for condition */
384 agx_instr *def = defs[I->src[0].value];
385 if (def->op != AGX_OPCODE_ICMP && def->op != AGX_OPCODE_FCMP)
386 return;
387
388 /* Fuse */
389 I->src[0] = def->src[0];
390 I->src[1] = def->src[1];
391 I->invert_cond = def->invert_cond ^ !I->invert_cond;
392
393 if (def->op == AGX_OPCODE_ICMP) {
394 I->op = AGX_OPCODE_IF_ICMP;
395 I->icond = def->icond;
396 } else {
397 I->op = AGX_OPCODE_IF_FCMP;
398 I->fcond = def->fcond;
399 }
400 }
401
402 /*
403 * Fuse invert into if. Acts on if_icmp and fuses:
404 *
405 * if_icmp(xor(x, 1), 0, ne) -> if_cmp(x, 0, eq)
406 */
407 static void
408 agx_optimizer_if_not(agx_instr **defs, agx_instr *I)
409 {
410 /* Check for unfused if */
411 if (!agx_is_equiv(I->src[1], agx_zero()) || I->icond != AGX_ICOND_UEQ ||
412 I->src[0].type != AGX_INDEX_NORMAL)
413 return;
414
415 /* Check for invert */
416 agx_instr *def = defs[I->src[0].value];
417 if (def->op != AGX_OPCODE_BITOP ||
418 !agx_is_equiv(def->src[1], agx_immediate(1)) ||
419 def->truth_table != UTIL_LUT2(a ^ b))
420 return;
421
422 /* Fuse */
423 I->src[0] = def->src[0];
424 I->invert_cond = !I->invert_cond;
425 }
426
427 /*
428 * Fuse conditions into select. Specifically, acts on icmpsel and fuses:
429 *
430 * icmpsel(cmp(x, y, *), 0, z, w, eq) -> cmpsel(x, y, w, z, *)
431 *
432 * Care must be taken to invert the condition by swapping cmpsel arguments.
433 */
434 static void
435 agx_optimizer_cmpsel(agx_instr **defs, agx_instr *I)
436 {
437 /* Check for unfused select */
438 if (!agx_is_equiv(I->src[1], agx_zero()) || I->icond != AGX_ICOND_UEQ ||
439 I->src[0].type != AGX_INDEX_NORMAL)
440 return;
441
442 /* Check for condition */
443 agx_instr *def = defs[I->src[0].value];
444 if (def->op != AGX_OPCODE_ICMP && def->op != AGX_OPCODE_FCMP)
445 return;
446
447 /* Fuse */
448 I->src[0] = def->src[0];
449 I->src[1] = def->src[1];
450
451 /* In the unfused select, the condition is inverted due to the form:
452 *
453 * (cond == 0) ? x : y
454 *
455 * So we need to swap the arguments when fusing to become cond ? y : x. If
456 * the condition was supposed to be inverted, we don't swap since it's
457 * already inverted. cmpsel does not have an invert_cond bit to use.
458 */
459 if (!def->invert_cond) {
460 agx_index temp = I->src[2];
461 I->src[2] = I->src[3];
462 I->src[3] = temp;
463 }
464
465 if (def->op == AGX_OPCODE_ICMP) {
466 I->op = AGX_OPCODE_ICMPSEL;
467 I->icond = def->icond;
468 } else {
469 I->op = AGX_OPCODE_FCMPSEL;
470 I->fcond = def->fcond;
471 }
472 }
473
474 /*
475 * Fuse conditions into ballots:
476 *
477 * ballot(cmp(x, y)) -> ballot_cmp(x, y)
478 */
479 static void
480 agx_optimizer_ballot(agx_context *ctx, agx_instr **defs, agx_instr *I)
481 {
482 if (I->src[0].type != AGX_INDEX_NORMAL)
483 return;
484
485 agx_instr *def = defs[I->src[0].value];
486 if (!def || (def->op != AGX_OPCODE_ICMP && def->op != AGX_OPCODE_FCMP))
487 return;
488
489 bool quad = I->op == AGX_OPCODE_QUAD_BALLOT;
490 assert(quad || I->op == AGX_OPCODE_BALLOT);
491
492 /* Replace with a fused instruction since the # of sources changes */
493 agx_builder b = agx_init_builder(ctx, agx_before_instr(I));
494
495 agx_instr *fused = agx_icmp_ballot_to(
496 &b, I->dest[0], def->src[0], def->src[1], def->icond, def->invert_cond);
497
498 if (def->op == AGX_OPCODE_ICMP) {
499 fused->op = quad ? AGX_OPCODE_ICMP_QUAD_BALLOT : AGX_OPCODE_ICMP_BALLOT;
500 } else {
501 fused->op = quad ? AGX_OPCODE_FCMP_QUAD_BALLOT : AGX_OPCODE_FCMP_BALLOT;
502 }
503
504 agx_remove_instruction(I);
505 }
506
507 /*
508 * Fuse not srcs into bitop.
509 */
510 static void
511 agx_optimizer_bitop(agx_instr **defs, agx_instr *I)
512 {
513 agx_foreach_ssa_src(I, s) {
514 agx_index src = I->src[s];
515 agx_instr *def = defs[src.value];
516
517 /* If we find a not, select new operation and fuse */
518 if (def->op == AGX_OPCODE_NOT) {
519 I->truth_table = util_lut2_invert_source(I->truth_table, s);
520 I->src[s] = def->src[0];
521 }
522 }
523 }
524
525 /*
526 * Fuse sign-extends into addition-like instructions:
527 */
528 static void
529 agx_optimizer_signext(agx_instr **defs, agx_instr *I)
530 {
531 agx_foreach_ssa_src(I, s) {
532 agx_index src = I->src[s];
533 agx_instr *def = defs[src.value];
534
535 if (def == NULL || def->op != AGX_OPCODE_SIGNEXT)
536 continue;
537
538 agx_replace_src(I, s, def->src[0]);
539 assert(!I->src[s].abs && "sign-extended");
540 }
541 }
542
543 void
544 agx_optimizer_forward(agx_context *ctx)
545 {
546 agx_instr **defs = calloc(ctx->alloc, sizeof(*defs));
547
548 agx_foreach_instr_global_safe(ctx, I) {
549 struct agx_opcode_info info = agx_opcodes_info[I->op];
550
551 agx_foreach_ssa_dest(I, d) {
552 defs[I->dest[d].value] = I;
553 }
554
555 /* Optimize moves */
556 agx_optimizer_copyprop(ctx, defs, I);
557
558 /* Propagate fmov down */
559 if (info.is_float || I->op == AGX_OPCODE_FCMPSEL ||
560 I->op == AGX_OPCODE_FCMP)
561 agx_optimizer_fmov(defs, I);
562
563 /* Inline immediates if we can. TODO: systematic */
564 if (I->op != AGX_OPCODE_COLLECT && I->op != AGX_OPCODE_IMAGE_LOAD &&
565 I->op != AGX_OPCODE_TEXTURE_LOAD &&
566 I->op != AGX_OPCODE_UNIFORM_STORE && I->op != AGX_OPCODE_EXPORT)
567 agx_optimizer_inline_imm(defs, I);
568
569 if (I->op == AGX_OPCODE_IF_ICMP) {
570 agx_optimizer_if_not(defs, I);
571 agx_optimizer_if_cmp(defs, I);
572 } else if (I->op == AGX_OPCODE_ICMPSEL) {
573 agx_optimizer_cmpsel(defs, I);
574 } else if (I->op == AGX_OPCODE_BALLOT ||
575 I->op == AGX_OPCODE_QUAD_BALLOT) {
576 agx_optimizer_ballot(ctx, defs, I);
577 } else if (I->op == AGX_OPCODE_BITOP) {
578 agx_optimizer_bitop(defs, I);
579 } else if (I->op == AGX_OPCODE_IADD || I->op == AGX_OPCODE_IMAD) {
580 agx_optimizer_signext(defs, I);
581 }
582 }
583
584 free(defs);
585 }
586
587 static void
588 record_use(agx_instr **uses, BITSET_WORD *multiple, agx_instr *I, unsigned src)
589 {
590 unsigned v = I->src[src].value;
591
592 if (uses[v])
593 BITSET_SET(multiple, v);
594 else
595 uses[v] = I;
596 }
597
598 void
599 agx_optimizer_backward(agx_context *ctx)
600 {
601 agx_instr **uses = calloc(ctx->alloc, sizeof(*uses));
602 BITSET_WORD *multiple = BITSET_CALLOC(ctx->alloc);
603
604 agx_foreach_block_rev(ctx, block) {
605 /* Phi sources are logically read at the end of predecessor, so process
606 * our source in our successors' phis firsts. This ensures we set
607 * `multiple` correctly with phi sources.
608 */
609 agx_foreach_successor(block, succ) {
610 unsigned s = agx_predecessor_index(succ, block);
611
612 agx_foreach_phi_in_block(succ, phi) {
613 record_use(uses, multiple, phi, s);
614 }
615 }
616
617 agx_foreach_instr_in_block_rev(block, I) {
618 struct agx_opcode_info info = agx_opcodes_info[I->op];
619
620 /* Skip phis, they're handled specially */
621 if (I->op == AGX_OPCODE_PHI) {
622 continue;
623 }
624
625 agx_foreach_ssa_src(I, s) {
626 record_use(uses, multiple, I, s);
627 }
628
629 if (info.nr_dests != 1)
630 continue;
631
632 if (I->dest[0].type != AGX_INDEX_NORMAL)
633 continue;
634
635 agx_instr *use = uses[I->dest[0].value];
636
637 if (!use || BITSET_TEST(multiple, I->dest[0].value))
638 continue;
639
640 if (agx_optimizer_not(I, use)) {
641 agx_remove_instruction(use);
642 continue;
643 }
644
645 /* Destination has a single use, try to propagate */
646 if (info.is_float && agx_optimizer_fmov_rev(I, use)) {
647 agx_remove_instruction(use);
648 continue;
649 }
650 }
651 }
652
653 free(uses);
654 free(multiple);
655 }