2011-09-18 05:02:34 +03:00
|
|
|
/*
|
|
|
|
* sched.c - O(n) ... O(n^2) scheduler
|
|
|
|
*
|
|
|
|
* Written 2011 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* Based on gfpus.c
|
|
|
|
* Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <fpvm/is.h>
|
|
|
|
#include <fpvm/fpvm.h>
|
|
|
|
#include <fpvm/pfpu.h>
|
|
|
|
#include <fpvm/gfpus.h>
|
|
|
|
|
|
|
|
#include <hw/pfpu.h>
|
|
|
|
|
|
|
|
|
2011-09-18 07:28:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define Dprintf printf
|
|
|
|
#else
|
2011-09-18 06:51:17 +03:00
|
|
|
#define Dprintf(...)
|
2011-09-18 07:28:02 +03:00
|
|
|
#endif
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
#define MAX_LATENCY 8 /* maximum latency; okay to make this bigger */
|
|
|
|
|
|
|
|
#define FIELD(w) (((pfpu_instruction *) &(w))->i)
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
struct list {
|
|
|
|
struct list *next, *prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct insn {
|
|
|
|
struct list more; /* more insns on same schedule */
|
|
|
|
struct fpvm_instruction *vm_insn;
|
|
|
|
struct data_ref {
|
|
|
|
struct list more; /* more refs sharing the data */
|
|
|
|
struct insn *insn; /* insn this is part of */
|
|
|
|
struct insn *dep; /* insn we depend on */
|
|
|
|
} opa, opb, dest, cond;
|
|
|
|
int arity;
|
|
|
|
int latency;
|
|
|
|
int unresolved; /* number of data refs we need before we can sched */
|
|
|
|
int earliest; /* earliest cycle dependencies seen so far are met */
|
|
|
|
struct list dependants; /* list of dependencies (constant) */
|
|
|
|
int num_dependants; /* number of unresolved dependencies */
|
|
|
|
} insns[FPVM_MAXCODELEN];
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Register initialization ------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Straight from gfpus.c, only with some whitespace changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void get_registers(struct fpvm_fragment *fragment,
|
|
|
|
unsigned int *registers)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
unsigned int n;
|
|
|
|
} fconv;
|
|
|
|
|
|
|
|
for(i = 0; i < fragment->nbindings; i++)
|
|
|
|
if (fragment->bindings[i].isvar)
|
|
|
|
registers[i] = 0;
|
|
|
|
else {
|
|
|
|
fconv.f = fragment->bindings[i].b.c;
|
|
|
|
registers[i] = fconv.n;
|
|
|
|
}
|
|
|
|
for(; i < PFPU_REG_COUNT; i++)
|
|
|
|
registers[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Doubly-linked list ------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-09-18 06:51:17 +03:00
|
|
|
* Use the naming conventions of include/linux/list.h
|
2011-09-18 05:02:34 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void list_init(struct list *list)
|
|
|
|
{
|
|
|
|
list->next = list->prev = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void list_del(struct list *item)
|
|
|
|
{
|
|
|
|
item->prev->next = item->next;
|
|
|
|
item->next->prev = item->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *list_pop(struct list *list)
|
|
|
|
{
|
|
|
|
struct list *first;
|
|
|
|
|
|
|
|
first = list->next;
|
|
|
|
if (first == list)
|
|
|
|
return NULL;
|
|
|
|
list_del(first);
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void list_add_tail(struct list *list, struct list *item)
|
|
|
|
{
|
|
|
|
item->next = list;
|
|
|
|
item->prev = list->prev;
|
|
|
|
list->prev->next = item;
|
|
|
|
list->prev = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void list_add(struct list *list, struct list *item)
|
|
|
|
{
|
|
|
|
item->next = list->next;
|
|
|
|
item->prev = list;
|
|
|
|
list->next->prev = item;
|
|
|
|
list->next = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void list_concat(struct list *a, struct list *b)
|
|
|
|
{
|
|
|
|
if (b->next != b) {
|
|
|
|
a->prev->next = b->next;
|
|
|
|
b->next->prev = a->prev;
|
|
|
|
b->prev->next = a;
|
|
|
|
a->prev = b->prev;
|
|
|
|
}
|
|
|
|
list_init(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not delete elements from the list while traversing it with foreach !
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define foreach(var, head) \
|
|
|
|
for (var = (void *) ((struct list *) (head))->next; \
|
|
|
|
(var) != (void *) (head); \
|
|
|
|
var = (void *) ((struct list *) (var))->next)
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Register management ----------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static struct vm_reg {
|
|
|
|
struct insn *setter; /* instruction setting it; NULL if none */
|
|
|
|
int pfpu_reg; /* underlying PFPU register */
|
|
|
|
int refs; /* usage count */
|
|
|
|
} *regs;
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
static struct pfpu_reg {
|
|
|
|
struct list more; /* list of unallocated PFPU registers */
|
|
|
|
int vm_reg; /* corresponding FPVM register if allocated */
|
|
|
|
int used; /* used somewhere in the program */
|
|
|
|
} pfpu_regs[PFPU_REG_COUNT];
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
static struct list unallocated; /* unallocated registers */
|
|
|
|
static int nbindings; /* "public" bindings */
|
|
|
|
|
|
|
|
|
|
|
|
static int reg2idx(int reg)
|
|
|
|
{
|
|
|
|
return reg >= 0 ? reg : nbindings-reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int alloc_reg(struct insn *setter)
|
|
|
|
{
|
2011-09-18 06:51:17 +03:00
|
|
|
struct pfpu_reg *reg;
|
2011-09-18 05:02:34 +03:00
|
|
|
int vm_reg, pfpu_reg, vm_idx;
|
|
|
|
|
|
|
|
vm_reg = setter->vm_insn->dest;
|
|
|
|
if (vm_reg >= 0)
|
|
|
|
return vm_reg;
|
|
|
|
reg = list_pop(&unallocated);
|
|
|
|
if (!reg)
|
|
|
|
abort();
|
2011-09-18 06:51:17 +03:00
|
|
|
reg->vm_reg = vm_reg;
|
2011-09-18 05:02:34 +03:00
|
|
|
pfpu_reg = reg-pfpu_regs;
|
2011-09-18 07:28:02 +03:00
|
|
|
|
|
|
|
Dprintf(" alloc reg %d -> %d\n", vm_reg, pfpu_reg);
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
vm_idx = reg2idx(vm_reg);
|
|
|
|
regs[vm_idx].setter = setter;
|
|
|
|
regs[vm_idx].pfpu_reg = pfpu_reg;
|
|
|
|
regs[vm_idx].refs = setter->num_dependants+1;
|
|
|
|
return pfpu_reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
static void put_reg(int vm_reg)
|
2011-09-18 05:02:34 +03:00
|
|
|
{
|
2011-09-18 06:51:17 +03:00
|
|
|
int vm_idx;
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
if (vm_reg >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vm_idx = reg2idx(vm_reg);
|
|
|
|
if (--regs[vm_idx].refs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dprintf(" free reg %d\n", regs[vm_idx].pfpu_reg);
|
|
|
|
/*
|
|
|
|
* Prepend so that register numbers stay small and bugs reveal
|
|
|
|
* themselves more rapidly.
|
|
|
|
*/
|
2011-09-18 06:51:17 +03:00
|
|
|
list_add(&unallocated, &pfpu_regs[regs[vm_idx].pfpu_reg].more);
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
/* clear it for style only */
|
|
|
|
regs[vm_idx].setter = NULL;
|
|
|
|
regs[vm_idx].pfpu_reg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
static void put_reg_by_setter(struct insn *setter)
|
|
|
|
{
|
|
|
|
if (setter)
|
|
|
|
put_reg(setter->vm_insn->dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
static int lookup_pfpu_reg(int vm_reg)
|
|
|
|
{
|
|
|
|
return vm_reg >= 0 ? vm_reg : regs[reg2idx(vm_reg)].pfpu_reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
static void mark(int vm_reg)
|
|
|
|
{
|
|
|
|
if (vm_reg > 0)
|
|
|
|
pfpu_regs[vm_reg].used = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
static void init_registers(struct fpvm_fragment *fragment,
|
|
|
|
unsigned int *registers)
|
|
|
|
{
|
|
|
|
size_t regs_size;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
get_registers(fragment, registers);
|
|
|
|
nbindings = fragment->nbindings;
|
|
|
|
|
|
|
|
regs_size = sizeof(struct vm_reg)*(nbindings-fragment->next_sur);
|
|
|
|
regs = malloc(regs_size);
|
|
|
|
memset(regs, 0, regs_size);
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
memset(pfpu_regs, 0, sizeof(pfpu_regs));
|
|
|
|
for (i = 0; i != fragment->ninstructions; i++) {
|
|
|
|
mark(fragment->code[i].opa);
|
|
|
|
mark(fragment->code[i].opb);
|
|
|
|
mark(fragment->code[i].dest);
|
|
|
|
}
|
2011-09-18 05:02:34 +03:00
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
list_init(&unallocated);
|
|
|
|
for (i = PFPU_SPREG_COUNT; i != PFPU_REG_COUNT; i++)
|
|
|
|
if (!pfpu_regs[i].used)
|
|
|
|
list_add_tail(&unallocated, &pfpu_regs[i].more);
|
2011-09-18 05:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Instruction scheduler --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static struct list unscheduled; /* unscheduled insns */
|
|
|
|
static struct list waiting; /* insns waiting to be scheduled */
|
|
|
|
static struct list ready[PFPU_PROGSIZE]; /* insns ready at nth cycle */
|
|
|
|
|
|
|
|
|
|
|
|
static struct vm_reg *add_data_ref(struct insn *insn, struct data_ref *ref,
|
|
|
|
int reg_num)
|
|
|
|
{
|
|
|
|
struct vm_reg *reg;
|
|
|
|
|
|
|
|
reg = regs+reg2idx(reg_num);
|
|
|
|
ref->insn = insn;
|
|
|
|
ref->dep = reg->setter;
|
|
|
|
if (ref->dep) {
|
|
|
|
list_add_tail(&ref->dep->dependants, &ref->more);
|
|
|
|
ref->dep->num_dependants++;
|
|
|
|
insn->unresolved++;
|
2011-09-18 07:28:02 +03:00
|
|
|
|
|
|
|
Dprintf("insn %lu: reg %d setter %lu unresolved %d\n",
|
|
|
|
insn-insns, reg_num, reg->setter-insns, insn->unresolved);
|
2011-09-18 05:02:34 +03:00
|
|
|
} else {
|
|
|
|
list_init(&ref->more);
|
|
|
|
}
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void init_scheduler(struct fpvm_fragment *frag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct insn *insn;
|
|
|
|
|
|
|
|
list_init(&unscheduled);
|
|
|
|
list_init(&waiting);
|
|
|
|
for (i = 0; i != PFPU_PROGSIZE; i++)
|
|
|
|
list_init(&ready[i]);
|
|
|
|
|
|
|
|
for (i = 0; i != frag->ninstructions; i++) {
|
|
|
|
insn = insns+i;
|
|
|
|
memset(insn, 0, sizeof(struct insn));
|
|
|
|
insn->vm_insn = frag->code+i;
|
|
|
|
insn->arity = fpvm_get_arity(frag->code[i].opcode);
|
|
|
|
insn->latency = pfpu_get_latency(frag->code[i].opcode);
|
|
|
|
list_init(&insn->dependants);
|
|
|
|
switch (insn->arity) {
|
|
|
|
case 3:
|
|
|
|
add_data_ref(insn, &insn->opb, FPVM_REG_IFB);
|
|
|
|
/* fall through */
|
|
|
|
case 2:
|
|
|
|
add_data_ref(insn, &insn->opb, frag->code[i].opb);
|
|
|
|
/* fall through */
|
|
|
|
case 1:
|
|
|
|
add_data_ref(insn, &insn->opa, frag->code[i].opa);
|
|
|
|
/* fall through */
|
|
|
|
case 0:
|
|
|
|
add_data_ref(insn,
|
|
|
|
&insn->dest, frag->code[i].dest)->setter = insn;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (insn->unresolved)
|
|
|
|
list_add_tail(&unscheduled, &insn->more);
|
|
|
|
else
|
|
|
|
list_add_tail(&ready[0], &insn->more);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
static void issue(struct insn *insn, int cycle, unsigned *code)
|
2011-09-18 05:02:34 +03:00
|
|
|
{
|
|
|
|
struct data_ref *ref;
|
|
|
|
int end;
|
|
|
|
end = cycle+insn->latency;
|
|
|
|
|
2011-09-18 07:28:02 +03:00
|
|
|
Dprintf("cycle %d: insn %lu L %d (A %d B %d)\n", cycle,
|
|
|
|
insn-insns, insn->latency, insn->vm_insn->opa, insn->vm_insn->opb);
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
switch (insn->arity) {
|
|
|
|
case 3:
|
|
|
|
/* fall through */
|
|
|
|
case 2:
|
2011-09-18 06:51:17 +03:00
|
|
|
FIELD(code[cycle]).opb = lookup_pfpu_reg(insn->vm_insn->opb);
|
|
|
|
put_reg_by_setter(insn->opb.dep);
|
2011-09-18 05:02:34 +03:00
|
|
|
/* fall through */
|
|
|
|
case 1:
|
2011-09-18 06:51:17 +03:00
|
|
|
FIELD(code[cycle]).opa = lookup_pfpu_reg(insn->vm_insn->opa);
|
|
|
|
put_reg_by_setter(insn->opa.dep);
|
2011-09-18 05:02:34 +03:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2011-09-18 06:51:17 +03:00
|
|
|
FIELD(code[end]).dest = alloc_reg(insn);
|
|
|
|
FIELD(code[cycle]).opcode = fpvm_to_pfpu(insn->vm_insn->opcode);
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
foreach (ref, &insn->dependants) {
|
|
|
|
if (ref->insn->earliest <= end)
|
|
|
|
ref->insn->earliest = end+1;
|
|
|
|
if (!--ref->insn->unresolved) {
|
2011-09-18 07:28:02 +03:00
|
|
|
Dprintf(" unlocked %lu -> %u\n", ref->insn-insns,
|
|
|
|
ref->insn->earliest);
|
2011-09-18 05:02:34 +03:00
|
|
|
list_del(&ref->insn->more);
|
|
|
|
list_add_tail(ready+ref->insn->earliest,
|
|
|
|
&ref->insn->more);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 07:28:02 +03:00
|
|
|
#ifdef DEBUG
|
2011-09-18 05:02:34 +03:00
|
|
|
static int count(const struct list *list)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
const struct list *p;
|
|
|
|
|
|
|
|
for (p = list->next; p != list; p = p->next)
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
2011-09-18 07:28:02 +03:00
|
|
|
#endif
|
2011-09-18 05:02:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
static int schedule(struct fpvm_fragment *frag, unsigned int *code)
|
|
|
|
{
|
|
|
|
int remaining;
|
|
|
|
int i, last, end;
|
|
|
|
struct insn *insn;
|
|
|
|
|
|
|
|
remaining = frag->ninstructions;
|
|
|
|
for (i = 0; remaining; i++) {
|
|
|
|
if (i == PFPU_PROGSIZE)
|
|
|
|
return -1;
|
2011-09-18 07:28:02 +03:00
|
|
|
|
|
|
|
Dprintf("@%d --- remaining %d, waiting %d + ready %d\n",
|
|
|
|
i, remaining, count(&waiting), count(&ready[i]));
|
|
|
|
|
2011-09-18 05:02:34 +03:00
|
|
|
list_concat(&waiting, &ready[i]);
|
2011-09-18 06:51:17 +03:00
|
|
|
foreach (insn, &waiting) {
|
|
|
|
end = i+insn->latency;
|
|
|
|
if (end >= PFPU_PROGSIZE)
|
|
|
|
return -1;
|
|
|
|
if (!FIELD(code[end]).dest) {
|
|
|
|
issue(insn, i, code);
|
2011-09-18 05:02:34 +03:00
|
|
|
list_del(&insn->more);
|
|
|
|
remaining--;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-18 06:51:17 +03:00
|
|
|
}
|
|
|
|
if (FIELD(code[i]).dest)
|
|
|
|
put_reg(pfpu_regs[FIELD(code[i]).dest].vm_reg);
|
2011-09-18 05:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add NOPs to cover unfinished instructions.
|
|
|
|
*/
|
|
|
|
last = i;
|
|
|
|
end = i+MAX_LATENCY;
|
|
|
|
if (end > PFPU_PROGSIZE)
|
|
|
|
end = PFPU_PROGSIZE;
|
|
|
|
while (i != end) {
|
2011-09-18 06:51:17 +03:00
|
|
|
if (FIELD(code[i]).dest)
|
2011-09-18 05:02:34 +03:00
|
|
|
last = i+1; /* @@@ ? */
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int gfpus_schedule(struct fpvm_fragment *frag, unsigned int *code,
|
|
|
|
unsigned int *reg)
|
|
|
|
{
|
|
|
|
pfpu_instruction vecout;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
init_registers(frag, reg);
|
|
|
|
memset(code, 0, PFPU_PROGSIZE*sizeof(*code));
|
|
|
|
init_scheduler(frag);
|
|
|
|
res = schedule(frag, code);
|
|
|
|
free(regs);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
if (frag->vector_mode)
|
|
|
|
return res;
|
|
|
|
if (res == PFPU_PROGSIZE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vecout.w = 0;
|
|
|
|
vecout.i.opcode = FPVM_OPCODE_VECTOUT;
|
|
|
|
code[res] = vecout.w;
|
|
|
|
|
|
|
|
return res+1;
|
|
|
|
}
|