File: alloc_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (98 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package pass

import (
	"testing"

	"github.com/mmcloughlin/avo/reg"
)

func TestAllocatorSimple(t *testing.T) {
	c := reg.NewCollection()
	x, y := c.XMM(), c.YMM()

	a, err := NewAllocatorForKind(reg.KindVector)
	if err != nil {
		t.Fatal(err)
	}

	a.Add(x.ID())
	a.Add(y.ID())
	a.AddInterference(x.ID(), y.ID())

	alloc, err := a.Allocate()
	if err != nil {
		t.Fatal(err)
	}

	t.Log(alloc)

	if alloc.LookupRegister(x) != reg.X0 || alloc.LookupRegister(y) != reg.Y1 {
		t.Fatalf("unexpected allocation")
	}
}

func TestAllocatorImpossible(t *testing.T) {
	a, err := NewAllocatorForKind(reg.KindVector)
	if err != nil {
		t.Fatal(err)
	}

	a.AddInterference(reg.X7.ID(), reg.Z7.ID())

	_, err = a.Allocate()
	if err == nil {
		t.Fatal("expected allocation error")
	}
}

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())
	}

	for i := 0; i < n; i++ {
		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())
		}
	}
}