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",
132 "Value.CanInterface",
138 "flag.mustBeAssignable",
139 "flag.mustBeExported",
148 // The following functions require the math_big_pure_go build tag.
153 "(*rngSource).Int63",
154 "(*rngSource).Uint64",
157 "(*UDPConn).ReadFromUDP",
161 if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
162 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
163 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
164 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
165 // to inline (Issue 22239).
166 want["runtime"] = append(want["runtime"], "nextFreeFast")
168 if runtime.GOARCH != "386" {
169 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
170 // The same applies to Bswap32.
171 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
172 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
173 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
175 if bits.UintSize == 64 {
176 // mix is only defined on 64-bit architectures
177 want["runtime"] = append(want["runtime"], "mix")
180 switch runtime.GOARCH {
181 case "386", "wasm", "arm":
183 // TODO(mvdan): As explained in /test/inline_sync.go, some
184 // architectures don't have atomic intrinsics, so these go over
185 // the inlining budget. Move back to the main table once that
186 // problem is solved.
187 want["sync"] = []string{
191 "(*RWMutex).RUnlock",
196 // Functions that must actually be inlined; they must have actual callers.
197 must := map[string]bool{
198 "compress/flate.byLiteral.Len": true,
199 "compress/flate.byLiteral.Less": true,
200 "compress/flate.byLiteral.Swap": true,
203 notInlinedReason := make(map[string]string)
204 pkgs := make([]string, 0, len(want))
205 for pname, fnames := range want {
206 pkgs = append(pkgs, pname)
207 for _, fname := range fnames {
208 fullName := pname + "." + fname
209 if _, ok := notInlinedReason[fullName]; ok {
210 t.Errorf("duplicate func: %s", fullName)
212 notInlinedReason[fullName] = "unknown reason"
216 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
217 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
221 cmdErr := make(chan error, 1)
226 scanner := bufio.NewScanner(pr)
228 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
229 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
230 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
232 line := scanner.Text()
233 if strings.HasPrefix(line, "# ") {
237 if m := haveInlined.FindStringSubmatch(line); m != nil {
239 delete(notInlinedReason, curPkg+"."+fname)
242 if m := canInline.FindStringSubmatch(line); m != nil {
244 fullname := curPkg + "." + fname
245 // If function must be inlined somewhere, being inlinable is not enough
246 if _, ok := must[fullname]; !ok {
247 delete(notInlinedReason, fullname)
251 if m := cannotInline.FindStringSubmatch(line); m != nil {
252 fname, reason := m[1], m[2]
253 fullName := curPkg + "." + fname
254 if _, ok := notInlinedReason[fullName]; ok {
255 // cmd/compile gave us a reason why
256 notInlinedReason[fullName] = reason
261 if err := <-cmdErr; err != nil {
264 if err := scanner.Err(); err != nil {
267 for fullName, reason := range notInlinedReason {
268 t.Errorf("%s was not inlined: %s", fullName, reason)