2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "midgard_ops.h"
26 #include "util/register_allocate.h"
27 #include "util/u_math.h"
29 /* For work registers, we can subdivide in various ways. So we create
30 * classes for the various sizes and conflict accordingly, keeping in
31 * mind that physical registers are divided along 128-bit boundaries.
32 * The important part is that 128-bit boundaries are not crossed.
34 * For each 128-bit register, we can subdivide to 32-bits 10 ways
41 * For each 64-bit register, we can subdivide similarly to 16-bit
42 * (TODO: half-float RA, not that we support fp16 yet)
45 #define WORK_STRIDE 10
47 /* Prepacked masks/swizzles for virtual register types */
48 static unsigned reg_type_to_mask
[WORK_STRIDE
] = {
50 0x7, 0x7 << 1, /* xyz */
51 0x3, 0x3 << 1, 0x3 << 2, /* xy */
52 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
55 static unsigned reg_type_to_swizzle
[WORK_STRIDE
] = {
56 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
58 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
59 SWIZZLE(COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
, COMPONENT_W
),
61 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
62 SWIZZLE(COMPONENT_Y
, COMPONENT_Z
, COMPONENT_Z
, COMPONENT_W
),
63 SWIZZLE(COMPONENT_Z
, COMPONENT_W
, COMPONENT_Z
, COMPONENT_W
),
65 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
66 SWIZZLE(COMPONENT_Y
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
67 SWIZZLE(COMPONENT_Z
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
68 SWIZZLE(COMPONENT_W
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
77 /* Given the mask/swizzle of both the register and the original source,
78 * compose to find the actual mask/swizzle to give the hardware */
81 compose_writemask(unsigned mask
, struct phys_reg reg
)
83 /* Note: the reg mask is guaranteed to be contiguous. So we shift
84 * into the X place, compose via a simple AND, and shift back */
86 unsigned shift
= __builtin_ctz(reg
.mask
);
87 return ((reg
.mask
>> shift
) & mask
) << shift
;
91 compose_swizzle(unsigned swizzle
, unsigned mask
,
92 struct phys_reg reg
, struct phys_reg dst
)
96 for (unsigned c
= 0; c
< 4; ++c
) {
97 unsigned s
= (swizzle
>> (2*c
)) & 0x3;
98 unsigned q
= (reg
.swizzle
>> (2*s
)) & 0x3;
103 /* Based on the register mask, we need to adjust over. E.g if we're
104 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
105 * original first component (x). But to prevent duplicate shifting
106 * (only applies to ALU -- mask param is set to xyzw out on L/S to
107 * prevent changes), we have to account for the shift inherent to the
108 * original writemask */
110 unsigned rep
= out
& 0x3;
111 unsigned shift
= __builtin_ctz(dst
.mask
) - __builtin_ctz(mask
);
112 unsigned shifted
= out
<< (2*shift
);
114 /* ..but we fill in the gaps so it appears to replicate */
116 for (unsigned s
= 0; s
< shift
; ++s
)
117 shifted
|= rep
<< (2*s
);
122 /* When we're 'squeezing down' the values in the IR, we maintain a hash
126 find_or_allocate_temp(compiler_context
*ctx
, unsigned hash
)
128 if ((hash
< 0) || (hash
>= SSA_FIXED_MINIMUM
))
131 unsigned temp
= (uintptr_t) _mesa_hash_table_u64_search(
132 ctx
->hash_to_temp
, hash
+ 1);
137 /* If no temp is find, allocate one */
138 temp
= ctx
->temp_count
++;
139 ctx
->max_hash
= MAX2(ctx
->max_hash
, hash
);
141 _mesa_hash_table_u64_insert(ctx
->hash_to_temp
,
142 hash
+ 1, (void *) ((uintptr_t) temp
+ 1));
147 /* Callback for register allocation selection, trivial default for now */
150 midgard_ra_select_callback(struct ra_graph
*g
, BITSET_WORD
*regs
, void *data
)
152 /* Choose the first available register to minimise register pressure */
154 for (int i
= 0; i
< (16 * WORK_STRIDE
); ++i
) {
155 if (BITSET_TEST(regs
, i
)) {
164 /* Helper to return the default phys_reg for a given register */
166 static struct phys_reg
167 default_phys_reg(int reg
)
169 struct phys_reg r
= {
171 .mask
= 0xF, /* xyzw */
172 .swizzle
= 0xE4 /* xyzw */
178 /* Determine which physical register, swizzle, and mask a virtual
179 * register corresponds to */
181 static struct phys_reg
182 index_to_reg(compiler_context
*ctx
, struct ra_graph
*g
, int reg
)
184 /* Check for special cases */
185 if (reg
>= SSA_FIXED_MINIMUM
)
186 return default_phys_reg(SSA_REG_FROM_FIXED(reg
));
187 else if ((reg
< 0) || !g
)
188 return default_phys_reg(REGISTER_UNUSED
);
190 /* Special cases aside, we pick the underlying register */
191 int virt
= ra_get_node_reg(g
, reg
);
193 /* Divide out the register and classification */
194 int phys
= virt
/ WORK_STRIDE
;
195 int type
= virt
% WORK_STRIDE
;
197 struct phys_reg r
= {
199 .mask
= reg_type_to_mask
[type
],
200 .swizzle
= reg_type_to_swizzle
[type
]
203 /* Report that we actually use this register, and return it */
204 ctx
->work_registers
= MAX2(ctx
->work_registers
, phys
);
208 /* This routine performs the actual register allocation. It should be succeeded
209 * by install_registers */
212 allocate_registers(compiler_context
*ctx
)
214 /* The number of vec4 work registers available depends on when the
215 * uniforms start, so compute that first */
217 int work_count
= 16 - MAX2((ctx
->uniform_cutoff
- 8), 0);
219 int virtual_count
= work_count
* WORK_STRIDE
;
221 /* First, initialize the RA */
222 struct ra_regs
*regs
= ra_alloc_reg_set(NULL
, virtual_count
, true);
224 int work_vec4
= ra_alloc_reg_class(regs
);
225 int work_vec3
= ra_alloc_reg_class(regs
);
226 int work_vec2
= ra_alloc_reg_class(regs
);
227 int work_vec1
= ra_alloc_reg_class(regs
);
229 unsigned classes
[4] = {
236 /* Add the full set of work registers */
237 for (unsigned i
= 0; i
< work_count
; ++i
) {
238 int base
= WORK_STRIDE
* i
;
240 /* Build a full set of subdivisions */
241 ra_class_add_reg(regs
, work_vec4
, base
);
242 ra_class_add_reg(regs
, work_vec3
, base
+ 1);
243 ra_class_add_reg(regs
, work_vec3
, base
+ 2);
244 ra_class_add_reg(regs
, work_vec2
, base
+ 3);
245 ra_class_add_reg(regs
, work_vec2
, base
+ 4);
246 ra_class_add_reg(regs
, work_vec2
, base
+ 5);
247 ra_class_add_reg(regs
, work_vec1
, base
+ 6);
248 ra_class_add_reg(regs
, work_vec1
, base
+ 7);
249 ra_class_add_reg(regs
, work_vec1
, base
+ 8);
250 ra_class_add_reg(regs
, work_vec1
, base
+ 9);
252 for (unsigned a
= 0; a
< 10; ++a
) {
253 unsigned mask1
= reg_type_to_mask
[a
];
255 for (unsigned b
= 0; b
< 10; ++b
) {
256 unsigned mask2
= reg_type_to_mask
[b
];
259 ra_add_reg_conflict(regs
,
265 /* We're done setting up */
266 ra_set_finalize(regs
, NULL
);
268 /* Transform the MIR into squeezed index form */
269 mir_foreach_block(ctx
, block
) {
270 mir_foreach_instr_in_block(block
, ins
) {
271 if (ins
->compact_branch
) continue;
273 ins
->ssa_args
.dest
= find_or_allocate_temp(ctx
, ins
->ssa_args
.dest
);
274 ins
->ssa_args
.src0
= find_or_allocate_temp(ctx
, ins
->ssa_args
.src0
);
276 if (!ins
->ssa_args
.inline_constant
)
277 ins
->ssa_args
.src1
= find_or_allocate_temp(ctx
, ins
->ssa_args
.src1
);
282 /* No register allocation to do with no SSA */
284 if (!ctx
->temp_count
)
287 /* Let's actually do register allocation */
288 int nodes
= ctx
->temp_count
;
289 struct ra_graph
*g
= ra_alloc_interference_graph(regs
, nodes
);
291 /* Determine minimum size needed to hold values, to indirectly
294 unsigned *found_class
= calloc(sizeof(unsigned), ctx
->temp_count
);
296 mir_foreach_block(ctx
, block
) {
297 mir_foreach_instr_in_block(block
, ins
) {
298 if (ins
->compact_branch
) continue;
299 if (ins
->ssa_args
.dest
< 0) continue;
300 if (ins
->ssa_args
.dest
>= SSA_FIXED_MINIMUM
) continue;
302 /* Default to vec4 if we're not sure */
306 if (ins
->type
== TAG_ALU_4
)
307 mask
= squeeze_writemask(ins
->alu
.mask
);
308 else if (ins
->type
== TAG_LOAD_STORE_4
)
309 mask
= ins
->load_store
.mask
;
311 int class = util_logbase2(mask
) + 1;
313 /* Use the largest class if there's ambiguity, this
314 * handles partial writes */
316 int dest
= ins
->ssa_args
.dest
;
317 found_class
[dest
] = MAX2(found_class
[dest
], class);
321 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
322 unsigned class = found_class
[i
];
323 if (!class) continue;
324 ra_set_node_class(g
, i
, classes
[class - 1]);
327 /* Determine liveness */
329 int *live_start
= malloc(nodes
* sizeof(int));
330 int *live_end
= malloc(nodes
* sizeof(int));
332 /* Initialize as non-existent */
334 for (int i
= 0; i
< nodes
; ++i
) {
335 live_start
[i
] = live_end
[i
] = -1;
340 mir_foreach_block(ctx
, block
) {
341 mir_foreach_instr_in_block(block
, ins
) {
342 if (ins
->compact_branch
) continue;
344 /* Dest is < 0 for st_vary instructions, which break
345 * the usual SSA conventions. Liveness analysis doesn't
346 * make sense on these instructions, so skip them to
347 * avoid memory corruption */
349 if (ins
->ssa_args
.dest
< 0) continue;
351 if (ins
->ssa_args
.dest
< SSA_FIXED_MINIMUM
) {
352 /* If this destination is not yet live, it is
353 * now since we just wrote it */
355 int dest
= ins
->ssa_args
.dest
;
357 if (live_start
[dest
] == -1)
358 live_start
[dest
] = d
;
361 /* Since we just used a source, the source might be
362 * dead now. Scan the rest of the block for
363 * invocations, and if there are none, the source dies
367 ins
->ssa_args
.src0
, ins
->ssa_args
.src1
370 for (int src
= 0; src
< 2; ++src
) {
371 int s
= sources
[src
];
375 if (s
>= SSA_FIXED_MINIMUM
) continue;
377 if (!mir_is_live_after(ctx
, block
, ins
, s
)) {
386 /* If a node still hasn't been killed, kill it now */
388 for (int i
= 0; i
< nodes
; ++i
) {
389 /* live_start == -1 most likely indicates a pinned output */
391 if (live_end
[i
] == -1)
395 /* Setup interference between nodes that are live at the same time */
397 for (int i
= 0; i
< nodes
; ++i
) {
398 for (int j
= i
+ 1; j
< nodes
; ++j
) {
399 bool j_overlaps_i
= live_start
[j
] < live_end
[i
];
400 bool i_overlaps_j
= live_end
[j
] < live_start
[i
];
402 if (i_overlaps_j
|| j_overlaps_i
)
403 ra_add_node_interference(g
, i
, j
);
407 ra_set_select_reg_callback(g
, midgard_ra_select_callback
, NULL
);
409 if (!ra_allocate(g
)) {
410 unreachable("Error allocating registers\n");
420 /* Once registers have been decided via register allocation
421 * (allocate_registers), we need to rewrite the MIR to use registers instead of
425 install_registers_instr(
426 compiler_context
*ctx
,
428 midgard_instruction
*ins
)
430 ssa_args args
= ins
->ssa_args
;
434 int adjusted_src
= args
.inline_constant
? -1 : args
.src1
;
435 struct phys_reg src1
= index_to_reg(ctx
, g
, args
.src0
);
436 struct phys_reg src2
= index_to_reg(ctx
, g
, adjusted_src
);
437 struct phys_reg dest
= index_to_reg(ctx
, g
, args
.dest
);
439 unsigned mask
= squeeze_writemask(ins
->alu
.mask
);
440 ins
->alu
.mask
= expand_writemask(compose_writemask(mask
, dest
));
442 /* Adjust the dest mask if necessary. Mostly this is a no-op
443 * but it matters for dot products */
444 dest
.mask
= effective_writemask(&ins
->alu
);
446 midgard_vector_alu_src mod1
=
447 vector_alu_from_unsigned(ins
->alu
.src1
);
448 mod1
.swizzle
= compose_swizzle(mod1
.swizzle
, mask
, src1
, dest
);
449 ins
->alu
.src1
= vector_alu_srco_unsigned(mod1
);
451 ins
->registers
.src1_reg
= src1
.reg
;
453 ins
->registers
.src2_imm
= args
.inline_constant
;
455 if (args
.inline_constant
) {
456 /* Encode inline 16-bit constant. See disassembler for
457 * where the algorithm is from */
459 ins
->registers
.src2_reg
= ins
->inline_constant
>> 11;
461 int lower_11
= ins
->inline_constant
& ((1 << 12) - 1);
462 uint16_t imm
= ((lower_11
>> 8) & 0x7) |
463 ((lower_11
& 0xFF) << 3);
465 ins
->alu
.src2
= imm
<< 2;
467 midgard_vector_alu_src mod2
=
468 vector_alu_from_unsigned(ins
->alu
.src2
);
469 mod2
.swizzle
= compose_swizzle(
470 mod2
.swizzle
, mask
, src2
, dest
);
471 ins
->alu
.src2
= vector_alu_srco_unsigned(mod2
);
473 ins
->registers
.src2_reg
= src2
.reg
;
476 ins
->registers
.out_reg
= dest
.reg
;
480 case TAG_LOAD_STORE_4
: {
481 if (OP_IS_STORE(ins
->load_store
.op
)) {
482 /* TODO: use ssa_args for st_vary */
483 ins
->load_store
.reg
= 0;
485 struct phys_reg src
= index_to_reg(ctx
, g
, args
.dest
);
487 ins
->load_store
.reg
= src
.reg
;
489 ins
->load_store
.swizzle
= compose_swizzle(
490 ins
->load_store
.swizzle
, 0xF,
491 default_phys_reg(0), src
);
493 ins
->load_store
.mask
= compose_writemask(
494 ins
->load_store
.mask
, src
);
506 install_registers(compiler_context
*ctx
, struct ra_graph
*g
)
508 mir_foreach_block(ctx
, block
) {
509 mir_foreach_instr_in_block(block
, ins
) {
510 if (ins
->compact_branch
) continue;
511 install_registers_instr(ctx
, g
, ins
);