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{
78 "heapBits.morePointers",
87 "(*gcWork).tryGetFast",
89 "(*markBits).advance",
90 "(*mspan).allocBitsForIndex",
92 "(*mspan).markBitsForBase",
93 "(*mspan).markBitsForIndex",
97 "runtime/internal/sys": {},
98 "runtime/internal/math": {
108 "(*Buffer).ReadByte",
111 "(*Buffer).UnreadByte",
112 "(*Buffer).tryGrowByReslice",
118 "(*dictDecoder).tryWriteCopy",
133 "Value.CanInterface",
139 "flag.mustBeAssignable",
140 "flag.mustBeExported",
149 // The following functions require the math_big_pure_go build tag.
154 "(*rngSource).Int63",
155 "(*rngSource).Uint64",
158 "(*UDPConn).ReadFromUDP",
162 if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
163 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
164 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
165 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
166 // to inline (Issue 22239).
167 want["runtime"] = append(want["runtime"], "nextFreeFast")
169 if runtime.GOARCH != "386" {
170 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
171 // The same applies to Bswap32.
172 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
173 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
174 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
176 if bits.UintSize == 64 {
177 // mix is only defined on 64-bit architectures
178 want["runtime"] = append(want["runtime"], "mix")
181 switch runtime.GOARCH {
182 case "386", "wasm", "arm":
184 // TODO(mvdan): As explained in /test/inline_sync.go, some
185 // architectures don't have atomic intrinsics, so these go over
186 // the inlining budget. Move back to the main table once that
187 // problem is solved.
188 want["sync"] = []string{
192 "(*RWMutex).RUnlock",
197 // Functions that must actually be inlined; they must have actual callers.
198 must := map[string]bool{
199 "compress/flate.byLiteral.Len": true,
200 "compress/flate.byLiteral.Less": true,
201 "compress/flate.byLiteral.Swap": true,
204 notInlinedReason := make(map[string]string)
205 pkgs := make([]string, 0, len(want))
206 for pname, fnames := range want {
207 pkgs = append(pkgs, pname)
208 for _, fname := range fnames {
209 fullName := pname + "." + fname
210 if _, ok := notInlinedReason[fullName]; ok {
211 t.Errorf("duplicate func: %s", fullName)
213 notInlinedReason[fullName] = "unknown reason"
217 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
218 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
222 cmdErr := make(chan error, 1)
227 scanner := bufio.NewScanner(pr)
229 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
230 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
231 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
233 line := scanner.Text()
234 if strings.HasPrefix(line, "# ") {
238 if m := haveInlined.FindStringSubmatch(line); m != nil {
240 delete(notInlinedReason, curPkg+"."+fname)
243 if m := canInline.FindStringSubmatch(line); m != nil {
245 fullname := curPkg + "." + fname
246 // If function must be inlined somewhere, being inlinable is not enough
247 if _, ok := must[fullname]; !ok {
248 delete(notInlinedReason, fullname)
252 if m := cannotInline.FindStringSubmatch(line); m != nil {
253 fname, reason := m[1], m[2]
254 fullName := curPkg + "." + fname
255 if _, ok := notInlinedReason[fullName]; ok {
256 // cmd/compile gave us a reason why
257 notInlinedReason[fullName] = reason
262 if err := <-cmdErr; err != nil {
265 if err := scanner.Err(); err != nil {
268 for fullName, reason := range notInlinedReason {
269 t.Errorf("%s was not inlined: %s", fullName, reason)