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 /* This file contains the final passes of the compiler. Running after
27 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
28 * bits on the wire (as well as fixup branches) */
31 bi_pack_header(bi_clause
*clause
, bi_clause
*next
)
33 struct bifrost_header header
= {
35 .no_end_of_shader
= (next
!= NULL
),
39 memcpy(&u
, &header
, sizeof(header
));
43 /* Represents the assignment of ports for a given bundle */
46 /* Register to assign to each port */
49 /* Read ports can be disabled */
52 /* Should we write FMA? what about ADD? If only a single port is
53 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
54 bool write_fma
, write_add
;
56 /* Should we read with port 3? */
59 /* Packed uniform/constant */
60 unsigned uniform_constant
;
62 /* Whether writes are actually for the last instruction */
63 bool first_instruction
;
66 /* Determines the register control field, ignoring the first? flag */
68 static enum bifrost_reg_control
69 bi_pack_register_ctrl_lo(struct bi_registers r
)
73 assert(!r
.read_port3
);
74 return BIFROST_WRITE_ADD_P2_FMA_P3
;
77 return BIFROST_WRITE_FMA_P2_READ_P3
;
79 return BIFROST_WRITE_FMA_P2
;
81 } else if (r
.write_add
) {
83 return BIFROST_WRITE_ADD_P2_READ_P3
;
85 return BIFROST_WRITE_ADD_P2
;
86 } else if (r
.read_port3
)
87 return BIFROST_READ_P3
;
89 return BIFROST_REG_NONE
;
92 /* Ditto but account for the first? flag this time */
94 static enum bifrost_reg_control
95 bi_pack_register_ctrl(struct bi_registers r
)
97 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl_lo(r
);
99 if (r
.first_instruction
) {
100 if (ctrl
== BIFROST_REG_NONE
)
101 ctrl
= BIFROST_FIRST_NONE
;
103 ctrl
|= BIFROST_FIRST_NONE
;
110 bi_pack_registers(struct bi_registers regs
)
112 enum bifrost_reg_control ctrl
= bi_pack_register_ctrl(regs
);
113 struct bifrost_regs s
;
116 if (regs
.enabled
[1]) {
117 /* Gotta save that bit!~ Required by the 63-x trick */
118 assert(regs
.port
[1] > regs
.port
[0]);
119 assert(regs
.enabled
[0]);
121 /* Do the 63-x trick, see docs/disasm */
122 if (regs
.port
[0] > 31) {
123 regs
.port
[0] = 63 - regs
.port
[0];
124 regs
.port
[1] = 63 - regs
.port
[1];
127 assert(regs
.port
[0] <= 31);
128 assert(regs
.port
[1] <= 63);
131 s
.reg1
= regs
.port
[1];
132 s
.reg0
= regs
.port
[0];
134 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
137 if (regs
.enabled
[0]) {
138 /* Bit 0 upper bit of port 0 */
139 s
.reg1
|= (regs
.port
[0] >> 5);
141 /* Rest of port 0 in usual spot */
142 s
.reg0
= (regs
.port
[0] & 0b11111);
144 /* Bit 1 set if port 0 also disabled */
149 s
.reg3
= regs
.port
[3];
150 s
.reg2
= regs
.port
[2];
151 s
.uniform_const
= regs
.uniform_constant
;
153 memcpy(&packed
, &s
, sizeof(s
));
158 bi_pack_fma(bi_clause
*clause
, bi_bundle bundle
)
161 return BIFROST_FMA_NOP
;
165 bi_pack_add(bi_clause
*clause
, bi_bundle bundle
)
168 return BIFROST_ADD_NOP
;
171 struct bi_packed_bundle
{
176 static struct bi_packed_bundle
177 bi_pack_bundle(bi_clause
*clause
, bi_bundle bundle
)
179 struct bi_registers regs
= {
180 .enabled
= { true, true },
181 .port
= { 34, 35, 3, 4 },
184 .first_instruction
= true,
187 uint64_t reg
= bi_pack_registers(regs
);
188 uint64_t fma
= bi_pack_fma(clause
, bundle
);
189 uint64_t add
= bi_pack_add(clause
, bundle
);
191 struct bi_packed_bundle packed
= {
192 .lo
= reg
| (fma
<< 35) | ((add
& 0b111111) << 58),
200 bi_pack_clause(bi_context
*ctx
, bi_clause
*clause
, bi_clause
*next
,
201 struct util_dynarray
*emission
)
203 struct bi_packed_bundle ins_1
= bi_pack_bundle(clause
, clause
->bundles
[0]);
204 assert(clause
->bundle_count
== 1);
206 struct bifrost_fmt1 quad_1
= {
207 .tag
= BIFROST_FMT1_FINAL
,
208 .header
= bi_pack_header(clause
, next
),
210 .ins_2
= ins_1
.hi
& ((1 << 11) - 1),
211 .ins_0
= (ins_1
.hi
>> 11) & 0b111,
214 util_dynarray_append(emission
, struct bifrost_fmt1
, quad_1
);
218 bi_next_clause(bi_context
*ctx
, pan_block
*block
, bi_clause
*clause
)
220 /* Try the next clause in this block */
221 if (clause
->link
.next
!= &((bi_block
*) block
)->clauses
)
222 return list_first_entry(&(clause
->link
), bi_clause
, link
);
224 /* Try the next block, or the one after that if it's empty, etc .*/
225 pan_block
*next_block
= pan_next_block(block
);
227 bi_foreach_block_from(ctx
, next_block
, block
) {
228 bi_block
*blk
= (bi_block
*) block
;
230 if (!list_is_empty(&blk
->clauses
))
231 return list_first_entry(&(blk
->clauses
), bi_clause
, link
);
238 bi_pack(bi_context
*ctx
, struct util_dynarray
*emission
)
240 util_dynarray_init(emission
, NULL
);
242 bi_foreach_block(ctx
, _block
) {
243 bi_block
*block
= (bi_block
*) _block
;
245 bi_foreach_clause_in_block(block
, clause
) {
246 bi_clause
*next
= bi_next_clause(ctx
, _block
, clause
);
247 bi_pack_clause(ctx
, clause
, next
, emission
);