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 /* First, assign reads */
116 bi_foreach_src(now
.fma
, src
)
117 bi_assign_port_read(®s
, now
.fma
->src
[src
]);
120 bi_foreach_src(now
.add
, src
)
121 bi_assign_port_read(®s
, now
.add
->src
[src
]);
123 /* Next, assign writes */
125 if (prev
.fma
&& prev
.fma
->dest
& BIR_INDEX_REGISTER
) {
126 regs
.port
[2] = prev
.fma
->dest
& ~BIR_INDEX_REGISTER
;
127 regs
.write_fma
= true;
130 if (prev
.add
&& prev
.add
->dest
& BIR_INDEX_REGISTER
) {
131 unsigned r
= prev
.add
->dest
& ~BIR_INDEX_REGISTER
;
133 if (regs
.write_fma
) {
134 /* Scheduler constraint: cannot read 3 and write 2 */
135 assert(!regs
.read_port3
);
141 regs
.write_add
= true;
144 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
146 if (regs
.enabled
[0] && regs
.enabled
[1] && regs
.port
[1] < regs
.port
[0]) {
147 unsigned temp
= regs
.port
[0];
148 regs
.port
[0] = regs
.port
[1];
155 /* Determines the register control field, ignoring the first? flag */
157 static enum bifrost_reg_control
158 bi_pack_register_ctrl_lo(struct bi_registers r
)
162 assert(!r
.read_port3
);
163 return BIFROST_WRITE_ADD_P2_FMA_P3
;
166 return BIFROST_WRITE_FMA_P2_READ_P3
;
168 return BIFROST_WRITE_FMA_P2
;
170 } else if (r
.write_add
) {
172 return BIFROST_WRITE_ADD_P2_READ_P3
;
174 return BIFROST_WRITE_ADD_P2
;
175 } else if (r
.read_port3
)
176 return BIFROST_READ_P3
;
178 return BIFROST_REG_NONE
;
181 /* Ditto but account for the first? flag this time */
183 static enum bifrost_reg_control
184 bi_pack_register_ctrl(struct bi_registers r
)
186 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl_lo(r
);
188 if (r
.first_instruction
) {
189 if (ctrl
== BIFROST_REG_NONE
)
190 ctrl
= BIFROST_FIRST_NONE
;
192 ctrl
|= BIFROST_FIRST_NONE
;
199 bi_pack_registers(struct bi_registers regs
)
201 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl(regs
);
202 struct bifrost_regs s
;
205 if (regs
.enabled
[1]) {
206 /* Gotta save that bit!~ Required by the 63-x trick */
207 assert(regs
.port
[1] > regs
.port
[0]);
208 assert(regs
.enabled
[0]);
210 /* Do the 63-x trick, see docs/disasm */
211 if (regs
.port
[0] > 31) {
212 regs
.port
[0] = 63 - regs
.port
[0];
213 regs
.port
[1] = 63 - regs
.port
[1];
216 assert(regs
.port
[0] <= 31);
217 assert(regs
.port
[1] <= 63);
220 s
.reg1
= regs
.port
[1];
221 s
.reg0
= regs
.port
[0];
223 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
226 if (regs
.enabled
[0]) {
227 /* Bit 0 upper bit of port 0 */
228 s
.reg1
|= (regs
.port
[0] >> 5);
230 /* Rest of port 0 in usual spot */
231 s
.reg0
= (regs
.port
[0] & 0b11111);
233 /* Bit 1 set if port 0 also disabled */
238 s
.reg3
= regs
.port
[3];
239 s
.reg2
= regs
.port
[2];
240 s
.uniform_const
= regs
.uniform_constant
;
242 memcpy(&packed
, &s
, sizeof(s
));
246 static enum bifrost_packed_src
247 bi_get_src_reg_port(struct bi_registers
*regs
, unsigned src
)
249 unsigned reg
= src
& ~BIR_INDEX_REGISTER
;
251 if (regs
->port
[0] == reg
&& regs
->enabled
[0])
252 return BIFROST_SRC_PORT0
;
253 else if (regs
->port
[1] == reg
&& regs
->enabled
[1])
254 return BIFROST_SRC_PORT1
;
255 else if (regs
->port
[3] == reg
&& regs
->read_port3
)
256 return BIFROST_SRC_PORT3
;
258 unreachable("Tried to access register with no port");
261 static enum bifrost_packed_src
262 bi_get_src_const(struct bi_registers
*regs
, unsigned constant
)
264 if (regs
->uniform_constant
& (1 << 7))
265 unreachable("Tried to get constant but loading uniforms");
267 unsigned loc
= (regs
->uniform_constant
>> 4) & 0x7;
270 unreachable("TODO: constants in clauses");
272 unsigned lo
= regs
->uniform_constant
& 0xF;
276 unreachable("Tried to load !0 in 0 slot");
278 return BIFROST_SRC_CONST_LO
;
280 unreachable("Special slot is not a fixed immediate");
284 static enum bifrost_packed_src
285 bi_get_src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned s
, bool is_fma
)
287 unsigned src
= ins
->src
[s
];
289 if (src
& BIR_INDEX_REGISTER
)
290 return bi_get_src_reg_port(regs
, src
);
291 else if (src
& BIR_INDEX_ZERO
&& is_fma
)
292 return BIFROST_SRC_STAGE
;
293 else if (src
& BIR_INDEX_ZERO
)
294 return bi_get_src_const(regs
, 0);
295 else if (src
& BIR_INDEX_PASS
)
296 return src
& ~BIR_INDEX_PASS
;
298 unreachable("Unknown src");
302 bi_pack_fma_fma(bi_instruction
*ins
, struct bi_registers
*regs
)
304 /* (-a)(-b) = ab, so we only need one negate bit */
305 bool negate_mul
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
307 struct bifrost_fma_fma pack
= {
308 .src0
= bi_get_src(ins
, regs
, 0, true),
309 .src1
= bi_get_src(ins
, regs
, 1, true),
310 .src2
= bi_get_src(ins
, regs
, 2, true),
311 .src0_abs
= ins
->src_abs
[0],
312 .src1_abs
= ins
->src_abs
[1],
313 .src2_abs
= ins
->src_abs
[2],
314 .src0_neg
= negate_mul
,
315 .src2_neg
= ins
->src_neg
[2],
316 .op
= BIFROST_FMA_OP_FMA
323 bi_pack_fma_add(bi_instruction
*ins
, struct bi_registers
*regs
)
325 /* TODO: fadd16 packing is a bit different */
326 assert(ins
->dest_type
== nir_type_float32
);
328 struct bifrost_fma_add pack
= {
329 .src0
= bi_get_src(ins
, regs
, 0, true),
330 .src1
= bi_get_src(ins
, regs
, 1, true),
331 .src0_abs
= ins
->src_abs
[0],
332 .src1_abs
= ins
->src_abs
[1],
333 .src0_neg
= ins
->src_neg
[0],
334 .src1_neg
= ins
->src_neg
[1],
336 .outmod
= ins
->outmod
,
337 .roundmode
= ins
->roundmode
,
338 .op
= BIFROST_FMA_OP_FADD32
345 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
348 return BIFROST_FMA_NOP
;
350 switch (bundle
.fma
->type
) {
352 return bi_pack_fma_add(bundle
.fma
, regs
);
357 return BIFROST_FMA_NOP
;
359 return bi_pack_fma_fma(bundle
.fma
, regs
);
367 return BIFROST_FMA_NOP
;
369 unreachable("Cannot encode class as FMA");
374 bi_pack_add_ld_vary(bi_instruction
*ins
, struct bi_registers
*regs
)
376 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
377 assert(size
== 32 || size
== 16);
379 unsigned op
= (size
== 32) ?
380 BIFROST_ADD_OP_LD_VAR_32
:
381 BIFROST_ADD_OP_LD_VAR_16
;
383 unsigned cmask
= bi_from_bytemask(ins
->writemask
, size
/ 8);
384 unsigned channels
= util_bitcount(cmask
);
385 assert(cmask
== ((1 << channels
) - 1));
387 unsigned packed_addr
= 0;
389 if (ins
->src
[0] & BIR_INDEX_CONSTANT
) {
390 /* Direct uses address field directly */
391 packed_addr
= ins
->src
[0] & ~BIR_INDEX_CONSTANT
;
392 assert(packed_addr
< 0b1000);
394 /* Indirect gets an extra source */
395 packed_addr
= bi_get_src(ins
, regs
, 0, false) | 0b11000;
398 assert(channels
>= 1 && channels
<= 4);
400 struct bifrost_ld_var pack
= {
401 .src0
= bi_get_src(ins
, regs
, 1, false),
403 .channels
= MALI_POSITIVE(channels
),
404 .interp_mode
= ins
->load_vary
.interp_mode
,
405 .reuse
= ins
->load_vary
.reuse
,
406 .flat
= ins
->load_vary
.flat
,
414 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
417 return BIFROST_ADD_NOP
;
419 switch (bundle
.add
->type
) {
431 case BI_LOAD_UNIFORM
:
433 return BIFROST_ADD_NOP
;
435 return bi_pack_add_ld_vary(bundle
.add
, regs
);
436 case BI_LOAD_VAR_ADDRESS
:
446 return BIFROST_ADD_NOP
;
448 unreachable("Cannot encode class as ADD");
452 struct bi_packed_bundle
{
457 static struct bi_packed_bundle
458 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
, bi_bundle prev
, bool first_bundle
)
460 struct bi_registers regs
= bi_assign_ports(bundle
, prev
);
461 regs
.first_instruction
= first_bundle
;
463 uint64_t reg
= bi_pack_registers(regs
);
464 uint64_t fma
= bi_pack_fma(clause
, bundle
, ®s
);
465 uint64_t add
= bi_pack_add(clause
, bundle
, ®s
);
467 struct bi_packed_bundle packed
= {
468 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
476 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
, bi_clause
*next
,
477 struct util_dynarray
*emission
)
479 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0], clause
->bundles
[0], true);
480 assert(clause
->bundle_count
== 1);
482 struct bifrost_fmt1 quad_1
= {
483 .tag
= BIFROST_FMT1_FINAL
,
484 .header
= bi_pack_header(clause
, next
),
486 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
487 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
490 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
494 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
496 /* Try the next clause in this block */
497 if (clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
498 return list_first_entry(&(clause
->link
), bi_clause
, link
);
500 /* Try the next block, or the one after that if it's empty, etc .*/
501 pan_block
*next_block
= pan_next_block(block
);
503 bi_foreach_block_from(ctx
, next_block
, block
) {
504 bi_block
*blk
= (bi_block
*) block
;
506 if (!list_is_empty(&blk
->clauses
))
507 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
514 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
516 util_dynarray_init(emission
, NULL
);
518 bi_foreach_block(ctx
, _block
) {
519 bi_block
*block
= (bi_block
*) _block
;
521 bi_foreach_clause_in_block(block
, clause
) {
522 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
523 bi_pack_clause(ctx
, clause
, next
, emission
);