| 1 |
/* |
| 2 |
* Copyright 2023 Valve Corporation |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include "util/bitset.h" |
| 8 |
#include "util/hash_table.h" |
| 9 |
#include "util/ralloc.h" |
| 10 |
#include "agx_compiler.h" |
| 11 |
#include "agx_opcodes.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* Information about a constant, indexed by its 64-bit value. This describes the |
| 15 |
* value, not the move that generated it. If there are multiple moves in the |
| 16 |
* shader with the same immediate value, they resolve to the same constant. |
| 17 |
*/ |
| 18 |
struct constant_info { |
| 19 |
uint64_t value; |
| 20 |
|
| 21 |
/* Number of uses of the constant that could be promoted */ |
| 22 |
unsigned nr_promotable_uses; |
| 23 |
|
| 24 |
/* If we push, the uniform used */ |
| 25 |
uint16_t uniform; |
| 26 |
|
| 27 |
/* Alignment in 16-bit units needed for the constant */ |
| 28 |
uint8_t align_16; |
| 29 |
|
| 30 |
/* True if the constant was promoted to a uniform */ |
| 31 |
bool promoted; |
| 32 |
}; |
| 33 |
|
| 34 |
/* |
| 35 |
* Choosing constants to promote is similar to the 0-1 knapsack problem. We use |
| 36 |
* a well-known heuristic: sort by benefit divided by size. We approximate |
| 37 |
* benefit by use count. |
| 38 |
*/ |
| 39 |
static int |
| 40 |
constant_priority(const struct constant_info *const info) |
| 41 |
{ |
| 42 |
int size = info->align_16; |
| 43 |
assert(size == 1 || size == 2 || size == 4); |
| 44 |
int inverse_size = (size == 1) ? 4 : (size == 2) ? 2 : 1; |
| 45 |
|
| 46 |
return info->nr_promotable_uses * inverse_size; |
| 47 |
} |
| 48 |
|
| 49 |
static int |
| 50 |
priority_compare(const void *A_, const void *B_) |
| 51 |
{ |
| 52 |
const struct constant_info *const *A = A_; |
| 53 |
const struct constant_info *const *B = B_; |
| 54 |
|
| 55 |
/* This is backwards from qsort's documentation, because we want descending |
| 56 |
* order and qsort returns ascending. |
| 57 |
*/ |
| 58 |
return constant_priority(*B) - constant_priority(*A); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
record_use(void *memctx, struct hash_table_u64 *constants, uint64_t imm, |
| 63 |
enum agx_size size) |
| 64 |
{ |
| 65 |
struct constant_info *info = _mesa_hash_table_u64_search(constants, imm); |
| 66 |
|
| 67 |
if (!info) { |
| 68 |
info = rzalloc(memctx, struct constant_info); |
| 69 |
info->value = imm; |
| 70 |
_mesa_hash_table_u64_insert(constants, imm, info); |
| 71 |
} |
| 72 |
|
| 73 |
info->nr_promotable_uses++; |
| 74 |
info->align_16 = MAX2(info->align_16, agx_size_align_16(size)); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
pass(agx_context *ctx, void *memctx) |
| 79 |
{ |
| 80 |
/* Map from SSA indices to struct constant_info */ |
| 81 |
struct hash_table_u64 *constants = _mesa_hash_table_u64_create(memctx); |
| 82 |
|
| 83 |
/* Map from SSA indices to immediate values */ |
| 84 |
uint64_t *values = rzalloc_array(memctx, uint64_t, ctx->alloc); |
| 85 |
|
| 86 |
/* Set of SSA indices that map to immediate values */ |
| 87 |
BITSET_WORD *is_immediate = BITSET_RZALLOC(memctx, ctx->alloc); |
| 88 |
|
| 89 |
/* Gather constant definitions and use */ |
| 90 |
agx_foreach_instr_global(ctx, I) { |
| 91 |
if (I->op == AGX_OPCODE_MOV_IMM) { |
| 92 |
assert(I->dest[0].type == AGX_INDEX_NORMAL); |
| 93 |
BITSET_SET(is_immediate, I->dest[0].value); |
| 94 |
values[I->dest[0].value] = I->imm; |
| 95 |
} else { |
| 96 |
agx_foreach_ssa_src(I, s) { |
| 97 |
if (BITSET_TEST(is_immediate, I->src[s].value) && |
| 98 |
agx_instr_accepts_uniform(I->op, s, ctx->out->push_count, |
| 99 |
I->src[s].size)) { |
| 100 |
|
| 101 |
record_use(memctx, constants, values[I->src[s].value], |
| 102 |
I->src[s].size); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* Early exit if there were no constants */ |
| 109 |
unsigned nr_nodes = _mesa_hash_table_u64_num_entries(constants); |
| 110 |
if (nr_nodes == 0) |
| 111 |
return; |
| 112 |
|
| 113 |
/* Collect nodes that are promotable */ |
| 114 |
struct constant_info **flat = |
| 115 |
rzalloc_array(memctx, struct constant_info *, nr_nodes); |
| 116 |
|
| 117 |
unsigned flat_count = 0; |
| 118 |
hash_table_u64_foreach(constants, entry) { |
| 119 |
flat[flat_count++] = entry.data; |
| 120 |
} |
| 121 |
|
| 122 |
/* Select constants. Even when we can promote everything, sorting keeps hot |
| 123 |
* constants in lower uniforms, required by some instructions. |
| 124 |
*/ |
| 125 |
qsort(flat, flat_count, sizeof(*flat), priority_compare); |
| 126 |
|
| 127 |
/* Promote as many constants as we can */ |
| 128 |
for (unsigned i = 0; i < flat_count; ++i) { |
| 129 |
struct constant_info *info = flat[i]; |
| 130 |
assert(info->nr_promotable_uses > 0); |
| 131 |
|
| 132 |
/* Try to assign a uniform */ |
| 133 |
unsigned uniform = ALIGN_POT(ctx->out->push_count, info->align_16); |
| 134 |
unsigned new_count = uniform + info->align_16; |
| 135 |
if (new_count > AGX_NUM_UNIFORMS) |
| 136 |
break; |
| 137 |
|
| 138 |
/* Set the base for the first constant. This avoids uploading an extra |
| 139 |
* zero if we needed to pad, compared to doing this before the loop. Same |
| 140 |
* shader code, but slightly more efficient USC record. |
| 141 |
*/ |
| 142 |
if (i == 0) { |
| 143 |
ctx->out->rodata.base_uniform = uniform; |
| 144 |
} |
| 145 |
|
| 146 |
info->uniform = uniform; |
| 147 |
info->promoted = true; |
| 148 |
ctx->out->push_count = new_count; |
| 149 |
|
| 150 |
unsigned size_B = info->align_16 * 2; |
| 151 |
unsigned uniform_offs = uniform - ctx->out->rodata.base_uniform; |
| 152 |
assert(uniform_offs < ARRAY_SIZE(ctx->rodata)); |
| 153 |
memcpy(&ctx->rodata[uniform_offs], &info->value, size_B); |
| 154 |
|
| 155 |
ctx->out->rodata.size_16 = new_count - ctx->out->rodata.base_uniform; |
| 156 |
} |
| 157 |
|
| 158 |
/* Promote in the IR */ |
| 159 |
agx_foreach_instr_global(ctx, I) { |
| 160 |
agx_foreach_ssa_src(I, s) { |
| 161 |
if (!BITSET_TEST(is_immediate, I->src[s].value)) |
| 162 |
continue; |
| 163 |
|
| 164 |
struct constant_info *info = |
| 165 |
_mesa_hash_table_u64_search(constants, values[I->src[s].value]); |
| 166 |
|
| 167 |
if (info && info->promoted && |
| 168 |
agx_instr_accepts_uniform(I->op, s, info->uniform, |
| 169 |
I->src[s].size)) { |
| 170 |
|
| 171 |
agx_replace_src(I, s, agx_uniform(info->uniform, I->src[s].size)); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
agx_opt_promote_constants(agx_context *ctx) |
| 179 |
{ |
| 180 |
/* We do not promote constants in preambles since it's pointless and wastes |
| 181 |
* uniform slots. |
| 182 |
*/ |
| 183 |
if (ctx->is_preamble) |
| 184 |
return; |
| 185 |
|
| 186 |
void *memctx = ralloc_context(NULL); |
| 187 |
pass(ctx, memctx); |
| 188 |
ralloc_free(memctx); |
| 189 |
} |