2 * Copyright (C) 2020 Collabora, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define RETURN_PACKED(str) { \
28 memcpy(&temp, &str, sizeof(str)); \
32 /* This file contains the final passes of the compiler. Running after
33 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
34 * bits on the wire (as well as fixup branches) */
37 bi_pack_header(bi_clause
*clause
, bi_clause
*next
, bool is_fragment
)
39 struct bifrost_header header
= {
40 .back_to_back
= clause
->back_to_back
,
41 .no_end_of_shader
= (next
!= NULL
),
42 .elide_writes
= is_fragment
,
43 .branch_cond
= clause
->branch_conditional
,
44 .datareg_writebarrier
= clause
->data_register_write_barrier
,
45 .datareg
= clause
->data_register
,
46 .scoreboard_deps
= next
? next
->dependencies
: 0,
47 .scoreboard_index
= clause
->scoreboard_id
,
48 .clause_type
= clause
->clause_type
,
49 .next_clause_type
= next
? next
->clause_type
: 0,
53 memcpy(&u
, &header
, sizeof(header
));
57 /* Represents the assignment of ports for a given bundle */
60 /* Register to assign to each port */
63 /* Read ports can be disabled */
66 /* Should we write FMA? what about ADD? If only a single port is
67 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
68 bool write_fma
, write_add
;
70 /* Should we read with port 3? */
73 /* Packed uniform/constant */
74 uint8_t uniform_constant
;
76 /* Whether writes are actually for the last instruction */
77 bool first_instruction
;
80 /* The uniform/constant slot allows loading a contiguous 64-bit immediate or
81 * pushed uniform per bundle. Figure out which one we need in the bundle (the
82 * scheduler needs to ensure we only have one type per bundle), validate
83 * everything, and rewrite away the register/uniform indices to use 3-bit
84 * sources directly. */
87 bi_lookup_constant(bi_clause
*clause
, uint64_t cons
, bool *hi
, bool b64
)
89 uint64_t want
= (cons
>> 4);
91 for (unsigned i
= 0; i
< clause
->constant_count
; ++i
) {
92 /* Only check top 60-bits since that's what's actually embedded
93 * in the clause, the bottom 4-bits are bundle-inline */
95 unsigned candidates
[2] = {
96 clause
->constants
[i
] >> 4,
97 clause
->constants
[i
] >> 36
101 candidates
[0] &= 0xFFFFFFFF;
103 if (candidates
[0] == want
)
106 if (candidates
[1] == want
&& !b64
) {
112 unreachable("Invalid constant accessed");
116 bi_constant_field(unsigned idx
)
120 const unsigned values
[] = {
124 return values
[idx
] << 4;
128 bi_assign_uniform_constant_single(
129 struct bi_registers
*regs
,
131 bi_instruction
*ins
, bool assigned
, bool fast_zero
)
136 bi_foreach_src(ins
, s
) {
137 if (s
== 0 && (ins
->type
== BI_LOAD_VAR_ADDRESS
|| ins
->type
== BI_LOAD_ATTR
)) continue;
139 if (ins
->src
[s
] & BIR_INDEX_CONSTANT
) {
141 bool b64
= nir_alu_type_get_type_size(ins
->src_types
[s
]) > 32;
142 uint64_t cons
= bi_get_immediate(ins
, ins
->src
[s
]);
143 unsigned idx
= bi_lookup_constant(clause
, cons
, &hi
, b64
);
144 unsigned f
= bi_constant_field(idx
) | (cons
& 0xF);
146 if (assigned
&& regs
->uniform_constant
!= f
)
147 unreachable("Mismatched uniform/const field: imm");
149 regs
->uniform_constant
= f
;
150 ins
->src
[s
] = BIR_INDEX_PASS
| (hi
? BIFROST_SRC_CONST_HI
: BIFROST_SRC_CONST_LO
);
152 } else if (ins
->src
[s
] & BIR_INDEX_ZERO
&& (ins
->type
== BI_LOAD_UNIFORM
|| ins
->type
== BI_LOAD_VAR
)) {
153 /* XXX: HACK UNTIL WE HAVE HI MATCHING DUE TO OVERFLOW XXX */
154 ins
->src
[s
] = BIR_INDEX_PASS
| BIFROST_SRC_CONST_HI
;
155 } else if (ins
->src
[s
] & BIR_INDEX_ZERO
&& !fast_zero
) {
156 /* FMAs have a fast zero port, ADD needs to use the
157 * uniform/const port's special 0 mode handled here */
160 if (assigned
&& regs
->uniform_constant
!= f
)
161 unreachable("Mismatched uniform/const field: 0");
163 regs
->uniform_constant
= f
;
164 ins
->src
[s
] = BIR_INDEX_PASS
| BIFROST_SRC_CONST_LO
;
166 } else if (s
& BIR_INDEX_UNIFORM
) {
167 unreachable("Push uniforms not implemented yet");
175 bi_assign_uniform_constant(
177 struct bi_registers
*regs
,
181 bi_assign_uniform_constant_single(regs
, clause
, bundle
.fma
, false, true);
183 bi_assign_uniform_constant_single(regs
, clause
, bundle
.add
, assigned
, false);
186 /* Assigns a port for reading, before anything is written */
189 bi_assign_port_read(struct bi_registers
*regs
, unsigned src
)
191 /* We only assign for registers */
192 if (!(src
& BIR_INDEX_REGISTER
))
195 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
197 /* Check if we already assigned the port */
198 for (unsigned i
= 0; i
<= 1; ++i
) {
199 if (regs
->port
[i
] == reg
&& regs
->enabled
[i
])
203 if (regs
->port
[3] == reg
&& regs
->read_port3
)
208 for (unsigned i
= 0; i
<= 1; ++i
) {
209 if (!regs
->enabled
[i
]) {
211 regs
->enabled
[i
] = true;
216 if (!regs
->read_port3
) {
218 regs
->read_port3
= true;
222 static struct bi_registers
223 bi_assign_ports(bi_bundle now
, bi_bundle prev
)
225 struct bi_registers regs
= { 0 };
227 /* We assign ports for the main register mechanism. Special ops
228 * use the data registers, which has its own mechanism entirely
229 * and thus gets skipped over here. */
231 unsigned read_dreg
= now
.add
&&
232 bi_class_props
[now
.add
->type
] & BI_DATA_REG_SRC
;
234 unsigned write_dreg
= prev
.add
&&
235 bi_class_props
[prev
.add
->type
] & BI_DATA_REG_DEST
;
237 /* First, assign reads */
240 bi_foreach_src(now
.fma
, src
)
241 bi_assign_port_read(®s
, now
.fma
->src
[src
]);
244 bi_foreach_src(now
.add
, src
) {
245 if (!(src
== 0 && read_dreg
))
246 bi_assign_port_read(®s
, now
.add
->src
[src
]);
250 /* Next, assign writes */
252 if (prev
.fma
&& prev
.fma
->dest
& BIR_INDEX_REGISTER
) {
253 regs
.port
[2] = prev
.fma
->dest
& ~BIR_INDEX_REGISTER
;
254 regs
.write_fma
= true;
257 if (prev
.add
&& prev
.add
->dest
& BIR_INDEX_REGISTER
&& !write_dreg
) {
258 unsigned r
= prev
.add
->dest
& ~BIR_INDEX_REGISTER
;
260 if (regs
.write_fma
) {
261 /* Scheduler constraint: cannot read 3 and write 2 */
262 assert(!regs
.read_port3
);
268 regs
.write_add
= true;
271 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
273 if (regs
.enabled
[0] && regs
.enabled
[1] && regs
.port
[1] < regs
.port
[0]) {
274 unsigned temp
= regs
.port
[0];
275 regs
.port
[0] = regs
.port
[1];
282 /* Determines the register control field, ignoring the first? flag */
284 static enum bifrost_reg_control
285 bi_pack_register_ctrl_lo(struct bi_registers r
)
289 assert(!r
.read_port3
);
290 return BIFROST_WRITE_ADD_P2_FMA_P3
;
293 return BIFROST_WRITE_FMA_P2_READ_P3
;
295 return BIFROST_WRITE_FMA_P2
;
297 } else if (r
.write_add
) {
299 return BIFROST_WRITE_ADD_P2_READ_P3
;
301 return BIFROST_WRITE_ADD_P2
;
302 } else if (r
.read_port3
)
303 return BIFROST_READ_P3
;
305 return BIFROST_REG_NONE
;
308 /* Ditto but account for the first? flag this time */
310 static enum bifrost_reg_control
311 bi_pack_register_ctrl(struct bi_registers r
)
313 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl_lo(r
);
315 if (r
.first_instruction
) {
316 if (ctrl
== BIFROST_REG_NONE
)
317 ctrl
= BIFROST_FIRST_NONE
;
319 ctrl
|= BIFROST_FIRST_NONE
;
326 bi_pack_registers(struct bi_registers regs
)
328 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl(regs
);
329 struct bifrost_regs s
;
332 if (regs
.enabled
[1]) {
333 /* Gotta save that bit!~ Required by the 63-x trick */
334 assert(regs
.port
[1] > regs
.port
[0]);
335 assert(regs
.enabled
[0]);
337 /* Do the 63-x trick, see docs/disasm */
338 if (regs
.port
[0] > 31) {
339 regs
.port
[0] = 63 - regs
.port
[0];
340 regs
.port
[1] = 63 - regs
.port
[1];
343 assert(regs
.port
[0] <= 31);
344 assert(regs
.port
[1] <= 63);
347 s
.reg1
= regs
.port
[1];
348 s
.reg0
= regs
.port
[0];
350 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
353 if (regs
.enabled
[0]) {
354 /* Bit 0 upper bit of port 0 */
355 s
.reg1
|= (regs
.port
[0] >> 5);
357 /* Rest of port 0 in usual spot */
358 s
.reg0
= (regs
.port
[0] & 0b11111);
360 /* Bit 1 set if port 0 also disabled */
365 s
.reg3
= regs
.port
[3];
366 s
.reg2
= regs
.port
[2];
367 s
.uniform_const
= regs
.uniform_constant
;
369 memcpy(&packed
, &s
, sizeof(s
));
374 bi_set_data_register(bi_clause
*clause
, unsigned idx
)
376 assert(idx
& BIR_INDEX_REGISTER
);
377 unsigned reg
= idx
& ~BIR_INDEX_REGISTER
;
379 clause
->data_register
= reg
;
383 bi_read_data_register(bi_clause
*clause
, bi_instruction
*ins
)
385 bi_set_data_register(clause
, ins
->src
[0]);
389 bi_write_data_register(bi_clause
*clause
, bi_instruction
*ins
)
391 bi_set_data_register(clause
, ins
->dest
);
394 static enum bifrost_packed_src
395 bi_get_src_reg_port(struct bi_registers
*regs
, unsigned src
)
397 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
399 if (regs
->port
[0] == reg
&& regs
->enabled
[0])
400 return BIFROST_SRC_PORT0
;
401 else if (regs
->port
[1] == reg
&& regs
->enabled
[1])
402 return BIFROST_SRC_PORT1
;
403 else if (regs
->port
[3] == reg
&& regs
->read_port3
)
404 return BIFROST_SRC_PORT3
;
406 unreachable("Tried to access register with no port");
409 static enum bifrost_packed_src
410 bi_get_src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned s
, bool is_fma
)
412 unsigned src
= ins
->src
[s
];
414 if (src
& BIR_INDEX_REGISTER
)
415 return bi_get_src_reg_port(regs
, src
);
416 else if (src
& BIR_INDEX_ZERO
&& is_fma
)
417 return BIFROST_SRC_STAGE
;
418 else if (src
& BIR_INDEX_PASS
)
419 return src
& ~BIR_INDEX_PASS
;
421 unreachable("Unknown src");
424 /* Constructs a packed 2-bit swizzle for a 16-bit vec2 source. Source must be
425 * 16-bit and written components must correspond to valid swizzles (component x
429 bi_swiz16(bi_instruction
*ins
, unsigned src
)
431 assert(nir_alu_type_get_type_size(ins
->src_types
[src
]) == 16);
432 unsigned swizzle
= 0;
434 for (unsigned c
= 0; c
< 2; ++c
) {
435 if (!bi_writes_component(ins
, src
)) continue;
437 unsigned k
= ins
->swizzle
[src
][c
];
446 bi_pack_fma_fma(bi_instruction
*ins
, struct bi_registers
*regs
)
448 /* (-a)(-b) = ab, so we only need one negate bit */
449 bool negate_mul
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
451 if (ins
->dest_type
== nir_type_float32
) {
452 struct bifrost_fma_fma pack
= {
453 .src0
= bi_get_src(ins
, regs
, 0, true),
454 .src1
= bi_get_src(ins
, regs
, 1, true),
455 .src2
= bi_get_src(ins
, regs
, 2, true),
456 .src0_abs
= ins
->src_abs
[0],
457 .src1_abs
= ins
->src_abs
[1],
458 .src2_abs
= ins
->src_abs
[2],
459 .src0_neg
= negate_mul
,
460 .src2_neg
= ins
->src_neg
[2],
461 .outmod
= ins
->outmod
,
462 .roundmode
= ins
->roundmode
,
463 .op
= BIFROST_FMA_OP_FMA
467 } else if (ins
->dest_type
== nir_type_float16
) {
468 struct bifrost_fma_fma16 pack
= {
469 .src0
= bi_get_src(ins
, regs
, 0, true),
470 .src1
= bi_get_src(ins
, regs
, 1, true),
471 .src2
= bi_get_src(ins
, regs
, 2, true),
472 .swizzle_0
= bi_swiz16(ins
, 0),
473 .swizzle_1
= bi_swiz16(ins
, 1),
474 .swizzle_2
= bi_swiz16(ins
, 2),
475 .src0_neg
= negate_mul
,
476 .src2_neg
= ins
->src_neg
[2],
477 .outmod
= ins
->outmod
,
478 .roundmode
= ins
->roundmode
,
479 .op
= BIFROST_FMA_OP_FMA16
484 unreachable("Invalid fma dest type");
489 bi_pack_fma_add_f32(bi_instruction
*ins
, struct bi_registers
*regs
)
491 struct bifrost_fma_add pack
= {
492 .src0
= bi_get_src(ins
, regs
, 0, true),
493 .src1
= bi_get_src(ins
, regs
, 1, true),
494 .src0_abs
= ins
->src_abs
[0],
495 .src1_abs
= ins
->src_abs
[1],
496 .src0_neg
= ins
->src_neg
[0],
497 .src1_neg
= ins
->src_neg
[1],
499 .outmod
= ins
->outmod
,
500 .roundmode
= ins
->roundmode
,
501 .op
= BIFROST_FMA_OP_FADD32
508 bi_pack_fma_addmin_f16(bi_instruction
*ins
, struct bi_registers
*regs
)
511 (ins
->type
== BI_ADD
) ? BIFROST_FMA_OP_FADD16
:
512 (ins
->op
.minmax
== BI_MINMAX_MIN
) ? BIFROST_FMA_OP_FMIN16
:
513 BIFROST_FMA_OP_FMAX16
;
515 /* Absolute values are packed in a quirky way. Let k = src1 < src0. Let
516 * l be an auxiliary bit we encode. Then the hardware determines:
521 * Since add/min/max are commutative, this saves a bit by using the
522 * order of the operands as a bit (k). To pack this, first note:
524 * (l && k) implies (l || k).
526 * That is, if the second argument is abs'd, then the first argument
527 * also has abs. So there are three cases:
529 * Case 0: Neither src has absolute value. Then we have l = k = 0.
531 * Case 1: Exactly one src has absolute value. Assign that source to
532 * src0 and the other source to src1. Compute k = src1 < src0 based on
533 * that assignment. Then l = ~k.
535 * Case 2: Both sources have absolute value. Then we have l = k = 1.
536 * Note to force k = 1 requires that (src1 < src0) OR (src0 < src1).
537 * That is, this encoding is only valid if src1 and src0 are distinct.
538 * This is a scheduling restriction (XXX); if an op of this type
539 * requires both identical sources to have abs value, then we must
540 * schedule to ADD (which does not use this ordering trick).
543 unsigned abs_0
= ins
->src_abs
[0], abs_1
= ins
->src_abs
[1];
544 unsigned src_0
= bi_get_src(ins
, regs
, 0, true);
545 unsigned src_1
= bi_get_src(ins
, regs
, 0, true);
548 if (!abs_0
&& !abs_1
) {
549 /* Force k = 0 <===> NOT(src1 < src0) <==> src1 >= src0 */
551 unsigned tmp
= src_0
;
555 } else if (abs_0
&& !abs_1
) {
557 } else if (abs_1
&& !abs_0
) {
558 unsigned tmp
= src_0
;
564 if (src_0
>= src_1
) {
565 unsigned tmp
= src_0
;
573 struct bifrost_fma_add_minmax16 pack
= {
576 .src0_neg
= ins
->src_neg
[0],
577 .src1_neg
= ins
->src_neg
[1],
579 .outmod
= ins
->outmod
,
580 .mode
= (ins
->type
== BI_ADD
) ? ins
->roundmode
: ins
->minmax
,
588 bi_pack_fma_add(bi_instruction
*ins
, struct bi_registers
*regs
)
590 if (ins
->dest_type
== nir_type_float32
)
591 return bi_pack_fma_add_f32(ins
, regs
);
592 else if(ins
->dest_type
== nir_type_float16
)
593 return bi_pack_fma_addmin_f16(ins
, regs
);
595 unreachable("Unknown FMA/ADD type");
599 bi_pack_fma_1src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned op
)
601 struct bifrost_fma_inst pack
= {
602 .src0
= bi_get_src(ins
, regs
, 0, true),
609 static enum bifrost_csel_cond
610 bi_cond_to_csel(enum bi_cond cond
, bool *flip
, bool *invert
, nir_alu_type T
)
612 nir_alu_type B
= nir_alu_type_get_base_type(T
);
613 unsigned idx
= (B
== nir_type_float
) ? 0 :
614 ((B
== nir_type_int
) ? 1 : 2);
620 const enum bifrost_csel_cond ops
[] = {
631 const enum bifrost_csel_cond ops
[] = {
642 const enum bifrost_csel_cond ops
[] = {
645 BIFROST_IEQ_F
/* sign is irrelevant */
651 unreachable("Invalid op for csel");
656 bi_pack_fma_csel(bi_instruction
*ins
, struct bi_registers
*regs
)
658 /* TODO: Use csel3 as well */
659 bool flip
= false, invert
= false;
661 enum bifrost_csel_cond cond
=
662 bi_cond_to_csel(ins
->csel_cond
, &flip
, &invert
, ins
->src_types
[0]);
664 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
666 unsigned cmp_0
= (flip
? 3 : 0);
667 unsigned cmp_1
= (flip
? 0 : 3);
668 unsigned res_0
= (invert
? 2 : 1);
669 unsigned res_1
= (invert
? 1 : 2);
671 struct bifrost_csel4 pack
= {
672 .src0
= bi_get_src(ins
, regs
, cmp_0
, true),
673 .src1
= bi_get_src(ins
, regs
, cmp_1
, true),
674 .src2
= bi_get_src(ins
, regs
, res_0
, true),
675 .src3
= bi_get_src(ins
, regs
, res_1
, true),
677 .op
= (size
== 16) ? BIFROST_FMA_OP_CSEL4_V16
:
684 /* We have a single convert opcode in the IR but a number of opcodes that could
685 * come out. In particular we have native opcodes for:
687 * [ui]16 --> [fui]32 -- int16_to_32
688 * f16 --> f32 -- float16_to_32
689 * f32 --> f16 -- float32_to_16
690 * f32 --> [ui]32 -- float32_to_int
691 * [ui]32 --> f32 -- int_to_float32
692 * [fui]16 --> [fui]16 -- f2i_i2f16
696 bi_pack_fma_convert(bi_instruction
*ins
, struct bi_registers
*regs
)
698 nir_alu_type from_base
= nir_alu_type_get_base_type(ins
->src_types
[0]);
699 unsigned from_size
= nir_alu_type_get_type_size(ins
->src_types
[0]);
700 bool from_unsigned
= from_base
== nir_type_uint
;
702 nir_alu_type to_base
= nir_alu_type_get_base_type(ins
->dest_type
);
703 unsigned to_size
= nir_alu_type_get_type_size(ins
->dest_type
);
704 bool to_unsigned
= to_base
== nir_type_uint
;
707 assert((from_base
!= to_base
) || (from_size
!= to_size
));
708 assert((MAX2(from_size
, to_size
) / MIN2(from_size
, to_size
)) <= 2);
710 if (from_size
== 16 && to_size
== 16) {
712 unreachable("i16 not yet implemented");
713 } else if (from_size
== 32 && to_size
== 32) {
716 if (from_base
== nir_type_float
) {
717 op
= BIFROST_FMA_FLOAT32_TO_INT(to_unsigned
);
719 op
= BIFROST_FMA_INT_TO_FLOAT32(from_unsigned
);
722 return bi_pack_fma_1src(ins
, regs
, op
);
723 } else if (from_size
== 16 && to_size
== 32) {
724 bool from_y
= ins
->swizzle
[0][0];
726 if (from_base
== nir_type_float
) {
727 return bi_pack_fma_1src(ins
, regs
,
728 BIFROST_FMA_FLOAT16_TO_32(from_y
));
730 unreachable("i16 not yet implemented");
732 } else if (from_size
== 32 && to_size
== 16) {
733 if (from_base
== nir_type_float
) {
734 /* TODO: second vectorized source? */
735 struct bifrost_fma_2src pack
= {
736 .src0
= bi_get_src(ins
, regs
, 0, true),
737 .src1
= BIFROST_SRC_STAGE
, /* 0 */
738 .op
= BIFROST_FMA_FLOAT32_TO_16
743 unreachable("i16 not yet implemented");
747 unreachable("Unknown convert");
751 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
754 return BIFROST_FMA_NOP
;
756 switch (bundle
.fma
->type
) {
758 return bi_pack_fma_add(bundle
.fma
, regs
);
761 return BIFROST_FMA_NOP
;
763 return bi_pack_fma_convert(bundle
.fma
, regs
);
765 return bi_pack_fma_csel(bundle
.fma
, regs
);
767 return bi_pack_fma_fma(bundle
.fma
, regs
);
771 return BIFROST_FMA_NOP
;
773 return bi_pack_fma_1src(bundle
.fma
, regs
, BIFROST_FMA_OP_MOV
);
778 return BIFROST_FMA_NOP
;
780 unreachable("Cannot encode class as FMA");
785 bi_pack_add_ld_vary(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
787 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
788 assert(size
== 32 || size
== 16);
790 unsigned op
= (size
== 32) ?
791 BIFROST_ADD_OP_LD_VAR_32
:
792 BIFROST_ADD_OP_LD_VAR_16
;
794 unsigned cmask
= bi_from_bytemask(ins
->writemask
, size
/ 8);
795 unsigned channels
= util_bitcount(cmask
);
796 assert(cmask
== ((1 << channels
) - 1));
798 unsigned packed_addr
= 0;
800 if (ins
->src
[0] & BIR_INDEX_CONSTANT
) {
801 /* Direct uses address field directly */
802 packed_addr
= bi_get_immediate(ins
, ins
->src
[0]);
803 assert(packed_addr
< 0b1000);
805 /* Indirect gets an extra source */
806 packed_addr
= bi_get_src(ins
, regs
, 0, false) | 0b11000;
809 /* The destination is thrown in the data register */
810 assert(ins
->dest
& BIR_INDEX_REGISTER
);
811 clause
->data_register
= ins
->dest
& ~BIR_INDEX_REGISTER
;
813 assert(channels
>= 1 && channels
<= 4);
815 struct bifrost_ld_var pack
= {
816 .src0
= bi_get_src(ins
, regs
, 1, false),
818 .channels
= MALI_POSITIVE(channels
),
819 .interp_mode
= ins
->load_vary
.interp_mode
,
820 .reuse
= ins
->load_vary
.reuse
,
821 .flat
= ins
->load_vary
.flat
,
829 bi_pack_add_2src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned op
)
831 struct bifrost_add_2src pack
= {
832 .src0
= bi_get_src(ins
, regs
, 0, true),
833 .src1
= bi_get_src(ins
, regs
, 1, true),
841 bi_pack_add_ld_ubo(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
843 unsigned components
= bi_load32_components(ins
);
845 const unsigned ops
[4] = {
846 BIFROST_ADD_OP_LD_UBO_1
,
847 BIFROST_ADD_OP_LD_UBO_2
,
848 BIFROST_ADD_OP_LD_UBO_3
,
849 BIFROST_ADD_OP_LD_UBO_4
852 bi_write_data_register(clause
, ins
);
853 return bi_pack_add_2src(ins
, regs
, ops
[components
- 1]);
856 static enum bifrost_ldst_type
857 bi_pack_ldst_type(nir_alu_type T
)
860 case nir_type_float16
: return BIFROST_LDST_F16
;
861 case nir_type_float32
: return BIFROST_LDST_F32
;
862 case nir_type_int32
: return BIFROST_LDST_I32
;
863 case nir_type_uint32
: return BIFROST_LDST_U32
;
864 default: unreachable("Invalid type loaded");
869 bi_pack_add_ld_var_addr(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
871 struct bifrost_ld_var_addr pack
= {
872 .src0
= bi_get_src(ins
, regs
, 1, false),
873 .src1
= bi_get_src(ins
, regs
, 2, false),
874 .location
= bi_get_immediate(ins
, ins
->src
[0]),
875 .type
= bi_pack_ldst_type(ins
->src_types
[3]),
876 .op
= BIFROST_ADD_OP_LD_VAR_ADDR
879 bi_write_data_register(clause
, ins
);
884 bi_pack_add_ld_attr(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
886 struct bifrost_ld_attr pack
= {
887 .src0
= bi_get_src(ins
, regs
, 1, false),
888 .src1
= bi_get_src(ins
, regs
, 2, false),
889 .location
= bi_get_immediate(ins
, ins
->src
[0]),
890 .channels
= MALI_POSITIVE(bi_load32_components(ins
)),
891 .type
= bi_pack_ldst_type(ins
->dest_type
),
892 .op
= BIFROST_ADD_OP_LD_ATTR
895 bi_write_data_register(clause
, ins
);
900 bi_pack_add_st_vary(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
902 assert(ins
->store_channels
>= 1 && ins
->store_channels
<= 4);
904 struct bifrost_st_vary pack
= {
905 .src0
= bi_get_src(ins
, regs
, 1, false),
906 .src1
= bi_get_src(ins
, regs
, 2, false),
907 .src2
= bi_get_src(ins
, regs
, 3, false),
908 .channels
= MALI_POSITIVE(ins
->store_channels
),
909 .op
= BIFROST_ADD_OP_ST_VAR
912 bi_read_data_register(clause
, ins
);
917 bi_pack_add_atest(bi_clause
*clause
, bi_instruction
*ins
, struct bi_registers
*regs
)
920 assert(ins
->src_types
[1] == nir_type_float32
);
922 struct bifrost_add_atest pack
= {
923 .src0
= bi_get_src(ins
, regs
, 0, false),
924 .src1
= bi_get_src(ins
, regs
, 1, false),
925 .component
= 1, /* Set for fp32 */
926 .op
= BIFROST_ADD_OP_ATEST
,
929 /* Despite *also* writing with the usual mechanism... quirky and
930 * perhaps unnecessary, but let's match the blob */
931 clause
->data_register
= ins
->dest
& ~BIR_INDEX_REGISTER
;
937 bi_pack_add_blend(bi_instruction
*ins
, struct bi_registers
*regs
)
939 struct bifrost_add_inst pack
= {
940 .src0
= bi_get_src(ins
, regs
, 0, false),
941 .op
= BIFROST_ADD_OP_BLEND
944 /* TODO: Pack location in uniform_const */
945 assert(ins
->blend_location
== 0);
951 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
954 return BIFROST_ADD_NOP
;
956 switch (bundle
.add
->type
) {
958 return BIFROST_ADD_NOP
;
960 return bi_pack_add_atest(clause
, bundle
.add
, regs
);
963 return BIFROST_ADD_NOP
;
965 return bi_pack_add_blend(bundle
.add
, regs
);
972 return BIFROST_ADD_NOP
;
974 return bi_pack_add_ld_attr(clause
, bundle
.add
, regs
);
975 case BI_LOAD_UNIFORM
:
976 return bi_pack_add_ld_ubo(clause
, bundle
.add
, regs
);
978 return bi_pack_add_ld_vary(clause
, bundle
.add
, regs
);
979 case BI_LOAD_VAR_ADDRESS
:
980 return bi_pack_add_ld_var_addr(clause
, bundle
.add
, regs
);
986 return BIFROST_ADD_NOP
;
988 return bi_pack_add_st_vary(clause
, bundle
.add
, regs
);
993 return BIFROST_ADD_NOP
;
995 unreachable("Cannot encode class as ADD");
999 struct bi_packed_bundle
{
1004 static struct bi_packed_bundle
1005 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
, bi_bundle prev
, bool first_bundle
)
1007 struct bi_registers regs
= bi_assign_ports(bundle
, prev
);
1008 bi_assign_uniform_constant(clause
, ®s
, bundle
);
1009 regs
.first_instruction
= first_bundle
;
1011 uint64_t reg
= bi_pack_registers(regs
);
1012 uint64_t fma
= bi_pack_fma(clause
, bundle
, ®s
);
1013 uint64_t add
= bi_pack_add(clause
, bundle
, ®s
);
1015 struct bi_packed_bundle packed
= {
1016 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
1023 /* Packs the next two constants as a dedicated constant quadword at the end of
1024 * the clause, returning the number packed. */
1027 bi_pack_constants(bi_context
*ctx
, bi_clause
*clause
,
1029 struct util_dynarray
*emission
)
1031 /* After these two, are we done? Determines tag */
1032 bool done
= clause
->constant_count
<= (index
+ 2);
1033 bool only
= clause
->constant_count
<= (index
+ 1);
1036 assert(index
== 0 && clause
->bundle_count
== 1);
1038 struct bifrost_fmt_constant quad
= {
1039 .pos
= 0, /* TODO */
1040 .tag
= done
? BIFROST_FMTC_FINAL
: BIFROST_FMTC_CONSTANTS
,
1041 .imm_1
= clause
->constants
[index
+ 0] >> 4,
1042 .imm_2
= only
? 0 : clause
->constants
[index
+ 1] >> 4
1045 /* XXX: On G71, Connor observed that the difference of the top 4 bits
1046 * of the second constant with the first must be less than 8, otherwise
1047 * we have to swap them. I am not able to reproduce this on G52,
1048 * further investigation needed. Possibly an errata. XXX */
1050 util_dynarray_append(emission
, struct bifrost_fmt_constant
, quad
);
1056 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
, bi_clause
*next
,
1057 struct util_dynarray
*emission
)
1059 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0], clause
->bundles
[0], true);
1060 assert(clause
->bundle_count
== 1);
1062 /* Used to decide if we elide writes */
1063 bool is_fragment
= ctx
->stage
== MESA_SHADER_FRAGMENT
;
1065 /* State for packing constants throughout */
1066 unsigned constant_index
= 0;
1068 struct bifrost_fmt1 quad_1
= {
1069 .tag
= clause
->constant_count
? BIFROST_FMT1_CONSTANTS
: BIFROST_FMT1_FINAL
,
1070 .header
= bi_pack_header(clause
, next
, is_fragment
),
1072 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
1073 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
1076 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
1078 /* Pack the remaining constants */
1080 while (constant_index
< clause
->constant_count
) {
1081 constant_index
+= bi_pack_constants(ctx
, clause
,
1082 constant_index
, emission
);
1087 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
1089 /* Try the next clause in this block */
1090 if (clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
1091 return list_first_entry(&(clause
->link
), bi_clause
, link
);
1093 /* Try the next block, or the one after that if it's empty, etc .*/
1094 pan_block
*next_block
= pan_next_block(block
);
1096 bi_foreach_block_from(ctx
, next_block
, block
) {
1097 bi_block
*blk
= (bi_block
*) block
;
1099 if (!list_is_empty(&blk
->clauses
))
1100 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
1107 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
1109 util_dynarray_init(emission
, NULL
);
1111 bi_foreach_block(ctx
, _block
) {
1112 bi_block
*block
= (bi_block
*) _block
;
1114 bi_foreach_clause_in_block(block
, clause
) {
1115 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
1116 bi_pack_clause(ctx
, clause
, next
, emission
);