Debian Codegraph @

src/freedreno/ir3/ir3_legalize.c

raw symbols SCIP
1 /*
2 * Copyright © 2014 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include "util/ralloc.h"
10 #include "util/u_math.h"
11
12 #include "ir3.h"
13 #include "ir3_shader.h"
14
15 /*
16 * Legalize:
17 *
18 * The legalize pass handles ensuring sufficient nop's and sync flags for
19 * correct execution.
20 *
21 * 1) Iteratively determine where sync ((sy)/(ss)) flags are needed,
22 * based on state flowing out of predecessor blocks until there is
23 * no further change. In some cases this requires inserting nops.
24 * 2) Mark (ei) on last varying input
25 * 3) Final nop scheduling for instruction latency
26 * 4) Resolve jumps and schedule blocks, marking potential convergence
27 * points with (jp)
28 */
29
30 struct ir3_legalize_ctx {
31 struct ir3_compiler *compiler;
32 struct ir3_shader_variant *so;
33 mesa_shader_stage type;
34 int max_bary;
35 bool early_input_release;
36 bool has_inputs;
37 bool has_tex_prefetch;
38 };
39
40 struct ir3_legalize_block_data {
41 bool valid;
42 struct ir3_legalize_state begin_state;
43 struct ir3_legalize_state state;
44 };
45
46 static inline bool
47 needs_ss_war(struct ir3_legalize_state *state, struct ir3_register *dst,
48 bool is_scalar_alu, enum ir3_instruction_flags cur_flags)
49 {
50 if (regmask_get(&state->needs_ss_war, dst))
51 return true;
52 if (!(cur_flags & IR3_INSTR_SY) &&
53 regmask_get(&state->needs_ss_or_sy_war, dst)) {
54 return true;
55 }
56
57 if (!is_scalar_alu) {
58 if (regmask_get(&state->needs_ss_scalar_war, dst))
59 return true;
60 if (!(cur_flags & IR3_INSTR_SY) &&
61 regmask_get(&state->needs_ss_or_sy_scalar_war, dst)) {
62 return true;
63 }
64 }
65
66 return false;
67 }
68
69 static inline bool
70 needs_sy_war(struct ir3_legalize_state *state, struct ir3_register *dst)
71 {
72 return regmask_get(&state->needs_sy_war, dst);
73 }
74
75 enum ir3_instruction_flags
76 ir3_required_sync_flags(struct ir3_legalize_state *state,
77 struct ir3_compiler *compiler,
78 struct ir3_instruction *n)
79 {
80 if (n->opc == OPC_SHPE) {
81 return IR3_INSTR_SS | IR3_INSTR_SY;
82 }
83
84 enum ir3_instruction_flags flags = 0;
85 bool n_is_scalar_alu = is_scalar_alu(n, compiler);
86
87 if (state->force_ss) {
88 flags |= IR3_INSTR_SS;
89 }
90
91 if (state->force_sy) {
92 flags |= IR3_INSTR_SY;
93 }
94
95 /* NOTE: consider dst register too.. it could happen that
96 * texture sample instruction (for example) writes some
97 * components which are unused. A subsequent instruction
98 * that writes the same register can race w/ the sam instr
99 * resulting in undefined results:
100 */
101 for (unsigned i = 0; i < n->dsts_count + n->srcs_count; i++) {
102 struct ir3_register *reg;
103 if (i < n->dsts_count)
104 reg = n->dsts[i];
105 else
106 reg = n->srcs[i - n->dsts_count];
107
108 if (reg->flags & IR3_REG_DUMMY)
109 continue;
110
111 if (is_reg_gpr(reg)) {
112
113 /* TODO: we probably only need (ss) for alu
114 * instr consuming sfu result.. need to make
115 * some tests for both this and (sy)..
116 */
117 if (regmask_get(&state->needs_ss, reg)) {
118 flags |= IR3_INSTR_SS;
119 }
120
121 /* There is a fast feedback path for scalar ALU instructions which
122 * only takes 1 cycle of latency, similar to the normal 3 cycle
123 * latency path for ALU instructions. For this fast path the
124 * producer and consumer must use the same register size (i.e. no
125 * writing a full register and then reading half of it or vice
126 * versa). If we don't hit this path, either because of a mismatched
127 * size or a read via the regular ALU, then the write latency is
128 * variable and we must use (ss) to wait for the scalar ALU. This is
129 * different from the fixed 6 cycle latency for mismatched vector
130 * ALU accesses.
131 */
132 if (n_is_scalar_alu) {
133 /* Check if we have a mismatched size RaW dependency */
134 if (regmask_get((reg->flags & IR3_REG_HALF)
135 ? &state->needs_ss_scalar_half
136 : &state->needs_ss_scalar_full,
137 reg)) {
138 flags |= IR3_INSTR_SS;
139 }
140 } else {
141 /* check if we have a scalar -> vector RaW dependency */
142 if (regmask_get(&state->needs_ss_scalar_half, reg) ||
143 regmask_get(&state->needs_ss_scalar_full, reg)) {
144 flags |= IR3_INSTR_SS;
145 }
146 }
147
148 if (regmask_get(&state->needs_sy, reg)) {
149 flags |= IR3_INSTR_SY;
150 }
151 } else if ((reg->flags & IR3_REG_CONST)) {
152 if (reg->flags & IR3_REG_RELATIV) {
153 /* Since we don't know which const reg is accessed, add sync flags
154 * if any const reg need them.
155 */
156 if (!BITSET_IS_EMPTY(state->needs_ss_for_const)) {
157 flags |= IR3_INSTR_SS;
158 }
159 if (!BITSET_IS_EMPTY(state->needs_sy_for_const)) {
160 flags |= IR3_INSTR_SY;
161 }
162 } else {
163 if (BITSET_TEST(state->needs_ss_for_const, reg->num)) {
164 flags |= IR3_INSTR_SS;
165 }
166 if (BITSET_TEST(state->needs_sy_for_const, reg->num)) {
167 flags |= IR3_INSTR_SY;
168 }
169 }
170 } else if (!(reg->flags & (IR3_REG_IMMED | IR3_REG_RT))) {
171 if (regmask_get(&state->needs_ss, reg)) {
172 flags |= IR3_INSTR_SS;
173 }
174 }
175 }
176
177 foreach_dst (reg, n) {
178 if (reg->flags & (IR3_REG_RT | IR3_REG_DUMMY))
179 continue;
180 if (needs_sy_war(state, reg)) {
181 flags |= IR3_INSTR_SY;
182 }
183 if (needs_ss_war(state, reg, n_is_scalar_alu, flags)) {
184 flags |= IR3_INSTR_SS;
185 }
186 }
187
188 return flags;
189 }
190
191 static inline void
192 apply_ss(struct ir3_legalize_state *state, bool mergedregs)
193 {
194 regmask_init(&state->needs_ss_war, mergedregs);
195 regmask_init(&state->needs_ss_or_sy_war, mergedregs);
196 regmask_init(&state->needs_ss, mergedregs);
197 regmask_init(&state->needs_ss_scalar_war, mergedregs);
198 regmask_init(&state->needs_ss_or_sy_scalar_war, mergedregs);
199 regmask_init(&state->needs_ss_scalar_full, mergedregs);
200 regmask_init(&state->needs_ss_scalar_half, mergedregs);
201 BITSET_ZERO(state->needs_ss_for_const);
202 state->force_ss = false;
203 }
204
205 static inline void
206 apply_sy(struct ir3_legalize_state *state, bool mergedregs)
207 {
208 regmask_init(&state->needs_sy, mergedregs);
209 regmask_init(&state->needs_sy_war, mergedregs);
210 regmask_init(&state->needs_ss_or_sy_war, mergedregs);
211 regmask_init(&state->needs_ss_or_sy_scalar_war, mergedregs);
212 BITSET_ZERO(state->needs_sy_for_const);
213 state->force_sy = false;
214 }
215
216 static void
217 sync_update(struct ir3_legalize_state *state, struct ir3_compiler *compiler,
218 struct ir3_instruction *n)
219 {
220 if (n->flags & IR3_INSTR_SS)
221 apply_ss(state, compiler->mergedregs);
222 if (n->flags & IR3_INSTR_SY)
223 apply_sy(state, compiler->mergedregs);
224
225 if (is_barrier(n)) {
226 state->force_ss = true;
227 state->force_sy = true;
228 } else {
229 state->force_ss = false;
230 state->force_sy = false;
231 }
232
233 bool n_is_scalar_alu = is_scalar_alu(n, compiler);
234
235 if (is_sfu(n) || n->opc == OPC_SHFL)
236 regmask_set(&state->needs_ss, n->dsts[0]);
237
238 foreach_dst (dst, n) {
239 if (dst->flags & IR3_REG_SHARED) {
240 if (n_is_scalar_alu) {
241 if (dst->flags & IR3_REG_HALF)
242 regmask_set(&state->needs_ss_scalar_full, dst);
243 else
244 regmask_set(&state->needs_ss_scalar_half, dst);
245 } else {
246 regmask_set(&state->needs_ss, dst);
247 }
248 } else if (reg_is_addr1(dst) && n->block->in_early_preamble) {
249 regmask_set(&state->needs_ss, dst);
250 } else if (dst->flags & IR3_REG_UNIFORM) {
251 regmask_set(&state->needs_ss, dst);
252 }
253 }
254
255 if (is_tex_or_prefetch(n) && !has_dummy_dst(n)) {
256 regmask_set(&state->needs_sy, n->dsts[0]);
257 } else if (n->opc == OPC_RESINFO && !has_dummy_dst(n)) {
258 regmask_set(&state->needs_ss, n->dsts[0]);
259 } else if (is_load(n)) {
260 if (is_local_mem_load(n))
261 regmask_set(&state->needs_ss, n->dsts[0]);
262 else
263 regmask_set(&state->needs_sy, n->dsts[0]);
264 } else if (is_atomic(n->opc)) {
265 if (is_bindless_atomic(n->opc)) {
266 regmask_set(&state->needs_sy, n->srcs[2]);
267 } else if (is_global_a3xx_atomic(n->opc) ||
268 is_global_a6xx_atomic(n->opc)) {
269 regmask_set(&state->needs_sy, n->dsts[0]);
270 } else {
271 regmask_set(&state->needs_ss, n->dsts[0]);
272 }
273 } else if (n->opc == OPC_PUSH_CONSTS_LOAD_MACRO) {
274 unsigned const_dst = n->push_consts.dst_base;
275 unsigned const_size = n->push_consts.src_size * 2;
276 BITSET_SET_COUNT(state->needs_ss_for_const, const_dst, const_size);
277 } else if (n->opc == OPC_STC) {
278 unsigned const_dst = n->cat6.dst_offset;
279 unsigned const_size = n->cat6.iim_val;
280 BITSET_SET_COUNT(state->needs_ss_for_const, const_dst, const_size);
281 } else if (n->opc == OPC_LDC_K) {
282 unsigned const_dst = n->cat6.dst_offset;
283 unsigned const_size = n->cat6.iim_val * 4;
284 BITSET_SET_COUNT(state->needs_sy_for_const, const_dst, const_size);
285 }
286
287 /* both tex/sfu appear to not always immediately consume
288 * their src register(s):
289 */
290 if (is_war_hazard_producer(n)) {
291 /* These WAR hazards can always be resolved with (ss). However, when
292 * the reader is a sy-producer, they can also be resolved using (sy)
293 * because once we have synced the reader's results using (sy), its
294 * sources have definitely been consumed. We track the two cases
295 * separately so that we don't add an unnecessary (ss) if a (sy) sync
296 * already happened.
297 * For example, this prevents adding the unnecessary (ss) in the
298 * following sequence:
299 * sam rd, rs, ...
300 * (sy)... ; sam synced so consumed its sources
301 * (ss)write rs ; (ss) unnecessary since rs has been consumed already
302 */
303 bool needs_ss = is_ss_producer(n) || is_store(n) || n->opc == OPC_STC;
304
305 /* It seems like ray_intersection WAR hazards cannot be resolved using
306 * (ss) and need a (sy) sync instead.
307 */
308 bool needs_sy = n->opc == OPC_RAY_INTERSECTION;
309
310 if (n_is_scalar_alu) {
311 /* Scalar ALU also does not immediately read its source because it
312 * is not executed right away, but scalar ALU instructions are
313 * executed in-order so subsequent scalar ALU instructions don't
314 * need to wait for previous ones.
315 */
316 regmask_t *mask = needs_ss ? &state->needs_ss_scalar_war
317 : &state->needs_ss_or_sy_scalar_war;
318
319 foreach_src (reg, n) {
320 if ((reg->flags & IR3_REG_SHARED) || is_reg_a0(reg)) {
321 regmask_set(mask, reg);
322 }
323 }
324 } else {
325 regmask_t *mask = needs_sy ? &state->needs_sy_war
326 : needs_ss ? &state->needs_ss_war
327 : &state->needs_ss_or_sy_war;
328
329 foreach_src (reg, n) {
330 if (!(reg->flags & (IR3_REG_IMMED | IR3_REG_CONST))) {
331 regmask_set(mask, reg);
332 }
333 }
334 }
335 }
336 }
337
338 void
339 ir3_init_legalize_state(struct ir3_legalize_state *state,
340 struct ir3_compiler *compiler)
341 {
342 regmask_init(&state->needs_ss_war, compiler->mergedregs);
343 regmask_init(&state->needs_sy_war, compiler->mergedregs);
344 regmask_init(&state->needs_ss_or_sy_war, compiler->mergedregs);
345 regmask_init(&state->needs_ss_scalar_war, compiler->mergedregs);
346 regmask_init(&state->needs_ss_or_sy_scalar_war, compiler->mergedregs);
347 regmask_init(&state->needs_ss_scalar_full, compiler->mergedregs);
348 regmask_init(&state->needs_ss_scalar_half, compiler->mergedregs);
349 regmask_init(&state->needs_ss, compiler->mergedregs);
350 regmask_init(&state->needs_sy, compiler->mergedregs);
351 }
352
353 static struct ir3_legalize_state *
354 get_block_legalize_state(struct ir3_block *block)
355 {
356 struct ir3_legalize_block_data *bd = block->data;
357 return &bd->state;
358 }
359
360 void
361 ir3_merge_pred_legalize_states(struct ir3_legalize_state *state,
362 struct ir3_block *block,
363 ir3_get_block_legalize_state_cb get_state)
364 {
365 /* Our input state is the OR of all predecessor blocks' state.
366 *
367 * Why don't we just zero the state at the beginning before merging in the
368 * predecessors? Because otherwise updates may not be a "lattice refinement",
369 * i.e. needs_ss may go from true to false for some register due to a (ss) we
370 * inserted the second time around (and the same for (sy)). This means that
371 * there's no solid guarantee the algorithm will converge, and in theory
372 * there may be infinite loops where we fight over the placment of an (ss).
373 */
374 for (unsigned i = 0; i < block->predecessors_count; i++) {
375 struct ir3_block *predecessor = block->predecessors[i];
376 struct ir3_legalize_state *pstate = get_state(predecessor);
377
378 if (!pstate) {
379 continue;
380 }
381
382 /* Our input (ss)/(sy) state is based on OR'ing the output
383 * state of all our predecessor blocks
384 */
385 regmask_or(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);
386 regmask_or(&state->needs_ss_war, &state->needs_ss_war,
387 &pstate->needs_ss_war);
388 regmask_or(&state->needs_sy_war, &state->needs_sy_war,
389 &pstate->needs_sy_war);
390 regmask_or(&state->needs_ss_or_sy_war, &state->needs_ss_or_sy_war,
391 &pstate->needs_ss_or_sy_war);
392 regmask_or(&state->needs_sy, &state->needs_sy, &pstate->needs_sy);
393 BITSET_OR(state->needs_ss_for_const, state->needs_ss_for_const,
394 pstate->needs_ss_for_const);
395 BITSET_OR(state->needs_sy_for_const, state->needs_sy_for_const,
396 pstate->needs_sy_for_const);
397 state->force_ss |= pstate->force_ss;
398 state->force_sy |= pstate->force_sy;
399
400 /* Our nop state is the max of the predecessor blocks. The predecessor nop
401 * state contains the cycle offset from the start of its block when each
402 * register becomes ready. But successor blocks need the cycle offset from
403 * their start, which is the predecessor's block's end. Translate the
404 * cycle offset.
405 */
406 for (unsigned i = 0; i < ARRAY_SIZE(state->pred_ready); i++)
407 state->pred_ready[i] =
408 MAX2(state->pred_ready[i],
409 MAX2(pstate->pred_ready[i], pstate->cycle) - pstate->cycle);
410 for (unsigned i = 0; i < ARRAY_SIZE(state->alu_nop.full_ready); i++) {
411 state->alu_nop.full_ready[i] = MAX2(
412 state->alu_nop.full_ready[i],
413 MAX2(pstate->alu_nop.full_ready[i], pstate->cycle) - pstate->cycle);
414 state->alu_nop.half_ready[i] = MAX2(
415 state->alu_nop.half_ready[i],
416 MAX2(pstate->alu_nop.half_ready[i], pstate->cycle) - pstate->cycle);
417 state->non_alu_nop.full_ready[i] =
418 MAX2(state->non_alu_nop.full_ready[i],
419 MAX2(pstate->non_alu_nop.full_ready[i], pstate->cycle) -
420 pstate->cycle);
421 state->non_alu_nop.half_ready[i] =
422 MAX2(state->non_alu_nop.half_ready[i],
423 MAX2(pstate->non_alu_nop.half_ready[i], pstate->cycle) -
424 pstate->cycle);
425 }
426 }
427
428 /* We need to take phsyical-only edges into account when tracking shared
429 * registers.
430 */
431 for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
432 struct ir3_block *predecessor = block->physical_predecessors[i];
433 struct ir3_legalize_state *pstate = get_state(predecessor);
434
435 if (!pstate) {
436 continue;
437 }
438
439 regmask_or_shared(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);
440 regmask_or_shared(&state->needs_ss_scalar_full,
441 &state->needs_ss_scalar_full,
442 &pstate->needs_ss_scalar_full);
443 regmask_or_shared(&state->needs_ss_scalar_half,
444 &state->needs_ss_scalar_half,
445 &pstate->needs_ss_scalar_half);
446 regmask_or_shared(&state->needs_ss_scalar_war,
447 &state->needs_ss_scalar_war,
448 &pstate->needs_ss_scalar_war);
449 regmask_or_shared(&state->needs_ss_or_sy_scalar_war,
450 &state->needs_ss_or_sy_scalar_war,
451 &pstate->needs_ss_or_sy_scalar_war);
452 }
453
454 mesa_shader_stage stage = block->shader->type;
455
456 if (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_GEOMETRY) {
457 if (block == ir3_start_block(block->shader)) {
458 state->force_ss = true;
459 state->force_sy = true;
460 }
461 }
462 }
463
464 static bool
465 count_instruction(struct ir3_instruction *n, struct ir3_compiler *compiler)
466 {
467 /* NOTE: don't count branch/jump since we don't know yet if they will
468 * be eliminated later in resolve_jumps().. really should do that
469 * earlier so we don't have this constraint.
470 */
471 return (is_alu(n) && !is_scalar_alu(n, compiler)) ||
472 (is_flow(n) && (n->opc != OPC_JUMP) && (n->opc != OPC_BR) &&
473 (n->opc != OPC_BRAA) && (n->opc != OPC_BRAO));
474 }
475
476 static unsigned *
477 get_ready_slot(struct ir3_legalize_state *state,
478 struct ir3_register *reg, unsigned num,
479 bool consumer_alu, bool matching_size)
480 {
481 if (reg->flags & IR3_REG_PREDICATE) {
482 assert(num == reg->num);
483 assert(reg_num(reg) == REG_P0);
484 return &state->pred_ready[reg_comp(reg)];
485 }
486 if (reg->num == regid(REG_A0, 0))
487 return &state->addr_ready[0];
488 if (reg->num == regid(REG_A0, 1))
489 return &state->addr_ready[1];
490 struct ir3_nop_state *nop =
491 consumer_alu ? &state->alu_nop : &state->non_alu_nop;
492 assert(!(reg->flags & IR3_REG_SHARED));
493 if (reg->flags & IR3_REG_HALF) {
494 if (matching_size) {
495 assert(num < ARRAY_SIZE(nop->half_ready));
496 return &nop->half_ready[num];
497 } else {
498 assert(num / 2 < ARRAY_SIZE(nop->full_ready));
499 return &nop->full_ready[num / 2];
500 }
501 } else {
502 if (matching_size) {
503 assert(num < ARRAY_SIZE(nop->full_ready));
504 return &nop->full_ready[num];
505 }
506 /* If "num" is large enough, then it can't alias a half-reg because only
507 * the first half of the full reg speace aliases half regs. Return NULL in
508 * this case.
509 */
510 else if (num * 2 < ARRAY_SIZE(nop->half_ready))
511 return &nop->half_ready[num * 2];
512 else
513 return NULL;
514 }
515 }
516
517 unsigned
518 ir3_required_delay(struct ir3_legalize_state *state,
519 struct ir3_compiler *compiler, struct ir3_instruction *instr)
520 {
521 /* As far as we know, shader outputs don't need any delay. */
522 if (instr->opc == OPC_END || instr->opc == OPC_CHMASK)
523 return 0;
524
525 unsigned delay = 0;
526 foreach_src_n (src, n, instr) {
527 if (src->flags & (IR3_REG_CONST | IR3_REG_IMMED | IR3_REG_SHARED))
528 continue;
529
530 unsigned elems = post_ra_reg_elems(src);
531 unsigned num = post_ra_reg_num(src);
532 unsigned src_cycle =
533 state->cycle + ir3_src_read_delay(compiler, instr, n);
534
535 for (unsigned elem = 0; elem < elems; elem++, num++) {
536 unsigned ready_cycle =
537 *get_ready_slot(state, src, num, is_alu(instr), true);
538 delay = MAX2(delay, MAX2(ready_cycle, src_cycle) - src_cycle);
539
540 /* Increment cycle for ALU instructions with (rptN) where sources are
541 * read each subsequent cycle.
542 */
543 if (instr->repeat && !(src->flags & IR3_REG_RELATIV))
544 src_cycle++;
545 }
546 }
547
548 return delay;
549 }
550
551 static void
552 delay_update(struct ir3_compiler *compiler,
553 struct ir3_legalize_state *state,
554 struct ir3_instruction *instr)
555 {
556 if (writes_addr1(instr) && instr->block->in_early_preamble)
557 return;
558
559 foreach_dst_n (dst, n, instr) {
560 if (dst->flags & (IR3_REG_RT | IR3_REG_DUMMY))
561 continue;
562
563 unsigned elems = post_ra_reg_elems(dst);
564 unsigned num = post_ra_reg_num(dst);
565 unsigned dst_cycle = state->cycle;
566
567 /* sct and swz have scalar destinations and each destination is written in
568 * a subsequent cycle.
569 */
570 if (instr->opc == OPC_SCT || instr->opc == OPC_SWZ)
571 dst_cycle += n;
572
573 /* For relative accesses with (rptN), we have no way of knowing which
574 * component is accessed when, so we have to assume the worst and mark
575 * every array member as being written at the end.
576 */
577 if (dst->flags & IR3_REG_RELATIV)
578 dst_cycle += instr->repeat;
579
580 if (dst->flags & (IR3_REG_SHARED | IR3_REG_UNIFORM))
581 continue;
582
583 for (unsigned elem = 0; elem < elems; elem++, num++) {
584 /* Don't update delays for registers that aren't actually written. */
585 if (!(dst->flags & IR3_REG_RELATIV) && !(dst->wrmask & (1 << elem)))
586 continue;
587
588 for (unsigned consumer_alu = 0; consumer_alu < 2; consumer_alu++) {
589 for (unsigned matching_size = 0; matching_size < 2; matching_size++) {
590 unsigned *ready_slot =
591 get_ready_slot(state, dst, num, consumer_alu, matching_size);
592
593 if (!ready_slot)
594 continue;
595
596 bool reset_ready_slot = false;
597 unsigned delay = 0;
598 if (!is_alu(instr)) {
599 /* Apparently writes that require (ss) or (sy) are
600 * synchronized against previous writes, so consumers don't
601 * have to wait for any previous overlapping ALU instructions
602 * to complete.
603 */
604 reset_ready_slot = true;
605 } else if ((dst->flags & IR3_REG_PREDICATE) ||
606 reg_num(dst) == REG_A0) {
607 delay = compiler->delay_slots.non_alu;
608 if (!matching_size)
609 continue;
610 } else {
611 delay = (consumer_alu && matching_size)
612 ? compiler->delay_slots.alu_to_alu
613 : compiler->delay_slots.non_alu;
614 }
615
616 if (!matching_size) {
617 for (unsigned i = 0; i < reg_elem_size(dst); i++) {
618 ready_slot[i] =
619 reset_ready_slot ? 0 :
620 MAX2(ready_slot[i], dst_cycle + delay);
621 }
622 } else {
623 *ready_slot =
624 reset_ready_slot ? 0 :
625 MAX2(*ready_slot, dst_cycle + delay);
626 }
627 }
628 }
629
630 /* Increment cycle for ALU instructions with (rptN) where destinations
631 * are written each subsequent cycle.
632 */
633 if (instr->repeat && !(dst->flags & IR3_REG_RELATIV))
634 dst_cycle++;
635 }
636 }
637 }
638
639 void
640 ir3_update_legalize_state(struct ir3_legalize_state *state,
641 struct ir3_compiler *compiler,
642 struct ir3_instruction *n)
643 {
644 sync_update(state, compiler, n);
645
646 bool count = count_instruction(n, compiler);
647 if (count)
648 state->cycle += 1;
649
650 delay_update(compiler, state, n);
651
652 if (count)
653 state->cycle += n->repeat + n->nop;
654 }
655
656 static struct ir3_instruction *
657 insert_nop_flags(struct ir3_legalize_ctx *ctx,
658 struct ir3_legalize_state *state,
659 struct ir3_instruction *last_n,
660 struct ir3_builder *build,
661 enum ir3_instruction_flags flags)
662 {
663 if (last_n && last_n->opc == OPC_NOP) {
664 /* Note that reusing the previous nop isn't just an optimization
665 * but prevents infinitely adding nops when this block is in a loop
666 * and needs to be legalized more than once.
667 */
668 last_n->flags |= flags;
669
670 /* If we reuse the last nop, we shouldn't do a full state update as
671 * its delay has already been taken into account.
672 */
673 sync_update(state, ctx->compiler, last_n);
674 } else {
675 struct ir3_instruction *nop = ir3_NOP(build);
676 nop->flags |= flags;
677 ir3_update_legalize_state(state, ctx->compiler, nop);
678 last_n = nop;
679 }
680
681 return last_n;
682 }
683
684 /* We want to evaluate each block from the position of any other
685 * predecessor block, in order that the flags set are the union of
686 * all possible program paths.
687 *
688 * To do this, we need to know the output state (needs_ss/ss_war/sy)
689 * of all predecessor blocks. The tricky thing is loops, which mean
690 * that we can't simply recursively process each predecessor block
691 * before legalizing the current block.
692 *
693 * How we handle that is by looping over all the blocks until the
694 * results converge. If the output state of a given block changes
695 * in a given pass, this means that all successor blocks are not
696 * yet fully legalized.
697 */
698
699 static bool
700 legalize_block(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
701 {
702 struct ir3_legalize_block_data *bd = block->data;
703
704 if (bd->valid)
705 return false;
706
707 struct ir3_instruction *last_n = NULL;
708 struct list_head instr_list;
709 struct ir3_legalize_state prev_state = bd->state;
710 struct ir3_legalize_state *state = &bd->begin_state;
711 bool last_input_needs_ss = false;
712 struct ir3_builder build = ir3_builder_at(ir3_after_block(block));
713
714 ir3_merge_pred_legalize_states(state, block, get_block_legalize_state);
715
716 memcpy(&bd->state, state, sizeof(*state));
717 state = &bd->state;
718
719 unsigned input_count = 0;
720
721 foreach_instr (n, &block->instr_list) {
722 if (is_input(n)) {
723 input_count++;
724 }
725 }
726
727 unsigned inputs_remaining = input_count;
728
729 /* Either inputs are in the first block or we expect inputs to be released
730 * with the end of the program.
731 */
732 assert(input_count == 0 || !ctx->early_input_release ||
733 block == ir3_after_preamble(block->shader));
734
735 /* remove all the instructions from the list, we'll be adding
736 * them back in as we go
737 */
738 list_replace(&block->instr_list, &instr_list);
739 list_inithead(&block->instr_list);
740
741 state->cycle = 0;
742
743 foreach_instr_safe (n, &instr_list) {
744 unsigned i;
745
746 n->flags &= ~(IR3_INSTR_SS | IR3_INSTR_SY);
747
748 /* _meta::tex_prefetch instructions removed later in
749 * collect_tex_prefetches()
750 */
751 if (is_meta(n) && (n->opc != OPC_META_TEX_PREFETCH))
752 continue;
753
754 if (is_input(n)) {
755 struct ir3_register *inloc = n->srcs[0];
756 assert(inloc->flags & IR3_REG_IMMED);
757
758 int last_inloc =
759 inloc->iim_val + ((inloc->flags & IR3_REG_R) ? n->repeat : 0);
760 ctx->max_bary = MAX2(ctx->max_bary, last_inloc);
761 }
762
763 bool n_is_scalar_alu = is_scalar_alu(n, ctx->compiler);
764
765 enum ir3_instruction_flags sync_flags =
766 ir3_required_sync_flags(state, ctx->compiler, n);
767 n->flags |= sync_flags;
768
769 if (sync_flags & IR3_INSTR_SS) {
770 last_input_needs_ss = false;
771 }
772
773 /* In earlypreamble we need to use mova.u/ldc.u: */
774 if (block->in_early_preamble) {
775 if (writes_addr1(n) || (n->opc == OPC_LDC))
776 n->flags |= IR3_INSTR_U;
777 }
778
779 /* Ensure no pending uGPR writes before EP ends: */
780 if ((n->opc == OPC_SHPE) && (ctx->compiler->gen >= 8) &&
781 block->in_early_preamble &&
782 regmask_get_any_shared(&state->needs_sy)) {
783 last_n = insert_nop_flags(ctx, state, last_n, &build, IR3_INSTR_SY);
784 }
785
786 /* cat5+ does not have an (ss) bit, if needed we need to
787 * insert a nop to carry the sync flag. Would be kinda
788 * clever if we were aware of this during scheduling, but
789 * this should be a pretty rare case:
790 */
791 if ((n->flags & IR3_INSTR_SS) && !supports_ss(n)) {
792 last_n = insert_nop_flags(ctx, state, last_n, &build, IR3_INSTR_SS);
793 n->flags &= ~IR3_INSTR_SS;
794 }
795
796 unsigned delay = ir3_required_delay(state, ctx->compiler, n);
797
798 /* NOTE: I think the nopN encoding works for a5xx and
799 * probably a4xx, but not a3xx. So far only tested on
800 * a6xx.
801 */
802
803 if ((delay > 0) && (ctx->compiler->gen >= 6) && last_n &&
804 !n_is_scalar_alu &&
805 ((opc_cat(last_n->opc) == 2) || (opc_cat(last_n->opc) == 3)) &&
806 (last_n->repeat == 0)) {
807 /* the previous cat2/cat3 instruction can encode at most 3 nop's: */
808 unsigned transfer = MIN2(delay, 3 - last_n->nop);
809 last_n->nop += transfer;
810 delay -= transfer;
811 state->cycle += transfer;
812 }
813
814 if ((delay > 0) && last_n && (last_n->opc == OPC_NOP)) {
815 /* the previous nop can encode at most 5 repeats: */
816 unsigned transfer = MIN2(delay, 5 - last_n->repeat);
817 last_n->repeat += transfer;
818 delay -= transfer;
819 state->cycle += transfer;
820 }
821
822 if (delay > 0) {
823 assert(delay <= 6);
824 ir3_NOP(&build)->repeat = delay - 1;
825 state->cycle += delay;
826 }
827
828 if (ctx->compiler->samgq_workaround &&
829 !is_compute_or_frag(ctx->type) &&
830 n->opc == OPC_SAMGQ) {
831 struct ir3_instruction *samgp;
832
833 list_delinit(&n->node);
834
835 for (i = 0; i < 4; i++) {
836 samgp = ir3_instr_clone(n);
837 samgp->opc = OPC_SAMGP0 + i;
838 if (i > 1)
839 samgp->flags |= IR3_INSTR_SY;
840 }
841 } else {
842 list_delinit(&n->node);
843 list_addtail(&n->node, &block->instr_list);
844 }
845
846 if (n->opc == OPC_META_TEX_PREFETCH) {
847 assert(n->dsts_count > 0);
848 ctx->has_tex_prefetch = true;
849 }
850
851 if (n->opc == OPC_RESINFO && !has_dummy_dst(n)) {
852 ir3_update_legalize_state(state, ctx->compiler, n);
853
854 n = ir3_NOP(&build);
855 n->flags |= IR3_INSTR_SS;
856 last_input_needs_ss = false;
857 }
858
859 if (is_ssbo(n->opc) || is_global_a3xx_atomic(n->opc) ||
860 is_bindless_atomic(n->opc))
861 ctx->so->has_ssbo = true;
862
863 if (ctx->early_input_release && is_input(n)) {
864 last_input_needs_ss |= (n->opc == OPC_LDLV);
865
866 assert(inputs_remaining > 0);
867 inputs_remaining--;
868 if (inputs_remaining == 0) {
869 /* This is the last input. We add the (ei) flag to release
870 * varying memory after this executes. If it's an ldlv,
871 * however, we need to insert a dummy bary.f on which we can
872 * set the (ei) flag. We may also need to insert an (ss) to
873 * guarantee that all ldlv's have finished fetching their
874 * results before releasing the varying memory.
875 */
876 struct ir3_instruction *last_input = n;
877 if (n->opc == OPC_LDLV) {
878 struct ir3_instruction *baryf;
879
880 /* (ss)bary.f (ei)r63.x, 0, r0.x */
881 baryf = ir3_build_instr(&build, OPC_BARY_F, 1, 2);
882 ir3_dst_create(baryf, regid(63, 0), 0);
883 ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
884 ir3_src_create(baryf, regid(0, 0), 0);
885
886 last_input = baryf;
887 }
888
889 last_input->dsts[0]->flags |= IR3_REG_EI;
890 if (last_input_needs_ss) {
891 last_input->flags |= IR3_INSTR_SS;
892 }
893 }
894 }
895
896 ir3_update_legalize_state(state, ctx->compiler, n);
897 last_n = n;
898 }
899
900 assert(inputs_remaining == 0 || !ctx->early_input_release);
901
902 if (block == ir3_after_preamble(ctx->so->ir) &&
903 ctx->has_tex_prefetch && !ctx->has_inputs) {
904 /* texture prefetch, but *no* inputs.. we need to insert a
905 * dummy bary.f at the top of the shader to unblock varying
906 * storage:
907 */
908 struct ir3_instruction *baryf;
909
910 /* (ss)bary.f (ei)r63.x, 0, r0.x */
911 baryf = ir3_build_instr(&build, OPC_BARY_F, 1, 2);
912 ir3_dst_create(baryf, regid(63, 0), 0)->flags |= IR3_REG_EI;
913 ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
914 ir3_src_create(baryf, regid(0, 0), 0);
915
916 /* insert the dummy bary.f at head: */
917 list_delinit(&baryf->node);
918 list_add(&baryf->node, &block->instr_list);
919 }
920
921 bd->valid = true;
922
923 if (memcmp(&prev_state, state, sizeof(*state))) {
924 /* our output state changed, this invalidates all of our
925 * successors:
926 */
927 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
928 if (!block->successors[i])
929 break;
930 struct ir3_legalize_block_data *pbd = block->successors[i]->data;
931 pbd->valid = false;
932 }
933 }
934
935 return true;
936 }
937
938 /* Expands dsxpp and dsypp macros to:
939 *
940 * dsxpp.1 dst, src
941 * dsxpp.1.p dst, src
942 *
943 * We apply this after flags syncing, as we don't want to sync in between the
944 * two (which might happen if dst == src).
945 */
946 static bool
947 apply_fine_deriv_macro(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
948 {
949 struct list_head instr_list;
950
951 /* remove all the instructions from the list, we'll be adding
952 * them back in as we go
953 */
954 list_replace(&block->instr_list, &instr_list);
955 list_inithead(&block->instr_list);
956
957 foreach_instr_safe (n, &instr_list) {
958 list_addtail(&n->node, &block->instr_list);
959
960 if (n->opc == OPC_DSXPP_MACRO || n->opc == OPC_DSYPP_MACRO) {
961 n->opc = (n->opc == OPC_DSXPP_MACRO) ? OPC_DSXPP_1 : OPC_DSYPP_1;
962
963 struct ir3_instruction *op_p = ir3_instr_clone(n);
964 op_p->flags = IR3_INSTR_P;
965
966 ctx->so->need_full_quad = true;
967 }
968 }
969
970 return true;
971 }
972
973 static void
974 apply_push_consts_load_macro(struct ir3_legalize_ctx *ctx,
975 struct ir3_block *block)
976 {
977 foreach_instr (n, &block->instr_list) {
978 if (n->opc == OPC_PUSH_CONSTS_LOAD_MACRO) {
979 struct ir3_instruction *stsc =
980 ir3_instr_create_at(ir3_after_instr(n), OPC_STSC, 0, 2);
981 ir3_src_create(stsc, 0, IR3_REG_IMMED)->iim_val =
982 n->push_consts.dst_base;
983 ir3_src_create(stsc, 0, IR3_REG_IMMED)->iim_val =
984 n->push_consts.src_base;
985 stsc->cat6.iim_val = n->push_consts.src_size;
986 stsc->cat6.type = TYPE_U32;
987
988 if (ctx->compiler->info->props.stsc_duplication_quirk) {
989 struct ir3_builder build = ir3_builder_at(ir3_after_instr(stsc));
990 struct ir3_instruction *nop = ir3_NOP(&build);
991 nop->flags |= IR3_INSTR_SS;
992 ir3_instr_move_after(ir3_instr_clone(stsc), nop);
993 }
994
995 list_delinit(&n->node);
996 break;
997 } else if (!is_meta(n)) {
998 break;
999 }
1000 }
1001 }
1002
1003 /* NOTE: branch instructions are always the last instruction(s)
1004 * in the block. We take advantage of this as we resolve the
1005 * branches, since "if (foo) break;" constructs turn into
1006 * something like:
1007 *
1008 * block3 {
1009 * ...
1010 * 0029:021: mov.s32s32 r62.x, r1.y
1011 * 0082:022: br !p0.x, target=block5
1012 * 0083:023: br p0.x, target=block4
1013 * // succs: if _[0029:021: mov.s32s32] block4; else block5;
1014 * }
1015 * block4 {
1016 * 0084:024: jump, target=block6
1017 * // succs: block6;
1018 * }
1019 * block5 {
1020 * 0085:025: jump, target=block7
1021 * // succs: block7;
1022 * }
1023 *
1024 * ie. only instruction in block4/block5 is a jump, so when
1025 * resolving branches we can easily detect this by checking
1026 * that the first instruction in the target block is itself
1027 * a jump, and setup the br directly to the jump's target
1028 * (and strip back out the now unreached jump)
1029 *
1030 * TODO sometimes we end up with things like:
1031 *
1032 * br !p0.x, #2
1033 * br p0.x, #12
1034 * add.u r0.y, r0.y, 1
1035 *
1036 * If we swapped the order of the branches, we could drop one.
1037 */
1038 static struct ir3_block *
1039 resolve_dest_block(struct ir3_block *block)
1040 {
1041 /* special case for last block: */
1042 if (!block->successors[0])
1043 return block;
1044
1045 /* NOTE that we may or may not have inserted the jump
1046 * in the target block yet, so conditions to resolve
1047 * the dest to the dest block's successor are:
1048 *
1049 * (1) successor[1] == NULL &&
1050 * (2) (block-is-empty || only-instr-is-jump)
1051 */
1052 if (block->successors[1] == NULL) {
1053 if (list_is_empty(&block->instr_list)) {
1054 return block->successors[0];
1055 } else if (list_length(&block->instr_list) == 1) {
1056 struct ir3_instruction *instr =
1057 list_first_entry(&block->instr_list, struct ir3_instruction, node);
1058 if (instr->opc == OPC_JUMP) {
1059 /* If this jump is backwards, then we will probably convert
1060 * the jump being resolved to a backwards jump, which will
1061 * change a loop-with-continue or loop-with-if into a
1062 * doubly-nested loop and change the convergence behavior.
1063 * Disallow this here.
1064 */
1065 if (block->successors[0]->index <= block->index)
1066 return block;
1067 return block->successors[0];
1068 }
1069 }
1070 }
1071 return block;
1072 }
1073
1074 static void
1075 remove_unused_block(struct ir3_block *old_target)
1076 {
1077 list_delinit(&old_target->node);
1078
1079 /* cleanup dangling predecessors: */
1080 for (unsigned i = 0; i < ARRAY_SIZE(old_target->successors); i++) {
1081 if (old_target->successors[i]) {
1082 struct ir3_block *succ = old_target->successors[i];
1083 ir3_block_remove_predecessor(succ, old_target);
1084 }
1085 }
1086 }
1087
1088 static bool
1089 retarget_jump(struct ir3_instruction *instr, struct ir3_block *new_target)
1090 {
1091 struct ir3_block *old_target = instr->cat0.target;
1092 struct ir3_block *cur_block = instr->block;
1093
1094 /* update current blocks successors to reflect the retargetting: */
1095 if (cur_block->successors[0] == old_target) {
1096 cur_block->successors[0] = new_target;
1097 } else {
1098 assert(cur_block->successors[1] == old_target);
1099 cur_block->successors[1] = new_target;
1100 }
1101
1102 /* update new target's predecessors: */
1103 ir3_block_add_predecessor(new_target, cur_block);
1104
1105 /* and remove old_target's predecessor: */
1106 ir3_block_remove_predecessor(old_target, cur_block);
1107
1108 instr->cat0.target = new_target;
1109
1110 if (old_target->predecessors_count == 0) {
1111 remove_unused_block(old_target);
1112 return true;
1113 }
1114
1115 return false;
1116 }
1117
1118 static bool
1119 is_invertible_branch(struct ir3_instruction *instr)
1120 {
1121 switch (instr->opc) {
1122 case OPC_BR:
1123 case OPC_BRAA:
1124 case OPC_BRAO:
1125 case OPC_BANY:
1126 case OPC_BALL:
1127 return true;
1128 default:
1129 return false;
1130 }
1131 }
1132
1133 static bool
1134 opt_jump(struct ir3 *ir)
1135 {
1136 bool progress = false;
1137
1138 unsigned index = 0;
1139 foreach_block (block, &ir->block_list)
1140 block->index = index++;
1141
1142 foreach_block (block, &ir->block_list) {
1143 /* This pass destroys the physical CFG so don't keep it around to avoid
1144 * validation errors.
1145 */
1146 block->physical_successors_count = 0;
1147 block->physical_predecessors_count = 0;
1148
1149 foreach_instr (instr, &block->instr_list) {
1150 if (!is_flow(instr) || !instr->cat0.target)
1151 continue;
1152
1153 struct ir3_block *tblock = resolve_dest_block(instr->cat0.target);
1154 if (tblock != instr->cat0.target) {
1155 progress = true;
1156
1157 /* Exit early if we deleted a block to avoid iterator
1158 * weirdness/assert fails
1159 */
1160 if (retarget_jump(instr, tblock))
1161 return true;
1162 }
1163 }
1164
1165 /* Detect the case where the block ends either with:
1166 * - A single unconditional jump to the next block.
1167 * - Two jump instructions with opposite conditions, and one of the
1168 * them jumps to the next block.
1169 * We can remove the one that jumps to the next block in either case.
1170 */
1171 if (list_is_empty(&block->instr_list))
1172 continue;
1173
1174 struct ir3_instruction *jumps[2] = {NULL, NULL};
1175 jumps[0] =
1176 list_last_entry(&block->instr_list, struct ir3_instruction, node);
1177 if (!list_is_singular(&block->instr_list))
1178 jumps[1] =
1179 list_last_entry(&jumps[0]->node, struct ir3_instruction, node);
1180
1181 if (jumps[0]->opc == OPC_JUMP)
1182 jumps[1] = NULL;
1183 else if (!is_invertible_branch(jumps[0]) || !jumps[1] ||
1184 !is_invertible_branch(jumps[1])) {
1185 continue;
1186 }
1187
1188 for (unsigned i = 0; i < 2; i++) {
1189 if (!jumps[i])
1190 continue;
1191 struct ir3_block *tblock = jumps[i]->cat0.target;
1192 if (&tblock->node == block->node.next) {
1193 list_delinit(&jumps[i]->node);
1194 progress = true;
1195 break;
1196 }
1197 }
1198 }
1199
1200 return progress;
1201 }
1202
1203 static void
1204 resolve_jumps(struct ir3 *ir)
1205 {
1206 foreach_block (block, &ir->block_list)
1207 foreach_instr (instr, &block->instr_list)
1208 if (is_flow(instr) && instr->cat0.target) {
1209 struct ir3_instruction *target = list_first_entry(
1210 &instr->cat0.target->instr_list, struct ir3_instruction, node);
1211
1212 instr->cat0.immed = (int)target->ip - (int)instr->ip;
1213 }
1214 }
1215
1216 static void
1217 mark_jp(struct ir3_block *block)
1218 {
1219 /* We only call this on the end block (in kill_sched) or after retargeting
1220 * all jumps to empty blocks (in mark_xvergence_points) so there's no need to
1221 * worry about empty blocks.
1222 */
1223 assert(!list_is_empty(&block->instr_list));
1224
1225 struct ir3_instruction *target =
1226 list_first_entry(&block->instr_list, struct ir3_instruction, node);
1227
1228 /* Add nop instruction for (jp) flag since it has no effect on a5xx when set
1229 * on the end instruction.
1230 */
1231 if (target->opc == OPC_END && block->shader->compiler->gen == 5) {
1232 struct ir3_builder build = ir3_builder_at(ir3_before_instr(target));
1233 target = ir3_NOP(&build);
1234 }
1235
1236 target->flags |= IR3_INSTR_JP;
1237 }
1238
1239 /* Mark points where control flow reconverges.
1240 *
1241 * Re-convergence points are where "parked" threads are reconverged with threads
1242 * that took the opposite path last time around. We already calculated them, we
1243 * just need to mark them with (jp).
1244 */
1245 static void
1246 mark_xvergence_points(struct ir3 *ir)
1247 {
1248 foreach_block (block, &ir->block_list) {
1249 if (block->reconvergence_point || block->wave_reconvergence_point)
1250 mark_jp(block);
1251 }
1252 }
1253
1254 static void
1255 invert_branch(struct ir3_instruction *branch)
1256 {
1257 switch (branch->opc) {
1258 case OPC_BR:
1259 break;
1260 case OPC_BALL:
1261 branch->opc = OPC_BANY;
1262 break;
1263 case OPC_BANY:
1264 branch->opc = OPC_BALL;
1265 break;
1266 case OPC_BRAA:
1267 branch->opc = OPC_BRAO;
1268 break;
1269 case OPC_BRAO:
1270 branch->opc = OPC_BRAA;
1271 break;
1272 default:
1273 UNREACHABLE("can't get here");
1274 }
1275
1276 branch->cat0.inv1 = !branch->cat0.inv1;
1277 branch->cat0.inv2 = !branch->cat0.inv2;
1278 branch->cat0.target = branch->block->successors[1];
1279 }
1280
1281 /* Insert the branch/jump instructions for flow control between blocks.
1282 * Initially this is done naively, without considering if the successor
1283 * block immediately follows the current block (ie. so no jump required),
1284 * but that is cleaned up in opt_jump().
1285 */
1286 static void
1287 block_sched(struct ir3 *ir)
1288 {
1289 foreach_block (block, &ir->block_list) {
1290 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
1291
1292 if (block->successors[1]) {
1293 /* if/else, conditional branches to "then" or "else": */
1294 struct ir3_instruction *br1, *br2;
1295
1296 assert(terminator);
1297 unsigned opc = terminator->opc;
1298
1299 if (opc == OPC_GETONE || opc == OPC_SHPS || opc == OPC_GETLAST) {
1300 /* getone/shps can't be inverted, and it wouldn't even make sense
1301 * to follow it with an inverted branch, so follow it by an
1302 * unconditional branch.
1303 */
1304 assert(terminator->srcs_count == 0);
1305 br1 = terminator;
1306 br1->cat0.target = block->successors[1];
1307
1308 struct ir3_builder build = ir3_builder_at(ir3_after_block(block));
1309 br2 = ir3_JUMP(&build);
1310 br2->cat0.target = block->successors[0];
1311 } else if (opc == OPC_BR || opc == OPC_BRAA || opc == OPC_BRAO ||
1312 opc == OPC_BALL || opc == OPC_BANY) {
1313 /* create "else" branch first (since "then" block should
1314 * frequently/always end up being a fall-thru):
1315 */
1316 br1 = terminator;
1317 br2 = ir3_instr_clone(br1);
1318 invert_branch(br1);
1319 br2->cat0.target = block->successors[0];
1320 } else {
1321 assert(opc == OPC_PREDT || opc == OPC_PREDF);
1322
1323 /* Handled by prede_sched. */
1324 terminator->cat0.target = block->successors[0];
1325 continue;
1326 }
1327
1328 /* Creating br2 caused it to be moved before the terminator b1, move it
1329 * back.
1330 */
1331 ir3_instr_move_after(br2, br1);
1332 } else if (block->successors[0]) {
1333 /* otherwise unconditional jump or predt/predf to next block which
1334 * should already have been inserted.
1335 */
1336 assert(terminator);
1337 assert(terminator->opc == OPC_JUMP || terminator->opc == OPC_PREDT ||
1338 terminator->opc == OPC_PREDF || terminator->opc == OPC_SHPE);
1339
1340 /* shpe is not a branch (we just treat it as a terminator to make sure
1341 * it stays at the end of its block) so add one now.
1342 */
1343 if (terminator->opc == OPC_SHPE) {
1344 struct ir3_builder b = ir3_builder_at(ir3_after_instr(terminator));
1345 terminator = ir3_JUMP(&b);
1346 }
1347
1348 terminator->cat0.target = block->successors[0];
1349 }
1350 }
1351 }
1352
1353 static void
1354 add_nop_before_block(struct ir3_block *block, unsigned repeat)
1355 {
1356 struct ir3_instruction *nop = ir3_block_get_first_instr(block);
1357
1358 if (!nop || nop->opc != OPC_NOP) {
1359 struct ir3_builder build = ir3_builder_at(ir3_before_block(block));
1360 nop = ir3_NOP(&build);
1361 }
1362
1363 nop->repeat = MAX2(nop->repeat, repeat);
1364 }
1365
1366 /* Some gens have a hardware issue that needs to be worked around by 1)
1367 * inserting 4 nops after the second pred[tf] of a pred[tf]/pred[ft] pair and/or
1368 * inserting 6 nops after prede.
1369 *
1370 * This function should be called with the second pred[tf] of such a pair and
1371 * NULL if there is only one pred[tf].
1372 */
1373 static void
1374 add_predication_workaround(struct ir3_compiler *compiler,
1375 struct ir3_instruction *predtf,
1376 struct ir3_instruction *prede)
1377 {
1378 if (predtf && compiler->info->props.predtf_nop_quirk) {
1379 add_nop_before_block(predtf->block->predecessors[0]->successors[1], 4);
1380 }
1381
1382 if (compiler->info->props.prede_nop_quirk) {
1383 add_nop_before_block(prede->block->successors[0], 6);
1384 }
1385 }
1386
1387 static void
1388 prede_sched(struct ir3 *ir)
1389 {
1390 unsigned index = 0;
1391 foreach_block (block, &ir->block_list)
1392 block->index = index++;
1393
1394 foreach_block (block, &ir->block_list) {
1395 /* Look for the following pattern generated by NIR lowering. The numbers
1396 * at the top of blocks are their index.
1397 * |--- i ----|
1398 * | ... |
1399 * | pred[tf] |
1400 * |----------|
1401 * succ0 / \ succ1
1402 * |-- i+1 ---| |-- i+2 ---|
1403 * | ... | | ... |
1404 * | pred[ft] | | ... |
1405 * |----------| |----------|
1406 * succ0 \ / succ0
1407 * |--- j ----|
1408 * | ... |
1409 * |----------|
1410 */
1411 struct ir3_block *succ0 = block->successors[0];
1412 struct ir3_block *succ1 = block->successors[1];
1413
1414 if (!succ1)
1415 continue;
1416
1417 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
1418 if (!terminator)
1419 continue;
1420 if (terminator->opc != OPC_PREDT && terminator->opc != OPC_PREDF)
1421 continue;
1422
1423 assert(!succ0->successors[1] && !succ1->successors[1]);
1424 assert(succ0->successors[0] == succ1->successors[0]);
1425 assert(succ0->predecessors_count == 1 && succ1->predecessors_count == 1);
1426 assert(succ0->index == (block->index + 1));
1427 assert(succ1->index == (block->index + 2));
1428
1429 struct ir3_instruction *succ0_terminator =
1430 ir3_block_get_terminator(succ0);
1431 assert(succ0_terminator);
1432 assert(succ0_terminator->opc ==
1433 (terminator->opc == OPC_PREDT ? OPC_PREDF : OPC_PREDT));
1434
1435 ASSERTED struct ir3_instruction *succ1_terminator =
1436 ir3_block_get_terminator(succ1);
1437 assert(!succ1_terminator || (succ1_terminator->opc == OPC_JUMP));
1438
1439 /* Simple case: both successors contain instructions. Keep both blocks and
1440 * insert prede before the second successor's terminator:
1441 * |--- i ----|
1442 * | ... |
1443 * | pred[tf] |
1444 * |----------|
1445 * succ0 / \ succ1
1446 * |-- i+1 ---| |-- i+2 ---|
1447 * | ... | | ... |
1448 * | pred[ft] | | prede |
1449 * |----------| |----------|
1450 * succ0 \ / succ0
1451 * |--- j ----|
1452 * | ... |
1453 * |----------|
1454 */
1455 if (!list_is_empty(&succ1->instr_list)) {
1456 struct ir3_builder build =
1457 ir3_builder_at(ir3_before_terminator(succ1));
1458 struct ir3_instruction *prede = ir3_PREDE(&build);
1459 add_predication_workaround(ir->compiler, succ0_terminator, prede);
1460 continue;
1461 }
1462
1463 /* Second successor is empty so we can remove it:
1464 * |--- i ----|
1465 * | ... |
1466 * | pred[tf] |
1467 * |----------|
1468 * succ0 / \ succ1
1469 * |-- i+1 ---| |
1470 * | ... | |
1471 * | prede | |
1472 * |----------| |
1473 * succ0 \ /
1474 * |--- j ----|
1475 * | ... |
1476 * |----------|
1477 */
1478 list_delinit(&succ0_terminator->node);
1479 struct ir3_builder build = ir3_builder_at(ir3_before_terminator(succ0));
1480 struct ir3_instruction *prede = ir3_PREDE(&build);
1481
1482 /* legalize_block might have set sync flags on the terminator that we just
1483 * removed; set them on prede instead.
1484 */
1485 prede->flags |= succ0_terminator->flags;
1486 add_predication_workaround(ir->compiler, NULL, prede);
1487 remove_unused_block(succ1);
1488 block->successors[1] = succ0->successors[0];
1489 ir3_block_add_predecessor(succ0->successors[0], block);
1490 }
1491 }
1492
1493 /* Here we workaround the fact that kill doesn't actually kill the thread as
1494 * GL expects. The last instruction always needs to be an end instruction,
1495 * which means that if we're stuck in a loop where kill is the only way out,
1496 * then we may have to jump out to the end. kill may also have the d3d
1497 * semantics of converting the thread to a helper thread, rather than setting
1498 * the exec mask to 0, in which case the helper thread could get stuck in an
1499 * infinite loop.
1500 *
1501 * We do this late, both to give the scheduler the opportunity to reschedule
1502 * kill instructions earlier and to avoid having to create a separate basic
1503 * block.
1504 *
1505 * TODO: Assuming that the wavefront doesn't stop as soon as all threads are
1506 * killed, we might benefit by doing this more aggressively when the remaining
1507 * part of the program after the kill is large, since that would let us
1508 * skip over the instructions when there are no non-killed threads left.
1509 */
1510 static void
1511 kill_sched(struct ir3 *ir, struct ir3_shader_variant *so)
1512 {
1513 ir3_count_instructions(ir);
1514
1515 /* True if we know that this block will always eventually lead to the end
1516 * block:
1517 */
1518 bool always_ends = true;
1519 bool added = false;
1520 struct ir3_block *last_block =
1521 list_last_entry(&ir->block_list, struct ir3_block, node);
1522
1523 foreach_block_rev (block, &ir->block_list) {
1524 for (unsigned i = 0; i < 2 && block->successors[i]; i++) {
1525 if (block->successors[i]->start_ip < block->end_ip)
1526 always_ends = false;
1527 }
1528
1529 if (always_ends)
1530 continue;
1531
1532 foreach_instr_safe (instr, &block->instr_list) {
1533 if (instr->opc != OPC_KILL)
1534 continue;
1535
1536 struct ir3_instruction *br =
1537 ir3_instr_create_at(ir3_after_instr(instr), OPC_BR, 0, 1);
1538 ir3_src_create(br, instr->srcs[0]->num, instr->srcs[0]->flags)->wrmask =
1539 1;
1540 br->cat0.target =
1541 list_last_entry(&ir->block_list, struct ir3_block, node);
1542
1543 added = true;
1544 }
1545 }
1546
1547 if (added) {
1548 /* I'm not entirely sure how the branchstack works, but we probably
1549 * need to add at least one entry for the divergence which is resolved
1550 * at the end:
1551 */
1552 so->branchstack++;
1553
1554 /* We don't update predecessors/successors, so we have to do this
1555 * manually:
1556 */
1557 mark_jp(last_block);
1558 }
1559 }
1560
1561 static void
1562 dbg_sync_sched(struct ir3 *ir, struct ir3_shader_variant *so)
1563 {
1564 foreach_block (block, &ir->block_list) {
1565 foreach_instr_safe (instr, &block->instr_list) {
1566 if (is_ss_producer(instr) || is_sy_producer(instr)) {
1567 struct ir3_builder build = ir3_builder_at(ir3_after_instr(instr));
1568 struct ir3_instruction *nop = ir3_NOP(&build);
1569 nop->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
1570 }
1571 }
1572 }
1573 }
1574
1575 static void
1576 dbg_nop_sched(struct ir3 *ir, struct ir3_shader_variant *so)
1577 {
1578 foreach_block (block, &ir->block_list) {
1579 foreach_instr_safe (instr, &block->instr_list) {
1580 struct ir3_builder build = ir3_builder_at(ir3_before_instr(instr));
1581 struct ir3_instruction *nop = ir3_NOP(&build);
1582 nop->repeat = 5;
1583 }
1584 }
1585 }
1586
1587 static void
1588 dbg_expand_rpt(struct ir3 *ir)
1589 {
1590 foreach_block (block, &ir->block_list) {
1591 foreach_instr_safe (instr, &block->instr_list) {
1592 if (instr->repeat == 0 || instr->opc == OPC_NOP ||
1593 instr->opc == OPC_SWZ || instr->opc == OPC_GAT ||
1594 instr->opc == OPC_SCT) {
1595 continue;
1596 }
1597
1598 for (unsigned i = 0; i <= instr->repeat; ++i) {
1599 struct ir3_instruction *rpt = ir3_instr_clone(instr);
1600 ir3_instr_move_before(rpt, instr);
1601 rpt->repeat = 0;
1602
1603 foreach_dst (dst, rpt) {
1604 dst->num += i;
1605 dst->wrmask = 1;
1606 }
1607
1608 foreach_src (src, rpt) {
1609 if (!(src->flags & IR3_REG_R))
1610 continue;
1611
1612 src->num += i;
1613 src->uim_val += i;
1614 src->wrmask = 1;
1615 src->flags &= ~IR3_REG_R;
1616 }
1617 }
1618
1619 list_delinit(&instr->node);
1620 }
1621 }
1622 }
1623
1624 struct ir3_mark_helpers_data {
1625 bool valid;
1626 regmask_t needs_helpers;
1627 };
1628
1629 static void
1630 instr_mark_helpers(struct ir3_mark_helpers_data *bd,
1631 struct ir3_instruction *instr)
1632 {
1633 if (instr->flags & IR3_INSTR_NEEDS_HELPERS) {
1634 return;
1635 }
1636
1637 foreach_dst (dst, instr) {
1638 if (dst->flags & (IR3_REG_RT | IR3_REG_DUMMY)) {
1639 continue;
1640 }
1641
1642 if (regmask_get(&bd->needs_helpers, dst)) {
1643 instr->flags |= IR3_INSTR_NEEDS_HELPERS;
1644 return;
1645 }
1646 }
1647
1648 switch (instr->opc) {
1649 case OPC_MOVMSK:
1650 case OPC_BRCST_ACTIVE:
1651 case OPC_QUAD_SHUFFLE_BRCST:
1652 case OPC_QUAD_SHUFFLE_HORIZ:
1653 case OPC_QUAD_SHUFFLE_VERT:
1654 case OPC_QUAD_SHUFFLE_DIAG:
1655 case OPC_BALL:
1656 case OPC_BANY:
1657 /* Subgroup operations don't require helper invocations to be present, but
1658 * will use helper invocations if they are present.
1659 */
1660 instr->flags |= IR3_INSTR_NEEDS_HELPERS;
1661 return;
1662
1663 case OPC_SAM:
1664 case OPC_SAMB:
1665 case OPC_GETLOD:
1666 case OPC_DSX:
1667 case OPC_DSY:
1668 case OPC_DSXPP_1:
1669 case OPC_DSYPP_1: {
1670 if (instr->opc == OPC_SAM && has_dummy_dst(instr)) {
1671 /* sam requires helper invocations except for dummy prefetch
1672 * instructions.
1673 */
1674 return;
1675 }
1676
1677 /* These instructions don't use helpers themselves but have a src that
1678 * needs to be calculated using helpers (e.g., the coordinates used to
1679 * calculate derivatives). Mark the src register as needing helpers so
1680 * that we can keep them enabled until it is written.
1681 */
1682 unsigned nsrcs;
1683 unsigned first_src_n = 0;
1684
1685 if (instr->opc == OPC_SAM || instr->opc == OPC_SAMB ||
1686 instr->opc == OPC_GETLOD) {
1687 nsrcs = (instr->flags & IR3_INSTR_3D) ? 3 : 2;
1688
1689 /* sam.s2en has the samp/tex in the first src; skip over it. */
1690 if (instr->flags & IR3_INSTR_S2EN) {
1691 if (instr->srcs[0]->flags & IR3_REG_FIRST_ALIAS) {
1692 assert(ir3_alias_group_size(instr, 0) == 2);
1693 first_src_n = 2;
1694 } else {
1695 first_src_n = 1;
1696 }
1697 }
1698 } else {
1699 /* dsx/dsy: derive the number of sources from the dst wrmask since the
1700 * src itself may use aliases.
1701 */
1702 nsrcs = util_last_bit(instr->dsts[0]->wrmask);
1703 }
1704
1705 if (instr->srcs[first_src_n]->flags & IR3_REG_FIRST_ALIAS) {
1706 assert(first_src_n + nsrcs <= instr->srcs_count);
1707
1708 for (unsigned i = first_src_n; i < first_src_n + nsrcs; i++) {
1709 struct ir3_register *src = instr->srcs[i];
1710
1711 if (is_reg_gpr(src)) {
1712 regmask_set(&bd->needs_helpers, src);
1713 }
1714 }
1715 } else {
1716 regmask_set_masked(&bd->needs_helpers, instr->srcs[first_src_n],
1717 MASK(nsrcs));
1718 }
1719
1720 break;
1721 }
1722
1723 default:
1724 break;
1725 }
1726 }
1727
1728 /* Apply IR3_INSTR_NEEDS_HELPERS to instructions that need helper invocations to
1729 * be active. Note that we don't necessarily apply it to all instructions that
1730 * need helpers, just to the last one in each block, as that gives us enough
1731 * information for inserting (eq) to kill helpers.
1732 *
1733 * We use a backwards data-flow analysis because we cannot always know whether
1734 * an instruction needs helpers by just looking at the opcode. For example,
1735 * instructions that calculate (implicit) derivatives don't need helpers to be
1736 * active but the calculation of their src needs to be done with active helpers.
1737 */
1738 static bool
1739 mark_helpers(struct ir3_legalize_ctx *ctx, struct ir3 *ir,
1740 struct ir3_shader_variant *so)
1741 {
1742 foreach_block (block, &ir->block_list) {
1743 struct ir3_mark_helpers_data *bd =
1744 ralloc(ctx, struct ir3_mark_helpers_data);
1745 bd->valid = false;
1746 regmask_init(&bd->needs_helpers, ctx->compiler->mergedregs);
1747 block->data = bd;
1748 }
1749
1750 bool uses_helpers = false;
1751 bool progress;
1752
1753 do {
1754 progress = false;
1755
1756 foreach_block_rev (block, &ir->block_list) {
1757 struct ir3_mark_helpers_data *bd = block->data;
1758
1759 if (bd->valid) {
1760 continue;
1761 }
1762
1763 struct ir3_mark_helpers_data prev_bd = *bd;
1764 regmask_init(&bd->needs_helpers, ctx->compiler->mergedregs);
1765 bool may_have_needs_helpers_at_entry = true;
1766
1767 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
1768 struct ir3_block *succ = block->successors[i];
1769 if (!succ) {
1770 continue;
1771 }
1772
1773 struct ir3_mark_helpers_data *succ_bd = succ->data;
1774 regmask_or(&bd->needs_helpers, &bd->needs_helpers,
1775 &succ_bd->needs_helpers);
1776 }
1777
1778 foreach_instr_rev (instr, &block->instr_list) {
1779 instr_mark_helpers(bd, instr);
1780
1781 /* We only care about the last instruction needing helpers. */
1782 if (instr->flags & IR3_INSTR_NEEDS_HELPERS) {
1783 uses_helpers = true;
1784
1785 /* This also means we can stop tracking needs_helpers. This saves
1786 * us from unnecessarily invalidating predecessors. Making sure
1787 * loops are handled correctly is done in helper_sched.
1788 */
1789 regmask_init(&bd->needs_helpers, ctx->compiler->mergedregs);
1790 may_have_needs_helpers_at_entry = false;
1791 break;
1792 }
1793 }
1794
1795 bd->valid = true;
1796
1797 /* We have to invalidate the block's predecessors whenever it has more
1798 * needs_helpers registers as the previous time around because this may
1799 * cause more instructions being marked as needing helpers in its
1800 * predecessors. We don't have to do this when it has less
1801 * needs_helpers registers as this won't change anything. This is
1802 * checked using may_have_needs_helpers_at_entry which will be false
1803 * whenever we cleared needs_helpers.
1804 */
1805 if (may_have_needs_helpers_at_entry &&
1806 memcmp(&prev_bd.needs_helpers, &bd->needs_helpers,
1807 sizeof(prev_bd.needs_helpers)) != 0) {
1808 progress = true;
1809
1810 for (unsigned i = 0; i < block->predecessors_count; i++) {
1811 struct ir3_mark_helpers_data *pred_bd =
1812 block->predecessors[i]->data;
1813 pred_bd->valid = false;
1814 }
1815 }
1816 }
1817 } while (progress);
1818
1819 struct ir3_block *start_block = ir3_start_block(ir);
1820 struct ir3_mark_helpers_data *start_bd = start_block->data;
1821
1822 foreach_input (input, ir) {
1823 if (regmask_get(&start_bd->needs_helpers, input->dsts[0])) {
1824 /* If we need helpers for an input reg, we have to make sure helpers
1825 * are enabled when we enter the shader. Just mark the first
1826 * instruction as needing helpers.
1827 */
1828 struct ir3_instruction *first = ir3_block_get_first_instr(start_block);
1829 first->flags |= IR3_INSTR_NEEDS_HELPERS;
1830 uses_helpers = true;
1831 break;
1832 }
1833 }
1834
1835 return uses_helpers;
1836 }
1837
1838 struct ir3_end_of_feature_block_data {
1839 /* Whether the feature may be used on any path starting at the
1840 * beginning of the block.
1841 */
1842 bool uses_feature_beginning;
1843
1844 /* Whether the feature may be used by the end of the block. Branch
1845 * instructions are considered to be "between" blocks, because the flag has
1846 * to be inserted after them in the successor blocks, so branch instructions
1847 * using the feature will result in uses_feature_end = true for their block.
1848 */
1849 bool uses_feature_end;
1850 };
1851
1852 /* There are NOP flags which signify that certain feature will not be used after
1853 * this point. It could be the end of helper invocations (eq), the end of
1854 * cat5/cat6 usage (eolm)/(eogm). The common denominator is that such flags are
1855 * placed onto nop and cannot be used inside a control flow.
1856 *
1857 * Use a backwards dataflow analysis to determine at which points
1858 * in the feature is definitely never used, and then insert
1859 * corresponding flag at the point where we cross from a point where they may be
1860 * used to a point where they are never used.
1861 */
1862 static bool
1863 feature_usage_sched(struct ir3_legalize_ctx *ctx, struct ir3 *ir,
1864 struct ir3_shader_variant *so,
1865 bool (*check_instr)(struct ir3_instruction *),
1866 bool (*cheap_instr)(struct ir3_instruction *),
1867 uint32_t new_flag)
1868 {
1869 bool uses_feature = false;
1870
1871 foreach_block (block, &ir->block_list) {
1872 struct ir3_end_of_feature_block_data *bd =
1873 rzalloc(ctx, struct ir3_end_of_feature_block_data);
1874 foreach_instr (instr, &block->instr_list) {
1875 if (check_instr(instr)) {
1876 uses_feature = true;
1877 bd->uses_feature_beginning = true;
1878
1879 if (is_terminator(instr)) {
1880 bd->uses_feature_end = true;
1881 }
1882 }
1883
1884 if (instr->opc == OPC_SHPE) {
1885 /* The flags are not allowed in preambles, mark the whole preamble
1886 * as using it to avoid putting it there.
1887 */
1888 bd->uses_feature_beginning = true;
1889 bd->uses_feature_end = true;
1890 }
1891 }
1892
1893 block->data = bd;
1894 }
1895
1896 /* If the feature was not needed by any instruction, return early. Features
1897 * that aren't used in the shader can generally be disabled at the top level
1898 * (helpers disabled at dispatch, local memory size set to 0), so no need to
1899 * add a NOP just to add the flag.
1900 */
1901 if (!uses_feature)
1902 return false;
1903
1904 bool progress;
1905 do {
1906 progress = false;
1907 foreach_block_rev (block, &ir->block_list) {
1908 struct ir3_end_of_feature_block_data *bd = block->data;
1909
1910 if (!bd->uses_feature_beginning)
1911 continue;
1912
1913 for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
1914 struct ir3_block *pred = block->physical_predecessors[i];
1915 struct ir3_end_of_feature_block_data *pred_bd = pred->data;
1916 if (!pred_bd->uses_feature_end) {
1917 pred_bd->uses_feature_end = true;
1918 }
1919 if (!pred_bd->uses_feature_beginning) {
1920 pred_bd->uses_feature_beginning = true;
1921 progress = true;
1922 }
1923 }
1924 }
1925 } while (progress);
1926
1927 /* Now, we need to determine the points where the feature become
1928 * unused.
1929 */
1930 foreach_block (block, &ir->block_list) {
1931 struct ir3_end_of_feature_block_data *bd = block->data;
1932 if (bd->uses_feature_end)
1933 continue;
1934
1935 /* We need to check the predecessors because of situations with critical
1936 * edges like this that can occur after optimizing jumps:
1937 *
1938 * br p0.x, #endif
1939 * ...
1940 * sam ...
1941 * ...
1942 * endif:
1943 * ...
1944 * end
1945 *
1946 * The endif block will have uses_feature_beginning = false and
1947 * uses_feature_end = false, but because we jump to there from the
1948 * beginning of the if where uses_feature_end = true, we still want to
1949 * add the flag at the beginning of the block:
1950 *
1951 * br p0.x, #endif
1952 * ...
1953 * sam ...
1954 * (eq)nop
1955 * ...
1956 * endif:
1957 * (eq)nop
1958 * ...
1959 * end
1960 *
1961 * This an extra nop in the case where the branch isn't taken, but that's
1962 * probably preferable to adding an extra jump instruction which is what
1963 * would happen if we ran this pass before optimizing jumps:
1964 *
1965 * br p0.x, #else
1966 * ...
1967 * sam ...
1968 * (eq)nop
1969 * ...
1970 * jump #endif
1971 * else:
1972 * (eq)nop
1973 * endif:
1974 * ...
1975 * end
1976 *
1977 * We also need this to make sure we insert the flag after branches which use
1978 * the feature.
1979 */
1980 bool pred_uses_feature = bd->uses_feature_beginning;
1981 for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
1982 struct ir3_block *pred = block->physical_predecessors[i];
1983 struct ir3_end_of_feature_block_data *pred_bd = pred->data;
1984 if (pred_bd->uses_feature_end) {
1985 pred_uses_feature = true;
1986 break;
1987 }
1988 }
1989
1990 if (!pred_uses_feature)
1991 continue;
1992
1993 /* The last use of the feature is somewhere between the beginning and the
1994 * end. first_instr will be the first instruction where the feature are no
1995 * longer required, or NULL if the feature are not required just at the end.
1996 */
1997 struct ir3_instruction *first_instr = NULL;
1998 foreach_instr_rev (instr, &block->instr_list) {
1999 if (check_instr(instr))
2000 break;
2001 first_instr = instr;
2002 }
2003
2004 bool killed = false;
2005 bool expensive_instruction_in_block = false;
2006 if (first_instr) {
2007 foreach_instr_from (instr, first_instr, &block->instr_list) {
2008 /* If there's already a nop, we don't have to worry about whether to
2009 * insert one.
2010 */
2011 if (instr->opc == OPC_NOP) {
2012 instr->flags |= new_flag;
2013 killed = true;
2014 break;
2015 }
2016
2017 if (cheap_instr(instr))
2018 continue;
2019 if (instr->opc == OPC_PREDE)
2020 continue;
2021
2022 expensive_instruction_in_block = true;
2023 break;
2024 }
2025 }
2026
2027 /* If this block isn't the last block before the end instruction, assume
2028 * that there may be expensive instructions in later blocks so it's worth
2029 * it to insert a nop.
2030 */
2031 if (!killed && (expensive_instruction_in_block ||
2032 block->successors[0] != ir3_end_block(ir))) {
2033 struct ir3_cursor cursor = first_instr ? ir3_before_instr(first_instr)
2034 : ir3_before_terminator(block);
2035 struct ir3_builder build = ir3_builder_at(cursor);
2036 struct ir3_instruction *nop = ir3_NOP(&build);
2037 nop->flags |= new_flag;
2038 }
2039 }
2040
2041 return true;
2042 }
2043
2044 static bool
2045 uses_helpers(struct ir3_instruction *instr)
2046 {
2047 return !!(instr->flags & IR3_INSTR_NEEDS_HELPERS);
2048 }
2049
2050 static bool
2051 is_cheap_for_eq(struct ir3_instruction *instr)
2052 {
2053 /* ALU and SFU instructions probably aren't going to benefit much
2054 * from killing helper invocations, because they complete at least
2055 * an entire quad in a cycle and don't access any quad-divergent
2056 * memory, so delay emitting (eq) in the hopes that we find a nop
2057 * afterwards.
2058 */
2059 return is_alu(instr) || is_sfu(instr);
2060 }
2061
2062 /* Insert (eq) after the last instruction using the results of helper
2063 * invocations. Use a backwards dataflow analysis to determine at which points
2064 * in the program helper invocations are definitely never used, and then insert
2065 * (eq) at the point where we cross from a point where they may be used to a
2066 * point where they are never used.
2067 */
2068 static void
2069 helper_sched(struct ir3_legalize_ctx *ctx, struct ir3 *ir,
2070 struct ir3_shader_variant *so)
2071 {
2072 if (!feature_usage_sched(ctx, ir, so, uses_helpers, is_cheap_for_eq,
2073 IR3_INSTR_EQ) &&
2074 so->num_sampler_prefetch) {
2075 /* If only prefetches use helpers then we can disable them in the shader
2076 * via a register setting.
2077 */
2078 so->prefetch_end_of_quad = true;
2079 }
2080 }
2081
2082 static bool
2083 needs_eolm(struct ir3_instruction *instr)
2084 {
2085 switch (instr->opc) {
2086 case OPC_STL:
2087 case OPC_LDL:
2088 case OPC_STLW:
2089 case OPC_LDLW:
2090 case OPC_ATOMIC_ADD:
2091 case OPC_ATOMIC_SUB:
2092 case OPC_ATOMIC_XCHG:
2093 case OPC_ATOMIC_INC:
2094 case OPC_ATOMIC_DEC:
2095 case OPC_ATOMIC_CMPXCHG:
2096 case OPC_ATOMIC_MIN:
2097 case OPC_ATOMIC_MAX:
2098 case OPC_ATOMIC_AND:
2099 case OPC_ATOMIC_OR:
2100 case OPC_ATOMIC_XOR:
2101 return true;
2102 default:
2103 return false;
2104 }
2105 }
2106
2107 static bool
2108 needs_eogm(struct ir3_instruction *instr)
2109 {
2110 return opc_cat(instr->opc) == 5 || opc_cat(instr->opc) == 6;
2111 }
2112
2113 static bool
2114 is_cheap_for_eolm_eogm(struct ir3_instruction *instr)
2115 {
2116 /* Blob inserts these flags as soon as possible, so consider all instructions
2117 * expensive.
2118 */
2119 return false;
2120 }
2121
2122 struct ir3_last_block_data {
2123 /* Whether a read will be done on a register at a later point, it is
2124 * considered safe to set (last) when this is false for a particular
2125 * register.
2126 */
2127 regmask_t will_read;
2128 };
2129
2130 /* Use a backwards dataflow analysis to determine when a certain register is
2131 * always written to prior to being read in a similar manner to SSA liveness
2132 * which we can use to determine when we can insert (last) on src regs.
2133 */
2134 static void
2135 track_last(struct ir3_legalize_ctx *ctx, struct ir3 *ir,
2136 struct ir3_shader_variant *so)
2137 {
2138 foreach_block (block, &ir->block_list) {
2139 struct ir3_last_block_data *bd = rzalloc(ctx, struct ir3_last_block_data);
2140
2141 regmask_init(&bd->will_read, so->mergedregs);
2142
2143 block->data = bd;
2144 }
2145
2146 bool progress;
2147 do {
2148 progress = false;
2149 regmask_t will_read;
2150 regmask_init(&will_read, so->mergedregs);
2151
2152 foreach_block_rev (block, &ir->block_list) {
2153 struct ir3_last_block_data *bd = block->data;
2154
2155 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
2156 struct ir3_block *succ = block->successors[i];
2157 if (!succ)
2158 continue;
2159
2160 struct ir3_last_block_data *succ_bd = succ->data;
2161 regmask_or(&will_read, &will_read, &succ_bd->will_read);
2162 }
2163
2164 foreach_instr_rev (instr, &block->instr_list) {
2165 for (unsigned i = 0; i < instr->dsts_count; i++) {
2166 struct ir3_register *dst = instr->dsts[i];
2167 if (dst->flags & (IR3_REG_IMMED | IR3_REG_CONST |
2168 IR3_REG_SHARED | IR3_REG_RT)) {
2169 continue;
2170 }
2171
2172 regmask_clear(&will_read, dst);
2173 }
2174
2175 for (unsigned i = 0; i < instr->srcs_count; i++) {
2176 struct ir3_register *src = instr->srcs[i];
2177 if (src->flags &
2178 (IR3_REG_IMMED | IR3_REG_CONST | IR3_REG_SHARED)) {
2179 continue;
2180 }
2181
2182 if (!regmask_get(&will_read, src)) {
2183 if (!(src->flags & IR3_REG_LAST_USE)) {
2184 progress = true;
2185 src->flags |= IR3_REG_LAST_USE;
2186 }
2187 } else if (src->flags & IR3_REG_LAST_USE) {
2188 progress = true;
2189 src->flags &= ~IR3_REG_LAST_USE;
2190 }
2191
2192 /* Setting will_read immediately ensures that only the first src
2193 * of multiple identical srcs will have (last) set. This matches
2194 * the blob's behavior.
2195 */
2196 regmask_set(&will_read, src);
2197 }
2198 }
2199
2200 bd->will_read = will_read;
2201 }
2202 } while (progress);
2203 }
2204
2205 /* An instruction where we may insert nops without increasing the runtime nop
2206 * count. These are of the form `(rptN)nop` and `(nopN)foo`. We can insert nops
2207 * after them while decreasing their rpt/nop count without affecting the runtime
2208 * behavior.
2209 */
2210 struct nop_insert_point {
2211 struct ir3_instruction *instr;
2212
2213 /* The maximum number of nops that can be inserted after `instr` without
2214 * pushing any subsequent alias sequences over the next cache line. Since
2215 * inserting nops at an earlier insert point affects all later ones, the
2216 * max_nops value of the last insert point is the maximum for all insert
2217 * points. This is taken into account when iterating insert points so that we
2218 * only have to update the last insert point whenever we encounter an alias
2219 * sequence.
2220 */
2221 unsigned max_nops;
2222 };
2223
2224 struct alias_align_ctx {
2225 DECLARE_ARRAY(struct nop_insert_point, nop_insert_points);
2226 };
2227
2228 #define ICACHE_LINE_INSTRS 16
2229
2230 static unsigned
2231 icache_line(unsigned ip)
2232 {
2233 return ip / ICACHE_LINE_INSTRS;
2234 }
2235
2236 static uint8_t *
2237 extra_nops(struct ir3_instruction *instr)
2238 {
2239 if (instr->opc == OPC_NOP) {
2240 return &instr->repeat;
2241 }
2242
2243 return &instr->nop;
2244 }
2245
2246 static bool
2247 is_insert_point(struct ir3_instruction *instr)
2248 {
2249 uint8_t *instr_extra_nops = extra_nops(instr);
2250 return *instr_extra_nops != 0;
2251 }
2252
2253 static void
2254 update_last_insert_point_max_nops(struct alias_align_ctx *ctx,
2255 unsigned max_nops)
2256 {
2257 if (max_nops == 0) {
2258 /* If we cannot insert more nops at the last insert point, we cannot
2259 * insert more anywhere.
2260 */
2261 ctx->nop_insert_points_count = 0;
2262 } else if (ctx->nop_insert_points_count != 0) {
2263 struct nop_insert_point *last_nop_insert_point =
2264 &ctx->nop_insert_points[ctx->nop_insert_points_count - 1];
2265 last_nop_insert_point->max_nops =
2266 MIN2(last_nop_insert_point->max_nops, max_nops);
2267 }
2268 }
2269
2270 static void
2271 insert_nops(struct ir3_cursor cursor, unsigned n)
2272 {
2273 struct ir3_builder b = ir3_builder_at(cursor);
2274
2275 for (unsigned i = 0; i < n; i++) {
2276 ir3_NOP(&b);
2277 }
2278 }
2279
2280 /* There seems to be a hardware bug that sometimes causes a GPU hang when an
2281 * alias...sam sequence crosses an instruction cache line boundary. This pass
2282 * inserts padding nops to ensure no such sequence cross a cache line.
2283 *
2284 * While the number of nops we have to insert is fixed at this point, we can try
2285 * to minimize the number of nops executed at runtime by replacing nops encoded
2286 * in instructions by standalone nops. That is, if this pass has to insert one
2287 * nop, it will try to make one of the following replacements:
2288 * - (rptN)nop -> (rptN-1)nop; nop
2289 * - (nopN)foo -> (nopN-1)foo; nop
2290 *
2291 * It does so by keeping track of "insert points". Each insert point keeps track
2292 * of the instruction and the maximum number of nops that can be inserted there
2293 * without pushing any subsequent alias sequences over the next cache line.
2294 * Whenever we need to insert nops, we first try it at the encountered insert
2295 * points and only if that doesn't work, we insert them right before the first
2296 * alias. The pass makes sure the insert points are only visited a bounded
2297 * number of times in total to keep the whole pass O(n).
2298 */
2299 static void
2300 align_aliases(struct ir3 *ir)
2301 {
2302 if (!ir->compiler->has_alias_tex) {
2303 return;
2304 }
2305
2306 struct alias_align_ctx *ctx = rzalloc(ir, struct alias_align_ctx);
2307 unsigned ip = 0;
2308
2309 foreach_block (block, &ir->block_list) {
2310 bool in_alias_sequence = false;
2311
2312 foreach_instr (instr, &block->instr_list) {
2313 if (instr->opc == OPC_META_TEX_PREFETCH) {
2314 /* These will not be part of the final binary. */
2315 continue;
2316 }
2317
2318 instr->ip = ip++;
2319
2320 if (is_insert_point(instr)) {
2321 struct nop_insert_point nop_insert_point = {
2322 .instr = instr,
2323
2324 /* This will be set when we encounter the next alias sequence. */
2325 .max_nops = ~0,
2326 };
2327
2328 array_insert(ctx, ctx->nop_insert_points, nop_insert_point);
2329 continue;
2330 }
2331
2332 if (instr->opc != OPC_ALIAS || instr->cat7.alias_scope != ALIAS_TEX) {
2333 in_alias_sequence = false;
2334 continue;
2335 }
2336
2337 if (in_alias_sequence) {
2338 /* We only have to handle the first alias. */
2339 continue;
2340 }
2341
2342 in_alias_sequence = true;
2343 unsigned num_aliases = instr->cat7.alias_table_size_minus_one + 1;
2344 unsigned last_ip = instr->ip + num_aliases;
2345 unsigned num_aliases_first_icache_line =
2346 num_aliases - (last_ip % ICACHE_LINE_INSTRS);
2347
2348 if (icache_line(instr->ip) != icache_line(last_ip)) {
2349 unsigned nops_left = num_aliases_first_icache_line;
2350 assert(nops_left > 0);
2351
2352 /* Maximum number of nops we can still insert without pushing any
2353 * existing sequences over the next cache line. Will be set to the
2354 * minimum of the insert points we encounter.
2355 */
2356 unsigned max_nops = ~0;
2357
2358 /* First try to insert nops at the available insert points. */
2359 for (unsigned i = ctx->nop_insert_points_count; i > 0; i--) {
2360 /* Every iteration, we either:
2361 * 1. Break because max_nops has become zero, which removes all
2362 * insert points;
2363 * 2. Pop the current insert point because it has no nops left;
2364 * 3. Break because we're done, this may leave the last insert
2365 * point in the list but will have decreased the number of
2366 * nops available there.
2367 *
2368 * This ensures that every insert point is encountered at most a
2369 * constant number of times (and this constant is bounded by the
2370 * maximum number of nops that can be inserted at any insert
2371 * point, which is 6 for (rpt6)nop). This means that the *total*
2372 * number of iterations for this loop is O(n) and hence the whole
2373 * pass is O(n).
2374 */
2375 assert(i == ctx->nop_insert_points_count);
2376
2377 struct nop_insert_point *nop_insert_point =
2378 &ctx->nop_insert_points[i - 1];
2379
2380 max_nops = MIN2(max_nops, nop_insert_point->max_nops);
2381
2382 uint8_t *instr_extra_nops = extra_nops(nop_insert_point->instr);
2383 unsigned num_nops = MIN3(*instr_extra_nops, nops_left, max_nops);
2384 assert(num_nops != 0);
2385
2386 insert_nops(ir3_after_instr(nop_insert_point->instr), num_nops);
2387 *instr_extra_nops -= num_nops;
2388 nops_left -= num_nops;
2389
2390 /* If max_nops == ~0, then nop_insert_point->max_nops == ~0. This
2391 * means that nop_insert_point is currently unconstrained because
2392 * we didn't encounter a subsequent sequence yet (other than the
2393 * current one). Leave it unconstrained, if it hasn't been
2394 * removed when we're done inserting nops, it will be updated
2395 * when we finish handling this instruction.
2396 */
2397 if (max_nops != ~0) {
2398 max_nops -= num_nops;
2399 nop_insert_point->max_nops = max_nops;
2400 }
2401
2402 if (max_nops == 0) {
2403 /* 1. If we cannot insert more nops at the last insert point,
2404 * we cannot insert more anywhere.
2405 */
2406 ctx->nop_insert_points_count = 0;
2407 break;
2408 }
2409
2410 if (*instr_extra_nops == 0) {
2411 /* 2. The insert point is all used up, remove it. */
2412 ctx->nop_insert_points_count--;
2413
2414 /* Since we only set max_nops on the last insert point, we
2415 * have to propagate it now to the new last.
2416 */
2417 update_last_insert_point_max_nops(ctx, max_nops);
2418 } else {
2419 /* The current insert point has nops left so we should have
2420 * used them if needed.
2421 */
2422 assert(nops_left == 0);
2423 }
2424
2425 if (nops_left == 0) {
2426 /* 3. All necessary nops inserted. */
2427 break;
2428 }
2429 }
2430
2431 /* Then, insert any remaining nops before the first alias. */
2432 insert_nops(ir3_before_instr(instr), nops_left);
2433
2434 instr->ip += num_aliases_first_icache_line;
2435 assert(util_is_aligned(instr->ip, ICACHE_LINE_INSTRS));
2436
2437 last_ip = instr->ip + num_aliases;
2438 assert(icache_line(instr->ip) == icache_line(last_ip));
2439
2440 ip = instr->ip + 1;
2441 }
2442
2443 /* Calculate how many nops we can insert before the first alias without
2444 * moving the whole sequence over the next cache line. This allows us
2445 * to insert nops for subsequent aliases before this one.
2446 */
2447 unsigned max_nops =
2448 ICACHE_LINE_INSTRS - 1 - last_ip % ICACHE_LINE_INSTRS;
2449 update_last_insert_point_max_nops(ctx, max_nops);
2450 }
2451 }
2452
2453 ralloc_free(ctx);
2454 }
2455
2456 bool
2457 ir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary)
2458 {
2459 struct ir3_legalize_ctx *ctx = rzalloc(ir, struct ir3_legalize_ctx);
2460 bool progress;
2461
2462 ctx->so = so;
2463 ctx->max_bary = -1;
2464 ctx->compiler = ir->compiler;
2465 ctx->type = ir->type;
2466
2467 /* allocate per-block data: */
2468 foreach_block (block, &ir->block_list) {
2469 struct ir3_legalize_block_data *bd =
2470 rzalloc(ctx, struct ir3_legalize_block_data);
2471
2472 ir3_init_legalize_state(&bd->state, so->compiler);
2473 ir3_init_legalize_state(&bd->begin_state, so->compiler);
2474
2475 block->data = bd;
2476 }
2477
2478 /* We may have failed to pull all input loads into the first block.
2479 * In such case at the moment we aren't able to find a better place
2480 * to for (ei) than the end of the program.
2481 * a5xx and a6xx do automatically release varying storage at the end.
2482 */
2483 ctx->early_input_release = true;
2484
2485 struct ir3_block *start_block = ir3_after_preamble(ir);
2486
2487 /* Gather information to determine whether we can enable early preamble.
2488 */
2489 bool gpr_in_preamble = false;
2490 bool pred_in_preamble = false;
2491 bool relative_in_preamble = false;
2492 bool in_preamble = start_block != ir3_start_block(ir);
2493 bool has_preamble = start_block != ir3_start_block(ir);
2494
2495 foreach_block (block, &ir->block_list) {
2496 if (block == start_block)
2497 in_preamble = false;
2498
2499 foreach_instr (instr, &block->instr_list) {
2500 if (is_input(instr)) {
2501 ctx->has_inputs = true;
2502 if (block != start_block) {
2503 ctx->early_input_release = false;
2504 }
2505 }
2506
2507 if (is_meta(instr))
2508 continue;
2509
2510 foreach_src (reg, instr) {
2511 if (in_preamble) {
2512 if (!(reg->flags & IR3_REG_SHARED) && is_reg_gpr(reg))
2513 gpr_in_preamble = true;
2514 if (reg->flags & IR3_REG_RELATIV)
2515 relative_in_preamble = true;
2516 }
2517 }
2518
2519 foreach_dst (reg, instr) {
2520 if (is_dest_gpr(reg)) {
2521 if (in_preamble) {
2522 if (!(reg->flags & IR3_REG_SHARED))
2523 gpr_in_preamble = true;
2524 if (reg->flags & IR3_REG_RELATIV)
2525 relative_in_preamble = true;
2526 }
2527 }
2528 }
2529
2530 if (in_preamble && writes_pred(instr) &&
2531 !(instr->dsts[0]->flags & IR3_REG_UNIFORM)) {
2532 pred_in_preamble = true;
2533 }
2534 }
2535 }
2536
2537 so->early_preamble = has_preamble && !gpr_in_preamble &&
2538 !pred_in_preamble && !relative_in_preamble &&
2539 ir->compiler->info->props.has_early_preamble &&
2540 !(ir3_shader_debug & IR3_DBG_NOEARLYPREAMBLE);
2541
2542 /* On a7xx, sync behavior for a1.x is different in the early preamble. RaW
2543 * dependencies must be synchronized with (ss) there must be an extra
2544 * (r) on the source of the mova1 instruction.
2545 */
2546 if (so->early_preamble && ir->compiler->gen >= 7) {
2547 foreach_block (block, &ir->block_list) {
2548 if (block == start_block)
2549 break;
2550 block->in_early_preamble = true;
2551 }
2552 }
2553
2554 assert(ctx->early_input_release || ctx->compiler->gen >= 5);
2555
2556 if (ir3_shader_debug & IR3_DBG_EXPANDRPT) {
2557 dbg_expand_rpt(ir);
2558 }
2559
2560 /* process each block: */
2561 do {
2562 progress = false;
2563 foreach_block (block, &ir->block_list) {
2564 progress |= legalize_block(ctx, block);
2565 }
2566 } while (progress);
2567
2568 *max_bary = ctx->max_bary;
2569
2570 foreach_block (block, &ir->block_list) {
2571 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
2572 if (terminator && terminator->opc == OPC_GETONE) {
2573 apply_push_consts_load_macro(ctx, block->successors[0]);
2574 break;
2575 }
2576 }
2577
2578 block_sched(ir);
2579
2580 foreach_block (block, &ir->block_list) {
2581 progress |= apply_fine_deriv_macro(ctx, block);
2582 }
2583
2584 bool cfg_changed = false;
2585 while (opt_jump(ir))
2586 cfg_changed = true;
2587
2588 prede_sched(ir);
2589
2590 if (cfg_changed)
2591 ir3_calc_reconvergence(so);
2592
2593 if (so->type == MESA_SHADER_FRAGMENT)
2594 kill_sched(ir, so);
2595
2596 if ((so->type == MESA_SHADER_FRAGMENT || ir3_shader_compute(so)) &&
2597 so->compiler->info->props.has_eolm_eogm) {
2598 feature_usage_sched(ctx, ir, so, needs_eolm, is_cheap_for_eolm_eogm,
2599 IR3_INSTR_EOLM);
2600 feature_usage_sched(ctx, ir, so, needs_eogm, is_cheap_for_eolm_eogm,
2601 IR3_INSTR_EOGM);
2602 }
2603
2604 /* TODO: does (eq) exist before a6xx? */
2605 if (so->type == MESA_SHADER_FRAGMENT && so->need_pixlod &&
2606 so->compiler->gen >= 6) {
2607 if (mark_helpers(ctx, ir, so)) {
2608 helper_sched(ctx, ir, so);
2609 } else {
2610 /* If no instructions use helpers, we can disable them in the shader
2611 * via a register setting.
2612 */
2613 so->prefetch_end_of_quad = true;
2614 }
2615 }
2616
2617 if (ir3_shader_debug & IR3_DBG_FULLSYNC) {
2618 dbg_sync_sched(ir, so);
2619 }
2620
2621 if (ir3_shader_debug & IR3_DBG_FULLNOP) {
2622 dbg_nop_sched(ir, so);
2623 }
2624
2625 /* Note: insert (last) before alias.tex to have the sources that are actually
2626 * read by instructions (as opposed to alias registers) more easily
2627 * available.
2628 */
2629 if (so->compiler->gen >= 7)
2630 track_last(ctx, ir, so);
2631
2632 ir3_insert_alias_tex(ir);
2633 align_aliases(ir);
2634 ir3_count_instructions(ir);
2635 resolve_jumps(ir);
2636
2637 mark_xvergence_points(ir);
2638
2639 ralloc_free(ctx);
2640
2641 return true;
2642 }