]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/test/inl_test.go
[dev.boringcrypto] all: merge master (2f0da6d) into dev.boringcrypto
[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 runtime 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                         "funcPC",
52                         "getArgInfoFast",
53                         "getm",
54                         "getMCache",
55                         "isDirectIface",
56                         "itabHashFunc",
57                         "noescape",
58                         "pcvalueCacheKey",
59                         "readUnaligned32",
60                         "readUnaligned64",
61                         "releasem",
62                         "roundupsize",
63                         "stackmapdata",
64                         "stringStructOf",
65                         "subtract1",
66                         "subtractb",
67                         "tophash",
68                         "totaldefersize",
69                         "(*bmap).keys",
70                         "(*bmap).overflow",
71                         "(*waitq).enqueue",
72
73                         // GC-related ones
74                         "cgoInRange",
75                         "gclinkptr.ptr",
76                         "guintptr.ptr",
77                         "heapBits.bits",
78                         "heapBits.isPointer",
79                         "heapBits.morePointers",
80                         "heapBits.next",
81                         "heapBitsForAddr",
82                         "markBits.isMarked",
83                         "muintptr.ptr",
84                         "puintptr.ptr",
85                         "spanOf",
86                         "spanOfUnchecked",
87                         "(*gcWork).putFast",
88                         "(*gcWork).tryGetFast",
89                         "(*guintptr).set",
90                         "(*markBits).advance",
91                         "(*mspan).allocBitsForIndex",
92                         "(*mspan).base",
93                         "(*mspan).markBitsForBase",
94                         "(*mspan).markBitsForIndex",
95                         "(*muintptr).set",
96                         "(*puintptr).set",
97                 },
98                 "runtime/internal/sys": {},
99                 "runtime/internal/math": {
100                         "MulUintptr",
101                 },
102                 "bytes": {
103                         "(*Buffer).Bytes",
104                         "(*Buffer).Cap",
105                         "(*Buffer).Len",
106                         "(*Buffer).Grow",
107                         "(*Buffer).Next",
108                         "(*Buffer).Read",
109                         "(*Buffer).ReadByte",
110                         "(*Buffer).Reset",
111                         "(*Buffer).String",
112                         "(*Buffer).UnreadByte",
113                         "(*Buffer).tryGrowByReslice",
114                 },
115                 "compress/flate": {
116                         "byLiteral.Len",
117                         "byLiteral.Less",
118                         "byLiteral.Swap",
119                         "(*dictDecoder).tryWriteCopy",
120                 },
121                 "encoding/base64": {
122                         "assemble32",
123                         "assemble64",
124                 },
125                 "unicode/utf8": {
126                         "FullRune",
127                         "FullRuneInString",
128                         "RuneLen",
129                         "ValidRune",
130                 },
131                 "reflect": {
132                         "Value.CanAddr",
133                         "Value.CanSet",
134                         "Value.CanInterface",
135                         "Value.IsValid",
136                         "Value.pointer",
137                         "add",
138                         "align",
139                         "flag.mustBe",
140                         "flag.mustBeAssignable",
141                         "flag.mustBeExported",
142                         "flag.kind",
143                         "flag.ro",
144                 },
145                 "regexp": {
146                         "(*bitState).push",
147                 },
148                 "math/big": {
149                         "bigEndianWord",
150                         // The following functions require the math_big_pure_go build tag.
151                         "addVW",
152                         "subVW",
153                 },
154                 "math/rand": {
155                         "(*rngSource).Int63",
156                         "(*rngSource).Uint64",
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                 // rotl_31 is only defined on 64-bit architectures
176                 want["runtime"] = append(want["runtime"], "rotl_31")
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 }