2018-12-28 15:48:37 -08:00
|
|
|
package pass
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-06 20:14:02 +00:00
|
|
|
"sources.truenas.cloud/code/avo/reg"
|
2018-12-28 15:48:37 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAllocatorSimple(t *testing.T) {
|
|
|
|
|
c := reg.NewCollection()
|
2019-01-04 18:23:44 -08:00
|
|
|
x, y := c.XMM(), c.YMM()
|
2018-12-28 15:48:37 -08:00
|
|
|
|
2018-12-30 18:40:45 -08:00
|
|
|
a, err := NewAllocatorForKind(reg.KindVector)
|
2018-12-28 15:48:37 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 22:50:40 -08:00
|
|
|
a.Add(x.ID())
|
|
|
|
|
a.Add(y.ID())
|
|
|
|
|
a.AddInterference(x.ID(), y.ID())
|
2018-12-28 15:48:37 -08:00
|
|
|
|
|
|
|
|
alloc, err := a.Allocate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Log(alloc)
|
|
|
|
|
|
2020-01-22 22:50:40 -08:00
|
|
|
if alloc.LookupRegister(x) != reg.X0 || alloc.LookupRegister(y) != reg.Y1 {
|
2018-12-28 15:48:37 -08:00
|
|
|
t.Fatalf("unexpected allocation")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAllocatorImpossible(t *testing.T) {
|
2018-12-30 18:40:45 -08:00
|
|
|
a, err := NewAllocatorForKind(reg.KindVector)
|
2018-12-28 15:48:37 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 22:50:40 -08:00
|
|
|
a.AddInterference(reg.X7.ID(), reg.Z7.ID())
|
2018-12-28 15:48:37 -08:00
|
|
|
|
|
|
|
|
_, err = a.Allocate()
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("expected allocation error")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-11 23:32:52 -07:00
|
|
|
|
|
|
|
|
func TestAllocatorPriority(t *testing.T) {
|
|
|
|
|
const n = 4
|
|
|
|
|
|
|
|
|
|
// Create an allocator with custom priorities.
|
|
|
|
|
a, err := NewAllocatorForKind(reg.KindVector)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.SetPriority(reg.X0.ID(), -1)
|
|
|
|
|
a.SetPriority(reg.X7.ID(), 1)
|
|
|
|
|
a.SetPriority(reg.X13.ID(), 1)
|
|
|
|
|
a.SetPriority(reg.X3.ID(), 2)
|
|
|
|
|
|
|
|
|
|
// The expected n highest priority registers.
|
|
|
|
|
expect := [n]reg.Physical{
|
|
|
|
|
reg.X3, // priority 2, id 3
|
|
|
|
|
reg.X7, // priority 1, id 7
|
|
|
|
|
reg.X13, // priority 1, id 13
|
|
|
|
|
reg.X1, // priority 0, id 1 (X0 has priority -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup allocation problem with n conflicting registers.
|
|
|
|
|
c := reg.NewCollection()
|
|
|
|
|
x := make([]reg.Virtual, n)
|
|
|
|
|
for i := range x {
|
|
|
|
|
x[i] = c.XMM()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range x {
|
|
|
|
|
a.Add(x[i].ID())
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 19:01:48 -05:00
|
|
|
for i := range n {
|
2021-04-11 23:32:52 -07:00
|
|
|
for j := i + 1; j < n; j++ {
|
|
|
|
|
a.AddInterference(x[i].ID(), x[j].ID())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate and confirm expectation.
|
|
|
|
|
alloc, err := a.Allocate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range x {
|
|
|
|
|
if got := alloc.LookupRegister(x[i]); got != expect[i] {
|
|
|
|
|
t.Errorf("x[%d] allocated %s; expected %s", i, got.Asm(), expect[i].Asm())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|