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.
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",
92 "runtime/internal/sys": {},
93 "runtime/internal/math": {
103 "(*Buffer).ReadByte",
106 "(*Buffer).UnreadByte",
107 "(*Buffer).tryGrowByReslice",
113 "(*dictDecoder).tryWriteCopy",
133 "Value.CanInterface",
146 "Value.OverflowComplex",
147 "Value.OverflowFloat",
149 "Value.OverflowUint",
158 "flag.mustBeAssignable",
159 "flag.mustBeExported",
168 // The following functions require the math_big_pure_go build tag.
173 "(*rngSource).Int63",
174 "(*rngSource).Uint64",
177 "(*UDPConn).ReadFromUDP",
180 // (*Bool).CompareAndSwap handled below.
185 "(*Int32).CompareAndSwap",
190 "(*Int64).CompareAndSwap",
195 "(*Uint32).CompareAndSwap",
200 "(*Uint64).CompareAndSwap",
205 "(*Uintptr).CompareAndSwap",
209 // TODO(rsc): Why are these not reported as inlined?
210 // "(*Pointer[T]).CompareAndSwap",
211 // "(*Pointer[T]).Load",
212 // "(*Pointer[T]).Store",
213 // "(*Pointer[T]).Swap",
217 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
218 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
219 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
220 // On loong64, mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
221 // to inline (Issue 22239).
222 want["runtime"] = append(want["runtime"], "nextFreeFast")
223 // Same behavior for heapBits.nextFast.
224 want["runtime"] = append(want["runtime"], "heapBits.nextFast")
226 if runtime.GOARCH != "386" {
227 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
228 // The same applies to Bswap32.
229 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
230 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
231 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
233 if bits.UintSize == 64 {
234 // mix is only defined on 64-bit architectures
235 want["runtime"] = append(want["runtime"], "mix")
236 // (*Bool).CompareAndSwap is just over budget on 32-bit systems (386, arm).
237 want["sync/atomic"] = append(want["sync/atomic"], "(*Bool).CompareAndSwap")
240 switch runtime.GOARCH {
241 case "386", "wasm", "arm":
243 // TODO(mvdan): As explained in /test/inline_sync.go, some
244 // architectures don't have atomic intrinsics, so these go over
245 // the inlining budget. Move back to the main table once that
246 // problem is solved.
247 want["sync"] = []string{
251 "(*RWMutex).RUnlock",
256 // Functions that must actually be inlined; they must have actual callers.
257 must := map[string]bool{
258 "compress/flate.byLiteral.Len": true,
259 "compress/flate.byLiteral.Less": true,
260 "compress/flate.byLiteral.Swap": true,
263 notInlinedReason := make(map[string]string)
264 pkgs := make([]string, 0, len(want))
265 for pname, fnames := range want {
266 pkgs = append(pkgs, pname)
267 for _, fname := range fnames {
268 fullName := pname + "." + fname
269 if _, ok := notInlinedReason[fullName]; ok {
270 t.Errorf("duplicate func: %s", fullName)
272 notInlinedReason[fullName] = "unknown reason"
276 args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
277 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
281 cmdErr := make(chan error, 1)
286 scanner := bufio.NewScanner(pr)
288 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
289 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
290 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
292 line := scanner.Text()
293 if strings.HasPrefix(line, "# ") {
297 if m := haveInlined.FindStringSubmatch(line); m != nil {
299 delete(notInlinedReason, curPkg+"."+fname)
302 if m := canInline.FindStringSubmatch(line); m != nil {
304 fullname := curPkg + "." + fname
305 // If function must be inlined somewhere, being inlinable is not enough
306 if _, ok := must[fullname]; !ok {
307 delete(notInlinedReason, fullname)
311 if m := cannotInline.FindStringSubmatch(line); m != nil {
312 fname, reason := m[1], m[2]
313 fullName := curPkg + "." + fname
314 if _, ok := notInlinedReason[fullName]; ok {
315 // cmd/compile gave us a reason why
316 notInlinedReason[fullName] = reason
321 if err := <-cmdErr; err != nil {
324 if err := scanner.Err(); err != nil {
327 for fullName, reason := range notInlinedReason {
328 t.Errorf("%s was not inlined: %s", fullName, reason)