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 "heapBits.morePointers",
84 "(*gcWork).tryGetFast",
86 "(*markBits).advance",
87 "(*mspan).allocBitsForIndex",
89 "(*mspan).markBitsForBase",
90 "(*mspan).markBitsForIndex",
94 "runtime/internal/sys": {},
95 "runtime/internal/math": {
105 "(*Buffer).ReadByte",
108 "(*Buffer).UnreadByte",
109 "(*Buffer).tryGrowByReslice",
115 "(*dictDecoder).tryWriteCopy",
131 "Value.CanInterface",
137 "flag.mustBeAssignable",
138 "flag.mustBeExported",
147 // The following functions require the math_big_pure_go build tag.
152 "(*rngSource).Int63",
153 "(*rngSource).Uint64",
156 "(*UDPConn).ReadFromUDP",
160 if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
161 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
162 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
163 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
164 // to inline (Issue 22239).
165 want["runtime"] = append(want["runtime"], "nextFreeFast")
167 if runtime.GOARCH != "386" {
168 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
169 // The same applies to Bswap32.
170 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
171 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
172 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
174 if bits.UintSize == 64 {
175 // mix is only defined on 64-bit architectures
176 want["runtime"] = append(want["runtime"], "mix")
179 switch runtime.GOARCH {
180 case "386", "wasm", "arm":
182 // TODO(mvdan): As explained in /test/inline_sync.go, some
183 // architectures don't have atomic intrinsics, so these go over
184 // the inlining budget. Move back to the main table once that
185 // problem is solved.
186 want["sync"] = []string{
190 "(*RWMutex).RUnlock",
195 // Functions that must actually be inlined; they must have actual callers.
196 must := map[string]bool{
197 "compress/flate.byLiteral.Len": true,
198 "compress/flate.byLiteral.Less": true,
199 "compress/flate.byLiteral.Swap": true,
202 notInlinedReason := make(map[string]string)
203 pkgs := make([]string, 0, len(want))
204 for pname, fnames := range want {
205 pkgs = append(pkgs, pname)
206 for _, fname := range fnames {
207 fullName := pname + "." + fname
208 if _, ok := notInlinedReason[fullName]; ok {
209 t.Errorf("duplicate func: %s", fullName)
211 notInlinedReason[fullName] = "unknown reason"
215 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
216 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
220 cmdErr := make(chan error, 1)
225 scanner := bufio.NewScanner(pr)
227 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
228 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
229 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
231 line := scanner.Text()
232 if strings.HasPrefix(line, "# ") {
236 if m := haveInlined.FindStringSubmatch(line); m != nil {
238 delete(notInlinedReason, curPkg+"."+fname)
241 if m := canInline.FindStringSubmatch(line); m != nil {
243 fullname := curPkg + "." + fname
244 // If function must be inlined somewhere, being inlinable is not enough
245 if _, ok := must[fullname]; !ok {
246 delete(notInlinedReason, fullname)
250 if m := cannotInline.FindStringSubmatch(line); m != nil {
251 fname, reason := m[1], m[2]
252 fullName := curPkg + "." + fname
253 if _, ok := notInlinedReason[fullName]; ok {
254 // cmd/compile gave us a reason why
255 notInlinedReason[fullName] = reason
260 if err := <-cmdErr; err != nil {
263 if err := scanner.Err(); err != nil {
266 for fullName, reason := range notInlinedReason {
267 t.Errorf("%s was not inlined: %s", fullName, reason)