Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/ir3/ir3_legalize.c
4565 views
1
/*
2
* Copyright (C) 2014 Rob Clark <[email protected]>
3
*
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:
10
*
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
13
* Software.
14
*
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
21
* SOFTWARE.
22
*
23
* Authors:
24
* Rob Clark <[email protected]>
25
*/
26
27
#include "util/ralloc.h"
28
#include "util/u_math.h"
29
30
#include "ir3.h"
31
#include "ir3_shader.h"
32
33
/*
34
* Legalize:
35
*
36
* The legalize pass handles ensuring sufficient nop's and sync flags for
37
* correct execution.
38
*
39
* 1) Iteratively determine where sync ((sy)/(ss)) flags are needed,
40
* based on state flowing out of predecessor blocks until there is
41
* no further change. In some cases this requires inserting nops.
42
* 2) Mark (ei) on last varying input, and (ul) on last use of a0.x
43
* 3) Final nop scheduling for instruction latency
44
* 4) Resolve jumps and schedule blocks, marking potential convergence
45
* points with (jp)
46
*/
47
48
struct ir3_legalize_ctx {
49
struct ir3_compiler *compiler;
50
struct ir3_shader_variant *so;
51
gl_shader_stage type;
52
int max_bary;
53
bool early_input_release;
54
};
55
56
struct ir3_legalize_state {
57
regmask_t needs_ss;
58
regmask_t needs_ss_war; /* write after read */
59
regmask_t needs_sy;
60
};
61
62
struct ir3_legalize_block_data {
63
bool valid;
64
struct ir3_legalize_state state;
65
};
66
67
/* We want to evaluate each block from the position of any other
68
* predecessor block, in order that the flags set are the union of
69
* all possible program paths.
70
*
71
* To do this, we need to know the output state (needs_ss/ss_war/sy)
72
* of all predecessor blocks. The tricky thing is loops, which mean
73
* that we can't simply recursively process each predecessor block
74
* before legalizing the current block.
75
*
76
* How we handle that is by looping over all the blocks until the
77
* results converge. If the output state of a given block changes
78
* in a given pass, this means that all successor blocks are not
79
* yet fully legalized.
80
*/
81
82
static bool
83
legalize_block(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
84
{
85
struct ir3_legalize_block_data *bd = block->data;
86
87
if (bd->valid)
88
return false;
89
90
struct ir3_instruction *last_rel = NULL;
91
struct ir3_instruction *last_n = NULL;
92
struct list_head instr_list;
93
struct ir3_legalize_state prev_state = bd->state;
94
struct ir3_legalize_state *state = &bd->state;
95
bool last_input_needs_ss = false;
96
bool has_tex_prefetch = false;
97
bool mergedregs = ctx->so->mergedregs;
98
99
/* our input state is the OR of all predecessor blocks' state: */
100
for (unsigned i = 0; i < block->predecessors_count; i++) {
101
struct ir3_block *predecessor = block->predecessors[i];
102
struct ir3_legalize_block_data *pbd = predecessor->data;
103
struct ir3_legalize_state *pstate = &pbd->state;
104
105
/* Our input (ss)/(sy) state is based on OR'ing the output
106
* state of all our predecessor blocks
107
*/
108
regmask_or(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);
109
regmask_or(&state->needs_ss_war, &state->needs_ss_war,
110
&pstate->needs_ss_war);
111
regmask_or(&state->needs_sy, &state->needs_sy, &pstate->needs_sy);
112
}
113
114
unsigned input_count = 0;
115
116
foreach_instr (n, &block->instr_list) {
117
if (is_input(n)) {
118
input_count++;
119
}
120
}
121
122
unsigned inputs_remaining = input_count;
123
124
/* Either inputs are in the first block or we expect inputs to be released
125
* with the end of the program.
126
*/
127
assert(input_count == 0 || !ctx->early_input_release ||
128
block == ir3_start_block(block->shader));
129
130
/* remove all the instructions from the list, we'll be adding
131
* them back in as we go
132
*/
133
list_replace(&block->instr_list, &instr_list);
134
list_inithead(&block->instr_list);
135
136
foreach_instr_safe (n, &instr_list) {
137
unsigned i;
138
139
n->flags &= ~(IR3_INSTR_SS | IR3_INSTR_SY);
140
141
/* _meta::tex_prefetch instructions removed later in
142
* collect_tex_prefetches()
143
*/
144
if (is_meta(n) && (n->opc != OPC_META_TEX_PREFETCH))
145
continue;
146
147
if (is_input(n)) {
148
struct ir3_register *inloc = n->srcs[0];
149
assert(inloc->flags & IR3_REG_IMMED);
150
ctx->max_bary = MAX2(ctx->max_bary, inloc->iim_val);
151
}
152
153
if (last_n && is_barrier(last_n)) {
154
n->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
155
last_input_needs_ss = false;
156
regmask_init(&state->needs_ss_war, mergedregs);
157
regmask_init(&state->needs_ss, mergedregs);
158
regmask_init(&state->needs_sy, mergedregs);
159
}
160
161
if (last_n && (last_n->opc == OPC_PREDT)) {
162
n->flags |= IR3_INSTR_SS;
163
regmask_init(&state->needs_ss_war, mergedregs);
164
regmask_init(&state->needs_ss, mergedregs);
165
}
166
167
/* NOTE: consider dst register too.. it could happen that
168
* texture sample instruction (for example) writes some
169
* components which are unused. A subsequent instruction
170
* that writes the same register can race w/ the sam instr
171
* resulting in undefined results:
172
*/
173
for (i = 0; i < n->dsts_count + n->srcs_count; i++) {
174
struct ir3_register *reg;
175
if (i < n->dsts_count)
176
reg = n->dsts[i];
177
else
178
reg = n->srcs[i - n->dsts_count];
179
180
if (reg_gpr(reg)) {
181
182
/* TODO: we probably only need (ss) for alu
183
* instr consuming sfu result.. need to make
184
* some tests for both this and (sy)..
185
*/
186
if (regmask_get(&state->needs_ss, reg)) {
187
n->flags |= IR3_INSTR_SS;
188
last_input_needs_ss = false;
189
regmask_init(&state->needs_ss_war, mergedregs);
190
regmask_init(&state->needs_ss, mergedregs);
191
}
192
193
if (regmask_get(&state->needs_sy, reg)) {
194
n->flags |= IR3_INSTR_SY;
195
regmask_init(&state->needs_sy, mergedregs);
196
}
197
}
198
199
/* TODO: is it valid to have address reg loaded from a
200
* relative src (ie. mova a0, c<a0.x+4>)? If so, the
201
* last_rel check below should be moved ahead of this:
202
*/
203
if (reg->flags & IR3_REG_RELATIV)
204
last_rel = n;
205
}
206
207
foreach_dst (reg, n) {
208
if (regmask_get(&state->needs_ss_war, reg)) {
209
n->flags |= IR3_INSTR_SS;
210
last_input_needs_ss = false;
211
regmask_init(&state->needs_ss_war, mergedregs);
212
regmask_init(&state->needs_ss, mergedregs);
213
}
214
215
if (last_rel && (reg->num == regid(REG_A0, 0))) {
216
last_rel->flags |= IR3_INSTR_UL;
217
last_rel = NULL;
218
}
219
}
220
221
/* cat5+ does not have an (ss) bit, if needed we need to
222
* insert a nop to carry the sync flag. Would be kinda
223
* clever if we were aware of this during scheduling, but
224
* this should be a pretty rare case:
225
*/
226
if ((n->flags & IR3_INSTR_SS) && (opc_cat(n->opc) >= 5)) {
227
struct ir3_instruction *nop;
228
nop = ir3_NOP(block);
229
nop->flags |= IR3_INSTR_SS;
230
n->flags &= ~IR3_INSTR_SS;
231
}
232
233
/* need to be able to set (ss) on first instruction: */
234
if (list_is_empty(&block->instr_list) && (opc_cat(n->opc) >= 5))
235
ir3_NOP(block);
236
237
if (ctx->compiler->samgq_workaround && ctx->type == MESA_SHADER_VERTEX &&
238
n->opc == OPC_SAMGQ) {
239
struct ir3_instruction *samgp;
240
241
list_delinit(&n->node);
242
243
for (i = 0; i < 4; i++) {
244
samgp = ir3_instr_clone(n);
245
samgp->opc = OPC_SAMGP0 + i;
246
if (i > 1)
247
samgp->flags |= IR3_INSTR_SY;
248
}
249
} else {
250
list_delinit(&n->node);
251
list_addtail(&n->node, &block->instr_list);
252
}
253
254
if (is_sfu(n))
255
regmask_set(&state->needs_ss, n->dsts[0]);
256
257
if (is_tex_or_prefetch(n)) {
258
regmask_set(&state->needs_sy, n->dsts[0]);
259
if (n->opc == OPC_META_TEX_PREFETCH)
260
has_tex_prefetch = true;
261
} else if (n->opc == OPC_RESINFO) {
262
regmask_set(&state->needs_ss, n->dsts[0]);
263
ir3_NOP(block)->flags |= IR3_INSTR_SS;
264
last_input_needs_ss = false;
265
} else if (is_load(n)) {
266
/* seems like ldlv needs (ss) bit instead?? which is odd but
267
* makes a bunch of flat-varying tests start working on a4xx.
268
*/
269
if ((n->opc == OPC_LDLV) || (n->opc == OPC_LDL) ||
270
(n->opc == OPC_LDLW))
271
regmask_set(&state->needs_ss, n->dsts[0]);
272
else
273
regmask_set(&state->needs_sy, n->dsts[0]);
274
} else if (is_atomic(n->opc)) {
275
if (n->flags & IR3_INSTR_G) {
276
if (ctx->compiler->gpu_id >= 600) {
277
/* New encoding, returns result via second src: */
278
regmask_set(&state->needs_sy, n->srcs[2]);
279
} else {
280
regmask_set(&state->needs_sy, n->dsts[0]);
281
}
282
} else {
283
regmask_set(&state->needs_ss, n->dsts[0]);
284
}
285
}
286
287
if (is_ssbo(n->opc) || (is_atomic(n->opc) && (n->flags & IR3_INSTR_G)))
288
ctx->so->has_ssbo = true;
289
290
/* both tex/sfu appear to not always immediately consume
291
* their src register(s):
292
*/
293
if (is_tex(n) || is_sfu(n) || is_mem(n)) {
294
foreach_src (reg, n) {
295
if (reg_gpr(reg))
296
regmask_set(&state->needs_ss_war, reg);
297
}
298
}
299
300
if (ctx->early_input_release && is_input(n)) {
301
last_input_needs_ss |= (n->opc == OPC_LDLV);
302
303
assert(inputs_remaining > 0);
304
inputs_remaining--;
305
if (inputs_remaining == 0) {
306
/* This is the last input. We add the (ei) flag to release
307
* varying memory after this executes. If it's an ldlv,
308
* however, we need to insert a dummy bary.f on which we can
309
* set the (ei) flag. We may also need to insert an (ss) to
310
* guarantee that all ldlv's have finished fetching their
311
* results before releasing the varying memory.
312
*/
313
struct ir3_instruction *last_input = n;
314
if (n->opc == OPC_LDLV) {
315
struct ir3_instruction *baryf;
316
317
/* (ss)bary.f (ei)r63.x, 0, r0.x */
318
baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);
319
ir3_dst_create(baryf, regid(63, 0), 0);
320
ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
321
ir3_src_create(baryf, regid(0, 0), 0);
322
323
last_input = baryf;
324
}
325
326
last_input->dsts[0]->flags |= IR3_REG_EI;
327
if (last_input_needs_ss) {
328
last_input->flags |= IR3_INSTR_SS;
329
regmask_init(&state->needs_ss_war, mergedregs);
330
regmask_init(&state->needs_ss, mergedregs);
331
}
332
}
333
}
334
335
last_n = n;
336
}
337
338
assert(inputs_remaining == 0 || !ctx->early_input_release);
339
340
if (has_tex_prefetch && input_count == 0) {
341
/* texture prefetch, but *no* inputs.. we need to insert a
342
* dummy bary.f at the top of the shader to unblock varying
343
* storage:
344
*/
345
struct ir3_instruction *baryf;
346
347
/* (ss)bary.f (ei)r63.x, 0, r0.x */
348
baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);
349
ir3_dst_create(baryf, regid(63, 0), 0)->flags |= IR3_REG_EI;
350
ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
351
ir3_src_create(baryf, regid(0, 0), 0);
352
353
/* insert the dummy bary.f at head: */
354
list_delinit(&baryf->node);
355
list_add(&baryf->node, &block->instr_list);
356
}
357
358
if (last_rel)
359
last_rel->flags |= IR3_INSTR_UL;
360
361
bd->valid = true;
362
363
if (memcmp(&prev_state, state, sizeof(*state))) {
364
/* our output state changed, this invalidates all of our
365
* successors:
366
*/
367
for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
368
if (!block->successors[i])
369
break;
370
struct ir3_legalize_block_data *pbd = block->successors[i]->data;
371
pbd->valid = false;
372
}
373
}
374
375
return true;
376
}
377
378
/* Expands dsxpp and dsypp macros to:
379
*
380
* dsxpp.1 dst, src
381
* dsxpp.1.p dst, src
382
*
383
* We apply this after flags syncing, as we don't want to sync in between the
384
* two (which might happen if dst == src). We do it before nop scheduling
385
* because that needs to count actual instructions.
386
*/
387
static bool
388
apply_fine_deriv_macro(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
389
{
390
struct list_head instr_list;
391
392
/* remove all the instructions from the list, we'll be adding
393
* them back in as we go
394
*/
395
list_replace(&block->instr_list, &instr_list);
396
list_inithead(&block->instr_list);
397
398
foreach_instr_safe (n, &instr_list) {
399
list_addtail(&n->node, &block->instr_list);
400
401
if (n->opc == OPC_DSXPP_MACRO || n->opc == OPC_DSYPP_MACRO) {
402
n->opc = (n->opc == OPC_DSXPP_MACRO) ? OPC_DSXPP_1 : OPC_DSYPP_1;
403
404
struct ir3_instruction *op_p = ir3_instr_clone(n);
405
op_p->flags = IR3_INSTR_P;
406
407
ctx->so->need_fine_derivatives = true;
408
}
409
}
410
411
return true;
412
}
413
414
/* NOTE: branch instructions are always the last instruction(s)
415
* in the block. We take advantage of this as we resolve the
416
* branches, since "if (foo) break;" constructs turn into
417
* something like:
418
*
419
* block3 {
420
* ...
421
* 0029:021: mov.s32s32 r62.x, r1.y
422
* 0082:022: br !p0.x, target=block5
423
* 0083:023: br p0.x, target=block4
424
* // succs: if _[0029:021: mov.s32s32] block4; else block5;
425
* }
426
* block4 {
427
* 0084:024: jump, target=block6
428
* // succs: block6;
429
* }
430
* block5 {
431
* 0085:025: jump, target=block7
432
* // succs: block7;
433
* }
434
*
435
* ie. only instruction in block4/block5 is a jump, so when
436
* resolving branches we can easily detect this by checking
437
* that the first instruction in the target block is itself
438
* a jump, and setup the br directly to the jump's target
439
* (and strip back out the now unreached jump)
440
*
441
* TODO sometimes we end up with things like:
442
*
443
* br !p0.x, #2
444
* br p0.x, #12
445
* add.u r0.y, r0.y, 1
446
*
447
* If we swapped the order of the branches, we could drop one.
448
*/
449
static struct ir3_block *
450
resolve_dest_block(struct ir3_block *block)
451
{
452
/* special case for last block: */
453
if (!block->successors[0])
454
return block;
455
456
/* NOTE that we may or may not have inserted the jump
457
* in the target block yet, so conditions to resolve
458
* the dest to the dest block's successor are:
459
*
460
* (1) successor[1] == NULL &&
461
* (2) (block-is-empty || only-instr-is-jump)
462
*/
463
if (block->successors[1] == NULL) {
464
if (list_is_empty(&block->instr_list)) {
465
return block->successors[0];
466
} else if (list_length(&block->instr_list) == 1) {
467
struct ir3_instruction *instr =
468
list_first_entry(&block->instr_list, struct ir3_instruction, node);
469
if (instr->opc == OPC_JUMP) {
470
/* If this jump is backwards, then we will probably convert
471
* the jump being resolved to a backwards jump, which will
472
* change a loop-with-continue or loop-with-if into a
473
* doubly-nested loop and change the convergence behavior.
474
* Disallow this here.
475
*/
476
if (block->successors[0]->index <= block->index)
477
return block;
478
return block->successors[0];
479
}
480
}
481
}
482
return block;
483
}
484
485
static void
486
remove_unused_block(struct ir3_block *old_target)
487
{
488
list_delinit(&old_target->node);
489
490
/* cleanup dangling predecessors: */
491
for (unsigned i = 0; i < ARRAY_SIZE(old_target->successors); i++) {
492
if (old_target->successors[i]) {
493
struct ir3_block *succ = old_target->successors[i];
494
ir3_block_remove_predecessor(succ, old_target);
495
}
496
}
497
}
498
499
static bool
500
retarget_jump(struct ir3_instruction *instr, struct ir3_block *new_target)
501
{
502
struct ir3_block *old_target = instr->cat0.target;
503
struct ir3_block *cur_block = instr->block;
504
505
/* update current blocks successors to reflect the retargetting: */
506
if (cur_block->successors[0] == old_target) {
507
cur_block->successors[0] = new_target;
508
} else {
509
debug_assert(cur_block->successors[1] == old_target);
510
cur_block->successors[1] = new_target;
511
}
512
513
/* update new target's predecessors: */
514
ir3_block_add_predecessor(new_target, cur_block);
515
516
/* and remove old_target's predecessor: */
517
ir3_block_remove_predecessor(old_target, cur_block);
518
519
instr->cat0.target = new_target;
520
521
if (old_target->predecessors_count == 0) {
522
remove_unused_block(old_target);
523
return true;
524
}
525
526
return false;
527
}
528
529
static bool
530
opt_jump(struct ir3 *ir)
531
{
532
bool progress = false;
533
534
unsigned index = 0;
535
foreach_block (block, &ir->block_list)
536
block->index = index++;
537
538
foreach_block (block, &ir->block_list) {
539
foreach_instr (instr, &block->instr_list) {
540
if (!is_flow(instr) || !instr->cat0.target)
541
continue;
542
543
struct ir3_block *tblock = resolve_dest_block(instr->cat0.target);
544
if (tblock != instr->cat0.target) {
545
progress = true;
546
547
/* Exit early if we deleted a block to avoid iterator
548
* weirdness/assert fails
549
*/
550
if (retarget_jump(instr, tblock))
551
return true;
552
}
553
}
554
555
/* Detect the case where the block ends either with:
556
* - A single unconditional jump to the next block.
557
* - Two jump instructions with opposite conditions, and one of the
558
* them jumps to the next block.
559
* We can remove the one that jumps to the next block in either case.
560
*/
561
if (list_is_empty(&block->instr_list))
562
continue;
563
564
struct ir3_instruction *jumps[2] = {NULL, NULL};
565
jumps[0] =
566
list_last_entry(&block->instr_list, struct ir3_instruction, node);
567
if (!list_is_singular(&block->instr_list))
568
jumps[1] =
569
list_last_entry(&jumps[0]->node, struct ir3_instruction, node);
570
571
if (jumps[0]->opc == OPC_JUMP)
572
jumps[1] = NULL;
573
else if (jumps[0]->opc != OPC_B || !jumps[1] || jumps[1]->opc != OPC_B)
574
continue;
575
576
for (unsigned i = 0; i < 2; i++) {
577
if (!jumps[i])
578
continue;
579
580
struct ir3_block *tblock = jumps[i]->cat0.target;
581
if (&tblock->node == block->node.next) {
582
list_delinit(&jumps[i]->node);
583
progress = true;
584
break;
585
}
586
}
587
}
588
589
return progress;
590
}
591
592
static void
593
resolve_jumps(struct ir3 *ir)
594
{
595
foreach_block (block, &ir->block_list)
596
foreach_instr (instr, &block->instr_list)
597
if (is_flow(instr) && instr->cat0.target) {
598
struct ir3_instruction *target = list_first_entry(
599
&instr->cat0.target->instr_list, struct ir3_instruction, node);
600
601
instr->cat0.immed = (int)target->ip - (int)instr->ip;
602
}
603
}
604
605
static void
606
mark_jp(struct ir3_block *block)
607
{
608
struct ir3_instruction *target =
609
list_first_entry(&block->instr_list, struct ir3_instruction, node);
610
target->flags |= IR3_INSTR_JP;
611
}
612
613
/* Mark points where control flow converges or diverges.
614
*
615
* Divergence points could actually be re-convergence points where
616
* "parked" threads are recoverged with threads that took the opposite
617
* path last time around. Possibly it is easier to think of (jp) as
618
* "the execution mask might have changed".
619
*/
620
static void
621
mark_xvergence_points(struct ir3 *ir)
622
{
623
foreach_block (block, &ir->block_list) {
624
if (block->predecessors_count > 1) {
625
/* if a block has more than one possible predecessor, then
626
* the first instruction is a convergence point.
627
*/
628
mark_jp(block);
629
} else if (block->predecessors_count == 1) {
630
/* If a block has one predecessor, which has multiple possible
631
* successors, it is a divergence point.
632
*/
633
for (unsigned i = 0; i < block->predecessors_count; i++) {
634
struct ir3_block *predecessor = block->predecessors[i];
635
if (predecessor->successors[1]) {
636
mark_jp(block);
637
}
638
}
639
}
640
}
641
}
642
643
/* Insert the branch/jump instructions for flow control between blocks.
644
* Initially this is done naively, without considering if the successor
645
* block immediately follows the current block (ie. so no jump required),
646
* but that is cleaned up in opt_jump().
647
*
648
* TODO what ensures that the last write to p0.x in a block is the
649
* branch condition? Have we been getting lucky all this time?
650
*/
651
static void
652
block_sched(struct ir3 *ir)
653
{
654
foreach_block (block, &ir->block_list) {
655
if (block->successors[1]) {
656
/* if/else, conditional branches to "then" or "else": */
657
struct ir3_instruction *br1, *br2;
658
659
if (block->brtype == IR3_BRANCH_GETONE) {
660
/* getone can't be inverted, and it wouldn't even make sense
661
* to follow it with an inverted branch, so follow it by an
662
* unconditional branch.
663
*/
664
debug_assert(!block->condition);
665
br1 = ir3_GETONE(block);
666
br1->cat0.target = block->successors[1];
667
668
br2 = ir3_JUMP(block);
669
br2->cat0.target = block->successors[0];
670
} else {
671
debug_assert(block->condition);
672
673
/* create "else" branch first (since "then" block should
674
* frequently/always end up being a fall-thru):
675
*/
676
br1 = ir3_instr_create(block, OPC_B, 0, 1);
677
ir3_src_create(br1, regid(REG_P0, 0), 0)->def =
678
block->condition->dsts[0];
679
br1->cat0.inv1 = true;
680
br1->cat0.target = block->successors[1];
681
682
/* "then" branch: */
683
br2 = ir3_instr_create(block, OPC_B, 0, 1);
684
ir3_src_create(br2, regid(REG_P0, 0), 0)->def =
685
block->condition->dsts[0];
686
br2->cat0.target = block->successors[0];
687
688
switch (block->brtype) {
689
case IR3_BRANCH_COND:
690
br1->cat0.brtype = br2->cat0.brtype = BRANCH_PLAIN;
691
break;
692
case IR3_BRANCH_ALL:
693
br1->cat0.brtype = BRANCH_ANY;
694
br2->cat0.brtype = BRANCH_ALL;
695
break;
696
case IR3_BRANCH_ANY:
697
br1->cat0.brtype = BRANCH_ALL;
698
br2->cat0.brtype = BRANCH_ANY;
699
break;
700
case IR3_BRANCH_GETONE:
701
unreachable("can't get here");
702
}
703
}
704
} else if (block->successors[0]) {
705
/* otherwise unconditional jump to next block: */
706
struct ir3_instruction *jmp;
707
708
jmp = ir3_JUMP(block);
709
jmp->cat0.target = block->successors[0];
710
}
711
}
712
}
713
714
/* Here we workaround the fact that kill doesn't actually kill the thread as
715
* GL expects. The last instruction always needs to be an end instruction,
716
* which means that if we're stuck in a loop where kill is the only way out,
717
* then we may have to jump out to the end. kill may also have the d3d
718
* semantics of converting the thread to a helper thread, rather than setting
719
* the exec mask to 0, in which case the helper thread could get stuck in an
720
* infinite loop.
721
*
722
* We do this late, both to give the scheduler the opportunity to reschedule
723
* kill instructions earlier and to avoid having to create a separate basic
724
* block.
725
*
726
* TODO: Assuming that the wavefront doesn't stop as soon as all threads are
727
* killed, we might benefit by doing this more aggressively when the remaining
728
* part of the program after the kill is large, since that would let us
729
* skip over the instructions when there are no non-killed threads left.
730
*/
731
static void
732
kill_sched(struct ir3 *ir, struct ir3_shader_variant *so)
733
{
734
/* True if we know that this block will always eventually lead to the end
735
* block:
736
*/
737
bool always_ends = true;
738
bool added = false;
739
struct ir3_block *last_block =
740
list_last_entry(&ir->block_list, struct ir3_block, node);
741
742
foreach_block_rev (block, &ir->block_list) {
743
for (unsigned i = 0; i < 2 && block->successors[i]; i++) {
744
if (block->successors[i]->start_ip <= block->end_ip)
745
always_ends = false;
746
}
747
748
if (always_ends)
749
continue;
750
751
foreach_instr_safe (instr, &block->instr_list) {
752
if (instr->opc != OPC_KILL)
753
continue;
754
755
struct ir3_instruction *br = ir3_instr_create(block, OPC_B, 0, 1);
756
ir3_src_create(br, instr->srcs[0]->num, instr->srcs[0]->flags)->wrmask =
757
1;
758
br->cat0.target =
759
list_last_entry(&ir->block_list, struct ir3_block, node);
760
761
list_del(&br->node);
762
list_add(&br->node, &instr->node);
763
764
added = true;
765
}
766
}
767
768
if (added) {
769
/* I'm not entirely sure how the branchstack works, but we probably
770
* need to add at least one entry for the divergence which is resolved
771
* at the end:
772
*/
773
so->branchstack++;
774
775
/* We don't update predecessors/successors, so we have to do this
776
* manually:
777
*/
778
mark_jp(last_block);
779
}
780
}
781
782
/* Insert nop's required to make this a legal/valid shader program: */
783
static void
784
nop_sched(struct ir3 *ir, struct ir3_shader_variant *so)
785
{
786
foreach_block (block, &ir->block_list) {
787
struct ir3_instruction *last = NULL;
788
struct list_head instr_list;
789
790
/* remove all the instructions from the list, we'll be adding
791
* them back in as we go
792
*/
793
list_replace(&block->instr_list, &instr_list);
794
list_inithead(&block->instr_list);
795
796
foreach_instr_safe (instr, &instr_list) {
797
unsigned delay = ir3_delay_calc_exact(block, instr, so->mergedregs);
798
799
/* NOTE: I think the nopN encoding works for a5xx and
800
* probably a4xx, but not a3xx. So far only tested on
801
* a6xx.
802
*/
803
804
if ((delay > 0) && (ir->compiler->gpu_id >= 600) && last &&
805
((opc_cat(last->opc) == 2) || (opc_cat(last->opc) == 3)) &&
806
(last->repeat == 0)) {
807
/* the previous cat2/cat3 instruction can encode at most 3 nop's: */
808
unsigned transfer = MIN2(delay, 3 - last->nop);
809
last->nop += transfer;
810
delay -= transfer;
811
}
812
813
if ((delay > 0) && last && (last->opc == OPC_NOP)) {
814
/* the previous nop can encode at most 5 repeats: */
815
unsigned transfer = MIN2(delay, 5 - last->repeat);
816
last->repeat += transfer;
817
delay -= transfer;
818
}
819
820
if (delay > 0) {
821
debug_assert(delay <= 6);
822
ir3_NOP(block)->repeat = delay - 1;
823
}
824
825
list_addtail(&instr->node, &block->instr_list);
826
last = instr;
827
}
828
}
829
}
830
831
bool
832
ir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary)
833
{
834
struct ir3_legalize_ctx *ctx = rzalloc(ir, struct ir3_legalize_ctx);
835
bool mergedregs = so->mergedregs;
836
bool progress;
837
838
ctx->so = so;
839
ctx->max_bary = -1;
840
ctx->compiler = ir->compiler;
841
ctx->type = ir->type;
842
843
/* allocate per-block data: */
844
foreach_block (block, &ir->block_list) {
845
struct ir3_legalize_block_data *bd =
846
rzalloc(ctx, struct ir3_legalize_block_data);
847
848
regmask_init(&bd->state.needs_ss_war, mergedregs);
849
regmask_init(&bd->state.needs_ss, mergedregs);
850
regmask_init(&bd->state.needs_sy, mergedregs);
851
852
block->data = bd;
853
}
854
855
ir3_remove_nops(ir);
856
857
/* We may have failed to pull all input loads into the first block.
858
* In such case at the moment we aren't able to find a better place
859
* to for (ei) than the end of the program.
860
* a5xx and a6xx do automatically release varying storage at the end.
861
*/
862
ctx->early_input_release = true;
863
struct ir3_block *start_block = ir3_start_block(ir);
864
foreach_block (block, &ir->block_list) {
865
foreach_instr (instr, &block->instr_list) {
866
if (is_input(instr) && block != start_block) {
867
ctx->early_input_release = false;
868
break;
869
}
870
}
871
}
872
873
assert(ctx->early_input_release || ctx->compiler->gpu_id > 500);
874
875
/* process each block: */
876
do {
877
progress = false;
878
foreach_block (block, &ir->block_list) {
879
progress |= legalize_block(ctx, block);
880
}
881
} while (progress);
882
883
*max_bary = ctx->max_bary;
884
885
block_sched(ir);
886
if (so->type == MESA_SHADER_FRAGMENT)
887
kill_sched(ir, so);
888
889
foreach_block (block, &ir->block_list) {
890
progress |= apply_fine_deriv_macro(ctx, block);
891
}
892
893
nop_sched(ir, so);
894
895
while (opt_jump(ir))
896
;
897
898
ir3_count_instructions(ir);
899
resolve_jumps(ir);
900
901
mark_xvergence_points(ir);
902
903
ralloc_free(ctx);
904
905
return true;
906
}
907
908