Debian Codegraph @

src/panfrost/compiler/bifrost/bi_pressure_schedule.c

raw
1 /*
2 * Copyright (C) 2022 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 /* Bottom-up local scheduler to reduce register pressure */
7
8 #include "util/dag.h"
9 #include "compiler.h"
10
11 struct sched_ctx {
12 /* Dependency graph */
13 struct dag *dag;
14
15 /* Live set */
16 BITSET_WORD *live;
17 };
18
19 struct sched_node {
20 struct dag_node dag;
21
22 /* Instruction this node represents */
23 bi_instr *instr;
24 };
25
26 static void
27 add_dep(struct sched_node *a, struct sched_node *b)
28 {
29 if (a && b)
30 dag_add_edge(&a->dag, &b->dag, 0);
31 }
32
33 static struct dag *
34 create_dag(bi_context *ctx, bi_block *block, void *memctx)
35 {
36 struct dag *dag = dag_create(ctx);
37
38 struct sched_node **last_write =
39 calloc(ctx->ssa_alloc, sizeof(struct sched_node *));
40 struct sched_node *coverage = NULL;
41 struct sched_node *preload = NULL;
42
43 /* Last memory load, to serialize stores against */
44 struct sched_node *memory_load = NULL;
45
46 /* Last memory store, to serialize loads and stores against */
47 struct sched_node *memory_store = NULL;
48
49 bi_foreach_instr_in_block(block, I) {
50 /* Leave branches at the end */
51 if (I->op == BI_OPCODE_JUMP || bi_get_opcode_props(I)->branch)
52 break;
53
54 assert(I->branch_target == NULL);
55
56 struct sched_node *node = rzalloc(memctx, struct sched_node);
57 node->instr = I;
58 dag_init_node(dag, &node->dag);
59
60 /* Reads depend on writes, no other hazards in SSA */
61 bi_foreach_ssa_src(I, s)
62 add_dep(node, last_write[I->src[s].value]);
63
64 bi_foreach_dest(I, d)
65 last_write[I->dest[d].value] = node;
66
67 add_dep(node, preload);
68
69 switch (bi_get_opcode_props(I)->message) {
70 case BIFROST_MESSAGE_LOAD:
71 /* Regular memory loads needs to be serialized against
72 * other memory access. However, UBO memory is read-only
73 * so it can be moved around freely.
74 */
75 if (I->seg != BI_SEG_UBO) {
76 add_dep(node, memory_store);
77 memory_load = node;
78 }
79
80 break;
81
82 case BIFROST_MESSAGE_ATTRIBUTE:
83 /* Regular attribute loads can be reordered, but
84 * writeable attributes can't be. Our one use of
85 * writeable attributes are images.
86 */
87 if ((I->op == BI_OPCODE_LD_TEX) || (I->op == BI_OPCODE_LD_TEX_IMM) ||
88 (I->op == BI_OPCODE_LD_ATTR_TEX)) {
89 add_dep(node, memory_store);
90 memory_load = node;
91 }
92
93 break;
94
95 case BIFROST_MESSAGE_STORE:
96 assert(I->seg != BI_SEG_UBO);
97 add_dep(node, memory_load);
98 add_dep(node, memory_store);
99 memory_store = node;
100 break;
101
102 case BIFROST_MESSAGE_ATOMIC:
103 case BIFROST_MESSAGE_BARRIER:
104 add_dep(node, memory_load);
105 add_dep(node, memory_store);
106 memory_load = node;
107 memory_store = node;
108 break;
109
110 case BIFROST_MESSAGE_BLEND:
111 case BIFROST_MESSAGE_Z_STENCIL:
112 case BIFROST_MESSAGE_TILE:
113 add_dep(node, coverage);
114 coverage = node;
115 break;
116
117 case BIFROST_MESSAGE_ATEST:
118 /* ATEST signals the end of shader side effects */
119 add_dep(node, memory_store);
120 memory_store = node;
121
122 /* ATEST also updates coverage */
123 add_dep(node, coverage);
124 coverage = node;
125 break;
126 default:
127 break;
128 }
129
130 if (I->op == BI_OPCODE_DISCARD_F32) {
131 /* Serialize against ATEST */
132 add_dep(node, coverage);
133 coverage = node;
134 }
135 if (I->op == BI_OPCODE_DISCARD_F32 ||
136 bi_is_scheduling_barrier(I)) {
137 /* Serialize against memory operations and barriers.
138 *
139 * TODO: This is *not* quite sufficient in the case of a scheduling
140 * barrier. We need to serialize against *all* operations with
141 * side-effects in that case.
142 */
143 add_dep(node, memory_load);
144 add_dep(node, memory_store);
145 memory_load = node;
146 memory_store = node;
147 }
148 if ((I->op == BI_OPCODE_PHI) ||
149 (I->op == BI_OPCODE_MOV_I32 &&
150 I->src[0].type == BI_INDEX_REGISTER)) {
151 preload = node;
152 }
153 }
154
155 free(last_write);
156
157 return dag;
158 }
159
160 /*
161 * Calculate the change in register pressure from scheduling a given
162 * instruction. Equivalently, calculate the difference in the number of live
163 * registers before and after the instruction, given the live set after the
164 * instruction. This calculation follows immediately from the dataflow
165 * definition of liveness:
166 *
167 * live_in = (live_out - KILL) + GEN
168 */
169 static signed
170 calculate_pressure_delta(bi_instr *I, BITSET_WORD *live)
171 {
172 signed delta = 0;
173
174 /* Destinations must be unique */
175 bi_foreach_dest(I, d) {
176 if (BITSET_TEST(live, I->dest[d].value))
177 delta -= bi_count_write_registers(I, d);
178 }
179
180 bi_foreach_ssa_src(I, src) {
181 /* Filter duplicates */
182 bool dupe = false;
183
184 for (unsigned i = 0; i < src; ++i) {
185 if (bi_is_equiv(I->src[i], I->src[src])) {
186 dupe = true;
187 break;
188 }
189 }
190
191 if (!dupe && !BITSET_TEST(live, I->src[src].value))
192 delta += bi_count_read_registers(I, src);
193 }
194
195 return delta;
196 }
197
198 /*
199 * Choose the next instruction, bottom-up. For now we use a simple greedy
200 * heuristic: choose the instruction that has the best effect on liveness.
201 */
202 static struct sched_node *
203 choose_instr(struct sched_ctx *s)
204 {
205 int32_t min_delta = INT32_MAX;
206 struct sched_node *best = NULL;
207
208 list_for_each_entry(struct sched_node, n, &s->dag->heads, dag.link) {
209 int32_t delta = calculate_pressure_delta(n->instr, s->live);
210
211 if (delta < min_delta) {
212 best = n;
213 min_delta = delta;
214 }
215 }
216
217 return best;
218 }
219
220 static void
221 pressure_schedule_block(bi_context *ctx, bi_block *block, struct sched_ctx *s)
222 {
223 /* off by a constant, that's ok */
224 signed pressure = 0;
225 signed orig_max_pressure = 0;
226 unsigned nr_ins = 0;
227
228 memcpy(s->live, block->ssa_live_out, BITSET_BYTES(ctx->ssa_alloc));
229
230 bi_foreach_instr_in_block_rev(block, I) {
231 pressure += calculate_pressure_delta(I, s->live);
232 orig_max_pressure = MAX2(pressure, orig_max_pressure);
233 bi_liveness_ins_update_ssa(s->live, I);
234 nr_ins++;
235 }
236
237 memcpy(s->live, block->ssa_live_out, BITSET_BYTES(ctx->ssa_alloc));
238
239 /* off by a constant, that's ok */
240 signed max_pressure = 0;
241 pressure = 0;
242
243 struct sched_node **schedule = calloc(nr_ins, sizeof(struct sched_node *));
244 nr_ins = 0;
245
246 while (!list_is_empty(&s->dag->heads)) {
247 struct sched_node *node = choose_instr(s);
248 pressure += calculate_pressure_delta(node->instr, s->live);
249 max_pressure = MAX2(pressure, max_pressure);
250 dag_prune_head(s->dag, &node->dag);
251
252 schedule[nr_ins++] = node;
253 bi_liveness_ins_update_ssa(s->live, node->instr);
254 }
255
256 /* Bail if it looks like it's worse */
257 if (max_pressure >= orig_max_pressure) {
258 free(schedule);
259 return;
260 }
261
262 /* Apply the schedule */
263 for (unsigned i = 0; i < nr_ins; ++i) {
264 bi_remove_instruction(schedule[i]->instr);
265 list_add(&schedule[i]->instr->link, &block->instructions);
266 }
267
268 free(schedule);
269 }
270
271 void
272 bi_pressure_schedule(bi_context *ctx)
273 {
274 bi_compute_liveness_ssa(ctx);
275 void *memctx = ralloc_context(ctx);
276 BITSET_WORD *live =
277 ralloc_array(memctx, BITSET_WORD, BITSET_WORDS(ctx->ssa_alloc));
278
279 bi_foreach_block(ctx, block) {
280 struct sched_ctx sctx = {.dag = create_dag(ctx, block, memctx),
281 .live = live};
282
283 pressure_schedule_block(ctx, block, &sctx);
284 }
285
286 ralloc_free(memctx);
287 }