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{
75 "writeHeapBitsForAddr",
82 "(*gcWork).tryGetFast",
84 "(*markBits).advance",
85 "(*mspan).allocBitsForIndex",
87 "(*mspan).markBitsForBase",
88 "(*mspan).markBitsForIndex",
94 "runtime/internal/sys": {},
95 "runtime/internal/math": {
105 "(*Buffer).ReadByte",
108 "(*Buffer).UnreadByte",
109 "(*Buffer).tryGrowByReslice",
112 "UseInterfaceSwitchCache",
118 "(*dictDecoder).tryWriteCopy",
141 "Value.CanInterface",
154 "Value.OverflowComplex",
155 "Value.OverflowFloat",
157 "Value.OverflowUint",
166 "flag.mustBeAssignable",
167 "flag.mustBeExported",
176 // The following functions require the math_big_pure_go build tag.
181 "(*rngSource).Int63",
182 "(*rngSource).Uint64",
185 "(*UDPConn).ReadFromUDP",
188 // Both OnceFunc and its returned closure need to be inlinable so
189 // that the returned closure can be inlined into the caller of OnceFunc.
191 "OnceFunc.func2", // The returned closure.
192 // TODO(austin): It would be good to check OnceValue and OnceValues,
193 // too, but currently they aren't reported because they have type
194 // parameters and aren't instantiated in sync.
197 // (*Bool).CompareAndSwap handled below.
202 "(*Int32).CompareAndSwap",
207 "(*Int64).CompareAndSwap",
212 "(*Uint32).CompareAndSwap",
217 "(*Uint64).CompareAndSwap",
222 "(*Uintptr).CompareAndSwap",
226 "(*Pointer[go.shape.int]).CompareAndSwap",
227 "(*Pointer[go.shape.int]).Load",
228 "(*Pointer[go.shape.int]).Store",
229 "(*Pointer[go.shape.int]).Swap",
233 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
234 // nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
235 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
236 // On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
237 // too expensive to inline (Issue 22239).
238 want["runtime"] = append(want["runtime"], "nextFreeFast")
239 // Same behavior for heapBits.nextFast.
240 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
242 if runtime.GOARCH != "386" {
243 // As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
244 // The same applies to Bswap32.
245 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros64")
246 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros32")
247 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
249 if bits.UintSize == 64 {
250 // mix is only defined on 64-bit architectures
251 want["runtime"] = append(want["runtime"], "mix")
252 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
253 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
256 switch runtime.GOARCH {
257 case "386", "wasm", "arm":
259 // TODO(mvdan): As explained in /test/inline_sync.go, some
260 // architectures don't have atomic intrinsics, so these go over
261 // the inlining budget. Move back to the main table once that
262 // problem is solved.
263 want["sync"] = []string{
267 "(*RWMutex).RUnlock",
272 // Functions that must actually be inlined; they must have actual callers.
273 must := map[string]bool{
274 "compress/flate.byLiteral.Len": true,
275 "compress/flate.byLiteral.Less": true,
276 "compress/flate.byLiteral.Swap": true,
279 notInlinedReason := make(map[string]string)
280 pkgs := make([]string, 0, len(want))
281 for pname, fnames := range want {
282 pkgs = append(pkgs, pname)
283 for _, fname := range fnames {
284 fullName := pname + "." + fname
285 if _, ok := notInlinedReason[fullName]; ok {
286 t.Errorf("duplicate func: %s", fullName)
288 notInlinedReason[fullName] = "unknown reason"
292 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
293 cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
297 cmdErr := make(chan error, 1)
302 scanner := bufio.NewScanner(pr)
304 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
305 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
306 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
308 line := scanner.Text()
309 if strings.HasPrefix(line, "# ") {
313 if m := haveInlined.FindStringSubmatch(line); m != nil {
315 delete(notInlinedReason, curPkg+"."+fname)
318 if m := canInline.FindStringSubmatch(line); m != nil {
320 fullname := curPkg + "." + fname
321 // If function must be inlined somewhere, being inlinable is not enough
322 if _, ok := must[fullname]; !ok {
323 delete(notInlinedReason, fullname)
327 if m := cannotInline.FindStringSubmatch(line); m != nil {
328 fname, reason := m[1], m[2]
329 fullName := curPkg + "." + fname
330 if _, ok := notInlinedReason[fullName]; ok {
331 // cmd/compile gave us a reason why
332 notInlinedReason[fullName] = reason
337 if err := <-cmdErr; err != nil {
340 if err := scanner.Err(); err != nil {
343 for fullName, reason := range notInlinedReason {
344 t.Errorf("%s was not inlined: %s", fullName, reason)
348 func collectInlCands(msgs string) map[string]struct{} {
349 rv := make(map[string]struct{})
350 lines := strings.Split(msgs, "\n")
351 re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
352 for _, line := range lines {
353 m := re.FindStringSubmatch(line)
355 rv[m[1]] = struct{}{}
361 func TestIssue56044(t *testing.T) {
363 t.Skipf("skipping test: too long for short mode")
365 if !goexperiment.CoverageRedesign {
366 t.Skipf("skipping new coverage tests (experiment not enabled)")
369 testenv.MustHaveGoBuild(t)
371 modes := []string{"-covermode=set", "-covermode=atomic"}
373 for _, mode := range modes {
374 // Build the Go runtime with "-m", capturing output.
375 args := []string{"build", "-gcflags=runtime=-m", "runtime"}
376 cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
377 b, err := cmd.CombinedOutput()
379 t.Fatalf("build failed (%v): %s", err, b)
381 mbase := collectInlCands(string(b))
383 // Redo the build with -cover, also with "-m".
384 args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
385 cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
386 b, err = cmd.CombinedOutput()
388 t.Fatalf("build failed (%v): %s", err, b)
390 mcov := collectInlCands(string(b))
392 // Make sure that there aren't any functions that are marked
393 // as inline candidates at base but not with coverage.
394 for k := range mbase {
395 if _, ok := mcov[k]; !ok {
396 t.Errorf("error: did not find %s in coverage -m output", k)