| 1 |
/* |
| 2 |
* Copyright 2023-2024 Alyssa Rosenzweig |
| 3 |
* Copyright 2023-2024 Valve Corporation |
| 4 |
* Copyright 2022 Collabora Ltd. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "util/list.h" |
| 8 |
#include "util/set.h" |
| 9 |
#include "util/u_memory.h" |
| 10 |
#include "bifrost_compile.h" |
| 11 |
#include "bifrost_nir.h" |
| 12 |
#include "compiler.h" |
| 13 |
|
| 14 |
/* |
| 15 |
* RA treats the nesting counter, the divergent shuffle temporary, and the |
| 16 |
* spiller temporaries as alive throughout if used anywhere. This could be |
| 17 |
* optimized. Using a single power-of-two reserved region at the start ensures |
| 18 |
* these registers are never shuffled. |
| 19 |
*/ |
| 20 |
static unsigned |
| 21 |
reserved_size(bi_context *ctx) |
| 22 |
{ |
| 23 |
if (ctx->has_spill_pcopy_reserved) |
| 24 |
return 8; |
| 25 |
else |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
/* |
| 30 |
* Calculate register demand in registers, while gathering widths and |
| 31 |
* classes. Becuase we allocate in SSA, this calculation is exact in |
| 32 |
* linear-time. Depends on SSA liveness information. |
| 33 |
*/ |
| 34 |
unsigned |
| 35 |
bi_calc_register_demand(bi_context *ctx) |
| 36 |
{ |
| 37 |
/* Print detailed demand calculation, helpful to debug spilling */ |
| 38 |
bool debug = false; |
| 39 |
|
| 40 |
if (debug) { |
| 41 |
bi_print_shader(ctx, stdout); |
| 42 |
} |
| 43 |
|
| 44 |
uint8_t *widths = calloc(ctx->ssa_alloc, sizeof(uint8_t)); |
| 45 |
enum ra_class *classes = calloc(ctx->ssa_alloc, sizeof(enum ra_class)); |
| 46 |
|
| 47 |
bi_foreach_instr_global(ctx, I) { |
| 48 |
bi_foreach_ssa_dest(I, d) { |
| 49 |
unsigned v = I->dest[d].value; |
| 50 |
assert(widths[v] == 0 && "broken SSA"); |
| 51 |
/* Round up vectors for easier live range splitting */ |
| 52 |
widths[v] = bi_count_write_registers(I, d); |
| 53 |
classes[v] = ra_class_for_index(I->dest[d]); |
| 54 |
} |
| 55 |
} |
| 56 |
/* now that we know the rest of the sizes, find the sizes for PHI nodes */ |
| 57 |
bi_foreach_block(ctx, block) { |
| 58 |
bi_foreach_phi_in_block(block, I) { |
| 59 |
if (I->dest[0].type != BI_INDEX_NORMAL) |
| 60 |
continue; |
| 61 |
unsigned idx = I->dest[0].value; |
| 62 |
widths[idx] = 1; |
| 63 |
bi_foreach_ssa_src(I, s) { |
| 64 |
widths[idx] = MAX2(widths[idx], widths[I->src[s].value]); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/* Calculate demand at the start of each block based on live-in, then update |
| 70 |
* for each instruction processed. Calculate rolling maximum. |
| 71 |
*/ |
| 72 |
unsigned max_demand = 0; |
| 73 |
|
| 74 |
bi_foreach_block(ctx, block) { |
| 75 |
unsigned demand = reserved_size(ctx); |
| 76 |
|
| 77 |
/* Everything live-in */ |
| 78 |
{ |
| 79 |
int i; |
| 80 |
BITSET_FOREACH_SET(i, block->ssa_live_in, ctx->ssa_alloc) { |
| 81 |
if (classes[i] == RA_GPR) |
| 82 |
demand += widths[i]; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
max_demand = MAX2(demand, max_demand); |
| 87 |
|
| 88 |
/* To handle non-power-of-two vectors, sometimes live range splitting |
| 89 |
* needs extra registers for 1 instruction. This counter tracks the number |
| 90 |
* of registers to be freed after 1 extra instruction. |
| 91 |
*/ |
| 92 |
unsigned late_kill_count = 0; |
| 93 |
|
| 94 |
if (debug) { |
| 95 |
printf("\n"); |
| 96 |
} |
| 97 |
|
| 98 |
bi_foreach_instr_in_block(block, I) { |
| 99 |
/* Phis happen in parallel and are already accounted for in the live-in |
| 100 |
* set, just skip them so we don't double count. |
| 101 |
*/ |
| 102 |
if (I->op == BI_OPCODE_PHI) |
| 103 |
continue; |
| 104 |
|
| 105 |
if (debug) { |
| 106 |
printf("%u: ", demand); |
| 107 |
bi_print_instr(I, stdout); |
| 108 |
} |
| 109 |
|
| 110 |
/* Handle late-kill registers from last instruction */ |
| 111 |
demand -= late_kill_count; |
| 112 |
late_kill_count = 0; |
| 113 |
|
| 114 |
/* Kill sources the first time we see them */ |
| 115 |
bi_foreach_src(I, s) { |
| 116 |
if (!I->src[s].kill_ssa) |
| 117 |
continue; |
| 118 |
assert(I->src[s].type == BI_INDEX_NORMAL); |
| 119 |
if (ra_class_for_index(I->src[s]) != RA_GPR) |
| 120 |
continue; |
| 121 |
|
| 122 |
bool skip = false; |
| 123 |
|
| 124 |
for (unsigned backwards = 0; backwards < s; ++backwards) { |
| 125 |
if (bi_is_equiv(I->src[backwards], I->src[s])) { |
| 126 |
skip = true; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (!skip) |
| 132 |
demand -= widths[I->src[s].value]; |
| 133 |
} |
| 134 |
|
| 135 |
/* Make destinations live */ |
| 136 |
bi_foreach_ssa_dest(I, d) { |
| 137 |
if (ra_class_for_index(I->dest[d]) != RA_GPR) |
| 138 |
continue; |
| 139 |
|
| 140 |
/* Live range splits allocate at power-of-two granularity. Round up |
| 141 |
* destination sizes (temporarily) to powers-of-two. |
| 142 |
*/ |
| 143 |
unsigned real_width = widths[I->dest[d].value]; |
| 144 |
unsigned pot_width = util_next_power_of_two(real_width); |
| 145 |
|
| 146 |
demand += pot_width; |
| 147 |
late_kill_count += (pot_width - real_width); |
| 148 |
} |
| 149 |
|
| 150 |
max_demand = MAX2(demand, max_demand); |
| 151 |
} |
| 152 |
|
| 153 |
demand -= late_kill_count; |
| 154 |
} |
| 155 |
|
| 156 |
free(widths); |
| 157 |
free(classes); |
| 158 |
return max_demand; |
| 159 |
} |