* IN THE SOFTWARE.
*/
-#include <inttypes.h>
-
+#include "util/ralloc.h"
+#include "util/register_allocate.h"
#include "vc4_context.h"
#include "vc4_qir.h"
#include "vc4_qpu.h"
{ QPU_MUX_R3, 0},
{ QPU_MUX_R4, 0},
QPU_R(A, 0),
- QPU_R(A, 1),
- QPU_R(A, 2),
- QPU_R(A, 3),
- QPU_R(A, 4),
- QPU_R(A, 5),
- QPU_R(A, 6),
- QPU_R(A, 7),
- QPU_R(A, 8),
- QPU_R(A, 9),
- QPU_R(A, 10),
- QPU_R(A, 11),
- QPU_R(A, 12),
- QPU_R(A, 13),
- QPU_R(A, 14),
- QPU_R(A, 15),
- QPU_R(A, 16),
- QPU_R(A, 17),
- QPU_R(A, 18),
- QPU_R(A, 19),
- QPU_R(A, 20),
- QPU_R(A, 21),
- QPU_R(A, 22),
- QPU_R(A, 23),
- QPU_R(A, 24),
- QPU_R(A, 25),
- QPU_R(A, 26),
- QPU_R(A, 27),
- QPU_R(A, 28),
- QPU_R(A, 29),
- QPU_R(A, 30),
- QPU_R(A, 31),
QPU_R(B, 0),
+ QPU_R(A, 1),
QPU_R(B, 1),
+ QPU_R(A, 2),
QPU_R(B, 2),
+ QPU_R(A, 3),
QPU_R(B, 3),
+ QPU_R(A, 4),
QPU_R(B, 4),
+ QPU_R(A, 5),
QPU_R(B, 5),
+ QPU_R(A, 6),
QPU_R(B, 6),
+ QPU_R(A, 7),
QPU_R(B, 7),
+ QPU_R(A, 8),
QPU_R(B, 8),
+ QPU_R(A, 9),
QPU_R(B, 9),
+ QPU_R(A, 10),
QPU_R(B, 10),
+ QPU_R(A, 11),
QPU_R(B, 11),
+ QPU_R(A, 12),
QPU_R(B, 12),
+ QPU_R(A, 13),
QPU_R(B, 13),
+ QPU_R(A, 14),
QPU_R(B, 14),
+ QPU_R(A, 15),
QPU_R(B, 15),
+ QPU_R(A, 16),
QPU_R(B, 16),
+ QPU_R(A, 17),
QPU_R(B, 17),
+ QPU_R(A, 18),
QPU_R(B, 18),
+ QPU_R(A, 19),
QPU_R(B, 19),
+ QPU_R(A, 20),
QPU_R(B, 20),
+ QPU_R(A, 21),
QPU_R(B, 21),
+ QPU_R(A, 22),
QPU_R(B, 22),
+ QPU_R(A, 23),
QPU_R(B, 23),
+ QPU_R(A, 24),
QPU_R(B, 24),
+ QPU_R(A, 25),
QPU_R(B, 25),
+ QPU_R(A, 26),
QPU_R(B, 26),
+ QPU_R(A, 27),
QPU_R(B, 27),
+ QPU_R(A, 28),
QPU_R(B, 28),
+ QPU_R(A, 29),
QPU_R(B, 29),
+ QPU_R(A, 30),
QPU_R(B, 30),
+ QPU_R(A, 31),
QPU_R(B, 31),
};
#define ACC_INDEX 0
-#define A_INDEX (ACC_INDEX + 5)
-#define B_INDEX (A_INDEX + 32)
+#define AB_INDEX (ACC_INDEX + 5)
+
+static void
+vc4_alloc_reg_set(struct vc4_context *vc4)
+{
+ assert(vc4_regs[AB_INDEX].addr == 0);
+ assert(vc4_regs[AB_INDEX + 1].addr == 0);
+ STATIC_ASSERT(ARRAY_SIZE(vc4_regs) == AB_INDEX + 64);
+
+ if (vc4->regs)
+ return;
+
+ vc4->regs = ra_alloc_reg_set(vc4, ARRAY_SIZE(vc4_regs));
+
+ vc4->reg_class_any = ra_alloc_reg_class(vc4->regs);
+ for (uint32_t i = 0; i < ARRAY_SIZE(vc4_regs); i++) {
+ /* Reserve ra31/rb31 for spilling fixup_raddr_conflict() in
+ * vc4_qpu_emit.c
+ */
+ if (vc4_regs[i].addr == 31)
+ continue;
+
+ /* R4 can't be written as a general purpose register. (it's
+ * TMU_NOSWAP as a write address).
+ */
+ if (vc4_regs[i].mux == QPU_MUX_R4)
+ continue;
+
+ ra_class_add_reg(vc4->regs, vc4->reg_class_any, i);
+ }
+
+ vc4->reg_class_a = ra_alloc_reg_class(vc4->regs);
+ for (uint32_t i = AB_INDEX; i < AB_INDEX + 64; i += 2)
+ ra_class_add_reg(vc4->regs, vc4->reg_class_a, i);
+
+ ra_set_finalize(vc4->regs, NULL);
+}
+
+struct node_to_temp_map {
+ uint32_t temp;
+ uint32_t priority;
+};
+
+static int
+node_to_temp_priority(const void *in_a, const void *in_b)
+{
+ const struct node_to_temp_map *a = in_a;
+ const struct node_to_temp_map *b = in_b;
+
+ return a->priority - b->priority;
+}
/**
* Returns a mapping from QFILE_TEMP indices to struct qpu_regs.
* The return value should be freed by the caller.
*/
struct qpu_reg *
-vc4_register_allocate(struct vc4_compile *c)
+vc4_register_allocate(struct vc4_context *vc4, struct vc4_compile *c)
{
- struct simple_node *node;
- bool reg_in_use[ARRAY_SIZE(vc4_regs)];
- int *reg_allocated = calloc(c->num_temps, sizeof(*reg_allocated));
- int *reg_uses_remaining =
- calloc(c->num_temps, sizeof(*reg_uses_remaining));
+ struct node_to_temp_map map[c->num_temps];
+ uint32_t temp_to_node[c->num_temps];
+ uint32_t def[c->num_temps];
+ uint32_t use[c->num_temps];
struct qpu_reg *temp_registers = calloc(c->num_temps,
sizeof(*temp_registers));
-
- for (int i = 0; i < ARRAY_SIZE(reg_in_use); i++)
- reg_in_use[i] = false;
- for (int i = 0; i < c->num_temps; i++)
- reg_allocated[i] = -1;
+ memset(def, 0, sizeof(def));
+ memset(use, 0, sizeof(use));
/* If things aren't ever written (undefined values), just read from
* r0.
*/
- for (int i = 0; i < c->num_temps; i++)
+ for (uint32_t i = 0; i < c->num_temps; i++)
temp_registers[i] = qpu_rn(0);
- /* Reserve r3 for spilling-like operations in vc4_qpu_emit.c */
- reg_in_use[ACC_INDEX + 3] = true;
+ vc4_alloc_reg_set(vc4);
- foreach(node, &c->instructions) {
- struct qinst *qinst = (struct qinst *)node;
+ struct ra_graph *g = ra_alloc_interference_graph(vc4->regs,
+ c->num_temps);
- if (qinst->dst.file == QFILE_TEMP)
- reg_uses_remaining[qinst->dst.index]++;
- for (int i = 0; i < qir_get_op_nsrc(qinst->op); i++) {
- if (qinst->src[i].file == QFILE_TEMP)
- reg_uses_remaining[qinst->src[i].index]++;
+ for (uint32_t i = 0; i < c->num_temps; i++) {
+ ra_set_node_class(g, i, vc4->reg_class_any);
+ }
+
+ /* Compute the live ranges so we can figure out interference.
+ */
+ uint32_t ip = 0;
+ list_for_each_entry(struct qinst, inst, &c->instructions, link) {
+ if (inst->dst.file == QFILE_TEMP) {
+ def[inst->dst.index] = ip;
+ use[inst->dst.index] = ip;
}
- if (qinst->op == QOP_FRAG_Z)
- reg_in_use[3 + 32 + QPU_R_FRAG_PAYLOAD_ZW] = true;
- if (qinst->op == QOP_FRAG_W)
- reg_in_use[3 + QPU_R_FRAG_PAYLOAD_ZW] = true;
+
+ for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
+ if (inst->src[i].file == QFILE_TEMP)
+ use[inst->src[i].index] = ip;
+ }
+
+ switch (inst->op) {
+ case QOP_FRAG_Z:
+ case QOP_FRAG_W:
+ /* The payload registers have values implicitly loaded
+ * at the start of the program.
+ */
+ def[inst->dst.index] = 0;
+ break;
+ default:
+ break;
+ }
+
+ ip++;
}
- foreach(node, &c->instructions) {
- struct qinst *qinst = (struct qinst *)node;
+ for (uint32_t i = 0; i < c->num_temps; i++) {
+ map[i].temp = i;
+ map[i].priority = use[i] - def[i];
+ }
+ qsort(map, c->num_temps, sizeof(map[0]), node_to_temp_priority);
+ for (uint32_t i = 0; i < c->num_temps; i++) {
+ temp_to_node[map[i].temp] = i;
+ }
- for (int i = 0; i < qir_get_op_nsrc(qinst->op); i++) {
- int index = qinst->src[i].index;
+ /* Figure out our register classes and preallocated registers*/
+ list_for_each_entry(struct qinst, inst, &c->instructions, link) {
+ switch (inst->op) {
+ case QOP_FRAG_Z:
+ ra_set_node_reg(g, temp_to_node[inst->dst.index],
+ AB_INDEX + QPU_R_FRAG_PAYLOAD_ZW * 2 + 1);
+ break;
- if (qinst->src[i].file != QFILE_TEMP)
- continue;
+ case QOP_FRAG_W:
+ ra_set_node_reg(g, temp_to_node[inst->dst.index],
+ AB_INDEX + QPU_R_FRAG_PAYLOAD_ZW * 2);
+ break;
- if (reg_allocated[index] == -1) {
- fprintf(stderr, "undefined reg use: ");
- qir_dump_inst(qinst);
- fprintf(stderr, "\n");
- } else {
- reg_uses_remaining[index]--;
- if (reg_uses_remaining[index] == 0)
- reg_in_use[reg_allocated[index]] = false;
- }
+ case QOP_TEX_RESULT:
+ case QOP_TLB_COLOR_READ:
+ assert(vc4_regs[ACC_INDEX + 4].mux == QPU_MUX_R4);
+ ra_set_node_reg(g, temp_to_node[inst->dst.index],
+ ACC_INDEX + 4);
+ break;
+
+ case QOP_PACK_SCALED:
+ /* The pack flags require an A-file dst register. */
+ ra_set_node_class(g, temp_to_node[inst->dst.index],
+ vc4->reg_class_a);
+ break;
+
+ default:
+ break;
}
- if (qinst->dst.file == QFILE_TEMP) {
- if (reg_allocated[qinst->dst.index] == -1) {
- int alloc;
- for (alloc = 0;
- alloc < ARRAY_SIZE(reg_in_use);
- alloc++) {
- struct qpu_reg reg = vc4_regs[alloc];
-
- switch (qinst->op) {
- case QOP_PACK_SCALED:
- /* The pack flags require an
- * A-file register.
- */
- if (reg.mux != QPU_MUX_A)
- continue;
- break;
- case QOP_TEX_RESULT:
- case QOP_TLB_COLOR_READ:
- /* Only R4-generating
- * instructions get to store
- * values in R4 for now, until
- * we figure out how to do
- * interference.
- */
- if (reg.mux != QPU_MUX_R4)
- continue;
- break;
- case QOP_FRAG_Z:
- if (reg.mux != QPU_MUX_B ||
- reg.addr != QPU_R_FRAG_PAYLOAD_ZW) {
- continue;
- }
- break;
- case QOP_FRAG_W:
- if (reg.mux != QPU_MUX_A ||
- reg.addr != QPU_R_FRAG_PAYLOAD_ZW) {
- continue;
- }
- break;
- default:
- if (reg.mux == QPU_MUX_R4)
- continue;
- break;
- }
-
- if (!reg_in_use[alloc])
- break;
- }
- assert(alloc != ARRAY_SIZE(reg_in_use) && "need better reg alloc");
- reg_in_use[alloc] = true;
- reg_allocated[qinst->dst.index] = alloc;
- temp_registers[qinst->dst.index] = vc4_regs[alloc];
- }
+ if (qir_src_needs_a_file(inst)) {
+ ra_set_node_class(g, temp_to_node[inst->src[0].index],
+ vc4->reg_class_a);
+ }
+ }
- reg_uses_remaining[qinst->dst.index]--;
- if (reg_uses_remaining[qinst->dst.index] == 0) {
- reg_in_use[reg_allocated[qinst->dst.index]] =
- false;
+ for (uint32_t i = 0; i < c->num_temps; i++) {
+ for (uint32_t j = i + 1; j < c->num_temps; j++) {
+ if (!(def[i] >= use[j] || def[j] >= use[i])) {
+ ra_add_node_interference(g,
+ temp_to_node[i],
+ temp_to_node[j]);
}
}
}
- free(reg_allocated);
- free(reg_uses_remaining);
+ bool ok = ra_allocate(g);
+ if (!ok) {
+ fprintf(stderr, "Failed to register allocate:\n");
+ qir_dump(c);
+ abort();
+ }
+
+ for (uint32_t i = 0; i < c->num_temps; i++) {
+ temp_registers[i] = vc4_regs[ra_get_node_reg(g, temp_to_node[i])];
+
+ /* If the value's never used, just write to the NOP register
+ * for clarity in debug output.
+ */
+ if (def[i] == use[i])
+ temp_registers[i] = qpu_ra(QPU_W_NOP);
+ }
+
+ ralloc_free(g);
return temp_registers;
}