Debian Codegraph @

pass/alloc_test.go

raw symbols SCIP
1 package pass
2
3 import (
4 "testing"
5
6 "github.com/mmcloughlin/avo/reg"
7 )
8
9 func TestAllocatorSimple(t *testing.T) {
10 c := reg.NewCollection()
11 x, y := c.XMM(), c.YMM()
12
13 a, err := NewAllocatorForKind(reg.KindVector)
14 if err != nil {
15 t.Fatal(err)
16 }
17
18 a.Add(x.ID())
19 a.Add(y.ID())
20 a.AddInterference(x.ID(), y.ID())
21
22 alloc, err := a.Allocate()
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 t.Log(alloc)
28
29 if alloc.LookupRegister(x) != reg.X0 || alloc.LookupRegister(y) != reg.Y1 {
30 t.Fatalf("unexpected allocation")
31 }
32 }
33
34 func TestAllocatorImpossible(t *testing.T) {
35 a, err := NewAllocatorForKind(reg.KindVector)
36 if err != nil {
37 t.Fatal(err)
38 }
39
40 a.AddInterference(reg.X7.ID(), reg.Z7.ID())
41
42 _, err = a.Allocate()
43 if err == nil {
44 t.Fatal("expected allocation error")
45 }
46 }
47
48 func TestAllocatorPriority(t *testing.T) {
49 const n = 4
50
51 // Create an allocator with custom priorities.
52 a, err := NewAllocatorForKind(reg.KindVector)
53 if err != nil {
54 t.Fatal(err)
55 }
56
57 a.SetPriority(reg.X0.ID(), -1)
58 a.SetPriority(reg.X7.ID(), 1)
59 a.SetPriority(reg.X13.ID(), 1)
60 a.SetPriority(reg.X3.ID(), 2)
61
62 // The expected n highest priority registers.
63 expect := [n]reg.Physical{
64 reg.X3, // priority 2, id 3
65 reg.X7, // priority 1, id 7
66 reg.X13, // priority 1, id 13
67 reg.X1, // priority 0, id 1 (X0 has priority -1)
68 }
69
70 // Setup allocation problem with n conflicting registers.
71 c := reg.NewCollection()
72 x := make([]reg.Virtual, n)
73 for i := range x {
74 x[i] = c.XMM()
75 }
76
77 for i := range x {
78 a.Add(x[i].ID())
79 }
80
81 for i := 0; i < n; i++ {
82 for j := i + 1; j < n; j++ {
83 a.AddInterference(x[i].ID(), x[j].ID())
84 }
85 }
86
87 // Allocate and confirm expectation.
88 alloc, err := a.Allocate()
89 if err != nil {
90 t.Fatal(err)
91 }
92
93 for i := range x {
94 if got := alloc.LookupRegister(x[i]); got != expect[i] {
95 t.Errorf("x[%d] allocated %s; expected %s", i, got.Asm(), expect[i].Asm())
96 }
97 }
98 }