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
27 #define RETURN_PACKED(str) { \
29 memcpy(&temp, &str, sizeof(str)); \
33 /* This file contains the final passes of the compiler. Running after
34 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
35 * bits on the wire (as well as fixup branches) */
38 bi_pack_header(bi_clause
*clause
, bi_clause
*next_1
, bi_clause
*next_2
, bool is_fragment
)
40 /* next_dependencies are the union of the dependencies of successors'
43 unsigned scoreboard_deps
= next_1
? next_1
->dependencies
: 0;
44 scoreboard_deps
|= next_2
? next_2
->dependencies
: 0;
46 struct bifrost_header header
= {
47 .back_to_back
= clause
->back_to_back
,
48 .no_end_of_shader
= (next_1
!= NULL
),
49 .elide_writes
= is_fragment
,
50 .branch_cond
= clause
->branch_conditional
|| clause
->back_to_back
,
51 .datareg_writebarrier
= clause
->data_register_write_barrier
,
52 .datareg
= clause
->data_register
,
53 .scoreboard_deps
= scoreboard_deps
,
54 .scoreboard_index
= clause
->scoreboard_id
,
55 .clause_type
= clause
->clause_type
,
56 .next_clause_type
= next_1
? next_1
->clause_type
: 0,
61 header
.branch_cond
|= header
.back_to_back
;
64 memcpy(&u
, &header
, sizeof(header
));
68 /* The uniform/constant slot allows loading a contiguous 64-bit immediate or
69 * pushed uniform per bundle. Figure out which one we need in the bundle (the
70 * scheduler needs to ensure we only have one type per bundle), validate
71 * everything, and rewrite away the register/uniform indices to use 3-bit
72 * sources directly. */
75 bi_lookup_constant(bi_clause
*clause
, uint64_t cons
, bool *hi
, bool b64
)
77 uint64_t want
= (cons
>> 4);
79 for (unsigned i
= 0; i
< clause
->constant_count
; ++i
) {
80 /* Only check top 60-bits since that's what's actually embedded
81 * in the clause, the bottom 4-bits are bundle-inline */
83 uint64_t candidates
[2] = {
84 clause
->constants
[i
] >> 4,
85 clause
->constants
[i
] >> 36
88 /* For <64-bit mode, we treat lo/hi separately */
91 candidates
[0] &= (0xFFFFFFFF >> 4);
93 if (candidates
[0] == want
)
96 if (candidates
[1] == want
&& !b64
) {
102 unreachable("Invalid constant accessed");
106 bi_constant_field(unsigned idx
)
110 const unsigned values
[] = {
114 return values
[idx
] << 4;
118 bi_assign_uniform_constant_single(
121 bi_instruction
*ins
, bool assigned
, bool fast_zero
)
126 if (ins
->type
== BI_BLEND
) {
128 regs
->uniform_constant
= 0x8;
132 if (ins
->type
== BI_BRANCH
&& clause
->branch_constant
) {
133 /* By convention branch constant is last */
134 unsigned idx
= clause
->constant_count
- 1;
136 /* We can only jump to clauses which are qword aligned so the
137 * bottom 4-bits of the offset are necessarily 0 */
140 /* Build the constant */
141 unsigned C
= bi_constant_field(idx
) | lo
;
143 if (assigned
&& regs
->uniform_constant
!= C
)
144 unreachable("Mismatched uniform/const field: branch");
146 regs
->uniform_constant
= C
;
150 bi_foreach_src(ins
, s
) {
151 if (s
== 0 && (ins
->type
== BI_LOAD_VAR_ADDRESS
|| ins
->type
== BI_LOAD_ATTR
)) continue;
152 if (s
== 1 && (ins
->type
== BI_BRANCH
)) continue;
154 if (ins
->src
[s
] & BIR_INDEX_CONSTANT
) {
155 /* Let direct addresses through */
156 if (ins
->type
== BI_LOAD_VAR
)
160 bool b64
= nir_alu_type_get_type_size(ins
->src_types
[s
]) > 32;
161 uint64_t cons
= bi_get_immediate(ins
, s
);
162 unsigned idx
= bi_lookup_constant(clause
, cons
, &hi
, b64
);
163 unsigned lo
= clause
->constants
[idx
] & 0xF;
164 unsigned f
= bi_constant_field(idx
) | lo
;
166 if (assigned
&& regs
->uniform_constant
!= f
)
167 unreachable("Mismatched uniform/const field: imm");
169 regs
->uniform_constant
= f
;
170 ins
->src
[s
] = BIR_INDEX_PASS
| (hi
? BIFROST_SRC_CONST_HI
: BIFROST_SRC_CONST_LO
);
172 } else if (ins
->src
[s
] & BIR_INDEX_ZERO
&& (ins
->type
== BI_LOAD_UNIFORM
|| ins
->type
== BI_LOAD_VAR
)) {
173 /* XXX: HACK UNTIL WE HAVE HI MATCHING DUE TO OVERFLOW XXX */
174 ins
->src
[s
] = BIR_INDEX_PASS
| BIFROST_SRC_CONST_HI
;
175 } else if (ins
->src
[s
] & BIR_INDEX_ZERO
&& !fast_zero
) {
176 /* FMAs have a fast zero port, ADD needs to use the
177 * uniform/const port's special 0 mode handled here */
180 if (assigned
&& regs
->uniform_constant
!= f
)
181 unreachable("Mismatched uniform/const field: 0");
183 regs
->uniform_constant
= f
;
184 ins
->src
[s
] = BIR_INDEX_PASS
| BIFROST_SRC_CONST_LO
;
186 } else if (ins
->src
[s
] & BIR_INDEX_ZERO
&& fast_zero
) {
187 ins
->src
[s
] = BIR_INDEX_PASS
| BIFROST_SRC_STAGE
;
188 } else if (s
& BIR_INDEX_UNIFORM
) {
189 unreachable("Push uniforms not implemented yet");
197 bi_assign_uniform_constant(
203 bi_assign_uniform_constant_single(regs
, clause
, bundle
.fma
, false, true);
205 bi_assign_uniform_constant_single(regs
, clause
, bundle
.add
, assigned
, false);
208 /* Assigns a port for reading, before anything is written */
211 bi_assign_port_read(bi_registers
*regs
, unsigned src
)
213 /* We only assign for registers */
214 if (!(src
& BIR_INDEX_REGISTER
))
217 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
219 /* Check if we already assigned the port */
220 for (unsigned i
= 0; i
<= 1; ++i
) {
221 if (regs
->port
[i
] == reg
&& regs
->enabled
[i
])
225 if (regs
->port
[3] == reg
&& regs
->read_port3
)
230 for (unsigned i
= 0; i
<= 1; ++i
) {
231 if (!regs
->enabled
[i
]) {
233 regs
->enabled
[i
] = true;
238 if (!regs
->read_port3
) {
240 regs
->read_port3
= true;
244 bi_print_ports(regs
, stderr
);
245 unreachable("Failed to find a free port for src");
249 bi_assign_ports(bi_bundle
*now
, bi_bundle
*prev
)
251 /* We assign ports for the main register mechanism. Special ops
252 * use the data registers, which has its own mechanism entirely
253 * and thus gets skipped over here. */
255 unsigned read_dreg
= now
->add
&&
256 bi_class_props
[now
->add
->type
] & BI_DATA_REG_SRC
;
258 unsigned write_dreg
= prev
->add
&&
259 bi_class_props
[prev
->add
->type
] & BI_DATA_REG_DEST
;
261 /* First, assign reads */
264 bi_foreach_src(now
->fma
, src
)
265 bi_assign_port_read(&now
->regs
, now
->fma
->src
[src
]);
268 bi_foreach_src(now
->add
, src
) {
269 if (!(src
== 0 && read_dreg
))
270 bi_assign_port_read(&now
->regs
, now
->add
->src
[src
]);
274 /* Next, assign writes */
276 if (prev
->add
&& prev
->add
->dest
& BIR_INDEX_REGISTER
&& !write_dreg
) {
277 now
->regs
.port
[2] = prev
->add
->dest
& ~BIR_INDEX_REGISTER
;
278 now
->regs
.write_add
= true;
281 if (prev
->fma
&& prev
->fma
->dest
& BIR_INDEX_REGISTER
) {
282 unsigned r
= prev
->fma
->dest
& ~BIR_INDEX_REGISTER
;
284 if (now
->regs
.write_add
) {
285 /* Scheduler constraint: cannot read 3 and write 2 */
286 assert(!now
->regs
.read_port3
);
287 now
->regs
.port
[3] = r
;
289 now
->regs
.port
[2] = r
;
292 now
->regs
.write_fma
= true;
298 /* Determines the register control field, ignoring the first? flag */
300 static enum bifrost_reg_control
301 bi_pack_register_ctrl_lo(bi_registers r
)
305 assert(!r
.read_port3
);
306 return BIFROST_WRITE_ADD_P2_FMA_P3
;
309 return BIFROST_WRITE_FMA_P2_READ_P3
;
311 return BIFROST_WRITE_FMA_P2
;
313 } else if (r
.write_add
) {
315 return BIFROST_WRITE_ADD_P2_READ_P3
;
317 return BIFROST_WRITE_ADD_P2
;
318 } else if (r
.read_port3
)
319 return BIFROST_READ_P3
;
321 return BIFROST_REG_NONE
;
324 /* Ditto but account for the first? flag this time */
326 static enum bifrost_reg_control
327 bi_pack_register_ctrl(bi_registers r
)
329 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl_lo(r
);
331 if (r
.first_instruction
) {
332 if (ctrl
== BIFROST_REG_NONE
)
333 ctrl
= BIFROST_FIRST_NONE
;
334 else if (ctrl
== BIFROST_WRITE_FMA_P2_READ_P3
)
335 ctrl
= BIFROST_FIRST_WRITE_FMA_P2_READ_P3
;
337 ctrl
|= BIFROST_FIRST_NONE
;
344 bi_pack_registers(bi_registers regs
)
346 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl(regs
);
347 struct bifrost_regs s
= { 0 };
350 if (regs
.enabled
[1]) {
351 /* Gotta save that bit!~ Required by the 63-x trick */
352 assert(regs
.port
[1] > regs
.port
[0]);
353 assert(regs
.enabled
[0]);
355 /* Do the 63-x trick, see docs/disasm */
356 if (regs
.port
[0] > 31) {
357 regs
.port
[0] = 63 - regs
.port
[0];
358 regs
.port
[1] = 63 - regs
.port
[1];
361 assert(regs
.port
[0] <= 31);
362 assert(regs
.port
[1] <= 63);
365 s
.reg1
= regs
.port
[1];
366 s
.reg0
= regs
.port
[0];
368 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
372 if (regs
.enabled
[0]) {
373 /* Bit 0 upper bit of port 0 */
374 s
.reg1
|= (regs
.port
[0] >> 5);
376 /* Rest of port 0 in usual spot */
377 s
.reg0
= (regs
.port
[0] & 0b11111);
379 /* Bit 1 set if port 0 also disabled */
384 /* When port 3 isn't used, we have to set it to port 2, and vice versa,
385 * or INSTR_INVALID_ENC is raised. The reason is unknown. */
387 bool has_port2
= regs
.write_fma
|| regs
.write_add
;
388 bool has_port3
= regs
.read_port3
|| (regs
.write_fma
&& regs
.write_add
);
391 regs
.port
[3] = regs
.port
[2];
394 regs
.port
[2] = regs
.port
[3];
396 s
.reg3
= regs
.port
[3];
397 s
.reg2
= regs
.port
[2];
398 s
.uniform_const
= regs
.uniform_constant
;
400 memcpy(&packed
, &s
, sizeof(s
));
405 bi_set_data_register(bi_clause
*clause
, unsigned idx
)
407 assert(idx
& BIR_INDEX_REGISTER
);
408 unsigned reg
= idx
& ~BIR_INDEX_REGISTER
;
410 clause
->data_register
= reg
;
414 bi_read_data_register(bi_clause
*clause
, bi_instruction
*ins
)
416 bi_set_data_register(clause
, ins
->src
[0]);
420 bi_write_data_register(bi_clause
*clause
, bi_instruction
*ins
)
422 bi_set_data_register(clause
, ins
->dest
);
425 static enum bifrost_packed_src
426 bi_get_src_reg_port(bi_registers
*regs
, unsigned src
)
428 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
430 if (regs
->port
[0] == reg
&& regs
->enabled
[0])
431 return BIFROST_SRC_PORT0
;
432 else if (regs
->port
[1] == reg
&& regs
->enabled
[1])
433 return BIFROST_SRC_PORT1
;
434 else if (regs
->port
[3] == reg
&& regs
->read_port3
)
435 return BIFROST_SRC_PORT3
;
437 unreachable("Tried to access register with no port");
440 static enum bifrost_packed_src
441 bi_get_src(bi_instruction
*ins
, bi_registers
*regs
, unsigned s
)
443 unsigned src
= ins
->src
[s
];
445 if (src
& BIR_INDEX_REGISTER
)
446 return bi_get_src_reg_port(regs
, src
);
447 else if (src
& BIR_INDEX_PASS
)
448 return src
& ~BIR_INDEX_PASS
;
450 bi_print_instruction(ins
, stderr
);
451 unreachable("Unknown src in above instruction");
455 /* Constructs a packed 2-bit swizzle for a 16-bit vec2 source. Source must be
456 * 16-bit and written components must correspond to valid swizzles (component x
460 bi_swiz16(bi_instruction
*ins
, unsigned src
)
462 assert(nir_alu_type_get_type_size(ins
->src_types
[src
]) == 16);
463 unsigned swizzle
= 0;
465 for (unsigned c
= 0; c
< 2; ++c
) {
466 if (!bi_writes_component(ins
, src
)) continue;
468 unsigned k
= ins
->swizzle
[src
][c
];
477 bi_pack_fma_fma(bi_instruction
*ins
, bi_registers
*regs
)
479 /* (-a)(-b) = ab, so we only need one negate bit */
480 bool negate_mul
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
482 if (ins
->op
.mscale
) {
483 assert(!(ins
->src_abs
[0] && ins
->src_abs
[1]));
484 assert(!ins
->src_abs
[2] || !ins
->src_neg
[3] || !ins
->src_abs
[3]);
486 /* We can have exactly one abs, and can flip the multiplication
487 * to make it fit if we have to */
488 bool flip_ab
= ins
->src_abs
[1];
490 struct bifrost_fma_mscale pack
= {
491 .src0
= bi_get_src(ins
, regs
, flip_ab
? 1 : 0),
492 .src1
= bi_get_src(ins
, regs
, flip_ab
? 0 : 1),
493 .src2
= bi_get_src(ins
, regs
, 2),
494 .src3
= bi_get_src(ins
, regs
, 3),
497 .src0_abs
= ins
->src_abs
[0] || ins
->src_abs
[1],
498 .src1_neg
= negate_mul
,
499 .src2_neg
= ins
->src_neg
[2],
500 .op
= BIFROST_FMA_OP_MSCALE
,
504 } else if (ins
->dest_type
== nir_type_float32
) {
505 struct bifrost_fma_fma pack
= {
506 .src0
= bi_get_src(ins
, regs
, 0),
507 .src1
= bi_get_src(ins
, regs
, 1),
508 .src2
= bi_get_src(ins
, regs
, 2),
509 .src0_abs
= ins
->src_abs
[0],
510 .src1_abs
= ins
->src_abs
[1],
511 .src2_abs
= ins
->src_abs
[2],
512 .src0_neg
= negate_mul
,
513 .src2_neg
= ins
->src_neg
[2],
514 .outmod
= ins
->outmod
,
515 .roundmode
= ins
->roundmode
,
516 .op
= BIFROST_FMA_OP_FMA
520 } else if (ins
->dest_type
== nir_type_float16
) {
521 struct bifrost_fma_fma16 pack
= {
522 .src0
= bi_get_src(ins
, regs
, 0),
523 .src1
= bi_get_src(ins
, regs
, 1),
524 .src2
= bi_get_src(ins
, regs
, 2),
525 .swizzle_0
= bi_swiz16(ins
, 0),
526 .swizzle_1
= bi_swiz16(ins
, 1),
527 .swizzle_2
= bi_swiz16(ins
, 2),
528 .src0_neg
= negate_mul
,
529 .src2_neg
= ins
->src_neg
[2],
530 .outmod
= ins
->outmod
,
531 .roundmode
= ins
->roundmode
,
532 .op
= BIFROST_FMA_OP_FMA16
537 unreachable("Invalid fma dest type");
542 bi_pack_fma_addmin_f32(bi_instruction
*ins
, bi_registers
*regs
)
545 (ins
->type
== BI_ADD
) ? BIFROST_FMA_OP_FADD32
:
546 (ins
->op
.minmax
== BI_MINMAX_MIN
) ? BIFROST_FMA_OP_FMIN32
:
547 BIFROST_FMA_OP_FMAX32
;
549 struct bifrost_fma_add pack
= {
550 .src0
= bi_get_src(ins
, regs
, 0),
551 .src1
= bi_get_src(ins
, regs
, 1),
552 .src0_abs
= ins
->src_abs
[0],
553 .src1_abs
= ins
->src_abs
[1],
554 .src0_neg
= ins
->src_neg
[0],
555 .src1_neg
= ins
->src_neg
[1],
557 .outmod
= ins
->outmod
,
558 .roundmode
= (ins
->type
== BI_ADD
) ? ins
->roundmode
: ins
->minmax
,
566 bi_pack_fp16_abs(bi_instruction
*ins
, bi_registers
*regs
, bool *flip
)
568 /* Absolute values are packed in a quirky way. Let k = src1 < src0. Let
569 * l be an auxiliary bit we encode. Then the hardware determines:
574 * Since add/min/max are commutative, this saves a bit by using the
575 * order of the operands as a bit (k). To pack this, first note:
577 * (l && k) implies (l || k).
579 * That is, if the second argument is abs'd, then the first argument
580 * also has abs. So there are three cases:
582 * Case 0: Neither src has absolute value. Then we have l = k = 0.
584 * Case 1: Exactly one src has absolute value. Assign that source to
585 * src0 and the other source to src1. Compute k = src1 < src0 based on
586 * that assignment. Then l = ~k.
588 * Case 2: Both sources have absolute value. Then we have l = k = 1.
589 * Note to force k = 1 requires that (src1 < src0) OR (src0 < src1).
590 * That is, this encoding is only valid if src1 and src0 are distinct.
591 * This is a scheduling restriction (XXX); if an op of this type
592 * requires both identical sources to have abs value, then we must
593 * schedule to ADD (which does not use this ordering trick).
596 unsigned abs_0
= ins
->src_abs
[0], abs_1
= ins
->src_abs
[1];
597 unsigned src_0
= bi_get_src(ins
, regs
, 0);
598 unsigned src_1
= bi_get_src(ins
, regs
, 1);
600 assert(!(abs_0
&& abs_1
&& src_0
== src_1
));
602 if (!abs_0
&& !abs_1
) {
603 /* Force k = 0 <===> NOT(src1 < src0) */
604 *flip
= (src_1
< src_0
);
606 } else if (abs_0
&& !abs_1
) {
607 return src_1
>= src_0
;
608 } else if (abs_1
&& !abs_0
) {
610 return src_0
>= src_1
;
612 *flip
= !(src_1
< src_0
);
618 bi_pack_fmadd_min_f16(bi_instruction
*ins
, bi_registers
*regs
, bool FMA
)
621 (!FMA
) ? ((ins
->op
.minmax
== BI_MINMAX_MIN
) ?
622 BIFROST_ADD_OP_FMIN16
: BIFROST_ADD_OP_FMAX16
) :
623 (ins
->type
== BI_ADD
) ? BIFROST_FMA_OP_FADD16
:
624 (ins
->op
.minmax
== BI_MINMAX_MIN
) ? BIFROST_FMA_OP_FMIN16
:
625 BIFROST_FMA_OP_FMAX16
;
628 bool l
= bi_pack_fp16_abs(ins
, regs
, &flip
);
629 unsigned src_0
= bi_get_src(ins
, regs
, 0);
630 unsigned src_1
= bi_get_src(ins
, regs
, 1);
633 struct bifrost_fma_add_minmax16 pack
= {
634 .src0
= flip
? src_1
: src_0
,
635 .src1
= flip
? src_0
: src_1
,
636 .src0_neg
= ins
->src_neg
[flip
? 1 : 0],
637 .src1_neg
= ins
->src_neg
[flip
? 0 : 1],
638 .src0_swizzle
= bi_swiz16(ins
, flip
? 1 : 0),
639 .src1_swizzle
= bi_swiz16(ins
, flip
? 0 : 1),
641 .outmod
= ins
->outmod
,
642 .mode
= (ins
->type
== BI_ADD
) ? ins
->roundmode
: ins
->minmax
,
648 /* Can't have modes for fp16 */
649 assert(ins
->outmod
== 0);
651 struct bifrost_add_fmin16 pack
= {
652 .src0
= flip
? src_1
: src_0
,
653 .src1
= flip
? src_0
: src_1
,
654 .src0_neg
= ins
->src_neg
[flip
? 1 : 0],
655 .src1_neg
= ins
->src_neg
[flip
? 0 : 1],
657 .src0_swizzle
= bi_swiz16(ins
, flip
? 1 : 0),
658 .src1_swizzle
= bi_swiz16(ins
, flip
? 0 : 1),
668 bi_pack_fma_addmin(bi_instruction
*ins
, bi_registers
*regs
)
670 if (ins
->dest_type
== nir_type_float32
)
671 return bi_pack_fma_addmin_f32(ins
, regs
);
672 else if(ins
->dest_type
== nir_type_float16
)
673 return bi_pack_fmadd_min_f16(ins
, regs
, true);
675 unreachable("Unknown FMA/ADD type");
679 bi_pack_fma_1src(bi_instruction
*ins
, bi_registers
*regs
, unsigned op
)
681 struct bifrost_fma_inst pack
= {
682 .src0
= bi_get_src(ins
, regs
, 0),
690 bi_pack_fma_2src(bi_instruction
*ins
, bi_registers
*regs
, unsigned op
)
692 struct bifrost_fma_2src pack
= {
693 .src0
= bi_get_src(ins
, regs
, 0),
694 .src1
= bi_get_src(ins
, regs
, 1),
702 bi_pack_add_1src(bi_instruction
*ins
, bi_registers
*regs
, unsigned op
)
704 struct bifrost_add_inst pack
= {
705 .src0
= bi_get_src(ins
, regs
, 0),
712 static enum bifrost_csel_cond
713 bi_cond_to_csel(enum bi_cond cond
, bool *flip
, bool *invert
, nir_alu_type T
)
715 nir_alu_type B
= nir_alu_type_get_base_type(T
);
716 unsigned idx
= (B
== nir_type_float
) ? 0 :
717 ((B
== nir_type_int
) ? 1 : 2);
724 const enum bifrost_csel_cond ops
[] = {
736 const enum bifrost_csel_cond ops
[] = {
748 const enum bifrost_csel_cond ops
[] = {
751 BIFROST_IEQ_F
/* sign is irrelevant */
757 unreachable("Invalid op for csel");
762 bi_pack_fma_csel(bi_instruction
*ins
, bi_registers
*regs
)
764 /* TODO: Use csel3 as well */
765 bool flip
= false, invert
= false;
767 enum bifrost_csel_cond cond
=
768 bi_cond_to_csel(ins
->cond
, &flip
, &invert
, ins
->src_types
[0]);
770 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
772 unsigned cmp_0
= (flip
? 1 : 0);
773 unsigned cmp_1
= (flip
? 0 : 1);
774 unsigned res_0
= (invert
? 3 : 2);
775 unsigned res_1
= (invert
? 2 : 3);
777 struct bifrost_csel4 pack
= {
778 .src0
= bi_get_src(ins
, regs
, cmp_0
),
779 .src1
= bi_get_src(ins
, regs
, cmp_1
),
780 .src2
= bi_get_src(ins
, regs
, res_0
),
781 .src3
= bi_get_src(ins
, regs
, res_1
),
783 .op
= (size
== 16) ? BIFROST_FMA_OP_CSEL4_V16
:
791 bi_pack_fma_frexp(bi_instruction
*ins
, bi_registers
*regs
)
793 unsigned op
= BIFROST_FMA_OP_FREXPE_LOG
;
794 return bi_pack_fma_1src(ins
, regs
, op
);
798 bi_pack_fma_reduce(bi_instruction
*ins
, bi_registers
*regs
)
800 if (ins
->op
.reduce
== BI_REDUCE_ADD_FREXPM
) {
801 return bi_pack_fma_2src(ins
, regs
, BIFROST_FMA_OP_ADD_FREXPM
);
803 unreachable("Invalid reduce op");
807 /* We have a single convert opcode in the IR but a number of opcodes that could
808 * come out. In particular we have native opcodes for:
810 * [ui]16 --> [fui]32 -- int16_to_32
811 * f16 --> f32 -- float16_to_32
812 * f32 --> f16 -- float32_to_16
813 * f32 --> [ui]32 -- float32_to_int
814 * [ui]32 --> f32 -- int_to_float32
815 * [fui]16 --> [fui]16 -- f2i_i2f16
819 bi_pack_convert(bi_instruction
*ins
, bi_registers
*regs
, bool FMA
)
821 nir_alu_type from_base
= nir_alu_type_get_base_type(ins
->src_types
[0]);
822 unsigned from_size
= nir_alu_type_get_type_size(ins
->src_types
[0]);
823 bool from_unsigned
= from_base
== nir_type_uint
;
825 nir_alu_type to_base
= nir_alu_type_get_base_type(ins
->dest_type
);
826 unsigned to_size
= nir_alu_type_get_type_size(ins
->dest_type
);
827 bool to_unsigned
= to_base
== nir_type_uint
;
828 bool to_float
= to_base
== nir_type_float
;
831 assert((from_base
!= to_base
) || (from_size
!= to_size
));
832 assert((MAX2(from_size
, to_size
) / MIN2(from_size
, to_size
)) <= 2);
834 /* f32 to f16 is special */
835 if (from_size
== 32 && to_size
== 16 && from_base
== to_base
) {
837 assert(from_base
== nir_type_float
);
839 struct bifrost_fma_2src pfma
= {
840 .src0
= bi_get_src(ins
, regs
, 0),
841 .src1
= bi_get_src(ins
, regs
, 1),
842 .op
= BIFROST_FMA_FLOAT32_TO_16
845 struct bifrost_add_2src padd
= {
846 .src0
= bi_get_src(ins
, regs
, 0),
847 .src1
= bi_get_src(ins
, regs
, 1),
848 .op
= BIFROST_ADD_FLOAT32_TO_16
858 /* Otherwise, figure out the mode */
861 if (from_size
== 16 && to_size
== 32) {
862 unsigned component
= ins
->swizzle
[0][0];
863 assert(component
<= 1);
865 if (from_base
== nir_type_float
)
866 op
= BIFROST_CONVERT_5(component
);
868 op
= BIFROST_CONVERT_4(from_unsigned
, component
, to_float
);
871 unsigned swizzle
= (from_size
== 16) ? bi_swiz16(ins
, 0) : 0;
872 bool is_unsigned
= from_unsigned
;
874 if (from_base
== nir_type_float
) {
875 assert(to_base
!= nir_type_float
);
876 is_unsigned
= to_unsigned
;
878 if (from_size
== 32 && to_size
== 32)
879 mode
= BIFROST_CONV_F32_TO_I32
;
880 else if (from_size
== 16 && to_size
== 16)
881 mode
= BIFROST_CONV_F16_TO_I16
;
883 unreachable("Invalid float conversion");
885 assert(to_base
== nir_type_float
);
886 assert(from_size
== to_size
);
889 mode
= BIFROST_CONV_I32_TO_F32
;
890 else if (to_size
== 16)
891 mode
= BIFROST_CONV_I16_TO_F16
;
893 unreachable("Invalid int conversion");
896 /* Fixup swizzle for 32-bit only modes */
898 if (mode
== BIFROST_CONV_I32_TO_F32
)
900 else if (mode
== BIFROST_CONV_F32_TO_I32
)
903 op
= BIFROST_CONVERT(is_unsigned
, ins
->roundmode
, swizzle
, mode
);
905 /* Unclear what the top bit is for... maybe 16-bit related */
906 bool mode2
= mode
== BIFROST_CONV_F16_TO_I16
;
907 bool mode6
= mode
== BIFROST_CONV_I16_TO_F16
;
909 if (!(mode2
|| mode6
))
914 return bi_pack_fma_1src(ins
, regs
, BIFROST_FMA_CONVERT
| op
);
916 return bi_pack_add_1src(ins
, regs
, BIFROST_ADD_CONVERT
| op
);
920 bi_pack_fma_select(bi_instruction
*ins
, bi_registers
*regs
)
922 unsigned size
= nir_alu_type_get_type_size(ins
->src_types
[0]);
925 unsigned swiz
= (ins
->swizzle
[0][0] | (ins
->swizzle
[1][0] << 1));
926 unsigned op
= BIFROST_FMA_SEL_16(swiz
);
927 return bi_pack_fma_2src(ins
, regs
, op
);
928 } else if (size
== 8) {
931 for (unsigned c
= 0; c
< 4; ++c
) {
932 if (ins
->swizzle
[c
][0]) {
933 /* Ensure lowering restriction is met */
934 assert(ins
->swizzle
[c
][0] == 2);
939 struct bifrost_fma_sel8 pack
= {
940 .src0
= bi_get_src(ins
, regs
, 0),
941 .src1
= bi_get_src(ins
, regs
, 1),
942 .src2
= bi_get_src(ins
, regs
, 2),
943 .src3
= bi_get_src(ins
, regs
, 3),
945 .op
= BIFROST_FMA_OP_SEL8
950 unreachable("Unimplemented");
954 static enum bifrost_fcmp_cond
955 bi_fcmp_cond(enum bi_cond cond
)
958 case BI_COND_LT
: return BIFROST_OLT
;
959 case BI_COND_LE
: return BIFROST_OLE
;
960 case BI_COND_GE
: return BIFROST_OGE
;
961 case BI_COND_GT
: return BIFROST_OGT
;
962 case BI_COND_EQ
: return BIFROST_OEQ
;
963 case BI_COND_NE
: return BIFROST_UNE
;
964 default: unreachable("Unknown bi_cond");
968 /* a <?> b <==> b <flip(?)> a (TODO: NaN behaviour?) */
970 static enum bifrost_fcmp_cond
971 bi_flip_fcmp(enum bifrost_fcmp_cond cond
)
986 unreachable("Unknown fcmp cond");
991 bi_pack_fma_cmp(bi_instruction
*ins
, bi_registers
*regs
)
993 nir_alu_type Tl
= ins
->src_types
[0];
994 nir_alu_type Tr
= ins
->src_types
[1];
996 if (Tl
== nir_type_float32
|| Tr
== nir_type_float32
) {
997 /* TODO: Mixed 32/16 cmp */
1000 enum bifrost_fcmp_cond cond
= bi_fcmp_cond(ins
->cond
);
1002 /* Only src1 has neg, so we arrange:
1005 * -a < -b <===> a > b
1006 * -a < b <===> a > -b
1007 * TODO: Is this NaN-precise?
1010 bool flip
= ins
->src_neg
[0];
1011 bool neg
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
1014 cond
= bi_flip_fcmp(cond
);
1016 struct bifrost_fma_fcmp pack
= {
1017 .src0
= bi_get_src(ins
, regs
, 0),
1018 .src1
= bi_get_src(ins
, regs
, 1),
1019 .src0_abs
= ins
->src_abs
[0],
1020 .src1_abs
= ins
->src_abs
[1],
1025 .op
= BIFROST_FMA_OP_FCMP_D3D
1028 RETURN_PACKED(pack
);
1029 } else if (Tl
== nir_type_float16
&& Tr
== nir_type_float16
) {
1031 bool l
= bi_pack_fp16_abs(ins
, regs
, &flip
);
1032 enum bifrost_fcmp_cond cond
= bi_fcmp_cond(ins
->cond
);
1035 cond
= bi_flip_fcmp(cond
);
1037 struct bifrost_fma_fcmp16 pack
= {
1038 .src0
= bi_get_src(ins
, regs
, flip
? 1 : 0),
1039 .src1
= bi_get_src(ins
, regs
, flip
? 0 : 1),
1040 .src0_swizzle
= bi_swiz16(ins
, flip
? 1 : 0),
1041 .src1_swizzle
= bi_swiz16(ins
, flip
? 0 : 1),
1045 .op
= BIFROST_FMA_OP_FCMP_D3D_16
,
1048 RETURN_PACKED(pack
);
1050 unreachable("Unknown cmp type");
1055 bi_fma_bitwise_op(enum bi_bitwise_op op
, bool rshift
)
1059 /* Via De Morgan's */
1061 BIFROST_FMA_OP_RSHIFT_NAND
:
1062 BIFROST_FMA_OP_LSHIFT_NAND
;
1063 case BI_BITWISE_AND
:
1065 BIFROST_FMA_OP_RSHIFT_AND
:
1066 BIFROST_FMA_OP_LSHIFT_AND
;
1067 case BI_BITWISE_XOR
:
1068 /* Shift direction handled out of band */
1069 return BIFROST_FMA_OP_RSHIFT_XOR
;
1071 unreachable("Unknown op");
1076 bi_pack_fma_bitwise(bi_instruction
*ins
, bi_registers
*regs
)
1078 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
1081 bool invert_0
= ins
->bitwise
.src_invert
[0];
1082 bool invert_1
= ins
->bitwise
.src_invert
[1];
1084 if (ins
->op
.bitwise
== BI_BITWISE_OR
) {
1085 /* Becomes NAND, so via De Morgan's:
1086 * f(A) | f(B) = ~(~f(A) & ~f(B))
1087 * = NAND(~f(A), ~f(B))
1090 invert_0
= !invert_0
;
1091 invert_1
= !invert_1
;
1092 } else if (ins
->op
.bitwise
== BI_BITWISE_XOR
) {
1093 /* ~A ^ ~B = ~(A ^ ~B) = ~(~(A ^ B)) = A ^ B
1094 * ~A ^ B = ~(A ^ B) = A ^ ~B
1097 invert_0
^= invert_1
;
1100 /* invert_1 ends up specifying shift direction */
1101 invert_1
= !ins
->bitwise
.rshift
;
1104 struct bifrost_shift_fma pack
= {
1105 .src0
= bi_get_src(ins
, regs
, 0),
1106 .src1
= bi_get_src(ins
, regs
, 1),
1107 .src2
= bi_get_src(ins
, regs
, 2),
1108 .half
= (size
== 32) ? 0 : (size
== 16) ? 0x7 : (size
== 8) ? 0x4 : 0,
1110 .invert_1
= invert_0
,
1111 .invert_2
= invert_1
,
1112 .op
= bi_fma_bitwise_op(ins
->op
.bitwise
, ins
->bitwise
.rshift
)
1115 RETURN_PACKED(pack
);
1119 bi_pack_fma_round(bi_instruction
*ins
, bi_registers
*regs
)
1121 bool fp16
= ins
->dest_type
== nir_type_float16
;
1122 assert(fp16
|| ins
->dest_type
== nir_type_float32
);
1125 ? BIFROST_FMA_ROUND_16(ins
->roundmode
, bi_swiz16(ins
, 0))
1126 : BIFROST_FMA_ROUND_32(ins
->roundmode
);
1128 return bi_pack_fma_1src(ins
, regs
, op
);
1132 bi_pack_fma_imath(bi_instruction
*ins
, bi_registers
*regs
)
1134 /* Scheduler: only ADD can have 8/16-bit imath */
1135 assert(ins
->dest_type
== nir_type_int32
|| ins
->dest_type
== nir_type_uint32
);
1137 unsigned op
= ins
->op
.imath
== BI_IMATH_ADD
1138 ? BIFROST_FMA_IADD_32
1139 : BIFROST_FMA_ISUB_32
;
1141 return bi_pack_fma_2src(ins
, regs
, op
);
1145 bi_pack_fma_imul(bi_instruction
*ins
, bi_registers
*regs
)
1147 assert(ins
->op
.imul
== BI_IMUL_IMUL
);
1148 unsigned op
= BIFROST_FMA_IMUL_32
;
1149 return bi_pack_fma_2src(ins
, regs
, op
);
1153 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
, bi_registers
*regs
)
1156 return BIFROST_FMA_NOP
;
1158 switch (bundle
.fma
->type
) {
1160 return bi_pack_fma_addmin(bundle
.fma
, regs
);
1162 return bi_pack_fma_cmp(bundle
.fma
, regs
);
1164 return bi_pack_fma_bitwise(bundle
.fma
, regs
);
1166 return bi_pack_convert(bundle
.fma
, regs
, true);
1168 return bi_pack_fma_csel(bundle
.fma
, regs
);
1170 return bi_pack_fma_fma(bundle
.fma
, regs
);
1172 return bi_pack_fma_frexp(bundle
.fma
, regs
);
1174 return bi_pack_fma_imath(bundle
.fma
, regs
);
1176 return bi_pack_fma_addmin(bundle
.fma
, regs
);
1178 return bi_pack_fma_1src(bundle
.fma
, regs
, BIFROST_FMA_OP_MOV
);
1180 return bi_pack_fma_select(bundle
.fma
, regs
);
1182 return bi_pack_fma_round(bundle
.fma
, regs
);
1184 return bi_pack_fma_reduce(bundle
.fma
, regs
);
1186 return bi_pack_fma_imul(bundle
.fma
, regs
);
1188 unreachable("Cannot encode class as FMA");
1193 bi_pack_add_ld_vary(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1195 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
1196 assert(size
== 32 || size
== 16);
1198 unsigned op
= (size
== 32) ?
1199 BIFROST_ADD_OP_LD_VAR_32
:
1200 BIFROST_ADD_OP_LD_VAR_16
;
1202 unsigned packed_addr
= 0;
1204 if (ins
->src
[0] & BIR_INDEX_CONSTANT
) {
1205 /* Direct uses address field directly */
1206 packed_addr
= bi_get_immediate(ins
, 0);
1208 /* Indirect gets an extra source */
1209 packed_addr
= bi_get_src(ins
, regs
, 0) | 0b11000;
1212 /* The destination is thrown in the data register */
1213 assert(ins
->dest
& BIR_INDEX_REGISTER
);
1214 clause
->data_register
= ins
->dest
& ~BIR_INDEX_REGISTER
;
1216 unsigned channels
= ins
->vector_channels
;
1217 assert(channels
>= 1 && channels
<= 4);
1219 struct bifrost_ld_var pack
= {
1220 .src0
= bi_get_src(ins
, regs
, 1),
1221 .addr
= packed_addr
,
1222 .channels
= MALI_POSITIVE(channels
),
1223 .interp_mode
= ins
->load_vary
.interp_mode
,
1224 .reuse
= ins
->load_vary
.reuse
,
1225 .flat
= ins
->load_vary
.flat
,
1229 RETURN_PACKED(pack
);
1233 bi_pack_add_2src(bi_instruction
*ins
, bi_registers
*regs
, unsigned op
)
1235 struct bifrost_add_2src pack
= {
1236 .src0
= bi_get_src(ins
, regs
, 0),
1237 .src1
= bi_get_src(ins
, regs
, 1),
1241 RETURN_PACKED(pack
);
1245 bi_pack_add_addmin_f32(bi_instruction
*ins
, bi_registers
*regs
)
1248 (ins
->type
== BI_ADD
) ? BIFROST_ADD_OP_FADD32
:
1249 (ins
->op
.minmax
== BI_MINMAX_MIN
) ? BIFROST_ADD_OP_FMIN32
:
1250 BIFROST_ADD_OP_FMAX32
;
1252 struct bifrost_add_faddmin pack
= {
1253 .src0
= bi_get_src(ins
, regs
, 0),
1254 .src1
= bi_get_src(ins
, regs
, 1),
1255 .src0_abs
= ins
->src_abs
[0],
1256 .src1_abs
= ins
->src_abs
[1],
1257 .src0_neg
= ins
->src_neg
[0],
1258 .src1_neg
= ins
->src_neg
[1],
1259 .outmod
= ins
->outmod
,
1260 .mode
= (ins
->type
== BI_ADD
) ? ins
->roundmode
: ins
->minmax
,
1264 RETURN_PACKED(pack
);
1268 bi_pack_add_add_f16(bi_instruction
*ins
, bi_registers
*regs
)
1270 /* ADD.v2f16 can't have outmod */
1271 assert(ins
->outmod
== BIFROST_NONE
);
1273 struct bifrost_add_faddmin pack
= {
1274 .src0
= bi_get_src(ins
, regs
, 0),
1275 .src1
= bi_get_src(ins
, regs
, 1),
1276 .src0_abs
= ins
->src_abs
[0],
1277 .src1_abs
= ins
->src_abs
[1],
1278 .src0_neg
= ins
->src_neg
[0],
1279 .src1_neg
= ins
->src_neg
[1],
1280 .select
= bi_swiz16(ins
, 0), /* swizzle_0 */
1281 .outmod
= bi_swiz16(ins
, 1), /* swizzle_1 */
1282 .mode
= ins
->roundmode
,
1283 .op
= BIFROST_ADD_OP_FADD16
1286 RETURN_PACKED(pack
);
1290 bi_pack_add_addmin(bi_instruction
*ins
, bi_registers
*regs
)
1292 if (ins
->dest_type
== nir_type_float32
)
1293 return bi_pack_add_addmin_f32(ins
, regs
);
1294 else if (ins
->dest_type
== nir_type_float16
) {
1295 if (ins
->type
== BI_ADD
)
1296 return bi_pack_add_add_f16(ins
, regs
);
1298 return bi_pack_fmadd_min_f16(ins
, regs
, false);
1300 unreachable("Unknown FMA/ADD type");
1304 bi_pack_add_ld_ubo(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1306 assert(ins
->vector_channels
>= 1 && ins
->vector_channels
<= 4);
1308 const unsigned ops
[4] = {
1309 BIFROST_ADD_OP_LD_UBO_1
,
1310 BIFROST_ADD_OP_LD_UBO_2
,
1311 BIFROST_ADD_OP_LD_UBO_3
,
1312 BIFROST_ADD_OP_LD_UBO_4
1315 bi_write_data_register(clause
, ins
);
1316 return bi_pack_add_2src(ins
, regs
, ops
[ins
->vector_channels
- 1]);
1319 static enum bifrost_ldst_type
1320 bi_pack_ldst_type(nir_alu_type T
)
1323 case nir_type_float16
: return BIFROST_LDST_F16
;
1324 case nir_type_float32
: return BIFROST_LDST_F32
;
1325 case nir_type_int32
: return BIFROST_LDST_I32
;
1326 case nir_type_uint32
: return BIFROST_LDST_U32
;
1327 default: unreachable("Invalid type loaded");
1332 bi_pack_add_ld_var_addr(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1334 struct bifrost_ld_var_addr pack
= {
1335 .src0
= bi_get_src(ins
, regs
, 1),
1336 .src1
= bi_get_src(ins
, regs
, 2),
1337 .location
= bi_get_immediate(ins
, 0),
1338 .type
= bi_pack_ldst_type(ins
->src_types
[3]),
1339 .op
= BIFROST_ADD_OP_LD_VAR_ADDR
1342 bi_write_data_register(clause
, ins
);
1343 RETURN_PACKED(pack
);
1347 bi_pack_add_ld_attr(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1349 assert(ins
->vector_channels
>= 0 && ins
->vector_channels
<= 4);
1351 struct bifrost_ld_attr pack
= {
1352 .src0
= bi_get_src(ins
, regs
, 1),
1353 .src1
= bi_get_src(ins
, regs
, 2),
1354 .location
= bi_get_immediate(ins
, 0),
1355 .channels
= MALI_POSITIVE(ins
->vector_channels
),
1356 .type
= bi_pack_ldst_type(ins
->dest_type
),
1357 .op
= BIFROST_ADD_OP_LD_ATTR
1360 bi_write_data_register(clause
, ins
);
1361 RETURN_PACKED(pack
);
1365 bi_pack_add_st_vary(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1367 assert(ins
->vector_channels
>= 1 && ins
->vector_channels
<= 4);
1369 struct bifrost_st_vary pack
= {
1370 .src0
= bi_get_src(ins
, regs
, 1),
1371 .src1
= bi_get_src(ins
, regs
, 2),
1372 .src2
= bi_get_src(ins
, regs
, 3),
1373 .channels
= MALI_POSITIVE(ins
->vector_channels
),
1374 .op
= BIFROST_ADD_OP_ST_VAR
1377 bi_read_data_register(clause
, ins
);
1378 RETURN_PACKED(pack
);
1382 bi_pack_add_atest(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1384 bool fp16
= (ins
->src_types
[1] == nir_type_float16
);
1386 struct bifrost_add_atest pack
= {
1387 .src0
= bi_get_src(ins
, regs
, 0),
1388 .src1
= bi_get_src(ins
, regs
, 1),
1390 .component
= fp16
? ins
->swizzle
[1][0] : 1, /* Set for fp32 */
1391 .op
= BIFROST_ADD_OP_ATEST
,
1394 /* Despite *also* writing with the usual mechanism... quirky and
1395 * perhaps unnecessary, but let's match the blob */
1396 clause
->data_register
= ins
->dest
& ~BIR_INDEX_REGISTER
;
1398 RETURN_PACKED(pack
);
1402 bi_pack_add_blend(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
)
1404 struct bifrost_add_inst pack
= {
1405 .src0
= bi_get_src(ins
, regs
, 1),
1406 .op
= BIFROST_ADD_OP_BLEND
1409 /* TODO: Pack location in uniform_const */
1410 assert(ins
->blend_location
== 0);
1412 bi_read_data_register(clause
, ins
);
1413 RETURN_PACKED(pack
);
1417 bi_pack_add_special(bi_instruction
*ins
, bi_registers
*regs
)
1420 bool fp16
= ins
->dest_type
== nir_type_float16
;
1421 bool Y
= ins
->swizzle
[0][0];
1423 if (ins
->op
.special
== BI_SPECIAL_FRCP
) {
1425 (Y
? BIFROST_ADD_OP_FRCP_FAST_F16_Y
:
1426 BIFROST_ADD_OP_FRCP_FAST_F16_X
) :
1427 BIFROST_ADD_OP_FRCP_FAST_F32
;
1428 } else if (ins
->op
.special
== BI_SPECIAL_FRSQ
) {
1430 (Y
? BIFROST_ADD_OP_FRSQ_FAST_F16_Y
:
1431 BIFROST_ADD_OP_FRSQ_FAST_F16_X
) :
1432 BIFROST_ADD_OP_FRSQ_FAST_F32
;
1434 } else if (ins
->op
.special
== BI_SPECIAL_EXP2_LOW
) {
1436 op
= BIFROST_ADD_OP_FEXP2_FAST
;
1438 unreachable("Unknown special op");
1441 return bi_pack_add_1src(ins
, regs
, op
);
1445 bi_pack_add_table(bi_instruction
*ins
, bi_registers
*regs
)
1448 assert(ins
->dest_type
== nir_type_float32
);
1450 op
= BIFROST_ADD_OP_LOG2_HELP
;
1451 return bi_pack_add_1src(ins
, regs
, op
);
1454 bi_pack_add_tex_compact(bi_clause
*clause
, bi_instruction
*ins
, bi_registers
*regs
, gl_shader_stage stage
)
1456 bool f16
= ins
->dest_type
== nir_type_float16
;
1457 bool vtx
= stage
!= MESA_SHADER_FRAGMENT
;
1459 struct bifrost_tex_compact pack
= {
1460 .src0
= bi_get_src(ins
, regs
, 0),
1461 .src1
= bi_get_src(ins
, regs
, 1),
1462 .op
= f16
? BIFROST_ADD_OP_TEX_COMPACT_F16(vtx
) :
1463 BIFROST_ADD_OP_TEX_COMPACT_F32(vtx
),
1464 .compute_lod
= !vtx
,
1465 .tex_index
= ins
->texture
.texture_index
,
1466 .sampler_index
= ins
->texture
.sampler_index
1469 bi_write_data_register(clause
, ins
);
1470 RETURN_PACKED(pack
);
1474 bi_pack_add_select(bi_instruction
*ins
, bi_registers
*regs
)
1476 unsigned size
= nir_alu_type_get_type_size(ins
->src_types
[0]);
1479 unsigned swiz
= (ins
->swizzle
[0][0] | (ins
->swizzle
[1][0] << 1));
1480 unsigned op
= BIFROST_ADD_SEL_16(swiz
);
1481 return bi_pack_add_2src(ins
, regs
, op
);
1484 static enum bifrost_discard_cond
1485 bi_cond_to_discard(enum bi_cond cond
, bool *flip
)
1492 return BIFROST_DISCARD_FLT
;
1497 return BIFROST_DISCARD_FLE
;
1499 return BIFROST_DISCARD_FNE
;
1501 return BIFROST_DISCARD_FEQ
;
1503 unreachable("Invalid op for discard");
1508 bi_pack_add_discard(bi_instruction
*ins
, bi_registers
*regs
)
1510 bool fp16
= ins
->src_types
[0] == nir_type_float16
;
1511 assert(fp16
|| ins
->src_types
[0] == nir_type_float32
);
1514 enum bifrost_discard_cond cond
= bi_cond_to_discard(ins
->cond
, &flip
);
1516 struct bifrost_add_discard pack
= {
1517 .src0
= bi_get_src(ins
, regs
, flip
? 1 : 0),
1518 .src1
= bi_get_src(ins
, regs
, flip
? 0 : 1),
1520 .src0_select
= fp16
? ins
->swizzle
[0][0] : 0,
1521 .src1_select
= fp16
? ins
->swizzle
[1][0] : 0,
1522 .fp32
= fp16
? 0 : 1,
1523 .op
= BIFROST_ADD_OP_DISCARD
1526 RETURN_PACKED(pack
);
1529 static enum bifrost_icmp_cond
1530 bi_cond_to_icmp(enum bi_cond cond
, bool *flip
, bool is_unsigned
, bool is_16
)
1537 return is_unsigned
? (is_16
? BIFROST_ICMP_IGE
: BIFROST_ICMP_UGT
)
1543 return is_unsigned
? BIFROST_ICMP_UGE
:
1544 (is_16
? BIFROST_ICMP_UGT
: BIFROST_ICMP_IGE
);
1546 return BIFROST_ICMP_NEQ
;
1548 return BIFROST_ICMP_EQ
;
1550 unreachable("Invalid op for icmp");
1555 bi_pack_add_icmp32(bi_instruction
*ins
, bi_registers
*regs
, bool flip
,
1556 enum bifrost_icmp_cond cond
)
1558 struct bifrost_add_icmp pack
= {
1559 .src0
= bi_get_src(ins
, regs
, flip
? 1 : 0),
1560 .src1
= bi_get_src(ins
, regs
, flip
? 0 : 1),
1564 .op
= BIFROST_ADD_OP_ICMP_32
1567 RETURN_PACKED(pack
);
1571 bi_pack_add_icmp16(bi_instruction
*ins
, bi_registers
*regs
, bool flip
,
1572 enum bifrost_icmp_cond cond
)
1574 struct bifrost_add_icmp16 pack
= {
1575 .src0
= bi_get_src(ins
, regs
, flip
? 1 : 0),
1576 .src1
= bi_get_src(ins
, regs
, flip
? 0 : 1),
1577 .src0_swizzle
= bi_swiz16(ins
, flip
? 1 : 0),
1578 .src1_swizzle
= bi_swiz16(ins
, flip
? 0 : 1),
1581 .op
= BIFROST_ADD_OP_ICMP_16
1584 RETURN_PACKED(pack
);
1588 bi_pack_add_cmp(bi_instruction
*ins
, bi_registers
*regs
)
1590 nir_alu_type Tl
= ins
->src_types
[0];
1591 nir_alu_type Tr
= ins
->src_types
[1];
1592 nir_alu_type Bl
= nir_alu_type_get_base_type(Tl
);
1594 if (Bl
== nir_type_uint
|| Bl
== nir_type_int
) {
1596 unsigned sz
= nir_alu_type_get_type_size(Tl
);
1600 enum bifrost_icmp_cond cond
= bi_cond_to_icmp(
1601 sz
== 16 ? /*bi_invert_cond*/(ins
->cond
) : ins
->cond
,
1602 &flip
, Bl
== nir_type_uint
, sz
== 16);
1605 return bi_pack_add_icmp32(ins
, regs
, flip
, cond
);
1607 return bi_pack_add_icmp16(ins
, regs
, flip
, cond
);
1609 unreachable("TODO");
1611 unreachable("TODO");
1616 bi_pack_add_imath(bi_instruction
*ins
, bi_registers
*regs
)
1618 /* TODO: 32+16 add */
1619 assert(ins
->src_types
[0] == ins
->src_types
[1]);
1620 unsigned sz
= nir_alu_type_get_type_size(ins
->src_types
[0]);
1621 enum bi_imath_op p
= ins
->op
.imath
;
1626 op
= (p
== BI_IMATH_ADD
) ? BIFROST_ADD_IADD_8
:
1628 } else if (sz
== 16) {
1629 op
= (p
== BI_IMATH_ADD
) ? BIFROST_ADD_IADD_16
:
1630 BIFROST_ADD_ISUB_16
;
1631 } else if (sz
== 32) {
1632 op
= (p
== BI_IMATH_ADD
) ? BIFROST_ADD_IADD_32
:
1633 BIFROST_ADD_ISUB_32
;
1635 unreachable("64-bit todo");
1638 return bi_pack_add_2src(ins
, regs
, op
);
1642 bi_pack_add_branch_cond(bi_instruction
*ins
, bi_registers
*regs
)
1644 assert(ins
->cond
== BI_COND_EQ
);
1645 assert(ins
->src
[1] == BIR_INDEX_ZERO
);
1647 unsigned zero_ctrl
= 0;
1648 unsigned size
= nir_alu_type_get_type_size(ins
->src_types
[0]);
1651 /* See BR_SIZE_ZERO swizzle disassembly */
1652 zero_ctrl
= ins
->swizzle
[0][0] ? 1 : 2;
1658 bool port_swapped
= false;
1660 /* We assigned the constant port to fetch the branch offset so we can
1661 * just passthrough here. We put in the HI slot to match the blob since
1662 * that's where the magic flags end up */
1663 struct bifrost_branch pack
= {
1664 .src0
= bi_get_src(ins
, regs
, 0),
1665 .src1
= (zero_ctrl
<< 1) | !port_swapped
,
1666 .src2
= BIFROST_SRC_CONST_HI
,
1668 .size
= BR_SIZE_ZERO
,
1669 .op
= BIFROST_ADD_OP_BRANCH
1672 RETURN_PACKED(pack
);
1676 bi_pack_add_branch_uncond(bi_instruction
*ins
, bi_registers
*regs
)
1678 struct bifrost_branch pack
= {
1679 /* It's unclear what these bits actually mean */
1680 .src0
= BIFROST_SRC_CONST_LO
,
1681 .src1
= BIFROST_SRC_PASS_FMA
,
1683 /* Offset, see above */
1684 .src2
= BIFROST_SRC_CONST_HI
,
1686 /* All ones in fact */
1687 .cond
= (BR_ALWAYS
& 0x7),
1688 .size
= (BR_ALWAYS
>> 3),
1689 .op
= BIFROST_ADD_OP_BRANCH
1692 RETURN_PACKED(pack
);
1696 bi_pack_add_branch(bi_instruction
*ins
, bi_registers
*regs
)
1698 if (ins
->cond
== BI_COND_ALWAYS
)
1699 return bi_pack_add_branch_uncond(ins
, regs
);
1701 return bi_pack_add_branch_cond(ins
, regs
);
1705 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
, bi_registers
*regs
, gl_shader_stage stage
)
1708 return BIFROST_ADD_NOP
;
1710 switch (bundle
.add
->type
) {
1712 return bi_pack_add_addmin(bundle
.add
, regs
);
1714 return bi_pack_add_atest(clause
, bundle
.add
, regs
);
1716 return bi_pack_add_branch(bundle
.add
, regs
);
1718 return bi_pack_add_cmp(bundle
.add
, regs
);
1720 return bi_pack_add_blend(clause
, bundle
.add
, regs
);
1722 unreachable("Packing todo");
1724 return bi_pack_convert(bundle
.add
, regs
, false);
1726 return bi_pack_add_discard(bundle
.add
, regs
);
1728 unreachable("Packing todo");
1730 return bi_pack_add_imath(bundle
.add
, regs
);
1732 unreachable("Packing todo");
1734 return bi_pack_add_ld_attr(clause
, bundle
.add
, regs
);
1735 case BI_LOAD_UNIFORM
:
1736 return bi_pack_add_ld_ubo(clause
, bundle
.add
, regs
);
1738 return bi_pack_add_ld_vary(clause
, bundle
.add
, regs
);
1739 case BI_LOAD_VAR_ADDRESS
:
1740 return bi_pack_add_ld_var_addr(clause
, bundle
.add
, regs
);
1742 return bi_pack_add_addmin(bundle
.add
, regs
);
1745 unreachable("Packing todo");
1747 return bi_pack_add_st_vary(clause
, bundle
.add
, regs
);
1749 return bi_pack_add_special(bundle
.add
, regs
);
1751 return bi_pack_add_table(bundle
.add
, regs
);
1753 return bi_pack_add_select(bundle
.add
, regs
);
1755 if (bundle
.add
->op
.texture
== BI_TEX_COMPACT
)
1756 return bi_pack_add_tex_compact(clause
, bundle
.add
, regs
, stage
);
1758 unreachable("Unknown tex type");
1760 unreachable("Packing todo");
1762 unreachable("Cannot encode class as ADD");
1766 struct bi_packed_bundle
{
1771 /* We must ensure port 1 > port 0 for the 63-x trick to function, so we fix
1772 * this up at pack time. (Scheduling doesn't care.) */
1775 bi_flip_ports(bi_registers
*regs
)
1777 if (regs
->enabled
[0] && regs
->enabled
[1] && regs
->port
[1] < regs
->port
[0]) {
1778 unsigned temp
= regs
->port
[0];
1779 regs
->port
[0] = regs
->port
[1];
1780 regs
->port
[1] = temp
;
1785 static struct bi_packed_bundle
1786 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
, bi_bundle prev
, bool first_bundle
, gl_shader_stage stage
)
1788 bi_assign_ports(&bundle
, &prev
);
1789 bi_assign_uniform_constant(clause
, &bundle
.regs
, bundle
);
1790 bundle
.regs
.first_instruction
= first_bundle
;
1792 bi_flip_ports(&bundle
.regs
);
1794 uint64_t reg
= bi_pack_registers(bundle
.regs
);
1795 uint64_t fma
= bi_pack_fma(clause
, bundle
, &bundle
.regs
);
1796 uint64_t add
= bi_pack_add(clause
, bundle
, &bundle
.regs
, stage
);
1798 struct bi_packed_bundle packed
= {
1799 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
1806 /* Packs the next two constants as a dedicated constant quadword at the end of
1807 * the clause, returning the number packed. There are two cases to consider:
1809 * Case #1: Branching is not used. For a single constant copy the upper nibble
1812 * Case #2: Branching is used. For a single constant, it suffices to set the
1813 * upper nibble to 4 and leave the latter constant 0, which matches what the
1816 * Extending to multiple constants is considerably more tricky and left for
1821 bi_pack_constants(bi_context
*ctx
, bi_clause
*clause
,
1823 struct util_dynarray
*emission
)
1825 /* After these two, are we done? Determines tag */
1826 bool done
= clause
->constant_count
<= (index
+ 2);
1827 bool only
= clause
->constant_count
<= (index
+ 1);
1829 /* Is the constant we're packing for a branch? */
1830 bool branches
= clause
->branch_constant
&& done
;
1833 assert(index
== 0 && clause
->bundle_count
== 1);
1836 /* Compute branch offset instead of a dummy 0 */
1838 bi_instruction
*br
= clause
->bundles
[clause
->bundle_count
- 1].add
;
1839 assert(br
&& br
->type
== BI_BRANCH
&& br
->branch_target
);
1841 /* Put it in the high place */
1842 int32_t qwords
= bi_block_offset(ctx
, clause
, br
->branch_target
);
1843 int32_t bytes
= qwords
* 16;
1845 /* Copy so we get proper sign behaviour */
1847 memcpy(&raw
, &bytes
, sizeof(raw
));
1849 /* Clear off top bits for the magic bits */
1852 /* Put in top 32-bits */
1853 clause
->constants
[index
+ 0] = ((uint64_t) raw
) << 32ull;
1856 uint64_t hi
= clause
->constants
[index
+ 0] >> 60ull;
1858 struct bifrost_fmt_constant quad
= {
1859 .pos
= 0, /* TODO */
1860 .tag
= done
? BIFROST_FMTC_FINAL
: BIFROST_FMTC_CONSTANTS
,
1861 .imm_1
= clause
->constants
[index
+ 0] >> 4,
1862 .imm_2
= ((hi
< 8) ? (hi
<< 60ull) : 0) >> 4,
1866 /* Branch offsets are less than 60-bits so this should work at
1868 quad
.imm_1
|= (4ull << 60ull) >> 4;
1872 /* XXX: On G71, Connor observed that the difference of the top 4 bits
1873 * of the second constant with the first must be less than 8, otherwise
1874 * we have to swap them. On G52, I'm able to reproduce a similar issue
1875 * but with a different workaround (modeled above with a single
1876 * constant, unclear how to workaround for multiple constants.) Further
1877 * investigation needed. Possibly an errata. XXX */
1879 util_dynarray_append(emission
, struct bifrost_fmt_constant
, quad
);
1885 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
,
1886 bi_clause
*next_1
, bi_clause
*next_2
,
1887 struct util_dynarray
*emission
, gl_shader_stage stage
)
1889 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0], clause
->bundles
[0], true, stage
);
1890 assert(clause
->bundle_count
== 1);
1892 /* Used to decide if we elide writes */
1893 bool is_fragment
= ctx
->stage
== MESA_SHADER_FRAGMENT
;
1895 /* State for packing constants throughout */
1896 unsigned constant_index
= 0;
1898 struct bifrost_fmt1 quad_1
= {
1899 .tag
= clause
->constant_count
? BIFROST_FMT1_CONSTANTS
: BIFROST_FMT1_FINAL
,
1900 .header
= bi_pack_header(clause
, next_1
, next_2
, is_fragment
),
1902 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
1903 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
1906 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
1908 /* Pack the remaining constants */
1910 while (constant_index
< clause
->constant_count
) {
1911 constant_index
+= bi_pack_constants(ctx
, clause
,
1912 constant_index
, emission
);
1917 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
1919 /* Try the first clause in this block if we're starting from scratch */
1920 if (!clause
&& !list_is_empty(&((bi_block
*) block
)->clauses
))
1921 return list_first_entry(&((bi_block
*) block
)->clauses
, bi_clause
, link
);
1923 /* Try the next clause in this block */
1924 if (clause
&& clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
1925 return list_first_entry(&(clause
->link
), bi_clause
, link
);
1927 /* Try the next block, or the one after that if it's empty, etc .*/
1928 pan_block
*next_block
= pan_next_block(block
);
1930 bi_foreach_block_from(ctx
, next_block
, block
) {
1931 bi_block
*blk
= (bi_block
*) block
;
1933 if (!list_is_empty(&blk
->clauses
))
1934 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
1941 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
1943 util_dynarray_init(emission
, NULL
);
1945 bi_foreach_block(ctx
, _block
) {
1946 bi_block
*block
= (bi_block
*) _block
;
1948 /* Passthrough the first clause of where we're branching to for
1949 * the last clause of the block (the clause with the branch) */
1951 bi_clause
*succ_clause
= block
->base
.successors
[1] ?
1952 bi_next_clause(ctx
, block
->base
.successors
[0], NULL
) : NULL
;
1954 bi_foreach_clause_in_block(block
, clause
) {
1955 bool is_last
= clause
->link
.next
== &block
->clauses
;
1957 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
1958 bi_clause
*next_2
= is_last
? succ_clause
: NULL
;
1960 bi_pack_clause(ctx
, clause
, next
, next_2
, emission
, ctx
->stage
);