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",
115 "(*dictDecoder).tryWriteCopy",
138 "Value.CanInterface",
151 "Value.OverflowComplex",
152 "Value.OverflowFloat",
154 "Value.OverflowUint",
163 "flag.mustBeAssignable",
164 "flag.mustBeExported",
173 // The following functions require the math_big_pure_go build tag.
178 "(*rngSource).Int63",
179 "(*rngSource).Uint64",
182 "(*UDPConn).ReadFromUDP",
185 // Both OnceFunc and its returned closure need to be inlinable so
186 // that the returned closure can be inlined into the caller of OnceFunc.
188 "OnceFunc.func2", // The returned closure.
189 // TODO(austin): It would be good to check OnceValue and OnceValues,
190 // too, but currently they aren't reported because they have type
191 // parameters and aren't instantiated in sync.
194 // (*Bool).CompareAndSwap handled below.
199 "(*Int32).CompareAndSwap",
204 "(*Int64).CompareAndSwap",
209 "(*Uint32).CompareAndSwap",
214 "(*Uint64).CompareAndSwap",
219 "(*Uintptr).CompareAndSwap",
223 "(*Pointer[go.shape.int]).CompareAndSwap",
224 "(*Pointer[go.shape.int]).Load",
225 "(*Pointer[go.shape.int]).Store",
226 "(*Pointer[go.shape.int]).Swap",
230 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
231 // nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
232 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
233 // On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
234 // too expensive to inline (Issue 22239).
235 want["runtime"] = append(want["runtime"], "nextFreeFast")
236 // Same behavior for heapBits.nextFast.
237 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
239 if runtime.GOARCH != "386" {
240 // As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
241 // The same applies to Bswap32.
242 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros64")
243 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros32")
244 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
246 if bits.UintSize == 64 {
247 // mix is only defined on 64-bit architectures
248 want["runtime"] = append(want["runtime"], "mix")
249 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
250 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
253 switch runtime.GOARCH {
254 case "386", "wasm", "arm":
256 // TODO(mvdan): As explained in /test/inline_sync.go, some
257 // architectures don't have atomic intrinsics, so these go over
258 // the inlining budget. Move back to the main table once that
259 // problem is solved.
260 want["sync"] = []string{
264 "(*RWMutex).RUnlock",
269 // Functions that must actually be inlined; they must have actual callers.
270 must := map[string]bool{
271 "compress/flate.byLiteral.Len": true,
272 "compress/flate.byLiteral.Less": true,
273 "compress/flate.byLiteral.Swap": true,
276 notInlinedReason := make(map[string]string)
277 pkgs := make([]string, 0, len(want))
278 for pname, fnames := range want {
279 pkgs = append(pkgs, pname)
280 for _, fname := range fnames {
281 fullName := pname + "." + fname
282 if _, ok := notInlinedReason[fullName]; ok {
283 t.Errorf("duplicate func: %s", fullName)
285 notInlinedReason[fullName] = "unknown reason"
289 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
290 cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
294 cmdErr := make(chan error, 1)
299 scanner := bufio.NewScanner(pr)
301 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
302 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
303 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
305 line := scanner.Text()
306 if strings.HasPrefix(line, "# ") {
310 if m := haveInlined.FindStringSubmatch(line); m != nil {
312 delete(notInlinedReason, curPkg+"."+fname)
315 if m := canInline.FindStringSubmatch(line); m != nil {
317 fullname := curPkg + "." + fname
318 // If function must be inlined somewhere, being inlinable is not enough
319 if _, ok := must[fullname]; !ok {
320 delete(notInlinedReason, fullname)
324 if m := cannotInline.FindStringSubmatch(line); m != nil {
325 fname, reason := m[1], m[2]
326 fullName := curPkg + "." + fname
327 if _, ok := notInlinedReason[fullName]; ok {
328 // cmd/compile gave us a reason why
329 notInlinedReason[fullName] = reason
334 if err := <-cmdErr; err != nil {
337 if err := scanner.Err(); err != nil {
340 for fullName, reason := range notInlinedReason {
341 t.Errorf("%s was not inlined: %s", fullName, reason)
345 func collectInlCands(msgs string) map[string]struct{} {
346 rv := make(map[string]struct{})
347 lines := strings.Split(msgs, "\n")
348 re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
349 for _, line := range lines {
350 m := re.FindStringSubmatch(line)
352 rv[m[1]] = struct{}{}
358 func TestIssue56044(t *testing.T) {
360 t.Skipf("skipping test: too long for short mode")
362 if !goexperiment.CoverageRedesign {
363 t.Skipf("skipping new coverage tests (experiment not enabled)")
366 testenv.MustHaveGoBuild(t)
368 modes := []string{"-covermode=set", "-covermode=atomic"}
370 for _, mode := range modes {
371 // Build the Go runtime with "-m", capturing output.
372 args := []string{"build", "-gcflags=runtime=-m", "runtime"}
373 cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
374 b, err := cmd.CombinedOutput()
376 t.Fatalf("build failed (%v): %s", err, b)
378 mbase := collectInlCands(string(b))
380 // Redo the build with -cover, also with "-m".
381 args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
382 cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
383 b, err = cmd.CombinedOutput()
385 t.Fatalf("build failed (%v): %s", err, b)
387 mcov := collectInlCands(string(b))
389 // Make sure that there aren't any functions that are marked
390 // as inline candidates at base but not with coverage.
391 for k := range mbase {
392 if _, ok := mcov[k]; !ok {
393 t.Errorf("error: did not find %s in coverage -m output", k)