Debian Codegraph @

src/panfrost/compiler/bifrost/valhall/va_optimize.c

raw
1 /*
2 * Copyright (C) 2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "va_compiler.h"
8
9 /* Valhall specific instruction selection optimizations */
10
11 static enum bi_opcode
12 va_op_add_imm(enum bi_opcode op)
13 {
14 switch (op) {
15 case BI_OPCODE_FADD_F32:
16 return BI_OPCODE_FADD_IMM_F32;
17 case BI_OPCODE_FADD_V2F16:
18 return BI_OPCODE_FADD_IMM_V2F16;
19 case BI_OPCODE_IADD_S32:
20 case BI_OPCODE_IADD_U32:
21 return BI_OPCODE_IADD_IMM_I32;
22 case BI_OPCODE_IADD_V2S16:
23 case BI_OPCODE_IADD_V2U16:
24 return BI_OPCODE_IADD_IMM_V2I16;
25 case BI_OPCODE_IADD_V4S8:
26 case BI_OPCODE_IADD_V4U8:
27 return BI_OPCODE_IADD_IMM_V4I8;
28 default:
29 return 0;
30 }
31 }
32
33 static bool
34 va_is_add_imm(bi_instr *I, unsigned s)
35 {
36 assert(s < I->nr_srcs);
37
38 return I->src[s].swizzle == BI_SWIZZLE_H01 && !I->src[s].abs &&
39 !I->src[s].neg && !I->clamp && !I->round;
40 }
41
42 static unsigned
43 va_choose_imm(bi_instr *I)
44 {
45 for (unsigned i = 0; i < 2; ++i) {
46 if (I->src[i].type == BI_INDEX_CONSTANT)
47 return i;
48 }
49
50 return ~0;
51 }
52
53 /* Lower MOV.i32 #constant --> IADD_IMM.i32 0x0, #constant */
54 static void
55 va_lower_mov_imm(bi_instr *I)
56 {
57 assert(I->nr_srcs == 1);
58
59 if (I->src[0].type == BI_INDEX_CONSTANT) {
60 bi_set_opcode(I, BI_OPCODE_IADD_IMM_I32);
61 I->index = I->src[0].value;
62 I->src[0] = bi_zero();
63 }
64 }
65
66 void
67 va_fuse_add_imm(bi_instr *I)
68 {
69 if (I->op == BI_OPCODE_MOV_I32) {
70 va_lower_mov_imm(I);
71 return;
72 }
73
74 /* If the instruction does some conversion depending on swizzle, we should
75 * not touch it unless the swizzle is H01. */
76 if (va_op_dest_modifier_does_convert(I->op) &&
77 I->dest->swizzle != BI_SWIZZLE_H01)
78 return;
79
80 enum bi_opcode op = va_op_add_imm(I->op);
81 if (!op)
82 return;
83
84 unsigned s = va_choose_imm(I);
85 if (s > 1)
86 return;
87 if (!va_is_add_imm(I, 1 - s))
88 return;
89
90 bi_set_opcode(I, op);
91 I->index = bi_apply_swizzle(I->src[s].value, I->src[s].swizzle);
92
93 assert(!I->src[s].abs && "redundant .abs set");
94
95 /* If the constant is negated, flip the sign bit */
96 if (I->src[s].neg) {
97 if (I->op == BI_OPCODE_FADD_IMM_F32)
98 I->index ^= (1u << 31);
99 else if (I->op == BI_OPCODE_FADD_IMM_V2F16)
100 I->index ^= (1u << 31) | (1u << 15);
101 else
102 UNREACHABLE("unexpected .neg");
103 }
104
105 I->src[0] = I->src[1 - s];
106 bi_drop_srcs(I, 1);
107 }
108
109 enum va_cmp_type {
110 VA_CMP_TYPE_INVALID,
111 VA_CMP_TYPE_F,
112 VA_CMP_TYPE_S,
113 VA_CMP_TYPE_U,
114 };
115
116 static enum bi_opcode
117 va_remap_logical_to_logical_cmp(enum bi_opcode op, enum va_cmp_type type)
118 {
119 if (type == VA_CMP_TYPE_F) {
120 switch (op) {
121 case BI_OPCODE_LSHIFT_OR_I32:
122 return BI_OPCODE_FCMP_OR_F32;
123 case BI_OPCODE_LSHIFT_OR_V2I16:
124 return BI_OPCODE_FCMP_OR_V2F16;
125 case BI_OPCODE_LSHIFT_AND_I32:
126 return BI_OPCODE_FCMP_AND_F32;
127 case BI_OPCODE_LSHIFT_AND_V2I16:
128 return BI_OPCODE_FCMP_AND_V2F16;
129 default:
130 return 0;
131 }
132 } else if (type == VA_CMP_TYPE_S) {
133 switch (op) {
134 case BI_OPCODE_LSHIFT_OR_I32:
135 return BI_OPCODE_ICMP_OR_S32;
136 case BI_OPCODE_LSHIFT_OR_V2I16:
137 return BI_OPCODE_ICMP_OR_V2S16;
138 case BI_OPCODE_LSHIFT_OR_V4I8:
139 return BI_OPCODE_ICMP_OR_V4S8;
140 case BI_OPCODE_LSHIFT_AND_I32:
141 return BI_OPCODE_ICMP_AND_S32;
142 case BI_OPCODE_LSHIFT_AND_V2I16:
143 return BI_OPCODE_ICMP_AND_V2S16;
144 case BI_OPCODE_LSHIFT_AND_V4I8:
145 return BI_OPCODE_ICMP_AND_V4S8;
146 default:
147 return 0;
148 }
149 } else if (type == VA_CMP_TYPE_U) {
150 switch (op) {
151 case BI_OPCODE_LSHIFT_OR_I32:
152 return BI_OPCODE_ICMP_OR_U32;
153 case BI_OPCODE_LSHIFT_OR_V2I16:
154 return BI_OPCODE_ICMP_OR_V2U16;
155 case BI_OPCODE_LSHIFT_OR_V4I8:
156 return BI_OPCODE_ICMP_OR_V4U8;
157 case BI_OPCODE_LSHIFT_AND_I32:
158 return BI_OPCODE_ICMP_AND_U32;
159 case BI_OPCODE_LSHIFT_AND_V2I16:
160 return BI_OPCODE_ICMP_AND_V2U16;
161 case BI_OPCODE_LSHIFT_AND_V4I8:
162 return BI_OPCODE_ICMP_AND_V4U8;
163 default:
164 return 0;
165 }
166 }
167
168 assert(0 && "invalid va_cmp_type");
169 return 0;
170 }
171
172 static bool
173 va_cmp_can_fuse(enum bi_opcode op)
174 {
175 /* We only allow fusing with OR variants */
176 switch (op) {
177 case BI_OPCODE_FCMP_OR_F32:
178 case BI_OPCODE_FCMP_OR_V2F16:
179 case BI_OPCODE_ICMP_OR_S32:
180 case BI_OPCODE_ICMP_OR_V2S16:
181 case BI_OPCODE_ICMP_OR_V4S8:
182 case BI_OPCODE_ICMP_OR_U32:
183 case BI_OPCODE_ICMP_OR_V2U16:
184 case BI_OPCODE_ICMP_OR_V4U8:
185 return true;
186 default:
187 return false;
188 }
189 }
190
191 static enum va_cmp_type
192 va_cmp_opcode_to_cmp_type(enum bi_opcode op)
193 {
194 switch (op) {
195 case BI_OPCODE_FCMP_AND_F32:
196 case BI_OPCODE_FCMP_AND_V2F16:
197 case BI_OPCODE_FCMP_OR_F32:
198 case BI_OPCODE_FCMP_OR_V2F16:
199 return VA_CMP_TYPE_F;
200 case BI_OPCODE_ICMP_AND_S32:
201 case BI_OPCODE_ICMP_AND_V2S16:
202 case BI_OPCODE_ICMP_OR_S32:
203 case BI_OPCODE_ICMP_OR_V2S16:
204 case BI_OPCODE_ICMP_OR_V4S8:
205 return VA_CMP_TYPE_S;
206 case BI_OPCODE_ICMP_AND_U32:
207 case BI_OPCODE_ICMP_AND_V2U16:
208 case BI_OPCODE_ICMP_OR_U32:
209 case BI_OPCODE_ICMP_OR_V2U16:
210 case BI_OPCODE_ICMP_OR_V4U8:
211 return VA_CMP_TYPE_U;
212 default:
213 return VA_CMP_TYPE_INVALID;
214 }
215 }
216
217 /* LSHIFT_X_F32(FCMP_OR_F32(a, b, 0), FCMP_Y_F32(c, d, e), 0) -> FCMP_X_F32(a,
218 * b, FCMP_Y_F32(c, d, e))) */
219 static bool
220 va_fuse_cmp(bi_context *ctx, bi_instr **lut, const BITSET_WORD *multiple,
221 bi_instr *I)
222 {
223 /* Expect SSA values on other sources */
224 if (I->nr_srcs != 3 || !bi_is_ssa(I->src[0]) || !bi_is_ssa(I->src[1]))
225 return false;
226
227 bi_instr *src0_ins = lut[I->src[0].value];
228 bi_instr *src1_ins = lut[I->src[1].value];
229
230 /* %1 = FCMP(a, b)
231 * %2 = LSHIFT_X(%1, %1, 0)
232 * We cannot fold that.
233 */
234 if (src0_ins == src1_ins)
235 return false;
236
237 enum va_cmp_type cmp_type = va_cmp_opcode_to_cmp_type(src0_ins->op);
238
239 /* Expect both side to use the same form type */
240 if (cmp_type == VA_CMP_TYPE_INVALID ||
241 cmp_type != va_cmp_opcode_to_cmp_type(src1_ins->op))
242 return false;
243
244 /* Expect both side to use the same result type */
245 if (src0_ins->result_type != src1_ins->result_type)
246 return false;
247
248 /* Ensure we really have a LSHIFT that we can remap (so without shift) */
249 if (!va_remap_logical_to_logical_cmp(I->op, cmp_type) ||
250 I->src[0].swizzle != BI_SWIZZLE_H01 ||
251 I->src[1].swizzle != BI_SWIZZLE_H01 ||
252 !bi_is_zero(I->src[2]))
253 return false;
254
255 bi_instr *old_ins;
256 bi_index src2;
257
258 /* Try to fuse general case of LSHIFT_X_F32(FCMP_OR_F32(a, b, 0),
259 * FCMP_Y_F32(c, d, e), 0), otherwise try to fuse LSHIFT_OR_F32(FCMP_Y_F32(c,
260 * d, e), FCMP_OR_F32(a, b, 0), 0) */
261 if (va_cmp_can_fuse(src0_ins->op) &&
262 !BITSET_TEST(multiple, src0_ins->dest[0].value) &&
263 bi_is_zero(src0_ins->src[2])) {
264 old_ins = src0_ins;
265 src2 = src1_ins->dest[0];
266 } else if ((I->op == BI_OPCODE_LSHIFT_OR_I32 ||
267 I->op == BI_OPCODE_LSHIFT_OR_V2I16) &&
268 va_cmp_can_fuse(src1_ins->op) &&
269 !BITSET_TEST(multiple, src1_ins->dest[0].value) &&
270 bi_is_zero(src1_ins->src[2])) {
271 old_ins = src1_ins;
272 src2 = src0_ins->dest[0];
273 } else {
274 return false;
275 }
276
277 /* Replace old LSHIFT logic op with the CMP with correct logical op and
278 * accumulate other src */
279 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
280 bi_instr *new_ins =
281 bi_fcmp_or_f32_to(&b, I->dest[0], old_ins->src[0], old_ins->src[1], src2,
282 old_ins->cmpf, old_ins->result_type);
283 bi_set_opcode(new_ins, va_remap_logical_to_logical_cmp(I->op, cmp_type));
284
285 /* Remove the old instructions */
286 lut[old_ins->dest[0].value] = NULL;
287 lut[new_ins->dest[0].value] = new_ins;
288 bi_remove_instruction(old_ins);
289 bi_remove_instruction(I);
290 return true;
291 }
292
293 static bool
294 va_propagate_replicate_wide(bi_context *ctx, bi_instr **lut, bi_instr *I)
295 {
296 struct va_opcode_info info = valhall_opcodes[I->op];
297 bool progress = false;
298
299 bi_foreach_ssa_src(I, s) {
300 if (!info.srcs[s].widen)
301 continue;
302
303 bi_index *src = &I->src[s];
304 bi_instr *src_ins = lut[src->value];
305
306 assert(src_ins && "src has no corresponding instruction");
307
308 bi_index new_src = bi_null();
309 unsigned tmp[4];
310
311 /* If we have a MKVEC.v2i8 and current instruction only replicate, we
312 * should propagate */
313 if (src_ins->op == BI_OPCODE_MKVEC_V2I8 &&
314 bi_swizzle_replicates_8(src->swizzle)) {
315 bi_swizzle_to_byte_channels(src->swizzle, tmp);
316 unsigned byte_idx = *tmp;
317
318 /* In case of the top 16-bit, src2 contains the value we want without
319 * any swizzles */
320 if (byte_idx >= 2) {
321 /* src2 should not have non identity swizzle */
322 assert(src_ins->src[2].swizzle == BI_SWIZZLE_H01);
323
324 new_src = src_ins->src[2];
325 new_src.swizzle = BI_SWIZZLE_B0 + (byte_idx - 2);
326 } else {
327 new_src = src_ins->src[byte_idx];
328 }
329 }
330 /* In case of 16-bit source, attempt to propagate trivial conversions from
331 8-bit */
332 else if (bi_swizzle_replicates_16(src->swizzle) &&
333 !bi_swizzle_replicates_8(src->swizzle) &&
334 ((src_ins->op == BI_OPCODE_V2S8_TO_V2S16 && info.is_signed) ||
335 (src_ins->op == BI_OPCODE_V2U8_TO_V2U16 && !info.is_signed)) &&
336 bi_swizzle_replicates_8(src_ins->src[0].swizzle)) {
337 new_src = src_ins->src[0];
338 }
339
340 if (!bi_is_null(new_src)) {
341 *src = new_src;
342 progress = true;
343 }
344 }
345
346 return progress;
347 }
348
349 static void
350 va_optimize_forward(bi_context *ctx)
351 {
352 bool progress;
353
354 do {
355 progress = false;
356
357 unsigned count = ctx->ssa_alloc;
358 bi_instr **lut = rzalloc_array(ctx, bi_instr *, count);
359 bi_instr **uses = rzalloc_array(ctx, bi_instr *, count);
360 BITSET_WORD *multiple = BITSET_RZALLOC(ctx, count);
361
362 if (!lut || !uses || !multiple)
363 goto out;
364
365 /* Record usage across blocks */
366 bi_foreach_block(ctx, block) {
367 bi_foreach_instr_in_block(block, I) {
368 bi_foreach_dest(I, d) {
369 lut[I->dest[d].value] = I;
370 }
371
372 bi_foreach_ssa_src(I, s) {
373 bi_record_use(uses, multiple, I, s);
374 }
375 }
376 }
377
378 bi_foreach_instr_global_safe(ctx, I) {
379 progress |= va_propagate_replicate_wide(ctx, lut, I);
380 progress |= va_fuse_cmp(ctx, lut, multiple, I);
381 }
382
383 out:
384 ralloc_free(uses);
385 ralloc_free(lut);
386 ralloc_free(multiple);
387 } while (progress);
388 }
389
390 void
391 va_optimize(bi_context *ctx)
392 {
393 bi_foreach_instr_global(ctx, I) {
394 va_fuse_add_imm(I);
395 }
396
397 va_optimize_forward(ctx);
398 }