From a1440c51a4cd7f09e7e31d6fef8e977c58b41786 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Fri, 28 Dec 2018 15:48:37 -0800 Subject: [PATCH] pass: some basic allocator test cases --- pass/alloc.go | 6 +++++- pass/alloc_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pass/alloc_test.go diff --git a/pass/alloc.go b/pass/alloc.go index ce07890..dd616f3 100644 --- a/pass/alloc.go +++ b/pass/alloc.go @@ -64,11 +64,15 @@ func (a *Allocator) Add(r reg.Register) { } func (a *Allocator) Allocate() (reg.Allocation, error) { - for a.remaining() > 0 { + for { if err := a.update(); err != nil { return nil, err } + if a.remaining() == 0 { + break + } + v := a.mostrestricted() if err := a.alloc(v); err != nil { return nil, err diff --git a/pass/alloc_test.go b/pass/alloc_test.go new file mode 100644 index 0000000..e9b246e --- /dev/null +++ b/pass/alloc_test.go @@ -0,0 +1,46 @@ +package pass + +import ( + "testing" + + "github.com/mmcloughlin/avo/reg" +) + +func TestAllocatorSimple(t *testing.T) { + c := reg.NewCollection() + x, y := c.Xv(), c.Yv() + + a, err := NewAllocatorForKind(reg.SSEAVX) + if err != nil { + t.Fatal(err) + } + + a.Add(x) + a.Add(y) + a.AddInterference(x, y) + + alloc, err := a.Allocate() + if err != nil { + t.Fatal(err) + } + + t.Log(alloc) + + if alloc[x] != reg.X0 || alloc[y] != reg.Y1 { + t.Fatalf("unexpected allocation") + } +} + +func TestAllocatorImpossible(t *testing.T) { + a, err := NewAllocatorForKind(reg.SSEAVX) + if err != nil { + t.Fatal(err) + } + + a.AddInterference(reg.X7, reg.Z7) + + _, err = a.Allocate() + if err == nil { + t.Fatal("expected allocation error") + } +}