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