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",
91 "runtime/internal/sys": {},
92 "runtime/internal/math": {
102 "(*Buffer).ReadByte",
105 "(*Buffer).UnreadByte",
106 "(*Buffer).tryGrowByReslice",
112 "(*dictDecoder).tryWriteCopy",
135 "Value.CanInterface",
148 "Value.OverflowComplex",
149 "Value.OverflowFloat",
151 "Value.OverflowUint",
160 "flag.mustBeAssignable",
161 "flag.mustBeExported",
170 // The following functions require the math_big_pure_go build tag.
175 "(*rngSource).Int63",
176 "(*rngSource).Uint64",
179 "(*UDPConn).ReadFromUDP",
182 // (*Bool).CompareAndSwap handled below.
187 "(*Int32).CompareAndSwap",
192 "(*Int64).CompareAndSwap",
197 "(*Uint32).CompareAndSwap",
202 "(*Uint64).CompareAndSwap",
207 "(*Uintptr).CompareAndSwap",
211 "(*Pointer[go.shape.int]).CompareAndSwap",
212 "(*Pointer[go.shape.int]).Load",
213 "(*Pointer[go.shape.int]).Store",
214 "(*Pointer[go.shape.int]).Swap",
218 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
219 // nextFreeFast calls sys.TrailingZeros64, which on 386 is implemented in asm and is not inlinable.
220 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
221 // On loong64, mips64x and riscv64, TrailingZeros64 is not intrinsified and causes nextFreeFast
222 // too expensive to inline (Issue 22239).
223 want["runtime"] = append(want["runtime"], "nextFreeFast")
224 // Same behavior for heapBits.nextFast.
225 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
227 if runtime.GOARCH != "386" {
228 // As explained above, TrailingZeros64 and TrailingZeros32 are not Go code on 386.
229 // The same applies to Bswap32.
230 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros64")
231 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "TrailingZeros32")
232 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
234 if bits.UintSize == 64 {
235 // mix is only defined on 64-bit architectures
236 want["runtime"] = append(want["runtime"], "mix")
237 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
238 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
241 switch runtime.GOARCH {
242 case "386", "wasm", "arm":
244 // TODO(mvdan): As explained in /test/inline_sync.go, some
245 // architectures don't have atomic intrinsics, so these go over
246 // the inlining budget. Move back to the main table once that
247 // problem is solved.
248 want["sync"] = []string{
252 "(*RWMutex).RUnlock",
257 // Functions that must actually be inlined; they must have actual callers.
258 must := map[string]bool{
259 "compress/flate.byLiteral.Len": true,
260 "compress/flate.byLiteral.Less": true,
261 "compress/flate.byLiteral.Swap": true,
264 notInlinedReason := make(map[string]string)
265 pkgs := make([]string, 0, len(want))
266 for pname, fnames := range want {
267 pkgs = append(pkgs, pname)
268 for _, fname := range fnames {
269 fullName := pname + "." + fname
270 if _, ok := notInlinedReason[fullName]; ok {
271 t.Errorf("duplicate func: %s", fullName)
273 notInlinedReason[fullName] = "unknown reason"
277 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
278 cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
282 cmdErr := make(chan error, 1)
287 scanner := bufio.NewScanner(pr)
289 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
290 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
291 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
293 line := scanner.Text()
294 if strings.HasPrefix(line, "# ") {
298 if m := haveInlined.FindStringSubmatch(line); m != nil {
300 delete(notInlinedReason, curPkg+"."+fname)
303 if m := canInline.FindStringSubmatch(line); m != nil {
305 fullname := curPkg + "." + fname
306 // If function must be inlined somewhere, being inlinable is not enough
307 if _, ok := must[fullname]; !ok {
308 delete(notInlinedReason, fullname)
312 if m := cannotInline.FindStringSubmatch(line); m != nil {
313 fname, reason := m[1], m[2]
314 fullName := curPkg + "." + fname
315 if _, ok := notInlinedReason[fullName]; ok {
316 // cmd/compile gave us a reason why
317 notInlinedReason[fullName] = reason
322 if err := <-cmdErr; err != nil {
325 if err := scanner.Err(); err != nil {
328 for fullName, reason := range notInlinedReason {
329 t.Errorf("%s was not inlined: %s", fullName, reason)
333 func collectInlCands(msgs string) map[string]struct{} {
334 rv := make(map[string]struct{})
335 lines := strings.Split(msgs, "\n")
336 re := regexp.MustCompile(`^\S+\s+can\s+inline\s+(\S+)`)
337 for _, line := range lines {
338 m := re.FindStringSubmatch(line)
340 rv[m[1]] = struct{}{}
346 func TestIssue56044(t *testing.T) {
348 t.Skipf("skipping test: too long for short mode")
350 if !goexperiment.CoverageRedesign {
351 t.Skipf("skipping new coverage tests (experiment not enabled)")
354 testenv.MustHaveGoBuild(t)
356 modes := []string{"-covermode=set", "-covermode=atomic"}
358 for _, mode := range modes {
359 // Build the Go runtime with "-m", capturing output.
360 args := []string{"build", "-gcflags=runtime=-m", "runtime"}
361 cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
362 b, err := cmd.CombinedOutput()
364 t.Fatalf("build failed (%v): %s", err, b)
366 mbase := collectInlCands(string(b))
368 // Redo the build with -cover, also with "-m".
369 args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
370 cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
371 b, err = cmd.CombinedOutput()
373 t.Fatalf("build failed (%v): %s", err, b)
375 mcov := collectInlCands(string(b))
377 // Make sure that there aren't any functions that are marked
378 // as inline candidates at base but not with coverage.
379 for k := range mbase {
380 if _, ok := mcov[k]; !ok {
381 t.Errorf("error: did not find %s in coverage -m output", k)