]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/test/inl_test.go
[dev.typeparams] all: merge master (8212707) into dev.typeparams
[gostls13.git] / src / cmd / compile / internal / test / inl_test.go
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.
4
5 package test
6
7 import (
8         "bufio"
9         "internal/testenv"
10         "io"
11         "math/bits"
12         "os/exec"
13         "regexp"
14         "runtime"
15         "strings"
16         "testing"
17 )
18
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")
25         }
26         testenv.MustHaveGoRun(t)
27         t.Parallel()
28
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{
33                 "runtime": {
34                         "add",
35                         "acquirem",
36                         "add1",
37                         "addb",
38                         "adjustpanics",
39                         "adjustpointer",
40                         "alignDown",
41                         "alignUp",
42                         "bucketMask",
43                         "bucketShift",
44                         "chanbuf",
45                         "deferArgs",
46                         "deferclass",
47                         "evacuated",
48                         "fastlog2",
49                         "fastrand",
50                         "float64bits",
51                         "getArgInfoFast",
52                         "getm",
53                         "getMCache",
54                         "isDirectIface",
55                         "itabHashFunc",
56                         "noescape",
57                         "pcvalueCacheKey",
58                         "readUnaligned32",
59                         "readUnaligned64",
60                         "releasem",
61                         "roundupsize",
62                         "stackmapdata",
63                         "stringStructOf",
64                         "subtract1",
65                         "subtractb",
66                         "tophash",
67                         "totaldefersize",
68                         "(*bmap).keys",
69                         "(*bmap).overflow",
70                         "(*waitq).enqueue",
71
72                         // GC-related ones
73                         "cgoInRange",
74                         "gclinkptr.ptr",
75                         "guintptr.ptr",
76                         "heapBits.bits",
77                         "heapBits.isPointer",
78                         "heapBits.morePointers",
79                         "heapBits.next",
80                         "heapBitsForAddr",
81                         "markBits.isMarked",
82                         "muintptr.ptr",
83                         "puintptr.ptr",
84                         "spanOf",
85                         "spanOfUnchecked",
86                         "(*gcWork).putFast",
87                         "(*gcWork).tryGetFast",
88                         "(*guintptr).set",
89                         "(*markBits).advance",
90                         "(*mspan).allocBitsForIndex",
91                         "(*mspan).base",
92                         "(*mspan).markBitsForBase",
93                         "(*mspan).markBitsForIndex",
94                         "(*muintptr).set",
95                         "(*puintptr).set",
96                 },
97                 "runtime/internal/sys": {},
98                 "runtime/internal/math": {
99                         "MulUintptr",
100                 },
101                 "bytes": {
102                         "(*Buffer).Bytes",
103                         "(*Buffer).Cap",
104                         "(*Buffer).Len",
105                         "(*Buffer).Grow",
106                         "(*Buffer).Next",
107                         "(*Buffer).Read",
108                         "(*Buffer).ReadByte",
109                         "(*Buffer).Reset",
110                         "(*Buffer).String",
111                         "(*Buffer).UnreadByte",
112                         "(*Buffer).tryGrowByReslice",
113                 },
114                 "compress/flate": {
115                         "byLiteral.Len",
116                         "byLiteral.Less",
117                         "byLiteral.Swap",
118                         "(*dictDecoder).tryWriteCopy",
119                 },
120                 "encoding/base64": {
121                         "assemble32",
122                         "assemble64",
123                 },
124                 "unicode/utf8": {
125                         "FullRune",
126                         "FullRuneInString",
127                         "RuneLen",
128                         "ValidRune",
129                 },
130                 "reflect": {
131                         "Value.CanAddr",
132                         "Value.CanSet",
133                         "Value.CanInterface",
134                         "Value.IsValid",
135                         "Value.pointer",
136                         "add",
137                         "align",
138                         "flag.mustBe",
139                         "flag.mustBeAssignable",
140                         "flag.mustBeExported",
141                         "flag.kind",
142                         "flag.ro",
143                 },
144                 "regexp": {
145                         "(*bitState).push",
146                 },
147                 "math/big": {
148                         "bigEndianWord",
149                         // The following functions require the math_big_pure_go build tag.
150                         "addVW",
151                         "subVW",
152                 },
153                 "math/rand": {
154                         "(*rngSource).Int63",
155                         "(*rngSource).Uint64",
156                 },
157                 "net": {
158                         "(*UDPConn).ReadFromUDP",
159                 },
160         }
161
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")
168         }
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")
175         }
176         if bits.UintSize == 64 {
177                 // mix is only defined on 64-bit architectures
178                 want["runtime"] = append(want["runtime"], "mix")
179         }
180
181         switch runtime.GOARCH {
182         case "386", "wasm", "arm":
183         default:
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{
189                         "(*Mutex).Lock",
190                         "(*Mutex).Unlock",
191                         "(*RWMutex).RLock",
192                         "(*RWMutex).RUnlock",
193                         "(*Once).Do",
194                 }
195         }
196
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,
202         }
203
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)
212                         }
213                         notInlinedReason[fullName] = "unknown reason"
214                 }
215         }
216
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...))
219         pr, pw := io.Pipe()
220         cmd.Stdout = pw
221         cmd.Stderr = pw
222         cmdErr := make(chan error, 1)
223         go func() {
224                 cmdErr <- cmd.Run()
225                 pw.Close()
226         }()
227         scanner := bufio.NewScanner(pr)
228         curPkg := ""
229         canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
230         haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
231         cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
232         for scanner.Scan() {
233                 line := scanner.Text()
234                 if strings.HasPrefix(line, "# ") {
235                         curPkg = line[2:]
236                         continue
237                 }
238                 if m := haveInlined.FindStringSubmatch(line); m != nil {
239                         fname := m[1]
240                         delete(notInlinedReason, curPkg+"."+fname)
241                         continue
242                 }
243                 if m := canInline.FindStringSubmatch(line); m != nil {
244                         fname := m[1]
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)
249                                 continue
250                         }
251                 }
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
258                         }
259                         continue
260                 }
261         }
262         if err := <-cmdErr; err != nil {
263                 t.Fatal(err)
264         }
265         if err := scanner.Err(); err != nil {
266                 t.Fatal(err)
267         }
268         for fullName, reason := range notInlinedReason {
269                 t.Errorf("%s was not inlined: %s", fullName, reason)
270         }
271 }