]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/test/inl_test.go
[dev.typeparams] all: merge master (ecaa681) 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                         "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                         "ValidRune",
126                 },
127                 "reflect": {
128                         "Value.CanAddr",
129                         "Value.CanSet",
130                         "Value.CanInterface",
131                         "Value.IsValid",
132                         "Value.pointer",
133                         "add",
134                         "align",
135                         "flag.mustBe",
136                         "flag.mustBeAssignable",
137                         "flag.mustBeExported",
138                         "flag.kind",
139                         "flag.ro",
140                 },
141                 "regexp": {
142                         "(*bitState).push",
143                 },
144                 "math/big": {
145                         "bigEndianWord",
146                         // The following functions require the math_big_pure_go build tag.
147                         "addVW",
148                         "subVW",
149                 },
150                 "math/rand": {
151                         "(*rngSource).Int63",
152                         "(*rngSource).Uint64",
153                 },
154                 "net": {
155                         "(*UDPConn).ReadFromUDP",
156                 },
157         }
158
159         if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" && runtime.GOARCH != "riscv64" {
160                 // nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
161                 // We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
162                 // On mips64x and riscv64, Ctz64 is not intrinsified and causes nextFreeFast too expensive
163                 // to inline (Issue 22239).
164                 want["runtime"] = append(want["runtime"], "nextFreeFast")
165         }
166         if runtime.GOARCH != "386" {
167                 // As explained above, Ctz64 and Ctz32 are not Go code on 386.
168                 // The same applies to Bswap32.
169                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
170                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
171                 want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
172         }
173         if bits.UintSize == 64 {
174                 // mix is only defined on 64-bit architectures
175                 want["runtime"] = append(want["runtime"], "mix")
176         }
177
178         switch runtime.GOARCH {
179         case "386", "wasm", "arm":
180         default:
181                 // TODO(mvdan): As explained in /test/inline_sync.go, some
182                 // architectures don't have atomic intrinsics, so these go over
183                 // the inlining budget. Move back to the main table once that
184                 // problem is solved.
185                 want["sync"] = []string{
186                         "(*Mutex).Lock",
187                         "(*Mutex).Unlock",
188                         "(*RWMutex).RLock",
189                         "(*RWMutex).RUnlock",
190                         "(*Once).Do",
191                 }
192         }
193
194         // Functions that must actually be inlined; they must have actual callers.
195         must := map[string]bool{
196                 "compress/flate.byLiteral.Len":  true,
197                 "compress/flate.byLiteral.Less": true,
198                 "compress/flate.byLiteral.Swap": true,
199         }
200
201         notInlinedReason := make(map[string]string)
202         pkgs := make([]string, 0, len(want))
203         for pname, fnames := range want {
204                 pkgs = append(pkgs, pname)
205                 for _, fname := range fnames {
206                         fullName := pname + "." + fname
207                         if _, ok := notInlinedReason[fullName]; ok {
208                                 t.Errorf("duplicate func: %s", fullName)
209                         }
210                         notInlinedReason[fullName] = "unknown reason"
211                 }
212         }
213
214         args := append([]string{"build", "-a", "-gcflags=all=-m -m", "-tags=math_big_pure_go"}, pkgs...)
215         cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
216         pr, pw := io.Pipe()
217         cmd.Stdout = pw
218         cmd.Stderr = pw
219         cmdErr := make(chan error, 1)
220         go func() {
221                 cmdErr <- cmd.Run()
222                 pw.Close()
223         }()
224         scanner := bufio.NewScanner(pr)
225         curPkg := ""
226         canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
227         haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
228         cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
229         for scanner.Scan() {
230                 line := scanner.Text()
231                 if strings.HasPrefix(line, "# ") {
232                         curPkg = line[2:]
233                         continue
234                 }
235                 if m := haveInlined.FindStringSubmatch(line); m != nil {
236                         fname := m[1]
237                         delete(notInlinedReason, curPkg+"."+fname)
238                         continue
239                 }
240                 if m := canInline.FindStringSubmatch(line); m != nil {
241                         fname := m[1]
242                         fullname := curPkg + "." + fname
243                         // If function must be inlined somewhere, being inlinable is not enough
244                         if _, ok := must[fullname]; !ok {
245                                 delete(notInlinedReason, fullname)
246                                 continue
247                         }
248                 }
249                 if m := cannotInline.FindStringSubmatch(line); m != nil {
250                         fname, reason := m[1], m[2]
251                         fullName := curPkg + "." + fname
252                         if _, ok := notInlinedReason[fullName]; ok {
253                                 // cmd/compile gave us a reason why
254                                 notInlinedReason[fullName] = reason
255                         }
256                         continue
257                 }
258         }
259         if err := <-cmdErr; err != nil {
260                 t.Fatal(err)
261         }
262         if err := scanner.Err(); err != nil {
263                 t.Fatal(err)
264         }
265         for fullName, reason := range notInlinedReason {
266                 t.Errorf("%s was not inlined: %s", fullName, reason)
267         }
268 }