]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/test/inl_test.go
[dev.typeparams] runtime: remove variadic defer/go calls
[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                         "deferclass",
46                         "evacuated",
47                         "fastlog2",
48                         "fastrand",
49                         "float64bits",
50                         "getArgInfoFast",
51                         "getm",
52                         "getMCache",
53                         "isDirectIface",
54                         "itabHashFunc",
55                         "noescape",
56                         "pcvalueCacheKey",
57                         "readUnaligned32",
58                         "readUnaligned64",
59                         "releasem",
60                         "roundupsize",
61                         "stackmapdata",
62                         "stringStructOf",
63                         "subtract1",
64                         "subtractb",
65                         "tophash",
66                         "totaldefersize",
67                         "(*bmap).keys",
68                         "(*bmap).overflow",
69                         "(*waitq).enqueue",
70
71                         // GC-related ones
72                         "cgoInRange",
73                         "gclinkptr.ptr",
74                         "guintptr.ptr",
75                         "heapBits.bits",
76                         "heapBits.isPointer",
77                         "heapBits.morePointers",
78                         "heapBits.next",
79                         "heapBitsForAddr",
80                         "markBits.isMarked",
81                         "muintptr.ptr",
82                         "puintptr.ptr",
83                         "spanOf",
84                         "spanOfUnchecked",
85                         "(*gcWork).putFast",
86                         "(*gcWork).tryGetFast",
87                         "(*guintptr).set",
88                         "(*markBits).advance",
89                         "(*mspan).allocBitsForIndex",
90                         "(*mspan).base",
91                         "(*mspan).markBitsForBase",
92                         "(*mspan).markBitsForIndex",
93                         "(*muintptr).set",
94                         "(*puintptr).set",
95                 },
96                 "runtime/internal/sys": {},
97                 "runtime/internal/math": {
98                         "MulUintptr",
99                 },
100                 "bytes": {
101                         "(*Buffer).Bytes",
102                         "(*Buffer).Cap",
103                         "(*Buffer).Len",
104                         "(*Buffer).Grow",
105                         "(*Buffer).Next",
106                         "(*Buffer).Read",
107                         "(*Buffer).ReadByte",
108                         "(*Buffer).Reset",
109                         "(*Buffer).String",
110                         "(*Buffer).UnreadByte",
111                         "(*Buffer).tryGrowByReslice",
112                 },
113                 "compress/flate": {
114                         "byLiteral.Len",
115                         "byLiteral.Less",
116                         "byLiteral.Swap",
117                         "(*dictDecoder).tryWriteCopy",
118                 },
119                 "encoding/base64": {
120                         "assemble32",
121                         "assemble64",
122                 },
123                 "unicode/utf8": {
124                         "FullRune",
125                         "FullRuneInString",
126                         "RuneLen",
127                         "ValidRune",
128                 },
129                 "reflect": {
130                         "Value.CanAddr",
131                         "Value.CanSet",
132                         "Value.CanInterface",
133                         "Value.IsValid",
134                         "Value.pointer",
135                         "add",
136                         "align",
137                         "flag.mustBe",
138                         "flag.mustBeAssignable",
139                         "flag.mustBeExported",
140                         "flag.kind",
141                         "flag.ro",
142                 },
143                 "regexp": {
144                         "(*bitState).push",
145                 },
146                 "math/big": {
147                         "bigEndianWord",
148                         // The following functions require the math_big_pure_go build tag.
149                         "addVW",
150                         "subVW",
151                 },
152                 "math/rand": {
153                         "(*rngSource).Int63",
154                         "(*rngSource).Uint64",
155                 },
156                 "net": {
157                         "(*UDPConn).ReadFromUDP",
158                 },
159         }
160
161         if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
162                 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
163                 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
164                 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
165                 // to inline (Issue 22239).
166                 want["runtime"] = append(want["runtime"], "nextFreeFast")
167         }
168         if runtime.GOARCH != "386" {
169                 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
170                 // The same applies to Bswap32.
171                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
172                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
173                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
174         }
175         if bits.UintSize == 64 {
176                 // mix is only defined on 64-bit architectures
177                 want["runtime"] = append(want["runtime"], "mix")
178         }
179
180         switch runtime.GOARCH {
181         case "386", "wasm", "arm":
182         default:
183                 // TODO(mvdan): As explained in /test/inline_sync.go, some
184                 // architectures don't have atomic intrinsics, so these go over
185                 // the inlining budget. Move back to the main table once that
186                 // problem is solved.
187                 want["sync"] = []string{
188                         "(*Mutex).Lock",
189                         "(*Mutex).Unlock",
190                         "(*RWMutex).RLock",
191                         "(*RWMutex).RUnlock",
192                         "(*Once).Do",
193                 }
194         }
195
196         // Functions that must actually be inlined; they must have actual callers.
197         must := map[string]bool{
198                 "compress/flate.byLiteral.Len":  true,
199                 "compress/flate.byLiteral.Less": true,
200                 "compress/flate.byLiteral.Swap": true,
201         }
202
203         notInlinedReason := make(map[string]string)
204         pkgs := make([]string, 0, len(want))
205         for pname, fnames := range want {
206                 pkgs = append(pkgs, pname)
207                 for _, fname := range fnames {
208                         fullName := pname + "." + fname
209                         if _, ok := notInlinedReason[fullName]; ok {
210                                 t.Errorf("duplicate func: %s", fullName)
211                         }
212                         notInlinedReason[fullName] = "unknown reason"
213                 }
214         }
215
216         args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
217         cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
218         pr, pw := io.Pipe()
219         cmd.Stdout = pw
220         cmd.Stderr = pw
221         cmdErr := make(chan error, 1)
222         go func() {
223                 cmdErr <- cmd.Run()
224                 pw.Close()
225         }()
226         scanner := bufio.NewScanner(pr)
227         curPkg := ""
228         canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
229         haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
230         cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
231         for scanner.Scan() {
232                 line := scanner.Text()
233                 if strings.HasPrefix(line, "# ") {
234                         curPkg = line[2:]
235                         continue
236                 }
237                 if m := haveInlined.FindStringSubmatch(line); m != nil {
238                         fname := m[1]
239                         delete(notInlinedReason, curPkg+"."+fname)
240                         continue
241                 }
242                 if m := canInline.FindStringSubmatch(line); m != nil {
243                         fname := m[1]
244                         fullname := curPkg + "." + fname
245                         // If function must be inlined somewhere, being inlinable is not enough
246                         if _, ok := must[fullname]; !ok {
247                                 delete(notInlinedReason, fullname)
248                                 continue
249                         }
250                 }
251                 if m := cannotInline.FindStringSubmatch(line); m != nil {
252                         fname, reason := m[1], m[2]
253                         fullName := curPkg + "." + fname
254                         if _, ok := notInlinedReason[fullName]; ok {
255                                 // cmd/compile gave us a reason why
256                                 notInlinedReason[fullName] = reason
257                         }
258                         continue
259                 }
260         }
261         if err := <-cmdErr; err != nil {
262                 t.Fatal(err)
263         }
264         if err := scanner.Err(); err != nil {
265                 t.Fatal(err)
266         }
267         for fullName, reason := range notInlinedReason {
268                 t.Errorf("%s was not inlined: %s", fullName, reason)
269         }
270 }