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.
9 "internal/goexperiment"
19 // TestIntendedInlining tests that specific functions are inlined.
20 // This allows refactoring for code clarity and re-use without fear that
21 // changes to the compiler will cause silent performance regressions.
22 func TestIntendedInlining(t *testing.T) {
23 if testing.Short() && testenv.Builder() == "" {
24 t.Skip("skipping in short mode")
26 testenv.MustHaveGoRun(t)
29 // want is the list of function names (by package) that should
30 // be inlinable. If they have no callers in their packages, they
31 // might not actually be inlined anywhere.
32 want := map[string][]string{
74 "writeHeapBitsForAddr",
81 "(*gcWork).tryGetFast",
83 "(*markBits).advance",
84 "(*mspan).allocBitsForIndex",
86 "(*mspan).markBitsForBase",
87 "(*mspan).markBitsForIndex",
93 "runtime/internal/sys": {},
94 "runtime/internal/math": {
104 "(*Buffer).ReadByte",
107 "(*Buffer).UnreadByte",
108 "(*Buffer).tryGrowByReslice",
114 "(*dictDecoder).tryWriteCopy",
137 "Value.CanInterface",
150 "Value.OverflowComplex",
151 "Value.OverflowFloat",
153 "Value.OverflowUint",
162 "flag.mustBeAssignable",
163 "flag.mustBeExported",
172 // The following functions require the math_big_pure_go build tag.
177 "(*rngSource).Int63",
178 "(*rngSource).Uint64",
181 "(*UDPConn).ReadFromUDP",
184 // (*Bool).CompareAndSwap handled below.
189 "(*Int32).CompareAndSwap",
194 "(*Int64).CompareAndSwap",
199 "(*Uint32).CompareAndSwap",
204 "(*Uint64).CompareAndSwap",
209 "(*Uintptr).CompareAndSwap",
213 "(*Pointer[go.shape.int]).CompareAndSwap",
214 "(*Pointer[go.shape.int]).Load",
215 "(*Pointer[go.shape.int]).Store",
216 "(*Pointer[go.shape.int]).Swap",
220 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
221 // nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
222 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
223 // On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
224 // too expensive to inline (Issue 22239).
225 want["runtime"] = append(want["runtime"], "nextFreeFast")
226 // Same behavior for heapBits.nextFast.
227 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
229 if runtime.GOARCH != "386" {
230 // As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
231 // The same applies to Bswap32.
232 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros64")
233 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros32")
234 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
236 if bits.UintSize == 64 {
237 // mix is only defined on 64-bit architectures
238 want["runtime"] = append(want["runtime"], "mix")
239 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
240 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
243 switch runtime.GOARCH {
244 case "386", "wasm", "arm":
246 // TODO(mvdan): As explained in /test/inline_sync.go, some
247 // architectures don't have atomic intrinsics, so these go over
248 // the inlining budget. Move back to the main table once that
249 // problem is solved.
250 want["sync"] = []string{
254 "(*RWMutex).RUnlock",
259 // Functions that must actually be inlined; they must have actual callers.
260 must := map[string]bool{
261 "compress/flate.byLiteral.Len": true,
262 "compress/flate.byLiteral.Less": true,
263 "compress/flate.byLiteral.Swap": true,
266 notInlinedReason := make(map[string]string)
267 pkgs := make([]string, 0, len(want))
268 for pname, fnames := range want {
269 pkgs = append(pkgs, pname)
270 for _, fname := range fnames {
271 fullName := pname + "." + fname
272 if _, ok := notInlinedReason[fullName]; ok {
273 t.Errorf("duplicate func: %s", fullName)
275 notInlinedReason[fullName] = "unknown reason"
279 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
280 cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
284 cmdErr := make(chan error, 1)
289 scanner := bufio.NewScanner(pr)
291 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
292 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
293 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
295 line := scanner.Text()
296 if strings.HasPrefix(line, "# ") {
300 if m := haveInlined.FindStringSubmatch(line); m != nil {
302 delete(notInlinedReason, curPkg+"."+fname)
305 if m := canInline.FindStringSubmatch(line); m != nil {
307 fullname := curPkg + "." + fname
308 // If function must be inlined somewhere, being inlinable is not enough
309 if _, ok := must[fullname]; !ok {
310 delete(notInlinedReason, fullname)
314 if m := cannotInline.FindStringSubmatch(line); m != nil {
315 fname, reason := m[1], m[2]
316 fullName := curPkg + "." + fname
317 if _, ok := notInlinedReason[fullName]; ok {
318 // cmd/compile gave us a reason why
319 notInlinedReason[fullName] = reason
324 if err := <-cmdErr; err != nil {
327 if err := scanner.Err(); err != nil {
330 for fullName, reason := range notInlinedReason {
331 t.Errorf("%s was not inlined: %s", fullName, reason)
335 func collectInlCands(msgs string) map[string]struct{} {
336 rv := make(map[string]struct{})
337 lines := strings.Split(msgs, "\n")
338 re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
339 for _, line := range lines {
340 m := re.FindStringSubmatch(line)
342 rv[m[1]] = struct{}{}
348 func TestIssue56044(t *testing.T) {
350 t.Skipf("skipping test: too long for short mode")
352 if !goexperiment.CoverageRedesign {
353 t.Skipf("skipping new coverage tests (experiment not enabled)")
356 testenv.MustHaveGoBuild(t)
358 modes := []string{"-covermode=set", "-covermode=atomic"}
360 for _, mode := range modes {
361 // Build the Go runtime with "-m", capturing output.
362 args := []string{"build", "-gcflags=runtime=-m", "runtime"}
363 cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
364 b, err := cmd.CombinedOutput()
366 t.Fatalf("build failed (%v): %s", err, b)
368 mbase := collectInlCands(string(b))
370 // Redo the build with -cover, also with "-m".
371 args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
372 cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
373 b, err = cmd.CombinedOutput()
375 t.Fatalf("build failed (%v): %s", err, b)
377 mcov := collectInlCands(string(b))
379 // Make sure that there aren't any functions that are marked
380 // as inline candidates at base but not with coverage.
381 for k := range mbase {
382 if _, ok := mcov[k]; !ok {
383 t.Errorf("error: did not find %s in coverage -m output", k)