]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/inline/inlheur/callsite.go
7a1830fd68a335125ca31a5cf95fdab94ced115e
[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         Assign    ir.Node
31         Flags     CSPropBits
32         Score     int
33         ScoreMask scoreAdjustTyp
34         ID        uint
35 }
36
37 // CallSiteTab is a table of call sites, keyed by call expr.
38 // Ideally it would be nice to key the table by src.XPos, but
39 // this results in collisions for calls on very long lines (the
40 // front end saturates column numbers at 255). We also wind up
41 // with many calls that share the same auto-generated pos.
42 type CallSiteTab map[*ir.CallExpr]*CallSite
43
44 func GetCallSiteScore(fn *ir.Func, call *ir.CallExpr) (int, bool) {
45         if funcInlHeur, ok := fpmap[fn]; !ok {
46                 return 0, false
47         } else {
48                 cs, ok := funcInlHeur.cstab[call]
49                 if !ok {
50                         return 0, false
51                 }
52                 return cs.Score, true
53         }
54 }
55
56 type CSPropBits uint32
57
58 const (
59         CallSiteInLoop CSPropBits = 1 << iota
60         CallSiteOnPanicPath
61         CallSiteInInitFunc
62 )
63
64 // encodedCallSiteTab is a table keyed by "encoded" callsite
65 // (stringified src.XPos plus call site ID) mapping to a value of call
66 // property bits and score.
67 type encodedCallSiteTab map[string]propsAndScore
68
69 type propsAndScore struct {
70         props CSPropBits
71         score int
72         mask  scoreAdjustTyp
73 }
74
75 func (pas propsAndScore) String() string {
76         return fmt.Sprintf("P=%s|S=%d|M=%s", pas.props.String(),
77                 pas.score, pas.mask.String())
78 }
79
80 func (cst CallSiteTab) merge(other CallSiteTab) error {
81         for k, v := range other {
82                 if prev, ok := cst[k]; ok {
83                         return fmt.Errorf("internal error: collision during call site table merge, fn=%s callsite=%s", prev.Callee.Sym().Name, fmtFullPos(prev.Call.Pos()))
84                 }
85                 cst[k] = v
86         }
87         return nil
88 }
89
90 func fmtFullPos(p src.XPos) string {
91         var sb strings.Builder
92         sep := ""
93         base.Ctxt.AllPos(p, func(pos src.Pos) {
94                 fmt.Fprintf(&sb, sep)
95                 sep = "|"
96                 file := filepath.Base(pos.Filename())
97                 fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
98         })
99         return sb.String()
100 }
101
102 func EncodeCallSiteKey(cs *CallSite) string {
103         var sb strings.Builder
104         // FIXME: maybe rewrite line offsets relative to function start?
105         sb.WriteString(fmtFullPos(cs.Call.Pos()))
106         fmt.Fprintf(&sb, "|%d", cs.ID)
107         return sb.String()
108 }
109
110 func buildEncodedCallSiteTab(tab CallSiteTab) encodedCallSiteTab {
111         r := make(encodedCallSiteTab)
112         for _, cs := range tab {
113                 k := EncodeCallSiteKey(cs)
114                 r[k] = propsAndScore{
115                         props: cs.Flags,
116                         score: cs.Score,
117                         mask:  cs.ScoreMask,
118                 }
119         }
120         return r
121 }
122
123 // dumpCallSiteComments emits comments into the dump file for the
124 // callsites in the function of interest. If "ecst" is non-nil, we use
125 // that, otherwise generated a fresh encodedCallSiteTab from "tab".
126 func dumpCallSiteComments(w io.Writer, tab CallSiteTab, ecst encodedCallSiteTab) {
127         if ecst == nil {
128                 ecst = buildEncodedCallSiteTab(tab)
129         }
130         tags := make([]string, 0, len(ecst))
131         for k := range ecst {
132                 tags = append(tags, k)
133         }
134         sort.Strings(tags)
135         for _, s := range tags {
136                 v := ecst[s]
137                 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())
138         }
139         fmt.Fprintf(w, "// %s\n", csDelimiter)
140 }