| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "bi_builder.h" |
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* This optimization pass, intended to run once after code emission but before |
| 10 |
* copy propagation, analyzes direct word-aligned UBO reads and promotes a |
| 11 |
* subset to moves from FAU. It is the sole populator of the UBO push data |
| 12 |
* structure returned back to the command stream. */ |
| 13 |
|
| 14 |
static bool |
| 15 |
bi_is_ubo(bi_instr *ins) |
| 16 |
{ |
| 17 |
return (bi_get_opcode_props(ins)->message == BIFROST_MESSAGE_LOAD) && |
| 18 |
(ins->seg == BI_SEG_UBO); |
| 19 |
} |
| 20 |
|
| 21 |
static bool |
| 22 |
bi_is_pushable_ubo(bi_context *ctx, bi_instr *ins) |
| 23 |
{ |
| 24 |
if (!(bi_is_ubo(ins) && (ins->src[0].type == BI_INDEX_CONSTANT) && |
| 25 |
(ins->src[1].type == BI_INDEX_CONSTANT))) |
| 26 |
return false; |
| 27 |
|
| 28 |
unsigned ubo = pan_res_handle_get_index(ins->src[1].value); |
| 29 |
unsigned offset = ins->src[0].value; |
| 30 |
|
| 31 |
return ctx->inputs->pushable_ubos & BITFIELD_BIT(ubo) && (offset & 0x3) == 0; |
| 32 |
} |
| 33 |
|
| 34 |
/* Represents use data for a single UBO */ |
| 35 |
|
| 36 |
#define MAX_UBO_WORDS (65536 / 16) |
| 37 |
|
| 38 |
struct bi_ubo_block { |
| 39 |
BITSET_DECLARE(pushed, MAX_UBO_WORDS); |
| 40 |
uint8_t range[MAX_UBO_WORDS]; |
| 41 |
}; |
| 42 |
|
| 43 |
struct bi_ubo_analysis { |
| 44 |
/* Per block analysis */ |
| 45 |
unsigned nr_blocks; |
| 46 |
struct bi_ubo_block *blocks; |
| 47 |
}; |
| 48 |
|
| 49 |
static struct bi_ubo_analysis |
| 50 |
bi_analyze_ranges(bi_context *ctx) |
| 51 |
{ |
| 52 |
struct bi_ubo_analysis res = { |
| 53 |
.nr_blocks = ctx->nir->info.num_ubos + 1, |
| 54 |
}; |
| 55 |
|
| 56 |
res.blocks = calloc(res.nr_blocks, sizeof(struct bi_ubo_block)); |
| 57 |
|
| 58 |
bi_foreach_instr_global(ctx, ins) { |
| 59 |
if (!bi_is_pushable_ubo(ctx, ins)) |
| 60 |
continue; |
| 61 |
|
| 62 |
unsigned ubo = pan_res_handle_get_index(ins->src[1].value); |
| 63 |
unsigned word = ins->src[0].value / 4; |
| 64 |
unsigned channels = bi_get_opcode_props(ins)->sr_count; |
| 65 |
|
| 66 |
assert(ubo < res.nr_blocks); |
| 67 |
assert(channels > 0 && channels <= 4); |
| 68 |
|
| 69 |
/* Blend constants are handled by bi_pick_blend_constants, don't push |
| 70 |
* them a second time. */ |
| 71 |
if (ctx->stage == MESA_SHADER_FRAGMENT) { |
| 72 |
/* PAN_UBO_SYSVALS from the gallium driver */ |
| 73 |
unsigned sysval_ubo = 1; |
| 74 |
if(ubo == sysval_ubo && word == 0) |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
if (word >= MAX_UBO_WORDS) |
| 79 |
continue; |
| 80 |
|
| 81 |
/* Must use max if the same base is read with different channel |
| 82 |
* counts, which is possible with nir_opt_shrink_vectors */ |
| 83 |
uint8_t *range = res.blocks[ubo].range; |
| 84 |
range[word] = MAX2(range[word], channels); |
| 85 |
} |
| 86 |
|
| 87 |
return res; |
| 88 |
} |
| 89 |
|
| 90 |
/* We always map blend constants from the first slot in the sysval UBO to the |
| 91 |
* first four FAU words, so that they can be accessed from a consistent |
| 92 |
* location from the blend shader. */ |
| 93 |
static void |
| 94 |
bi_pick_blend_constants(bi_context *ctx, struct bi_ubo_analysis *analysis) |
| 95 |
{ |
| 96 |
/* PAN_UBO_SYSVALS from the gallium driver */ |
| 97 |
unsigned sysval_ubo = 1; |
| 98 |
unsigned offset = 0; |
| 99 |
|
| 100 |
assert(ctx->inputs->pushable_ubos & BITFIELD_BIT(sysval_ubo)); |
| 101 |
assert(ctx->info.push->count == 0); |
| 102 |
|
| 103 |
for (unsigned channel = 0; channel < 4; channel++) { |
| 104 |
struct pan_ubo_word word = { |
| 105 |
.ubo = sysval_ubo, |
| 106 |
.offset = offset + channel * 4, |
| 107 |
}; |
| 108 |
ctx->info.push->words[ctx->info.push->count++] = word; |
| 109 |
assert(ctx->info.push->count < PAN_MAX_PUSH); |
| 110 |
} |
| 111 |
|
| 112 |
BITSET_SET(analysis->blocks[sysval_ubo].pushed, offset); |
| 113 |
|
| 114 |
/* The blend constant FAU slots cannot be reordered */ |
| 115 |
ctx->info.push_offset = MAX2(ctx->info.push_offset, ctx->info.push->count); |
| 116 |
} |
| 117 |
|
| 118 |
/* Select UBO words to push. A sophisticated implementation would consider the |
| 119 |
* number of uses and perhaps the control flow to estimate benefit. This is not |
| 120 |
* sophisticated. Select from the last UBO first to prioritize sysvals. */ |
| 121 |
|
| 122 |
static void |
| 123 |
bi_pick_ubo(struct pan_ubo_push *push, struct bi_ubo_analysis *analysis) |
| 124 |
{ |
| 125 |
for (signed ubo = analysis->nr_blocks - 1; ubo >= 0; --ubo) { |
| 126 |
struct bi_ubo_block *block = &analysis->blocks[ubo]; |
| 127 |
|
| 128 |
for (unsigned r = 0; r < MAX_UBO_WORDS; ++r) { |
| 129 |
unsigned range = block->range[r]; |
| 130 |
|
| 131 |
/* Don't push something we don't access */ |
| 132 |
if (range == 0) |
| 133 |
continue; |
| 134 |
|
| 135 |
/* Don't push more than possible */ |
| 136 |
if (push->count > PAN_MAX_PUSH - range) |
| 137 |
return; |
| 138 |
|
| 139 |
for (unsigned offs = 0; offs < range; ++offs) { |
| 140 |
struct pan_ubo_word word = { |
| 141 |
.ubo = ubo, |
| 142 |
.offset = (r + offs) * 4, |
| 143 |
}; |
| 144 |
|
| 145 |
push->words[push->count++] = word; |
| 146 |
} |
| 147 |
|
| 148 |
/* Mark it as pushed so we can rewrite */ |
| 149 |
BITSET_SET(block->pushed, r); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
bi_opt_push_ubo(bi_context *ctx) |
| 156 |
{ |
| 157 |
struct bi_ubo_analysis analysis = bi_analyze_ranges(ctx); |
| 158 |
|
| 159 |
if (ctx->stage == MESA_SHADER_FRAGMENT) |
| 160 |
bi_pick_blend_constants(ctx, &analysis); |
| 161 |
bi_pick_ubo(ctx->info.push, &analysis); |
| 162 |
|
| 163 |
ctx->ubo_mask = 0; |
| 164 |
|
| 165 |
bi_foreach_instr_global_safe(ctx, ins) { |
| 166 |
if (!bi_is_ubo(ins)) |
| 167 |
continue; |
| 168 |
|
| 169 |
unsigned ubo = pan_res_handle_get_index(ins->src[1].value); |
| 170 |
unsigned offset = ins->src[0].value; |
| 171 |
|
| 172 |
if (!bi_is_pushable_ubo(ctx, ins)) { |
| 173 |
/* The load can't be pushed, so this UBO needs to be |
| 174 |
* uploaded conventionally */ |
| 175 |
if (ins->src[1].type == BI_INDEX_CONSTANT) |
| 176 |
ctx->ubo_mask |= BITSET_BIT(ubo); |
| 177 |
else |
| 178 |
ctx->ubo_mask = ~0; |
| 179 |
|
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
/* Check if we decided to push this */ |
| 184 |
assert(ubo < analysis.nr_blocks); |
| 185 |
if (!BITSET_TEST(analysis.blocks[ubo].pushed, offset / 4)) { |
| 186 |
ctx->ubo_mask |= BITSET_BIT(ubo); |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
/* Replace the UBO load with moves from FAU */ |
| 191 |
bi_builder b = bi_init_builder(ctx, bi_after_instr(ins)); |
| 192 |
|
| 193 |
unsigned nr = bi_get_opcode_props(ins)->sr_count; |
| 194 |
bi_instr *vec = bi_collect_i32_to(&b, ins->dest[0], nr); |
| 195 |
|
| 196 |
bi_foreach_src(vec, w) { |
| 197 |
/* FAU is grouped in pairs (2 x 4-byte) */ |
| 198 |
unsigned base = |
| 199 |
pan_lookup_pushed_ubo(ctx->info.push, ubo, (offset + 4 * w)); |
| 200 |
|
| 201 |
unsigned fau_idx = (base >> 1); |
| 202 |
unsigned fau_hi = (base & 1); |
| 203 |
|
| 204 |
vec->src[w] = bi_fau(BIR_FAU_UNIFORM | fau_idx, fau_hi); |
| 205 |
} |
| 206 |
|
| 207 |
bi_remove_instruction(ins); |
| 208 |
} |
| 209 |
|
| 210 |
free(analysis.blocks); |
| 211 |
} |
| 212 |
|
| 213 |
typedef struct { |
| 214 |
BITSET_DECLARE(row, PAN_MAX_PUSH); |
| 215 |
} adjacency_row; |
| 216 |
|
| 217 |
/* Find the connected component containing `node` with depth-first search */ |
| 218 |
static void |
| 219 |
bi_find_component(adjacency_row *adjacency, BITSET_WORD *visited, |
| 220 |
unsigned *component, unsigned *size, unsigned node) |
| 221 |
{ |
| 222 |
unsigned neighbour; |
| 223 |
|
| 224 |
BITSET_SET(visited, node); |
| 225 |
component[(*size)++] = node; |
| 226 |
|
| 227 |
BITSET_FOREACH_SET(neighbour, adjacency[node].row, PAN_MAX_PUSH) { |
| 228 |
if (!BITSET_TEST(visited, neighbour)) { |
| 229 |
bi_find_component(adjacency, visited, component, size, neighbour); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static bool |
| 235 |
bi_is_uniform(bi_index idx) |
| 236 |
{ |
| 237 |
return (idx.type == BI_INDEX_FAU) && (idx.value & BIR_FAU_UNIFORM); |
| 238 |
} |
| 239 |
|
| 240 |
/* Get the index of a uniform in 32-bit words from the start of FAU-RAM */ |
| 241 |
static unsigned |
| 242 |
bi_uniform_word(bi_index idx) |
| 243 |
{ |
| 244 |
assert(bi_is_uniform(idx)); |
| 245 |
assert(idx.offset <= 1); |
| 246 |
|
| 247 |
return ((idx.value & ~BIR_FAU_UNIFORM) << 1) | idx.offset; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Create an undirected graph where nodes are 32-bit uniform indices and edges |
| 252 |
* represent that two nodes are used in the same instruction. |
| 253 |
* |
| 254 |
* The graph is constructed as an adjacency matrix stored in adjacency. |
| 255 |
*/ |
| 256 |
static void |
| 257 |
bi_create_fau_interference_graph(bi_context *ctx, adjacency_row *adjacency) |
| 258 |
{ |
| 259 |
bi_foreach_instr_global(ctx, I) { |
| 260 |
unsigned nodes[BI_MAX_SRCS] = {}; |
| 261 |
unsigned node_count = 0; |
| 262 |
|
| 263 |
/* Set nodes[] to 32-bit uniforms accessed */ |
| 264 |
bi_foreach_src(I, s) { |
| 265 |
if (bi_is_uniform(I->src[s])) { |
| 266 |
unsigned word = bi_uniform_word(I->src[s]); |
| 267 |
|
| 268 |
if (word >= ctx->info.push_offset) |
| 269 |
nodes[node_count++] = word; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/* Create clique connecting nodes[] */ |
| 274 |
for (unsigned i = 0; i < node_count; ++i) { |
| 275 |
for (unsigned j = 0; j < node_count; ++j) { |
| 276 |
if (i == j) |
| 277 |
continue; |
| 278 |
|
| 279 |
unsigned x = nodes[i], y = nodes[j]; |
| 280 |
assert(MAX2(x, y) < ctx->info.push->count); |
| 281 |
|
| 282 |
/* Add undirected edge between the nodes */ |
| 283 |
BITSET_SET(adjacency[x].row, y); |
| 284 |
BITSET_SET(adjacency[y].row, x); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
* Optimization pass to reorder uniforms. The goal is to reduce the number of |
| 292 |
* moves we emit when lowering FAU. The pass groups uniforms used by the same |
| 293 |
* instruction. |
| 294 |
* |
| 295 |
* The pass works by creating a graph of pushed uniforms, where edges denote the |
| 296 |
* "both 32-bit uniforms required by the same instruction" relationship. We |
| 297 |
* perform depth-first search on this graph to find the connected components, |
| 298 |
* where each connected component is a cluster of uniforms that are used |
| 299 |
* together. We then select pairs of uniforms from each connected component. |
| 300 |
* The remaining unpaired uniforms (from components of odd sizes) are paired |
| 301 |
* together arbitrarily. |
| 302 |
* |
| 303 |
* After a new ordering is selected, pushed uniforms in the program and the |
| 304 |
* pan_ubo_push data structure must be remapped to use the new ordering. |
| 305 |
*/ |
| 306 |
void |
| 307 |
bi_opt_reorder_push(bi_context *ctx) |
| 308 |
{ |
| 309 |
adjacency_row adjacency[PAN_MAX_PUSH] = {0}; |
| 310 |
BITSET_DECLARE(visited, PAN_MAX_PUSH) = {0}; |
| 311 |
|
| 312 |
unsigned ordering[PAN_MAX_PUSH] = {0}; |
| 313 |
unsigned unpaired[PAN_MAX_PUSH] = {0}; |
| 314 |
unsigned pushed = 0, unpaired_count = 0; |
| 315 |
|
| 316 |
struct pan_ubo_push *push = ctx->info.push; |
| 317 |
unsigned push_offset = ctx->info.push_offset; |
| 318 |
|
| 319 |
bi_create_fau_interference_graph(ctx, adjacency); |
| 320 |
|
| 321 |
for (unsigned i = push_offset; i < push->count; ++i) { |
| 322 |
if (BITSET_TEST(visited, i)) |
| 323 |
continue; |
| 324 |
|
| 325 |
unsigned component[PAN_MAX_PUSH] = {0}; |
| 326 |
unsigned size = 0; |
| 327 |
bi_find_component(adjacency, visited, component, &size, i); |
| 328 |
|
| 329 |
/* If there is an odd number of uses, at least one use must be |
| 330 |
* unpaired. Arbitrarily take the last one. |
| 331 |
*/ |
| 332 |
if (size % 2) |
| 333 |
unpaired[unpaired_count++] = component[--size]; |
| 334 |
|
| 335 |
/* The rest of uses are paired */ |
| 336 |
assert((size % 2) == 0); |
| 337 |
|
| 338 |
/* Push the paired uses */ |
| 339 |
memcpy(ordering + pushed, component, sizeof(unsigned) * size); |
| 340 |
pushed += size; |
| 341 |
} |
| 342 |
|
| 343 |
/* Push unpaired nodes at the end */ |
| 344 |
memcpy(ordering + pushed, unpaired, sizeof(unsigned) * unpaired_count); |
| 345 |
pushed += unpaired_count; |
| 346 |
|
| 347 |
/* Ordering is a permutation. Invert it for O(1) lookup. */ |
| 348 |
unsigned old_to_new[PAN_MAX_PUSH] = {0}; |
| 349 |
|
| 350 |
for (unsigned i = 0; i < push_offset; ++i) { |
| 351 |
old_to_new[i] = i; |
| 352 |
} |
| 353 |
|
| 354 |
for (unsigned i = 0; i < pushed; ++i) { |
| 355 |
assert(ordering[i] >= push_offset); |
| 356 |
old_to_new[ordering[i]] = push_offset + i; |
| 357 |
} |
| 358 |
|
| 359 |
/* Use new ordering throughout the program */ |
| 360 |
bi_foreach_instr_global(ctx, I) { |
| 361 |
bi_foreach_src(I, s) { |
| 362 |
if (bi_is_uniform(I->src[s])) { |
| 363 |
unsigned node = bi_uniform_word(I->src[s]); |
| 364 |
unsigned new_node = old_to_new[node]; |
| 365 |
I->src[s].value = BIR_FAU_UNIFORM | (new_node >> 1); |
| 366 |
I->src[s].offset = new_node & 1; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* Use new ordering for push */ |
| 372 |
struct pan_ubo_push old = *push; |
| 373 |
for (unsigned i = 0; i < pushed; ++i) |
| 374 |
push->words[push_offset + i] = old.words[ordering[i]]; |
| 375 |
|
| 376 |
push->count = push_offset + pushed; |
| 377 |
} |