| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "util/u_math.h" |
| 7 |
#include "util/u_memory.h" |
| 8 |
#include "compiler.h" |
| 9 |
|
| 10 |
/* This pass promotes reads from UBOs to register-mapped uniforms. This saves |
| 11 |
* both instructions and work register pressure, but it reduces the work |
| 12 |
* registers available, requiring a balance. |
| 13 |
* |
| 14 |
* We use a heuristic to determine the ideal count, implemented by |
| 15 |
* mir_work_heuristic, which returns the ideal number of work registers. |
| 16 |
*/ |
| 17 |
|
| 18 |
static bool |
| 19 |
mir_is_ubo(midgard_instruction *ins) |
| 20 |
{ |
| 21 |
return (ins->type == TAG_LOAD_STORE_4) && (OP_IS_UBO_READ(ins->op)); |
| 22 |
} |
| 23 |
|
| 24 |
static bool |
| 25 |
mir_is_pushable_ubo(compiler_context *ctx, midgard_instruction *ins) |
| 26 |
{ |
| 27 |
if (!mir_is_ubo(ins)) |
| 28 |
return false; |
| 29 |
|
| 30 |
unsigned ubo = midgard_unpack_ubo_index_imm(ins->load_store); |
| 31 |
|
| 32 |
return !(ins->constants.u32[0] & 0xF) && |
| 33 |
(ins->src[1] == ~0) && (ins->src[2] == ~0) && |
| 34 |
(ctx->inputs->pushable_ubos & BITFIELD_BIT(ubo)); |
| 35 |
} |
| 36 |
|
| 37 |
/* Represents use data for a single UBO */ |
| 38 |
|
| 39 |
#define MAX_UBO_QWORDS (65536 / 16) |
| 40 |
|
| 41 |
struct mir_ubo_block { |
| 42 |
BITSET_DECLARE(uses, MAX_UBO_QWORDS); |
| 43 |
BITSET_DECLARE(pushed, MAX_UBO_QWORDS); |
| 44 |
}; |
| 45 |
|
| 46 |
struct mir_ubo_analysis { |
| 47 |
/* Per block analysis */ |
| 48 |
unsigned nr_blocks; |
| 49 |
struct mir_ubo_block *blocks; |
| 50 |
}; |
| 51 |
|
| 52 |
static struct mir_ubo_analysis |
| 53 |
mir_analyze_ranges(compiler_context *ctx) |
| 54 |
{ |
| 55 |
struct mir_ubo_analysis res = { |
| 56 |
.nr_blocks = ctx->nir->info.num_ubos + 1, |
| 57 |
}; |
| 58 |
|
| 59 |
res.blocks = calloc(res.nr_blocks, sizeof(struct mir_ubo_block)); |
| 60 |
|
| 61 |
mir_foreach_instr_global(ctx, ins) { |
| 62 |
if (!mir_is_pushable_ubo(ctx, ins)) |
| 63 |
continue; |
| 64 |
|
| 65 |
unsigned ubo = midgard_unpack_ubo_index_imm(ins->load_store); |
| 66 |
unsigned offset = ins->constants.u32[0] / 16; |
| 67 |
|
| 68 |
assert(ubo < res.nr_blocks); |
| 69 |
|
| 70 |
/* Blend constants are always loaded from the sysval UBO in blend shaders, |
| 71 |
* do not push them. */ |
| 72 |
if (ctx->stage == MESA_SHADER_FRAGMENT) { |
| 73 |
/* PAN_UBO_SYSVALS from the gallium driver */ |
| 74 |
unsigned sysval_ubo = 1; |
| 75 |
if(ubo == sysval_ubo && offset == 0) |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
if (offset < MAX_UBO_QWORDS) |
| 80 |
BITSET_SET(res.blocks[ubo].uses, offset); |
| 81 |
} |
| 82 |
|
| 83 |
return res; |
| 84 |
} |
| 85 |
|
| 86 |
/* Select UBO words to push. A sophisticated implementation would consider the |
| 87 |
* number of uses and perhaps the control flow to estimate benefit. This is not |
| 88 |
* sophisticated. Select from the last UBO first to prioritize sysvals. */ |
| 89 |
|
| 90 |
static void |
| 91 |
mir_pick_ubo(struct pan_ubo_push *push, struct mir_ubo_analysis *analysis, |
| 92 |
unsigned max_qwords) |
| 93 |
{ |
| 94 |
unsigned max_words = MIN2(PAN_MAX_PUSH, max_qwords * 4); |
| 95 |
|
| 96 |
for (signed ubo = analysis->nr_blocks - 1; ubo >= 0; --ubo) { |
| 97 |
struct mir_ubo_block *block = &analysis->blocks[ubo]; |
| 98 |
|
| 99 |
unsigned vec4; |
| 100 |
BITSET_FOREACH_SET(vec4, block->uses, MAX_UBO_QWORDS) { |
| 101 |
/* Don't push more than possible */ |
| 102 |
if (push->count > max_words - 4) |
| 103 |
return; |
| 104 |
|
| 105 |
for (unsigned offs = 0; offs < 4; ++offs) { |
| 106 |
struct pan_ubo_word word = { |
| 107 |
.ubo = ubo, |
| 108 |
.offset = (vec4 * 16) + (offs * 4), |
| 109 |
}; |
| 110 |
|
| 111 |
push->words[push->count++] = word; |
| 112 |
} |
| 113 |
|
| 114 |
/* Mark it as pushed so we can rewrite */ |
| 115 |
BITSET_SET(block->pushed, vec4); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#if 0 |
| 121 |
static void |
| 122 |
mir_dump_ubo_analysis(struct mir_ubo_analysis *res) |
| 123 |
{ |
| 124 |
printf("%u blocks\n", res->nr_blocks); |
| 125 |
|
| 126 |
for (unsigned i = 0; i < res->nr_blocks; ++i) { |
| 127 |
BITSET_WORD *uses = res->blocks[i].uses; |
| 128 |
BITSET_WORD *push = res->blocks[i].pushed; |
| 129 |
|
| 130 |
unsigned last = BITSET_LAST_BIT_SIZED(uses, BITSET_WORDS(MAX_UBO_QWORDS)); |
| 131 |
|
| 132 |
printf("\t"); |
| 133 |
|
| 134 |
for (unsigned j = 0; j < last; ++j) { |
| 135 |
bool used = BITSET_TEST(uses, j); |
| 136 |
bool pushed = BITSET_TEST(push, j); |
| 137 |
assert(used || !pushed); |
| 138 |
|
| 139 |
putchar(pushed ? '*' : used ? '-' : '_'); |
| 140 |
} |
| 141 |
|
| 142 |
printf("\n"); |
| 143 |
} |
| 144 |
} |
| 145 |
#endif |
| 146 |
|
| 147 |
static unsigned |
| 148 |
mir_promoteable_uniform_count(struct mir_ubo_analysis *analysis) |
| 149 |
{ |
| 150 |
unsigned count = 0; |
| 151 |
|
| 152 |
for (unsigned i = 0; i < analysis->nr_blocks; ++i) { |
| 153 |
BITSET_WORD *uses = analysis->blocks[i].uses; |
| 154 |
|
| 155 |
for (unsigned w = 0; w < BITSET_WORDS(MAX_UBO_QWORDS); ++w) |
| 156 |
count += util_bitcount(uses[w]); |
| 157 |
} |
| 158 |
|
| 159 |
return count; |
| 160 |
} |
| 161 |
|
| 162 |
static unsigned |
| 163 |
mir_count_live(uint16_t *live, unsigned temp_count) |
| 164 |
{ |
| 165 |
unsigned count = 0; |
| 166 |
|
| 167 |
for (unsigned i = 0; i < temp_count; ++i) |
| 168 |
count += util_bitcount(live[i]); |
| 169 |
|
| 170 |
return count; |
| 171 |
} |
| 172 |
|
| 173 |
static unsigned |
| 174 |
mir_estimate_pressure(compiler_context *ctx) |
| 175 |
{ |
| 176 |
mir_invalidate_liveness(ctx); |
| 177 |
mir_compute_liveness(ctx); |
| 178 |
|
| 179 |
unsigned max_live = 0; |
| 180 |
|
| 181 |
mir_foreach_block(ctx, _block) { |
| 182 |
midgard_block *block = (midgard_block *)_block; |
| 183 |
uint16_t *live = |
| 184 |
mem_dup(block->live_out, ctx->temp_count * sizeof(uint16_t)); |
| 185 |
|
| 186 |
mir_foreach_instr_in_block_rev(block, ins) { |
| 187 |
unsigned count = mir_count_live(live, ctx->temp_count); |
| 188 |
max_live = MAX2(max_live, count); |
| 189 |
mir_liveness_ins_update(live, ins, ctx->temp_count); |
| 190 |
} |
| 191 |
|
| 192 |
free(live); |
| 193 |
} |
| 194 |
|
| 195 |
return DIV_ROUND_UP(max_live, 16); |
| 196 |
} |
| 197 |
|
| 198 |
static unsigned |
| 199 |
mir_work_heuristic(compiler_context *ctx, struct mir_ubo_analysis *analysis) |
| 200 |
{ |
| 201 |
unsigned uniform_count = mir_promoteable_uniform_count(analysis); |
| 202 |
|
| 203 |
/* If there are 8 or fewer uniforms, it doesn't matter what we do, so |
| 204 |
* allow as many work registers as needed */ |
| 205 |
|
| 206 |
if (uniform_count <= 8) |
| 207 |
return 16; |
| 208 |
|
| 209 |
/* Otherwise, estimate the register pressure */ |
| 210 |
|
| 211 |
unsigned pressure = mir_estimate_pressure(ctx); |
| 212 |
|
| 213 |
/* Prioritize not spilling above all else. The relation between the |
| 214 |
* pressure estimate and the actual register pressure is a little |
| 215 |
* murkier than we might like (due to scheduling, pipeline registers, |
| 216 |
* failure to pack vector registers, load/store registers, texture |
| 217 |
* registers...), hence why this is a heuristic parameter */ |
| 218 |
|
| 219 |
if (pressure > 6) |
| 220 |
return 16; |
| 221 |
|
| 222 |
/* If there's no chance of spilling, prioritize UBOs and thread count */ |
| 223 |
|
| 224 |
return 8; |
| 225 |
} |
| 226 |
|
| 227 |
/* Bitset of indices that will be used as a special register -- inputs to a |
| 228 |
* non-ALU op. We precompute this set so that testing is efficient, otherwise |
| 229 |
* we end up O(mn) behaviour for n instructions and m uniform reads */ |
| 230 |
|
| 231 |
static BITSET_WORD * |
| 232 |
mir_special_indices(compiler_context *ctx) |
| 233 |
{ |
| 234 |
mir_compute_temp_count(ctx); |
| 235 |
BITSET_WORD *bset = BITSET_CALLOC(ctx->temp_count); |
| 236 |
|
| 237 |
mir_foreach_instr_global(ctx, ins) { |
| 238 |
/* Look for special instructions */ |
| 239 |
bool is_ldst = ins->type == TAG_LOAD_STORE_4; |
| 240 |
bool is_tex = ins->type == TAG_TEXTURE_4; |
| 241 |
bool is_writeout = ins->compact_branch && ins->writeout; |
| 242 |
|
| 243 |
if (!(is_ldst || is_tex || is_writeout)) |
| 244 |
continue; |
| 245 |
|
| 246 |
/* Anything read by a special instruction is itself special */ |
| 247 |
mir_foreach_src(ins, i) { |
| 248 |
unsigned idx = ins->src[i]; |
| 249 |
|
| 250 |
if (idx < ctx->temp_count) |
| 251 |
BITSET_SET(bset, idx); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return bset; |
| 256 |
} |
| 257 |
|
| 258 |
void |
| 259 |
midgard_promote_uniforms(compiler_context *ctx) |
| 260 |
{ |
| 261 |
if (!ctx->inputs->pushable_ubos) { |
| 262 |
/* If nothing is pushed, all UBOs need to be uploaded |
| 263 |
* conventionally */ |
| 264 |
ctx->ubo_mask = ~0; |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
struct mir_ubo_analysis analysis = mir_analyze_ranges(ctx); |
| 269 |
|
| 270 |
unsigned work_count = mir_work_heuristic(ctx, &analysis); |
| 271 |
unsigned promoted_count = 24 - work_count; |
| 272 |
|
| 273 |
/* Ensure we are 16 byte aligned to avoid underallocations */ |
| 274 |
mir_pick_ubo(&ctx->info->push, &analysis, promoted_count); |
| 275 |
ctx->info->push.count = ALIGN_POT(ctx->info->push.count, 4); |
| 276 |
|
| 277 |
/* First, figure out special indices a priori so we don't recompute a lot */ |
| 278 |
BITSET_WORD *special = mir_special_indices(ctx); |
| 279 |
|
| 280 |
ctx->ubo_mask = 0; |
| 281 |
|
| 282 |
mir_foreach_instr_global_safe(ctx, ins) { |
| 283 |
if (!mir_is_ubo(ins)) |
| 284 |
continue; |
| 285 |
|
| 286 |
unsigned ubo = midgard_unpack_ubo_index_imm(ins->load_store); |
| 287 |
unsigned qword = ins->constants.u32[0] / 16; |
| 288 |
|
| 289 |
if (!mir_is_pushable_ubo(ctx, ins)) { |
| 290 |
if (ins->src[1] == ~0) |
| 291 |
ctx->ubo_mask |= BITSET_BIT(ubo); |
| 292 |
else |
| 293 |
ctx->ubo_mask = ~0; |
| 294 |
|
| 295 |
continue; |
| 296 |
} |
| 297 |
|
| 298 |
/* Check if we decided to push this */ |
| 299 |
assert(ubo < analysis.nr_blocks); |
| 300 |
if (!BITSET_TEST(analysis.blocks[ubo].pushed, qword)) { |
| 301 |
ctx->ubo_mask |= BITSET_BIT(ubo); |
| 302 |
continue; |
| 303 |
} |
| 304 |
|
| 305 |
/* Find where we pushed to, TODO: unaligned pushes to pack */ |
| 306 |
unsigned base = pan_lookup_pushed_ubo(&ctx->info->push, ubo, qword * 16); |
| 307 |
assert((base & 0x3) == 0); |
| 308 |
|
| 309 |
unsigned address = base / 4; |
| 310 |
unsigned uniform_reg = 23 - address; |
| 311 |
|
| 312 |
/* Should've taken into account when pushing */ |
| 313 |
assert(address < promoted_count); |
| 314 |
unsigned promoted = SSA_FIXED_REGISTER(uniform_reg); |
| 315 |
|
| 316 |
/* We do need the move for safety for a non-SSA dest, or if |
| 317 |
* we're being fed into a special class */ |
| 318 |
|
| 319 |
bool needs_move = ins->dest & PAN_IS_REG || ins->dest == ctx->blend_src1; |
| 320 |
|
| 321 |
if (ins->dest < ctx->temp_count) |
| 322 |
needs_move |= BITSET_TEST(special, ins->dest); |
| 323 |
|
| 324 |
if (needs_move) { |
| 325 |
unsigned type_size = nir_alu_type_get_type_size(ins->dest_type); |
| 326 |
midgard_instruction mov = v_mov(promoted, ins->dest); |
| 327 |
mov.dest_type = nir_type_uint | type_size; |
| 328 |
mov.src_types[1] = mov.dest_type; |
| 329 |
|
| 330 |
uint16_t rounded = mir_round_bytemask_up(mir_bytemask(ins), type_size); |
| 331 |
mir_set_bytemask(&mov, rounded); |
| 332 |
mir_insert_instruction_before(ctx, ins, &mov); |
| 333 |
} else { |
| 334 |
mir_rewrite_index_src(ctx, ins->dest, promoted); |
| 335 |
} |
| 336 |
|
| 337 |
mir_remove_instruction(ins); |
| 338 |
} |
| 339 |
|
| 340 |
free(special); |
| 341 |
free(analysis.blocks); |
| 342 |
} |