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 "typePointers.nextFast",
84 "(*gcWork).tryGetFast",
86 "(*markBits).advance",
87 "(*mspan).allocBitsForIndex",
89 "(*mspan).markBitsForBase",
90 "(*mspan).markBitsForIndex",
91 "(*mspan).writeHeapBits",
97 "runtime/internal/sys": {},
98 "runtime/internal/math": {
108 "(*Buffer).ReadByte",
111 "(*Buffer).UnreadByte",
112 "(*Buffer).tryGrowByReslice",
115 "UseInterfaceSwitchCache",
121 "(*dictDecoder).tryWriteCopy",
144 "Value.CanInterface",
157 "Value.OverflowComplex",
158 "Value.OverflowFloat",
160 "Value.OverflowUint",
169 "flag.mustBeAssignable",
170 "flag.mustBeExported",
179 // The following functions require the math_big_pure_go build tag.
184 "(*rngSource).Int63",
185 "(*rngSource).Uint64",
188 "(*UDPConn).ReadFromUDP",
191 // Both OnceFunc and its returned closure need to be inlinable so
192 // that the returned closure can be inlined into the caller of OnceFunc.
194 "OnceFunc.func2", // The returned closure.
195 // TODO(austin): It would be good to check OnceValue and OnceValues,
196 // too, but currently they aren't reported because they have type
197 // parameters and aren't instantiated in sync.
200 // (*Bool).CompareAndSwap handled below.
205 "(*Int32).CompareAndSwap",
210 "(*Int64).CompareAndSwap",
215 "(*Uint32).CompareAndSwap",
220 "(*Uint64).CompareAndSwap",
225 "(*Uintptr).CompareAndSwap",
229 "(*Pointer[go.shape.int]).CompareAndSwap",
230 "(*Pointer[go.shape.int]).Load",
231 "(*Pointer[go.shape.int]).Store",
232 "(*Pointer[go.shape.int]).Swap",
236 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
237 // nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
238 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
239 // On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
240 // too expensive to inline (Issue 22239).
241 want["runtime"] = append(want["runtime"], "nextFreeFast")
242 // Same behavior for heapBits.nextFast.
243 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
245 if runtime.GOARCH != "386" {
246 // As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
247 // The same applies to Bswap32.
248 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros64")
249 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros32")
250 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
252 if bits.UintSize == 64 {
253 // mix is only defined on 64-bit architectures
254 want["runtime"] = append(want["runtime"], "mix")
255 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
256 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
259 switch runtime.GOARCH {
260 case "386", "wasm", "arm":
262 // TODO(mvdan): As explained in /test/inline_sync.go, some
263 // architectures don't have atomic intrinsics, so these go over
264 // the inlining budget. Move back to the main table once that
265 // problem is solved.
266 want["sync"] = []string{
270 "(*RWMutex).RUnlock",
275 // Functions that must actually be inlined; they must have actual callers.
276 must := map[string]bool{
277 "compress/flate.byLiteral.Len": true,
278 "compress/flate.byLiteral.Less": true,
279 "compress/flate.byLiteral.Swap": true,
282 notInlinedReason := make(map[string]string)
283 pkgs := make([]string, 0, len(want))
284 for pname, fnames := range want {
285 pkgs = append(pkgs, pname)
286 for _, fname := range fnames {
287 fullName := pname + "." + fname
288 if _, ok := notInlinedReason[fullName]; ok {
289 t.Errorf("duplicate func: %s", fullName)
291 notInlinedReason[fullName] = "unknown reason"
295 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
296 cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
300 cmdErr := make(chan error, 1)
305 scanner := bufio.NewScanner(pr)
307 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
308 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
309 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
311 line := scanner.Text()
312 if strings.HasPrefix(line, "# ") {
316 if m := haveInlined.FindStringSubmatch(line); m != nil {
318 delete(notInlinedReason, curPkg+"."+fname)
321 if m := canInline.FindStringSubmatch(line); m != nil {
323 fullname := curPkg + "." + fname
324 // If function must be inlined somewhere, being inlinable is not enough
325 if _, ok := must[fullname]; !ok {
326 delete(notInlinedReason, fullname)
330 if m := cannotInline.FindStringSubmatch(line); m != nil {
331 fname, reason := m[1], m[2]
332 fullName := curPkg + "." + fname
333 if _, ok := notInlinedReason[fullName]; ok {
334 // cmd/compile gave us a reason why
335 notInlinedReason[fullName] = reason
340 if err := <-cmdErr; err != nil {
343 if err := scanner.Err(); err != nil {
346 for fullName, reason := range notInlinedReason {
347 t.Errorf("%s was not inlined: %s", fullName, reason)
351 func collectInlCands(msgs string) map[string]struct{} {
352 rv := make(map[string]struct{})
353 lines := strings.Split(msgs, "\n")
354 re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
355 for _, line := range lines {
356 m := re.FindStringSubmatch(line)
358 rv[m[1]] = struct{}{}
364 func TestIssue56044(t *testing.T) {
366 t.Skipf("skipping test: too long for short mode")
368 if !goexperiment.CoverageRedesign {
369 t.Skipf("skipping new coverage tests (experiment not enabled)")
372 testenv.MustHaveGoBuild(t)
374 modes := []string{"-covermode=set", "-covermode=atomic"}
376 for _, mode := range modes {
377 // Build the Go runtime with "-m", capturing output.
378 args := []string{"build", "-gcflags=runtime=-m", "runtime"}
379 cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
380 b, err := cmd.CombinedOutput()
382 t.Fatalf("build failed (%v): %s", err, b)
384 mbase := collectInlCands(string(b))
386 // Redo the build with -cover, also with "-m".
387 args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
388 cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
389 b, err = cmd.CombinedOutput()
391 t.Fatalf("build failed (%v): %s", err, b)
393 mcov := collectInlCands(string(b))
395 // Make sure that there aren't any functions that are marked
396 // as inline candidates at base but not with coverage.
397 for k := range mbase {
398 if _, ok := mcov[k]; !ok {
399 t.Errorf("error: did not find %s in coverage -m output", k)