| 1 |
/* |
| 2 |
* Copyright 2021 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "util/bitset.h" |
| 7 |
#include "util/macros.h" |
| 8 |
#include "util/sparse_bitset.h" |
| 9 |
#include "util/u_dynarray.h" |
| 10 |
#include "util/u_math.h" |
| 11 |
#include "util/u_memory.h" |
| 12 |
#include "util/u_qsort.h" |
| 13 |
#include "agx_builder.h" |
| 14 |
#include "agx_compile.h" |
| 15 |
#include "agx_compiler.h" |
| 16 |
#include "agx_debug.h" |
| 17 |
#include "agx_opcodes.h" |
| 18 |
#include "shader_enums.h" |
| 19 |
|
| 20 |
/* SSA-based register allocator */ |
| 21 |
struct phi_web_node { |
| 22 |
/* Parent index, or circular for root */ |
| 23 |
uint32_t parent; |
| 24 |
|
| 25 |
/* If root, assigned register, or ~0 if no register assigned. */ |
| 26 |
uint16_t reg; |
| 27 |
bool assigned; |
| 28 |
|
| 29 |
/* Rank, at most log2(n) so need ~5-bits */ |
| 30 |
uint8_t rank; |
| 31 |
}; |
| 32 |
static_assert(sizeof(struct phi_web_node) == 8, "packed"); |
| 33 |
|
| 34 |
static unsigned |
| 35 |
phi_web_find(struct phi_web_node *web, unsigned x) |
| 36 |
{ |
| 37 |
if (web[x].parent == x) { |
| 38 |
/* Root */ |
| 39 |
return x; |
| 40 |
} else { |
| 41 |
/* Search up the tree */ |
| 42 |
unsigned root = x; |
| 43 |
while (web[root].parent != root) |
| 44 |
root = web[root].parent; |
| 45 |
|
| 46 |
/* Compress path. Second pass ensures O(1) memory usage. */ |
| 47 |
while (web[x].parent != x) { |
| 48 |
unsigned temp = web[x].parent; |
| 49 |
web[x].parent = root; |
| 50 |
x = temp; |
| 51 |
} |
| 52 |
|
| 53 |
return root; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
phi_web_union(struct phi_web_node *web, unsigned x, unsigned y) |
| 59 |
{ |
| 60 |
x = phi_web_find(web, x); |
| 61 |
y = phi_web_find(web, y); |
| 62 |
|
| 63 |
if (x == y) |
| 64 |
return; |
| 65 |
|
| 66 |
/* Union-by-rank: ensure x.rank >= y.rank */ |
| 67 |
if (web[x].rank < web[y].rank) { |
| 68 |
SWAP(x, y); |
| 69 |
} |
| 70 |
|
| 71 |
web[y].parent = x; |
| 72 |
|
| 73 |
/* Increment rank if necessary */ |
| 74 |
if (web[x].rank == web[y].rank) { |
| 75 |
web[x].rank++; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
struct ra_ctx { |
| 80 |
agx_context *shader; |
| 81 |
agx_block *block; |
| 82 |
agx_instr *instr; |
| 83 |
uint16_t *ssa_to_reg; |
| 84 |
uint8_t *ncomps; |
| 85 |
uint8_t *ncomps_unrounded; |
| 86 |
enum agx_size *sizes; |
| 87 |
enum ra_class *classes; |
| 88 |
BITSET_WORD *visited; |
| 89 |
BITSET_WORD *used_regs[RA_CLASSES]; |
| 90 |
|
| 91 |
/* Were any sources killed early this instruction? We assert this is not true |
| 92 |
* when shuffling. |
| 93 |
*/ |
| 94 |
bool early_killed; |
| 95 |
|
| 96 |
/* Maintained while assigning registers. Count of registers required, i.e. |
| 97 |
* the maximum register assigned + 1. |
| 98 |
*/ |
| 99 |
unsigned *count[RA_CLASSES]; |
| 100 |
|
| 101 |
/* For affinities */ |
| 102 |
agx_instr **src_to_collect_phi; |
| 103 |
struct phi_web_node *phi_web; |
| 104 |
|
| 105 |
/* If bit i of used_regs is set, and register i is the first consecutive |
| 106 |
* register holding an SSA value, then reg_to_ssa[i] is the SSA index of the |
| 107 |
* value currently in register i. |
| 108 |
* |
| 109 |
* Only for GPRs. We can add reg classes later if we have a use case. |
| 110 |
*/ |
| 111 |
uint32_t reg_to_ssa[AGX_NUM_REGS]; |
| 112 |
|
| 113 |
/* Maximum number of registers that RA is allowed to use */ |
| 114 |
unsigned bound[RA_CLASSES]; |
| 115 |
}; |
| 116 |
|
| 117 |
/* |
| 118 |
* RA treats the nesting counter, the divergent shuffle temporary, and the |
| 119 |
* spiller temporaries as alive throughout if used anywhere. This could be |
| 120 |
* optimized. Using a single power-of-two reserved region at the start ensures |
| 121 |
* these registers are never shuffled. |
| 122 |
*/ |
| 123 |
static unsigned |
| 124 |
reserved_size(agx_context *ctx) |
| 125 |
{ |
| 126 |
if (ctx->has_spill_pcopy_reserved) |
| 127 |
return 8; |
| 128 |
else if (ctx->any_quad_divergent_shuffle) |
| 129 |
return 2; |
| 130 |
else if (ctx->any_cf) |
| 131 |
return 1; |
| 132 |
else |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
UNUSED static void |
| 137 |
print_reg_file(struct ra_ctx *rctx, FILE *fp) |
| 138 |
{ |
| 139 |
unsigned reserved = reserved_size(rctx->shader); |
| 140 |
|
| 141 |
/* Dump the contents */ |
| 142 |
for (unsigned i = reserved; i < rctx->bound[RA_GPR]; ++i) { |
| 143 |
if (BITSET_TEST(rctx->used_regs[RA_GPR], i)) { |
| 144 |
uint32_t ssa = rctx->reg_to_ssa[i]; |
| 145 |
unsigned n = rctx->ncomps[ssa]; |
| 146 |
fprintf(fp, "h%u...%u: %u\n", i, i + n - 1, ssa); |
| 147 |
i += (n - 1); |
| 148 |
} |
| 149 |
} |
| 150 |
fprintf(fp, "\n"); |
| 151 |
|
| 152 |
/* Dump a visualization of the sizes to understand what live range |
| 153 |
* splitting is up against. |
| 154 |
*/ |
| 155 |
for (unsigned i = 0; i < rctx->bound[RA_GPR]; ++i) { |
| 156 |
/* Space out 16-bit vec4s */ |
| 157 |
if (i && (i % 4) == 0) { |
| 158 |
fprintf(fp, " "); |
| 159 |
} |
| 160 |
|
| 161 |
if (i < reserved) { |
| 162 |
fprintf(fp, "-"); |
| 163 |
} else if (BITSET_TEST(rctx->used_regs[RA_GPR], i)) { |
| 164 |
uint32_t ssa = rctx->reg_to_ssa[i]; |
| 165 |
unsigned n = rctx->ncomps[ssa]; |
| 166 |
for (unsigned j = 0; j < n; ++j) { |
| 167 |
assert(n < 10); |
| 168 |
fprintf(fp, "%u", n); |
| 169 |
} |
| 170 |
|
| 171 |
i += (n - 1); |
| 172 |
} else { |
| 173 |
fprintf(fp, "."); |
| 174 |
} |
| 175 |
} |
| 176 |
fprintf(fp, "\n\n"); |
| 177 |
} |
| 178 |
|
| 179 |
enum agx_size |
| 180 |
agx_split_width(const agx_instr *I) |
| 181 |
{ |
| 182 |
enum agx_size width = ~0; |
| 183 |
|
| 184 |
agx_foreach_dest(I, d) { |
| 185 |
if (I->dest[d].type == AGX_INDEX_NULL) |
| 186 |
continue; |
| 187 |
else if (width != ~0) |
| 188 |
assert(width == I->dest[d].size); |
| 189 |
else |
| 190 |
width = I->dest[d].size; |
| 191 |
} |
| 192 |
|
| 193 |
assert(width != ~0 && "should have been DCE'd"); |
| 194 |
return width; |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
* Calculate register demand in 16-bit registers, while gathering widths and |
| 199 |
* classes. Becuase we allocate in SSA, this calculation is exact in |
| 200 |
* linear-time. Depends on liveness information. |
| 201 |
*/ |
| 202 |
static unsigned |
| 203 |
agx_calc_register_demand(agx_context *ctx, bool remat) |
| 204 |
{ |
| 205 |
/* Print detailed demand calculation, helpful to debug spilling */ |
| 206 |
bool debug = false; |
| 207 |
|
| 208 |
if (debug) { |
| 209 |
agx_print_shader(ctx, stdout); |
| 210 |
} |
| 211 |
|
| 212 |
uint8_t *widths = calloc(ctx->alloc, sizeof(uint8_t)); |
| 213 |
enum ra_class *classes = calloc(ctx->alloc, sizeof(enum ra_class)); |
| 214 |
|
| 215 |
agx_foreach_instr_global(ctx, I) { |
| 216 |
if (I->op == AGX_OPCODE_MOV_IMM && remat) |
| 217 |
continue; |
| 218 |
|
| 219 |
agx_foreach_ssa_dest(I, d) { |
| 220 |
unsigned v = I->dest[d].value; |
| 221 |
assert(widths[v] == 0 && "broken SSA"); |
| 222 |
/* Round up vectors for easier live range splitting */ |
| 223 |
widths[v] = util_next_power_of_two(agx_index_size_16(I->dest[d])); |
| 224 |
classes[v] = ra_class_for_index(I->dest[d]); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/* Calculate demand at the start of each block based on live-in, then update |
| 229 |
* for each instruction processed. Calculate rolling maximum. |
| 230 |
*/ |
| 231 |
unsigned max_demand = 0; |
| 232 |
|
| 233 |
agx_foreach_block(ctx, block) { |
| 234 |
unsigned demand = reserved_size(ctx); |
| 235 |
|
| 236 |
/* Everything live-in */ |
| 237 |
U_SPARSE_BITSET_FOREACH_SET(&block->live_in, i) { |
| 238 |
if (classes[i] == RA_GPR) |
| 239 |
demand += widths[i]; |
| 240 |
} |
| 241 |
|
| 242 |
max_demand = MAX2(demand, max_demand); |
| 243 |
|
| 244 |
/* To handle late-kill sources, this counter tracks the number of |
| 245 |
* registers to be freed after 1 extra instruction. |
| 246 |
*/ |
| 247 |
unsigned late_kill_count = 0; |
| 248 |
|
| 249 |
if (debug) { |
| 250 |
printf("\n"); |
| 251 |
} |
| 252 |
|
| 253 |
agx_foreach_instr_in_block(block, I) { |
| 254 |
/* Phis happen in parallel and are already accounted for in the live-in |
| 255 |
* set, just skip them so we don't double count. |
| 256 |
*/ |
| 257 |
if (I->op == AGX_OPCODE_PHI) |
| 258 |
continue; |
| 259 |
|
| 260 |
if (I->op == AGX_OPCODE_MOV_IMM && remat) |
| 261 |
continue; |
| 262 |
|
| 263 |
if (debug) { |
| 264 |
printf("%u: ", demand); |
| 265 |
agx_print_instr(I, stdout); |
| 266 |
} |
| 267 |
|
| 268 |
if (I->op == AGX_OPCODE_PRELOAD) { |
| 269 |
unsigned size = agx_size_align_16(I->src[0].size); |
| 270 |
max_demand = MAX2(max_demand, I->src[0].value + size); |
| 271 |
} else if (I->op == AGX_OPCODE_EXPORT) { |
| 272 |
unsigned size = agx_size_align_16(I->src[0].size); |
| 273 |
max_demand = MAX2(max_demand, I->imm + size); |
| 274 |
} |
| 275 |
|
| 276 |
/* Handle late-kill registers from last instruction */ |
| 277 |
demand -= late_kill_count; |
| 278 |
late_kill_count = 0; |
| 279 |
|
| 280 |
/* Late-kill sources the first time we see them. This simplifies RA. We |
| 281 |
* could optimize to early-kill in some situations if we wanted. |
| 282 |
*/ |
| 283 |
agx_foreach_src(I, s) { |
| 284 |
if (!I->src[s].kill) |
| 285 |
continue; |
| 286 |
assert(I->src[s].type == AGX_INDEX_NORMAL); |
| 287 |
if (ra_class_for_index(I->src[s]) != RA_GPR) |
| 288 |
continue; |
| 289 |
|
| 290 |
bool skip = false; |
| 291 |
|
| 292 |
for (unsigned backwards = 0; backwards < s; ++backwards) { |
| 293 |
if (agx_is_equiv(I->src[backwards], I->src[s])) { |
| 294 |
skip = true; |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (!skip) |
| 300 |
late_kill_count += widths[I->src[s].value]; |
| 301 |
} |
| 302 |
|
| 303 |
/* Make destinations live */ |
| 304 |
agx_foreach_ssa_dest(I, d) { |
| 305 |
if (ra_class_for_index(I->dest[d]) != RA_GPR) |
| 306 |
continue; |
| 307 |
|
| 308 |
/* Live range splits allocate at power-of-two granularity. Round up |
| 309 |
* destination sizes (temporarily) to powers-of-two. |
| 310 |
*/ |
| 311 |
unsigned real_width = widths[I->dest[d].value]; |
| 312 |
unsigned pot_width = util_next_power_of_two(real_width); |
| 313 |
|
| 314 |
demand += pot_width; |
| 315 |
late_kill_count += (pot_width - real_width); |
| 316 |
} |
| 317 |
|
| 318 |
max_demand = MAX2(demand, max_demand); |
| 319 |
} |
| 320 |
|
| 321 |
demand -= late_kill_count; |
| 322 |
} |
| 323 |
|
| 324 |
free(widths); |
| 325 |
free(classes); |
| 326 |
return max_demand; |
| 327 |
} |
| 328 |
|
| 329 |
static bool |
| 330 |
find_regs_simple(struct ra_ctx *rctx, enum ra_class cls, unsigned count, |
| 331 |
unsigned align, unsigned *out) |
| 332 |
{ |
| 333 |
for (unsigned reg = 0; reg + count <= rctx->bound[cls]; reg += align) { |
| 334 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, count)) { |
| 335 |
*out = reg; |
| 336 |
return true; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
/* |
| 344 |
* Search the register file for the best contiguous aligned region of the given |
| 345 |
* size to evict when shuffling registers. The region must not contain any |
| 346 |
* register marked in the passed bitset. |
| 347 |
* |
| 348 |
* As a hint, this also takes in the set of registers from killed sources passed |
| 349 |
* to this instruction. These should be deprioritized, since they are more |
| 350 |
* expensive to use (extra moves to shuffle the contents away). |
| 351 |
* |
| 352 |
* Precondition: such a region exists. |
| 353 |
* |
| 354 |
* Postcondition: at least one register in the returned region is already free. |
| 355 |
*/ |
| 356 |
static unsigned |
| 357 |
find_best_region_to_evict(struct ra_ctx *rctx, unsigned size, |
| 358 |
BITSET_WORD *already_evicted) |
| 359 |
{ |
| 360 |
assert(util_is_power_of_two_or_zero(size) && "precondition"); |
| 361 |
assert((rctx->bound[RA_GPR] % size) == 0 && |
| 362 |
"register file size must be aligned to the maximum vector size"); |
| 363 |
|
| 364 |
/* Useful for testing RA */ |
| 365 |
bool invert = false; |
| 366 |
|
| 367 |
unsigned best_base = ~0; |
| 368 |
unsigned best_moves = invert ? 0 : ~0; |
| 369 |
|
| 370 |
for (unsigned base = 0; base + size <= rctx->bound[RA_GPR]; base += size) { |
| 371 |
/* The first k registers are preallocated and unevictable, so must be |
| 372 |
* skipped. By itself, this does not pose a problem. We are allocating n |
| 373 |
* registers, but this region has at most n-k free. Since there are at |
| 374 |
* least n free registers total, there is at least k free registers |
| 375 |
* outside this region. Choose any such free register. The region |
| 376 |
* containing it has at most n-1 occupied registers. In the worst case, |
| 377 |
* n-k of those registers are are moved to the beginning region and the |
| 378 |
* remaining (n-1)-(n-k) = k-1 registers are moved to the k-1 free |
| 379 |
* registers in other regions, given there are k free registers total. |
| 380 |
* These recursive shuffles work out because everything is power-of-two |
| 381 |
* sized and naturally aligned, so the sizes shuffled are strictly |
| 382 |
* descending. So, we do not need extra registers to handle "single |
| 383 |
* region" unevictability. |
| 384 |
*/ |
| 385 |
if (base < reserved_size(rctx->shader)) |
| 386 |
continue; |
| 387 |
|
| 388 |
/* Do not evict the same register multiple times. It's not necessary since |
| 389 |
* we're just shuffling, there are enough free registers elsewhere. |
| 390 |
*/ |
| 391 |
if (BITSET_TEST_COUNT(already_evicted, base, size)) |
| 392 |
continue; |
| 393 |
|
| 394 |
/* Estimate the number of moves required if we pick this region */ |
| 395 |
unsigned moves = 0; |
| 396 |
bool any_free = false; |
| 397 |
|
| 398 |
for (unsigned reg = base; reg < base + size; ++reg) { |
| 399 |
/* We need a move for each blocked register (TODO: we only need a |
| 400 |
* single move for 32-bit pairs, could optimize to use that instead.) |
| 401 |
*/ |
| 402 |
if (BITSET_TEST(rctx->used_regs[RA_GPR], reg)) |
| 403 |
moves++; |
| 404 |
else |
| 405 |
any_free = true; |
| 406 |
} |
| 407 |
|
| 408 |
/* Pick the region requiring fewest moves as a heuristic. Regions with no |
| 409 |
* free registers are skipped, since the recursive splitting algorithm |
| 410 |
* requires at least one free register. |
| 411 |
*/ |
| 412 |
if (any_free && ((moves < best_moves) ^ invert)) { |
| 413 |
best_moves = moves; |
| 414 |
best_base = base; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
assert(best_base < rctx->bound[RA_GPR] && |
| 419 |
"not enough registers (should have spilled already)"); |
| 420 |
return best_base; |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
set_ssa_to_reg(struct ra_ctx *rctx, unsigned ssa, unsigned reg) |
| 425 |
{ |
| 426 |
enum ra_class cls = rctx->classes[ssa]; |
| 427 |
*(rctx->count[cls]) = MAX2(*(rctx->count[cls]), reg + rctx->ncomps[ssa]); |
| 428 |
|
| 429 |
rctx->ssa_to_reg[ssa] = reg; |
| 430 |
|
| 431 |
if (cls == RA_GPR) { |
| 432 |
rctx->reg_to_ssa[reg] = ssa; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
/* |
| 437 |
* Insert parallel copies to move an SSA variable `var` to a new register |
| 438 |
* `new_reg`. This may require scalarizing. |
| 439 |
*/ |
| 440 |
static void |
| 441 |
insert_copy(struct ra_ctx *rctx, struct util_dynarray *copies, unsigned new_reg, |
| 442 |
unsigned var) |
| 443 |
{ |
| 444 |
enum agx_size size = rctx->sizes[var]; |
| 445 |
unsigned align = agx_size_align_16(size); |
| 446 |
|
| 447 |
for (unsigned i = 0; i < rctx->ncomps[var]; i += align) { |
| 448 |
struct agx_copy copy = { |
| 449 |
.dest = new_reg + i, |
| 450 |
.src = agx_register(rctx->ssa_to_reg[var] + i, size), |
| 451 |
}; |
| 452 |
|
| 453 |
assert((copy.dest % align) == 0 && "new dest must be aligned"); |
| 454 |
assert((copy.src.value % align) == 0 && "src must be aligned"); |
| 455 |
util_dynarray_append(copies, copy); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
static unsigned |
| 460 |
assign_regs_by_copying(struct ra_ctx *rctx, agx_index dest, const agx_instr *I, |
| 461 |
struct util_dynarray *copies) |
| 462 |
{ |
| 463 |
BITSET_DECLARE(clobbered, AGX_NUM_REGS) = {0}; |
| 464 |
assert(dest.type == AGX_INDEX_NORMAL); |
| 465 |
|
| 466 |
/* Initialize the worklist with the variable we're assigning */ |
| 467 |
unsigned blocked_vars[16] = {dest.value}; |
| 468 |
size_t nr_blocked = 1; |
| 469 |
|
| 470 |
while (nr_blocked > 0) { |
| 471 |
/* Grab the largest var. TODO: Consider not writing O(N^2) code. */ |
| 472 |
uint32_t ssa = ~0, nr = 0, chosen_idx = ~0; |
| 473 |
for (unsigned i = 0; i < nr_blocked; ++i) { |
| 474 |
uint32_t this_ssa = blocked_vars[i]; |
| 475 |
uint32_t this_nr = rctx->ncomps[this_ssa]; |
| 476 |
|
| 477 |
if (this_nr > nr) { |
| 478 |
nr = this_nr; |
| 479 |
ssa = this_ssa; |
| 480 |
chosen_idx = i; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
assert(ssa != ~0 && nr > 0 && "must have found something"); |
| 485 |
assert(chosen_idx < nr_blocked && "must have found something"); |
| 486 |
|
| 487 |
/* Pop it from the work list by swapping in the last element */ |
| 488 |
blocked_vars[chosen_idx] = blocked_vars[--nr_blocked]; |
| 489 |
|
| 490 |
/* We need to shuffle some variables to make room. Look for a range of |
| 491 |
* the register file that is partially blocked. |
| 492 |
*/ |
| 493 |
unsigned new_reg = find_best_region_to_evict(rctx, nr, clobbered); |
| 494 |
|
| 495 |
/* Blocked registers need to get reassigned. Add them to the worklist. */ |
| 496 |
for (unsigned i = 0; i < nr; ++i) { |
| 497 |
if (BITSET_TEST(rctx->used_regs[RA_GPR], new_reg + i)) { |
| 498 |
unsigned blocked_reg = new_reg + i; |
| 499 |
uint32_t blocked_ssa = rctx->reg_to_ssa[blocked_reg]; |
| 500 |
uint32_t blocked_nr = rctx->ncomps[blocked_ssa]; |
| 501 |
|
| 502 |
assert(blocked_nr >= 1 && "must be assigned"); |
| 503 |
|
| 504 |
blocked_vars[nr_blocked++] = blocked_ssa; |
| 505 |
assert( |
| 506 |
rctx->ssa_to_reg[blocked_ssa] == blocked_reg && |
| 507 |
"variable must start within the range, since vectors are limited"); |
| 508 |
|
| 509 |
for (unsigned j = 0; j < blocked_nr; ++j) { |
| 510 |
assert( |
| 511 |
BITSET_TEST(rctx->used_regs[RA_GPR], new_reg + i + j) && |
| 512 |
"variable is allocated contiguous and vectors are limited, " |
| 513 |
"so evicted in full"); |
| 514 |
} |
| 515 |
|
| 516 |
/* Skip to the next variable */ |
| 517 |
i += blocked_nr - 1; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/* We are going to allocate to this range, so it is now fully used. Mark |
| 522 |
* it as such so we don't reassign here later. |
| 523 |
*/ |
| 524 |
BITSET_SET_COUNT(rctx->used_regs[RA_GPR], new_reg, nr); |
| 525 |
|
| 526 |
/* The first iteration is special: it is the original allocation of a |
| 527 |
* variable. All subsequent iterations pick a new register for a blocked |
| 528 |
* variable. For those, copy the blocked variable to its new register. |
| 529 |
*/ |
| 530 |
if (ssa != dest.value) { |
| 531 |
insert_copy(rctx, copies, new_reg, ssa); |
| 532 |
} |
| 533 |
|
| 534 |
/* Mark down the set of clobbered registers, so that killed sources may be |
| 535 |
* handled correctly later. |
| 536 |
*/ |
| 537 |
BITSET_SET_COUNT(clobbered, new_reg, nr); |
| 538 |
|
| 539 |
/* Update bookkeeping for this variable */ |
| 540 |
set_ssa_to_reg(rctx, ssa, new_reg); |
| 541 |
} |
| 542 |
|
| 543 |
return rctx->ssa_to_reg[dest.value]; |
| 544 |
} |
| 545 |
|
| 546 |
static int |
| 547 |
sort_by_size(const void *a_, const void *b_, void *sizes_) |
| 548 |
{ |
| 549 |
const enum agx_size *sizes = sizes_; |
| 550 |
const unsigned *a = a_, *b = b_; |
| 551 |
|
| 552 |
return sizes[*b] - sizes[*a]; |
| 553 |
} |
| 554 |
|
| 555 |
/* |
| 556 |
* When shuffling registers to assign a phi destination, we can't simply insert |
| 557 |
* the required moves before the phi, since phis happen in parallel along the |
| 558 |
* edge. Instead, there are two cases: |
| 559 |
* |
| 560 |
* 1. The source of the copy is the destination of a phi. Since we are |
| 561 |
* emitting shuffle code, there will be no more reads of that destination |
| 562 |
* with the old register. Since the phis all happen in parallel and writes |
| 563 |
* precede reads, there was no previous read of that destination either. So |
| 564 |
* the old destination is dead. Just replace the phi's destination with the |
| 565 |
* moves's destination instead. |
| 566 |
* |
| 567 |
* 2. Otherwise, the source of the copy is a live-in value, since it's |
| 568 |
* live when assigning phis at the start of a block but it is not a phi. |
| 569 |
* If we move in parallel with the phi, the phi will still read the correct |
| 570 |
* old register regardless and the destinations can't alias. So, insert a phi |
| 571 |
* to do the copy in parallel along the incoming edges. |
| 572 |
*/ |
| 573 |
static void |
| 574 |
agx_emit_move_before_phi(agx_context *ctx, agx_block *block, |
| 575 |
struct agx_copy *copy) |
| 576 |
{ |
| 577 |
assert(!copy->dest_mem && !copy->src.memory && "no memory shuffles"); |
| 578 |
|
| 579 |
/* Look for the phi writing the destination */ |
| 580 |
agx_foreach_phi_in_block(block, phi) { |
| 581 |
if (agx_is_equiv(agx_as_register(phi->dest[0]), copy->src) && |
| 582 |
!phi->dest[0].memory) { |
| 583 |
|
| 584 |
phi->dest[0].reg = copy->dest; |
| 585 |
return; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/* There wasn't such a phi, so it's live-in. Insert a phi instead. */ |
| 590 |
agx_builder b = agx_init_builder(ctx, agx_before_block(block)); |
| 591 |
|
| 592 |
agx_instr *phi = agx_phi_to(&b, agx_register_like(copy->dest, copy->src), |
| 593 |
agx_num_predecessors(block)); |
| 594 |
assert(!copy->src.kill); |
| 595 |
|
| 596 |
agx_foreach_src(phi, s) { |
| 597 |
phi->src[s] = copy->src; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
static unsigned |
| 602 |
find_regs(struct ra_ctx *rctx, agx_instr *I, unsigned dest_idx, unsigned count, |
| 603 |
unsigned align) |
| 604 |
{ |
| 605 |
unsigned reg; |
| 606 |
assert(count == align); |
| 607 |
|
| 608 |
enum ra_class cls = ra_class_for_index(I->dest[dest_idx]); |
| 609 |
|
| 610 |
if (find_regs_simple(rctx, cls, count, align, ®)) { |
| 611 |
return reg; |
| 612 |
} else { |
| 613 |
assert(!rctx->early_killed && "no live range splits with early kill"); |
| 614 |
assert(cls == RA_GPR && "no memory live range splits"); |
| 615 |
|
| 616 |
struct util_dynarray copies = UTIL_DYNARRAY_INIT; |
| 617 |
|
| 618 |
reg = assign_regs_by_copying(rctx, I->dest[dest_idx], I, &copies); |
| 619 |
|
| 620 |
/* Insert the necessary copies. Phis need special handling since we can't |
| 621 |
* insert instructions before the phi. |
| 622 |
*/ |
| 623 |
if (I->op == AGX_OPCODE_PHI) { |
| 624 |
util_dynarray_foreach(&copies, struct agx_copy, copy) { |
| 625 |
agx_emit_move_before_phi(rctx->shader, rctx->block, copy); |
| 626 |
} |
| 627 |
} else { |
| 628 |
agx_builder b = agx_init_builder(rctx->shader, agx_before_instr(I)); |
| 629 |
agx_emit_parallel_copies( |
| 630 |
&b, copies.data, |
| 631 |
util_dynarray_num_elements(&copies, struct agx_copy)); |
| 632 |
} |
| 633 |
|
| 634 |
util_dynarray_fini(&copies); |
| 635 |
|
| 636 |
/* assign_regs asserts this is cleared, so clear to be reassigned */ |
| 637 |
BITSET_CLEAR_COUNT(rctx->used_regs[cls], reg, count); |
| 638 |
return reg; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
static uint32_t |
| 643 |
search_ssa_to_reg_out(struct ra_ctx *ctx, struct agx_block *blk, |
| 644 |
enum ra_class cls, unsigned ssa) |
| 645 |
{ |
| 646 |
for (unsigned reg = 0; reg < ctx->bound[cls]; ++reg) { |
| 647 |
if (blk->reg_to_ssa_out[cls][reg] == ssa) |
| 648 |
return reg; |
| 649 |
} |
| 650 |
|
| 651 |
UNREACHABLE("variable not defined in block"); |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* Loop over live-in values at the start of the block and mark their registers |
| 656 |
* as in-use. We process blocks in dominance order, so this handles everything |
| 657 |
* but loop headers. |
| 658 |
* |
| 659 |
* For loop headers, this handles the forward edges but not the back edge. |
| 660 |
* However, that's okay: we don't want to reserve the registers that are |
| 661 |
* defined within the loop, because then we'd get a contradiction. Instead we |
| 662 |
* leave them available and then they become fixed points of a sort. |
| 663 |
*/ |
| 664 |
static void |
| 665 |
reserve_live_in(struct ra_ctx *rctx) |
| 666 |
{ |
| 667 |
/* If there are no predecessors, there is nothing live-in */ |
| 668 |
unsigned nr_preds = agx_num_predecessors(rctx->block); |
| 669 |
if (nr_preds == 0) |
| 670 |
return; |
| 671 |
|
| 672 |
agx_builder b = |
| 673 |
agx_init_builder(rctx->shader, agx_before_block(rctx->block)); |
| 674 |
|
| 675 |
U_SPARSE_BITSET_FOREACH_SET(&rctx->block->live_in, i) { |
| 676 |
/* Skip values defined in loops when processing the loop header */ |
| 677 |
if (!BITSET_TEST(rctx->visited, i)) |
| 678 |
continue; |
| 679 |
|
| 680 |
unsigned base; |
| 681 |
enum ra_class cls = rctx->classes[i]; |
| 682 |
enum agx_size size = rctx->sizes[i]; |
| 683 |
|
| 684 |
/* We need to use the unrounded channel count, since the extra padding |
| 685 |
* will be uninitialized and would fail RA validation. |
| 686 |
*/ |
| 687 |
unsigned channels = rctx->ncomps_unrounded[i] / agx_size_align_16(size); |
| 688 |
|
| 689 |
/* If we split live ranges, the variable might be defined differently at |
| 690 |
* the end of each predecessor. Join them together with a phi inserted at |
| 691 |
* the start of the block. |
| 692 |
*/ |
| 693 |
if (nr_preds > 1) { |
| 694 |
/* We'll fill in the destination after, to coalesce one of the moves */ |
| 695 |
agx_instr *phi = agx_phi_to(&b, agx_null(), nr_preds); |
| 696 |
|
| 697 |
agx_foreach_predecessor(rctx->block, pred) { |
| 698 |
unsigned pred_idx = agx_predecessor_index(rctx->block, *pred); |
| 699 |
|
| 700 |
phi->src[pred_idx] = agx_get_vec_index(i, size, channels); |
| 701 |
phi->src[pred_idx].memory = cls == RA_MEM; |
| 702 |
|
| 703 |
if ((*pred)->reg_to_ssa_out[cls] == NULL) { |
| 704 |
/* If this is a loop header, we don't know where the register |
| 705 |
* will end up. So, we create a phi conservatively but don't fill |
| 706 |
* it in until the end of the loop. Stash in the information |
| 707 |
* we'll need to fill in the real register later. |
| 708 |
*/ |
| 709 |
assert(rctx->block->loop_header); |
| 710 |
} else { |
| 711 |
/* Otherwise, we can build the phi now */ |
| 712 |
phi->src[pred_idx].reg = |
| 713 |
search_ssa_to_reg_out(rctx, *pred, cls, i); |
| 714 |
phi->src[pred_idx].has_reg = true; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
/* Pick the phi destination to coalesce a move. Predecessor ordering is |
| 719 |
* stable, so this means all live-in values get their registers from a |
| 720 |
* particular predecessor. That means that such a register allocation |
| 721 |
* is valid here, because it was valid in the predecessor. |
| 722 |
*/ |
| 723 |
assert(phi->src[0].has_reg && "not loop source"); |
| 724 |
phi->dest[0] = phi->src[0]; |
| 725 |
base = phi->dest[0].reg; |
| 726 |
} else { |
| 727 |
/* If we don't emit a phi, there is already a unique register */ |
| 728 |
assert(nr_preds == 1); |
| 729 |
|
| 730 |
agx_block **pred = util_dynarray_begin(&rctx->block->predecessors); |
| 731 |
/* TODO: Flip logic to eliminate the search */ |
| 732 |
base = search_ssa_to_reg_out(rctx, *pred, cls, i); |
| 733 |
} |
| 734 |
|
| 735 |
set_ssa_to_reg(rctx, i, base); |
| 736 |
|
| 737 |
for (unsigned j = 0; j < rctx->ncomps[i]; ++j) { |
| 738 |
BITSET_SET(rctx->used_regs[cls], base + j); |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
static void |
| 744 |
assign_regs(struct ra_ctx *rctx, agx_index v, unsigned reg) |
| 745 |
{ |
| 746 |
enum ra_class cls = ra_class_for_index(v); |
| 747 |
assert(reg < rctx->bound[cls] && "must not overflow register file"); |
| 748 |
assert(v.type == AGX_INDEX_NORMAL && "only SSA gets registers allocated"); |
| 749 |
set_ssa_to_reg(rctx, v.value, reg); |
| 750 |
|
| 751 |
assert(!BITSET_TEST(rctx->visited, v.value) && "SSA violated"); |
| 752 |
BITSET_SET(rctx->visited, v.value); |
| 753 |
|
| 754 |
unsigned nr = rctx->ncomps[v.value]; |
| 755 |
assert(nr >= 1); |
| 756 |
assert(!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, nr) && |
| 757 |
"no interference"); |
| 758 |
|
| 759 |
BITSET_SET_COUNT(rctx->used_regs[cls], reg, nr); |
| 760 |
|
| 761 |
/* Phi webs need to remember which register they're assigned to */ |
| 762 |
struct phi_web_node *node = |
| 763 |
&rctx->phi_web[phi_web_find(rctx->phi_web, v.value)]; |
| 764 |
|
| 765 |
if (!node->assigned) { |
| 766 |
node->reg = reg; |
| 767 |
node->assigned = true; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
static void |
| 772 |
agx_set_sources(struct ra_ctx *rctx, agx_instr *I) |
| 773 |
{ |
| 774 |
assert(I->op != AGX_OPCODE_PHI); |
| 775 |
|
| 776 |
agx_foreach_ssa_src(I, s) { |
| 777 |
assert(BITSET_TEST(rctx->visited, I->src[s].value) && "no phis"); |
| 778 |
|
| 779 |
I->src[s].reg = rctx->ssa_to_reg[I->src[s].value]; |
| 780 |
I->src[s].has_reg = true; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
static void |
| 785 |
agx_set_dests(struct ra_ctx *rctx, agx_instr *I) |
| 786 |
{ |
| 787 |
agx_foreach_ssa_dest(I, s) { |
| 788 |
I->dest[s].reg = rctx->ssa_to_reg[I->dest[s].value]; |
| 789 |
I->dest[s].has_reg = true; |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
static unsigned |
| 794 |
affinity_base_of_collect(struct ra_ctx *rctx, agx_instr *collect, unsigned src) |
| 795 |
{ |
| 796 |
unsigned src_reg = rctx->ssa_to_reg[collect->src[src].value]; |
| 797 |
unsigned src_offset = src * agx_size_align_16(collect->src[src].size); |
| 798 |
|
| 799 |
if (src_reg >= src_offset) |
| 800 |
return src_reg - src_offset; |
| 801 |
else |
| 802 |
return ~0; |
| 803 |
} |
| 804 |
|
| 805 |
static bool |
| 806 |
try_coalesce_with(struct ra_ctx *rctx, agx_index ssa, unsigned count, |
| 807 |
bool may_be_unvisited, unsigned *out) |
| 808 |
{ |
| 809 |
assert(ssa.type == AGX_INDEX_NORMAL); |
| 810 |
if (!BITSET_TEST(rctx->visited, ssa.value)) { |
| 811 |
assert(may_be_unvisited); |
| 812 |
return false; |
| 813 |
} |
| 814 |
|
| 815 |
unsigned base = rctx->ssa_to_reg[ssa.value]; |
| 816 |
enum ra_class cls = ra_class_for_index(ssa); |
| 817 |
|
| 818 |
if (BITSET_TEST_COUNT(rctx->used_regs[cls], base, count)) |
| 819 |
return false; |
| 820 |
|
| 821 |
assert(base + count <= rctx->bound[cls] && "invariant"); |
| 822 |
*out = base; |
| 823 |
return true; |
| 824 |
} |
| 825 |
|
| 826 |
static unsigned |
| 827 |
pick_regs(struct ra_ctx *rctx, agx_instr *I, unsigned d) |
| 828 |
{ |
| 829 |
agx_index idx = I->dest[d]; |
| 830 |
enum ra_class cls = ra_class_for_index(idx); |
| 831 |
assert(idx.type == AGX_INDEX_NORMAL); |
| 832 |
|
| 833 |
unsigned count = rctx->ncomps[idx.value]; |
| 834 |
assert(count >= 1); |
| 835 |
|
| 836 |
unsigned align = count; |
| 837 |
|
| 838 |
/* Try to allocate entire phi webs compatibly */ |
| 839 |
unsigned phi_idx = phi_web_find(rctx->phi_web, idx.value); |
| 840 |
if (rctx->phi_web[phi_idx].assigned) { |
| 841 |
unsigned reg = rctx->phi_web[phi_idx].reg; |
| 842 |
if ((reg % align) == 0 && reg + align < rctx->bound[cls] && |
| 843 |
!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, align)) |
| 844 |
return reg; |
| 845 |
} |
| 846 |
|
| 847 |
/* Try to allocate moves compatibly with their sources */ |
| 848 |
if (I->op == AGX_OPCODE_MOV && I->src[0].type == AGX_INDEX_NORMAL && |
| 849 |
I->src[0].memory == I->dest[0].memory && |
| 850 |
I->src[0].size == I->dest[0].size) { |
| 851 |
|
| 852 |
unsigned out; |
| 853 |
if (try_coalesce_with(rctx, I->src[0], count, false, &out)) |
| 854 |
return out; |
| 855 |
} |
| 856 |
|
| 857 |
/* Try to allocate phis compatibly with their sources */ |
| 858 |
if (I->op == AGX_OPCODE_PHI) { |
| 859 |
agx_foreach_ssa_src(I, s) { |
| 860 |
/* Loop headers have phis with a source preceding the definition */ |
| 861 |
bool may_be_unvisited = rctx->block->loop_header; |
| 862 |
|
| 863 |
unsigned out; |
| 864 |
if (try_coalesce_with(rctx, I->src[s], count, may_be_unvisited, &out)) |
| 865 |
return out; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
/* Try to allocate collects compatibly with their sources */ |
| 870 |
if (I->op == AGX_OPCODE_COLLECT) { |
| 871 |
agx_foreach_ssa_src(I, s) { |
| 872 |
assert(BITSET_TEST(rctx->visited, I->src[s].value) && |
| 873 |
"registers assigned in an order compatible with dominance " |
| 874 |
"and this is not a phi node, so we have assigned a register"); |
| 875 |
|
| 876 |
unsigned base = affinity_base_of_collect(rctx, I, s); |
| 877 |
if (base >= rctx->bound[cls] || (base + count) > rctx->bound[cls]) |
| 878 |
continue; |
| 879 |
|
| 880 |
/* Unaligned destinations can happen when dest size > src size */ |
| 881 |
if (base % align) |
| 882 |
continue; |
| 883 |
|
| 884 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], base, count)) |
| 885 |
return base; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
/* Try to coalesce scalar exports */ |
| 890 |
agx_instr *collect_phi = rctx->src_to_collect_phi[idx.value]; |
| 891 |
if (collect_phi && collect_phi->op == AGX_OPCODE_EXPORT) { |
| 892 |
unsigned reg = collect_phi->imm; |
| 893 |
|
| 894 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, align) && |
| 895 |
(reg % align) == 0) |
| 896 |
return reg; |
| 897 |
} |
| 898 |
|
| 899 |
/* Try to coalesce vector exports */ |
| 900 |
if (collect_phi && collect_phi->op == AGX_OPCODE_SPLIT) { |
| 901 |
if (collect_phi->dest[0].type == AGX_INDEX_NORMAL) { |
| 902 |
agx_instr *exp = rctx->src_to_collect_phi[collect_phi->dest[0].value]; |
| 903 |
if (exp && exp->op == AGX_OPCODE_EXPORT) { |
| 904 |
unsigned reg = exp->imm; |
| 905 |
|
| 906 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, align) && |
| 907 |
(reg % align) == 0) |
| 908 |
return reg; |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
/* Try to allocate sources of collects contiguously */ |
| 914 |
if (collect_phi && collect_phi->op == AGX_OPCODE_COLLECT) { |
| 915 |
agx_instr *collect = collect_phi; |
| 916 |
|
| 917 |
assert(count == align && "collect sources are scalar"); |
| 918 |
|
| 919 |
/* Find our offset in the collect. If our source is repeated in the |
| 920 |
* collect, this may not be unique. We arbitrarily choose the first. |
| 921 |
*/ |
| 922 |
unsigned our_source = ~0; |
| 923 |
agx_foreach_ssa_src(collect, s) { |
| 924 |
if (agx_is_equiv(collect->src[s], idx)) { |
| 925 |
our_source = s; |
| 926 |
break; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
assert(our_source < collect->nr_srcs && "source must be in the collect"); |
| 931 |
|
| 932 |
/* See if we can allocate compatibly with any source of the collect */ |
| 933 |
agx_foreach_ssa_src(collect, s) { |
| 934 |
if (!BITSET_TEST(rctx->visited, collect->src[s].value)) |
| 935 |
continue; |
| 936 |
|
| 937 |
/* Determine where the collect should start relative to the source */ |
| 938 |
unsigned base = affinity_base_of_collect(rctx, collect, s); |
| 939 |
if (base >= rctx->bound[cls]) |
| 940 |
continue; |
| 941 |
|
| 942 |
unsigned our_reg = base + (our_source * align); |
| 943 |
|
| 944 |
/* Don't allocate past the end of the register file */ |
| 945 |
if ((our_reg + align) > rctx->bound[cls]) |
| 946 |
continue; |
| 947 |
|
| 948 |
/* If those registers are free, then choose them */ |
| 949 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], our_reg, align)) |
| 950 |
return our_reg; |
| 951 |
} |
| 952 |
|
| 953 |
unsigned collect_align = rctx->ncomps[collect->dest[0].value]; |
| 954 |
unsigned offset = our_source * align; |
| 955 |
|
| 956 |
/* Prefer ranges of the register file that leave room for all sources of |
| 957 |
* the collect contiguously. |
| 958 |
*/ |
| 959 |
for (unsigned base = 0; |
| 960 |
base + (collect->nr_srcs * align) <= rctx->bound[cls]; |
| 961 |
base += collect_align) { |
| 962 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], base, |
| 963 |
collect->nr_srcs * align)) |
| 964 |
return base + offset; |
| 965 |
} |
| 966 |
|
| 967 |
/* Try to respect the alignment requirement of the collect destination, |
| 968 |
* which may be greater than the sources (e.g. pack_64_2x32_split). Look |
| 969 |
* for a register for the source such that the collect base is aligned. |
| 970 |
*/ |
| 971 |
if (collect_align > align) { |
| 972 |
for (unsigned reg = offset; reg + collect_align <= rctx->bound[cls]; |
| 973 |
reg += collect_align) { |
| 974 |
if (!BITSET_TEST_COUNT(rctx->used_regs[cls], reg, count)) |
| 975 |
return reg; |
| 976 |
} |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
/* Try to allocate phi sources compatibly with their phis */ |
| 981 |
if (collect_phi && collect_phi->op == AGX_OPCODE_PHI) { |
| 982 |
agx_instr *phi = collect_phi; |
| 983 |
unsigned out; |
| 984 |
|
| 985 |
agx_foreach_ssa_src(phi, s) { |
| 986 |
if (try_coalesce_with(rctx, phi->src[s], count, true, &out)) |
| 987 |
return out; |
| 988 |
} |
| 989 |
|
| 990 |
/* If we're in a loop, we may have already allocated the phi. Try that. */ |
| 991 |
if (phi->dest[0].has_reg) { |
| 992 |
unsigned base = phi->dest[0].reg; |
| 993 |
|
| 994 |
if (base + count <= rctx->bound[cls] && |
| 995 |
!BITSET_TEST_COUNT(rctx->used_regs[cls], base, count)) |
| 996 |
return base; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
/* Default to any contiguous sequence of registers */ |
| 1001 |
return find_regs(rctx, I, d, count, align); |
| 1002 |
} |
| 1003 |
|
| 1004 |
static void |
| 1005 |
kill_source(struct ra_ctx *rctx, const agx_instr *I, unsigned s) |
| 1006 |
{ |
| 1007 |
enum ra_class cls = ra_class_for_index(I->src[s]); |
| 1008 |
unsigned reg = rctx->ssa_to_reg[I->src[s].value]; |
| 1009 |
unsigned count = rctx->ncomps[I->src[s].value]; |
| 1010 |
|
| 1011 |
assert(I->op != AGX_OPCODE_PHI && "phis don't use .kill"); |
| 1012 |
assert(count >= 1); |
| 1013 |
|
| 1014 |
BITSET_CLEAR_COUNT(rctx->used_regs[cls], reg, count); |
| 1015 |
} |
| 1016 |
|
| 1017 |
static void |
| 1018 |
try_kill_early_sources(struct ra_ctx *rctx, const agx_instr *I, |
| 1019 |
unsigned first_source, unsigned last_source, |
| 1020 |
unsigned region_end, unsigned region_base) |
| 1021 |
{ |
| 1022 |
unsigned dest_size = util_next_power_of_two(rctx->ncomps[I->dest[0].value]); |
| 1023 |
unsigned dest_end = region_base + dest_size; |
| 1024 |
|
| 1025 |
/* We can only early-kill a region if we can trivially allocate the |
| 1026 |
* destination to it. That way we never shuffle killed sources. |
| 1027 |
* |
| 1028 |
* To ensure that, the region must be aligned and cover the destination. |
| 1029 |
*/ |
| 1030 |
if (region_base == region_end || |
| 1031 |
(rctx->ssa_to_reg[I->src[first_source].value] & (dest_size - 1)) || |
| 1032 |
((region_end < dest_end) && |
| 1033 |
BITSET_TEST_RANGE(rctx->used_regs[RA_GPR], region_end, dest_end))) |
| 1034 |
return; |
| 1035 |
|
| 1036 |
for (unsigned s = first_source; s <= last_source; ++s) { |
| 1037 |
if (I->src[s].kill && !I->src[s].memory) { |
| 1038 |
kill_source(rctx, I, s); |
| 1039 |
rctx->early_killed = true; |
| 1040 |
I->src[s].kill = false; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
/** Assign registers to SSA values in a block. */ |
| 1046 |
|
| 1047 |
static void |
| 1048 |
agx_ra_assign_local(struct ra_ctx *rctx) |
| 1049 |
{ |
| 1050 |
BITSET_DECLARE(used_regs_gpr, AGX_NUM_REGS) = {0}; |
| 1051 |
BITSET_DECLARE(used_regs_mem, AGX_NUM_MODELED_REGS) = {0}; |
| 1052 |
uint16_t *ssa_to_reg = calloc(rctx->shader->alloc, sizeof(uint16_t)); |
| 1053 |
|
| 1054 |
agx_block *block = rctx->block; |
| 1055 |
rctx->used_regs[RA_GPR] = used_regs_gpr; |
| 1056 |
rctx->used_regs[RA_MEM] = used_regs_mem; |
| 1057 |
rctx->ssa_to_reg = ssa_to_reg; |
| 1058 |
|
| 1059 |
reserve_live_in(rctx); |
| 1060 |
|
| 1061 |
/* Force the nesting counter r0l live throughout shaders using control flow. |
| 1062 |
* This could be optimized (sync with agx_calc_register_demand). |
| 1063 |
*/ |
| 1064 |
if (rctx->shader->any_cf) |
| 1065 |
BITSET_SET(used_regs_gpr, 0); |
| 1066 |
|
| 1067 |
/* Force the zero r0h live throughout shaders using divergent shuffles. */ |
| 1068 |
if (rctx->shader->any_quad_divergent_shuffle) { |
| 1069 |
assert(rctx->shader->any_cf); |
| 1070 |
BITSET_SET(used_regs_gpr, 1); |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* Reserve bottom registers as temporaries for parallel copy lowering */ |
| 1074 |
if (rctx->shader->has_spill_pcopy_reserved) { |
| 1075 |
BITSET_SET_COUNT(used_regs_gpr, 0, 8); |
| 1076 |
} |
| 1077 |
|
| 1078 |
agx_foreach_instr_in_block(block, I) { |
| 1079 |
rctx->instr = I; |
| 1080 |
|
| 1081 |
/* Optimization: if a split contains the last use of a vector, the split |
| 1082 |
* can be removed by assigning the destinations overlapping the source. |
| 1083 |
*/ |
| 1084 |
if (I->op == AGX_OPCODE_SPLIT && I->src[0].kill) { |
| 1085 |
assert(ra_class_for_index(I->src[0]) == RA_GPR); |
| 1086 |
unsigned reg = ssa_to_reg[I->src[0].value]; |
| 1087 |
unsigned width = agx_size_align_16(agx_split_width(I)); |
| 1088 |
|
| 1089 |
agx_foreach_dest(I, d) { |
| 1090 |
assert(ra_class_for_index(I->dest[0]) == RA_GPR); |
| 1091 |
|
| 1092 |
/* Free up the source */ |
| 1093 |
unsigned offset_reg = reg + (d * width); |
| 1094 |
BITSET_CLEAR_COUNT(used_regs_gpr, offset_reg, width); |
| 1095 |
|
| 1096 |
/* Assign the destination where the source was */ |
| 1097 |
if (!agx_is_null(I->dest[d])) |
| 1098 |
assign_regs(rctx, I->dest[d], offset_reg); |
| 1099 |
} |
| 1100 |
|
| 1101 |
unsigned trail = rctx->ncomps[I->src[0].value] - (I->nr_dests * width); |
| 1102 |
BITSET_CLEAR_COUNT(used_regs_gpr, reg + (I->nr_dests * width), trail); |
| 1103 |
|
| 1104 |
agx_set_sources(rctx, I); |
| 1105 |
agx_set_dests(rctx, I); |
| 1106 |
continue; |
| 1107 |
} else if (I->op == AGX_OPCODE_PRELOAD) { |
| 1108 |
/* We must coalesce all preload moves */ |
| 1109 |
assert(I->dest[0].size == I->src[0].size); |
| 1110 |
assert(I->src[0].type == AGX_INDEX_REGISTER); |
| 1111 |
|
| 1112 |
/* r1l specifically is a preloaded register. It is reserved during |
| 1113 |
* demand calculations to ensure we don't need live range shuffling of |
| 1114 |
* spilling temporaries. But we can still preload to it. So if it's |
| 1115 |
* reserved, just free it. It'll be fine. |
| 1116 |
*/ |
| 1117 |
if (I->src[0].value == 2) { |
| 1118 |
BITSET_CLEAR(rctx->used_regs[RA_GPR], 2); |
| 1119 |
} |
| 1120 |
|
| 1121 |
assign_regs(rctx, I->dest[0], I->src[0].value); |
| 1122 |
agx_set_dests(rctx, I); |
| 1123 |
continue; |
| 1124 |
} |
| 1125 |
|
| 1126 |
/* Search for regions of contiguous killed sources to early-kill. */ |
| 1127 |
rctx->early_killed = false; |
| 1128 |
|
| 1129 |
if (I->nr_dests == 1) { |
| 1130 |
unsigned first_src = 0; |
| 1131 |
unsigned end = 0; |
| 1132 |
unsigned start = 0; |
| 1133 |
|
| 1134 |
agx_foreach_ssa_src(I, s) { |
| 1135 |
if (I->src[s].kill && !I->src[s].memory) { |
| 1136 |
unsigned reg = rctx->ssa_to_reg[I->src[s].value]; |
| 1137 |
|
| 1138 |
if (start == end || end != reg) { |
| 1139 |
try_kill_early_sources(rctx, I, first_src, s, end, start); |
| 1140 |
first_src = s; |
| 1141 |
start = reg; |
| 1142 |
} |
| 1143 |
|
| 1144 |
end = reg + rctx->ncomps[I->src[s].value]; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
try_kill_early_sources(rctx, I, first_src, I->nr_srcs - 1, end, start); |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* Next, assign destinations one at a time. This is always legal |
| 1152 |
* because of the SSA form. |
| 1153 |
*/ |
| 1154 |
agx_foreach_ssa_dest(I, d) { |
| 1155 |
if (I->op == AGX_OPCODE_PHI && I->dest[d].has_reg) |
| 1156 |
continue; |
| 1157 |
|
| 1158 |
assign_regs(rctx, I->dest[d], pick_regs(rctx, I, d)); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* Free late-killed sources */ |
| 1162 |
agx_foreach_ssa_src(I, s) { |
| 1163 |
if (I->src[s].kill) { |
| 1164 |
kill_source(rctx, I, s); |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
/* Phi sources are special. Set in the corresponding predecessors */ |
| 1169 |
if (I->op != AGX_OPCODE_PHI) |
| 1170 |
agx_set_sources(rctx, I); |
| 1171 |
|
| 1172 |
agx_set_dests(rctx, I); |
| 1173 |
} |
| 1174 |
|
| 1175 |
for (unsigned i = 0; i < RA_CLASSES; ++i) { |
| 1176 |
block->reg_to_ssa_out[i] = |
| 1177 |
malloc(rctx->bound[i] * sizeof(*block->reg_to_ssa_out[i])); |
| 1178 |
|
| 1179 |
/* Initialize with sentinel so we don't have unused regs mapping to r0 */ |
| 1180 |
memset(block->reg_to_ssa_out[i], 0xFF, |
| 1181 |
rctx->bound[i] * sizeof(*block->reg_to_ssa_out[i])); |
| 1182 |
} |
| 1183 |
|
| 1184 |
U_SPARSE_BITSET_FOREACH_SET(&block->live_out, i) { |
| 1185 |
block->reg_to_ssa_out[rctx->classes[i]][rctx->ssa_to_reg[i]] = i; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* Also set the sources for the phis in our successors, since that logically |
| 1189 |
* happens now (given the possibility of live range splits, etc) |
| 1190 |
*/ |
| 1191 |
agx_foreach_successor(block, succ) { |
| 1192 |
unsigned pred_idx = agx_predecessor_index(succ, block); |
| 1193 |
|
| 1194 |
agx_foreach_phi_in_block(succ, phi) { |
| 1195 |
if (phi->src[pred_idx].type == AGX_INDEX_NORMAL && |
| 1196 |
!phi->src[pred_idx].has_reg) { |
| 1197 |
/* This source needs a fixup */ |
| 1198 |
unsigned value = phi->src[pred_idx].value; |
| 1199 |
phi->src[pred_idx].reg = rctx->ssa_to_reg[value]; |
| 1200 |
phi->src[pred_idx].has_reg = true; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
free(rctx->ssa_to_reg); |
| 1206 |
} |
| 1207 |
|
| 1208 |
/* |
| 1209 |
* Lower phis to parallel copies at the logical end of a given block. If a block |
| 1210 |
* needs parallel copies inserted, a successor of the block has a phi node. To |
| 1211 |
* have a (nontrivial) phi node, a block must have multiple predecessors. So the |
| 1212 |
* edge from the block to the successor (with phi) is not the only edge entering |
| 1213 |
* the successor. Because the control flow graph has no critical edges, this |
| 1214 |
* edge must therefore be the only edge leaving the block, so the block must |
| 1215 |
* have only a single successor. |
| 1216 |
*/ |
| 1217 |
static void |
| 1218 |
agx_insert_parallel_copies(agx_context *ctx, agx_block *block) |
| 1219 |
{ |
| 1220 |
bool any_succ = false; |
| 1221 |
unsigned nr_phi = 0; |
| 1222 |
|
| 1223 |
/* Phi nodes logically happen on the control flow edge, so parallel copies |
| 1224 |
* are added at the end of the predecessor */ |
| 1225 |
agx_builder b = agx_init_builder(ctx, agx_after_block_logical(block)); |
| 1226 |
|
| 1227 |
agx_foreach_successor(block, succ) { |
| 1228 |
assert(nr_phi == 0 && "control flow graph has a critical edge"); |
| 1229 |
|
| 1230 |
agx_foreach_phi_in_block(succ, phi) { |
| 1231 |
assert(!any_succ && "control flow graph has a critical edge"); |
| 1232 |
nr_phi += agx_channels(phi->dest[0]); |
| 1233 |
} |
| 1234 |
|
| 1235 |
any_succ = true; |
| 1236 |
|
| 1237 |
/* Nothing to do if there are no phi nodes */ |
| 1238 |
if (nr_phi == 0) |
| 1239 |
continue; |
| 1240 |
|
| 1241 |
unsigned pred_index = agx_predecessor_index(succ, block); |
| 1242 |
|
| 1243 |
/* Create a parallel copy lowering all the phi nodes */ |
| 1244 |
struct agx_copy *copies = calloc(sizeof(*copies), nr_phi); |
| 1245 |
|
| 1246 |
unsigned i = 0; |
| 1247 |
|
| 1248 |
agx_foreach_phi_in_block(succ, phi) { |
| 1249 |
agx_index dest = phi->dest[0]; |
| 1250 |
agx_index src = phi->src[pred_index]; |
| 1251 |
|
| 1252 |
if (src.type == AGX_INDEX_IMMEDIATE) |
| 1253 |
src.size = dest.size; |
| 1254 |
|
| 1255 |
assert(dest.type == AGX_INDEX_REGISTER); |
| 1256 |
assert(dest.size == src.size); |
| 1257 |
|
| 1258 |
/* Scalarize the phi, since the parallel copy lowering doesn't handle |
| 1259 |
* vector phis. While we scalarize phis in NIR, we can generate vector |
| 1260 |
* phis from spilling so must take care. |
| 1261 |
*/ |
| 1262 |
for (unsigned c = 0; c < agx_channels(phi->dest[0]); ++c) { |
| 1263 |
agx_index src_ = src; |
| 1264 |
unsigned offs = c * agx_size_align_16(src.size); |
| 1265 |
|
| 1266 |
if (src.type != AGX_INDEX_IMMEDIATE) { |
| 1267 |
assert(src.type == AGX_INDEX_UNIFORM || |
| 1268 |
src.type == AGX_INDEX_REGISTER); |
| 1269 |
src_.value += offs; |
| 1270 |
src_.channels_m1 = 1 - 1; |
| 1271 |
} |
| 1272 |
|
| 1273 |
assert(i < nr_phi); |
| 1274 |
copies[i++] = (struct agx_copy){ |
| 1275 |
.dest = dest.value + offs, |
| 1276 |
.dest_mem = dest.memory, |
| 1277 |
.src = src_, |
| 1278 |
}; |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
agx_emit_parallel_copies(&b, copies, nr_phi); |
| 1283 |
|
| 1284 |
free(copies); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
static void |
| 1289 |
lower_exports(agx_context *ctx) |
| 1290 |
{ |
| 1291 |
struct agx_copy copies[AGX_NUM_REGS]; |
| 1292 |
unsigned nr = 0; |
| 1293 |
agx_block *block = agx_exit_block(ctx); |
| 1294 |
|
| 1295 |
agx_foreach_instr_in_block_safe(block, I) { |
| 1296 |
if (I->op != AGX_OPCODE_EXPORT) |
| 1297 |
continue; |
| 1298 |
|
| 1299 |
assert(agx_channels(I->src[0]) == 1 && "scalarized in frontend"); |
| 1300 |
assert(nr < ARRAY_SIZE(copies)); |
| 1301 |
|
| 1302 |
copies[nr++] = (struct agx_copy){ |
| 1303 |
.dest = I->imm, |
| 1304 |
.src = I->src[0], |
| 1305 |
}; |
| 1306 |
|
| 1307 |
/* The export itself is now trivial, reflect that for correct last-use |
| 1308 |
* tracking later. |
| 1309 |
*/ |
| 1310 |
I->src[0] = agx_register_like(I->imm, I->src[0]); |
| 1311 |
|
| 1312 |
/* We cannot use fewer registers than we export */ |
| 1313 |
ctx->max_reg = |
| 1314 |
MAX2(ctx->max_reg, I->imm + agx_size_align_16(I->src[0].size)); |
| 1315 |
} |
| 1316 |
|
| 1317 |
agx_builder b = agx_init_builder(ctx, agx_after_block_logical(block)); |
| 1318 |
agx_emit_parallel_copies(&b, copies, nr); |
| 1319 |
} |
| 1320 |
|
| 1321 |
void |
| 1322 |
agx_ra(agx_context *ctx) |
| 1323 |
{ |
| 1324 |
bool force_spilling = |
| 1325 |
(agx_compiler_debug & AGX_DBG_SPILL) && ctx->key->has_scratch; |
| 1326 |
|
| 1327 |
/* Determine maximum possible registers. We won't exceed this! */ |
| 1328 |
unsigned max_possible_regs = AGX_NUM_REGS; |
| 1329 |
|
| 1330 |
/* Compute shaders need to have their entire workgroup together, so our |
| 1331 |
* register usage is bounded by the workgroup size. |
| 1332 |
*/ |
| 1333 |
if (mesa_shader_stage_is_compute(ctx->stage)) { |
| 1334 |
unsigned threads_per_workgroup; |
| 1335 |
|
| 1336 |
/* If we don't know the workgroup size, worst case it. TODO: Optimize |
| 1337 |
* this, since it'll decimate opencl perf. |
| 1338 |
*/ |
| 1339 |
if (ctx->nir->info.workgroup_size_variable) { |
| 1340 |
threads_per_workgroup = 1024; |
| 1341 |
} else { |
| 1342 |
threads_per_workgroup = ctx->nir->info.workgroup_size[0] * |
| 1343 |
ctx->nir->info.workgroup_size[1] * |
| 1344 |
ctx->nir->info.workgroup_size[2]; |
| 1345 |
} |
| 1346 |
|
| 1347 |
max_possible_regs = |
| 1348 |
agx_max_registers_for_occupancy(threads_per_workgroup); |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (force_spilling) { |
| 1352 |
/* Even when testing spilling, we need enough room for preloaded/exported |
| 1353 |
* regs. |
| 1354 |
*/ |
| 1355 |
unsigned d = 24; |
| 1356 |
unsigned max_ncomps = 8; |
| 1357 |
|
| 1358 |
agx_foreach_instr_global(ctx, I) { |
| 1359 |
if (I->op == AGX_OPCODE_PRELOAD) { |
| 1360 |
unsigned size = agx_size_align_16(I->src[0].size); |
| 1361 |
d = MAX2(d, I->src[0].value + size); |
| 1362 |
} else if (I->op == AGX_OPCODE_EXPORT) { |
| 1363 |
unsigned size = agx_size_align_16(I->src[0].size); |
| 1364 |
d = MAX2(d, I->imm + size); |
| 1365 |
} else if (I->op == AGX_OPCODE_IMAGE_WRITE) { |
| 1366 |
/* vec4 source + vec4 coordinates + bindless handle + reserved */ |
| 1367 |
d = MAX2(d, 26); |
| 1368 |
} else if (I->op == AGX_OPCODE_TEXTURE_SAMPLE && |
| 1369 |
(I->lod_mode == AGX_LOD_MODE_LOD_GRAD || |
| 1370 |
I->lod_mode == AGX_LOD_MODE_LOD_GRAD_MIN)) { |
| 1371 |
/* as above but with big gradient */ |
| 1372 |
d = MAX2(d, 36); |
| 1373 |
} |
| 1374 |
|
| 1375 |
agx_foreach_ssa_dest(I, v) { |
| 1376 |
max_ncomps = MAX2(max_ncomps, agx_index_size_16(I->dest[v])); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
max_possible_regs = ALIGN_POT(d, util_next_power_of_two(max_ncomps)); |
| 1381 |
} else if (ctx->key->is_helper) { |
| 1382 |
/* The helper program is unspillable and has a limited register file */ |
| 1383 |
max_possible_regs = 32; |
| 1384 |
} |
| 1385 |
|
| 1386 |
/* Calculate the demand. We'll use it to determine if we need to spill and to |
| 1387 |
* bound register assignment. |
| 1388 |
*/ |
| 1389 |
agx_compute_liveness(ctx); |
| 1390 |
unsigned effective_demand = agx_calc_register_demand(ctx, false); |
| 1391 |
bool spilling = (effective_demand > max_possible_regs); |
| 1392 |
bool remat = false; |
| 1393 |
|
| 1394 |
/* If we need multiple waves, see if we can rematerialize constants to save |
| 1395 |
* waves. If we only have a single wave regardless, this is pointless. |
| 1396 |
*/ |
| 1397 |
if (effective_demand > agx_round_registers(1) && !spilling) { |
| 1398 |
unsigned effective_demand_remat = agx_calc_register_demand(ctx, true); |
| 1399 |
|
| 1400 |
/* Worst-case assume we need 6 16-bit registers for constants, for a |
| 1401 |
* four-source cmpsel where 3 sources are 32-bit constants. Rounded to |
| 1402 |
* ensure live-range splitting works. |
| 1403 |
*/ |
| 1404 |
if ((effective_demand_remat + 6) < effective_demand) { |
| 1405 |
unsigned l = agx_round_registers( |
| 1406 |
align(agx_round_registers(effective_demand_remat + 6), 16)); |
| 1407 |
|
| 1408 |
/* Only rematerialize if it actually lets us save a wave */ |
| 1409 |
if (l < agx_round_registers(effective_demand)) { |
| 1410 |
remat = true; |
| 1411 |
max_possible_regs = l; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (spilling || remat) { |
| 1417 |
assert((remat || ctx->key->has_scratch) && |
| 1418 |
"internal shaders are unspillable"); |
| 1419 |
|
| 1420 |
agx_spill(ctx, max_possible_regs, remat); |
| 1421 |
|
| 1422 |
/* After spilling, recalculate liveness and demand */ |
| 1423 |
agx_compute_liveness(ctx); |
| 1424 |
effective_demand = agx_calc_register_demand(ctx, false); |
| 1425 |
|
| 1426 |
/* The resulting program can now be assigned registers */ |
| 1427 |
assert(effective_demand <= max_possible_regs && "spiller post-condition"); |
| 1428 |
} |
| 1429 |
|
| 1430 |
/* Record all phi webs. First initialize the union-find data structure with |
| 1431 |
* all SSA defs in their own singletons, then union together anything related |
| 1432 |
* by a phi. The resulting union-find structure will be the webs. |
| 1433 |
*/ |
| 1434 |
struct phi_web_node *phi_web = calloc(ctx->alloc, sizeof(*phi_web)); |
| 1435 |
for (unsigned i = 0; i < ctx->alloc; ++i) { |
| 1436 |
phi_web[i].parent = i; |
| 1437 |
} |
| 1438 |
|
| 1439 |
agx_foreach_block(ctx, block) { |
| 1440 |
agx_foreach_phi_in_block(block, phi) { |
| 1441 |
agx_foreach_ssa_src(phi, s) { |
| 1442 |
phi_web_union(phi_web, phi->dest[0].value, phi->src[s].value); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
uint8_t *ncomps = calloc(ctx->alloc, sizeof(uint8_t)); |
| 1448 |
uint8_t *ncomps_unrounded = calloc(ctx->alloc, sizeof(uint8_t)); |
| 1449 |
enum ra_class *classes = calloc(ctx->alloc, sizeof(enum ra_class)); |
| 1450 |
agx_instr **src_to_collect_phi = calloc(ctx->alloc, sizeof(agx_instr *)); |
| 1451 |
enum agx_size *sizes = calloc(ctx->alloc, sizeof(enum agx_size)); |
| 1452 |
BITSET_WORD *visited = BITSET_CALLOC(ctx->alloc); |
| 1453 |
unsigned max_ncomps = 1; |
| 1454 |
|
| 1455 |
agx_foreach_instr_global(ctx, I) { |
| 1456 |
/* Record collects/phis so we can coalesce when assigning */ |
| 1457 |
if (I->op == AGX_OPCODE_COLLECT || I->op == AGX_OPCODE_PHI || |
| 1458 |
I->op == AGX_OPCODE_EXPORT || I->op == AGX_OPCODE_SPLIT) { |
| 1459 |
agx_foreach_ssa_src(I, s) { |
| 1460 |
src_to_collect_phi[I->src[s].value] = I; |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
agx_foreach_ssa_dest(I, d) { |
| 1465 |
unsigned v = I->dest[d].value; |
| 1466 |
assert(ncomps[v] == 0 && "broken SSA"); |
| 1467 |
/* Round up vectors for easier live range splitting */ |
| 1468 |
ncomps_unrounded[v] = agx_index_size_16(I->dest[d]); |
| 1469 |
ncomps[v] = util_next_power_of_two(ncomps_unrounded[v]); |
| 1470 |
sizes[v] = I->dest[d].size; |
| 1471 |
classes[v] = ra_class_for_index(I->dest[d]); |
| 1472 |
|
| 1473 |
max_ncomps = MAX2(max_ncomps, ncomps[v]); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
/* For live range splitting to work properly, ensure the register file is |
| 1478 |
* aligned to the larger vector size. Most of the time, this is a no-op since |
| 1479 |
* the largest vector size is usually 128-bit and the register file is |
| 1480 |
* naturally 128-bit aligned. However, this is required for correctness with |
| 1481 |
* 3D textureGrad, which can have a source vector of length 6x32-bit, |
| 1482 |
* rounding up to 256-bit and requiring special accounting here. |
| 1483 |
*/ |
| 1484 |
unsigned reg_file_alignment = MAX2(max_ncomps, 8); |
| 1485 |
assert(util_is_power_of_two_nonzero(reg_file_alignment)); |
| 1486 |
assert(reg_file_alignment <= 16 && "max size"); |
| 1487 |
|
| 1488 |
unsigned demand = ALIGN_POT(effective_demand, reg_file_alignment); |
| 1489 |
assert(demand <= max_possible_regs && "Invariant"); |
| 1490 |
|
| 1491 |
/* Round up the demand to the maximum number of registers we can use without |
| 1492 |
* affecting occupancy. This reduces live range splitting. |
| 1493 |
*/ |
| 1494 |
unsigned max_regs = agx_occupancy_for_register_count(demand).max_registers; |
| 1495 |
if (ctx->key->is_helper || force_spilling) |
| 1496 |
max_regs = max_possible_regs; |
| 1497 |
|
| 1498 |
max_regs = ROUND_DOWN_TO(max_regs, reg_file_alignment); |
| 1499 |
|
| 1500 |
/* Or, we can bound tightly for debugging */ |
| 1501 |
if (agx_compiler_debug & AGX_DBG_DEMAND) |
| 1502 |
max_regs = ALIGN_POT(MAX2(demand, 12), reg_file_alignment); |
| 1503 |
|
| 1504 |
/* ...but not too tightly */ |
| 1505 |
assert((max_regs % reg_file_alignment) == 0 && "occupancy limits aligned"); |
| 1506 |
assert(max_regs >= (6 * 2) && "space for vertex shader preloading"); |
| 1507 |
assert(max_regs <= max_possible_regs); |
| 1508 |
|
| 1509 |
unsigned reg_count = 0, mem_slot_count = 0; |
| 1510 |
|
| 1511 |
/* Assign registers in dominance-order. This coincides with source-order due |
| 1512 |
* to a NIR invariant, so we do not need special handling for this. |
| 1513 |
*/ |
| 1514 |
agx_foreach_block(ctx, block) { |
| 1515 |
agx_ra_assign_local(&(struct ra_ctx){ |
| 1516 |
.shader = ctx, |
| 1517 |
.block = block, |
| 1518 |
.src_to_collect_phi = src_to_collect_phi, |
| 1519 |
.phi_web = phi_web, |
| 1520 |
.ncomps = ncomps, |
| 1521 |
.ncomps_unrounded = ncomps_unrounded, |
| 1522 |
.sizes = sizes, |
| 1523 |
.classes = classes, |
| 1524 |
.visited = visited, |
| 1525 |
.bound[RA_GPR] = max_regs, |
| 1526 |
.bound[RA_MEM] = AGX_NUM_MODELED_REGS, |
| 1527 |
.count[RA_GPR] = ®_count, |
| 1528 |
.count[RA_MEM] = &mem_slot_count, |
| 1529 |
}); |
| 1530 |
} |
| 1531 |
|
| 1532 |
ctx->max_reg = reg_count ? (reg_count - 1) : 0; |
| 1533 |
ctx->spill_base_B = ctx->scratch_size_B; |
| 1534 |
ctx->scratch_size_B += mem_slot_count * 2; |
| 1535 |
|
| 1536 |
/* Vertex shaders preload the vertex/instance IDs (r5, r6) even if the shader |
| 1537 |
* don't use them. Account for that so the preload doesn't clobber GPRs. |
| 1538 |
* Hardware tessellation eval shaders preload patch/instance IDs there. |
| 1539 |
*/ |
| 1540 |
if (ctx->nir->info.stage == MESA_SHADER_VERTEX || |
| 1541 |
ctx->nir->info.stage == MESA_SHADER_TESS_EVAL) |
| 1542 |
ctx->max_reg = MAX2(ctx->max_reg, 6 * 2); |
| 1543 |
|
| 1544 |
assert(ctx->max_reg <= max_regs); |
| 1545 |
|
| 1546 |
/* Validate RA after assigning registers just before lowering SSA */ |
| 1547 |
agx_validate_ra(ctx); |
| 1548 |
|
| 1549 |
agx_foreach_instr_global_safe(ctx, ins) { |
| 1550 |
/* Lower away SSA */ |
| 1551 |
agx_foreach_ssa_dest(ins, d) { |
| 1552 |
ins->dest[d] = |
| 1553 |
agx_replace_index(ins->dest[d], agx_as_register(ins->dest[d])); |
| 1554 |
} |
| 1555 |
|
| 1556 |
agx_foreach_ssa_src(ins, s) { |
| 1557 |
agx_replace_src(ins, s, agx_as_register(ins->src[s])); |
| 1558 |
} |
| 1559 |
|
| 1560 |
/* Lower away RA pseudo-instructions */ |
| 1561 |
agx_builder b = agx_init_builder(ctx, agx_after_instr(ins)); |
| 1562 |
|
| 1563 |
if (ins->op == AGX_OPCODE_COLLECT) { |
| 1564 |
assert(ins->dest[0].type == AGX_INDEX_REGISTER); |
| 1565 |
assert(!ins->dest[0].memory); |
| 1566 |
|
| 1567 |
unsigned base = ins->dest[0].value; |
| 1568 |
unsigned width = agx_size_align_16(ins->src[0].size); |
| 1569 |
|
| 1570 |
struct agx_copy *copies = alloca(sizeof(copies[0]) * ins->nr_srcs); |
| 1571 |
unsigned n = 0; |
| 1572 |
|
| 1573 |
/* Move the sources */ |
| 1574 |
agx_foreach_src(ins, i) { |
| 1575 |
if (agx_is_null(ins->src[i]) || ins->src[i].type == AGX_INDEX_UNDEF) |
| 1576 |
continue; |
| 1577 |
assert(ins->src[i].size == ins->src[0].size); |
| 1578 |
|
| 1579 |
assert(n < ins->nr_srcs); |
| 1580 |
copies[n++] = (struct agx_copy){ |
| 1581 |
.dest = base + (i * width), |
| 1582 |
.src = ins->src[i], |
| 1583 |
}; |
| 1584 |
} |
| 1585 |
|
| 1586 |
agx_emit_parallel_copies(&b, copies, n); |
| 1587 |
agx_remove_instruction(ins); |
| 1588 |
continue; |
| 1589 |
} else if (ins->op == AGX_OPCODE_SPLIT) { |
| 1590 |
assert(ins->src[0].type == AGX_INDEX_REGISTER || |
| 1591 |
ins->src[0].type == AGX_INDEX_UNIFORM); |
| 1592 |
|
| 1593 |
struct agx_copy copies[8]; |
| 1594 |
assert(ins->nr_dests <= ARRAY_SIZE(copies)); |
| 1595 |
|
| 1596 |
unsigned n = 0; |
| 1597 |
unsigned width = agx_size_align_16(agx_split_width(ins)); |
| 1598 |
|
| 1599 |
/* Move the sources */ |
| 1600 |
agx_foreach_dest(ins, i) { |
| 1601 |
if (ins->dest[i].type != AGX_INDEX_REGISTER) |
| 1602 |
continue; |
| 1603 |
|
| 1604 |
assert(!ins->dest[i].memory); |
| 1605 |
|
| 1606 |
agx_index src = ins->src[0]; |
| 1607 |
src.size = ins->dest[i].size; |
| 1608 |
src.channels_m1 = 0; |
| 1609 |
src.value += (i * width); |
| 1610 |
|
| 1611 |
assert(n < ARRAY_SIZE(copies)); |
| 1612 |
copies[n++] = (struct agx_copy){ |
| 1613 |
.dest = ins->dest[i].value, |
| 1614 |
.src = src, |
| 1615 |
}; |
| 1616 |
} |
| 1617 |
|
| 1618 |
/* Lower away */ |
| 1619 |
agx_builder b = agx_init_builder(ctx, agx_after_instr(ins)); |
| 1620 |
agx_emit_parallel_copies(&b, copies, n); |
| 1621 |
agx_remove_instruction(ins); |
| 1622 |
continue; |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
/* Insert parallel copies lowering phi nodes and exports */ |
| 1627 |
agx_foreach_block(ctx, block) { |
| 1628 |
agx_insert_parallel_copies(ctx, block); |
| 1629 |
} |
| 1630 |
|
| 1631 |
lower_exports(ctx); |
| 1632 |
|
| 1633 |
agx_foreach_instr_global_safe(ctx, I) { |
| 1634 |
switch (I->op) { |
| 1635 |
/* Pseudoinstructions for RA must be removed now */ |
| 1636 |
case AGX_OPCODE_PHI: |
| 1637 |
case AGX_OPCODE_PRELOAD: |
| 1638 |
agx_remove_instruction(I); |
| 1639 |
break; |
| 1640 |
|
| 1641 |
/* Coalesced moves can be removed */ |
| 1642 |
case AGX_OPCODE_MOV: |
| 1643 |
if (I->src[0].type == AGX_INDEX_REGISTER && |
| 1644 |
I->dest[0].size == I->src[0].size && |
| 1645 |
I->src[0].value == I->dest[0].value && |
| 1646 |
I->src[0].memory == I->dest[0].memory) { |
| 1647 |
|
| 1648 |
assert(I->dest[0].type == AGX_INDEX_REGISTER); |
| 1649 |
agx_remove_instruction(I); |
| 1650 |
} |
| 1651 |
break; |
| 1652 |
|
| 1653 |
default: |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
if (spilling) |
| 1659 |
agx_lower_spill(ctx); |
| 1660 |
|
| 1661 |
agx_foreach_block(ctx, block) { |
| 1662 |
for (unsigned i = 0; i < ARRAY_SIZE(block->reg_to_ssa_out); ++i) { |
| 1663 |
free(block->reg_to_ssa_out[i]); |
| 1664 |
block->reg_to_ssa_out[i] = NULL; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
free(phi_web); |
| 1669 |
free(src_to_collect_phi); |
| 1670 |
free(ncomps); |
| 1671 |
free(ncomps_unrounded); |
| 1672 |
free(sizes); |
| 1673 |
free(classes); |
| 1674 |
free(visited); |
| 1675 |
} |