panfrost/midgard: Apply code styling
[mesa.git] / src / gallium / drivers / panfrost / midgard / midgard_ra.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "compiler.h"
26 #include "midgard_ops.h"
27 #include "util/register_allocate.h"
28 #include "util/u_math.h"
29
30 /* For work registers, we can subdivide in various ways. So we create
31 * classes for the various sizes and conflict accordingly, keeping in
32 * mind that physical registers are divided along 128-bit boundaries.
33 * The important part is that 128-bit boundaries are not crossed.
34 *
35 * For each 128-bit register, we can subdivide to 32-bits 10 ways
36 *
37 * vec4: xyzw
38 * vec3: xyz, yzw
39 * vec2: xy, yz, zw,
40 * vec1: x, y, z, w
41 *
42 * For each 64-bit register, we can subdivide similarly to 16-bit
43 * (TODO: half-float RA, not that we support fp16 yet)
44 */
45
46 #define WORK_STRIDE 10
47
48 /* Prepacked masks/swizzles for virtual register types */
49 static unsigned reg_type_to_mask[WORK_STRIDE] = {
50 0xF, /* xyzw */
51 0x7, 0x7 << 1, /* xyz */
52 0x3, 0x3 << 1, 0x3 << 2, /* xy */
53 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
54 };
55
56 static unsigned reg_type_to_swizzle[WORK_STRIDE] = {
57 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
58
59 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
60 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_W, COMPONENT_W),
61
62 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
63 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_Z, COMPONENT_W),
64 SWIZZLE(COMPONENT_Z, COMPONENT_W, COMPONENT_Z, COMPONENT_W),
65
66 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
67 SWIZZLE(COMPONENT_Y, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
68 SWIZZLE(COMPONENT_Z, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
69 SWIZZLE(COMPONENT_W, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
70 };
71
72 struct phys_reg {
73 unsigned reg;
74 unsigned mask;
75 unsigned swizzle;
76 };
77
78 /* Given the mask/swizzle of both the register and the original source,
79 * compose to find the actual mask/swizzle to give the hardware */
80
81 static unsigned
82 compose_writemask(unsigned mask, struct phys_reg reg)
83 {
84 /* Note: the reg mask is guaranteed to be contiguous. So we shift
85 * into the X place, compose via a simple AND, and shift back */
86
87 unsigned shift = __builtin_ctz(reg.mask);
88 return ((reg.mask >> shift) & mask) << shift;
89 }
90
91 static unsigned
92 compose_swizzle(unsigned swizzle, unsigned mask,
93 struct phys_reg reg, struct phys_reg dst)
94 {
95 unsigned out = pan_compose_swizzle(swizzle, reg.swizzle);
96
97 /* Based on the register mask, we need to adjust over. E.g if we're
98 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
99 * original first component (x). But to prevent duplicate shifting
100 * (only applies to ALU -- mask param is set to xyzw out on L/S to
101 * prevent changes), we have to account for the shift inherent to the
102 * original writemask */
103
104 unsigned rep = out & 0x3;
105 unsigned shift = __builtin_ctz(dst.mask) - __builtin_ctz(mask);
106 unsigned shifted = out << (2*shift);
107
108 /* ..but we fill in the gaps so it appears to replicate */
109
110 for (unsigned s = 0; s < shift; ++s)
111 shifted |= rep << (2*s);
112
113 return shifted;
114 }
115
116 /* When we're 'squeezing down' the values in the IR, we maintain a hash
117 * as such */
118
119 static unsigned
120 find_or_allocate_temp(compiler_context *ctx, unsigned hash)
121 {
122 if ((hash < 0) || (hash >= SSA_FIXED_MINIMUM))
123 return hash;
124
125 unsigned temp = (uintptr_t) _mesa_hash_table_u64_search(
126 ctx->hash_to_temp, hash + 1);
127
128 if (temp)
129 return temp - 1;
130
131 /* If no temp is find, allocate one */
132 temp = ctx->temp_count++;
133 ctx->max_hash = MAX2(ctx->max_hash, hash);
134
135 _mesa_hash_table_u64_insert(ctx->hash_to_temp,
136 hash + 1, (void *) ((uintptr_t) temp + 1));
137
138 return temp;
139 }
140
141 /* Callback for register allocation selection, trivial default for now */
142
143 static unsigned int
144 midgard_ra_select_callback(struct ra_graph *g, BITSET_WORD *regs, void *data)
145 {
146 /* Choose the first available register to minimise register pressure */
147
148 for (int i = 0; i < (16 * WORK_STRIDE); ++i) {
149 if (BITSET_TEST(regs, i)) {
150 return i;
151 }
152 }
153
154 assert(0);
155 return 0;
156 }
157
158 /* Helper to return the default phys_reg for a given register */
159
160 static struct phys_reg
161 default_phys_reg(int reg)
162 {
163 struct phys_reg r = {
164 .reg = reg,
165 .mask = 0xF, /* xyzw */
166 .swizzle = 0xE4 /* xyzw */
167 };
168
169 return r;
170 }
171
172 /* Determine which physical register, swizzle, and mask a virtual
173 * register corresponds to */
174
175 static struct phys_reg
176 index_to_reg(compiler_context *ctx, struct ra_graph *g, int reg)
177 {
178 /* Check for special cases */
179 if (reg >= SSA_FIXED_MINIMUM)
180 return default_phys_reg(SSA_REG_FROM_FIXED(reg));
181 else if ((reg < 0) || !g)
182 return default_phys_reg(REGISTER_UNUSED);
183
184 /* Special cases aside, we pick the underlying register */
185 int virt = ra_get_node_reg(g, reg);
186
187 /* Divide out the register and classification */
188 int phys = virt / WORK_STRIDE;
189 int type = virt % WORK_STRIDE;
190
191 struct phys_reg r = {
192 .reg = phys,
193 .mask = reg_type_to_mask[type],
194 .swizzle = reg_type_to_swizzle[type]
195 };
196
197 /* Report that we actually use this register, and return it */
198 ctx->work_registers = MAX2(ctx->work_registers, phys);
199 return r;
200 }
201
202 /* This routine performs the actual register allocation. It should be succeeded
203 * by install_registers */
204
205 struct ra_graph *
206 allocate_registers(compiler_context *ctx)
207 {
208 /* The number of vec4 work registers available depends on when the
209 * uniforms start, so compute that first */
210
211 int work_count = 16 - MAX2((ctx->uniform_cutoff - 8), 0);
212
213 int virtual_count = work_count * WORK_STRIDE;
214
215 /* First, initialize the RA */
216 struct ra_regs *regs = ra_alloc_reg_set(NULL, virtual_count, true);
217
218 int work_vec4 = ra_alloc_reg_class(regs);
219 int work_vec3 = ra_alloc_reg_class(regs);
220 int work_vec2 = ra_alloc_reg_class(regs);
221 int work_vec1 = ra_alloc_reg_class(regs);
222
223 unsigned classes[4] = {
224 work_vec1,
225 work_vec2,
226 work_vec3,
227 work_vec4
228 };
229
230 /* Add the full set of work registers */
231 for (unsigned i = 0; i < work_count; ++i) {
232 int base = WORK_STRIDE * i;
233
234 /* Build a full set of subdivisions */
235 ra_class_add_reg(regs, work_vec4, base);
236 ra_class_add_reg(regs, work_vec3, base + 1);
237 ra_class_add_reg(regs, work_vec3, base + 2);
238 ra_class_add_reg(regs, work_vec2, base + 3);
239 ra_class_add_reg(regs, work_vec2, base + 4);
240 ra_class_add_reg(regs, work_vec2, base + 5);
241 ra_class_add_reg(regs, work_vec1, base + 6);
242 ra_class_add_reg(regs, work_vec1, base + 7);
243 ra_class_add_reg(regs, work_vec1, base + 8);
244 ra_class_add_reg(regs, work_vec1, base + 9);
245
246 for (unsigned a = 0; a < 10; ++a) {
247 unsigned mask1 = reg_type_to_mask[a];
248
249 for (unsigned b = 0; b < 10; ++b) {
250 unsigned mask2 = reg_type_to_mask[b];
251
252 if (mask1 & mask2)
253 ra_add_reg_conflict(regs,
254 base + a, base + b);
255 }
256 }
257 }
258
259 /* We're done setting up */
260 ra_set_finalize(regs, NULL);
261
262 /* Transform the MIR into squeezed index form */
263 mir_foreach_block(ctx, block) {
264 mir_foreach_instr_in_block(block, ins) {
265 if (ins->compact_branch) continue;
266
267 ins->ssa_args.dest = find_or_allocate_temp(ctx, ins->ssa_args.dest);
268 ins->ssa_args.src0 = find_or_allocate_temp(ctx, ins->ssa_args.src0);
269
270 if (!ins->ssa_args.inline_constant)
271 ins->ssa_args.src1 = find_or_allocate_temp(ctx, ins->ssa_args.src1);
272
273 }
274 }
275
276 /* No register allocation to do with no SSA */
277
278 if (!ctx->temp_count)
279 return NULL;
280
281 /* Let's actually do register allocation */
282 int nodes = ctx->temp_count;
283 struct ra_graph *g = ra_alloc_interference_graph(regs, nodes);
284
285 /* Determine minimum size needed to hold values, to indirectly
286 * determine class */
287
288 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
289
290 mir_foreach_block(ctx, block) {
291 mir_foreach_instr_in_block(block, ins) {
292 if (ins->compact_branch) continue;
293 if (ins->ssa_args.dest < 0) continue;
294 if (ins->ssa_args.dest >= SSA_FIXED_MINIMUM) continue;
295
296 int class = util_logbase2(ins->mask) + 1;
297
298 /* Use the largest class if there's ambiguity, this
299 * handles partial writes */
300
301 int dest = ins->ssa_args.dest;
302 found_class[dest] = MAX2(found_class[dest], class);
303 }
304 }
305
306 for (unsigned i = 0; i < ctx->temp_count; ++i) {
307 unsigned class = found_class[i];
308 if (!class) continue;
309 ra_set_node_class(g, i, classes[class - 1]);
310 }
311
312 /* Determine liveness */
313
314 int *live_start = malloc(nodes * sizeof(int));
315 int *live_end = malloc(nodes * sizeof(int));
316
317 /* Initialize as non-existent */
318
319 for (int i = 0; i < nodes; ++i) {
320 live_start[i] = live_end[i] = -1;
321 }
322
323 int d = 0;
324
325 mir_foreach_block(ctx, block) {
326 mir_foreach_instr_in_block(block, ins) {
327 if (ins->compact_branch) continue;
328
329 /* Dest is < 0 for st_vary instructions, which break
330 * the usual SSA conventions. Liveness analysis doesn't
331 * make sense on these instructions, so skip them to
332 * avoid memory corruption */
333
334 if (ins->ssa_args.dest < 0) continue;
335
336 if (ins->ssa_args.dest < SSA_FIXED_MINIMUM) {
337 /* If this destination is not yet live, it is
338 * now since we just wrote it */
339
340 int dest = ins->ssa_args.dest;
341
342 if (live_start[dest] == -1)
343 live_start[dest] = d;
344 }
345
346 /* Since we just used a source, the source might be
347 * dead now. Scan the rest of the block for
348 * invocations, and if there are none, the source dies
349 * */
350
351 int sources[2] = {
352 ins->ssa_args.src0, ins->ssa_args.src1
353 };
354
355 for (int src = 0; src < 2; ++src) {
356 int s = sources[src];
357
358 if (s < 0) continue;
359
360 if (s >= SSA_FIXED_MINIMUM) continue;
361
362 if (!mir_is_live_after(ctx, block, ins, s)) {
363 live_end[s] = d;
364 }
365 }
366
367 ++d;
368 }
369 }
370
371 /* If a node still hasn't been killed, kill it now */
372
373 for (int i = 0; i < nodes; ++i) {
374 /* live_start == -1 most likely indicates a pinned output */
375
376 if (live_end[i] == -1)
377 live_end[i] = d;
378 }
379
380 /* Setup interference between nodes that are live at the same time */
381
382 for (int i = 0; i < nodes; ++i) {
383 for (int j = i + 1; j < nodes; ++j) {
384 bool j_overlaps_i = live_start[j] < live_end[i];
385 bool i_overlaps_j = live_end[j] < live_start[i];
386
387 if (i_overlaps_j || j_overlaps_i)
388 ra_add_node_interference(g, i, j);
389 }
390 }
391
392 ra_set_select_reg_callback(g, midgard_ra_select_callback, NULL);
393
394 if (!ra_allocate(g)) {
395 unreachable("Error allocating registers\n");
396 }
397
398 /* Cleanup */
399 free(live_start);
400 free(live_end);
401
402 return g;
403 }
404
405 /* Once registers have been decided via register allocation
406 * (allocate_registers), we need to rewrite the MIR to use registers instead of
407 * indices */
408
409 static void
410 install_registers_instr(
411 compiler_context *ctx,
412 struct ra_graph *g,
413 midgard_instruction *ins)
414 {
415 ssa_args args = ins->ssa_args;
416
417 switch (ins->type) {
418 case TAG_ALU_4: {
419 int adjusted_src = args.inline_constant ? -1 : args.src1;
420 struct phys_reg src1 = index_to_reg(ctx, g, args.src0);
421 struct phys_reg src2 = index_to_reg(ctx, g, adjusted_src);
422 struct phys_reg dest = index_to_reg(ctx, g, args.dest);
423
424 unsigned uncomposed_mask = ins->mask;
425 ins->mask = compose_writemask(uncomposed_mask, dest);
426
427 /* Adjust the dest mask if necessary. Mostly this is a no-op
428 * but it matters for dot products */
429 dest.mask = effective_writemask(&ins->alu, ins->mask);
430
431 midgard_vector_alu_src mod1 =
432 vector_alu_from_unsigned(ins->alu.src1);
433 mod1.swizzle = compose_swizzle(mod1.swizzle, uncomposed_mask, src1, dest);
434 ins->alu.src1 = vector_alu_srco_unsigned(mod1);
435
436 ins->registers.src1_reg = src1.reg;
437
438 ins->registers.src2_imm = args.inline_constant;
439
440 if (args.inline_constant) {
441 /* Encode inline 16-bit constant. See disassembler for
442 * where the algorithm is from */
443
444 ins->registers.src2_reg = ins->inline_constant >> 11;
445
446 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
447 uint16_t imm = ((lower_11 >> 8) & 0x7) |
448 ((lower_11 & 0xFF) << 3);
449
450 ins->alu.src2 = imm << 2;
451 } else {
452 midgard_vector_alu_src mod2 =
453 vector_alu_from_unsigned(ins->alu.src2);
454 mod2.swizzle = compose_swizzle(
455 mod2.swizzle, uncomposed_mask, src2, dest);
456 ins->alu.src2 = vector_alu_srco_unsigned(mod2);
457
458 ins->registers.src2_reg = src2.reg;
459 }
460
461 ins->registers.out_reg = dest.reg;
462 break;
463 }
464
465 case TAG_LOAD_STORE_4: {
466 if (OP_IS_STORE_VARY(ins->load_store.op)) {
467 /* TODO: use ssa_args for st_vary */
468 ins->load_store.reg = 0;
469 } else {
470 /* Which physical register we read off depends on
471 * whether we are loading or storing -- think about the
472 * logical dataflow */
473
474 unsigned r = OP_IS_STORE(ins->load_store.op) ?
475 args.src0 : args.dest;
476 struct phys_reg src = index_to_reg(ctx, g, r);
477
478 ins->load_store.reg = src.reg;
479
480 ins->load_store.swizzle = compose_swizzle(
481 ins->load_store.swizzle, 0xF,
482 default_phys_reg(0), src);
483
484 ins->mask = compose_writemask(
485 ins->mask, src);
486 }
487
488 break;
489 }
490
491 default:
492 break;
493 }
494 }
495
496 void
497 install_registers(compiler_context *ctx, struct ra_graph *g)
498 {
499 mir_foreach_block(ctx, block) {
500 mir_foreach_instr_in_block(block, ins) {
501 if (ins->compact_branch) continue;
502 install_registers_instr(ctx, g, ins);
503 }
504 }
505
506 }