Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/nir/nir_from_ssa.c
4549 views
1
/*
2
* Copyright © 2014 Intel Corporation
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
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*
23
* Authors:
24
* Jason Ekstrand ([email protected])
25
*
26
*/
27
28
#include "nir.h"
29
#include "nir_builder.h"
30
#include "nir_vla.h"
31
32
/*
33
* This file implements an out-of-SSA pass as described in "Revisiting
34
* Out-of-SSA Translation for Correctness, Code Quality, and Efficiency" by
35
* Boissinot et al.
36
*/
37
38
struct from_ssa_state {
39
nir_builder builder;
40
void *dead_ctx;
41
bool phi_webs_only;
42
struct hash_table *merge_node_table;
43
nir_instr *instr;
44
bool progress;
45
};
46
47
/* Returns if def @a comes after def @b.
48
*
49
* The core observation that makes the Boissinot algorithm efficient
50
* is that, given two properly sorted sets, we can check for
51
* interference in these sets via a linear walk. This is accomplished
52
* by doing single combined walk over union of the two sets in DFS
53
* order. It doesn't matter what DFS we do so long as we're
54
* consistent. Fortunately, the dominance algorithm we ran prior to
55
* this pass did such a walk and recorded the pre- and post-indices in
56
* the blocks.
57
*
58
* We treat SSA undefs as always coming before other instruction types.
59
*/
60
static bool
61
def_after(nir_ssa_def *a, nir_ssa_def *b)
62
{
63
if (a->parent_instr->type == nir_instr_type_ssa_undef)
64
return false;
65
66
if (b->parent_instr->type == nir_instr_type_ssa_undef)
67
return true;
68
69
/* If they're in the same block, we can rely on whichever instruction
70
* comes first in the block.
71
*/
72
if (a->parent_instr->block == b->parent_instr->block)
73
return a->parent_instr->index > b->parent_instr->index;
74
75
/* Otherwise, if blocks are distinct, we sort them in DFS pre-order */
76
return a->parent_instr->block->dom_pre_index >
77
b->parent_instr->block->dom_pre_index;
78
}
79
80
/* Returns true if a dominates b */
81
static bool
82
ssa_def_dominates(nir_ssa_def *a, nir_ssa_def *b)
83
{
84
if (a->parent_instr->type == nir_instr_type_ssa_undef) {
85
/* SSA undefs always dominate */
86
return true;
87
} if (def_after(a, b)) {
88
return false;
89
} else if (a->parent_instr->block == b->parent_instr->block) {
90
return def_after(b, a);
91
} else {
92
return nir_block_dominates(a->parent_instr->block,
93
b->parent_instr->block);
94
}
95
}
96
97
98
/* The following data structure, which I have named merge_set is a way of
99
* representing a set registers of non-interfering registers. This is
100
* based on the concept of a "dominance forest" presented in "Fast Copy
101
* Coalescing and Live-Range Identification" by Budimlic et al. but the
102
* implementation concept is taken from "Revisiting Out-of-SSA Translation
103
* for Correctness, Code Quality, and Efficiency" by Boissinot et al.
104
*
105
* Each SSA definition is associated with a merge_node and the association
106
* is represented by a combination of a hash table and the "def" parameter
107
* in the merge_node structure. The merge_set stores a linked list of
108
* merge_nodes in dominance order of the ssa definitions. (Since the
109
* liveness analysis pass indexes the SSA values in dominance order for us,
110
* this is an easy thing to keep up.) It is assumed that no pair of the
111
* nodes in a given set interfere. Merging two sets or checking for
112
* interference can be done in a single linear-time merge-sort walk of the
113
* two lists of nodes.
114
*/
115
struct merge_set;
116
117
typedef struct {
118
struct exec_node node;
119
struct merge_set *set;
120
nir_ssa_def *def;
121
} merge_node;
122
123
typedef struct merge_set {
124
struct exec_list nodes;
125
unsigned size;
126
bool divergent;
127
nir_register *reg;
128
} merge_set;
129
130
#if 0
131
static void
132
merge_set_dump(merge_set *set, FILE *fp)
133
{
134
nir_ssa_def *dom[set->size];
135
int dom_idx = -1;
136
137
foreach_list_typed(merge_node, node, node, &set->nodes) {
138
while (dom_idx >= 0 && !ssa_def_dominates(dom[dom_idx], node->def))
139
dom_idx--;
140
141
for (int i = 0; i <= dom_idx; i++)
142
fprintf(fp, " ");
143
144
fprintf(fp, "ssa_%d\n", node->def->index);
145
146
dom[++dom_idx] = node->def;
147
}
148
}
149
#endif
150
151
static merge_node *
152
get_merge_node(nir_ssa_def *def, struct from_ssa_state *state)
153
{
154
struct hash_entry *entry =
155
_mesa_hash_table_search(state->merge_node_table, def);
156
if (entry)
157
return entry->data;
158
159
merge_set *set = ralloc(state->dead_ctx, merge_set);
160
exec_list_make_empty(&set->nodes);
161
set->size = 1;
162
set->divergent = def->divergent;
163
set->reg = NULL;
164
165
merge_node *node = ralloc(state->dead_ctx, merge_node);
166
node->set = set;
167
node->def = def;
168
exec_list_push_head(&set->nodes, &node->node);
169
170
_mesa_hash_table_insert(state->merge_node_table, def, node);
171
172
return node;
173
}
174
175
static bool
176
merge_nodes_interfere(merge_node *a, merge_node *b)
177
{
178
/* There's no need to check for interference within the same set,
179
* because we assume, that sets themselves are already
180
* interference-free.
181
*/
182
if (a->set == b->set)
183
return false;
184
185
return nir_ssa_defs_interfere(a->def, b->def);
186
}
187
188
/* Merges b into a */
189
static merge_set *
190
merge_merge_sets(merge_set *a, merge_set *b)
191
{
192
struct exec_node *an = exec_list_get_head(&a->nodes);
193
struct exec_node *bn = exec_list_get_head(&b->nodes);
194
while (!exec_node_is_tail_sentinel(bn)) {
195
merge_node *a_node = exec_node_data(merge_node, an, node);
196
merge_node *b_node = exec_node_data(merge_node, bn, node);
197
198
if (exec_node_is_tail_sentinel(an) ||
199
def_after(a_node->def, b_node->def)) {
200
struct exec_node *next = bn->next;
201
exec_node_remove(bn);
202
exec_node_insert_node_before(an, bn);
203
exec_node_data(merge_node, bn, node)->set = a;
204
bn = next;
205
} else {
206
an = an->next;
207
}
208
}
209
210
a->size += b->size;
211
b->size = 0;
212
a->divergent |= b->divergent;
213
214
return a;
215
}
216
217
/* Checks for any interference between two merge sets
218
*
219
* This is an implementation of Algorithm 2 in "Revisiting Out-of-SSA
220
* Translation for Correctness, Code Quality, and Efficiency" by
221
* Boissinot et al.
222
*/
223
static bool
224
merge_sets_interfere(merge_set *a, merge_set *b)
225
{
226
NIR_VLA(merge_node *, dom, a->size + b->size);
227
int dom_idx = -1;
228
229
struct exec_node *an = exec_list_get_head(&a->nodes);
230
struct exec_node *bn = exec_list_get_head(&b->nodes);
231
while (!exec_node_is_tail_sentinel(an) ||
232
!exec_node_is_tail_sentinel(bn)) {
233
234
merge_node *current;
235
if (exec_node_is_tail_sentinel(an)) {
236
current = exec_node_data(merge_node, bn, node);
237
bn = bn->next;
238
} else if (exec_node_is_tail_sentinel(bn)) {
239
current = exec_node_data(merge_node, an, node);
240
an = an->next;
241
} else {
242
merge_node *a_node = exec_node_data(merge_node, an, node);
243
merge_node *b_node = exec_node_data(merge_node, bn, node);
244
245
if (def_after(b_node->def, a_node->def)) {
246
current = a_node;
247
an = an->next;
248
} else {
249
current = b_node;
250
bn = bn->next;
251
}
252
}
253
254
while (dom_idx >= 0 &&
255
!ssa_def_dominates(dom[dom_idx]->def, current->def))
256
dom_idx--;
257
258
if (dom_idx >= 0 && merge_nodes_interfere(current, dom[dom_idx]))
259
return true;
260
261
dom[++dom_idx] = current;
262
}
263
264
return false;
265
}
266
267
static bool
268
add_parallel_copy_to_end_of_block(nir_block *block, void *dead_ctx)
269
{
270
271
bool need_end_copy = false;
272
if (block->successors[0]) {
273
nir_instr *instr = nir_block_first_instr(block->successors[0]);
274
if (instr && instr->type == nir_instr_type_phi)
275
need_end_copy = true;
276
}
277
278
if (block->successors[1]) {
279
nir_instr *instr = nir_block_first_instr(block->successors[1]);
280
if (instr && instr->type == nir_instr_type_phi)
281
need_end_copy = true;
282
}
283
284
if (need_end_copy) {
285
/* If one of our successors has at least one phi node, we need to
286
* create a parallel copy at the end of the block but before the jump
287
* (if there is one).
288
*/
289
nir_parallel_copy_instr *pcopy =
290
nir_parallel_copy_instr_create(dead_ctx);
291
292
nir_instr_insert(nir_after_block_before_jump(block), &pcopy->instr);
293
}
294
295
return true;
296
}
297
298
static nir_parallel_copy_instr *
299
get_parallel_copy_at_end_of_block(nir_block *block)
300
{
301
nir_instr *last_instr = nir_block_last_instr(block);
302
if (last_instr == NULL)
303
return NULL;
304
305
/* The last instruction may be a jump in which case the parallel copy is
306
* right before it.
307
*/
308
if (last_instr->type == nir_instr_type_jump)
309
last_instr = nir_instr_prev(last_instr);
310
311
if (last_instr && last_instr->type == nir_instr_type_parallel_copy)
312
return nir_instr_as_parallel_copy(last_instr);
313
else
314
return NULL;
315
}
316
317
/** Isolate phi nodes with parallel copies
318
*
319
* In order to solve the dependency problems with the sources and
320
* destinations of phi nodes, we first isolate them by adding parallel
321
* copies to the beginnings and ends of basic blocks. For every block with
322
* phi nodes, we add a parallel copy immediately following the last phi
323
* node that copies the destinations of all of the phi nodes to new SSA
324
* values. We also add a parallel copy to the end of every block that has
325
* a successor with phi nodes that, for each phi node in each successor,
326
* copies the corresponding sorce of the phi node and adjust the phi to
327
* used the destination of the parallel copy.
328
*
329
* In SSA form, each value has exactly one definition. What this does is
330
* ensure that each value used in a phi also has exactly one use. The
331
* destinations of phis are only used by the parallel copy immediately
332
* following the phi nodes and. Thanks to the parallel copy at the end of
333
* the predecessor block, the sources of phi nodes are are the only use of
334
* that value. This allows us to immediately assign all the sources and
335
* destinations of any given phi node to the same register without worrying
336
* about interference at all. We do coalescing to get rid of the parallel
337
* copies where possible.
338
*
339
* Before this pass can be run, we have to iterate over the blocks with
340
* add_parallel_copy_to_end_of_block to ensure that the parallel copies at
341
* the ends of blocks exist. We can create the ones at the beginnings as
342
* we go, but the ones at the ends of blocks need to be created ahead of
343
* time because of potential back-edges in the CFG.
344
*/
345
static bool
346
isolate_phi_nodes_block(nir_block *block, void *dead_ctx)
347
{
348
nir_instr *last_phi_instr = NULL;
349
nir_foreach_instr(instr, block) {
350
/* Phi nodes only ever come at the start of a block */
351
if (instr->type != nir_instr_type_phi)
352
break;
353
354
last_phi_instr = instr;
355
}
356
357
/* If we don't have any phis, then there's nothing for us to do. */
358
if (last_phi_instr == NULL)
359
return true;
360
361
/* If we have phi nodes, we need to create a parallel copy at the
362
* start of this block but after the phi nodes.
363
*/
364
nir_parallel_copy_instr *block_pcopy =
365
nir_parallel_copy_instr_create(dead_ctx);
366
nir_instr_insert_after(last_phi_instr, &block_pcopy->instr);
367
368
nir_foreach_instr(instr, block) {
369
/* Phi nodes only ever come at the start of a block */
370
if (instr->type != nir_instr_type_phi)
371
break;
372
373
nir_phi_instr *phi = nir_instr_as_phi(instr);
374
assert(phi->dest.is_ssa);
375
nir_foreach_phi_src(src, phi) {
376
nir_parallel_copy_instr *pcopy =
377
get_parallel_copy_at_end_of_block(src->pred);
378
assert(pcopy);
379
380
nir_parallel_copy_entry *entry = rzalloc(dead_ctx,
381
nir_parallel_copy_entry);
382
nir_ssa_dest_init(&pcopy->instr, &entry->dest,
383
phi->dest.ssa.num_components,
384
phi->dest.ssa.bit_size, NULL);
385
entry->dest.ssa.divergent = nir_src_is_divergent(src->src);
386
exec_list_push_tail(&pcopy->entries, &entry->node);
387
388
assert(src->src.is_ssa);
389
nir_instr_rewrite_src(&pcopy->instr, &entry->src, src->src);
390
391
nir_instr_rewrite_src(&phi->instr, &src->src,
392
nir_src_for_ssa(&entry->dest.ssa));
393
}
394
395
nir_parallel_copy_entry *entry = rzalloc(dead_ctx,
396
nir_parallel_copy_entry);
397
nir_ssa_dest_init(&block_pcopy->instr, &entry->dest,
398
phi->dest.ssa.num_components, phi->dest.ssa.bit_size,
399
NULL);
400
entry->dest.ssa.divergent = phi->dest.ssa.divergent;
401
exec_list_push_tail(&block_pcopy->entries, &entry->node);
402
403
nir_ssa_def_rewrite_uses(&phi->dest.ssa,
404
&entry->dest.ssa);
405
406
nir_instr_rewrite_src(&block_pcopy->instr, &entry->src,
407
nir_src_for_ssa(&phi->dest.ssa));
408
}
409
410
return true;
411
}
412
413
static bool
414
coalesce_phi_nodes_block(nir_block *block, struct from_ssa_state *state)
415
{
416
nir_foreach_instr(instr, block) {
417
/* Phi nodes only ever come at the start of a block */
418
if (instr->type != nir_instr_type_phi)
419
break;
420
421
nir_phi_instr *phi = nir_instr_as_phi(instr);
422
423
assert(phi->dest.is_ssa);
424
merge_node *dest_node = get_merge_node(&phi->dest.ssa, state);
425
426
nir_foreach_phi_src(src, phi) {
427
assert(src->src.is_ssa);
428
merge_node *src_node = get_merge_node(src->src.ssa, state);
429
if (src_node->set != dest_node->set)
430
merge_merge_sets(dest_node->set, src_node->set);
431
}
432
}
433
434
return true;
435
}
436
437
static void
438
aggressive_coalesce_parallel_copy(nir_parallel_copy_instr *pcopy,
439
struct from_ssa_state *state)
440
{
441
nir_foreach_parallel_copy_entry(entry, pcopy) {
442
if (!entry->src.is_ssa)
443
continue;
444
445
/* Since load_const instructions are SSA only, we can't replace their
446
* destinations with registers and, therefore, can't coalesce them.
447
*/
448
if (entry->src.ssa->parent_instr->type == nir_instr_type_load_const)
449
continue;
450
451
/* Don't try and coalesce these */
452
if (entry->dest.ssa.num_components != entry->src.ssa->num_components)
453
continue;
454
455
merge_node *src_node = get_merge_node(entry->src.ssa, state);
456
merge_node *dest_node = get_merge_node(&entry->dest.ssa, state);
457
458
if (src_node->set == dest_node->set)
459
continue;
460
461
/* TODO: We can probably do better here but for now we should be safe if
462
* we just don't coalesce things with different divergence.
463
*/
464
if (dest_node->set->divergent != src_node->set->divergent)
465
continue;
466
467
if (!merge_sets_interfere(src_node->set, dest_node->set))
468
merge_merge_sets(src_node->set, dest_node->set);
469
}
470
}
471
472
static bool
473
aggressive_coalesce_block(nir_block *block, struct from_ssa_state *state)
474
{
475
nir_parallel_copy_instr *start_pcopy = NULL;
476
nir_foreach_instr(instr, block) {
477
/* Phi nodes only ever come at the start of a block */
478
if (instr->type != nir_instr_type_phi) {
479
if (instr->type != nir_instr_type_parallel_copy)
480
break; /* The parallel copy must be right after the phis */
481
482
start_pcopy = nir_instr_as_parallel_copy(instr);
483
484
aggressive_coalesce_parallel_copy(start_pcopy, state);
485
486
break;
487
}
488
}
489
490
nir_parallel_copy_instr *end_pcopy =
491
get_parallel_copy_at_end_of_block(block);
492
493
if (end_pcopy && end_pcopy != start_pcopy)
494
aggressive_coalesce_parallel_copy(end_pcopy, state);
495
496
return true;
497
}
498
499
static nir_register *
500
create_reg_for_ssa_def(nir_ssa_def *def, nir_function_impl *impl)
501
{
502
nir_register *reg = nir_local_reg_create(impl);
503
504
reg->num_components = def->num_components;
505
reg->bit_size = def->bit_size;
506
reg->num_array_elems = 0;
507
508
return reg;
509
}
510
511
static bool
512
rewrite_ssa_def(nir_ssa_def *def, void *void_state)
513
{
514
struct from_ssa_state *state = void_state;
515
nir_register *reg;
516
517
struct hash_entry *entry =
518
_mesa_hash_table_search(state->merge_node_table, def);
519
if (entry) {
520
/* In this case, we're part of a phi web. Use the web's register. */
521
merge_node *node = (merge_node *)entry->data;
522
523
/* If it doesn't have a register yet, create one. Note that all of
524
* the things in the merge set should be the same so it doesn't
525
* matter which node's definition we use.
526
*/
527
if (node->set->reg == NULL) {
528
node->set->reg = create_reg_for_ssa_def(def, state->builder.impl);
529
node->set->reg->divergent = node->set->divergent;
530
}
531
532
reg = node->set->reg;
533
} else {
534
if (state->phi_webs_only)
535
return true;
536
537
/* We leave load_const SSA values alone. They act as immediates to
538
* the backend. If it got coalesced into a phi, that's ok.
539
*/
540
if (def->parent_instr->type == nir_instr_type_load_const)
541
return true;
542
543
reg = create_reg_for_ssa_def(def, state->builder.impl);
544
}
545
546
nir_ssa_def_rewrite_uses_src(def, nir_src_for_reg(reg));
547
assert(nir_ssa_def_is_unused(def));
548
549
if (def->parent_instr->type == nir_instr_type_ssa_undef) {
550
/* If it's an ssa_undef instruction, remove it since we know we just got
551
* rid of all its uses.
552
*/
553
nir_instr *parent_instr = def->parent_instr;
554
nir_instr_remove(parent_instr);
555
ralloc_steal(state->dead_ctx, parent_instr);
556
state->progress = true;
557
return true;
558
}
559
560
assert(def->parent_instr->type != nir_instr_type_load_const);
561
562
/* At this point we know a priori that this SSA def is part of a
563
* nir_dest. We can use exec_node_data to get the dest pointer.
564
*/
565
nir_dest *dest = exec_node_data(nir_dest, def, ssa);
566
567
nir_instr_rewrite_dest(state->instr, dest, nir_dest_for_reg(reg));
568
state->progress = true;
569
return true;
570
}
571
572
/* Resolves ssa definitions to registers. While we're at it, we also
573
* remove phi nodes.
574
*/
575
static void
576
resolve_registers_block(nir_block *block, struct from_ssa_state *state)
577
{
578
nir_foreach_instr_safe(instr, block) {
579
state->instr = instr;
580
nir_foreach_ssa_def(instr, rewrite_ssa_def, state);
581
582
if (instr->type == nir_instr_type_phi) {
583
nir_instr_remove(instr);
584
ralloc_steal(state->dead_ctx, instr);
585
state->progress = true;
586
}
587
}
588
state->instr = NULL;
589
}
590
591
static void
592
emit_copy(nir_builder *b, nir_src src, nir_src dest_src)
593
{
594
assert(!dest_src.is_ssa &&
595
dest_src.reg.indirect == NULL &&
596
dest_src.reg.base_offset == 0);
597
598
assert(!nir_src_is_divergent(src) || nir_src_is_divergent(dest_src));
599
600
if (src.is_ssa)
601
assert(src.ssa->num_components >= dest_src.reg.reg->num_components);
602
else
603
assert(src.reg.reg->num_components >= dest_src.reg.reg->num_components);
604
605
nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_mov);
606
nir_src_copy(&mov->src[0].src, &src, mov);
607
mov->dest.dest = nir_dest_for_reg(dest_src.reg.reg);
608
mov->dest.write_mask = (1 << dest_src.reg.reg->num_components) - 1;
609
610
nir_builder_instr_insert(b, &mov->instr);
611
}
612
613
/* Resolves a single parallel copy operation into a sequence of movs
614
*
615
* This is based on Algorithm 1 from "Revisiting Out-of-SSA Translation for
616
* Correctness, Code Quality, and Efficiency" by Boissinot et al.
617
* However, I never got the algorithm to work as written, so this version
618
* is slightly modified.
619
*
620
* The algorithm works by playing this little shell game with the values.
621
* We start by recording where every source value is and which source value
622
* each destination value should receive. We then grab any copy whose
623
* destination is "empty", i.e. not used as a source, and do the following:
624
* - Find where its source value currently lives
625
* - Emit the move instruction
626
* - Set the location of the source value to the destination
627
* - Mark the location containing the source value
628
* - Mark the destination as no longer needing to be copied
629
*
630
* When we run out of "empty" destinations, we have a cycle and so we
631
* create a temporary register, copy to that register, and mark the value
632
* we copied as living in that temporary. Now, the cycle is broken, so we
633
* can continue with the above steps.
634
*/
635
static void
636
resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
637
struct from_ssa_state *state)
638
{
639
unsigned num_copies = 0;
640
nir_foreach_parallel_copy_entry(entry, pcopy) {
641
/* Sources may be SSA */
642
if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
643
continue;
644
645
num_copies++;
646
}
647
648
if (num_copies == 0) {
649
/* Hooray, we don't need any copies! */
650
nir_instr_remove(&pcopy->instr);
651
return;
652
}
653
654
/* The register/source corresponding to the given index */
655
NIR_VLA_ZERO(nir_src, values, num_copies * 2);
656
657
/* The current location of a given piece of data. We will use -1 for "null" */
658
NIR_VLA_FILL(int, loc, num_copies * 2, -1);
659
660
/* The piece of data that the given piece of data is to be copied from. We will use -1 for "null" */
661
NIR_VLA_FILL(int, pred, num_copies * 2, -1);
662
663
/* The destinations we have yet to properly fill */
664
NIR_VLA(int, to_do, num_copies * 2);
665
int to_do_idx = -1;
666
667
state->builder.cursor = nir_before_instr(&pcopy->instr);
668
669
/* Now we set everything up:
670
* - All values get assigned a temporary index
671
* - Current locations are set from sources
672
* - Predicessors are recorded from sources and destinations
673
*/
674
int num_vals = 0;
675
nir_foreach_parallel_copy_entry(entry, pcopy) {
676
/* Sources may be SSA */
677
if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
678
continue;
679
680
int src_idx = -1;
681
for (int i = 0; i < num_vals; ++i) {
682
if (nir_srcs_equal(values[i], entry->src))
683
src_idx = i;
684
}
685
if (src_idx < 0) {
686
src_idx = num_vals++;
687
values[src_idx] = entry->src;
688
}
689
690
nir_src dest_src = nir_src_for_reg(entry->dest.reg.reg);
691
692
int dest_idx = -1;
693
for (int i = 0; i < num_vals; ++i) {
694
if (nir_srcs_equal(values[i], dest_src)) {
695
/* Each destination of a parallel copy instruction should be
696
* unique. A destination may get used as a source, so we still
697
* have to walk the list. However, the predecessor should not,
698
* at this point, be set yet, so we should have -1 here.
699
*/
700
assert(pred[i] == -1);
701
dest_idx = i;
702
}
703
}
704
if (dest_idx < 0) {
705
dest_idx = num_vals++;
706
values[dest_idx] = dest_src;
707
}
708
709
loc[src_idx] = src_idx;
710
pred[dest_idx] = src_idx;
711
712
to_do[++to_do_idx] = dest_idx;
713
}
714
715
/* Currently empty destinations we can go ahead and fill */
716
NIR_VLA(int, ready, num_copies * 2);
717
int ready_idx = -1;
718
719
/* Mark the ones that are ready for copying. We know an index is a
720
* destination if it has a predecessor and it's ready for copying if
721
* it's not marked as containing data.
722
*/
723
for (int i = 0; i < num_vals; i++) {
724
if (pred[i] != -1 && loc[i] == -1)
725
ready[++ready_idx] = i;
726
}
727
728
while (to_do_idx >= 0) {
729
while (ready_idx >= 0) {
730
int b = ready[ready_idx--];
731
int a = pred[b];
732
emit_copy(&state->builder, values[loc[a]], values[b]);
733
734
/* b has been filled, mark it as not needing to be copied */
735
pred[b] = -1;
736
737
/* The next bit only applies if the source and destination have the
738
* same divergence. If they differ (it must be convergent ->
739
* divergent), then we can't guarantee we won't need the convergent
740
* version of again.
741
*/
742
if (nir_src_is_divergent(values[a]) ==
743
nir_src_is_divergent(values[b])) {
744
/* If any other copies want a they can find it at b but only if the
745
* two have the same divergence.
746
*/
747
loc[a] = b;
748
749
/* If a needs to be filled... */
750
if (pred[a] != -1) {
751
/* If any other copies want a they can find it at b */
752
loc[a] = b;
753
754
/* It's ready for copying now */
755
ready[++ready_idx] = a;
756
}
757
}
758
}
759
int b = to_do[to_do_idx--];
760
if (pred[b] == -1)
761
continue;
762
763
/* If we got here, then we don't have any more trivial copies that we
764
* can do. We have to break a cycle, so we create a new temporary
765
* register for that purpose. Normally, if going out of SSA after
766
* register allocation, you would want to avoid creating temporary
767
* registers. However, we are going out of SSA before register
768
* allocation, so we would rather not create extra register
769
* dependencies for the backend to deal with. If it wants, the
770
* backend can coalesce the (possibly multiple) temporaries.
771
*/
772
assert(num_vals < num_copies * 2);
773
nir_register *reg = nir_local_reg_create(state->builder.impl);
774
reg->num_array_elems = 0;
775
if (values[b].is_ssa) {
776
reg->num_components = values[b].ssa->num_components;
777
reg->bit_size = values[b].ssa->bit_size;
778
} else {
779
reg->num_components = values[b].reg.reg->num_components;
780
reg->bit_size = values[b].reg.reg->bit_size;
781
}
782
reg->divergent = nir_src_is_divergent(values[b]);
783
values[num_vals].is_ssa = false;
784
values[num_vals].reg.reg = reg;
785
786
emit_copy(&state->builder, values[b], values[num_vals]);
787
loc[b] = num_vals;
788
ready[++ready_idx] = b;
789
num_vals++;
790
}
791
792
nir_instr_remove(&pcopy->instr);
793
}
794
795
/* Resolves the parallel copies in a block. Each block can have at most
796
* two: One at the beginning, right after all the phi noces, and one at
797
* the end (or right before the final jump if it exists).
798
*/
799
static bool
800
resolve_parallel_copies_block(nir_block *block, struct from_ssa_state *state)
801
{
802
/* At this point, we have removed all of the phi nodes. If a parallel
803
* copy existed right after the phi nodes in this block, it is now the
804
* first instruction.
805
*/
806
nir_instr *first_instr = nir_block_first_instr(block);
807
if (first_instr == NULL)
808
return true; /* Empty, nothing to do. */
809
810
if (first_instr->type == nir_instr_type_parallel_copy) {
811
nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(first_instr);
812
813
resolve_parallel_copy(pcopy, state);
814
}
815
816
/* It's possible that the above code already cleaned up the end parallel
817
* copy. However, doing so removed it form the instructions list so we
818
* won't find it here. Therefore, it's safe to go ahead and just look
819
* for one and clean it up if it exists.
820
*/
821
nir_parallel_copy_instr *end_pcopy =
822
get_parallel_copy_at_end_of_block(block);
823
if (end_pcopy)
824
resolve_parallel_copy(end_pcopy, state);
825
826
return true;
827
}
828
829
static bool
830
nir_convert_from_ssa_impl(nir_function_impl *impl, bool phi_webs_only)
831
{
832
struct from_ssa_state state;
833
834
nir_builder_init(&state.builder, impl);
835
state.dead_ctx = ralloc_context(NULL);
836
state.phi_webs_only = phi_webs_only;
837
state.merge_node_table = _mesa_pointer_hash_table_create(NULL);
838
state.progress = false;
839
840
nir_foreach_block(block, impl) {
841
add_parallel_copy_to_end_of_block(block, state.dead_ctx);
842
}
843
844
nir_foreach_block(block, impl) {
845
isolate_phi_nodes_block(block, state.dead_ctx);
846
}
847
848
/* Mark metadata as dirty before we ask for liveness analysis */
849
nir_metadata_preserve(impl, nir_metadata_block_index |
850
nir_metadata_dominance);
851
852
nir_metadata_require(impl, nir_metadata_instr_index |
853
nir_metadata_live_ssa_defs |
854
nir_metadata_dominance);
855
856
nir_foreach_block(block, impl) {
857
coalesce_phi_nodes_block(block, &state);
858
}
859
860
nir_foreach_block(block, impl) {
861
aggressive_coalesce_block(block, &state);
862
}
863
864
nir_foreach_block(block, impl) {
865
resolve_registers_block(block, &state);
866
}
867
868
nir_foreach_block(block, impl) {
869
resolve_parallel_copies_block(block, &state);
870
}
871
872
nir_metadata_preserve(impl, nir_metadata_block_index |
873
nir_metadata_dominance);
874
875
/* Clean up dead instructions and the hash tables */
876
_mesa_hash_table_destroy(state.merge_node_table, NULL);
877
ralloc_free(state.dead_ctx);
878
return state.progress;
879
}
880
881
bool
882
nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only)
883
{
884
bool progress = false;
885
886
nir_foreach_function(function, shader) {
887
if (function->impl)
888
progress |= nir_convert_from_ssa_impl(function->impl, phi_webs_only);
889
}
890
891
return progress;
892
}
893
894
895
static void
896
place_phi_read(nir_builder *b, nir_register *reg,
897
nir_ssa_def *def, nir_block *block, unsigned depth)
898
{
899
if (block != def->parent_instr->block) {
900
/* Try to go up the single-successor tree */
901
bool all_single_successors = true;
902
set_foreach(block->predecessors, entry) {
903
nir_block *pred = (nir_block *)entry->key;
904
if (pred->successors[0] && pred->successors[1]) {
905
all_single_successors = false;
906
break;
907
}
908
}
909
910
if (all_single_successors && depth < 32) {
911
/* All predecessors of this block have exactly one successor and it
912
* is this block so they must eventually lead here without
913
* intersecting each other. Place the reads in the predecessors
914
* instead of this block.
915
*
916
* We only let this function recurse 32 times because it can recurse
917
* indefinitely in the presence of infinite loops. Because we're
918
* crawling a single-successor chain, it doesn't matter where we
919
* place it so it's ok to stop at an arbitrary distance.
920
*
921
* TODO: One day, we could detect back edges and avoid the recursion
922
* that way.
923
*/
924
set_foreach(block->predecessors, entry) {
925
place_phi_read(b, reg, def, (nir_block *)entry->key, depth + 1);
926
}
927
return;
928
}
929
}
930
931
b->cursor = nir_after_block_before_jump(block);
932
nir_store_reg(b, reg, def, ~0);
933
}
934
935
/** Lower all of the phi nodes in a block to movs to and from a register
936
*
937
* This provides a very quick-and-dirty out-of-SSA pass that you can run on a
938
* single block to convert all of its phis to a register and some movs.
939
* The code that is generated, while not optimal for actual codegen in a
940
* back-end, is easy to generate, correct, and will turn into the same set of
941
* phis after you call regs_to_ssa and do some copy propagation.
942
*
943
* The one intelligent thing this pass does is that it places the moves from
944
* the phi sources as high up the predecessor tree as possible instead of in
945
* the exact predecessor. This means that, in particular, it will crawl into
946
* the deepest nesting of any if-ladders. In order to ensure that doing so is
947
* safe, it stops as soon as one of the predecessors has multiple successors.
948
*/
949
bool
950
nir_lower_phis_to_regs_block(nir_block *block)
951
{
952
nir_builder b;
953
nir_builder_init(&b, nir_cf_node_get_function(&block->cf_node));
954
955
bool progress = false;
956
nir_foreach_instr_safe(instr, block) {
957
if (instr->type != nir_instr_type_phi)
958
break;
959
960
nir_phi_instr *phi = nir_instr_as_phi(instr);
961
assert(phi->dest.is_ssa);
962
963
nir_register *reg = create_reg_for_ssa_def(&phi->dest.ssa, b.impl);
964
965
b.cursor = nir_after_instr(&phi->instr);
966
nir_ssa_def *def = nir_load_reg(&b, reg);
967
968
nir_ssa_def_rewrite_uses(&phi->dest.ssa, def);
969
970
nir_foreach_phi_src(src, phi) {
971
assert(src->src.is_ssa);
972
place_phi_read(&b, reg, src->src.ssa, src->pred, 0);
973
}
974
975
nir_instr_remove(&phi->instr);
976
977
progress = true;
978
}
979
980
return progress;
981
}
982
983
struct ssa_def_to_reg_state {
984
nir_function_impl *impl;
985
bool progress;
986
};
987
988
static bool
989
dest_replace_ssa_with_reg(nir_dest *dest, void *void_state)
990
{
991
struct ssa_def_to_reg_state *state = void_state;
992
993
if (!dest->is_ssa)
994
return true;
995
996
nir_register *reg = create_reg_for_ssa_def(&dest->ssa, state->impl);
997
998
nir_ssa_def_rewrite_uses_src(&dest->ssa, nir_src_for_reg(reg));
999
1000
nir_instr *instr = dest->ssa.parent_instr;
1001
*dest = nir_dest_for_reg(reg);
1002
dest->reg.parent_instr = instr;
1003
list_addtail(&dest->reg.def_link, &reg->defs);
1004
1005
state->progress = true;
1006
1007
return true;
1008
}
1009
1010
static bool
1011
ssa_def_is_local_to_block(nir_ssa_def *def, UNUSED void *state)
1012
{
1013
nir_block *block = def->parent_instr->block;
1014
nir_foreach_use(use_src, def) {
1015
if (use_src->parent_instr->block != block ||
1016
use_src->parent_instr->type == nir_instr_type_phi) {
1017
return false;
1018
}
1019
}
1020
1021
if (!list_is_empty(&def->if_uses))
1022
return false;
1023
1024
return true;
1025
}
1026
1027
/** Lower all of the SSA defs in a block to registers
1028
*
1029
* This performs the very simple operation of blindly replacing all of the SSA
1030
* defs in the given block with registers. If not used carefully, this may
1031
* result in phi nodes with register sources which is technically invalid.
1032
* Fortunately, the register-based into-SSA pass handles them anyway.
1033
*/
1034
bool
1035
nir_lower_ssa_defs_to_regs_block(nir_block *block)
1036
{
1037
nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
1038
nir_shader *shader = impl->function->shader;
1039
1040
struct ssa_def_to_reg_state state = {
1041
.impl = impl,
1042
.progress = false,
1043
};
1044
1045
nir_foreach_instr(instr, block) {
1046
if (instr->type == nir_instr_type_ssa_undef) {
1047
/* Undefs are just a read of something never written. */
1048
nir_ssa_undef_instr *undef = nir_instr_as_ssa_undef(instr);
1049
nir_register *reg = create_reg_for_ssa_def(&undef->def, state.impl);
1050
nir_ssa_def_rewrite_uses_src(&undef->def, nir_src_for_reg(reg));
1051
} else if (instr->type == nir_instr_type_load_const) {
1052
/* Constant loads are SSA-only, we need to insert a move */
1053
nir_load_const_instr *load = nir_instr_as_load_const(instr);
1054
nir_register *reg = create_reg_for_ssa_def(&load->def, state.impl);
1055
nir_ssa_def_rewrite_uses_src(&load->def, nir_src_for_reg(reg));
1056
1057
nir_alu_instr *mov = nir_alu_instr_create(shader, nir_op_mov);
1058
mov->src[0].src = nir_src_for_ssa(&load->def);
1059
mov->dest.dest = nir_dest_for_reg(reg);
1060
mov->dest.write_mask = (1 << reg->num_components) - 1;
1061
nir_instr_insert(nir_after_instr(&load->instr), &mov->instr);
1062
} else if (nir_foreach_ssa_def(instr, ssa_def_is_local_to_block, NULL)) {
1063
/* If the SSA def produced by this instruction is only in the block
1064
* in which it is defined and is not used by ifs or phis, then we
1065
* don't have a reason to convert it to a register.
1066
*/
1067
} else {
1068
nir_foreach_dest(instr, dest_replace_ssa_with_reg, &state);
1069
}
1070
}
1071
1072
return state.progress;
1073
}
1074
1075