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{
77 "heapBits.morePointers",
86 "(*gcWork).tryGetFast",
88 "(*markBits).advance",
89 "(*mspan).allocBitsForIndex",
91 "(*mspan).markBitsForBase",
92 "(*mspan).markBitsForIndex",
96 "runtime/internal/sys": {},
97 "runtime/internal/math": {
107 "(*Buffer).ReadByte",
110 "(*Buffer).UnreadByte",
111 "(*Buffer).tryGrowByReslice",
117 "(*dictDecoder).tryWriteCopy",
137 "Value.CanInterface",
144 "flag.mustBeAssignable",
145 "flag.mustBeExported",
154 // The following functions require the math_big_pure_go build tag.
159 "(*rngSource).Int63",
160 "(*rngSource).Uint64",
163 "(*UDPConn).ReadFromUDP",
167 if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
168 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
169 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
170 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
171 // to inline (Issue 22239).
172 want["runtime"] = append(want["runtime"], "nextFreeFast")
174 if runtime.GOARCH != "386" {
175 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
176 // The same applies to Bswap32.
177 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
178 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
179 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
181 if bits.UintSize == 64 {
182 // mix is only defined on 64-bit architectures
183 want["runtime"] = append(want["runtime"], "mix")
186 switch runtime.GOARCH {
187 case "386", "wasm", "arm":
189 // TODO(mvdan): As explained in /test/inline_sync.go, some
190 // architectures don't have atomic intrinsics, so these go over
191 // the inlining budget. Move back to the main table once that
192 // problem is solved.
193 want["sync"] = []string{
197 "(*RWMutex).RUnlock",
202 // Functions that must actually be inlined; they must have actual callers.
203 must := map[string]bool{
204 "compress/flate.byLiteral.Len": true,
205 "compress/flate.byLiteral.Less": true,
206 "compress/flate.byLiteral.Swap": true,
209 notInlinedReason := make(map[string]string)
210 pkgs := make([]string, 0, len(want))
211 for pname, fnames := range want {
212 pkgs = append(pkgs, pname)
213 for _, fname := range fnames {
214 fullName := pname + "." + fname
215 if _, ok := notInlinedReason[fullName]; ok {
216 t.Errorf("duplicate func: %s", fullName)
218 notInlinedReason[fullName] = "unknown reason"
222 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
223 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
227 cmdErr := make(chan error, 1)
232 scanner := bufio.NewScanner(pr)
234 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
235 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
236 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
238 line := scanner.Text()
239 if strings.HasPrefix(line, "# ") {
243 if m := haveInlined.FindStringSubmatch(line); m != nil {
245 delete(notInlinedReason, curPkg+"."+fname)
248 if m := canInline.FindStringSubmatch(line); m != nil {
250 fullname := curPkg + "." + fname
251 // If function must be inlined somewhere, being inlinable is not enough
252 if _, ok := must[fullname]; !ok {
253 delete(notInlinedReason, fullname)
257 if m := cannotInline.FindStringSubmatch(line); m != nil {
258 fname, reason := m[1], m[2]
259 fullName := curPkg + "." + fname
260 if _, ok := notInlinedReason[fullName]; ok {
261 // cmd/compile gave us a reason why
262 notInlinedReason[fullName] = reason
267 if err := <-cmdErr; err != nil {
270 if err := scanner.Err(); err != nil {
273 for fullName, reason := range notInlinedReason {
274 t.Errorf("%s was not inlined: %s", fullName, reason)