]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/inline/inlheur/callsite.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / src / cmd / compile / internal / inline / inlheur / callsite.go
1 // Copyright 2023 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 inlheur
6
7 import (
8         "cmd/compile/internal/base"
9         "cmd/compile/internal/ir"
10         "cmd/internal/src"
11         "fmt"
12         "io"
13         "path/filepath"
14         "sort"
15         "strings"
16 )
17
18 // CallSite records useful information about a potentially inlinable
19 // (direct) function call. "Callee" is the target of the call, "Call"
20 // is the ir node corresponding to the call itself, "Assign" is
21 // the top-level assignment statement containing the call (if the call
22 // appears in the form of a top-level statement, e.g. "x := foo()"),
23 // "Flags" contains properties of the call that might be useful for
24 // making inlining decisions, "Score" is the final score assigned to
25 // the site, and "ID" is a numeric ID for the site within its
26 // containing function.
27 type CallSite struct {
28         Callee    *ir.Func
29         Call      *ir.CallExpr
30         parent    *CallSite
31         Assign    ir.Node
32         Flags     CSPropBits
33         Score     int
34         ScoreMask scoreAdjustTyp
35         ID        uint
36         aux       uint8
37 }
38
39 // CallSiteTab is a table of call sites, keyed by call expr.
40 // Ideally it would be nice to key the table by src.XPos, but
41 // this results in collisions for calls on very long lines (the
42 // front end saturates column numbers at 255). We also wind up
43 // with many calls that share the same auto-generated pos.
44 type CallSiteTab map[*ir.CallExpr]*CallSite
45
46 type CSPropBits uint32
47
48 const (
49         CallSiteInLoop CSPropBits = 1 << iota
50         CallSiteOnPanicPath
51         CallSiteInInitFunc
52 )
53
54 type csAuxBits uint8
55
56 const (
57         csAuxInlined = 1 << iota
58 )
59
60 // encodedCallSiteTab is a table keyed by "encoded" callsite
61 // (stringified src.XPos plus call site ID) mapping to a value of call
62 // property bits and score.
63 type encodedCallSiteTab map[string]propsAndScore
64
65 type propsAndScore struct {
66         props CSPropBits
67         score int
68         mask  scoreAdjustTyp
69 }
70
71 func (pas propsAndScore) String() string {
72         return fmt.Sprintf("P=%s|S=%d|M=%s", pas.props.String(),
73                 pas.score, pas.mask.String())
74 }
75
76 func (cst CallSiteTab) merge(other CallSiteTab) error {
77         for k, v := range other {
78                 if prev, ok := cst[k]; ok {
79                         return fmt.Errorf("internal error: collision during call site table merge, fn=%s callsite=%s", prev.Callee.Sym().Name, fmtFullPos(prev.Call.Pos()))
80                 }
81                 cst[k] = v
82         }
83         return nil
84 }
85
86 func fmtFullPos(p src.XPos) string {
87         var sb strings.Builder
88         sep := ""
89         base.Ctxt.AllPos(p, func(pos src.Pos) {
90                 fmt.Fprintf(&sb, sep)
91                 sep = "|"
92                 file := filepath.Base(pos.Filename())
93                 fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
94         })
95         return sb.String()
96 }
97
98 func EncodeCallSiteKey(cs *CallSite) string {
99         var sb strings.Builder
100         // FIXME: maybe rewrite line offsets relative to function start?
101         sb.WriteString(fmtFullPos(cs.Call.Pos()))
102         fmt.Fprintf(&sb, "|%d", cs.ID)
103         return sb.String()
104 }
105
106 func buildEncodedCallSiteTab(tab CallSiteTab) encodedCallSiteTab {
107         r := make(encodedCallSiteTab)
108         for _, cs := range tab {
109                 k := EncodeCallSiteKey(cs)
110                 r[k] = propsAndScore{
111                         props: cs.Flags,
112                         score: cs.Score,
113                         mask:  cs.ScoreMask,
114                 }
115         }
116         return r
117 }
118
119 // dumpCallSiteComments emits comments into the dump file for the
120 // callsites in the function of interest. If "ecst" is non-nil, we use
121 // that, otherwise generated a fresh encodedCallSiteTab from "tab".
122 func dumpCallSiteComments(w io.Writer, tab CallSiteTab, ecst encodedCallSiteTab) {
123         if ecst == nil {
124                 ecst = buildEncodedCallSiteTab(tab)
125         }
126         tags := make([]string, 0, len(ecst))
127         for k := range ecst {
128                 tags = append(tags, k)
129         }
130         sort.Strings(tags)
131         for _, s := range tags {
132                 v := ecst[s]
133                 fmt.Fprintf(w, "// callsite: %s flagstr %q flagval %d score %d mask %d maskstr %q\n", s, v.props.String(), v.props, v.score, v.mask, v.mask.String())
134         }
135         fmt.Fprintf(w, "// %s\n", csDelimiter)
136 }