Debian Codegraph @

src/panfrost/compiler/bifrost/bir.c

raw
1 /*
2 * Copyright (C) 2020 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "compiler.h"
8
9 bool
10 bi_has_arg(const bi_instr *ins, bi_index arg)
11 {
12 if (!ins)
13 return false;
14
15 bi_foreach_src(ins, s) {
16 if (bi_is_equiv(ins->src[s], arg))
17 return true;
18 }
19
20 return false;
21 }
22
23 /* Precondition: valid 16-bit or 32-bit register format. Returns whether it is
24 * 32-bit. Note auto reads to 32-bit registers even if the memory format is
25 * 16-bit, so is considered as such here */
26
27 bool
28 bi_is_regfmt_16(enum bi_register_format fmt)
29 {
30 switch (fmt) {
31 case BI_REGISTER_FORMAT_F16:
32 case BI_REGISTER_FORMAT_S16:
33 case BI_REGISTER_FORMAT_U16:
34 return true;
35 case BI_REGISTER_FORMAT_F32:
36 case BI_REGISTER_FORMAT_S32:
37 case BI_REGISTER_FORMAT_U32:
38 case BI_REGISTER_FORMAT_AUTO:
39 return false;
40 default:
41 UNREACHABLE("Invalid register format");
42 }
43 }
44
45 static unsigned
46 bi_count_staging_registers(const bi_instr *ins)
47 {
48 enum bi_sr_count count = bi_get_opcode_props(ins)->sr_count;
49 unsigned vecsize = ins->vecsize + 1; /* XXX: off-by-one */
50
51 switch (count) {
52 case BI_SR_COUNT_0 ... BI_SR_COUNT_4:
53 return count;
54 case BI_SR_COUNT_FORMAT:
55 return bi_is_regfmt_16(ins->register_format) ? DIV_ROUND_UP(vecsize, 2)
56 : vecsize;
57 case BI_SR_COUNT_VECSIZE:
58 return vecsize;
59 case BI_SR_COUNT_SR_COUNT:
60 return ins->sr_count;
61 }
62
63 UNREACHABLE("Invalid sr_count");
64 }
65
66 unsigned
67 bi_count_read_registers(const bi_instr *ins, unsigned s)
68 {
69 /* ATOM reads 1 but writes 2. Exception for ACMPXCHG */
70 if (s == 0 && ins->op == BI_OPCODE_ATOM_RETURN_I32)
71 return (ins->atom_opc == BI_ATOM_OPC_ACMPXCHG) ? 2 : 1;
72 else if (s == 0 && ins->op == BI_OPCODE_ATOM_RETURN_I64)
73 return (ins->atom_opc == BI_ATOM_OPC_ACMPXCHG) ? 4 : 2;
74 else if (s == 0 && bi_get_opcode_props(ins)->sr_read)
75 return bi_count_staging_registers(ins);
76 else if (s == 4 && ins->op == BI_OPCODE_BLEND)
77 return ins->sr_count_2; /* Dual source blending */
78 else if (s == 0 && ins->op == BI_OPCODE_SPLIT_I32)
79 return ins->nr_dests;
80 else if (ins->op == BI_OPCODE_SHADDX_S64 || ins->op == BI_OPCODE_SHADDX_U64)
81 return 2;
82 else
83 return 1;
84 }
85
86 unsigned
87 bi_count_write_registers(const bi_instr *ins, unsigned d)
88 {
89 if (d == 0 && bi_get_opcode_props(ins)->sr_write) {
90 switch (ins->op) {
91 case BI_OPCODE_TEXC:
92 case BI_OPCODE_TEXC_DUAL:
93 if (ins->sr_count_2)
94 return ins->sr_count;
95 else
96 return bi_is_regfmt_16(ins->register_format) ? 2 : 4;
97
98 case BI_OPCODE_TEX_SINGLE:
99 case BI_OPCODE_TEX_FETCH:
100 case BI_OPCODE_TEX_GRADIENT:
101 case BI_OPCODE_TEX_GATHER: {
102 unsigned chans = util_bitcount(ins->write_mask);
103
104 return bi_is_regfmt_16(ins->register_format) ? DIV_ROUND_UP(chans, 2)
105 : chans;
106 }
107
108 case BI_OPCODE_ACMPXCHG_I32:
109 /* Reads 2 but writes 1 */
110 return 1;
111 case BI_OPCODE_ACMPXCHG_I64:
112 /* Reads 4 but writes 2 */
113 return 2;
114 case BI_OPCODE_ATOM_POST_I32:
115 /* Reads 2 but writes 1 */
116 return 1;
117 case BI_OPCODE_ATOM_POST_I64:
118 /* Reads 4 but writes 2 */
119 return 2;
120
121 case BI_OPCODE_ATOM1_RETURN_I32:
122 case BI_OPCODE_ATOM1_RETURN_I64:
123 /* Allow omitting the destination for plain ATOM1 */
124 return bi_is_null(ins->dest[0]) ? 0 : ins->sr_count;
125 default:
126 return bi_count_staging_registers(ins);
127 }
128 } else if (ins->op == BI_OPCODE_SEG_ADD_I64 ||
129 ins->op == BI_OPCODE_SHADDX_S64 ||
130 ins->op == BI_OPCODE_SHADDX_U64) {
131 return 2;
132 } else if (ins->op == BI_OPCODE_TEXC_DUAL && d == 1) {
133 return ins->sr_count_2;
134 } else if (ins->op == BI_OPCODE_COLLECT_I32 && d == 0) {
135 return ins->nr_srcs;
136 }
137
138 return 1;
139 }
140
141 unsigned
142 bi_writemask(const bi_instr *ins, unsigned d)
143 {
144 unsigned mask = BITFIELD_MASK(bi_count_write_registers(ins, d));
145 unsigned shift = ins->dest[d].offset;
146 return (mask << shift);
147 }
148
149 bi_clause *
150 bi_next_clause(bi_context *ctx, bi_block *block, bi_clause *clause)
151 {
152 if (!block && !clause)
153 return NULL;
154
155 /* Try the first clause in this block if we're starting from scratch */
156 if (!clause && !list_is_empty(&block->clauses))
157 return list_first_entry(&block->clauses, bi_clause, link);
158
159 /* Try the next clause in this block */
160 if (clause && clause->link.next != &block->clauses)
161 return list_first_entry(&(clause->link), bi_clause, link);
162
163 /* Try the next block, or the one after that if it's empty, etc .*/
164 bi_block *next_block = bi_next_block(block);
165
166 bi_foreach_block_from(ctx, next_block, block) {
167 if (!list_is_empty(&block->clauses))
168 return list_first_entry(&block->clauses, bi_clause, link);
169 }
170
171 return NULL;
172 }
173
174 /* Does an instruction have a side effect not captured by its register
175 * destination? Applies to certain message-passing instructions, +DISCARD, and
176 * branching only, used in dead code elimation. Branches are characterized by
177 * `last` which applies to them and some atomics, +BARRIER, +BLEND which
178 * implies no loss of generality */
179
180 bool
181 bi_side_effects(const bi_instr *I)
182 {
183 if (bi_get_opcode_props(I)->last)
184 return true;
185
186 if (bi_is_scheduling_barrier(I))
187 return true;
188
189 switch (I->op) {
190 case BI_OPCODE_DISCARD_F32:
191 case BI_OPCODE_DISCARD_B32:
192 return true;
193 default:
194 break;
195 }
196
197 switch (bi_get_opcode_props(I)->message) {
198 case BIFROST_MESSAGE_NONE:
199 case BIFROST_MESSAGE_VARYING:
200 case BIFROST_MESSAGE_ATTRIBUTE:
201 case BIFROST_MESSAGE_TEX:
202 case BIFROST_MESSAGE_VARTEX:
203 case BIFROST_MESSAGE_LOAD:
204 case BIFROST_MESSAGE_64BIT:
205 return false;
206
207 case BIFROST_MESSAGE_STORE:
208 case BIFROST_MESSAGE_ATOMIC:
209 case BIFROST_MESSAGE_BARRIER:
210 case BIFROST_MESSAGE_BLEND:
211 case BIFROST_MESSAGE_Z_STENCIL:
212 case BIFROST_MESSAGE_ATEST:
213 case BIFROST_MESSAGE_JOB:
214 return true;
215
216 case BIFROST_MESSAGE_TILE:
217 return (I->op != BI_OPCODE_LD_TILE);
218 }
219
220 UNREACHABLE("Invalid message type");
221 }
222
223 /* Branch reconvergence is required when the execution mask may change
224 * between adjacent instructions (clauses). This occurs for conditional
225 * branches and for the last instruction (clause) in a block whose
226 * fallthrough successor has multiple predecessors.
227 */
228
229 bool
230 bi_reconverge_branches(bi_block *block)
231 {
232 if (bi_num_successors(block) == 1)
233 return bi_num_predecessors(block->successors[0]) > 1;
234 else
235 return true;
236 }
237
238 /*
239 * When MUX.i32 or MUX.v2i16 is used to multiplex entire sources, they can be
240 * replaced by CSEL as follows:
241 *
242 * MUX.neg(x, y, b) -> CSEL.s.lt(b, 0, x, y)
243 * MUX.int_zero(x, y, b) -> CSEL.i.eq(b, 0, x, y)
244 * MUX.fp_zero(x, y, b) -> CSEL.f.eq(b, 0, x, y)
245 *
246 * MUX.bit cannot be transformed like this.
247 *
248 * Note that MUX.v2i16 has partial support for swizzles, which CSEL.v2i16 lacks.
249 * So we must check the swizzles too.
250 */
251 bool
252 bi_can_replace_with_csel(bi_instr *I)
253 {
254 return ((I->op == BI_OPCODE_MUX_I32) || (I->op == BI_OPCODE_MUX_V2I16)) &&
255 (I->mux != BI_MUX_BIT) && (I->src[0].swizzle == BI_SWIZZLE_H01) &&
256 (I->src[1].swizzle == BI_SWIZZLE_H01) &&
257 (I->src[2].swizzle == BI_SWIZZLE_H01);
258 }
259
260 static enum bi_opcode
261 bi_csel_for_mux(bool must_sign, bool b32, enum bi_mux mux)
262 {
263 switch (mux) {
264 case BI_MUX_INT_ZERO:
265 if (must_sign)
266 return b32 ? BI_OPCODE_CSEL_U32 : BI_OPCODE_CSEL_V2U16;
267 else
268 return b32 ? BI_OPCODE_CSEL_I32 : BI_OPCODE_CSEL_V2I16;
269 case BI_MUX_NEG:
270 return b32 ? BI_OPCODE_CSEL_S32 : BI_OPCODE_CSEL_V2S16;
271 case BI_MUX_FP_ZERO:
272 return b32 ? BI_OPCODE_CSEL_F32 : BI_OPCODE_CSEL_V2F16;
273 default:
274 UNREACHABLE("No CSEL for MUX.bit");
275 }
276 }
277
278 bi_instr *
279 bi_csel_from_mux(bi_builder *b, const bi_instr *I, bool must_sign)
280 {
281 assert(I->op == BI_OPCODE_MUX_I32 || I->op == BI_OPCODE_MUX_V2I16);
282
283 /* Build a new CSEL */
284 enum bi_cmpf cmpf = (I->mux == BI_MUX_NEG) ? BI_CMPF_LT : BI_CMPF_EQ;
285 bi_instr *csel = bi_csel_u32_to(b, I->dest[0], I->src[2], bi_zero(),
286 I->src[0], I->src[1], cmpf);
287
288 /* Fixup the opcode and use it */
289 bi_set_opcode(csel,
290 bi_csel_for_mux(must_sign, I->op == BI_OPCODE_MUX_I32, I->mux));
291 return csel;
292 }