Debian Codegraph @

src/panfrost/compiler/bifrost/compiler.h

raw
1 /*
2 * Copyright (C) 2020 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef __BIFROST_COMPILER_H
7 #define __BIFROST_COMPILER_H
8
9 #include "compiler/nir/nir.h"
10 #include "panfrost/compiler/pan_compiler.h"
11 #include "util/half_float.h"
12 #include "util/shader_stats.h"
13 #include "util/u_math.h"
14 #include "util/u_worklist.h"
15 #include "bi_opcodes.h"
16 #include "bifrost.h"
17 #include "valhall_enums.h"
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /* Swizzles across bytes in a 32-bit word. Expresses swz in the XML directly.
24 * To express widen, use the correpsonding replicated form, i.e. H01 = identity
25 * for widen = none, H00 for widen = h0, B1111 for widen = b1. For lane, also
26 * use the replicated form (interpretation is governed by the opcode). For
27 * 8-bit lanes with two channels, use replicated forms for replicated forms.
28 * For 8-bit lanes with four channels using matching form.
29 */
30
31 enum bi_swizzle {
32 /* 16-bit swizzles, ordered sequentially for fast compute */
33 BI_SWIZZLE_H00 = 0,
34 BI_SWIZZLE_H01 = 1,
35 BI_SWIZZLE_H10 = 2,
36 BI_SWIZZLE_H11 = 3,
37
38 /* Identity swizzle */
39 BI_SWIZZLE_NONE = BI_SWIZZLE_H01,
40
41 /* 8-bit swizzle equivalents */
42 BI_SWIZZLE_B0101 = BI_SWIZZLE_H00,
43 BI_SWIZZLE_B0123 = BI_SWIZZLE_H01,
44 BI_SWIZZLE_B2301 = BI_SWIZZLE_H10,
45 BI_SWIZZLE_B2323 = BI_SWIZZLE_H11,
46
47 /* 8-bit replication swizzles, ordered sequentially for fast compute */
48 BI_SWIZZLE_B0000 = 4, /* single channel (replicate) */
49 BI_SWIZZLE_B1111 = 5,
50 BI_SWIZZLE_B2222 = 6,
51 BI_SWIZZLE_B3333 = 7,
52
53 /* remaining 8-bit swizzles in arbitrary order */
54 BI_SWIZZLE_B0011 = 8, /* +SWZ.v4i8 */
55 BI_SWIZZLE_B2233 = 9, /* +SWZ.v4i8 */
56 BI_SWIZZLE_B1032 = 10, /* +SWZ.v4i8 */
57 BI_SWIZZLE_B3210 = 11, /* +SWZ.v4i8 */
58
59 /* 8-bit swizzles that only exist in HW as 8-bit half swizzles */
60 BI_SWIZZLE_B0022 = 12,
61 BI_SWIZZLE_B1100 = 13,
62 BI_SWIZZLE_B2200 = 14,
63 BI_SWIZZLE_B3300 = 15,
64 BI_SWIZZLE_B2211 = 16,
65 BI_SWIZZLE_B3311 = 17,
66 BI_SWIZZLE_B1122 = 18,
67 BI_SWIZZLE_B3322 = 19,
68 BI_SWIZZLE_B0033 = 20,
69 BI_SWIZZLE_B1133 = 21,
70
71 /* 16-bit single-lane, values ordered sequentially */
72 BI_SWIZZLE_H0 = BI_SWIZZLE_H00,
73 BI_SWIZZLE_H1 = BI_SWIZZLE_H11,
74
75 /* 8-bit single-lane, values order sequentially */
76 BI_SWIZZLE_B0 = BI_SWIZZLE_B0000,
77 BI_SWIZZLE_B1 = BI_SWIZZLE_B1111,
78 BI_SWIZZLE_B2 = BI_SWIZZLE_B2222,
79 BI_SWIZZLE_B3 = BI_SWIZZLE_B3333,
80
81 /* 8-bit half-swizzle
82 *
83 * Values for replication are sequential. Other half-swizzles have
84 * arbitrary value ordering. */
85 BI_SWIZZLE_B00 = BI_SWIZZLE_B0000,
86 BI_SWIZZLE_B10 = BI_SWIZZLE_B1100,
87 BI_SWIZZLE_B20 = BI_SWIZZLE_B2200,
88 BI_SWIZZLE_B30 = BI_SWIZZLE_B3300,
89 BI_SWIZZLE_B01 = BI_SWIZZLE_B0011,
90 BI_SWIZZLE_B11 = BI_SWIZZLE_B1111,
91 BI_SWIZZLE_B21 = BI_SWIZZLE_B2211,
92 BI_SWIZZLE_B31 = BI_SWIZZLE_B3311,
93 BI_SWIZZLE_B02 = BI_SWIZZLE_B0022,
94 BI_SWIZZLE_B12 = BI_SWIZZLE_B1122,
95 BI_SWIZZLE_B22 = BI_SWIZZLE_B2222,
96 BI_SWIZZLE_B32 = BI_SWIZZLE_B3322,
97 BI_SWIZZLE_B03 = BI_SWIZZLE_B0033,
98 BI_SWIZZLE_B13 = BI_SWIZZLE_B1133,
99 BI_SWIZZLE_B23 = BI_SWIZZLE_B2233,
100 BI_SWIZZLE_B33 = BI_SWIZZLE_B3333,
101 };
102
103 bool bi_op_supports_swizzle(enum bi_opcode op, unsigned src,
104 enum bi_swizzle swizzle, unsigned arch);
105
106 static inline void
107 bi_swizzle_to_byte_channels(enum bi_swizzle swizzle, unsigned *channels)
108 {
109 #define B(b0, b1, b2, b3) \
110 case BI_SWIZZLE_B##b0##b1##b2##b3: { \
111 channels[0] = b0; \
112 channels[1] = b1; \
113 channels[2] = b2; \
114 channels[3] = b3; \
115 return; \
116 }
117 switch (swizzle) {
118 B(0, 1, 0, 1);
119 B(0, 1, 2, 3);
120 B(2, 3, 0, 1);
121 B(2, 3, 2, 3);
122 B(0, 0, 0, 0);
123 B(1, 1, 1, 1);
124 B(2, 2, 2, 2);
125 B(3, 3, 3, 3);
126 B(0, 0, 1, 1);
127 B(2, 2, 3, 3);
128 B(1, 0, 3, 2);
129 B(3, 2, 1, 0);
130 B(0, 0, 2, 2);
131 B(1, 1, 0, 0);
132 B(2, 2, 0, 0);
133 B(3, 3, 0, 0);
134 B(2, 2, 1, 1);
135 B(3, 3, 1, 1);
136 B(1, 1, 2, 2);
137 B(3, 3, 2, 2);
138 B(0, 0, 3, 3);
139 B(1, 1, 3, 3);
140 }
141 #undef B
142
143 UNREACHABLE("Invalid bi_swizzle");
144 }
145
146 static inline bool
147 bi_swizzle_from_byte_channels(const unsigned byte_channels[4],
148 enum bi_swizzle *out_swizzle)
149 {
150 unsigned index = 0;
151 for (unsigned i = 0; i < 4; i++) {
152 assert(byte_channels[i] < 4);
153 index = (index << 2) | byte_channels[i];
154 }
155
156 switch (index) {
157 #define B(b0, b1, b2, b3) \
158 case ((b0) << 6) | ((b1) << 4) | ((b2) << 2) | (b3): \
159 *out_swizzle = BI_SWIZZLE_B##b0##b1##b2##b3; \
160 return true;
161 B(0, 1, 0, 1);
162 B(0, 1, 2, 3);
163 B(2, 3, 0, 1);
164 B(2, 3, 2, 3);
165 B(0, 0, 0, 0);
166 B(1, 1, 1, 1);
167 B(2, 2, 2, 2);
168 B(3, 3, 3, 3);
169 B(0, 0, 1, 1);
170 B(2, 2, 3, 3);
171 B(1, 0, 3, 2);
172 B(3, 2, 1, 0);
173 B(0, 0, 2, 2);
174 B(1, 1, 0, 0);
175 B(2, 2, 0, 0);
176 B(3, 3, 0, 0);
177 B(2, 2, 1, 1);
178 B(3, 3, 1, 1);
179 B(1, 1, 2, 2);
180 B(3, 3, 2, 2);
181 B(0, 0, 3, 3);
182 B(1, 1, 3, 3);
183 #undef B
184 default:
185 return false;
186 }
187 }
188
189 static inline bool
190 bi_try_compose_swizzles(enum bi_swizzle *ab_out,
191 enum bi_swizzle a, enum bi_swizzle b)
192 {
193 unsigned a_bytes[4], b_bytes[4];
194 bi_swizzle_to_byte_channels(a, a_bytes);
195 bi_swizzle_to_byte_channels(b, b_bytes);
196
197 unsigned ab_bytes[4];
198 for (unsigned i = 0; i < 4; i++) {
199 ab_bytes[i] = b_bytes[a_bytes[i]];
200 }
201
202 return bi_swizzle_from_byte_channels(ab_bytes, ab_out);
203 }
204
205 static inline enum bi_swizzle
206 bi_swizzle_from_half(bool x, bool y)
207 {
208 return (enum bi_swizzle)(BI_SWIZZLE_H00 | (x << 1) | y);
209 }
210
211 /* Given a packed i16vec2/i8vec4 constant, apply a swizzle. Useful for constant
212 * folding and Valhall constant optimization. */
213
214 static inline uint32_t
215 bi_apply_swizzle(uint32_t value, enum bi_swizzle swz)
216 {
217 const uint8_t *b = (const uint8_t *)&value;
218
219 #define B(b0, b1, b2, b3) \
220 case BI_SWIZZLE_B##b0##b1##b2##b3: \
221 return (b[b0] | ((uint32_t)b[b1] << 8) | ((uint32_t)b[b2] << 16) | \
222 ((uint32_t)b[b3] << 24));
223
224 switch (swz) {
225 B(0, 1, 0, 1);
226 B(0, 1, 2, 3);
227 B(2, 3, 0, 1);
228 B(2, 3, 2, 3);
229 B(0, 0, 0, 0);
230 B(1, 1, 1, 1);
231 B(2, 2, 2, 2);
232 B(3, 3, 3, 3);
233 B(0, 0, 1, 1);
234 B(2, 2, 3, 3);
235 B(1, 0, 3, 2);
236 B(3, 2, 1, 0);
237 B(0, 0, 2, 2);
238 B(1, 1, 0, 0);
239 B(2, 2, 0, 0);
240 B(3, 3, 0, 0);
241 B(2, 2, 1, 1);
242 B(3, 3, 1, 1);
243 B(1, 1, 2, 2);
244 B(3, 3, 2, 2);
245 B(0, 0, 3, 3);
246 B(1, 1, 3, 3);
247 }
248
249 #undef H
250 #undef B
251
252 UNREACHABLE("Invalid swizzle");
253 }
254
255 static inline bool
256 bi_swizzle_replicates_8(enum bi_swizzle swz)
257 {
258 switch (swz) {
259 case BI_SWIZZLE_B0000:
260 case BI_SWIZZLE_B1111:
261 case BI_SWIZZLE_B2222:
262 case BI_SWIZZLE_B3333:
263 return true;
264 default:
265 return false;
266 }
267 }
268
269 static inline bool
270 bi_swizzle_replicates_16(enum bi_swizzle swz)
271 {
272 switch (swz) {
273 case BI_SWIZZLE_H00:
274 case BI_SWIZZLE_H11:
275 return true;
276 default:
277 /* If a swizzle replicates every 8-bits, it also replicates
278 * every 16-bits, so allow 8-bit replicating swizzles.
279 */
280 return bi_swizzle_replicates_8(swz);
281 }
282 }
283
284 enum bi_index_type {
285 BI_INDEX_NULL = 0,
286 BI_INDEX_NORMAL = 1,
287 BI_INDEX_REGISTER = 2,
288 BI_INDEX_CONSTANT = 3,
289 BI_INDEX_PASS = 4,
290 BI_INDEX_FAU = 5
291 };
292
293 typedef struct {
294 uint32_t value;
295
296 /* modifiers, should only be set if applicable for a given instruction.
297 * For *IDP.v4i8, abs plays the role of sign. For bitwise ops where
298 * applicable, neg plays the role of not */
299 bool abs : 1;
300 bool neg : 1;
301
302 /* The last use of a value, should be purged from the register cache.
303 * Set by liveness analysis. */
304 bool discard : 1;
305
306 /* For a source, the swizzle. For a destination, acts a bit like a
307 * write mask. Identity for the full 32-bit, H00 for only caring about
308 * the lower half, other values unused. */
309 enum bi_swizzle swizzle : 5;
310 uint32_t offset : 3;
311 enum bi_index_type type : 3;
312
313 /* Last use of an SSA value; similar to discard, but applies to the
314 * SSA analysis and does not have any HW restrictions (discard gets
315 * sent to the hardware eventually. */
316 bool kill_ssa : 1;
317
318 /* Register class */
319 bool memory : 1;
320
321 /* Must be zeroed so we can hash the whole 64-bits at a time */
322 unsigned padding : (32 - 16);
323 } bi_index;
324
325 static inline bi_index
326 bi_get_index(unsigned value)
327 {
328 return (bi_index){
329 .value = value,
330 .swizzle = BI_SWIZZLE_H01,
331 .type = BI_INDEX_NORMAL,
332 };
333 }
334
335 enum ra_class {
336 /* General purpose register */
337 RA_GPR,
338
339 /* Memory, used to assign stack slots */
340 RA_MEM,
341
342 /* Keep last */
343 RA_CLASSES,
344 };
345
346 static inline enum ra_class
347 ra_class_for_index(bi_index idx)
348 {
349 return idx.memory ? RA_MEM : RA_GPR;
350 }
351
352 static inline bi_index
353 bi_register(unsigned reg)
354 {
355 assert(reg < 64);
356
357 return (bi_index){
358 .value = reg,
359 .swizzle = BI_SWIZZLE_H01,
360 .type = BI_INDEX_REGISTER,
361 };
362 }
363
364 static inline bi_index
365 bi_imm_u32(uint32_t imm)
366 {
367 return (bi_index){
368 .value = imm,
369 .swizzle = BI_SWIZZLE_H01,
370 .type = BI_INDEX_CONSTANT,
371 };
372 }
373
374 static inline bi_index
375 bi_imm_f32(float imm)
376 {
377 return bi_imm_u32(fui(imm));
378 }
379
380 static inline bi_index
381 bi_null()
382 {
383 return (bi_index){.type = BI_INDEX_NULL};
384 }
385
386 static inline bi_index
387 bi_zero()
388 {
389 return bi_imm_u32(0);
390 }
391
392 static inline bi_index
393 bi_passthrough(enum bifrost_packed_src value)
394 {
395 return (bi_index){
396 .value = value,
397 .swizzle = BI_SWIZZLE_H01,
398 .type = BI_INDEX_PASS,
399 };
400 }
401
402 /* Helps construct swizzles */
403 static inline bi_index
404 bi_swz_16(bi_index idx, bool x, bool y)
405 {
406 bool halves[2];
407 switch (idx.swizzle) {
408 case BI_SWIZZLE_H00: halves[0] = 0; halves[1] = 0; break;
409 case BI_SWIZZLE_H01: halves[0] = 0; halves[1] = 1; break;
410 case BI_SWIZZLE_H10: halves[0] = 1; halves[1] = 0; break;
411 case BI_SWIZZLE_H11: halves[0] = 1; halves[1] = 1; break;
412 default: UNREACHABLE("Not a half swizzle");
413 }
414 assert(idx.swizzle == bi_swizzle_from_half(halves[0], halves[1]));
415
416 idx.swizzle = bi_swizzle_from_half(halves[x], halves[y]);
417 return idx;
418 }
419
420 static inline bi_index
421 bi_half(bi_index idx, bool upper)
422 {
423 return bi_swz_16(idx, upper, upper);
424 }
425
426 static inline bi_index
427 bi_byte(bi_index idx, unsigned lane)
428 {
429 unsigned bytes[4];
430 bi_swizzle_to_byte_channels(idx.swizzle, bytes);
431
432 assert(lane < 4);
433 idx.swizzle = (enum bi_swizzle)(BI_SWIZZLE_B0 + bytes[lane]);
434 return idx;
435 }
436
437 static inline bi_index
438 bi_abs(bi_index idx)
439 {
440 idx.abs = true;
441 return idx;
442 }
443
444 static inline bi_index
445 bi_neg(bi_index idx)
446 {
447 idx.neg ^= true;
448 return idx;
449 }
450
451 static inline bi_index
452 bi_discard(bi_index idx)
453 {
454 idx.discard = true;
455 return idx;
456 }
457
458 /* Additive identity in IEEE 754 arithmetic */
459 static inline bi_index
460 bi_negzero()
461 {
462 return bi_neg(bi_zero());
463 }
464
465 /* Replaces an index, preserving any modifiers */
466
467 static inline bi_index
468 bi_replace_index(bi_index old, bi_index replacement)
469 {
470 replacement.abs = old.abs;
471 replacement.neg = old.neg;
472 replacement.swizzle = old.swizzle;
473 replacement.discard = false; /* needs liveness analysis to set */
474 return replacement;
475 }
476
477 /* Remove any modifiers. This has the property:
478 *
479 * replace_index(x, strip_index(x)) = x
480 *
481 * This ensures it is suitable to use when lowering sources to moves */
482
483 static inline bi_index
484 bi_strip_index(bi_index index)
485 {
486 index.abs = index.neg = false;
487 index.swizzle = BI_SWIZZLE_H01;
488 return index;
489 }
490
491 /* For bitwise instructions */
492 #define bi_not(x) bi_neg(x)
493
494 static inline bi_index
495 bi_imm_u8(uint8_t imm)
496 {
497 return bi_byte(bi_imm_u32(imm), 0);
498 }
499
500 static inline bi_index
501 bi_imm_u16(uint16_t imm)
502 {
503 return bi_half(bi_imm_u32(imm), false);
504 }
505
506 static inline bi_index
507 bi_imm_uintN(uint32_t imm, unsigned sz)
508 {
509 assert(sz == 8 || sz == 16 || sz == 32);
510 return (sz == 8) ? bi_imm_u8(imm)
511 : (sz == 16) ? bi_imm_u16(imm)
512 : bi_imm_u32(imm);
513 }
514
515 static inline bi_index
516 bi_imm_f16(float imm)
517 {
518 return bi_imm_u16(_mesa_float_to_half(imm));
519 }
520
521 static inline bool
522 bi_is_null(bi_index idx)
523 {
524 return idx.type == BI_INDEX_NULL;
525 }
526
527 static inline bool
528 bi_is_ssa(bi_index idx)
529 {
530 return idx.type == BI_INDEX_NORMAL;
531 }
532
533 static inline bool
534 bi_is_zero(const bi_index idx)
535 {
536 return idx.type == BI_INDEX_CONSTANT && idx.value == 0;
537 }
538
539 /* Compares equivalence as references. Does not compare offsets, swizzles, or
540 * modifiers. In other words, this forms bi_index equivalence classes by
541 * partitioning memory. E.g. -abs(foo[1].yx) == foo.xy but foo != bar */
542
543 static inline bool
544 bi_is_equiv(bi_index left, bi_index right)
545 {
546 return (left.type == right.type) && (left.value == right.value);
547 }
548
549 /* A stronger equivalence relation that requires the indices access the
550 * same offset, useful for RA/scheduling to see what registers will
551 * correspond to */
552
553 static inline bool
554 bi_is_word_equiv(bi_index left, bi_index right)
555 {
556 return bi_is_equiv(left, right) && left.offset == right.offset;
557 }
558
559 /* An even stronger equivalence that checks if indices correspond to the
560 * right value when evaluated
561 */
562 static inline bool
563 bi_is_value_equiv(bi_index left, bi_index right)
564 {
565 if (left.type == BI_INDEX_CONSTANT && right.type == BI_INDEX_CONSTANT) {
566 return (bi_apply_swizzle(left.value, left.swizzle) ==
567 bi_apply_swizzle(right.value, right.swizzle)) &&
568 (left.abs == right.abs) && (left.neg == right.neg);
569 } else {
570 return (left.value == right.value) && (left.abs == right.abs) &&
571 (left.neg == right.neg) && (left.swizzle == right.swizzle) &&
572 (left.offset == right.offset) && (left.type == right.type);
573 }
574 }
575
576 #define BI_MAX_VEC 16
577 #define BI_MAX_DESTS 4
578 #define BI_MAX_SRCS 8
579
580 typedef struct {
581 /* Must be first */
582 struct list_head link;
583 bi_index *dest;
584 bi_index *src;
585
586 enum bi_opcode op;
587 uint8_t nr_srcs;
588 uint8_t nr_dests;
589
590 union {
591 /* For a branch */
592 struct bi_block *branch_target;
593
594 /* For a phi node that hasn't been translated yet. This is only
595 * used during NIR->BIR
596 */
597 nir_phi_instr *phi;
598 };
599
600 /* These don't fit neatly with anything else.. */
601 enum bi_register_format register_format;
602 enum bi_vecsize vecsize;
603
604 /* Flow control associated with a Valhall instruction */
605 uint8_t flow;
606
607 /* Valhall-only property to relax waits on read-only resources */
608 bool wait_resource;
609
610 /* Slot associated with a message-passing instruction */
611 uint8_t slot;
612
613 /* Can we spill the value written here? Used to prevent
614 * useless double fills */
615 bool no_spill;
616
617 /* Tags the gl_PointSize memory write, this is used if we want to
618 * create a variant without psiz writes */
619 bool is_psiz_write;
620
621 /* On Bifrost: A value of bi_table to override the table, inducing a
622 * DTSEL_IMM pair if nonzero.
623 *
624 * On Valhall: the table index to use for resource instructions.
625 *
626 * These two interpretations are equivalent if you squint a bit.
627 */
628 unsigned table;
629
630 /* Everything after this MUST NOT be accessed directly, since
631 * interpretation depends on opcodes */
632
633 /* Destination modifiers */
634 union {
635 enum bi_clamp clamp;
636 bool saturate;
637 bool not_result;
638 unsigned dest_mod;
639 };
640
641 /* Immediates. All seen alone in an instruction, except for varying/texture
642 * which are specified jointly for VARTEX */
643 union {
644 uint32_t shift;
645 uint32_t fill;
646 uint32_t index;
647 uint32_t attribute_index;
648
649 struct {
650 uint32_t varying_index;
651 uint32_t sampler_index;
652 uint32_t texture_index;
653 };
654
655 /* TEXC, ATOM_CX: # of staging registers used */
656 struct {
657 uint32_t sr_count;
658 uint32_t sr_count_2;
659
660 union {
661 /* Atomics effectively require all three */
662 int32_t byte_offset;
663
664 /* BLEND requires all three */
665 int32_t branch_offset;
666 };
667 };
668 };
669
670 /* Modifiers specific to particular instructions are thrown in a union */
671 union {
672 enum bi_adj adj; /* FEXP_TABLE.u4 */
673 enum bi_atom_opc atom_opc; /* atomics */
674 enum bi_func func; /* FPOW_SC_DET */
675 enum bi_function function; /* LD_VAR_FLAT */
676 enum bi_mux mux; /* MUX */
677 enum bi_sem sem; /* FMAX, FMIN */
678 enum bi_source source; /* LD_GCLK */
679 bool scale; /* VN_ASST2, FSINCOS_OFFSET */
680 bool offset; /* FSIN_TABLE, FOCS_TABLE */
681 bool mask; /* CLZ */
682 bool threads; /* IMULD, IMOV_FMA */
683 bool combine; /* BRANCHC */
684 bool format; /* LEA_TEX */
685 bool z_stencil; /* LD_TILE */
686 bool scheduling_barrier; /* NOP */
687 unsigned blend_target; /* BLEND */
688
689 struct {
690 enum bi_special special; /* FADD_RSCALE, FMA_RSCALE */
691 enum bi_round round; /* FMA, converts, FADD, _RSCALE, etc */
692 bool ftz; /* Flush-to-zero for F16_TO_F32 and FLUSH */
693 enum va_nan_mode nan_mode; /* NaN flush mode, for FLUSH */
694 bool flush_inf; /* Flush infinity to finite, for FLUSH */
695 };
696
697 struct {
698 enum bi_result_type result_type; /* FCMP, ICMP */
699 enum bi_cmpf cmpf; /* CSEL, FCMP, ICMP, BRANCH */
700 };
701
702 struct {
703 enum bi_stack_mode stack_mode; /* JUMP_EX */
704 bool test_mode;
705 };
706
707 struct {
708 enum bi_seg seg; /* LOAD, STORE, SEG_ADD, SEG_SUB */
709 enum va_memory_access mem_access; /* LOAD, STORE, LD_CVT, ST_CVT */
710 bool preserve_null; /* SEG_ADD, SEG_SUB */
711 enum bi_extend extend; /* LOAD, IMUL */
712 };
713
714 struct {
715 enum bi_sample sample; /* VAR_TEX, LD_VAR */
716 enum bi_update update; /* VAR_TEX, LD_VAR */
717 enum bi_varying_name varying_name; /* LD_VAR_SPECIAL */
718 bool skip; /* VAR_TEX, TEXS, TEXC */
719 bool lod_mode; /* VAR_TEX, TEXS, implicitly for TEXC */
720 enum bi_source_format source_format; /* LD_VAR_BUF */
721
722 /* Used for valhall texturing */
723 bool shadow;
724 bool wide_indices;
725 bool texel_offset;
726 bool array_enable;
727 bool integer_coordinates;
728 bool derivative_enable;
729 bool force_delta_enable;
730 bool lod_bias_disable;
731 bool lod_clamp_disable;
732 enum bi_fetch_component fetch_component;
733 enum bi_va_lod_mode va_lod_mode;
734 enum bi_dimension dimension;
735 enum bi_write_mask write_mask;
736 };
737
738 /* Maximum size, for hashing */
739 unsigned flags[14];
740
741 struct {
742 enum bi_subgroup subgroup; /* WMASK, CLPER */
743 enum bi_inactive_result inactive_result; /* CLPER */
744 enum bi_lane_op lane_op; /* CLPER */
745 };
746
747 struct {
748 bool z; /* ZS_EMIT */
749 bool stencil; /* ZS_EMIT */
750 };
751
752 struct {
753 bool h; /* VN_ASST1.f16 */
754 bool l; /* VN_ASST1.f16 */
755 };
756
757 struct {
758 bool bytes2; /* RROT_DOUBLE, FRSHIFT_DOUBLE */
759 bool result_word;
760 bool arithmetic; /* ARSHIFT_OR */
761 };
762
763 struct {
764 bool sqrt; /* FREXPM */
765 bool log; /* FREXPM */
766 };
767
768 struct {
769 enum bi_mode mode; /* FLOG_TABLE */
770 enum bi_precision precision; /* FLOG_TABLE */
771 bool divzero; /* FRSQ_APPROX, FRSQ */
772 };
773 };
774
775 const nir_instr_debug_info *debug_info;
776 } bi_instr;
777
778 /*
779 * Helpers to set opcode and to get properties related to
780 * the opcode. In principle this would allow different
781 * properties to be used based on the architecture. In practice
782 * we've unified the valhall/bifrost descriptions so this
783 * isn't necessary now. We may want it for a future architecture
784 * though.
785 */
786 static inline void
787 bi_set_opcode(bi_instr *I, enum bi_opcode opc)
788 {
789 I->op = opc;
790 }
791
792 static inline struct bi_op_props *
793 bi_get_opcode_props(const bi_instr *I)
794 {
795 return &bi_opcode_props[I->op];
796 }
797
798 static inline bool
799 bi_is_staging_src(const bi_instr *I, unsigned s)
800 {
801 return (s == 0 || s == 4) && bi_get_opcode_props(I)->sr_read;
802 }
803
804 static inline bool
805 bi_is_scheduling_barrier(const bi_instr *I)
806 {
807 return I->op == BI_OPCODE_NOP && I->scheduling_barrier;
808 }
809
810 static inline bool
811 bi_is_branch(const bi_instr *I)
812 {
813 return I != NULL && bi_opcode_props[I->op].branch;
814 }
815
816 /*
817 * Safe helpers to remove destinations/sources at the end of the
818 * destination/source array when changing opcodes. Unlike adding
819 * sources/destinations, this does not require reallocation.
820 */
821 static inline void
822 bi_drop_dests(bi_instr *I, unsigned new_count)
823 {
824 assert(new_count < I->nr_dests);
825
826 for (unsigned i = new_count; i < I->nr_dests; ++i)
827 I->dest[i] = bi_null();
828
829 I->nr_dests = new_count;
830 }
831
832 static inline void
833 bi_drop_srcs(bi_instr *I, unsigned new_count)
834 {
835 assert(new_count < I->nr_srcs);
836
837 for (unsigned i = new_count; i < I->nr_srcs; ++i)
838 I->src[i] = bi_null();
839
840 I->nr_srcs = new_count;
841 }
842
843 static inline void
844 bi_replace_src(bi_instr *I, unsigned src_index, bi_index replacement)
845 {
846 I->src[src_index] = bi_replace_index(I->src[src_index], replacement);
847 }
848
849 /* Represents the assignment of slots for a given bi_tuple */
850
851 typedef struct {
852 /* Register to assign to each slot */
853 unsigned slot[4];
854
855 /* Read slots can be disabled */
856 bool enabled[2];
857
858 /* Configuration for slots 2/3 */
859 struct bifrost_reg_ctrl_23 slot23;
860
861 /* Fast-Access-Uniform RAM index */
862 uint8_t fau_idx;
863
864 /* Whether writes are actually for the last instruction */
865 bool first_instruction;
866 } bi_registers;
867
868 /* A bi_tuple contains two paired instruction pointers. If a slot is unfilled,
869 * leave it NULL; the emitter will fill in a nop. Instructions reference
870 * registers via slots which are assigned per tuple.
871 */
872
873 typedef struct {
874 uint8_t fau_idx;
875 bi_registers regs;
876 bi_instr *fma;
877 bi_instr *add;
878 } bi_tuple;
879
880 struct bi_block;
881
882 typedef struct {
883 struct list_head link;
884
885 /* Link back up for branch calculations */
886 struct bi_block *block;
887
888 /* Architectural limit of 8 tuples/clause */
889 unsigned tuple_count;
890 bi_tuple tuples[8];
891
892 /* For scoreboarding -- the clause ID (this is not globally unique!)
893 * and its dependencies in terms of other clauses, computed during
894 * scheduling and used when emitting code. Dependencies expressed as a
895 * bitfield matching the hardware, except shifted by a clause (the
896 * shift back to the ISA's off-by-one encoding is worked out when
897 * emitting clauses) */
898 unsigned scoreboard_id;
899 uint8_t dependencies;
900
901 /* See ISA header for description */
902 enum bifrost_flow flow_control;
903
904 /* Can we prefetch the next clause? Usually it makes sense, except for
905 * clauses ending in unconditional branches */
906 bool next_clause_prefetch;
907
908 /* Assigned data register */
909 unsigned staging_register;
910
911 /* Corresponds to the usual bit but shifted by a clause */
912 bool staging_barrier;
913
914 /* Constants read by this clause. ISA limit. Must satisfy:
915 *
916 * constant_count + tuple_count <= 13
917 *
918 * Also implicitly constant_count <= tuple_count since a tuple only
919 * reads a single constant.
920 */
921 uint64_t constants[8];
922 unsigned constant_count;
923
924 /* Index of a constant to be PC-relative */
925 unsigned pcrel_idx;
926
927 /* Branches encode a constant offset relative to the program counter
928 * with some magic flags. By convention, if there is a branch, its
929 * constant will be last. Set this flag to indicate this is required.
930 */
931 bool branch_constant;
932
933 /* Unique in a clause */
934 enum bifrost_message_type message_type;
935 bi_instr *message;
936
937 /* Discard helper threads */
938 bool td;
939
940 /* Should flush-to-zero mode be enabled for this clause? */
941 bool ftz;
942 } bi_clause;
943
944 #define BI_NUM_SLOTS 8
945
946 /* A model for the state of the scoreboard */
947 struct bi_scoreboard_state {
948 /** Bitmap of registers read/written by a slot */
949 uint64_t read[BI_NUM_SLOTS];
950 uint64_t write[BI_NUM_SLOTS];
951
952 /* Nonregister dependencies present by a slot */
953 uint8_t varying : BI_NUM_SLOTS;
954 uint8_t memory : BI_NUM_SLOTS;
955 };
956
957 typedef struct bi_block {
958 /* Link to next block. Must be first for mir_get_block */
959 struct list_head link;
960
961 /* List of instructions emitted for the current block */
962 struct list_head instructions;
963
964 /* Index of the block in source order */
965 unsigned index;
966
967 /* Control flow graph */
968 struct bi_block *successors[2];
969 struct util_dynarray predecessors;
970 bool unconditional_jumps;
971 bool loop_header;
972
973 /* Per 32-bit word live masks for the block indexed by node */
974 uint8_t *live_in;
975 uint8_t *live_out;
976
977 /* Scalar liveness indexed by SSA index */
978 BITSET_WORD *ssa_live_in;
979 BITSET_WORD *ssa_live_out;
980 uint32_t ssa_max_live;
981
982 /* If true, uses clauses; if false, uses instructions */
983 bool scheduled;
984 struct list_head clauses; /* list of bi_clause */
985
986 /* Post-RA liveness */
987 uint64_t reg_live_in, reg_live_out;
988
989 /* Scoreboard state at the start/end of block */
990 struct bi_scoreboard_state scoreboard_in, scoreboard_out;
991
992 /* On Valhall, indicates we need a terminal NOP to implement jumps to
993 * the end of the shader.
994 */
995 bool needs_nop;
996
997 /* Flags available for pass-internal use */
998 uint8_t pass_flags;
999
1000 /*
1001 * this node's immediate dominator in the dominance tree - set to NULL for
1002 * the start block and any unreachable blocks.
1003 */
1004 struct bi_block *imm_dom;
1005
1006 /* This node's children in the dominance tree */
1007 unsigned num_dom_children;
1008 struct bi_block **dom_children;
1009
1010 /*
1011 * This is the backing storage for "dom_children" if the array is small
1012 * enough to fit in it, so that we don't have to ralloc the array.
1013 */
1014 struct bi_block *_dom_children_storage[3];
1015
1016 /* Set of nir_blocks on the dominance frontier of this block */
1017 struct set dom_frontier;
1018
1019 /*
1020 * These two indices have the property that dom_{pre,post}_index for each
1021 * child of this block in the dominance tree will always be between
1022 * dom_pre_index and dom_post_index for this block, which makes testing if
1023 * a given block is dominated by another block an O(1) operation.
1024 */
1025 uint32_t dom_pre_index, dom_post_index;
1026 } bi_block;
1027
1028 static inline unsigned
1029 bi_num_successors(bi_block *block)
1030 {
1031 STATIC_ASSERT(ARRAY_SIZE(block->successors) == 2);
1032 assert(block->successors[0] || !block->successors[1]);
1033
1034 if (block->successors[1])
1035 return 2;
1036 else if (block->successors[0])
1037 return 1;
1038 else
1039 return 0;
1040 }
1041
1042 static inline unsigned
1043 bi_num_predecessors(bi_block *block)
1044 {
1045 return util_dynarray_num_elements(&block->predecessors, bi_block *);
1046 }
1047
1048 static inline bi_block *
1049 bi_start_block(struct list_head *blocks)
1050 {
1051 bi_block *first = list_first_entry(blocks, bi_block, link);
1052 assert(bi_num_predecessors(first) == 0);
1053 return first;
1054 }
1055
1056 static inline bi_block *
1057 bi_exit_block(struct list_head *blocks)
1058 {
1059 bi_block *last = list_last_entry(blocks, bi_block, link);
1060 assert(bi_num_successors(last) == 0);
1061 return last;
1062 }
1063
1064 static inline void
1065 bi_block_add_successor(bi_block *block, bi_block *successor)
1066 {
1067 assert(block != NULL && successor != NULL);
1068
1069 /* Cull impossible edges */
1070 if (block->unconditional_jumps)
1071 return;
1072
1073 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
1074 if (block->successors[i]) {
1075 if (block->successors[i] == successor)
1076 return;
1077 else
1078 continue;
1079 }
1080
1081 block->successors[i] = successor;
1082 util_dynarray_append(&successor->predecessors, block);
1083 return;
1084 }
1085
1086 UNREACHABLE("Too many successors");
1087 }
1088
1089 /* Subset of pan_shader_info needed per-variant, in order to support IDVS */
1090 struct bi_shader_info {
1091 struct pan_ubo_push *push;
1092 struct bifrost_shader_info *bifrost;
1093 struct pan_stats stats;
1094 unsigned tls_size;
1095 unsigned work_reg_count;
1096 unsigned push_offset;
1097 unsigned init_fau_consts_count;
1098 bool has_ld_gclk_instr;
1099 };
1100
1101 /* State of index-driven vertex shading for current shader */
1102 enum bi_idvs_mode {
1103 /* IDVS not in use */
1104 BI_IDVS_NONE = 0,
1105
1106 /* IDVS in use. Compiling a position shader */
1107 BI_IDVS_POSITION = 1,
1108
1109 /* IDVS in use. Compiling a varying shader */
1110 BI_IDVS_VARYING = 2,
1111
1112 /* IDVS2 in use. Compiling a deferred shader (v12+) */
1113 BI_IDVS_ALL = 3,
1114 };
1115
1116 #define BI_MAX_REGS 64
1117
1118 enum bi_preload {
1119 /* Compute */
1120 BI_PRELOAD_LOCAL_ID_0,
1121 BI_PRELOAD_LOCAL_ID_1,
1122 BI_PRELOAD_LOCAL_ID_2,
1123 BI_PRELOAD_WORKGROUP_ID_0,
1124 BI_PRELOAD_WORKGROUP_ID_1,
1125 BI_PRELOAD_WORKGROUP_ID_2,
1126 BI_PRELOAD_GLOBAL_ID_0,
1127 BI_PRELOAD_GLOBAL_ID_1,
1128 BI_PRELOAD_GLOBAL_ID_2,
1129 /* Vertex */
1130 BI_PRELOAD_POS_RESULT_PTR_LO,
1131 BI_PRELOAD_POS_RESULT_PTR_HI,
1132 BI_PRELOAD_INTERNAL_ID,
1133 BI_PRELOAD_VERTEX_ID,
1134 BI_PRELOAD_INSTANCE_ID,
1135 BI_PRELOAD_DRAW_ID,
1136 BI_PRELOAD_VIEW_ID,
1137 /* Fragment */
1138 BI_PRELOAD_PRIMITIVE_ID,
1139 BI_PRELOAD_PRIMITIVE_FLAGS,
1140 BI_PRELOAD_POSITION_XY,
1141 BI_PRELOAD_CUMULATIVE_COVERAGE,
1142 BI_PRELOAD_RASTERIZER_COVERAGE,
1143 BI_PRELOAD_SAMPLE_ID,
1144 BI_PRELOAD_CENTROID_ID,
1145 BI_PRELOAD_FRAME_ARG,
1146 /* Blend */
1147 BI_PRELOAD_BLEND_SRC0_C0,
1148 BI_PRELOAD_BLEND_SRC0_C1,
1149 BI_PRELOAD_BLEND_SRC0_C2,
1150 BI_PRELOAD_BLEND_SRC0_C3,
1151 BI_PRELOAD_BLEND_SRC1_C0,
1152 BI_PRELOAD_BLEND_SRC1_C1,
1153 BI_PRELOAD_BLEND_SRC1_C2,
1154 BI_PRELOAD_BLEND_SRC1_C3,
1155 BI_PRELOAD_BLEND_LINK,
1156 };
1157
1158 static inline unsigned
1159 bi_preload_reg(enum bi_preload val, unsigned arch)
1160 {
1161 switch (val) {
1162 /* Compute */
1163 case BI_PRELOAD_LOCAL_ID_0:
1164 /* Bits [15;0] */
1165 return 55;
1166 case BI_PRELOAD_LOCAL_ID_1:
1167 /* Bits [31;16] */
1168 return 55;
1169 case BI_PRELOAD_LOCAL_ID_2:
1170 /* Bits [15;0] */
1171 return 56;
1172 case BI_PRELOAD_WORKGROUP_ID_0:
1173 return 57;
1174 case BI_PRELOAD_WORKGROUP_ID_1:
1175 return 58;
1176 case BI_PRELOAD_WORKGROUP_ID_2:
1177 return 59;
1178 case BI_PRELOAD_GLOBAL_ID_0:
1179 return 60;
1180 case BI_PRELOAD_GLOBAL_ID_1:
1181 return 61;
1182 case BI_PRELOAD_GLOBAL_ID_2:
1183 return 62;
1184 /* Vertex */
1185 case BI_PRELOAD_POS_RESULT_PTR_LO:
1186 assert(arch < 9);
1187 return 58;
1188 case BI_PRELOAD_POS_RESULT_PTR_HI:
1189 assert(arch < 9);
1190 return 59;
1191 case BI_PRELOAD_INTERNAL_ID:
1192 assert(arch >= 9);
1193 return 59;
1194 case BI_PRELOAD_VERTEX_ID:
1195 return (arch >= 9) ? 60 : 61;
1196 case BI_PRELOAD_INSTANCE_ID:
1197 return (arch >= 9) ? 61 : 62;
1198 case BI_PRELOAD_DRAW_ID:
1199 assert(arch >= 9);
1200 return 62;
1201 case BI_PRELOAD_VIEW_ID:
1202 assert(arch >= 9);
1203 return 63;
1204 /* Fragment */
1205 case BI_PRELOAD_PRIMITIVE_ID:
1206 return 57;
1207 case BI_PRELOAD_PRIMITIVE_FLAGS:
1208 return 58;
1209 case BI_PRELOAD_POSITION_XY:
1210 return 59;
1211 case BI_PRELOAD_CUMULATIVE_COVERAGE:
1212 /* Bits [15;0] */
1213 return 60;
1214 case BI_PRELOAD_RASTERIZER_COVERAGE:
1215 /* Bits [15;0] */
1216 return 61;
1217 case BI_PRELOAD_SAMPLE_ID:
1218 /* Bits [23;16] */
1219 return 61;
1220 case BI_PRELOAD_CENTROID_ID:
1221 /* Bits [31;24] */
1222 return 61;
1223 case BI_PRELOAD_FRAME_ARG:
1224 /* Double reg */
1225 return 62;
1226 /* Blend */
1227 case BI_PRELOAD_BLEND_SRC0_C0:
1228 return 0;
1229 case BI_PRELOAD_BLEND_SRC0_C1:
1230 return 1;
1231 case BI_PRELOAD_BLEND_SRC0_C2:
1232 return 2;
1233 case BI_PRELOAD_BLEND_SRC0_C3:
1234 return 3;
1235 case BI_PRELOAD_BLEND_SRC1_C0:
1236 return 4;
1237 case BI_PRELOAD_BLEND_SRC1_C1:
1238 return 5;
1239 case BI_PRELOAD_BLEND_SRC1_C2:
1240 return 6;
1241 case BI_PRELOAD_BLEND_SRC1_C3:
1242 return 7;
1243 case BI_PRELOAD_BLEND_LINK:
1244 return 48;
1245 }
1246 UNREACHABLE("Non-handled BI_PRELOAD");
1247 }
1248
1249 typedef struct {
1250 const struct pan_compile_inputs *inputs;
1251 nir_shader *nir;
1252 struct bi_shader_info info;
1253 mesa_shader_stage stage;
1254 struct list_head blocks; /* list of bi_block */
1255 uint32_t quirks;
1256 unsigned arch;
1257 enum bi_idvs_mode idvs;
1258 unsigned num_blocks;
1259
1260 /* Floating point rounding mode controls */
1261 bool rtz_fp16;
1262 bool rtz_fp32;
1263 bool ftz_fp32;
1264
1265 /* In any graphics shader, whether the "IDVS with memory
1266 * allocation" flow is used. This affects how varyings are loaded and
1267 * stored. Ignore for compute.
1268 */
1269 bool malloc_idvs;
1270
1271 /* During NIR->BIR */
1272 bi_block *current_block;
1273 bi_block *after_block;
1274 bi_block *break_block;
1275 bi_block *continue_block;
1276 bi_block **indexed_nir_blocks;
1277
1278 /* During NIR->BIR, the coverage bitmap. If this is NULL, the default
1279 * coverage bitmap should be source from preloaded register r60. This is
1280 * written by ATEST and ZS_EMIT
1281 */
1282 bi_index coverage;
1283
1284 /* During NIR->BIR, table of preloaded registers, or NULL if never
1285 * preloaded.
1286 */
1287 bi_index preloaded[BI_MAX_REGS];
1288
1289 uint32_t fau_consts_count;
1290
1291 /* For creating temporaries */
1292 unsigned ssa_alloc;
1293 unsigned reg_alloc;
1294
1295 /* Mask of UBOs that need to be uploaded */
1296 uint32_t ubo_mask;
1297
1298 /* During instruction selection, map from vector bi_index to its scalar
1299 * components, populated by a split.
1300 */
1301 struct hash_table_u64 *allocated_vec;
1302
1303 /* Beginning of our stack allocation used for spilling, below that is
1304 * NIR-level scratch.
1305 */
1306 unsigned spill_base_B;
1307
1308 /* Beginning of stack allocation used for parallel copy lowering */
1309 bool has_spill_pcopy_reserved;
1310 unsigned spill_pcopy_base;
1311
1312 /* Stats for shader-db */
1313 unsigned loop_count;
1314 unsigned spills;
1315 unsigned fills;
1316
1317 /* alignment needed for registers during register allocation */
1318 uint8_t *reg_alignment;
1319
1320 /* Computed after RA */
1321 uint64_t spill_cost;
1322 } bi_context;
1323
1324 static inline enum bi_round
1325 bi_round_mode(bi_context *ctx, unsigned bit_size)
1326 {
1327 assert(bit_size == 16 || bit_size == 32);
1328 bool rtz = bit_size == 16 ? ctx->rtz_fp16 : ctx->rtz_fp32;
1329 return rtz ? BI_ROUND_RTZ : BI_ROUND_NONE;
1330 }
1331
1332 static inline void
1333 bi_remove_instruction(bi_instr *ins)
1334 {
1335 list_del(&ins->link);
1336 }
1337
1338 enum bir_fau {
1339 BIR_FAU_ZERO = 0,
1340 BIR_FAU_LANE_ID = 1,
1341 BIR_FAU_WARP_ID = 2,
1342 BIR_FAU_CORE_ID = 3,
1343 BIR_FAU_FB_EXTENT = 4,
1344 BIR_FAU_ATEST_PARAM = 5,
1345 BIR_FAU_SAMPLE_POS_ARRAY = 6,
1346 BIR_FAU_BLEND_0 = 8,
1347 /* blend descs 1 - 7 */
1348 BIR_FAU_TYPE_MASK = 15,
1349
1350 /* Valhall only */
1351 BIR_FAU_TLS_PTR = 16,
1352 BIR_FAU_WLS_PTR = 17,
1353 BIR_FAU_PROGRAM_COUNTER = 18,
1354
1355 /* 5th Gen only */
1356 BIR_FAU_SHADER_OUTPUT = (1 << 9),
1357
1358 BIR_FAU_UNIFORM = (1 << 7),
1359 /* Look up table on Valhall */
1360 BIR_FAU_IMMEDIATE = (1 << 8),
1361
1362 };
1363
1364 static inline bi_index
1365 bi_fau(enum bir_fau value, bool hi)
1366 {
1367 return (bi_index){
1368 .value = value,
1369 .swizzle = BI_SWIZZLE_H01,
1370 .offset = hi ? 1u : 0u,
1371 .type = BI_INDEX_FAU,
1372 };
1373 }
1374
1375 /*
1376 * Builder for Valhall LUT entries. Generally, constants are modeled with
1377 * BI_INDEX_IMMEDIATE in the intermediate representation. This helper is only
1378 * necessary for passes running after lowering constants, as well as when
1379 * lowering constants.
1380 *
1381 */
1382 static inline bi_index
1383 va_lut(unsigned index)
1384 {
1385 return bi_fau((enum bir_fau)(BIR_FAU_IMMEDIATE | (index >> 1)), index & 1);
1386 }
1387
1388 /*
1389 * va_lut_zero is like bi_zero but only works on Valhall. It is intended for
1390 * use by late passes that run after constants are lowered, specifically
1391 * register allocation. bi_zero() is preferred where possible.
1392 */
1393 static inline bi_index
1394 va_zero_lut()
1395 {
1396 return va_lut(0);
1397 }
1398
1399 static inline bi_index
1400 bi_temp(bi_context *ctx)
1401 {
1402 return bi_get_index(ctx->ssa_alloc++);
1403 }
1404
1405 static inline bi_index
1406 bi_temp_like(bi_context *ctx, bi_index idx)
1407 {
1408 idx.value = ctx->ssa_alloc++;
1409 /* Reset the fields which don't make sense on a destination.
1410 * When the temp replaces a source, bi_replace_index copies these from the
1411 * old index.
1412 */
1413 idx.abs = false;
1414 idx.neg = false;
1415 idx.swizzle = BI_SWIZZLE_H01;
1416 idx.discard = false;
1417 return idx;
1418 }
1419
1420 static inline bi_index
1421 bi_def_index(nir_def *def)
1422 {
1423 return bi_get_index(def->index);
1424 }
1425
1426 /* Inline constants automatically, will be lowered out by bi_lower_fau where a
1427 * constant is not allowed. load_const_to_scalar gaurantees that this makes
1428 * sense */
1429
1430 static inline bi_index
1431 bi_src_index(nir_src *src)
1432 {
1433 if (nir_src_is_const(*src)) {
1434 unsigned bit_size = nir_src_bit_size(*src);
1435 unsigned num_comps = nir_src_num_components(*src);
1436 if (bit_size * num_comps <= 32) {
1437 uint32_t imm = 0;
1438 for (uint32_t i = 0; i < num_comps; i++)
1439 imm |= nir_src_comp_as_uint(*src, i) << (i * bit_size);
1440 return bi_imm_u32(imm);
1441 }
1442 }
1443
1444 return bi_def_index(src->ssa);
1445 }
1446
1447 /* Iterators for Bifrost IR */
1448
1449 #define bi_foreach_block(ctx, v) \
1450 list_for_each_entry(bi_block, v, &ctx->blocks, link)
1451
1452 #define bi_foreach_block_rev(ctx, v) \
1453 list_for_each_entry_rev(bi_block, v, &ctx->blocks, link)
1454
1455 #define bi_foreach_block_from(ctx, from, v) \
1456 list_for_each_entry_from(bi_block, v, from, &ctx->blocks, link)
1457
1458 #define bi_foreach_block_from_rev(ctx, from, v) \
1459 list_for_each_entry_from_rev(bi_block, v, from, &ctx->blocks, link)
1460
1461 #define bi_foreach_instr_in_block(block, v) \
1462 list_for_each_entry(bi_instr, v, &(block)->instructions, link)
1463
1464 #define bi_foreach_instr_in_block_rev(block, v) \
1465 list_for_each_entry_rev(bi_instr, v, &(block)->instructions, link)
1466
1467 #define bi_foreach_instr_in_block_safe(block, v) \
1468 list_for_each_entry_safe(bi_instr, v, &(block)->instructions, link)
1469
1470 #define bi_foreach_instr_in_block_safe_rev(block, v) \
1471 list_for_each_entry_safe_rev(bi_instr, v, &(block)->instructions, link)
1472
1473 #define bi_foreach_instr_in_block_from(block, v, from) \
1474 list_for_each_entry_from(bi_instr, v, from, &(block)->instructions, link)
1475
1476 #define bi_foreach_instr_in_block_from_rev(block, v, from) \
1477 list_for_each_entry_from_rev(bi_instr, v, from, &(block)->instructions, link)
1478
1479 #define bi_foreach_clause_in_block(block, v) \
1480 list_for_each_entry(bi_clause, v, &(block)->clauses, link)
1481
1482 #define bi_foreach_clause_in_block_rev(block, v) \
1483 list_for_each_entry_rev(bi_clause, v, &(block)->clauses, link)
1484
1485 #define bi_foreach_clause_in_block_safe(block, v) \
1486 list_for_each_entry_safe(bi_clause, v, &(block)->clauses, link)
1487
1488 #define bi_foreach_clause_in_block_from(block, v, from) \
1489 list_for_each_entry_from(bi_clause, v, from, &(block)->clauses, link)
1490
1491 #define bi_foreach_clause_in_block_from_rev(block, v, from) \
1492 list_for_each_entry_from_rev(bi_clause, v, from, &(block)->clauses, link)
1493
1494 #define bi_foreach_instr_global(ctx, v) \
1495 bi_foreach_block(ctx, v_block) \
1496 bi_foreach_instr_in_block(v_block, v)
1497
1498 #define bi_foreach_instr_global_rev(ctx, v) \
1499 bi_foreach_block_rev(ctx, v_block) \
1500 bi_foreach_instr_in_block_rev(v_block, v)
1501
1502 #define bi_foreach_instr_global_safe(ctx, v) \
1503 bi_foreach_block(ctx, v_block) \
1504 bi_foreach_instr_in_block_safe(v_block, v)
1505
1506 #define bi_foreach_instr_global_rev_safe(ctx, v) \
1507 bi_foreach_block_rev(ctx, v_block) \
1508 bi_foreach_instr_in_block_rev_safe(v_block, v)
1509
1510 #define bi_foreach_instr_in_tuple(tuple, v) \
1511 for (bi_instr *v = (tuple)->fma ?: (tuple)->add; v != NULL; \
1512 v = (v == (tuple)->add) ? NULL : (tuple)->add)
1513
1514 #define bi_foreach_successor(blk, v) \
1515 bi_block *v; \
1516 bi_block **_v; \
1517 for (_v = &blk->successors[0], v = *_v; \
1518 v != NULL && _v < &blk->successors[2]; _v++, v = *_v)
1519
1520 #define bi_foreach_predecessor(blk, v) \
1521 util_dynarray_foreach(&(blk)->predecessors, bi_block *, v)
1522
1523 #define bi_foreach_src(ins, v) for (unsigned v = 0; v < ins->nr_srcs; ++v)
1524 #define bi_foreach_src_rev(ins, v) for (signed v = ins->nr_srcs-1; v >= 0; --v)
1525
1526 #define bi_foreach_dest(ins, v) for (unsigned v = 0; v < ins->nr_dests; ++v)
1527 #define bi_foreach_dest_rev(ins, v) for (signed v = ins->nr_dests-1; v >= 0; --v)
1528
1529 #define bi_foreach_ssa_src(ins, v) \
1530 bi_foreach_src(ins, v) \
1531 if (ins->src[v].type == BI_INDEX_NORMAL)
1532
1533 #define bi_foreach_ssa_src_rev(ins, v) \
1534 bi_foreach_src_rev(ins, v) \
1535 if (ins->src[v].type == BI_INDEX_NORMAL)
1536
1537 #define bi_foreach_ssa_dest(ins, v) \
1538 bi_foreach_dest(ins, v) \
1539 if (ins->dest[v].type == BI_INDEX_NORMAL)
1540
1541 #define bi_foreach_instr_and_src_in_tuple(tuple, ins, s) \
1542 bi_foreach_instr_in_tuple(tuple, ins) \
1543 bi_foreach_src(ins, s)
1544
1545 #define bi_foreach_ssa_dest_rev(ins, v) \
1546 bi_foreach_dest_rev(ins, v) \
1547 if (ins->dest[v].type == BI_INDEX_NORMAL)
1548
1549 /* Phis only come at the start (after else instructions) so we stop as soon as
1550 * we hit a non-phi
1551 */
1552 #define bi_foreach_phi_in_block(block, v) \
1553 bi_foreach_instr_in_block(block, v) \
1554 if (v->op != BI_OPCODE_PHI) \
1555 break; \
1556 else
1557
1558 #define bi_foreach_phi_in_block_safe(block, v) \
1559 bi_foreach_instr_in_block_safe(block, v) \
1560 if (v->op != BI_OPCODE_PHI) \
1561 break; \
1562 else
1563
1564 /*
1565 * Find the index of a predecessor, used as the implicit order of phi sources.
1566 */
1567 static inline unsigned
1568 bi_predecessor_index(bi_block *succ, bi_block *pred)
1569 {
1570 unsigned index = 0;
1571
1572 bi_foreach_predecessor(succ, x) {
1573 if (*x == pred)
1574 return index;
1575
1576 index++;
1577 }
1578
1579 UNREACHABLE("Invalid predecessor");
1580 }
1581
1582 static inline bi_instr *
1583 bi_prev_op(bi_instr *ins)
1584 {
1585 return list_last_entry(&(ins->link), bi_instr, link);
1586 }
1587
1588 static inline bi_instr *
1589 bi_next_op(bi_instr *ins)
1590 {
1591 return list_first_entry(&(ins->link), bi_instr, link);
1592 }
1593
1594 static inline bi_block *
1595 bi_next_block(bi_block *block)
1596 {
1597 return list_first_entry(&(block->link), bi_block, link);
1598 }
1599
1600 static inline bi_block *
1601 bi_entry_block(bi_context *ctx)
1602 {
1603 return list_first_entry(&ctx->blocks, bi_block, link);
1604 }
1605
1606 /* BIR manipulation */
1607
1608 bool bi_has_arg(const bi_instr *ins, bi_index arg);
1609 unsigned bi_count_read_registers(const bi_instr *ins, unsigned src);
1610 unsigned bi_count_write_registers(const bi_instr *ins, unsigned dest);
1611 bool bi_is_regfmt_16(enum bi_register_format fmt);
1612 unsigned bi_writemask(const bi_instr *ins, unsigned dest);
1613 bi_clause *bi_next_clause(bi_context *ctx, bi_block *block, bi_clause *clause);
1614 bool bi_side_effects(const bi_instr *I);
1615 bool bi_reconverge_branches(bi_block *block);
1616
1617 bool bi_can_replace_with_csel(bi_instr *I);
1618
1619 void bi_calc_dominance(bi_context *ctx);
1620 bool bi_block_dominates(bi_block *parent, bi_block *child);
1621
1622 void bi_print_instr(const bi_instr *I, FILE *fp);
1623 void bi_print_instr_impl(const bi_instr *I, FILE *fp);
1624 void bi_print_slots(bi_registers *regs, FILE *fp);
1625 void bi_print_shader(bi_context *ctx, FILE *fp);
1626
1627 /* BIR passes */
1628
1629 bool bi_instr_uses_helpers(bi_instr *I);
1630 bool bi_block_terminates_helpers(bi_block *block);
1631 void bi_analyze_helper_terminate(bi_context *ctx);
1632 void bi_mark_clauses_td(bi_context *ctx);
1633
1634 void bi_analyze_helper_requirements(bi_context *ctx);
1635 void bi_opt_control_flow(bi_context *ctx);
1636 void bi_opt_copy_prop(bi_context *ctx);
1637 void bi_opt_dce(bi_context *ctx, bool partial);
1638 void bi_opt_cse(bi_context *ctx);
1639 void bi_opt_mod_prop_forward(bi_context *ctx);
1640 void bi_opt_mod_prop_backward(bi_context *ctx);
1641 void bi_opt_fuse_dual_texture(bi_context *ctx);
1642 void bi_opt_dce_post_ra(bi_context *ctx);
1643 void bi_opt_message_preload(bi_context *ctx);
1644 void bi_opt_push_ubo(bi_context *ctx);
1645 void bi_opt_reorder_push(bi_context *ctx);
1646 void bi_lower_swizzle(bi_context *ctx);
1647 void bi_lower_fau(bi_context *ctx);
1648 uint64_t bi_instr_read_mask(bi_instr *I, bool staging_only);
1649 uint64_t bi_instr_write_mask(bi_instr *I);
1650 void bi_assign_scoreboard(bi_context *ctx);
1651 void bi_register_allocate(bi_context *ctx);
1652 void va_optimize(bi_context *ctx);
1653 void va_lower_split_64bit(bi_context *ctx);
1654
1655 void bi_lower_opt_instructions(bi_context *ctx);
1656 void bi_lower_mkvec_swz(bi_context *ctx);
1657
1658 void bi_iterator_schedule(bi_context *ctx);
1659 void bi_pressure_schedule(bi_context *ctx);
1660 void bi_schedule(bi_context *ctx);
1661 bool bi_can_fma(bi_instr *ins);
1662 bool bi_can_add(bi_instr *ins);
1663 bool bi_must_message(bi_instr *ins);
1664 bool bi_reads_zero(bi_instr *ins);
1665 bool bi_reads_temps(bi_instr *ins, unsigned src);
1666 bool bi_reads_t(bi_instr *ins, unsigned src);
1667
1668 #ifndef NDEBUG
1669 bool bi_validate_initialization(bi_context *ctx);
1670 void bi_validate(bi_context *ctx, const char *after_str);
1671 #else
1672 static inline bool
1673 bi_validate_initialization(UNUSED bi_context *ctx)
1674 {
1675 return true;
1676 }
1677 static inline void
1678 bi_validate(UNUSED bi_context *ctx, UNUSED const char *after_str)
1679 {
1680 return;
1681 }
1682 #endif
1683
1684 uint32_t bi_fold_constant(bi_instr *I, bool *unsupported);
1685 bool bi_opt_constant_fold(bi_context *ctx);
1686
1687 /* Liveness */
1688
1689 void bi_compute_liveness_ssa(bi_context *ctx);
1690 void bi_liveness_ins_update_ssa(BITSET_WORD *live, const bi_instr *ins);
1691
1692 unsigned bi_calc_register_demand(bi_context *ctx);
1693
1694 void bi_postra_liveness(bi_context *ctx);
1695 uint64_t MUST_CHECK bi_postra_liveness_ins(uint64_t live, bi_instr *ins);
1696
1697 /* Record sizes of SSA values into the provided array. */
1698 void bi_record_sizes(bi_context *ctx, uint32_t *sizes);
1699
1700 /* SSA spilling */
1701 void bi_spill_ssa(bi_context *ctx, unsigned num_registers);
1702
1703 void bi_repair_ssa(bi_context *ctx);
1704
1705 /* Reindex SSA to reduce memory usage */
1706 void bi_reindex_ssa(bi_context *ctx);
1707
1708 /* Lower memory operands created during spilling. */
1709 unsigned bi_lower_spill(bi_context* ctx, uint32_t tls_base);
1710
1711 /* Layout */
1712
1713 signed bi_block_offset(bi_context *ctx, bi_clause *start, bi_block *target);
1714 bool bi_ec0_packed(unsigned tuple_count);
1715
1716 /* Check if there are no more instructions starting with a given block, this
1717 * needs to recurse in case a shader ends with multiple empty blocks */
1718
1719 static inline bool
1720 bi_is_terminal_block(bi_block *block)
1721 {
1722 return (block == NULL) || (list_is_empty(&block->instructions) &&
1723 bi_is_terminal_block(block->successors[0]) &&
1724 bi_is_terminal_block(block->successors[1]));
1725 }
1726
1727 /* Code emit */
1728
1729 /* Returns the size of the final clause */
1730 unsigned bi_pack(bi_context *ctx, struct util_dynarray *emission);
1731 void bi_pack_valhall(bi_context *ctx, struct util_dynarray *emission);
1732
1733 void bi_compile_variant(nir_shader *nir,
1734 const struct pan_compile_inputs *inputs,
1735 struct util_dynarray *binary, struct pan_shader_info *info,
1736 enum bi_idvs_mode idvs);
1737
1738 struct bi_packed_tuple {
1739 uint64_t lo;
1740 uint64_t hi;
1741 };
1742
1743 uint8_t bi_pack_literal(enum bi_clause_subword literal);
1744
1745 uint8_t bi_pack_upper(enum bi_clause_subword upper,
1746 struct bi_packed_tuple *tuples,
1747 ASSERTED unsigned tuple_count);
1748 uint64_t bi_pack_tuple_bits(enum bi_clause_subword idx,
1749 struct bi_packed_tuple *tuples,
1750 ASSERTED unsigned tuple_count, unsigned offset,
1751 unsigned nbits);
1752
1753 uint8_t bi_pack_sync(enum bi_clause_subword t1, enum bi_clause_subword t2,
1754 enum bi_clause_subword t3, struct bi_packed_tuple *tuples,
1755 ASSERTED unsigned tuple_count, bool z);
1756
1757 void bi_pack_format(struct util_dynarray *emission, unsigned index,
1758 struct bi_packed_tuple *tuples,
1759 ASSERTED unsigned tuple_count, uint64_t header,
1760 uint64_t ec0, unsigned m0, bool z);
1761
1762 unsigned bi_pack_fma(bi_instr *I, enum bifrost_packed_src src0,
1763 enum bifrost_packed_src src1, enum bifrost_packed_src src2,
1764 enum bifrost_packed_src src3);
1765 unsigned bi_pack_add(bi_instr *I, enum bifrost_packed_src src0,
1766 enum bifrost_packed_src src1, enum bifrost_packed_src src2,
1767 enum bifrost_packed_src src3);
1768
1769 /* Like in NIR, for use with the builder */
1770
1771 enum bi_cursor_option {
1772 bi_cursor_after_block,
1773 bi_cursor_before_instr,
1774 bi_cursor_after_instr
1775 };
1776
1777 typedef struct {
1778 enum bi_cursor_option option;
1779
1780 union {
1781 bi_block *block;
1782 bi_instr *instr;
1783 };
1784 } bi_cursor;
1785
1786 static inline bi_cursor
1787 bi_after_block(bi_block *block)
1788 {
1789 return (bi_cursor){.option = bi_cursor_after_block, .block = block};
1790 }
1791
1792 static inline bi_cursor
1793 bi_before_instr(bi_instr *instr)
1794 {
1795 return (bi_cursor){.option = bi_cursor_before_instr, .instr = instr};
1796 }
1797
1798 static inline bi_cursor
1799 bi_after_instr(bi_instr *instr)
1800 {
1801 return (bi_cursor){.option = bi_cursor_after_instr, .instr = instr};
1802 }
1803
1804 static inline bi_cursor
1805 bi_after_block_logical(bi_block *block)
1806 {
1807 if (list_is_empty(&block->instructions))
1808 return bi_after_block(block);
1809
1810 bi_instr *last = list_last_entry(&block->instructions, bi_instr, link);
1811 assert(last != NULL);
1812
1813 if (last->branch_target)
1814 return bi_before_instr(last);
1815 else
1816 return bi_after_block(block);
1817 }
1818
1819 static inline bi_cursor
1820 bi_before_nonempty_block(bi_block *block)
1821 {
1822 bi_instr *I = list_first_entry(&block->instructions, bi_instr, link);
1823 assert(I != NULL);
1824
1825 return bi_before_instr(I);
1826 }
1827
1828 static inline bi_cursor
1829 bi_before_block(bi_block *block)
1830 {
1831 if (list_is_empty(&block->instructions))
1832 return bi_after_block(block);
1833 else
1834 return bi_before_nonempty_block(block);
1835 }
1836
1837 /* Invariant: a tuple must be nonempty UNLESS it is the last tuple of a clause,
1838 * in which case there must exist a nonempty penultimate tuple */
1839
1840 ATTRIBUTE_RETURNS_NONNULL static inline bi_instr *
1841 bi_first_instr_in_tuple(bi_tuple *tuple)
1842 {
1843 bi_instr *instr = tuple->fma ?: tuple->add;
1844 assert(instr != NULL);
1845 return instr;
1846 }
1847
1848 ATTRIBUTE_RETURNS_NONNULL static inline bi_instr *
1849 bi_first_instr_in_clause(bi_clause *clause)
1850 {
1851 return bi_first_instr_in_tuple(&clause->tuples[0]);
1852 }
1853
1854 ATTRIBUTE_RETURNS_NONNULL static inline bi_instr *
1855 bi_last_instr_in_clause(bi_clause *clause)
1856 {
1857 bi_tuple tuple = clause->tuples[clause->tuple_count - 1];
1858 bi_instr *instr = tuple.add ?: tuple.fma;
1859
1860 if (!instr) {
1861 assert(clause->tuple_count >= 2);
1862 tuple = clause->tuples[clause->tuple_count - 2];
1863 instr = tuple.add ?: tuple.fma;
1864 }
1865
1866 assert(instr != NULL);
1867 return instr;
1868 }
1869
1870 /* Implemented by expanding bi_foreach_instr_in_block_from(_rev) with the start
1871 * (end) of the clause and adding a condition for the clause boundary */
1872
1873 #define bi_foreach_instr_in_clause(block, clause, pos) \
1874 for (bi_instr *pos = \
1875 list_entry(bi_first_instr_in_clause(clause), bi_instr, link); \
1876 (&pos->link != &(block)->instructions) && \
1877 (pos != bi_next_op(bi_last_instr_in_clause(clause))); \
1878 pos = list_entry(pos->link.next, bi_instr, link))
1879
1880 #define bi_foreach_instr_in_clause_rev(block, clause, pos) \
1881 for (bi_instr *pos = \
1882 list_entry(bi_last_instr_in_clause(clause), bi_instr, link); \
1883 (&pos->link != &(block)->instructions) && \
1884 pos != bi_prev_op(bi_first_instr_in_clause(clause)); \
1885 pos = list_entry(pos->link.prev, bi_instr, link))
1886
1887 static inline bi_cursor
1888 bi_before_clause(bi_clause *clause)
1889 {
1890 return bi_before_instr(bi_first_instr_in_clause(clause));
1891 }
1892
1893 static inline bi_cursor
1894 bi_before_tuple(bi_tuple *tuple)
1895 {
1896 return bi_before_instr(bi_first_instr_in_tuple(tuple));
1897 }
1898
1899 static inline bi_cursor
1900 bi_after_clause(bi_clause *clause)
1901 {
1902 return bi_after_instr(bi_last_instr_in_clause(clause));
1903 }
1904
1905 /* Get a cursor at the start of a function, after any preloads */
1906 static inline bi_cursor
1907 bi_before_function(bi_context *ctx)
1908 {
1909 bi_block *block = bi_start_block(&ctx->blocks);
1910
1911 return bi_before_block(block);
1912 }
1913
1914 /* IR builder in terms of cursor infrastructure */
1915
1916 typedef struct {
1917 bi_context *shader;
1918 bi_cursor cursor;
1919 const nir_instr_debug_info *debug_info;
1920 } bi_builder;
1921
1922 static inline bi_builder
1923 bi_init_builder(bi_context *ctx, bi_cursor cursor)
1924 {
1925 return (bi_builder){.shader = ctx, .cursor = cursor};
1926 }
1927
1928 /* insert load/store for spills */
1929 bi_instr *bi_load_tl(bi_builder *b, unsigned bits, bi_index dst, unsigned offset);
1930 void bi_store_tl(bi_builder *b, unsigned bits, bi_index src, unsigned offset);
1931
1932 /* Insert an instruction at the cursor and move the cursor */
1933
1934 static inline void
1935 bi_builder_insert(bi_cursor *cursor, bi_instr *I)
1936 {
1937 switch (cursor->option) {
1938 case bi_cursor_after_instr:
1939 list_add(&I->link, &cursor->instr->link);
1940 cursor->instr = I;
1941 return;
1942
1943 case bi_cursor_after_block:
1944 list_addtail(&I->link, &cursor->block->instructions);
1945 cursor->option = bi_cursor_after_instr;
1946 cursor->instr = I;
1947 return;
1948
1949 case bi_cursor_before_instr:
1950 list_addtail(&I->link, &cursor->instr->link);
1951 cursor->option = bi_cursor_after_instr;
1952 cursor->instr = I;
1953 return;
1954 }
1955
1956 UNREACHABLE("Invalid cursor option");
1957 }
1958
1959 bi_instr *bi_csel_from_mux(bi_builder *b, const bi_instr *I, bool must_sign);
1960
1961 /* Read back power-efficent garbage, TODO maybe merge with null? */
1962 static inline bi_index
1963 bi_dontcare(bi_builder *b)
1964 {
1965 if (b->shader->arch >= 9)
1966 return bi_zero();
1967 else
1968 return bi_passthrough(BIFROST_SRC_FAU_HI);
1969 }
1970
1971 bi_instr *bi_make_vec_to(bi_builder *b, bi_index dst, bi_index *src,
1972 unsigned *channel, unsigned count, unsigned bitsize);
1973
1974 #define bi_worklist_init(ctx, w) u_worklist_init(w, ctx->num_blocks, ctx)
1975 #define bi_worklist_push_head(w, block) u_worklist_push_head(w, block, index)
1976 #define bi_worklist_push_tail(w, block) u_worklist_push_tail(w, block, index)
1977 #define bi_worklist_peek_head(w) u_worklist_peek_head(w, bi_block, index)
1978 #define bi_worklist_pop_head(w) u_worklist_pop_head(w, bi_block, index)
1979 #define bi_worklist_peek_tail(w) u_worklist_peek_tail(w, bi_block, index)
1980 #define bi_worklist_pop_tail(w) u_worklist_pop_tail(w, bi_block, index)
1981
1982 static inline void
1983 bi_record_use(bi_instr **uses, BITSET_WORD *multiple, bi_instr *I, unsigned s)
1984 {
1985 unsigned v = I->src[s].value;
1986
1987 assert(I->src[s].type == BI_INDEX_NORMAL);
1988 if (uses[v] && uses[v] != I)
1989 BITSET_SET(multiple, v);
1990 else
1991 uses[v] = I;
1992 }
1993
1994 /* NIR passes */
1995
1996 bool bi_lower_divergent_indirects(nir_shader *shader, unsigned lanes);
1997
1998 void bi_find_loop_blocks(const bi_context *ctx, bi_block *header,
1999 BITSET_WORD *out);
2000
2001 #ifdef __cplusplus
2002 } /* extern C */
2003 #endif
2004
2005 #endif