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",
130 "Value.CanInterface",
136 "flag.mustBeAssignable",
137 "flag.mustBeExported",
146 // The following functions require the math_big_pure_go build tag.
151 "(*rngSource).Int63",
152 "(*rngSource).Uint64",
155 "(*UDPConn).ReadFromUDP",
159 if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
160 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
161 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
162 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
163 // to inline (Issue 22239).
164 want["runtime"] = append(want["runtime"], "nextFreeFast")
166 if runtime.GOARCH != "386" {
167 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
168 // The same applies to Bswap32.
169 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
170 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
171 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
173 if bits.UintSize == 64 {
174 // mix is only defined on 64-bit architectures
175 want["runtime"] = append(want["runtime"], "mix")
178 switch runtime.GOARCH {
179 case "386", "wasm", "arm":
181 // TODO(mvdan): As explained in /test/inline_sync.go, some
182 // architectures don't have atomic intrinsics, so these go over
183 // the inlining budget. Move back to the main table once that
184 // problem is solved.
185 want["sync"] = []string{
189 "(*RWMutex).RUnlock",
194 // Functions that must actually be inlined; they must have actual callers.
195 must := map[string]bool{
196 "compress/flate.byLiteral.Len": true,
197 "compress/flate.byLiteral.Less": true,
198 "compress/flate.byLiteral.Swap": true,
201 notInlinedReason := make(map[string]string)
202 pkgs := make([]string, 0, len(want))
203 for pname, fnames := range want {
204 pkgs = append(pkgs, pname)
205 for _, fname := range fnames {
206 fullName := pname + "." + fname
207 if _, ok := notInlinedReason[fullName]; ok {
208 t.Errorf("duplicate func: %s", fullName)
210 notInlinedReason[fullName] = "unknown reason"
214 args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
215 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
219 cmdErr := make(chan error, 1)
224 scanner := bufio.NewScanner(pr)
226 canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
227 haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
228 cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
230 line := scanner.Text()
231 if strings.HasPrefix(line, "# ") {
235 if m := haveInlined.FindStringSubmatch(line); m != nil {
237 delete(notInlinedReason, curPkg+"."+fname)
240 if m := canInline.FindStringSubmatch(line); m != nil {
242 fullname := curPkg + "." + fname
243 // If function must be inlined somewhere, being inlinable is not enough
244 if _, ok := must[fullname]; !ok {
245 delete(notInlinedReason, fullname)
249 if m := cannotInline.FindStringSubmatch(line); m != nil {
250 fname, reason := m[1], m[2]
251 fullName := curPkg + "." + fname
252 if _, ok := notInlinedReason[fullName]; ok {
253 // cmd/compile gave us a reason why
254 notInlinedReason[fullName] = reason
259 if err := <-cmdErr; err != nil {
262 if err := scanner.Err(); err != nil {
265 for fullName, reason := range notInlinedReason {
266 t.Errorf("%s was not inlined: %s", fullName, reason)