summaryrefslogtreecommitdiff
path: root/lib/mesa/src/panfrost/bifrost/bi_opt_push_ubo.c
blob: 5a37bf3a9b453b44e69f9c2b8e63290662431366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (C) 2021 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "compiler.h"
#include "bi_builder.h"

/* This optimization pass, intended to run once after code emission but before
 * copy propagation, analyzes direct word-aligned UBO reads and promotes a
 * subset to moves from FAU. It is the sole populator of the UBO push data
 * structure returned back to the command stream. */

static bool
bi_is_ubo(bi_instr *ins)
{
        return (bi_opcode_props[ins->op].message == BIFROST_MESSAGE_LOAD) &&
                (ins->seg == BI_SEG_UBO);
}

static bool
bi_is_direct_aligned_ubo(bi_instr *ins)
{
        return bi_is_ubo(ins) &&
                (ins->src[0].type == BI_INDEX_CONSTANT) &&
                (ins->src[1].type == BI_INDEX_CONSTANT) &&
                ((ins->src[0].value & 0x3) == 0);
}

/* Represents use data for a single UBO */

#define MAX_UBO_WORDS (65536 / 16)

struct bi_ubo_block {
        BITSET_DECLARE(pushed, MAX_UBO_WORDS);
        uint8_t range[MAX_UBO_WORDS];
};

struct bi_ubo_analysis {
        /* Per block analysis */
        unsigned nr_blocks;
        struct bi_ubo_block *blocks;
};

static struct bi_ubo_analysis
bi_analyze_ranges(bi_context *ctx)
{
        struct bi_ubo_analysis res = {
                .nr_blocks = ctx->nir->info.num_ubos + 1,
        };

        res.blocks = calloc(res.nr_blocks, sizeof(struct bi_ubo_block));

        bi_foreach_instr_global(ctx, ins) {
                if (!bi_is_direct_aligned_ubo(ins)) continue;

                unsigned ubo = ins->src[1].value;
                unsigned word = ins->src[0].value / 4;
                unsigned channels = bi_opcode_props[ins->op].sr_count;

                assert(ubo < res.nr_blocks);
                assert(channels > 0 && channels <= 4);

                if (word >= MAX_UBO_WORDS) continue;

                /* Must use max if the same base is read with different channel
                 * counts, which is possible with nir_opt_shrink_vectors */
                uint8_t *range = res.blocks[ubo].range;
                range[word] = MAX2(range[word], channels);
        }

        return res;
}

/* Select UBO words to push. A sophisticated implementation would consider the
 * number of uses and perhaps the control flow to estimate benefit. This is not
 * sophisticated. Select from the last UBO first to prioritize sysvals. */

static void
bi_pick_ubo(struct panfrost_ubo_push *push, struct bi_ubo_analysis *analysis)
{
        for (signed ubo = analysis->nr_blocks - 1; ubo >= 0; --ubo) {
                struct bi_ubo_block *block = &analysis->blocks[ubo];

                for (unsigned r = 0; r < MAX_UBO_WORDS; ++r) {
                        unsigned range = block->range[r];

                        /* Don't push something we don't access */
                        if (range == 0) continue;

                        /* Don't push more than possible */
                        if (push->count > PAN_MAX_PUSH - range)
                                return;

                        for (unsigned offs = 0; offs < range; ++offs) {
                                struct panfrost_ubo_word word = {
                                        .ubo = ubo,
                                        .offset = (r + offs) * 4
                                };

                                push->words[push->count++] = word;
                        }

                        /* Mark it as pushed so we can rewrite */
                        BITSET_SET(block->pushed, r);
                }
        }
}

void
bi_opt_push_ubo(bi_context *ctx)
{
        struct bi_ubo_analysis analysis = bi_analyze_ranges(ctx);
        bi_pick_ubo(ctx->info.push, &analysis);

        ctx->ubo_mask = 0;

        bi_foreach_instr_global_safe(ctx, ins) {
                if (!bi_is_ubo(ins)) continue;

                unsigned ubo = ins->src[1].value;
                unsigned offset = ins->src[0].value;

                if (!bi_is_direct_aligned_ubo(ins)) {
                        /* The load can't be pushed, so this UBO needs to be
                         * uploaded conventionally */
                        if (ins->src[1].type == BI_INDEX_CONSTANT)
                                ctx->ubo_mask |= BITSET_BIT(ubo);
                        else
                                ctx->ubo_mask = ~0;

                        continue;
                }

                /* Check if we decided to push this */
                assert(ubo < analysis.nr_blocks);
                if (!BITSET_TEST(analysis.blocks[ubo].pushed, offset / 4)) {
                        ctx->ubo_mask |= BITSET_BIT(ubo);
                        continue;
                }

                /* Replace the UBO load with moves from FAU */
                bi_builder b = bi_init_builder(ctx, bi_after_instr(ins));

                unsigned nr = bi_opcode_props[ins->op].sr_count;
                bi_instr *vec = bi_collect_i32_to(&b, ins->dest[0], nr);

                bi_foreach_src(vec, w) {
                        /* FAU is grouped in pairs (2 x 4-byte) */
                        unsigned base =
                                pan_lookup_pushed_ubo(ctx->info.push, ubo,
                                                      (offset + 4 * w));

                        unsigned fau_idx = (base >> 1);
                        unsigned fau_hi = (base & 1);

                        vec->src[w] = bi_fau(BIR_FAU_UNIFORM | fau_idx, fau_hi);
                }

                bi_remove_instruction(ins);
        }

        free(analysis.blocks);
}

typedef struct {
        BITSET_DECLARE(row, PAN_MAX_PUSH);
} adjacency_row;

