Source file src/cmd/compile/internal/test/inl_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"bufio"
     9  	"cmd/internal/sys"
    10  	"internal/testenv"
    11  	"io"
    12  	"math/bits"
    13  	"os"
    14  	"path/filepath"
    15  	"regexp"
    16  	"runtime"
    17  	"strconv"
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  // TestIntendedInlining tests that specific functions can be inlined,
    23  // as determined by parsing the compiler's -m diagnostic output when
    24  // building each function's package. This allows refactoring for code
    25  // clarity and re-use without fear that changes to the compiler will
    26  // cause silent performance regressions.
    27  func TestIntendedInlining(t *testing.T) {
    28  	if testing.Short() && testenv.Builder() == "" {
    29  		t.Skip("skipping in short mode")
    30  	}
    31  	testenv.MustHaveGoRun(t)
    32  	t.Parallel()
    33  
    34  	// want is the list of function names (by package) that should
    35  	// be inlinable. If they have no callers in their packages, they
    36  	// might not actually be inlined anywhere.
    37  	want := map[string][]string{
    38  		"runtime": {
    39  			"add",
    40  			"acquirem",
    41  			"add1",
    42  			"addb",
    43  			"adjustpanics",
    44  			"adjustpointer",
    45  			"alignDown",
    46  			"alignUp",
    47  			"chanbuf",
    48  			"fastlog2",
    49  			"float64bits",
    50  			"funcspdelta",
    51  			"getm",
    52  			"getMCache",
    53  			"heapSetTypeNoHeader",
    54  			"heapSetTypeSmallHeader",
    55  			"itabHashFunc",
    56  			"nextslicecap",
    57  			"noescape",
    58  			"pcvalueCacheKey",
    59  			"rand32",
    60  			"readUnaligned64",
    61  			"releasem",
    62  			"roundupsize",
    63  			"stackmapdata",
    64  			"stringStructOf",
    65  			"subtract1",
    66  			"subtractb",
    67  			"(*waitq).enqueue",
    68  			"funcInfo.entry",
    69  
    70  			// GC-related ones
    71  			"cgoInRange",
    72  			"gclinkptr.ptr",
    73  			"gcUsesSpanInlineMarkBits",
    74  			"guintptr.ptr",
    75  			"heapBitsSlice",
    76  			"markBits.isMarked",
    77  			"muintptr.ptr",
    78  			"puintptr.ptr",
    79  			"spanHeapBitsRange",
    80  			"spanOf",
    81  			"spanOfUnchecked",
    82  			"typePointers.nextFast",
    83  			"(*gcWork).putObjFast",
    84  			"(*gcWork).tryGetObjFast",
    85  			"(*guintptr).set",
    86  			"(*markBits).advance",
    87  			"(*mspan).allocBitsForIndex",
    88  			"(*mspan).base",
    89  			"(*mspan).markBitsForBase",
    90  			"(*mspan).markBitsForIndex",
    91  			"(*mspan).writeUserArenaHeapBits",
    92  			"(*muintptr).set",
    93  			"(*puintptr).set",
    94  			"(*wbBuf).get1",
    95  			"(*wbBuf).get2",
    96  
    97  			// Trace-related ones.
    98  			"traceLocker.ok",
    99  			"traceEnabled",
   100  		},
   101  		"bytes": {
   102  			"(*Buffer).Bytes",
   103  			"(*Buffer).Cap",
   104  			"(*Buffer).Len",
   105  			"(*Buffer).Grow",
   106  			"(*Buffer).Next",
   107  			"(*Buffer).Read",
   108  			"(*Buffer).ReadByte",
   109  			"(*Buffer).Reset",
   110  			"(*Buffer).String",
   111  			"(*Buffer).UnreadByte",
   112  			"(*Buffer).tryGrowByReslice",
   113  		},
   114  		"internal/abi": {
   115  			"(*Type).IsDirectIface",
   116  			"UseInterfaceSwitchCache",
   117  		},
   118  		"internal/runtime/math": {
   119  			"MulUintptr",
   120  		},
   121  		"internal/runtime/maps": {
   122  			"readUnaligned32",
   123  			"readUnaligned64",
   124  		},
   125  		"internal/runtime/sys": {},
   126  		"compress/flate": {
   127  			"(*dictDecoder).tryWriteCopy",
   128  		},
   129  		"encoding/base64": {
   130  			"assemble32",
   131  			"assemble64",
   132  		},
   133  		"unicode/utf8": {
   134  			"DecodeRune",
   135  			"DecodeRuneInString",
   136  			"FullRune",
   137  			"FullRuneInString",
   138  			"RuneLen",
   139  			"AppendRune",
   140  			"ValidRune",
   141  		},
   142  		"unicode/utf16": {
   143  			"Decode",
   144  		},
   145  		"reflect": {
   146  			"Value.Bool",
   147  			"Value.Bytes",
   148  			"Value.CanAddr",
   149  			"Value.CanComplex",
   150  			"Value.CanFloat",
   151  			"Value.CanInt",
   152  			"Value.CanInterface",
   153  			"Value.CanSet",
   154  			"Value.CanUint",
   155  			"Value.Cap",
   156  			"Value.Complex",
   157  			"Value.Float",
   158  			"Value.Int",
   159  			"Value.Interface",
   160  			"Value.IsNil",
   161  			"Value.IsValid",
   162  			"Value.Kind",
   163  			"Value.Len",
   164  			"Value.MapRange",
   165  			"Value.OverflowComplex",
   166  			"Value.OverflowFloat",
   167  			"Value.OverflowInt",
   168  			"Value.OverflowUint",
   169  			"Value.String",
   170  			"Value.Type",
   171  			"Value.Uint",
   172  			"Value.UnsafeAddr",
   173  			"Value.pointer",
   174  			"add",
   175  			"align",
   176  			"flag.mustBe",
   177  			"flag.mustBeAssignable",
   178  			"flag.mustBeExported",
   179  			"flag.kind",
   180  			"flag.ro",
   181  		},
   182  		"regexp": {
   183  			"(*bitState).push",
   184  		},
   185  		"sort": {
   186  			"Find",
   187  			"Search",
   188  		},
   189  		"math/big": {
   190  			"bigEndianWord",
   191  		},
   192  		"math/rand": {
   193  			"(*rngSource).Int63",
   194  			"(*rngSource).Uint64",
   195  		},
   196  		"net": {
   197  			"(*UDPConn).ReadFromUDP",
   198  		},
   199  		"sync": {
   200  			// Both OnceFunc and its returned closure need to be inlinable so
   201  			// that the returned closure can be inlined into the caller of OnceFunc.
   202  			"OnceFunc",
   203  			"OnceFunc.func1", // The returned closure.
   204  			// TODO(austin): It would be good to check OnceValue and OnceValues,
   205  			// too, but currently they aren't reported because they have type
   206  			// parameters and aren't instantiated in sync.
   207  		},
   208  		"sync/atomic": {
   209  			// (*Bool).CompareAndSwap handled below.
   210  			"(*Bool).Load",
   211  			"(*Bool).Store",
   212  			"(*Bool).Swap",
   213  			"(*Int32).Add",
   214  			"(*Int32).CompareAndSwap",
   215  			"(*Int32).Load",
   216  			"(*Int32).Store",
   217  			"(*Int32).Swap",
   218  			"(*Int64).Add",
   219  			"(*Int64).CompareAndSwap",
   220  			"(*Int64).Load",
   221  			"(*Int64).Store",
   222  			"(*Int64).Swap",
   223  			"(*Uint32).Add",
   224  			"(*Uint32).CompareAndSwap",
   225  			"(*Uint32).Load",
   226  			"(*Uint32).Store",
   227  			"(*Uint32).Swap",
   228  			"(*Uint64).Add",
   229  			"(*Uint64).CompareAndSwap",
   230  			"(*Uint64).Load",
   231  			"(*Uint64).Store",
   232  			"(*Uint64).Swap",
   233  			"(*Uintptr).Add",
   234  			"(*Uintptr).CompareAndSwap",
   235  			"(*Uintptr).Load",
   236  			"(*Uintptr).Store",
   237  			"(*Uintptr).Swap",
   238  			"(*Pointer[go.shape.int]).CompareAndSwap",
   239  			"(*Pointer[go.shape.int]).Load",
   240  			"(*Pointer[go.shape.int]).Store",
   241  			"(*Pointer[go.shape.int]).Swap",
   242  		},
   243  		"testing": {
   244  			"(*B).Loop",
   245  		},
   246  		"time": {
   247  			"Duration.String",
   248  		},
   249  		"uuid": {
   250  			"UUID.String",
   251  		},
   252  		"path": {
   253  			"scanChunk",
   254  		},
   255  		"path/filepath": {
   256  			"scanChunk",
   257  		},
   258  		"internal/strconv": {
   259  			"prescale",
   260  			"uscale",
   261  		},
   262  	}
   263  
   264  	if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
   265  		// nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
   266  		// We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
   267  		// On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
   268  		// too expensive to inline (Issue 22239).
   269  		want["runtime"] = append(want["runtime"], "nextFreeFast")
   270  	}
   271  	if runtime.GOARCH != "386" {
   272  		// As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
   273  		// The same applies to Bswap32.
   274  		want["internal/runtime/sys"] = append(want["internal/runtime/sys"], "TrailingZeros64")
   275  		want["internal/runtime/sys"] = append(want["internal/runtime/sys"], "TrailingZeros32")
   276  		want["internal/runtime/sys"] = append(want["internal/runtime/sys"], "Bswap32")
   277  	}
   278  	if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" || runtime.GOARCH == "loong64" || runtime.GOARCH == "mips" || runtime.GOARCH == "mips64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "riscv64" || runtime.GOARCH == "s390x" {
   279  		// internal/runtime/atomic.Loaduintptr is only intrinsified on these platforms.
   280  		want["runtime"] = append(want["runtime"], "traceAcquire")
   281  	}
   282  	if bits.UintSize == 64 {
   283  		// mix is only defined on 64-bit architectures
   284  		want["internal/runtime/maps"] = append(want["internal/runtime/maps"], "mix")
   285  		// (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
   286  		want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
   287  	}
   288  	if runtime.GOARCH != "amd64" {
   289  		// path.Base calls bytealg.LastIndexByteString. On amd64 it is implemented in
   290  		// assembly and is therefore not inlinable. This pushes path.Base over the
   291  		// inlining budget.
   292  		want["path"] = append(want["path"], "Base")
   293  	}
   294  
   295  	switch runtime.GOARCH {
   296  	case "386", "wasm", "arm":
   297  	default:
   298  		// TODO(mvdan): As explained in /test/inline_sync.go, some
   299  		// architectures don't have atomic intrinsics, so these go over
   300  		// the inlining budget. Move back to the main table once that
   301  		// problem is solved.
   302  		want["sync"] = []string{
   303  			"(*Mutex).Lock",
   304  			"(*Mutex).Unlock",
   305  			"(*RWMutex).RLock",
   306  			"(*RWMutex).RUnlock",
   307  			"(*Once).Do",
   308  		}
   309  	}
   310  
   311  	if runtime.GOARCH != "wasm" {
   312  		// mutex implementation for multi-threaded GOARCHes
   313  		want["runtime"] = append(want["runtime"],
   314  			// in the fast paths of lock2 and unlock2
   315  			"key8",
   316  			"(*mLockProfile).store",
   317  		)
   318  		if bits.UintSize == 64 {
   319  			// these use 64-bit arithmetic, which is hard to inline on 32-bit platforms
   320  			want["runtime"] = append(want["runtime"],
   321  				// in the fast paths of lock2 and unlock2
   322  				"mutexSampleContention",
   323  
   324  				// in a slow path of lock2, but within the critical section
   325  				"(*mLockProfile).end",
   326  			)
   327  		}
   328  	}
   329  
   330  	// Functions that must actually be inlined; they must have actual callers.
   331  	must := map[string]bool{}
   332  
   333  	notInlinedReason := make(map[string]string)
   334  	pkgs := make([]string, 0, len(want))
   335  	for pname, fnames := range want {
   336  		pkgs = append(pkgs, pname)
   337  		for _, fname := range fnames {
   338  			fullName := pname + "." + fname
   339  			if _, ok := notInlinedReason[fullName]; ok {
   340  				t.Errorf("duplicate func: %s", fullName)
   341  			}
   342  			notInlinedReason[fullName] = "unknown reason"
   343  		}
   344  	}
   345  
   346  	args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
   347  	cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
   348  	pr, pw := io.Pipe()
   349  	cmd.Stdout = pw
   350  	cmd.Stderr = pw
   351  	cmdErr := make(chan error, 1)
   352  	go func() {
   353  		cmdErr <- cmd.Run()
   354  		pw.Close()
   355  	}()
   356  	scanner := bufio.NewScanner(pr)
   357  	curPkg := ""
   358  	canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
   359  	haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
   360  	cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
   361  	for scanner.Scan() {
   362  		line := scanner.Text()
   363  		if strings.HasPrefix(line, "# ") {
   364  			curPkg = line[2:]
   365  			continue
   366  		}
   367  		if m := haveInlined.FindStringSubmatch(line); m != nil {
   368  			fname := m[1]
   369  			delete(notInlinedReason, curPkg+"."+fname)
   370  			continue
   371  		}
   372  		if m := canInline.FindStringSubmatch(line); m != nil {
   373  			fname := m[1]
   374  			fullname := curPkg + "." + fname
   375  			// If function must be inlined somewhere, being inlinable is not enough
   376  			if _, ok := must[fullname]; !ok {
   377  				delete(notInlinedReason, fullname)
   378  				continue
   379  			}
   380  		}
   381  		if m := cannotInline.FindStringSubmatch(line); m != nil {
   382  			fname, reason := m[1], m[2]
   383  			fullName := curPkg + "." + fname
   384  			if _, ok := notInlinedReason[fullName]; ok {
   385  				// cmd/compile gave us a reason why
   386  				notInlinedReason[fullName] = reason
   387  			}
   388  			continue
   389  		}
   390  	}
   391  	if err := <-cmdErr; err != nil {
   392  		t.Fatal(err)
   393  	}
   394  	if err := scanner.Err(); err != nil {
   395  		t.Fatal(err)
   396  	}
   397  	for fullName, reason := range notInlinedReason {
   398  		t.Errorf("%s was not inlined: %s", fullName, reason)
   399  	}
   400  }
   401  
   402  func collectInlCands(msgs string) map[string]struct{} {
   403  	rv := make(map[string]struct{})
   404  	lines := strings.Split(msgs, "\n")
   405  	re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
   406  	for _, line := range lines {
   407  		m := re.FindStringSubmatch(line)
   408  		if m != nil {
   409  			rv[m[1]] = struct{}{}
   410  		}
   411  	}
   412  	return rv
   413  }
   414  
   415  func TestIssue56044(t *testing.T) {
   416  	if testing.Short() {
   417  		t.Skipf("skipping test: too long for short mode")
   418  	}
   419  	testenv.MustHaveGoBuild(t)
   420  
   421  	modes := []string{"-covermode=set", "-covermode=atomic"}
   422  
   423  	for _, mode := range modes {
   424  		// Build the Go runtime with "-m", capturing output.
   425  		args := []string{"build", "-gcflags=runtime=-m", "runtime"}
   426  		cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
   427  		b, err := cmd.CombinedOutput()
   428  		if err != nil {
   429  			t.Fatalf("build failed (%v): %s", err, b)
   430  		}
   431  		mbase := collectInlCands(string(b))
   432  
   433  		// Redo the build with -cover, also with "-m".
   434  		args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
   435  		cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
   436  		b, err = cmd.CombinedOutput()
   437  		if err != nil {
   438  			t.Fatalf("build failed (%v): %s", err, b)
   439  		}
   440  		mcov := collectInlCands(string(b))
   441  
   442  		// Make sure that there aren't any functions that are marked
   443  		// as inline candidates at base but not with coverage.
   444  		for k := range mbase {
   445  			if _, ok := mcov[k]; !ok {
   446  				t.Errorf("error: did not find %s in coverage -m output", k)
   447  			}
   448  		}
   449  	}
   450  }
   451  
   452  func TestByteOrderInline(t *testing.T) {
   453  	testenv.MustHaveGoBuild(t)
   454  	for _, a := range sys.Archs {
   455  		if a.Name == runtime.GOARCH && !a.CanMergeLoads {
   456  			t.Skipf("%s does not merge loads", runtime.GOARCH)
   457  		}
   458  	}
   459  
   460  	cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), "build",
   461  		"-gcflags=encoding/binary=-m=2",
   462  		"-gcflags=internal/byteorder=-m=2",
   463  		"encoding/binary", "internal/byteorder",
   464  	))
   465  	out, err := cmd.CombinedOutput()
   466  	if err != nil {
   467  		t.Fatalf("build failed: %v\n%s", err, out)
   468  	}
   469  	for _, prefix := range []string{"littleEndian.", "bigEndian.", "LE", "BE"} {
   470  		for _, op := range []string{"Uint16", "Uint32", "Uint64", "PutUint16", "PutUint32", "PutUint64"} {
   471  			want := "can inline " + prefix + op + " with cost 1 "
   472  			if !strings.Contains(string(out), want) {
   473  				t.Errorf("missing %q in -m=2 output", want)
   474  			}
   475  		}
   476  		for _, op := range []string{"AppendUint16", "AppendUint32", "AppendUint64"} {
   477  			unwanted := "can inline " + prefix + op + " with cost 1 "
   478  			if strings.Contains(string(out), unwanted) {
   479  				t.Errorf("unexpected %q in -m=2 output", unwanted)
   480  			}
   481  		}
   482  	}
   483  
   484  	dir := t.TempDir()
   485  	src := filepath.Join(dir, "p.go")
   486  	const marker = "// big calls"
   487  	code := `package p
   488  import "encoding/binary"
   489  func wrap(b []byte) uint64 { return binary.LittleEndian.Uint64(b) + binary.BigEndian.Uint64(b) }
   490  func medium(b []byte) uint64 {
   491  	return uint64(b[0]) + uint64(b[1]) + uint64(b[2]) + uint64(b[3]) + uint64(b[4]) + uint64(b[5]) + uint64(b[6]) + uint64(b[7])
   492  }
   493  func big(b []byte) ([]byte, uint64) {
   494  	var x byte
   495  ` + strings.Repeat("\tx += b[0]\n", 1700) + "\t" + marker + `
   496  	v := uint64(x) + medium(b) + wrap(b) + uint64(binary.LittleEndian.Uint32(b))
   497  	binary.LittleEndian.PutUint64(b, v)
   498  	b = binary.LittleEndian.AppendUint64(b, v)
   499  	return b, v
   500  }
   501  `
   502  	if err := os.WriteFile(src, []byte(code), 0644); err != nil {
   503  		t.Fatal(err)
   504  	}
   505  	cmd = testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags=-m=2", "-o", filepath.Join(dir, "p.a"), src))
   506  	out, err = cmd.CombinedOutput()
   507  	if err != nil {
   508  		t.Fatalf("compile p.go failed: %v\n%s", err, out)
   509  	}
   510  	s := string(out)
   511  	if !strings.Contains(s, "function big considered 'big'") {
   512  		t.Fatalf("big was not considered 'big':\n%s", s)
   513  	}
   514  
   515  	// Collect inlined calls originating at or after the marker inside big,
   516  	// excluding wrap's own standalone compilation earlier in the file.
   517  	bigCallLine := 1 + strings.Count(code[:strings.Index(code, marker)], "\n")
   518  	inlRE := regexp.MustCompile(`p\.go:(\d+):\d+: inlining call to (\S+)`)
   519  	bigInlined := make(map[string]bool)
   520  	for _, m := range inlRE.FindAllStringSubmatch(s, -1) {
   521  		line, err := strconv.Atoi(m[1])
   522  		if err == nil && line >= bigCallLine {
   523  			bigInlined[m[2]] = true
   524  		}
   525  	}
   526  	for _, fn := range []string{"medium", "binary.littleEndian.AppendUint64"} {
   527  		if bigInlined[fn] {
   528  			t.Errorf("%s unexpectedly inlined into big", fn)
   529  		}
   530  	}
   531  	for _, fn := range []string{
   532  		"wrap",
   533  		"binary.littleEndian.Uint64",
   534  		"binary.bigEndian.Uint64",
   535  		"binary.littleEndian.Uint32",
   536  		"binary.littleEndian.PutUint64",
   537  	} {
   538  		if !bigInlined[fn] {
   539  			t.Errorf("expected %s to inline into big:\n%s", fn, s)
   540  		}
   541  	}
   542  }
   543  

View as plain text