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",
143 "flag.mustBeAssignable",
144 "flag.mustBeExported",
153 // The following functions require the math_big_pure_go build tag.
158 "(*rngSource).Int63",
159 "(*rngSource).Uint64",
162 "(*UDPConn).ReadFromUDP",
166 if runtime.GOARCH != "386" && runtime.GOARCH != "loong64" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
167 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
168 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
169 // On loong64, mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
170 // to inline (Issue 22239).
171 want["runtime"] = append(want["runtime"], "nextFreeFast")
173 if runtime.GOARCH != "386" {
174 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
175 // The same applies to Bswap32.
176 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
177 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
178 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
180 if bits.UintSize == 64 {
181 // mix is only defined on 64-bit architectures
182 want["runtime"] = append(want["runtime"], "mix")
185 switch runtime.GOARCH {
186 case "386", "wasm", "arm":
188 // TODO(mvdan): As explained in /test/inline_sync.go, some
189 // architectures don't have atomic intrinsics, so these go over
190 // the inlining budget. Move back to the main table once that
191 // problem is solved.
192 want["sync"] = []string{
196 "(*RWMutex).RUnlock",
201 // Functions that must actually be inlined; they must have actual callers.
202 must := map[string]bool{
203 "compress/flate.byLiteral.Len": true,
204 "compress/flate.byLiteral.Less": true,
205 "compress/flate.byLiteral.Swap": true,
208 notInlinedReason := make(map[string]string)
209 pkgs := make([]string, 0, len(want))
210 for pname, fnames := range want {
211 pkgs = append(pkgs, pname)
212 for _, fname := range fnames {
213 fullName := pname + "." + fname
214 if _, ok := notInlinedReason[fullName]; ok {
215 t.Errorf("duplicate func: %s", fullName)
217 notInlinedReason[fullName] = "unknown reason"
221 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
222 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
226 cmdErr := make(chan error, 1)
231 scanner := bufio.NewScanner(pr)
233 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
234 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
235 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
237 line := scanner.Text()
238 if strings.HasPrefix(line, "# ") {
242 if m := haveInlined.FindStringSubmatch(line); m != nil {
244 delete(notInlinedReason, curPkg+"."+fname)
247 if m := canInline.FindStringSubmatch(line); m != nil {
249 fullname := curPkg + "." + fname
250 // If function must be inlined somewhere, being inlinable is not enough
251 if _, ok := must[fullname]; !ok {
252 delete(notInlinedReason, fullname)
256 if m := cannotInline.FindStringSubmatch(line); m != nil {
257 fname, reason := m[1], m[2]
258 fullName := curPkg + "." + fname
259 if _, ok := notInlinedReason[fullName]; ok {
260 // cmd/compile gave us a reason why
261 notInlinedReason[fullName] = reason
266 if err := <-cmdErr; err != nil {
269 if err := scanner.Err(); err != nil {
272 for fullName, reason := range notInlinedReason {
273 t.Errorf("%s was not inlined: %s", fullName, reason)