Debian Codegraph @

src/panfrost/compiler/bifrost/bi_opt_mod_props.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * Copyright (C) 2021 Alyssa Rosenzweig
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "bi_builder.h"
8 #include "compiler.h"
9
10 /*
11 * Due to a Bifrost encoding restriction, some instructions cannot have an abs
12 * modifier on both sources. Check if adding a fabs modifier to a given source
13 * of a binary instruction would cause this restriction to be hit.
14 */
15 static bool
16 bi_would_impact_abs(unsigned arch, bi_instr *I, bi_index repl, unsigned s)
17 {
18 return (arch <= 8) && I->src[1 - s].abs &&
19 bi_is_word_equiv(I->src[1 - s], repl);
20 }
21
22 static bool
23 bi_takes_fabs(unsigned arch, bi_instr *I, bi_index repl, unsigned s)
24 {
25 switch (I->op) {
26 case BI_OPCODE_FCMP_V2F16:
27 case BI_OPCODE_FMAX_V2F16:
28 case BI_OPCODE_FMIN_V2F16:
29 return !bi_would_impact_abs(arch, I, repl, s);
30 case BI_OPCODE_FADD_V2F16:
31 /*
32 * For FADD.v2f16, the FMA pipe has the abs encoding hazard,
33 * while the FADD pipe cannot encode a clamp. Either case in
34 * isolation can be worked around in the scheduler, but both
35 * together is impossible to encode. Avoid the hazard.
36 */
37 return !(I->clamp && bi_would_impact_abs(arch, I, repl, s));
38 case BI_OPCODE_V2F32_TO_V2F16:
39 /* TODO: Needs both match or lower */
40 return false;
41 case BI_OPCODE_FLOG_TABLE_F32:
42 /* TODO: Need to check mode */
43 return false;
44 default:
45 return bi_get_opcode_props(I)->abs & BITFIELD_BIT(s);
46 }
47 }
48
49 static bool
50 bi_takes_fneg(unsigned arch, bi_instr *I, unsigned s)
51 {
52 switch (I->op) {
53 case BI_OPCODE_CUBE_SSEL:
54 case BI_OPCODE_CUBE_TSEL:
55 case BI_OPCODE_CUBEFACE:
56 /* TODO: Bifrost encoding restriction: need to match or lower */
57 return arch >= 9;
58 case BI_OPCODE_FREXPE_F32:
59 case BI_OPCODE_FREXPE_V2F16:
60 case BI_OPCODE_FLOG_TABLE_F32:
61 /* TODO: Need to check mode */
62 return false;
63 default:
64 return bi_get_opcode_props(I)->neg & BITFIELD_BIT(s);
65 }
66 }
67
68 static bool
69 bi_is_fabsneg(enum bi_opcode op, enum bi_size size)
70 {
71 return (size == BI_SIZE_32 && op == BI_OPCODE_FABSNEG_F32) ||
72 (size == BI_SIZE_16 && op == BI_OPCODE_FABSNEG_V2F16);
73 }
74
75 static enum bi_swizzle
76 bi_compose_swizzle_16(enum bi_swizzle a, enum bi_swizzle b)
77 {
78 assert(a <= BI_SWIZZLE_H11);
79 assert(b <= BI_SWIZZLE_H11);
80
81 bool al = (a & BI_SWIZZLE_H10);
82 bool ar = (a & BI_SWIZZLE_H01);
83 bool bl = (b & BI_SWIZZLE_H10);
84 bool br = (b & BI_SWIZZLE_H01);
85
86 return ((al ? br : bl) ? BI_SWIZZLE_H10 : 0) |
87 ((ar ? br : bl) ? BI_SWIZZLE_H01 : 0);
88 }
89
90 /* Like bi_replace_index, but composes instead of overwrites */
91
92 static inline bi_index
93 bi_compose_float_index(bi_index old, bi_index repl)
94 {
95 /* abs(-x) = abs(+x) so ignore repl.neg if old.abs is set, otherwise
96 * -(-x) = x but -(+x) = +(-x) so need to exclusive-or the negates */
97 repl.neg = old.neg ^ (repl.neg && !old.abs);
98
99 /* +/- abs(+/- abs(x)) = +/- abs(x), etc so just or the two */
100 repl.abs |= old.abs;
101
102 /* Use the old swizzle to select from the replacement swizzle */
103 repl.swizzle = bi_compose_swizzle_16(old.swizzle, repl.swizzle);
104
105 return repl;
106 }
107
108 /* DISCARD.b32(FCMP.f(x, y)) --> DISCARD.f(x, y) */
109
110 static inline bool
111 bi_fuse_discard_fcmp(bi_context *ctx, bi_instr *I, bi_instr *mod)
112 {
113 if (!mod)
114 return false;
115 if (I->op != BI_OPCODE_DISCARD_B32)
116 return false;
117 if (mod->op != BI_OPCODE_FCMP_F32 && mod->op != BI_OPCODE_FCMP_V2F16)
118 return false;
119 if (mod->cmpf >= BI_CMPF_GTLT)
120 return false;
121
122 /* result_type doesn't matter */
123
124 /* .abs and .neg modifiers allowed on Valhall DISCARD but not Bifrost */
125 bool absneg = mod->src[0].neg || mod->src[0].abs;
126 absneg |= mod->src[1].neg || mod->src[1].abs;
127
128 if (ctx->arch <= 8 && absneg)
129 return false;
130
131 enum bi_swizzle r = I->src[0].swizzle;
132
133 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
134 I = bi_discard_f32(&b, mod->src[0], mod->src[1], mod->cmpf);
135
136 if (mod->op == BI_OPCODE_FCMP_V2F16) {
137 I->src[0].swizzle = bi_compose_swizzle_16(r, I->src[0].swizzle);
138 I->src[1].swizzle = bi_compose_swizzle_16(r, I->src[1].swizzle);
139 }
140
141 return true;
142 }
143
144 /*
145 * S32_TO_F32(S8_TO_S32(x)) -> S8_TO_F32 and friends. Round modes don't matter
146 * because all 8-bit and 16-bit integers may be represented exactly as fp32.
147 */
148 struct {
149 enum bi_opcode inner;
150 enum bi_opcode outer;
151 enum bi_opcode small_replacement;
152 enum bi_opcode wide_replacement;
153 } bi_small_int_patterns[] = {
154 {BI_OPCODE_S8_TO_S32, BI_OPCODE_S32_TO_F32, BI_OPCODE_S8_TO_F32, BI_OPCODE_S32_TO_F32},
155 {BI_OPCODE_U8_TO_U32, BI_OPCODE_U32_TO_F32, BI_OPCODE_U8_TO_F32, BI_OPCODE_U32_TO_F32},
156 {BI_OPCODE_U8_TO_U32, BI_OPCODE_S32_TO_F32, BI_OPCODE_U8_TO_F32, BI_OPCODE_U32_TO_F32},
157 {BI_OPCODE_S16_TO_S32, BI_OPCODE_S32_TO_F32, BI_OPCODE_S16_TO_F32, BI_OPCODE_S32_TO_F32},
158 {BI_OPCODE_U16_TO_U32, BI_OPCODE_U32_TO_F32, BI_OPCODE_U16_TO_F32, BI_OPCODE_U32_TO_F32},
159 {BI_OPCODE_U16_TO_U32, BI_OPCODE_S32_TO_F32, BI_OPCODE_U16_TO_F32, BI_OPCODE_U32_TO_F32},
160 };
161
162 static inline void
163 bi_fuse_small_int_to_f32(bi_context *ctx, bi_instr *I, bi_instr *mod)
164 {
165 for (unsigned i = 0; i < ARRAY_SIZE(bi_small_int_patterns); ++i) {
166 if (I->op != bi_small_int_patterns[i].outer)
167 continue;
168 if (mod->op != bi_small_int_patterns[i].inner)
169 continue;
170
171 assert(I->src[0].swizzle == BI_SWIZZLE_H01);
172 I->src[0] = mod->src[0];
173 I->round = BI_ROUND_NONE;
174
175 /* On v11, All small int instructions are gone but we now can widen instead */
176 if (ctx->arch >= 11)
177 bi_set_opcode(I, bi_small_int_patterns[i].wide_replacement);
178 else
179 bi_set_opcode(I, bi_small_int_patterns[i].small_replacement);
180 }
181 }
182
183 void
184 bi_opt_mod_prop_forward(bi_context *ctx)
185 {
186 bi_instr **lut = calloc(sizeof(bi_instr *), ctx->ssa_alloc);
187
188 bi_foreach_instr_global_safe(ctx, I) {
189 /* Try fusing FCMP into DISCARD.b32, building a new DISCARD.f32
190 * instruction. As this is the only optimization DISCARD is
191 * involved in, this shortcircuits other processing.
192 */
193 if (I->op == BI_OPCODE_DISCARD_B32) {
194 if (bi_is_ssa(I->src[0]) &&
195 bi_fuse_discard_fcmp(ctx, I, lut[I->src[0].value])) {
196 bi_remove_instruction(I);
197 }
198
199 continue;
200 }
201
202 bi_foreach_dest(I, d) {
203 lut[I->dest[d].value] = I;
204 }
205
206 bi_foreach_ssa_src(I, s) {
207 bi_instr *mod = lut[I->src[s].value];
208
209 if (!mod)
210 continue;
211
212 unsigned size = bi_get_opcode_props(I)->size;
213
214 bi_fuse_small_int_to_f32(ctx, I, mod);
215
216 if (bi_is_fabsneg(mod->op, size)) {
217 if (mod->src[0].abs && !bi_takes_fabs(ctx->arch, I, mod->src[0], s))
218 continue;
219
220 if (mod->src[0].neg && !bi_takes_fneg(ctx->arch, I, s))
221 continue;
222
223 I->src[s] = bi_compose_float_index(I->src[s], mod->src[0]);
224 }
225 }
226 }
227
228 free(lut);
229 }
230
231 /* RSCALE has restrictions on how the clamp may be used, only used for
232 * specialized transcendental sequences that set the clamp explicitly anyway */
233
234 static bool
235 bi_takes_clamp(bi_instr *I)
236 {
237 switch (I->op) {
238 case BI_OPCODE_FMA_RSCALE_F32:
239 case BI_OPCODE_FMA_RSCALE_V2F16:
240 case BI_OPCODE_FADD_RSCALE_F32:
241 return false;
242 case BI_OPCODE_FADD_V2F16:
243 /* Encoding restriction */
244 return !(I->src[0].abs && I->src[1].abs &&
245 bi_is_word_equiv(I->src[0], I->src[1]));
246 default:
247 return bi_get_opcode_props(I)->clamp;
248 }
249 }
250
251 static bool
252 bi_is_fclamp(enum bi_opcode op, enum bi_size size)
253 {
254 return (size == BI_SIZE_32 && op == BI_OPCODE_FCLAMP_F32) ||
255 (size == BI_SIZE_16 && op == BI_OPCODE_FCLAMP_V2F16);
256 }
257
258 static bool
259 bi_optimizer_clamp(bi_instr *I, bi_instr *use)
260 {
261 if (!bi_is_fclamp(use->op, bi_get_opcode_props(I)->size))
262 return false;
263 if (!bi_takes_clamp(I))
264 return false;
265
266 /* Clamps are bitfields (clamp_m1_1/clamp_0_inf) so composition is OR */
267 I->clamp |= use->clamp;
268 I->dest[0] = use->dest[0];
269 return true;
270 }
271
272 static enum bi_opcode
273 bi_sized_mux_op(unsigned size)
274 {
275 switch (size) {
276 case 8:
277 return BI_OPCODE_MUX_V4I8;
278 case 16:
279 return BI_OPCODE_MUX_V2I16;
280 case 32:
281 return BI_OPCODE_MUX_I32;
282 default:
283 UNREACHABLE("invalid size");
284 }
285 }
286
287 static bool
288 bi_is_fixed_mux(bi_instr *I, unsigned size, bi_index v1)
289 {
290 return I->op == bi_sized_mux_op(size) &&
291 bi_is_value_equiv(I->src[0], bi_zero()) &&
292 bi_is_value_equiv(I->src[1], v1);
293 }
294
295 static bool
296 bi_takes_int_result_type(enum bi_opcode op)
297 {
298 switch (op) {
299 case BI_OPCODE_ICMP_I32:
300 case BI_OPCODE_ICMP_S32:
301 case BI_OPCODE_ICMP_U32:
302 case BI_OPCODE_ICMP_V2I16:
303 case BI_OPCODE_ICMP_V2S16:
304 case BI_OPCODE_ICMP_V2U16:
305 case BI_OPCODE_ICMP_V4I8:
306 case BI_OPCODE_ICMP_V4S8:
307 case BI_OPCODE_ICMP_V4U8:
308 case BI_OPCODE_FCMP_F32:
309 case BI_OPCODE_FCMP_V2F16:
310 return true;
311 default:
312 return false;
313 }
314 }
315
316 static bool
317 bi_takes_float_result_type(enum bi_opcode op)
318 {
319 return (op == BI_OPCODE_FCMP_F32) || (op == BI_OPCODE_FCMP_V2F16);
320 }
321
322 /* CMP+MUX -> CMP with result type */
323 static bool
324 bi_optimizer_result_type(bi_instr *I, bi_instr *mux)
325 {
326 if (bi_get_opcode_props(I)->size != bi_get_opcode_props(mux)->size)
327 return false;
328
329 if (bi_is_fixed_mux(mux, 32, bi_imm_f32(1.0)) ||
330 bi_is_fixed_mux(mux, 16, bi_imm_f16(1.0))) {
331
332 if (!bi_takes_float_result_type(I->op))
333 return false;
334
335 I->result_type = BI_RESULT_TYPE_F1;
336 } else if (bi_is_fixed_mux(mux, 32, bi_imm_u32(1)) ||
337 bi_is_fixed_mux(mux, 16, bi_imm_u16(1)) ||
338 bi_is_fixed_mux(mux, 8, bi_imm_u8(1))) {
339
340 if (!bi_takes_int_result_type(I->op))
341 return false;
342
343 I->result_type = BI_RESULT_TYPE_I1;
344 } else {
345 return false;
346 }
347
348 I->dest[0] = mux->dest[0];
349 return true;
350 }
351
352 static bool
353 bi_is_var_tex(bi_instr *var, bi_instr *tex)
354 {
355 return (var->op == BI_OPCODE_LD_VAR_IMM) &&
356 (tex->op == BI_OPCODE_TEXS_2D_F16 ||
357 tex->op == BI_OPCODE_TEXS_2D_F32) &&
358 (var->register_format == BI_REGISTER_FORMAT_F32) &&
359 ((var->sample == BI_SAMPLE_CENTER &&
360 var->update == BI_UPDATE_STORE) ||
361 (var->sample == BI_SAMPLE_NONE &&
362 var->update == BI_UPDATE_RETRIEVE)) &&
363 (tex->texture_index == tex->sampler_index) &&
364 (tex->texture_index < 4) && (var->index < 8);
365 }
366
367 static bool
368 bi_optimizer_var_tex(bi_context *ctx, bi_instr *var, bi_instr *tex)
369 {
370 if (!bi_is_var_tex(var, tex))
371 return false;
372
373 /* Construct the corresponding VAR_TEX intruction */
374 bi_builder b = bi_init_builder(ctx, bi_after_instr(var));
375
376 bi_instr *I = bi_var_tex_f32_to(&b, tex->dest[0], tex->lod_mode, var->sample,
377 var->update, tex->texture_index, var->index);
378 I->skip = tex->skip;
379
380 if (tex->op == BI_OPCODE_TEXS_2D_F16)
381 bi_set_opcode(I, BI_OPCODE_VAR_TEX_F16);
382
383 /* Dead code elimination will clean up for us */
384 return true;
385 }
386
387 void
388 bi_opt_mod_prop_backward(bi_context *ctx)
389 {
390 unsigned count = ctx->ssa_alloc;
391 bi_instr **uses = calloc(count, sizeof(*uses));
392 BITSET_WORD *multiple = BITSET_CALLOC(count);
393
394 bi_foreach_block_rev(ctx, block) {
395 /* Watch out for PHI instructions in loops!
396 * PHI sources are logically read at the end of the predecessor,
397 * so process our source in successor phis first
398 */
399 bi_foreach_successor(block, succ) {
400 unsigned s = bi_predecessor_index(succ, block);
401 bi_foreach_instr_in_block(succ, phi) {
402 /* the PHIs are all at the start of the block, so stop
403 * when we see a non-PHI
404 */
405 if (phi->op != BI_OPCODE_PHI)
406 break;
407 if (phi->src[s].type == BI_INDEX_NORMAL)
408 bi_record_use(uses, multiple, phi, s);
409 }
410 }
411 /* now go through the instructions backwards */
412 bi_foreach_instr_in_block_rev(block, I) {
413 /* skip PHIs, they are handled specially */
414 if (I->op == BI_OPCODE_PHI)
415 continue;
416
417 /* record uses */
418 bi_foreach_ssa_src(I, s) {
419 bi_record_use(uses, multiple, I, s);
420 }
421
422 /* check for definitions */
423 if (I->nr_dests != 1)
424 continue;
425
426 bi_instr *use = uses[I->dest[0].value];
427
428 if (!use || BITSET_TEST(multiple, I->dest[0].value))
429 continue;
430
431 /* Destination has a single use, try to propagate */
432 bool propagated =
433 bi_optimizer_clamp(I, use) || bi_optimizer_result_type(I, use);
434
435 if (!propagated && I->op == BI_OPCODE_LD_VAR_IMM &&
436 use->op == BI_OPCODE_SPLIT_I32) {
437 /* Need to see through the split in a
438 * ld_var_imm/split/var_tex sequence
439 */
440 bi_instr *tex = uses[use->dest[0].value];
441
442 if (!tex || BITSET_TEST(multiple, use->dest[0].value))
443 continue;
444
445 use = tex;
446 propagated = bi_optimizer_var_tex(ctx, I, use);
447 }
448
449 if (propagated) {
450 bi_remove_instruction(use);
451 continue;
452 }
453 }
454 }
455
456 free(uses);
457 free(multiple);
458 }
459
460 /*
461 * Lower pseudo instructions that exist to simplify the optimizer. Returns the
462 * replacement instruction, or NULL if no replacement is needed.
463 */
464 static bool
465 bi_lower_opt_instruction_helper(bi_builder *b, bi_instr *I)
466 {
467 bi_instr *repl;
468
469 switch (I->op) {
470 case BI_OPCODE_FABSNEG_F32:
471 case BI_OPCODE_FCLAMP_F32:
472 repl = bi_fadd_f32_to(b, I->dest[0], I->src[0], bi_negzero());
473 repl->clamp = I->clamp;
474 return true;
475
476 case BI_OPCODE_FABSNEG_V2F16:
477 case BI_OPCODE_FCLAMP_V2F16:
478 repl = bi_fadd_v2f16_to(b, I->dest[0], I->src[0], bi_negzero());
479 repl->clamp = I->clamp;
480 return true;
481
482 case BI_OPCODE_DISCARD_B32:
483 bi_discard_f32(b, I->src[0], bi_zero(), BI_CMPF_NE);
484 return true;
485
486 default:
487 return false;
488 }
489 }
490
491 void
492 bi_lower_opt_instructions(bi_context *ctx)
493 {
494 bi_foreach_instr_global_safe(ctx, I) {
495 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
496
497 if (bi_lower_opt_instruction_helper(&b, I))
498 bi_remove_instruction(I);
499 }
500 }