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
)
39 struct bifrost_header header
= {
41 .no_end_of_shader
= (next
!= NULL
),
45 memcpy(&u
, &header
, sizeof(header
));
49 /* Represents the assignment of ports for a given bundle */
52 /* Register to assign to each port */
55 /* Read ports can be disabled */
58 /* Should we write FMA? what about ADD? If only a single port is
59 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
60 bool write_fma
, write_add
;
62 /* Should we read with port 3? */
65 /* Packed uniform/constant */
66 unsigned uniform_constant
;
68 /* Whether writes are actually for the last instruction */
69 bool first_instruction
;
72 /* Assigns a port for reading, before anything is written */
75 bi_assign_port_read(struct bi_registers
*regs
, unsigned src
)
77 /* We only assign for registers */
78 if (!(src
& BIR_INDEX_REGISTER
))
81 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
83 /* Check if we already assigned the port */
84 for (unsigned i
= 0; i
<= 1; ++i
) {
85 if (regs
->port
[i
] == reg
&& regs
->enabled
[i
])
89 if (regs
->port
[3] == reg
&& regs
->read_port3
)
94 for (unsigned i
= 0; i
<= 1; ++i
) {
95 if (!regs
->enabled
[i
]) {
97 regs
->enabled
[i
] = true;
102 if (!regs
->read_port3
) {
104 regs
->read_port3
= true;
108 static struct bi_registers
109 bi_assign_ports(bi_bundle now
, bi_bundle prev
)
111 struct bi_registers regs
= { 0 };
113 /* We assign ports for the main register mechanism. Special ops
114 * use the data registers, which has its own mechanism entirely
115 * and thus gets skipped over here. */
117 unsigned read_dreg
= now
.add
&&
118 bi_class_props
[now
.add
->type
] & BI_DATA_REG_SRC
;
120 unsigned write_dreg
= prev
.add
&&
121 bi_class_props
[prev
.add
->type
] & BI_DATA_REG_DEST
;
123 /* First, assign reads */
126 bi_foreach_src(now
.fma
, src
)
127 bi_assign_port_read(®s
, now
.fma
->src
[src
]);
130 bi_foreach_src(now
.add
, src
) {
131 if (!(src
== 0 && read_dreg
))
132 bi_assign_port_read(®s
, now
.add
->src
[src
]);
136 /* Next, assign writes */
138 if (prev
.fma
&& prev
.fma
->dest
& BIR_INDEX_REGISTER
) {
139 regs
.port
[2] = prev
.fma
->dest
& ~BIR_INDEX_REGISTER
;
140 regs
.write_fma
= true;
143 if (prev
.add
&& prev
.add
->dest
& BIR_INDEX_REGISTER
&& !write_dreg
) {
144 unsigned r
= prev
.add
->dest
& ~BIR_INDEX_REGISTER
;
146 if (regs
.write_fma
) {
147 /* Scheduler constraint: cannot read 3 and write 2 */
148 assert(!regs
.read_port3
);
154 regs
.write_add
= true;
157 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
159 if (regs
.enabled
[0] && regs
.enabled
[1] && regs
.port
[1] < regs
.port
[0]) {
160 unsigned temp
= regs
.port
[0];
161 regs
.port
[0] = regs
.port
[1];
168 /* Determines the register control field, ignoring the first? flag */
170 static enum bifrost_reg_control
171 bi_pack_register_ctrl_lo(struct bi_registers r
)
175 assert(!r
.read_port3
);
176 return BIFROST_WRITE_ADD_P2_FMA_P3
;
179 return BIFROST_WRITE_FMA_P2_READ_P3
;
181 return BIFROST_WRITE_FMA_P2
;
183 } else if (r
.write_add
) {
185 return BIFROST_WRITE_ADD_P2_READ_P3
;
187 return BIFROST_WRITE_ADD_P2
;
188 } else if (r
.read_port3
)
189 return BIFROST_READ_P3
;
191 return BIFROST_REG_NONE
;
194 /* Ditto but account for the first? flag this time */
196 static enum bifrost_reg_control
197 bi_pack_register_ctrl(struct bi_registers r
)
199 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl_lo(r
);
201 if (r
.first_instruction
) {
202 if (ctrl
== BIFROST_REG_NONE
)
203 ctrl
= BIFROST_FIRST_NONE
;
205 ctrl
|= BIFROST_FIRST_NONE
;
212 bi_pack_registers(struct bi_registers regs
)
214 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl(regs
);
215 struct bifrost_regs s
;
218 if (regs
.enabled
[1]) {
219 /* Gotta save that bit!~ Required by the 63-x trick */
220 assert(regs
.port
[1] > regs
.port
[0]);
221 assert(regs
.enabled
[0]);
223 /* Do the 63-x trick, see docs/disasm */
224 if (regs
.port
[0] > 31) {
225 regs
.port
[0] = 63 - regs
.port
[0];
226 regs
.port
[1] = 63 - regs
.port
[1];
229 assert(regs
.port
[0] <= 31);
230 assert(regs
.port
[1] <= 63);
233 s
.reg1
= regs
.port
[1];
234 s
.reg0
= regs
.port
[0];
236 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
239 if (regs
.enabled
[0]) {
240 /* Bit 0 upper bit of port 0 */
241 s
.reg1
|= (regs
.port
[0] >> 5);
243 /* Rest of port 0 in usual spot */
244 s
.reg0
= (regs
.port
[0] & 0b11111);
246 /* Bit 1 set if port 0 also disabled */
251 s
.reg3
= regs
.port
[3];
252 s
.reg2
= regs
.port
[2];
253 s
.uniform_const
= regs
.uniform_constant
;
255 memcpy(&packed
, &s
, sizeof(s
));
259 static enum bifrost_packed_src
260 bi_get_src_reg_port(struct bi_registers
*regs
, unsigned src
)
262 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
264 if (regs
->port
[0] == reg
&& regs
->enabled
[0])
265 return BIFROST_SRC_PORT0
;
266 else if (regs
->port
[1] == reg
&& regs
->enabled
[1])
267 return BIFROST_SRC_PORT1
;
268 else if (regs
->port
[3] == reg
&& regs
->read_port3
)
269 return BIFROST_SRC_PORT3
;
271 unreachable("Tried to access register with no port");
274 static enum bifrost_packed_src
275 bi_get_src_const(struct bi_registers
*regs
, unsigned constant
)
277 if (regs
->uniform_constant
& (1 << 7))
278 unreachable("Tried to get constant but loading uniforms");
280 unsigned loc
= (regs
->uniform_constant
>> 4) & 0x7;
283 unreachable("TODO: constants in clauses");
285 unsigned lo
= regs
->uniform_constant
& 0xF;
289 unreachable("Tried to load !0 in 0 slot");
291 return BIFROST_SRC_CONST_LO
;
293 unreachable("Special slot is not a fixed immediate");
297 static enum bifrost_packed_src
298 bi_get_src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned s
, bool is_fma
)
300 unsigned src
= ins
->src
[s
];
302 if (src
& BIR_INDEX_REGISTER
)
303 return bi_get_src_reg_port(regs
, src
);
304 else if (src
& BIR_INDEX_ZERO
&& is_fma
)
305 return BIFROST_SRC_STAGE
;
306 else if (src
& BIR_INDEX_ZERO
)
307 return bi_get_src_const(regs
, 0);
308 else if (src
& BIR_INDEX_PASS
)
309 return src
& ~BIR_INDEX_PASS
;
311 unreachable("Unknown src");
315 bi_pack_fma_fma(bi_instruction
*ins
, struct bi_registers
*regs
)
317 /* (-a)(-b) = ab, so we only need one negate bit */
318 bool negate_mul
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
320 struct bifrost_fma_fma pack
= {
321 .src0
= bi_get_src(ins
, regs
, 0, true),
322 .src1
= bi_get_src(ins
, regs
, 1, true),
323 .src2
= bi_get_src(ins
, regs
, 2, true),
324 .src0_abs
= ins
->src_abs
[0],
325 .src1_abs
= ins
->src_abs
[1],
326 .src2_abs
= ins
->src_abs
[2],
327 .src0_neg
= negate_mul
,
328 .src2_neg
= ins
->src_neg
[2],
329 .op
= BIFROST_FMA_OP_FMA
336 bi_pack_fma_add(bi_instruction
*ins
, struct bi_registers
*regs
)
338 /* TODO: fadd16 packing is a bit different */
339 assert(ins
->dest_type
== nir_type_float32
);
341 struct bifrost_fma_add pack
= {
342 .src0
= bi_get_src(ins
, regs
, 0, true),
343 .src1
= bi_get_src(ins
, regs
, 1, true),
344 .src0_abs
= ins
->src_abs
[0],
345 .src1_abs
= ins
->src_abs
[1],
346 .src0_neg
= ins
->src_neg
[0],
347 .src1_neg
= ins
->src_neg
[1],
349 .outmod
= ins
->outmod
,
350 .roundmode
= ins
->roundmode
,
351 .op
= BIFROST_FMA_OP_FADD32
358 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
361 return BIFROST_FMA_NOP
;
363 switch (bundle
.fma
->type
) {
365 return bi_pack_fma_add(bundle
.fma
, regs
);
370 return BIFROST_FMA_NOP
;
372 return bi_pack_fma_fma(bundle
.fma
, regs
);
380 return BIFROST_FMA_NOP
;
382 unreachable("Cannot encode class as FMA");
387 bi_pack_add_ld_vary(bi_instruction
*ins
, struct bi_registers
*regs
)
389 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
390 assert(size
== 32 || size
== 16);
392 unsigned op
= (size
== 32) ?
393 BIFROST_ADD_OP_LD_VAR_32
:
394 BIFROST_ADD_OP_LD_VAR_16
;
396 unsigned cmask
= bi_from_bytemask(ins
->writemask
, size
/ 8);
397 unsigned channels
= util_bitcount(cmask
);
398 assert(cmask
== ((1 << channels
) - 1));
400 unsigned packed_addr
= 0;
402 if (ins
->src
[0] & BIR_INDEX_CONSTANT
) {
403 /* Direct uses address field directly */
404 packed_addr
= ins
->src
[0] & ~BIR_INDEX_CONSTANT
;
405 assert(packed_addr
< 0b1000);
407 /* Indirect gets an extra source */
408 packed_addr
= bi_get_src(ins
, regs
, 0, false) | 0b11000;
411 assert(channels
>= 1 && channels
<= 4);
413 struct bifrost_ld_var pack
= {
414 .src0
= bi_get_src(ins
, regs
, 1, false),
416 .channels
= MALI_POSITIVE(channels
),
417 .interp_mode
= ins
->load_vary
.interp_mode
,
418 .reuse
= ins
->load_vary
.reuse
,
419 .flat
= ins
->load_vary
.flat
,
427 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
430 return BIFROST_ADD_NOP
;
432 switch (bundle
.add
->type
) {
444 case BI_LOAD_UNIFORM
:
446 return BIFROST_ADD_NOP
;
448 return bi_pack_add_ld_vary(bundle
.add
, regs
);
449 case BI_LOAD_VAR_ADDRESS
:
459 return BIFROST_ADD_NOP
;
461 unreachable("Cannot encode class as ADD");
465 struct bi_packed_bundle
{
470 static struct bi_packed_bundle
471 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
, bi_bundle prev
, bool first_bundle
)
473 struct bi_registers regs
= bi_assign_ports(bundle
, prev
);
474 regs
.first_instruction
= first_bundle
;
476 uint64_t reg
= bi_pack_registers(regs
);
477 uint64_t fma
= bi_pack_fma(clause
, bundle
, ®s
);
478 uint64_t add
= bi_pack_add(clause
, bundle
, ®s
);
480 struct bi_packed_bundle packed
= {
481 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
489 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
, bi_clause
*next
,
490 struct util_dynarray
*emission
)
492 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0], clause
->bundles
[0], true);
493 assert(clause
->bundle_count
== 1);
495 struct bifrost_fmt1 quad_1
= {
496 .tag
= BIFROST_FMT1_FINAL
,
497 .header
= bi_pack_header(clause
, next
),
499 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
500 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
503 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
507 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
509 /* Try the next clause in this block */
510 if (clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
511 return list_first_entry(&(clause
->link
), bi_clause
, link
);
513 /* Try the next block, or the one after that if it's empty, etc .*/
514 pan_block
*next_block
= pan_next_block(block
);
516 bi_foreach_block_from(ctx
, next_block
, block
) {
517 bi_block
*blk
= (bi_block
*) block
;
519 if (!list_is_empty(&blk
->clauses
))
520 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
527 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
529 util_dynarray_init(emission
, NULL
);
531 bi_foreach_block(ctx
, _block
) {
532 bi_block
*block
= (bi_block
*) _block
;
534 bi_foreach_clause_in_block(block
, clause
) {
535 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
536 bi_pack_clause(ctx
, clause
, next
, emission
);