Debian Codegraph @

src/asahi/compiler/agx_compiler.h

raw
1 /*
2 * Copyright 2021 Alyssa Rosenzweig
3 * Copyright 2020 Collabora Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #pragma once
8
9 #include "asahi/isa/agx_minifloat.h"
10 #include "compiler/nir/nir.h"
11 #include "util/bitset.h"
12 #include "util/half_float.h"
13 #include "util/u_dynarray.h"
14 #include "util/u_math.h"
15 #include "util/u_worklist.h"
16 #include "agx_compile.h"
17 #include "agx_opcodes.h"
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /* r0-r127 inclusive, as pairs of 16-bits, gives 256 registers */
24 #define AGX_NUM_REGS (256)
25
26 /* u0-u255 inclusive, as pairs of 16-bits */
27 #define AGX_NUM_UNIFORMS (512)
28
29 /* Semi-arbitrary limit for spill slot allocation */
30 #define AGX_NUM_MODELED_REGS_LOG2 (11)
31 #define AGX_NUM_MODELED_REGS (1 << AGX_NUM_MODELED_REGS_LOG2)
32
33 /* Limit on number of sources for non-phi instructions */
34 #define AGX_MAX_NORMAL_SOURCES (16)
35
36 enum agx_index_type {
37 AGX_INDEX_NULL = 0,
38 AGX_INDEX_NORMAL = 1,
39 AGX_INDEX_IMMEDIATE = 2,
40 AGX_INDEX_UNIFORM = 3,
41 AGX_INDEX_REGISTER = 4,
42 AGX_INDEX_UNDEF = 5,
43 };
44
45 enum agx_size { AGX_SIZE_16 = 0, AGX_SIZE_32 = 1, AGX_SIZE_64 = 2 };
46
47 static inline unsigned
48 agx_size_align_16(enum agx_size size)
49 {
50 switch (size) {
51 case AGX_SIZE_16:
52 return 1;
53 case AGX_SIZE_32:
54 return 2;
55 case AGX_SIZE_64:
56 return 4;
57 }
58
59 UNREACHABLE("Invalid size");
60 }
61
62 /* Keep synced with hash_index */
63 typedef struct {
64 /* Sufficient for as many SSA values, immediates, and uniforms as we need. */
65 uint32_t value;
66
67 /* Indicates that this source kills the referenced value (because it is the
68 * last use in a block and the source is not live after the block). Set by
69 * liveness analysis.
70 */
71 bool kill : 1;
72
73 /* Cache hints */
74 bool cache : 1;
75 bool discard : 1;
76
77 /* src - float modifiers */
78 bool abs : 1;
79 bool neg : 1;
80
81 /* Register class */
82 bool memory : 1;
83
84 unsigned channels_m1 : 3;
85 enum agx_size size : 2;
86 enum agx_index_type type : 3;
87
88 /* If has_reg is set (during register allocation), the register assigned to
89 * this SSA value This is used with NORMAL. Contrast REGISTER which uses
90 * value instead.
91 *
92 * TODO: Unify.
93 */
94 unsigned reg : AGX_NUM_MODELED_REGS_LOG2;
95 bool has_reg : 1;
96 unsigned padding : 6;
97 } agx_index;
98 static_assert(sizeof(agx_index) == 8, "packed");
99
100 static inline unsigned
101 agx_channels(agx_index idx)
102 {
103 return idx.channels_m1 + 1;
104 }
105
106 static inline unsigned
107 agx_index_size_16(agx_index idx)
108 {
109 return agx_size_align_16(idx.size) * agx_channels(idx);
110 }
111
112 static inline agx_index
113 agx_get_vec_index(unsigned value, enum agx_size size, unsigned channels)
114 {
115 return (agx_index){
116 .value = value,
117 .channels_m1 = channels - 1,
118 .size = size,
119 .type = AGX_INDEX_NORMAL,
120 };
121 }
122
123 static inline agx_index
124 agx_get_index(unsigned value, enum agx_size size)
125 {
126 return agx_get_vec_index(value, size, 1);
127 }
128
129 static inline agx_index
130 agx_immediate(uint32_t imm)
131 {
132 assert(imm < (1 << 16) && "overflowed immediate");
133
134 return (agx_index){
135 .value = imm,
136 .size = AGX_SIZE_16,
137 .type = AGX_INDEX_IMMEDIATE,
138 };
139 }
140
141 static inline agx_index
142 agx_immediate_f(float f)
143 {
144 assert(agx_minifloat_exact(f));
145 return agx_immediate(agx_minifloat_encode(f));
146 }
147
148 /* in half-words, specify r0h as 1, r1 as 2... */
149 static inline agx_index
150 agx_register(uint32_t imm, enum agx_size size)
151 {
152 assert(imm < AGX_NUM_REGS);
153
154 return (agx_index){
155 .value = imm,
156 .size = size,
157 .type = AGX_INDEX_REGISTER,
158 };
159 }
160
161 static inline agx_index
162 agx_memory_register(uint32_t imm, enum agx_size size)
163 {
164 return (agx_index){
165 .value = imm,
166 .memory = true,
167 .size = size,
168 .type = AGX_INDEX_REGISTER,
169 };
170 }
171
172 static inline agx_index
173 agx_register_like(uint32_t imm, agx_index like)
174 {
175 return (agx_index){
176 .value = imm,
177 .memory = like.memory,
178 .channels_m1 = like.channels_m1,
179 .size = like.size,
180 .type = AGX_INDEX_REGISTER,
181 };
182 }
183
184 static inline agx_index
185 agx_as_register(agx_index x)
186 {
187 assert(x.has_reg);
188 return agx_register_like(x.reg, x);
189 }
190
191 static inline agx_index
192 agx_undef(enum agx_size size)
193 {
194 return (agx_index){
195 .size = size,
196 .type = AGX_INDEX_UNDEF,
197 };
198 }
199
200 /* Also in half-words */
201 static inline agx_index
202 agx_uniform(uint32_t imm, enum agx_size size)
203 {
204 assert(imm < AGX_NUM_UNIFORMS);
205
206 return (agx_index){
207 .value = imm,
208 .size = size,
209 .type = AGX_INDEX_UNIFORM,
210 };
211 }
212
213 static inline agx_index
214 agx_null()
215 {
216 return (agx_index){.type = AGX_INDEX_NULL};
217 }
218
219 static inline agx_index
220 agx_zero()
221 {
222 return agx_immediate(0);
223 }
224
225 /* IEEE 754 additive identity -0.0, stored as an 8-bit AGX minifloat: mantissa
226 * = exponent = 0, sign bit set */
227
228 static inline agx_index
229 agx_negzero()
230 {
231 return agx_immediate(0x80);
232 }
233
234 static inline agx_index
235 agx_abs(agx_index idx)
236 {
237 idx.abs = true;
238 idx.neg = false;
239 return idx;
240 }
241
242 static inline agx_index
243 agx_neg(agx_index idx)
244 {
245 idx.neg ^= true;
246 return idx;
247 }
248
249 /* Replaces an index, preserving any modifiers */
250
251 static inline agx_index
252 agx_replace_index(agx_index old, agx_index replacement)
253 {
254 replacement.abs = old.abs;
255 replacement.neg = old.neg;
256 return replacement;
257 }
258
259 static inline bool
260 agx_is_null(agx_index idx)
261 {
262 return idx.type == AGX_INDEX_NULL;
263 }
264
265 /* Compares equivalence as references */
266
267 static inline bool
268 agx_is_equiv(agx_index left, agx_index right)
269 {
270 return (left.type == right.type) && (left.value == right.value);
271 }
272
273 enum ra_class {
274 /* General purpose register */
275 RA_GPR,
276
277 /* Memory, used to assign stack slots */
278 RA_MEM,
279
280 /* Keep last */
281 RA_CLASSES,
282 };
283
284 static inline enum ra_class
285 ra_class_for_index(agx_index idx)
286 {
287 return idx.memory ? RA_MEM : RA_GPR;
288 }
289
290 enum agx_icond {
291 AGX_ICOND_UEQ = 0,
292 AGX_ICOND_ULT = 1,
293 AGX_ICOND_UGT = 2,
294 /* unknown */
295 AGX_ICOND_SEQ = 4,
296 AGX_ICOND_SLT = 5,
297 AGX_ICOND_SGT = 6,
298 /* unknown */
299 };
300
301 enum agx_fcond {
302 AGX_FCOND_EQ = 0,
303 AGX_FCOND_LT = 1,
304 AGX_FCOND_GT = 2,
305 AGX_FCOND_LTN = 3,
306 /* unknown */
307 AGX_FCOND_GE = 5,
308 AGX_FCOND_LE = 6,
309 AGX_FCOND_GTN = 7,
310 };
311
312 enum agx_round {
313 AGX_ROUND_RTZ = 0,
314 AGX_ROUND_RTE = 1,
315 };
316
317 enum agx_convert {
318 AGX_CONVERT_U8_TO_F = 0,
319 AGX_CONVERT_S8_TO_F = 1,
320 AGX_CONVERT_F_TO_U16 = 4,
321 AGX_CONVERT_F_TO_S16 = 5,
322 AGX_CONVERT_U16_TO_F = 6,
323 AGX_CONVERT_S16_TO_F = 7,
324 AGX_CONVERT_F_TO_U32 = 8,
325 AGX_CONVERT_F_TO_S32 = 9,
326 AGX_CONVERT_U32_TO_F = 10,
327 AGX_CONVERT_S32_TO_F = 11
328 };
329
330 enum agx_lod_mode {
331 AGX_LOD_MODE_AUTO_LOD = 0,
332 AGX_LOD_MODE_AUTO_LOD_BIAS_UNIFORM = 1,
333 AGX_LOD_MODE_LOD_MIN_UNIFORM = 2,
334 AGX_LOD_MODE_AUTO_LOD_BIAS = 5,
335 AGX_LOD_MODE_LOD_GRAD = 4,
336 AGX_LOD_MODE_LOD_MIN = 6,
337 AGX_LOD_MODE_AUTO_LOD_BIAS_MIN_UNIFORM = 9,
338 AGX_LOD_MODE_LOD_GRAD_MIN = 12,
339 AGX_LOD_MODE_AUTO_LOD_BIAS_MIN = 13,
340 };
341
342 /* Forward declare for branch target */
343 struct agx_block;
344
345 /* Keep synced with hash_instr */
346 typedef struct {
347 /* Must be first */
348 struct list_head link;
349
350 /* The sources list. */
351 agx_index *src;
352
353 /* Data flow */
354 agx_index *dest;
355
356 enum agx_opcode op;
357
358 uint8_t nr_dests;
359 uint8_t nr_srcs;
360
361 /* TODO: More efficient */
362 union {
363 enum agx_icond icond;
364 enum agx_fcond fcond;
365 };
366
367 union {
368 uint64_t imm;
369 uint32_t writeout;
370 uint32_t truth_table;
371 uint32_t component;
372 uint32_t channels;
373 uint32_t bfi_mask;
374 uint16_t pixel_offset;
375 uint16_t zs;
376 int16_t stack_size;
377 enum agx_sr sr;
378 enum agx_round round;
379 enum agx_atomic_opc atomic_opc;
380 enum agx_lod_mode lod_mode;
381 enum agx_simd_op simd_op;
382 struct agx_block *target;
383
384 /* As a special case to workaround ordering issues when translating phis,
385 * if nr_srcs == 0 and the opcode is PHI, points to the NIR phi.
386 */
387 nir_phi_instr *phi;
388 };
389
390 /* For local access */
391 enum agx_format format;
392
393 /* Number of nested control flow layers to jump by. TODO: Optimize */
394 uint32_t nest;
395
396 /* Invert icond/fcond */
397 bool invert_cond : 1;
398
399 /* TODO: Handle tex ops more efficient */
400 enum agx_dim dim : 4;
401 bool offset : 1;
402 bool shadow : 1;
403 bool query_lod : 1;
404 bool sparse : 1;
405 enum agx_gather gather : 3;
406
407 /* TODO: Handle tilebuffer ops more efficient */
408 bool explicit_coords : 1;
409
410 /* TODO: Handle iter ops more efficient */
411 enum agx_interpolation interpolation : 2;
412
413 /* TODO: Handle loads more efficiently */
414 bool coherent : 1;
415
416 /* Final st_vary op */
417 bool last : 1;
418
419 /* Shift for a bitwise or memory op (conflicts with format for memory ops) */
420 unsigned shift : 4;
421
422 /* Scoreboard index, 0 or 1. Leave as 0 for instructions that do not require
423 * scoreboarding (everything but memory load/store and texturing). */
424 unsigned scoreboard : 1;
425
426 /* Output modifiers */
427 bool saturate : 1;
428 unsigned mask : 4;
429
430 unsigned padding : 8;
431 } agx_instr;
432
433 static inline void
434 agx_replace_src(agx_instr *I, unsigned src_index, agx_index replacement)
435 {
436 I->src[src_index] = agx_replace_index(I->src[src_index], replacement);
437 }
438
439 struct agx_block;
440
441 typedef struct agx_block {
442 /* Link to next block. Must be first */
443 struct list_head link;
444
445 /* List of instructions emitted for the current block */
446 struct list_head instructions;
447
448 /* Index of the block in source order */
449 unsigned index;
450
451 /* Control flow graph */
452 struct agx_block *successors[2];
453 struct util_dynarray predecessors;
454 bool unconditional_jumps;
455
456 /* Could there be masked execution? */
457 bool divergent;
458
459 /* Liveness analysis results */
460 struct u_sparse_bitset live_in;
461 struct u_sparse_bitset live_out;
462
463 BITSET_DECLARE(reg_live_in, AGX_NUM_REGS);
464 BITSET_DECLARE(reg_live_out, AGX_NUM_REGS);
465
466 /* For visited blocks during register assignment and live-out registers, the
467 * mapping of registers to SSA names at the end of the block. This is dense,
468 * unlike its inverse.
469 */
470 uint32_t *reg_to_ssa_out[2];
471
472 /* Is this block a loop header? If not, all of its predecessors precede it in
473 * source order.
474 */
475 bool loop_header;
476
477 /* Offset of the block in the emitted binary */
478 off_t offset, last_offset;
479
480 /** Available for passes to use for metadata */
481 uint8_t pass_flags;
482 } agx_block;
483
484 typedef struct {
485 nir_shader *nir;
486 mesa_shader_stage stage;
487 bool is_preamble;
488 unsigned scratch_size_B;
489
490 struct list_head blocks; /* list of agx_block */
491 struct agx_shader_info *out;
492 struct agx_shader_key *key;
493
494 /* Maximum block index */
495 unsigned num_blocks;
496
497 /* For creating temporaries */
498 unsigned alloc;
499
500 /* Does the shader statically use scratch memory? */
501 bool any_scratch;
502
503 /* Mask of pixel fences we've definitely already waited for. */
504 uint16_t already_pixel_waited;
505
506 /* Has r0l been zeroed yet due to control flow? */
507 bool any_cf;
508
509 /* Do we need r0h zero throughout the program to handle quad-divergent
510 * shuffle?
511 */
512 bool any_quad_divergent_shuffle;
513
514 /* Number of nested control flow structures within the innermost loop. Since
515 * NIR is just loop and if-else, this is the number of nested if-else
516 * statements in the loop */
517 unsigned loop_nesting;
518
519 /* Total nesting across all loops, to determine if we need push_exec */
520 unsigned total_nesting;
521
522 /* During instruction selection, for inserting control flow */
523 agx_block *current_block;
524 agx_block *break_block;
525 agx_block *after_block;
526 agx_block **indexed_nir_blocks;
527
528 /* During instruction selection, map from vector agx_index to its scalar
529 * components, populated by a split. */
530 struct hash_table_u64 *allocated_vec;
531
532 /* During instruction selection, preloaded values or NULL if it hasn't been
533 * preloaded.
534 */
535 agx_index preloaded[AGX_NUM_REGS];
536
537 /* Beginning of our stack allocation used for spilling, below that is
538 * NIR-level scratch.
539 */
540 unsigned spill_base_B;
541
542 /* Beginning of stack allocation used for parallel copy lowering */
543 bool has_spill_pcopy_reserved;
544 unsigned spill_pcopy_base;
545
546 /* Stats for shader-db */
547 unsigned loop_count;
548 unsigned max_reg;
549
550 /* Promoted constants. These will be appended to the binary at the end. */
551 uint16_t rodata[512];
552 } agx_context;
553
554 static inline void
555 agx_remove_instruction(agx_instr *ins)
556 {
557 list_del(&ins->link);
558 }
559
560 static inline agx_index
561 agx_vec_temp(agx_context *ctx, enum agx_size size, unsigned channels)
562 {
563 return agx_get_vec_index(ctx->alloc++, size, channels);
564 }
565
566 static inline agx_index
567 agx_temp(agx_context *ctx, enum agx_size size)
568 {
569 return agx_get_index(ctx->alloc++, size);
570 }
571
572 static inline agx_index
573 agx_temp_like(agx_context *ctx, agx_index idx)
574 {
575 idx.value = ctx->alloc++;
576 return idx;
577 }
578
579 static enum agx_size
580 agx_size_for_bits(unsigned bits)
581 {
582 switch (bits) {
583 case 1:
584 case 8:
585 case 16:
586 return AGX_SIZE_16;
587 case 32:
588 return AGX_SIZE_32;
589 case 64:
590 return AGX_SIZE_64;
591 default:
592 UNREACHABLE("Invalid bitsize");
593 }
594 }
595
596 static inline agx_index
597 agx_def_index(nir_def *ssa)
598 {
599 return agx_get_vec_index(ssa->index, agx_size_for_bits(ssa->bit_size),
600 ssa->num_components);
601 }
602
603 static inline agx_index
604 agx_src_index(nir_src *src)
605 {
606 return agx_def_index(src->ssa);
607 }
608
609 static inline agx_index
610 agx_vec_for_def(agx_context *ctx, nir_def *def)
611 {
612 return agx_vec_temp(ctx, agx_size_for_bits(def->bit_size),
613 def->num_components);
614 }
615
616 static inline agx_index
617 agx_vec_for_intr(agx_context *ctx, nir_intrinsic_instr *instr)
618 {
619 return agx_vec_for_def(ctx, &instr->def);
620 }
621
622 static inline unsigned
623 agx_num_predecessors(agx_block *block)
624 {
625 return util_dynarray_num_elements(&block->predecessors, agx_block *);
626 }
627
628 static inline unsigned
629 agx_num_successors(agx_block *block)
630 {
631 STATIC_ASSERT(ARRAY_SIZE(block->successors) == 2);
632 return (block->successors[0] ? 1 : 0) + (block->successors[1] ? 1 : 0);
633 }
634
635 static inline agx_block *
636 agx_start_block(agx_context *ctx)
637 {
638 agx_block *first = list_first_entry(&ctx->blocks, agx_block, link);
639 assert(agx_num_predecessors(first) == 0);
640 return first;
641 }
642
643 static inline agx_block *
644 agx_end_block(agx_context *ctx)
645 {
646 agx_block *last = list_last_entry(&ctx->blocks, agx_block, link);
647 assert(agx_num_successors(last) == 0);
648 return last;
649 }
650
651 void agx_block_add_successor(agx_block *block, agx_block *successor);
652
653 /* Iterators for AGX IR */
654
655 #define agx_foreach_block(ctx, v) \
656 list_for_each_entry(agx_block, v, &ctx->blocks, link)
657
658 #define agx_foreach_block_safe(ctx, v) \
659 list_for_each_entry_safe(agx_block, v, &ctx->blocks, link)
660
661 #define agx_foreach_block_rev(ctx, v) \
662 list_for_each_entry_rev(agx_block, v, &ctx->blocks, link)
663
664 #define agx_foreach_block_from(ctx, from, v) \
665 list_for_each_entry_from(agx_block, v, from, &ctx->blocks, link)
666
667 #define agx_foreach_block_from_rev(ctx, from, v) \
668 list_for_each_entry_from_rev(agx_block, v, from, &ctx->blocks, link)
669
670 #define agx_foreach_instr_in_block(block, v) \
671 list_for_each_entry(agx_instr, v, &(block)->instructions, link)
672
673 #define agx_foreach_instr_in_block_rev(block, v) \
674 list_for_each_entry_rev(agx_instr, v, &(block)->instructions, link)
675
676 #define agx_foreach_instr_in_block_safe(block, v) \
677 list_for_each_entry_safe(agx_instr, v, &(block)->instructions, link)
678
679 #define agx_foreach_instr_in_block_safe_rev(block, v) \
680 list_for_each_entry_safe_rev(agx_instr, v, &(block)->instructions, link)
681
682 #define agx_foreach_instr_in_block_from(block, v, from) \
683 list_for_each_entry_from(agx_instr, v, from, &(block)->instructions, link)
684
685 #define agx_foreach_instr_in_block_from_rev(block, v, from) \
686 list_for_each_entry_from_rev(agx_instr, v, from, &(block)->instructions, \
687 link)
688
689 #define agx_foreach_instr_global(ctx, v) \
690 agx_foreach_block(ctx, v_block) \
691 agx_foreach_instr_in_block(v_block, v)
692
693 #define agx_foreach_instr_global_rev(ctx, v) \
694 agx_foreach_block_rev(ctx, v_block) \
695 agx_foreach_instr_in_block_rev(v_block, v)
696
697 #define agx_foreach_instr_global_safe(ctx, v) \
698 agx_foreach_block(ctx, v_block) \
699 agx_foreach_instr_in_block_safe(v_block, v)
700
701 #define agx_foreach_instr_global_safe_rev(ctx, v) \
702 agx_foreach_block_rev(ctx, v_block) \
703 agx_foreach_instr_in_block_safe_rev(v_block, v)
704
705 /* Based on set_foreach, expanded with automatic type casts */
706
707 #define agx_foreach_successor(blk, v) \
708 agx_block *v; \
709 agx_block **_v; \
710 for (_v = (agx_block **)&blk->successors[0], v = *_v; \
711 v != NULL && _v < (agx_block **)&blk->successors[2]; _v++, v = *_v)
712
713 #define agx_foreach_predecessor(blk, v) \
714 util_dynarray_foreach(&blk->predecessors, agx_block *, v)
715
716 #define agx_foreach_src(ins, v) for (unsigned v = 0; v < ins->nr_srcs; ++v)
717
718 #define agx_foreach_src_rev(ins, v) \
719 for (signed v = ins->nr_srcs - 1; v >= 0; --v)
720
721 #define agx_foreach_dest(ins, v) for (unsigned v = 0; v < ins->nr_dests; ++v)
722
723 #define agx_foreach_dest_rev(ins, v) \
724 for (signed v = ins->nr_dests - 1; v >= 0; --v)
725
726 #define agx_foreach_ssa_src(ins, v) \
727 agx_foreach_src(ins, v) \
728 if (ins->src[v].type == AGX_INDEX_NORMAL)
729
730 #define agx_foreach_ssa_src_rev(ins, v) \
731 agx_foreach_src_rev(ins, v) \
732 if (ins->src[v].type == AGX_INDEX_NORMAL)
733
734 #define agx_foreach_reg_src(ins, v) \
735 agx_foreach_src(ins, v) \
736 if (ins->src[v].type == AGX_INDEX_REGISTER)
737
738 #define agx_foreach_ssa_dest(ins, v) \
739 agx_foreach_dest(ins, v) \
740 if (ins->dest[v].type == AGX_INDEX_NORMAL)
741
742 #define agx_foreach_ssa_dest_rev(ins, v) \
743 agx_foreach_dest_rev(ins, v) \
744 if (ins->dest[v].type == AGX_INDEX_NORMAL)
745
746 #define agx_foreach_reg_dest(ins, v) \
747 agx_foreach_dest(ins, v) \
748 if (ins->dest[v].type == AGX_INDEX_REGISTER)
749
750 /* Phis only come at the start (after else instructions) so we stop as soon as
751 * we hit a non-phi
752 */
753 #define agx_foreach_phi_in_block(block, v) \
754 agx_foreach_instr_in_block(block, v) \
755 if (v->op == AGX_OPCODE_ELSE_ICMP || v->op == AGX_OPCODE_ELSE_FCMP) \
756 continue; \
757 else if (v->op != AGX_OPCODE_PHI) \
758 break; \
759 else
760
761 #define agx_foreach_phi_in_block_safe(block, v) \
762 agx_foreach_instr_in_block_safe(block, v) \
763 if (v->op == AGX_OPCODE_ELSE_ICMP || v->op == AGX_OPCODE_ELSE_FCMP) \
764 continue; \
765 else if (v->op != AGX_OPCODE_PHI) \
766 break; \
767 else
768
769 /*
770 * Find the index of a predecessor, used as the implicit order of phi sources.
771 */
772 static inline unsigned
773 agx_predecessor_index(agx_block *succ, agx_block *pred)
774 {
775 unsigned index = 0;
776
777 agx_foreach_predecessor(succ, x) {
778 if (*x == pred)
779 return index;
780
781 index++;
782 }
783
784 UNREACHABLE("Invalid predecessor");
785 }
786
787 static inline agx_block *
788 agx_prev_block(agx_block *ins)
789 {
790 return list_last_entry(&(ins->link), agx_block, link);
791 }
792
793 static inline agx_instr *
794 agx_prev_op(agx_instr *ins)
795 {
796 return list_last_entry(&(ins->link), agx_instr, link);
797 }
798
799 static inline agx_instr *
800 agx_first_instr(agx_block *block)
801 {
802 if (list_is_empty(&block->instructions))
803 return NULL;
804 else
805 return list_first_entry(&block->instructions, agx_instr, link);
806 }
807
808 static inline agx_instr *
809 agx_last_instr(agx_block *block)
810 {
811 if (list_is_empty(&block->instructions))
812 return NULL;
813 else
814 return list_last_entry(&block->instructions, agx_instr, link);
815 }
816
817 static inline agx_instr *
818 agx_next_op(agx_instr *ins)
819 {
820 return list_first_entry(&(ins->link), agx_instr, link);
821 }
822
823 static inline agx_block *
824 agx_next_block(agx_block *block)
825 {
826 return list_first_entry(&(block->link), agx_block, link);
827 }
828
829 static inline agx_block *
830 agx_exit_block(agx_context *ctx)
831 {
832 agx_block *last = list_last_entry(&ctx->blocks, agx_block, link);
833 assert(!last->successors[0] && !last->successors[1]);
834 return last;
835 }
836
837 #define agx_worklist_init(ctx, w) u_worklist_init(w, ctx->num_blocks, ctx)
838 #define agx_worklist_push_head(w, block) u_worklist_push_head(w, block, index)
839 #define agx_worklist_push_tail(w, block) u_worklist_push_tail(w, block, index)
840 #define agx_worklist_peek_head(w) u_worklist_peek_head(w, agx_block, index)
841 #define agx_worklist_pop_head(w) u_worklist_pop_head(w, agx_block, index)
842 #define agx_worklist_peek_tail(w) u_worklist_peek_tail(w, agx_block, index)
843 #define agx_worklist_pop_tail(w) u_worklist_pop_tail(w, agx_block, index)
844
845 /* Like in NIR, for use with the builder */
846
847 enum agx_cursor_option {
848 agx_cursor_after_block,
849 agx_cursor_before_instr,
850 agx_cursor_after_instr
851 };
852
853 typedef struct {
854 enum agx_cursor_option option;
855
856 union {
857 agx_block *block;
858 agx_instr *instr;
859 };
860 } agx_cursor;
861
862 static inline bool
863 agx_cursors_equal(agx_cursor a, agx_cursor b)
864 {
865 if (a.option != b.option)
866 return false;
867
868 if (a.option == agx_cursor_after_block)
869 return a.block == b.block;
870 else
871 return a.instr == b.instr;
872 }
873
874 static inline agx_cursor
875 agx_after_block(agx_block *block)
876 {
877 return (agx_cursor){
878 .option = agx_cursor_after_block,
879 .block = block,
880 };
881 }
882
883 static inline agx_cursor
884 agx_before_instr(agx_instr *instr)
885 {
886 return (agx_cursor){
887 .option = agx_cursor_before_instr,
888 .instr = instr,
889 };
890 }
891
892 static inline agx_cursor
893 agx_after_instr(agx_instr *instr)
894 {
895 return (agx_cursor){
896 .option = agx_cursor_after_instr,
897 .instr = instr,
898 };
899 }
900
901 static inline agx_cursor
902 agx_before_nonempty_block(agx_block *block)
903 {
904 agx_instr *I = list_first_entry(&block->instructions, agx_instr, link);
905 assert(I != NULL);
906
907 return agx_before_instr(I);
908 }
909
910 static inline agx_cursor
911 agx_before_block(agx_block *block)
912 {
913 if (list_is_empty(&block->instructions))
914 return agx_after_block(block);
915 else
916 return agx_before_nonempty_block(block);
917 }
918
919 static inline bool
920 instr_after_logical_end(const agx_instr *I)
921 {
922 switch (I->op) {
923 case AGX_OPCODE_JMP_EXEC_ANY:
924 case AGX_OPCODE_JMP_EXEC_NONE:
925 case AGX_OPCODE_POP_EXEC:
926 case AGX_OPCODE_BREAK:
927 case AGX_OPCODE_IF_ICMP:
928 case AGX_OPCODE_WHILE_ICMP:
929 case AGX_OPCODE_IF_FCMP:
930 case AGX_OPCODE_WHILE_FCMP:
931 case AGX_OPCODE_STOP:
932 case AGX_OPCODE_EXPORT:
933 return true;
934 default:
935 return false;
936 }
937 }
938
939 /*
940 * Get a cursor inserting at the logical end of the block. In particular, this
941 * is before branches or control flow instructions, which occur after the
942 * logical end but before the physical end.
943 */
944 static inline agx_cursor
945 agx_after_block_logical(agx_block *block)
946 {
947 /* Search for the first instruction that's not past the logical end */
948 agx_foreach_instr_in_block_rev(block, I) {
949 if (!instr_after_logical_end(I))
950 return agx_after_instr(I);
951 }
952
953 /* If we got here, the block is either empty or entirely control flow */
954 return agx_before_block(block);
955 }
956
957 /* Get a cursor at the start of a function, after any preloads */
958 static inline agx_cursor
959 agx_before_function(agx_context *ctx)
960 {
961 agx_block *block = agx_start_block(ctx);
962
963 agx_foreach_instr_in_block(block, I) {
964 if (I->op != AGX_OPCODE_PRELOAD)
965 return agx_before_instr(I);
966 }
967
968 /* The whole block is preloads, so insert at the end */
969 return agx_after_block(block);
970 }
971
972 /* IR builder in terms of cursor infrastructure */
973
974 typedef struct {
975 agx_context *shader;
976 agx_cursor cursor;
977 } agx_builder;
978
979 static inline agx_builder
980 agx_init_builder(agx_context *ctx, agx_cursor cursor)
981 {
982 return (agx_builder){
983 .shader = ctx,
984 .cursor = cursor,
985 };
986 }
987
988 /* Insert an instruction at the cursor and move the cursor */
989
990 static inline void
991 agx_builder_insert(agx_cursor *cursor, agx_instr *I)
992 {
993 switch (cursor->option) {
994 case agx_cursor_after_instr:
995 list_add(&I->link, &cursor->instr->link);
996 cursor->instr = I;
997 return;
998
999 case agx_cursor_after_block:
1000 list_addtail(&I->link, &cursor->block->instructions);
1001 cursor->option = agx_cursor_after_instr;
1002 cursor->instr = I;
1003 return;
1004
1005 case agx_cursor_before_instr:
1006 list_addtail(&I->link, &cursor->instr->link);
1007 cursor->option = agx_cursor_after_instr;
1008 cursor->instr = I;
1009 return;
1010 }
1011
1012 UNREACHABLE("Invalid cursor option");
1013 }
1014
1015 bool agx_instr_accepts_uniform(enum agx_opcode op, unsigned src_index,
1016 unsigned value, enum agx_size size);
1017
1018 /* Routines defined for AIR */
1019 void agx_print_index(agx_index index, bool is_float, FILE *fp);
1020 void agx_print_instr(const agx_instr *I, FILE *fp);
1021 void agx_print_block(const agx_block *block, FILE *fp);
1022 void agx_print_shader(const agx_context *ctx, FILE *fp);
1023 void agx_optimizer_forward(agx_context *ctx);
1024 void agx_optimizer_backward(agx_context *ctx);
1025 void agx_lower_divergent_shuffle(agx_context *ctx);
1026 void agx_lower_pseudo(agx_context *ctx);
1027 void agx_lower_spill(agx_context *ctx);
1028 void agx_lower_uniform_sources(agx_context *ctx);
1029 void agx_opt_cse(agx_context *ctx);
1030 void agx_opt_compact_constants(agx_context *ctx);
1031 void agx_opt_promote_constants(agx_context *ctx);
1032 void agx_dce(agx_context *ctx, bool partial);
1033 void agx_pressure_schedule(agx_context *ctx);
1034 void agx_spill(agx_context *ctx, unsigned k, bool remat_only);
1035 void agx_repair_ssa(agx_context *ctx);
1036 void agx_reindex_ssa(agx_context *ctx);
1037 void agx_ra(agx_context *ctx);
1038 void agx_lower_64bit_postra(agx_context *ctx);
1039 void agx_insert_waits(agx_context *ctx);
1040 void agx_opt_empty_else(agx_context *ctx);
1041 void agx_opt_register_cache(agx_context *ctx);
1042 void agx_opt_break_if(agx_context *ctx);
1043 void agx_opt_jmp_none(agx_context *ctx);
1044 void agx_pack_binary(agx_context *ctx, struct util_dynarray *emission);
1045
1046 #ifndef NDEBUG
1047 void agx_validate(agx_context *ctx, const char *after_str);
1048 void agx_validate_ra(agx_context *ctx);
1049 #else
1050 static inline void
1051 agx_validate(UNUSED agx_context *ctx, UNUSED const char *after_str)
1052 {
1053 }
1054
1055 static inline void
1056 agx_validate_ra(UNUSED agx_context *ctx)
1057 {
1058 }
1059 #endif
1060
1061 enum agx_size agx_split_width(const agx_instr *I);
1062 bool agx_allows_16bit_immediate(agx_instr *I);
1063 unsigned agx_negate_src_index(agx_instr *I);
1064
1065 static inline bool
1066 agx_is_float_src(const agx_instr *I, unsigned s)
1067 {
1068 struct agx_opcode_info info = agx_opcodes_info[I->op];
1069 bool fcmp = (I->op == AGX_OPCODE_FCMPSEL || I->op == AGX_OPCODE_FCMP);
1070
1071 /* fcmp takes first 2 as floats but returns an integer */
1072 return info.is_float || (s < 2 && fcmp);
1073 }
1074
1075 struct agx_copy {
1076 /* Base register destination of the copy */
1077 unsigned dest;
1078
1079 /* Destination is memory */
1080 bool dest_mem;
1081
1082 /* Source of the copy */
1083 agx_index src;
1084
1085 /* Whether the copy has been handled. Callers must leave to false. */
1086 bool done;
1087 };
1088
1089 void agx_emit_parallel_copies(agx_builder *b, struct agx_copy *copies,
1090 unsigned n);
1091
1092 void agx_compute_liveness(agx_context *ctx);
1093 void agx_liveness_ins_update(struct u_sparse_bitset *live, agx_instr *I);
1094
1095 bool agx_nir_opt_preamble(nir_shader *s, unsigned *sizes);
1096 bool agx_nir_lower_load_mask(nir_shader *shader);
1097 bool agx_nir_lower_ubo(nir_shader *shader);
1098 bool agx_nir_lower_shared_bitsize(nir_shader *shader);
1099 bool agx_nir_lower_frag_sidefx(nir_shader *s);
1100
1101 struct agx_cycle_estimate {
1102 /* ALU throughput */
1103 unsigned alu;
1104
1105 /* Floating point and SCIB (select, conditional, integer, and boolean)
1106 * throughput.
1107 */
1108 unsigned f_scib;
1109
1110 /* IC (Integer and complex) throughput */
1111 unsigned ic;
1112 };
1113
1114 struct agx_cycle_estimate agx_estimate_cycles(agx_context *ctx);
1115
1116 bool agx_is_alu(const agx_instr *I);
1117
1118 extern int agx_compiler_debug;
1119
1120 #ifdef __cplusplus
1121 } /* extern C */
1122 #endif