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