Skip to content

Commit

Permalink
internal/load: use alias slice for determinism
Browse files Browse the repository at this point in the history
Previously aliases were stored in a map which was causing
non-deterministic code generation (see recent build failures). This diff
changes to a slice to avoid this problem.

Updates mmcloughlin#50
  • Loading branch information
mmcloughlin committed Jan 21, 2019
1 parent 2d7a9dd commit 02ecaad
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
4 changes: 2 additions & 2 deletions internal/load/annoyingaliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ arch=${GOROOT}/src/cmd/asm/internal/arch/arch.go
echo
echo 'package load'
echo
echo 'var annoyingaliases = map[string]string{'
echo 'var annoyingaliases = []alias{'

awk '
/archX86/ { x86=1 }
Expand All @@ -27,7 +27,7 @@ arch=${GOROOT}/src/cmd/asm/internal/arch/arch.go
sub(/x86\.A/, "", to)
if(from != to) {
printf("\t\"%s\": \"%s\",\n", from, to)
printf("\t{\"%s\", \"%s\"},\n", from, to)
}
}
' ${arch}
Expand Down
20 changes: 12 additions & 8 deletions internal/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ func (l *Loader) Load() ([]inst.Instruction, error) {
im[e.Opcode] = e
}

// Apply list of aliases.
for from, to := range aliases {
if existing, found := im[from]; found {
im[to].Forms = append(im[to].Forms, existing.Forms...)
// Merge aliased forms. This is primarily for MOVQ (issue #50).
for _, a := range aliases {
if existing, found := im[a.From]; found {
im[a.To].Forms = append(im[a.To].Forms, existing.Forms...)
}
cpy := *im[to]
cpy.Opcode = from
cpy.AliasOf = to
im[from] = &cpy
}

// Apply list of aliases.
for _, a := range aliases {
cpy := *im[a.To]
cpy.Opcode = a.From
cpy.AliasOf = a.To
im[a.From] = &cpy
}

// Dedupe forms.
Expand Down
14 changes: 9 additions & 5 deletions internal/load/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import (
"github.com/mmcloughlin/avo/internal/inst"
)

// alias defines an opcode alias.
type alias struct {
From string
To string
}

// aliases defines a list of opcode aliases. Where possible these are extracted
// from the code (see note below).
var aliases = map[string]string{
var aliases = []alias{
// The PSHUFD/PSHUFL alias is not recorded in the list of "Annoying aliases" below. However the instructions are identical.
//
// Reference: https://github.com/golang/go/blob/048c9164a0c5572df18325e377473e7893dbfb07/src/cmd/internal/obj/x86/asm6.go#L1365
Expand All @@ -17,7 +23,7 @@ var aliases = map[string]string{
//
// {APSHUFD, yxshuf, Pq, opBytes{0x70, 0}},
//
"PSHUFD": "PSHUFL",
{"PSHUFD", "PSHUFL"},
}

// Go contains a list of self-proclaimed "Annoying aliases", as follows. We use
Expand Down Expand Up @@ -88,9 +94,7 @@ var aliases = map[string]string{
//go:generate ./annoyingaliases.sh zannoyingaliases.go

func init() {
for from, to := range annoyingaliases {
aliases[from] = to
}
aliases = append(aliases, annoyingaliases...)
}

// extras is simply a list of extra instructions to add to the database.
Expand Down
76 changes: 38 additions & 38 deletions internal/load/zannoyingaliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02ecaad

Please sign in to comment.