| 1 |
/* |
| 2 |
* Copyright 2023 Alyssa Rosenzweig |
| 3 |
* Copyright 2022 Collabora Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Bottom-up local scheduler to reduce register pressure */ |
| 8 |
|
| 9 |
#include "util/dag.h" |
| 10 |
#include "util/sparse_bitset.h" |
| 11 |
#include "agx_compiler.h" |
| 12 |
#include "agx_opcodes.h" |
| 13 |
|
| 14 |
struct sched_ctx { |
| 15 |
/* Dependency graph */ |
| 16 |
struct dag *dag; |
| 17 |
|
| 18 |
/* Live set */ |
| 19 |
struct u_sparse_bitset live; |
| 20 |
}; |
| 21 |
|
| 22 |
struct sched_node { |
| 23 |
struct dag_node dag; |
| 24 |
|
| 25 |
/* Instruction this node represents */ |
| 26 |
agx_instr *instr; |
| 27 |
}; |
| 28 |
|
| 29 |
static void |
| 30 |
add_dep(struct sched_node *a, struct sched_node *b) |
| 31 |
{ |
| 32 |
assert(a != b && "no self-dependencies"); |
| 33 |
|
| 34 |
if (a && b) |
| 35 |
dag_add_edge(&a->dag, &b->dag, 0); |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
serialize(struct sched_node *a, struct sched_node **b) |
| 40 |
{ |
| 41 |
add_dep(a, *b); |
| 42 |
*b = a; |
| 43 |
} |
| 44 |
|
| 45 |
static struct dag * |
| 46 |
create_dag(agx_context *ctx, agx_block *block, void *memctx) |
| 47 |
{ |
| 48 |
struct dag *dag = dag_create(ctx); |
| 49 |
|
| 50 |
struct sched_node **last_write = |
| 51 |
calloc(ctx->alloc, sizeof(struct sched_node *)); |
| 52 |
struct sched_node *coverage = NULL; |
| 53 |
struct sched_node *preload = NULL; |
| 54 |
|
| 55 |
/* Last memory load, to serialize stores against */ |
| 56 |
struct sched_node *memory_load = NULL; |
| 57 |
|
| 58 |
/* Last memory store, to serialize loads and stores against */ |
| 59 |
struct sched_node *memory_store = NULL; |
| 60 |
|
| 61 |
agx_foreach_instr_in_block(block, I) { |
| 62 |
/* Don't touch control flow */ |
| 63 |
if (instr_after_logical_end(I)) |
| 64 |
break; |
| 65 |
|
| 66 |
struct sched_node *node = rzalloc(memctx, struct sched_node); |
| 67 |
node->instr = I; |
| 68 |
dag_init_node(dag, &node->dag); |
| 69 |
|
| 70 |
/* Reads depend on writes, no other hazards in SSA */ |
| 71 |
agx_foreach_ssa_src(I, s) { |
| 72 |
add_dep(node, last_write[I->src[s].value]); |
| 73 |
} |
| 74 |
|
| 75 |
agx_foreach_ssa_dest(I, d) { |
| 76 |
assert(I->dest[d].value < ctx->alloc); |
| 77 |
last_write[I->dest[d].value] = node; |
| 78 |
} |
| 79 |
|
| 80 |
/* Classify the instruction and add dependencies according to the class */ |
| 81 |
enum agx_schedule_class dep = agx_opcodes_info[I->op].schedule_class; |
| 82 |
assert(dep != AGX_SCHEDULE_CLASS_INVALID && "invalid instruction seen"); |
| 83 |
|
| 84 |
bool barrier = dep == AGX_SCHEDULE_CLASS_BARRIER; |
| 85 |
bool discards = |
| 86 |
I->op == AGX_OPCODE_SAMPLE_MASK || I->op == AGX_OPCODE_ZS_EMIT; |
| 87 |
|
| 88 |
if (dep == AGX_SCHEDULE_CLASS_STORE) |
| 89 |
add_dep(node, memory_load); |
| 90 |
else if (dep == AGX_SCHEDULE_CLASS_ATOMIC || barrier) |
| 91 |
serialize(node, &memory_load); |
| 92 |
|
| 93 |
if (dep == AGX_SCHEDULE_CLASS_LOAD || dep == AGX_SCHEDULE_CLASS_STORE || |
| 94 |
dep == AGX_SCHEDULE_CLASS_ATOMIC || barrier) |
| 95 |
serialize(node, &memory_store); |
| 96 |
|
| 97 |
if (dep == AGX_SCHEDULE_CLASS_COVERAGE || barrier) |
| 98 |
serialize(node, &coverage); |
| 99 |
|
| 100 |
/* Make sure side effects happen before a discard */ |
| 101 |
if (discards) |
| 102 |
add_dep(node, memory_store); |
| 103 |
|
| 104 |
if (dep == AGX_SCHEDULE_CLASS_PRELOAD) |
| 105 |
serialize(node, &preload); |
| 106 |
else |
| 107 |
add_dep(node, preload); |
| 108 |
} |
| 109 |
|
| 110 |
free(last_write); |
| 111 |
|
| 112 |
return dag; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Calculate the change in register pressure from scheduling a given |
| 117 |
* instruction. Equivalently, calculate the difference in the number of live |
| 118 |
* registers before and after the instruction, given the live set after the |
| 119 |
* instruction. This calculation follows immediately from the dataflow |
| 120 |
* definition of liveness: |
| 121 |
* |
| 122 |
* live_in = (live_out - KILL) + GEN |
| 123 |
*/ |
| 124 |
static signed |
| 125 |
calculate_pressure_delta(agx_instr *I, struct u_sparse_bitset *live) |
| 126 |
{ |
| 127 |
signed delta = 0; |
| 128 |
|
| 129 |
/* Destinations must be unique */ |
| 130 |
agx_foreach_ssa_dest(I, d) { |
| 131 |
if (u_sparse_bitset_test(live, I->dest[d].value)) |
| 132 |
delta -= agx_index_size_16(I->dest[d]); |
| 133 |
} |
| 134 |
|
| 135 |
agx_foreach_ssa_src(I, src) { |
| 136 |
/* Filter duplicates */ |
| 137 |
bool dupe = false; |
| 138 |
|
| 139 |
for (unsigned i = 0; i < src; ++i) { |
| 140 |
if (agx_is_equiv(I->src[i], I->src[src])) { |
| 141 |
dupe = true; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (!dupe && !u_sparse_bitset_test(live, I->src[src].value)) |
| 147 |
delta += agx_index_size_16(I->src[src]); |
| 148 |
} |
| 149 |
|
| 150 |
return delta; |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* Choose the next instruction, bottom-up. For now we use a simple greedy |
| 155 |
* heuristic: choose the instruction that has the best effect on liveness, while |
| 156 |
* hoisting sample_mask. |
| 157 |
*/ |
| 158 |
static struct sched_node * |
| 159 |
choose_instr(struct sched_ctx *s) |
| 160 |
{ |
| 161 |
int32_t min_delta = INT32_MAX; |
| 162 |
struct sched_node *best = NULL; |
| 163 |
|
| 164 |
list_for_each_entry(struct sched_node, n, &s->dag->heads, dag.link) { |
| 165 |
/* Heuristic: hoist sample_mask/zs_emit. This allows depth/stencil tests |
| 166 |
* to run earlier, and potentially to discard the entire quad invocation |
| 167 |
* earlier, reducing how much redundant fragment shader we run. |
| 168 |
* |
| 169 |
* Since we schedule backwards, we make that happen by only choosing |
| 170 |
* sample_mask when all other instructions have been exhausted. |
| 171 |
*/ |
| 172 |
if (n->instr->op == AGX_OPCODE_SAMPLE_MASK || |
| 173 |
n->instr->op == AGX_OPCODE_ZS_EMIT) { |
| 174 |
|
| 175 |
if (!best) { |
| 176 |
best = n; |
| 177 |
assert(min_delta == INT32_MAX); |
| 178 |
} |
| 179 |
|
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
/* Heuristic: sink wait_pix to increase parallelism. Since wait_pix does |
| 184 |
* not read or write registers, this has no effect on pressure. |
| 185 |
*/ |
| 186 |
if (n->instr->op == AGX_OPCODE_WAIT_PIX) |
| 187 |
return n; |
| 188 |
|
| 189 |
int32_t delta = calculate_pressure_delta(n->instr, &s->live); |
| 190 |
|
| 191 |
if (delta < min_delta) { |
| 192 |
best = n; |
| 193 |
min_delta = delta; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return best; |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
pressure_schedule_block(agx_context *ctx, agx_block *block, struct sched_ctx *s) |
| 202 |
{ |
| 203 |
/* off by a constant, that's ok */ |
| 204 |
signed pressure = 0; |
| 205 |
signed orig_max_pressure = 0; |
| 206 |
unsigned nr_ins = 0; |
| 207 |
|
| 208 |
u_sparse_bitset_dup(&s->live, &block->live_out); |
| 209 |
|
| 210 |
agx_foreach_instr_in_block_rev(block, I) { |
| 211 |
pressure += calculate_pressure_delta(I, &s->live); |
| 212 |
orig_max_pressure = MAX2(pressure, orig_max_pressure); |
| 213 |
agx_liveness_ins_update(&s->live, I); |
| 214 |
nr_ins++; |
| 215 |
} |
| 216 |
|
| 217 |
u_sparse_bitset_dup(&s->live, &block->live_out); |
| 218 |
|
| 219 |
/* off by a constant, that's ok */ |
| 220 |
signed max_pressure = 0; |
| 221 |
pressure = 0; |
| 222 |
|
| 223 |
struct sched_node **schedule = calloc(nr_ins, sizeof(struct sched_node *)); |
| 224 |
nr_ins = 0; |
| 225 |
|
| 226 |
while (!list_is_empty(&s->dag->heads)) { |
| 227 |
struct sched_node *node = choose_instr(s); |
| 228 |
pressure += calculate_pressure_delta(node->instr, &s->live); |
| 229 |
max_pressure = MAX2(pressure, max_pressure); |
| 230 |
dag_prune_head(s->dag, &node->dag); |
| 231 |
|
| 232 |
schedule[nr_ins++] = node; |
| 233 |
agx_liveness_ins_update(&s->live, node->instr); |
| 234 |
} |
| 235 |
|
| 236 |
/* Bail if it looks like it's worse */ |
| 237 |
if (max_pressure >= orig_max_pressure) { |
| 238 |
free(schedule); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
/* Apply the schedule */ |
| 243 |
for (unsigned i = 0; i < nr_ins; ++i) { |
| 244 |
agx_remove_instruction(schedule[i]->instr); |
| 245 |
list_add(&schedule[i]->instr->link, &block->instructions); |
| 246 |
} |
| 247 |
|
| 248 |
free(schedule); |
| 249 |
} |
| 250 |
|
| 251 |
void |
| 252 |
agx_pressure_schedule(agx_context *ctx) |
| 253 |
{ |
| 254 |
agx_compute_liveness(ctx); |
| 255 |
void *memctx = ralloc_context(ctx); |
| 256 |
|
| 257 |
agx_foreach_block(ctx, block) { |
| 258 |
struct sched_ctx sctx = {.dag = create_dag(ctx, block, memctx)}; |
| 259 |
u_sparse_bitset_init(&sctx.live, ctx->alloc, memctx); |
| 260 |
|
| 261 |
pressure_schedule_block(ctx, block, &sctx); |
| 262 |
} |
| 263 |
|
| 264 |
/* Clean up after liveness analysis */ |
| 265 |
agx_foreach_instr_global(ctx, I) { |
| 266 |
agx_foreach_ssa_src(I, s) |
| 267 |
I->src[s].kill = false; |
| 268 |
} |
| 269 |
|
| 270 |
ralloc_free(memctx); |
| 271 |
} |