2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/ralloc.h"
28 #include "util/u_math.h"
31 #include "ir3_compiler.h"
36 * We currently require that scheduling ensures that we have enough nop's
37 * in all the right places. The legalize step mostly handles fixing up
38 * instruction flags ((ss)/(sy)/(ei)), and collapses sequences of nop's
39 * into fewer nop's w/ rpt flag.
42 struct ir3_legalize_ctx
{
43 struct ir3_compiler
*compiler
;
50 struct ir3_legalize_state
{
52 regmask_t needs_ss_war
; /* write after read */
56 struct ir3_legalize_block_data
{
58 struct ir3_legalize_state state
;
61 /* We want to evaluate each block from the position of any other
62 * predecessor block, in order that the flags set are the union of
63 * all possible program paths.
65 * To do this, we need to know the output state (needs_ss/ss_war/sy)
66 * of all predecessor blocks. The tricky thing is loops, which mean
67 * that we can't simply recursively process each predecessor block
68 * before legalizing the current block.
70 * How we handle that is by looping over all the blocks until the
71 * results converge. If the output state of a given block changes
72 * in a given pass, this means that all successor blocks are not
73 * yet fully legalized.
77 legalize_block(struct ir3_legalize_ctx
*ctx
, struct ir3_block
*block
)
79 struct ir3_legalize_block_data
*bd
= block
->data
;
84 struct ir3_instruction
*last_input
= NULL
;
85 struct ir3_instruction
*last_rel
= NULL
;
86 struct ir3_instruction
*last_n
= NULL
;
87 struct list_head instr_list
;
88 struct ir3_legalize_state prev_state
= bd
->state
;
89 struct ir3_legalize_state
*state
= &bd
->state
;
90 bool last_input_needs_ss
= false;
91 bool has_tex_prefetch
= false;
93 /* our input state is the OR of all predecessor blocks' state: */
94 set_foreach(block
->predecessors
, entry
) {
95 struct ir3_block
*predecessor
= (struct ir3_block
*)entry
->key
;
96 struct ir3_legalize_block_data
*pbd
= predecessor
->data
;
97 struct ir3_legalize_state
*pstate
= &pbd
->state
;
99 /* Our input (ss)/(sy) state is based on OR'ing the output
100 * state of all our predecessor blocks
102 regmask_or(&state
->needs_ss
,
103 &state
->needs_ss
, &pstate
->needs_ss
);
104 regmask_or(&state
->needs_ss_war
,
105 &state
->needs_ss_war
, &pstate
->needs_ss_war
);
106 regmask_or(&state
->needs_sy
,
107 &state
->needs_sy
, &pstate
->needs_sy
);
110 /* remove all the instructions from the list, we'll be adding
111 * them back in as we go
113 list_replace(&block
->instr_list
, &instr_list
);
114 list_inithead(&block
->instr_list
);
116 list_for_each_entry_safe (struct ir3_instruction
, n
, &instr_list
, node
) {
117 struct ir3_register
*reg
;
120 n
->flags
&= ~(IR3_INSTR_SS
| IR3_INSTR_SY
);
122 /* _meta::tex_prefetch instructions removed later in
123 * collect_tex_prefetches()
125 if (is_meta(n
) && (n
->opc
!= OPC_META_TEX_PREFETCH
))
129 struct ir3_register
*inloc
= n
->regs
[1];
130 assert(inloc
->flags
& IR3_REG_IMMED
);
131 ctx
->max_bary
= MAX2(ctx
->max_bary
, inloc
->iim_val
);
134 if (last_n
&& is_barrier(last_n
)) {
135 n
->flags
|= IR3_INSTR_SS
| IR3_INSTR_SY
;
136 last_input_needs_ss
= false;
139 /* NOTE: consider dst register too.. it could happen that
140 * texture sample instruction (for example) writes some
141 * components which are unused. A subsequent instruction
142 * that writes the same register can race w/ the sam instr
143 * resulting in undefined results:
145 for (i
= 0; i
< n
->regs_count
; i
++) {
150 /* TODO: we probably only need (ss) for alu
151 * instr consuming sfu result.. need to make
152 * some tests for both this and (sy)..
154 if (regmask_get(&state
->needs_ss
, reg
)) {
155 n
->flags
|= IR3_INSTR_SS
;
156 last_input_needs_ss
= false;
157 regmask_init(&state
->needs_ss_war
);
158 regmask_init(&state
->needs_ss
);
161 if (regmask_get(&state
->needs_sy
, reg
)) {
162 n
->flags
|= IR3_INSTR_SY
;
163 regmask_init(&state
->needs_sy
);
167 /* TODO: is it valid to have address reg loaded from a
168 * relative src (ie. mova a0, c<a0.x+4>)? If so, the
169 * last_rel check below should be moved ahead of this:
171 if (reg
->flags
& IR3_REG_RELATIV
)
175 if (n
->regs_count
> 0) {
177 if (regmask_get(&state
->needs_ss_war
, reg
)) {
178 n
->flags
|= IR3_INSTR_SS
;
179 last_input_needs_ss
= false;
180 regmask_init(&state
->needs_ss_war
);
181 regmask_init(&state
->needs_ss
);
184 if (last_rel
&& (reg
->num
== regid(REG_A0
, 0))) {
185 last_rel
->flags
|= IR3_INSTR_UL
;
190 /* cat5+ does not have an (ss) bit, if needed we need to
191 * insert a nop to carry the sync flag. Would be kinda
192 * clever if we were aware of this during scheduling, but
193 * this should be a pretty rare case:
195 if ((n
->flags
& IR3_INSTR_SS
) && (opc_cat(n
->opc
) >= 5)) {
196 struct ir3_instruction
*nop
;
197 nop
= ir3_NOP(block
);
198 nop
->flags
|= IR3_INSTR_SS
;
199 n
->flags
&= ~IR3_INSTR_SS
;
202 /* need to be able to set (ss) on first instruction: */
203 if (list_empty(&block
->instr_list
) && (opc_cat(n
->opc
) >= 5))
206 if (is_nop(n
) && !list_empty(&block
->instr_list
)) {
207 struct ir3_instruction
*last
= list_last_entry(&block
->instr_list
,
208 struct ir3_instruction
, node
);
209 if (is_nop(last
) && (last
->repeat
< 5)) {
211 last
->flags
|= n
->flags
;
215 /* NOTE: I think the nopN encoding works for a5xx and
216 * probably a4xx, but not a3xx. So far only tested on
219 if ((ctx
->compiler
->gpu_id
>= 600) && !n
->flags
&& (last
->nop
< 3) &&
220 ((opc_cat(last
->opc
) == 2) || (opc_cat(last
->opc
) == 3))) {
226 if (ctx
->compiler
->samgq_workaround
&&
227 ctx
->type
== MESA_SHADER_VERTEX
&& n
->opc
== OPC_SAMGQ
) {
228 struct ir3_instruction
*samgp
;
230 for (i
= 0; i
< 4; i
++) {
231 samgp
= ir3_instr_clone(n
);
232 samgp
->opc
= OPC_SAMGP0
+ i
;
234 samgp
->flags
|= IR3_INSTR_SY
;
236 list_delinit(&n
->node
);
238 list_addtail(&n
->node
, &block
->instr_list
);
242 regmask_set(&state
->needs_ss
, n
->regs
[0]);
244 if (is_tex(n
) || (n
->opc
== OPC_META_TEX_PREFETCH
)) {
245 regmask_set(&state
->needs_sy
, n
->regs
[0]);
246 ctx
->need_pixlod
= true;
247 if (n
->opc
== OPC_META_TEX_PREFETCH
)
248 has_tex_prefetch
= true;
249 } else if (n
->opc
== OPC_RESINFO
) {
250 regmask_set(&state
->needs_ss
, n
->regs
[0]);
251 ir3_NOP(block
)->flags
|= IR3_INSTR_SS
;
252 last_input_needs_ss
= false;
253 } else if (is_load(n
)) {
254 /* seems like ldlv needs (ss) bit instead?? which is odd but
255 * makes a bunch of flat-varying tests start working on a4xx.
257 if ((n
->opc
== OPC_LDLV
) || (n
->opc
== OPC_LDL
) || (n
->opc
== OPC_LDLW
))
258 regmask_set(&state
->needs_ss
, n
->regs
[0]);
260 regmask_set(&state
->needs_sy
, n
->regs
[0]);
261 } else if (is_atomic(n
->opc
)) {
262 if (n
->flags
& IR3_INSTR_G
) {
263 if (ctx
->compiler
->gpu_id
>= 600) {
264 /* New encoding, returns result via second src: */
265 regmask_set(&state
->needs_sy
, n
->regs
[3]);
267 regmask_set(&state
->needs_sy
, n
->regs
[0]);
270 regmask_set(&state
->needs_ss
, n
->regs
[0]);
274 if (is_ssbo(n
->opc
) || (is_atomic(n
->opc
) && (n
->flags
& IR3_INSTR_G
)))
275 ctx
->has_ssbo
= true;
277 /* both tex/sfu appear to not always immediately consume
278 * their src register(s):
280 if (is_tex(n
) || is_sfu(n
) || is_mem(n
)) {
281 foreach_src(reg
, n
) {
283 regmask_set(&state
->needs_ss_war
, reg
);
289 last_input_needs_ss
|= (n
->opc
== OPC_LDLV
);
296 assert(block
== list_first_entry(&block
->shader
->block_list
,
297 struct ir3_block
, node
));
298 /* special hack.. if using ldlv to bypass interpolation,
299 * we need to insert a dummy bary.f on which we can set
302 if (is_mem(last_input
) && (last_input
->opc
== OPC_LDLV
)) {
303 struct ir3_instruction
*baryf
;
305 /* (ss)bary.f (ei)r63.x, 0, r0.x */
306 baryf
= ir3_instr_create(block
, OPC_BARY_F
);
307 ir3_reg_create(baryf
, regid(63, 0), 0);
308 ir3_reg_create(baryf
, 0, IR3_REG_IMMED
)->iim_val
= 0;
309 ir3_reg_create(baryf
, regid(0, 0), 0);
311 /* insert the dummy bary.f after last_input: */
312 list_delinit(&baryf
->node
);
313 list_add(&baryf
->node
, &last_input
->node
);
317 /* by definition, we need (ss) since we are inserting
318 * the dummy bary.f immediately after the ldlv:
320 last_input_needs_ss
= true;
322 last_input
->regs
[0]->flags
|= IR3_REG_EI
;
323 if (last_input_needs_ss
)
324 last_input
->flags
|= IR3_INSTR_SS
;
325 } else if (has_tex_prefetch
) {
326 /* texture prefetch, but *no* inputs.. we need to insert a
327 * dummy bary.f at the top of the shader to unblock varying
330 struct ir3_instruction
*baryf
;
332 /* (ss)bary.f (ei)r63.x, 0, r0.x */
333 baryf
= ir3_instr_create(block
, OPC_BARY_F
);
334 ir3_reg_create(baryf
, regid(63, 0), 0)->flags
|= IR3_REG_EI
;
335 ir3_reg_create(baryf
, 0, IR3_REG_IMMED
)->iim_val
= 0;
336 ir3_reg_create(baryf
, regid(0, 0), 0);
338 /* insert the dummy bary.f at head: */
339 list_delinit(&baryf
->node
);
340 list_add(&baryf
->node
, &block
->instr_list
);
344 last_rel
->flags
|= IR3_INSTR_UL
;
348 if (memcmp(&prev_state
, state
, sizeof(*state
))) {
349 /* our output state changed, this invalidates all of our
352 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
353 if (!block
->successors
[i
])
355 struct ir3_legalize_block_data
*pbd
= block
->successors
[i
]->data
;
363 /* NOTE: branch instructions are always the last instruction(s)
364 * in the block. We take advantage of this as we resolve the
365 * branches, since "if (foo) break;" constructs turn into
370 * 0029:021: mov.s32s32 r62.x, r1.y
371 * 0082:022: br !p0.x, target=block5
372 * 0083:023: br p0.x, target=block4
373 * // succs: if _[0029:021: mov.s32s32] block4; else block5;
376 * 0084:024: jump, target=block6
380 * 0085:025: jump, target=block7
384 * ie. only instruction in block4/block5 is a jump, so when
385 * resolving branches we can easily detect this by checking
386 * that the first instruction in the target block is itself
387 * a jump, and setup the br directly to the jump's target
388 * (and strip back out the now unreached jump)
390 * TODO sometimes we end up with things like:
394 * add.u r0.y, r0.y, 1
396 * If we swapped the order of the branches, we could drop one.
398 static struct ir3_block
*
399 resolve_dest_block(struct ir3_block
*block
)
401 /* special case for last block: */
402 if (!block
->successors
[0])
405 /* NOTE that we may or may not have inserted the jump
406 * in the target block yet, so conditions to resolve
407 * the dest to the dest block's successor are:
409 * (1) successor[1] == NULL &&
410 * (2) (block-is-empty || only-instr-is-jump)
412 if (block
->successors
[1] == NULL
) {
413 if (list_empty(&block
->instr_list
)) {
414 return block
->successors
[0];
415 } else if (list_length(&block
->instr_list
) == 1) {
416 struct ir3_instruction
*instr
= list_first_entry(
417 &block
->instr_list
, struct ir3_instruction
, node
);
418 if (instr
->opc
== OPC_JUMP
)
419 return block
->successors
[0];
426 remove_unused_block(struct ir3_block
*old_target
)
428 list_delinit(&old_target
->node
);
430 /* cleanup dangling predecessors: */
431 for (unsigned i
= 0; i
< ARRAY_SIZE(old_target
->successors
); i
++) {
432 if (old_target
->successors
[i
]) {
433 struct ir3_block
*succ
= old_target
->successors
[i
];
434 _mesa_set_remove_key(succ
->predecessors
, old_target
);
440 retarget_jump(struct ir3_instruction
*instr
, struct ir3_block
*new_target
)
442 struct ir3_block
*old_target
= instr
->cat0
.target
;
443 struct ir3_block
*cur_block
= instr
->block
;
445 /* update current blocks successors to reflect the retargetting: */
446 if (cur_block
->successors
[0] == old_target
) {
447 cur_block
->successors
[0] = new_target
;
449 debug_assert(cur_block
->successors
[1] == old_target
);
450 cur_block
->successors
[1] = new_target
;
453 /* update new target's predecessors: */
454 _mesa_set_add(new_target
->predecessors
, cur_block
);
456 /* and remove old_target's predecessor: */
457 debug_assert(_mesa_set_search(old_target
->predecessors
, cur_block
));
458 _mesa_set_remove_key(old_target
->predecessors
, cur_block
);
460 if (old_target
->predecessors
->entries
== 0)
461 remove_unused_block(old_target
);
463 instr
->cat0
.target
= new_target
;
467 resolve_jump(struct ir3_instruction
*instr
)
469 struct ir3_block
*tblock
=
470 resolve_dest_block(instr
->cat0
.target
);
471 struct ir3_instruction
*target
;
473 if (tblock
!= instr
->cat0
.target
) {
474 retarget_jump(instr
, tblock
);
478 target
= list_first_entry(&tblock
->instr_list
,
479 struct ir3_instruction
, node
);
481 /* TODO maybe a less fragile way to do this. But we are expecting
482 * a pattern from sched_block() that looks like:
484 * br !p0.x, #else-block
487 * if the first branch target is +2, or if 2nd branch target is +1
488 * then we can just drop the jump.
491 if (instr
->cat0
.inv
== true)
496 if (target
->ip
== (instr
->ip
+ next_block
)) {
497 list_delinit(&instr
->node
);
501 (int)target
->ip
- (int)instr
->ip
;
506 /* resolve jumps, removing jumps/branches to immediately following
507 * instruction which we end up with from earlier stages. Since
508 * removing an instruction can invalidate earlier instruction's
509 * branch offsets, we need to do this iteratively until no more
510 * branches are removed.
513 resolve_jumps(struct ir3
*ir
)
515 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
)
516 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
)
517 if (is_flow(instr
) && instr
->cat0
.target
)
518 if (resolve_jump(instr
))
524 static void mark_jp(struct ir3_block
*block
)
526 struct ir3_instruction
*target
= list_first_entry(&block
->instr_list
,
527 struct ir3_instruction
, node
);
528 target
->flags
|= IR3_INSTR_JP
;
531 /* Mark points where control flow converges or diverges.
533 * Divergence points could actually be re-convergence points where
534 * "parked" threads are recoverged with threads that took the opposite
535 * path last time around. Possibly it is easier to think of (jp) as
536 * "the execution mask might have changed".
539 mark_xvergence_points(struct ir3
*ir
)
541 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
542 if (block
->predecessors
->entries
> 1) {
543 /* if a block has more than one possible predecessor, then
544 * the first instruction is a convergence point.
547 } else if (block
->predecessors
->entries
== 1) {
548 /* If a block has one predecessor, which has multiple possible
549 * successors, it is a divergence point.
551 set_foreach(block
->predecessors
, entry
) {
552 struct ir3_block
*predecessor
= (struct ir3_block
*)entry
->key
;
553 if (predecessor
->successors
[1]) {
562 ir3_legalize(struct ir3
*ir
, bool *has_ssbo
, bool *need_pixlod
, int *max_bary
)
564 struct ir3_legalize_ctx
*ctx
= rzalloc(ir
, struct ir3_legalize_ctx
);
568 ctx
->compiler
= ir
->compiler
;
569 ctx
->type
= ir
->type
;
571 /* allocate per-block data: */
572 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
573 block
->data
= rzalloc(ctx
, struct ir3_legalize_block_data
);
576 /* process each block: */
579 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
580 progress
|= legalize_block(ctx
, block
);
584 *has_ssbo
= ctx
->has_ssbo
;
585 *need_pixlod
= ctx
->need_pixlod
;
586 *max_bary
= ctx
->max_bary
;
589 ir3_count_instructions(ir
);
590 } while(resolve_jumps(ir
));
592 mark_xvergence_points(ir
);