/* Find the connected component containing `node` with depth-first search */
static void
bi_find_component(adjacency_row *adjacency, BITSET_WORD *visited,
                  unsigned *component, unsigned *size, unsigned node)
{
        unsigned neighbour;

        BITSET_SET(visited, node);
        component[(*size)++] = node;

        BITSET_FOREACH_SET(neighbour, adjacency[node].row, PAN_MAX_PUSH) {
                if (!BITSET_TEST(visited, neighbour)) {
                        bi_find_component(adjacency, visited, component, size,
                                          neighbour);
                }
        }
}

static bool
bi_is_uniform(bi_index idx)
{
        return (idx.type == BI_INDEX_FAU) && (idx.value & BIR_FAU_UNIFORM);
}

/* Get the index of a uniform in 32-bit words from the start of FAU-RAM */
static unsigned
bi_uniform_word(bi_index idx)
{
        assert(bi_is_uniform(idx));
        assert(idx.offset <= 1);

        return ((idx.value & ~BIR_FAU_UNIFORM) << 1) | idx.offset;
}

/*
 * Create an undirected graph where nodes are 32-bit uniform indices and edges
 * represent that two nodes are used in the same instruction.
 *
 * The graph is constructed as an adjacency matrix stored in adjacency.
 */
static void
bi_create_fau_interference_graph(bi_context *ctx, adjacency_row *adjacency)
{
        bi_foreach_instr_global(ctx, I) {
                unsigned nodes[BI_MAX_SRCS] = {};
                unsigned node_count = 0;

                /* Set nodes[] to 32-bit uniforms accessed */
                bi_foreach_src(I, s) {
                        if (bi_is_uniform(I->src[s])) {
                                unsigned word = bi_uniform_word(I->src[s]);

                                if (word >= ctx->info.push_offset)
                                        nodes[node_count++] = word;
                        }
                }

                /* Create clique connecting nodes[] */
                for (unsigned i = 0; i < node_count; ++i) {
                        for (unsigned j = 0; j < node_count; ++j) {
                                if (i == j)
                                        continue;

                                unsigned x = nodes[i], y = nodes[j];
                                assert(MAX2(x, y) < ctx->info.push->count);

                                /* Add undirected edge between the nodes */
                                BITSET_SET(adjacency[x].row, y);
                                BITSET_SET(adjacency[y].row, x);
                        }
                }
        }
}

/*
 * Optimization pass to reorder uniforms. The goal is to reduce the number of
 * moves we emit when lowering FAU. The pass groups uniforms used by the same
 * instruction.
 *
 * The pass works by creating a graph of pushed uniforms, where edges denote the
 * "both 32-bit uniforms required by the same instruction" relationship. We
 * perform depth-first search on this graph to find the connected components,
 * where each connected component is a cluster of uniforms that are used
 * together. We then select pairs of uniforms from each connected component.
 * The remaining unpaired uniforms (from components of odd sizes) are paired
 * together arbitrarily.
 *
 * After a new ordering is selected, pushed uniforms in the program and the
 * panfrost_ubo_push data structure must be remapped to use the new ordering.
 */
void
bi_opt_reorder_push(bi_context *ctx)
{
        adjacency_row adjacency[PAN_MAX_PUSH] = { 0 };
        BITSET_DECLARE(visited, PAN_MAX_PUSH) = { 0 };

        unsigned ordering[PAN_MAX_PUSH] = { 0 };
        unsigned unpaired[PAN_MAX_PUSH] = { 0 };
        unsigned pushed = 0, unpaired_count = 0;

        struct panfrost_ubo_push *push = ctx->info.push;
        unsigned push_offset = ctx->info.push_offset;

        bi_create_fau_interference_graph(ctx, adjacency);

        for (unsigned i = push_offset; i < push->count; ++i) {
                if (BITSET_TEST(visited, i)) continue;

                unsigned component[PAN_MAX_PUSH] = { 0 };
                unsigned size = 0;
                bi_find_component(adjacency, visited, component, &size, i);

                /* If there is an odd number of uses, at least one use must be
                 * unpaired. Arbitrarily take the last one.
                 */
                if (size % 2)
                        unpaired[unpaired_count++] = component[--size];

                /* The rest of uses are paired */
                assert((size % 2) == 0);

                /* Push the paired uses */
                memcpy(ordering + pushed, component, sizeof(unsigned) * size);
                pushed += size;
        }

        /* Push unpaired nodes at the end */
        memcpy(ordering + pushed, unpaired, sizeof(unsigned) * unpaired_count);
        pushed += unpaired_count;

        /* Ordering is a permutation. Invert it for O(1) lookup. */
        unsigned old_to_new[PAN_MAX_PUSH] = { 0 };

        for (unsigned i = 0; i < push_offset; ++i) {
                old_to_new[i] = i;
        }

        for (unsigned i = 0; i < pushed; ++i) {
                assert(ordering[i] >= push_offset);
                old_to_new[ordering[i]] = push_offset + i;
        }

        /* Use new ordering throughout the program */
        bi_foreach_instr_global(ctx, I) {
                bi_foreach_src(I, s) {
                        if (bi_is_uniform(I->src[s])) {
                                unsigned node = bi_uniform_word(I->src[s]);
                                unsigned new_node = old_to_new[node];
                                I->src[s].value = BIR_FAU_UNIFORM | (new_node >> 1);
                                I->src[s].offset = new_node & 1;
                        }
                }
        }

        /* Use new ordering for push */
        struct panfrost_ubo_push old = *push;
        for (unsigned i = 0; i < pushed; ++i)
                push->words[push_offset + i] = old.words[ordering[i]];

        push->count = push_offset + pushed;
}