2017-09-13 15:04:16 +02:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
2017-09-14 15:51:18 +01:00
|
|
|
"bufio"
|
2017-09-13 15:04:16 +02:00
|
|
|
"internal/testenv"
|
2017-09-14 15:51:18 +01:00
|
|
|
"io"
|
2017-09-13 15:04:16 +02:00
|
|
|
"os/exec"
|
2017-09-14 15:51:18 +01:00
|
|
|
"regexp"
|
2017-09-20 13:09:08 -05:00
|
|
|
"runtime"
|
2017-09-14 15:51:18 +01:00
|
|
|
"strings"
|
2017-09-13 15:04:16 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestIntendedInlining tests that specific runtime functions are inlined.
|
|
|
|
|
// This allows refactoring for code clarity and re-use without fear that
|
|
|
|
|
// changes to the compiler will cause silent performance regressions.
|
|
|
|
|
func TestIntendedInlining(t *testing.T) {
|
|
|
|
|
if testing.Short() && testenv.Builder() == "" {
|
|
|
|
|
t.Skip("skipping in short mode")
|
|
|
|
|
}
|
|
|
|
|
testenv.MustHaveGoRun(t)
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
2017-09-13 21:03:20 +02:00
|
|
|
// want is the list of function names (by package) that should
|
|
|
|
|
// be inlined.
|
|
|
|
|
want := map[string][]string{
|
|
|
|
|
"runtime": {
|
|
|
|
|
"tophash",
|
|
|
|
|
"add",
|
|
|
|
|
"addb",
|
|
|
|
|
"subtractb",
|
|
|
|
|
"(*bmap).keys",
|
|
|
|
|
"bucketShift",
|
|
|
|
|
"bucketMask",
|
|
|
|
|
"fastrand",
|
|
|
|
|
"noescape",
|
|
|
|
|
},
|
|
|
|
|
"unicode/utf8": {
|
|
|
|
|
"FullRune",
|
|
|
|
|
"FullRuneInString",
|
|
|
|
|
"RuneLen",
|
|
|
|
|
"ValidRune",
|
|
|
|
|
},
|
2017-09-13 15:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-20 13:09:08 -05:00
|
|
|
if runtime.GOARCH != "386" {
|
|
|
|
|
// nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
|
|
|
|
|
// We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
|
|
|
|
|
// So check for it only on non-386 platforms.
|
|
|
|
|
want["runtime"] = append(want["runtime"], "nextFreeFast")
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 15:51:18 +01:00
|
|
|
notInlinedReason := make(map[string]string)
|
2017-09-13 21:03:20 +02:00
|
|
|
pkgs := make([]string, 0, len(want))
|
|
|
|
|
for pname, fnames := range want {
|
|
|
|
|
pkgs = append(pkgs, pname)
|
|
|
|
|
for _, fname := range fnames {
|
2017-09-14 15:51:18 +01:00
|
|
|
notInlinedReason[pname+"."+fname] = "unknown reason"
|
2017-09-13 21:03:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 15:51:18 +01:00
|
|
|
args := append([]string{"build", "-a", "-gcflags=-m -m"}, pkgs...)
|
2017-09-13 21:03:20 +02:00
|
|
|
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
|
2017-09-14 15:51:18 +01:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
cmd.Stdout = pw
|
|
|
|
|
cmd.Stderr = pw
|
|
|
|
|
cmdErr := make(chan error, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
cmdErr <- cmd.Run()
|
|
|
|
|
pw.Close()
|
|
|
|
|
}()
|
|
|
|
|
scanner := bufio.NewScanner(pr)
|
2017-09-13 21:03:20 +02:00
|
|
|
curPkg := ""
|
2017-09-14 15:51:18 +01:00
|
|
|
canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
|
|
|
|
|
cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
line := scanner.Text()
|
|
|
|
|
if strings.HasPrefix(line, "# ") {
|
|
|
|
|
curPkg = line[2:]
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if m := canInline.FindStringSubmatch(line); m != nil {
|
|
|
|
|
fname := m[1]
|
|
|
|
|
delete(notInlinedReason, curPkg+"."+fname)
|
|
|
|
|
continue
|
2017-09-13 21:03:20 +02:00
|
|
|
}
|
2017-09-14 15:51:18 +01:00
|
|
|
if m := cannotInline.FindStringSubmatch(line); m != nil {
|
|
|
|
|
fname, reason := m[1], m[2]
|
|
|
|
|
fullName := curPkg + "." + fname
|
|
|
|
|
if _, ok := notInlinedReason[fullName]; ok {
|
|
|
|
|
// cmd/compile gave us a reason why
|
|
|
|
|
notInlinedReason[fullName] = reason
|
|
|
|
|
}
|
2017-09-13 15:04:16 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 15:51:18 +01:00
|
|
|
if err := <-cmdErr; err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for fullName, reason := range notInlinedReason {
|
|
|
|
|
t.Errorf("%s was not inlined: %s", fullName, reason)
|
2017-09-13 15:04:16 +02:00
|
|
|
}
|
|
|
|
|
}
|