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
, struct phys_reg reg
, struct phys_reg dst
)
95 for (unsigned c
= 0; c
< 4; ++c
) {
96 unsigned s
= (swizzle
>> (2*c
)) & 0x3;
97 unsigned q
= (reg
.swizzle
>> (2*s
)) & 0x3;
102 /* Based on the register mask, we need to adjust over. E.g if we're
103 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
104 * original first component (x). But to prevent duplicate shifting
105 * (only applies to ALU -- mask param is set to xyzw out on L/S to
106 * prevent changes), we have to account for the shift inherent to the
107 * original writemask */
109 unsigned rep
= out
& 0x3;
110 unsigned shift
= __builtin_ctz(dst
.mask
) - __builtin_ctz(mask
);
111 unsigned shifted
= out
<< (2*shift
);
113 /* ..but we fill in the gaps so it appears to replicate */
115 for (unsigned s
= 0; s
< shift
; ++s
)
116 shifted
|= rep
<< (2*s
);
121 /* When we're 'squeezing down' the values in the IR, we maintain a hash
125 find_or_allocate_temp(compiler_context
*ctx
, unsigned hash
)
127 if ((hash
< 0) || (hash
>= SSA_FIXED_MINIMUM
))
130 unsigned temp
= (uintptr_t) _mesa_hash_table_u64_search(ctx
->hash_to_temp
, hash
+ 1);
135 /* If no temp is find, allocate one */
136 temp
= ctx
->temp_count
++;
137 ctx
->max_hash
= MAX2(ctx
->max_hash
, hash
);
139 _mesa_hash_table_u64_insert(ctx
->hash_to_temp
, hash
+ 1, (void *) ((uintptr_t) temp
+ 1));
144 /* Callback for register allocation selection, trivial default for now */
147 midgard_ra_select_callback(struct ra_graph
*g
, BITSET_WORD
*regs
, void *data
)
149 /* Choose the first available register to minimise reported register pressure */
151 for (int i
= 0; i
< (16 * WORK_STRIDE
); ++i
) {
152 if (BITSET_TEST(regs
, i
)) {
161 /* Helper to return the default phys_reg for a given register */
163 static struct phys_reg
164 default_phys_reg(int reg
)
166 struct phys_reg r
= {
168 .mask
= 0xF, /* xyzw */
169 .swizzle
= 0xE4 /* xyzw */
175 /* Determine which physical register, swizzle, and mask a virtual
176 * register corresponds to */
178 static struct phys_reg
179 index_to_reg(compiler_context
*ctx
, struct ra_graph
*g
, int reg
)
181 /* Check for special cases */
182 if (reg
>= SSA_FIXED_MINIMUM
)
183 return default_phys_reg(SSA_REG_FROM_FIXED(reg
));
184 else if ((reg
< 0) || !g
)
185 return default_phys_reg(REGISTER_UNUSED
);
187 /* Special cases aside, we pick the underlying register */
188 int virt
= ra_get_node_reg(g
, reg
);
190 /* Divide out the register and classification */
191 int phys
= virt
/ WORK_STRIDE
;
192 int type
= virt
% WORK_STRIDE
;
194 struct phys_reg r
= {
196 .mask
= reg_type_to_mask
[type
],
197 .swizzle
= reg_type_to_swizzle
[type
]
200 /* Report that we actually use this register, and return it */
201 ctx
->work_registers
= MAX2(ctx
->work_registers
, phys
);
205 /* This routine performs the actual register allocation. It should be succeeded
206 * by install_registers */
209 allocate_registers(compiler_context
*ctx
)
211 /* The number of vec4 work registers available depends on when the
212 * uniforms start, so compute that first */
214 int work_count
= 16 - MAX2((ctx
->uniform_cutoff
- 8), 0);
216 int virtual_count
= work_count
* WORK_STRIDE
;
218 /* First, initialize the RA */
219 struct ra_regs
*regs
= ra_alloc_reg_set(NULL
, virtual_count
, true);
221 int work_vec4
= ra_alloc_reg_class(regs
);
222 int work_vec3
= ra_alloc_reg_class(regs
);
223 int work_vec2
= ra_alloc_reg_class(regs
);
224 int work_vec1
= ra_alloc_reg_class(regs
);
226 unsigned classes
[4] = {
233 /* Add the full set of work registers */
234 for (int i
= 0; i
< work_count
; ++i
) {
235 int base
= WORK_STRIDE
* i
;
237 /* Build a full set of subdivisions */
238 ra_class_add_reg(regs
, work_vec4
, base
);
239 ra_class_add_reg(regs
, work_vec3
, base
+ 1);
240 ra_class_add_reg(regs
, work_vec3
, base
+ 2);
241 ra_class_add_reg(regs
, work_vec2
, base
+ 3);
242 ra_class_add_reg(regs
, work_vec2
, base
+ 4);
243 ra_class_add_reg(regs
, work_vec2
, base
+ 5);
244 ra_class_add_reg(regs
, work_vec1
, base
+ 6);
245 ra_class_add_reg(regs
, work_vec1
, base
+ 7);
246 ra_class_add_reg(regs
, work_vec1
, base
+ 8);
247 ra_class_add_reg(regs
, work_vec1
, base
+ 9);
249 for (unsigned i
= 0; i
< 10; ++i
) {
250 for (unsigned j
= 0; j
< 10; ++j
) {
251 unsigned mask1
= reg_type_to_mask
[i
];
252 unsigned mask2
= reg_type_to_mask
[j
];
255 ra_add_reg_conflict(regs
, base
+ i
, base
+ j
);
260 /* We're done setting up */
261 ra_set_finalize(regs
, NULL
);
263 /* Transform the MIR into squeezed index form */
264 mir_foreach_block(ctx
, block
) {
265 mir_foreach_instr_in_block(block
, ins
) {
266 if (ins
->compact_branch
) continue;
268 ins
->ssa_args
.dest
= find_or_allocate_temp(ctx
, ins
->ssa_args
.dest
);
269 ins
->ssa_args
.src0
= find_or_allocate_temp(ctx
, ins
->ssa_args
.src0
);
271 if (!ins
->ssa_args
.inline_constant
)
272 ins
->ssa_args
.src1
= find_or_allocate_temp(ctx
, ins
->ssa_args
.src1
);
277 /* No register allocation to do with no SSA */
279 if (!ctx
->temp_count
)
282 /* Let's actually do register allocation */
283 int nodes
= ctx
->temp_count
;
284 struct ra_graph
*g
= ra_alloc_interference_graph(regs
, nodes
);
286 /* Determine minimum size needed to hold values, to indirectly
289 unsigned *found_class
= calloc(sizeof(unsigned), ctx
->temp_count
);
291 mir_foreach_block(ctx
, block
) {
292 mir_foreach_instr_in_block(block
, ins
) {
293 if (ins
->compact_branch
) continue;
294 if (ins
->ssa_args
.dest
< 0) continue;
295 if (ins
->ssa_args
.dest
>= SSA_FIXED_MINIMUM
) continue;
297 /* Default to vec4 if we're not sure */
301 if (ins
->type
== TAG_ALU_4
)
302 mask
= squeeze_writemask(ins
->alu
.mask
);
303 else if (ins
->type
== TAG_LOAD_STORE_4
)
304 mask
= ins
->load_store
.mask
;
306 int class = util_logbase2(mask
) + 1;
308 /* Use the largest class if there's ambiguity, this
309 * handles partial writes */
311 int dest
= ins
->ssa_args
.dest
;
312 found_class
[dest
] = MAX2(found_class
[dest
], class);
316 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
317 unsigned class = found_class
[i
];
318 if (!class) continue;
319 ra_set_node_class(g
, i
, classes
[class - 1]);
322 /* Determine liveness */
324 int *live_start
= malloc(nodes
* sizeof(int));
325 int *live_end
= malloc(nodes
* sizeof(int));
327 /* Initialize as non-existent */
329 for (int i
= 0; i
< nodes
; ++i
) {
330 live_start
[i
] = live_end
[i
] = -1;
335 mir_foreach_block(ctx
, block
) {
336 mir_foreach_instr_in_block(block
, ins
) {
337 if (ins
->compact_branch
) continue;
339 /* Dest is < 0 for st_vary instructions, which break
340 * the usual SSA conventions. Liveness analysis doesn't
341 * make sense on these instructions, so skip them to
342 * avoid memory corruption */
344 if (ins
->ssa_args
.dest
< 0) continue;
346 if (ins
->ssa_args
.dest
< SSA_FIXED_MINIMUM
) {
347 /* If this destination is not yet live, it is now since we just wrote it */
349 int dest
= ins
->ssa_args
.dest
;
351 if (live_start
[dest
] == -1)
352 live_start
[dest
] = d
;
355 /* Since we just used a source, the source might be
356 * dead now. Scan the rest of the block for
357 * invocations, and if there are none, the source dies
360 int sources
[2] = { ins
->ssa_args
.src0
, ins
->ssa_args
.src1
};
362 for (int src
= 0; src
< 2; ++src
) {
363 int s
= sources
[src
];
367 if (s
>= SSA_FIXED_MINIMUM
) continue;
369 if (!mir_is_live_after(ctx
, block
, ins
, s
)) {
378 /* If a node still hasn't been killed, kill it now */
380 for (int i
= 0; i
< nodes
; ++i
) {
381 /* live_start == -1 most likely indicates a pinned output */
383 if (live_end
[i
] == -1)
387 /* Setup interference between nodes that are live at the same time */
389 for (int i
= 0; i
< nodes
; ++i
) {
390 for (int j
= i
+ 1; j
< nodes
; ++j
) {
391 if (!(live_start
[i
] >= live_end
[j
] || live_start
[j
] >= live_end
[i
]))
392 ra_add_node_interference(g
, i
, j
);
396 ra_set_select_reg_callback(g
, midgard_ra_select_callback
, NULL
);
398 if (!ra_allocate(g
)) {
399 unreachable("Error allocating registers\n");
409 /* Once registers have been decided via register allocation
410 * (allocate_registers), we need to rewrite the MIR to use registers instead of
414 install_registers_instr(
415 compiler_context
*ctx
,
417 midgard_instruction
*ins
)
419 ssa_args args
= ins
->ssa_args
;
423 int adjusted_src
= args
.inline_constant
? -1 : args
.src1
;
424 struct phys_reg src1
= index_to_reg(ctx
, g
, args
.src0
);
425 struct phys_reg src2
= index_to_reg(ctx
, g
, adjusted_src
);
426 struct phys_reg dest
= index_to_reg(ctx
, g
, args
.dest
);
428 unsigned mask
= squeeze_writemask(ins
->alu
.mask
);
429 ins
->alu
.mask
= expand_writemask(compose_writemask(mask
, dest
));
431 /* Adjust the dest mask if necessary. Mostly this is a no-op
432 * but it matters for dot products */
433 dest
.mask
= effective_writemask(&ins
->alu
);
435 midgard_vector_alu_src mod1
=
436 vector_alu_from_unsigned(ins
->alu
.src1
);
437 mod1
.swizzle
= compose_swizzle(mod1
.swizzle
, mask
, src1
, dest
);
438 ins
->alu
.src1
= vector_alu_srco_unsigned(mod1
);
440 ins
->registers
.src1_reg
= src1
.reg
;
442 ins
->registers
.src2_imm
= args
.inline_constant
;
444 if (args
.inline_constant
) {
445 /* Encode inline 16-bit constant as a vector by default */
447 ins
->registers
.src2_reg
= ins
->inline_constant
>> 11;
449 int lower_11
= ins
->inline_constant
& ((1 << 12) - 1);
451 uint16_t imm
= ((lower_11
>> 8) & 0x7) | ((lower_11
& 0xFF) << 3);
452 ins
->alu
.src2
= imm
<< 2;
454 midgard_vector_alu_src mod2
=
455 vector_alu_from_unsigned(ins
->alu
.src2
);
456 mod2
.swizzle
= compose_swizzle(mod2
.swizzle
, mask
, src2
, dest
);
457 ins
->alu
.src2
= vector_alu_srco_unsigned(mod2
);
459 ins
->registers
.src2_reg
= src2
.reg
;
462 ins
->registers
.out_reg
= dest
.reg
;
466 case TAG_LOAD_STORE_4
: {
467 if (OP_IS_STORE(ins
->load_store
.op
)) {
468 /* TODO: use ssa_args for st_vary */
469 ins
->load_store
.reg
= 0;
471 struct phys_reg src
= index_to_reg(ctx
, g
, args
.dest
);
473 ins
->load_store
.reg
= src
.reg
;
475 ins
->load_store
.swizzle
= compose_swizzle(
476 ins
->load_store
.swizzle
, 0xF,
477 default_phys_reg(0), src
);
479 ins
->load_store
.mask
= compose_writemask(
480 ins
->load_store
.mask
, src
);
492 install_registers(compiler_context
*ctx
, struct ra_graph
*g
)
494 mir_foreach_block(ctx
, block
) {
495 mir_foreach_instr_in_block(block
, ins
) {
496 if (ins
->compact_branch
) continue;
497 install_registers_instr(ctx
, g
, ins
);