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_fma_src(bi_instruction
*ins
, struct bi_registers
*regs
, unsigned s
)
264 unsigned src
= ins
->src
[s
];
266 if (src
& BIR_INDEX_REGISTER
)
267 return bi_get_src_reg_port(regs
, src
);
268 else if (src
& BIR_INDEX_ZERO
)
269 return BIFROST_SRC_STAGE
;
270 else if (src
& BIR_INDEX_PASS
)
271 return src
& ~BIR_INDEX_PASS
;
273 unreachable("Unknown src in FMA");
277 bi_pack_fma_fma(bi_instruction
*ins
, struct bi_registers
*regs
)
279 /* (-a)(-b) = ab, so we only need one negate bit */
280 bool negate_mul
= ins
->src_neg
[0] ^ ins
->src_neg
[1];
282 struct bifrost_fma_fma pack
= {
283 .src0
= bi_get_fma_src(ins
, regs
, 0),
284 .src1
= bi_get_fma_src(ins
, regs
, 1),
285 .src2
= bi_get_fma_src(ins
, regs
, 2),
286 .src0_abs
= ins
->src_abs
[0],
287 .src1_abs
= ins
->src_abs
[1],
288 .src2_abs
= ins
->src_abs
[2],
289 .src0_neg
= negate_mul
,
290 .src2_neg
= ins
->src_neg
[2],
291 .op
= BIFROST_FMA_OP_FMA
298 bi_pack_fma_add(bi_instruction
*ins
, struct bi_registers
*regs
)
300 /* TODO: fadd16 packing is a bit different */
301 assert(ins
->dest_type
== nir_type_float32
);
303 struct bifrost_fma_add pack
= {
304 .src0
= bi_get_fma_src(ins
, regs
, 0),
305 .src1
= bi_get_fma_src(ins
, regs
, 1),
306 .src0_abs
= ins
->src_abs
[0],
307 .src1_abs
= ins
->src_abs
[1],
308 .src0_neg
= ins
->src_neg
[0],
309 .src1_neg
= ins
->src_neg
[1],
311 .outmod
= ins
->outmod
,
312 .roundmode
= ins
->roundmode
,
313 .op
= BIFROST_FMA_OP_FADD32
320 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
323 return BIFROST_FMA_NOP
;
325 switch (bundle
.fma
->type
) {
327 return bi_pack_fma_add(bundle
.fma
, regs
);
332 return BIFROST_FMA_NOP
;
334 return bi_pack_fma_fma(bundle
.fma
, regs
);
342 return BIFROST_FMA_NOP
;
344 unreachable("Cannot encode class as FMA");
349 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
, struct bi_registers
*regs
)
352 return BIFROST_ADD_NOP
;
355 struct bi_packed_bundle
{
360 static struct bi_packed_bundle
361 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
, bi_bundle prev
, bool first_bundle
)
363 struct bi_registers regs
= bi_assign_ports(bundle
, prev
);
364 regs
.first_instruction
= first_bundle
;
366 uint64_t reg
= bi_pack_registers(regs
);
367 uint64_t fma
= bi_pack_fma(clause
, bundle
, ®s
);
368 uint64_t add
= bi_pack_add(clause
, bundle
, ®s
);
370 struct bi_packed_bundle packed
= {
371 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
379 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
, bi_clause
*next
,
380 struct util_dynarray
*emission
)
382 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0], clause
->bundles
[0], true);
383 assert(clause
->bundle_count
== 1);
385 struct bifrost_fmt1 quad_1
= {
386 .tag
= BIFROST_FMT1_FINAL
,
387 .header
= bi_pack_header(clause
, next
),
389 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
390 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
393 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
397 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
399 /* Try the next clause in this block */
400 if (clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
401 return list_first_entry(&(clause
->link
), bi_clause
, link
);
403 /* Try the next block, or the one after that if it's empty, etc .*/
404 pan_block
*next_block
= pan_next_block(block
);
406 bi_foreach_block_from(ctx
, next_block
, block
) {
407 bi_block
*blk
= (bi_block
*) block
;
409 if (!list_is_empty(&blk
->clauses
))
410 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
417 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
419 util_dynarray_init(emission
, NULL
);
421 bi_foreach_block(ctx
, _block
) {
422 bi_block
*block
= (bi_block
*) _block
;
424 bi_foreach_clause_in_block(block
, clause
) {
425 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
426 bi_pack_clause(ctx
, clause
, next
, emission
);