]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/pgo/irgraph.go
cmd/compile/internal/pgo: add hint to missing start_line error
[gostls13.git] / src / cmd / compile / internal / pgo / irgraph.go
1 // Copyright 2022 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 // WORK IN PROGRESS
6
7 // A note on line numbers: when working with line numbers, we always use the
8 // binary-visible relative line number. i.e., the line number as adjusted by
9 // //line directives (ctxt.InnermostPos(ir.Node.Pos()).RelLine()). Use
10 // NodeLineOffset to compute line offsets.
11 //
12 // If you are thinking, "wait, doesn't that just make things more complex than
13 // using the real line number?", then you are 100% correct. Unfortunately,
14 // pprof profiles generated by the runtime always contain line numbers as
15 // adjusted by //line directives (because that is what we put in pclntab). Thus
16 // for the best behavior when attempting to match the source with the profile
17 // it makes sense to use the same line number space.
18 //
19 // Some of the effects of this to keep in mind:
20 //
21 //  - For files without //line directives there is no impact, as RelLine() ==
22 //    Line().
23 //  - For functions entirely covered by the same //line directive (i.e., a
24 //    directive before the function definition and no directives within the
25 //    function), there should also be no impact, as line offsets within the
26 //    function should be the same as the real line offsets.
27 //  - Functions containing //line directives may be impacted. As fake line
28 //    numbers need not be monotonic, we may compute negative line offsets. We
29 //    should accept these and attempt to use them for best-effort matching, as
30 //    these offsets should still match if the source is unchanged, and may
31 //    continue to match with changed source depending on the impact of the
32 //    changes on fake line numbers.
33 //  - Functions containing //line directives may also contain duplicate lines,
34 //    making it ambiguous which call the profile is referencing. This is a
35 //    similar problem to multiple calls on a single real line, as we don't
36 //    currently track column numbers.
37 //
38 // Long term it would be best to extend pprof profiles to include real line
39 // numbers. Until then, we have to live with these complexities. Luckily,
40 // //line directives that change line numbers in strange ways should be rare,
41 // and failing PGO matching on these files is not too big of a loss.
42
43 package pgo
44
45 import (
46         "cmd/compile/internal/base"
47         "cmd/compile/internal/ir"
48         "cmd/compile/internal/typecheck"
49         "cmd/compile/internal/types"
50         "fmt"
51         "internal/profile"
52         "log"
53         "os"
54 )
55
56 // IRGraph is the key datastrcture that is built from profile. It is
57 // essentially a call graph with nodes pointing to IRs of functions and edges
58 // carrying weights and callsite information. The graph is bidirectional that
59 // helps in removing nodes efficiently.
60 type IRGraph struct {
61         // Nodes of the graph
62         IRNodes  map[string]*IRNode
63         OutEdges IREdgeMap
64         InEdges  IREdgeMap
65 }
66
67 // IRNode represents a node in the IRGraph.
68 type IRNode struct {
69         // Pointer to the IR of the Function represented by this node.
70         AST *ir.Func
71         // Flat weight of the IRNode, obtained from profile.
72         Flat int64
73         // Cumulative weight of the IRNode.
74         Cum int64
75 }
76
77 // IREdgeMap maps an IRNode to its successors.
78 type IREdgeMap map[*IRNode][]*IREdge
79
80 // IREdge represents a call edge in the IRGraph with source, destination,
81 // weight, callsite, and line number information.
82 type IREdge struct {
83         // Source and destination of the edge in IRNode.
84         Src, Dst       *IRNode
85         Weight         int64
86         CallSiteOffset int // Line offset from function start line.
87 }
88
89 // NodeMapKey represents a hash key to identify unique call-edges in profile
90 // and in IR. Used for deduplication of call edges found in profile.
91 type NodeMapKey struct {
92         CallerName     string
93         CalleeName     string
94         CallSiteOffset int // Line offset from function start line.
95 }
96
97 // Weights capture both node weight and edge weight.
98 type Weights struct {
99         NFlat   int64
100         NCum    int64
101         EWeight int64
102 }
103
104 // CallSiteInfo captures call-site information and its caller/callee.
105 type CallSiteInfo struct {
106         LineOffset int // Line offset from function start line.
107         Caller     *ir.Func
108         Callee     *ir.Func
109 }
110
111 // Profile contains the processed PGO profile and weighted call graph used for
112 // PGO optimizations.
113 type Profile struct {
114         // Aggregated NodeWeights and EdgeWeights across the profile. This
115         // helps us determine the percentage threshold for hot/cold
116         // partitioning.
117         TotalNodeWeight int64
118         TotalEdgeWeight int64
119
120         // NodeMap contains all unique call-edges in the profile and their
121         // aggregated weight.
122         NodeMap map[NodeMapKey]*Weights
123
124         // WeightedCG represents the IRGraph built from profile, which we will
125         // update as part of inlining.
126         WeightedCG *IRGraph
127 }
128
129 // New generates a profile-graph from the profile.
130 func New(profileFile string) *Profile {
131         f, err := os.Open(profileFile)
132         if err != nil {
133                 log.Fatal("failed to open file " + profileFile)
134                 return nil
135         }
136         defer f.Close()
137         profile, err := profile.Parse(f)
138         if err != nil {
139                 log.Fatal("failed to Parse profile file.")
140                 return nil
141         }
142
143         g := newGraph(profile, &Options{
144                 CallTree:    false,
145                 SampleValue: func(v []int64) int64 { return v[1] },
146         })
147
148         p := &Profile{
149                 NodeMap: make(map[NodeMapKey]*Weights),
150                 WeightedCG: &IRGraph{
151                         IRNodes: make(map[string]*IRNode),
152                 },
153         }
154
155         // Build the node map and totals from the profile graph.
156         if !p.processprofileGraph(g) {
157                 return nil
158         }
159
160         // Create package-level call graph with weights from profile and IR.
161         p.initializeIRGraph()
162
163         return p
164 }
165
166 // processprofileGraph builds various maps from the profile-graph.
167 //
168 // It initializes NodeMap and Total{Node,Edge}Weight based on the name and
169 // callsite to compute node and edge weights which will be used later on to
170 // create edges for WeightedCG.
171 // Returns whether it successfully processed the profile.
172 func (p *Profile) processprofileGraph(g *Graph) bool {
173         nFlat := make(map[string]int64)
174         nCum := make(map[string]int64)
175         seenStartLine := false
176
177         // Accummulate weights for the same node.
178         for _, n := range g.Nodes {
179                 canonicalName := n.Info.Name
180                 nFlat[canonicalName] += n.FlatValue()
181                 nCum[canonicalName] += n.CumValue()
182         }
183
184         // Process graph and build various node and edge maps which will
185         // be consumed by AST walk.
186         for _, n := range g.Nodes {
187                 seenStartLine = seenStartLine || n.Info.StartLine != 0
188
189                 p.TotalNodeWeight += n.FlatValue()
190                 canonicalName := n.Info.Name
191                 // Create the key to the nodeMapKey.
192                 nodeinfo := NodeMapKey{
193                         CallerName:     canonicalName,
194                         CallSiteOffset: n.Info.Lineno - n.Info.StartLine,
195                 }
196
197                 for _, e := range n.Out {
198                         p.TotalEdgeWeight += e.WeightValue()
199                         nodeinfo.CalleeName = e.Dest.Info.Name
200                         if w, ok := p.NodeMap[nodeinfo]; ok {
201                                 w.EWeight += e.WeightValue()
202                         } else {
203                                 weights := new(Weights)
204                                 weights.NFlat = nFlat[canonicalName]
205                                 weights.NCum = nCum[canonicalName]
206                                 weights.EWeight = e.WeightValue()
207                                 p.NodeMap[nodeinfo] = weights
208                         }
209                 }
210         }
211
212         if p.TotalNodeWeight == 0 || p.TotalEdgeWeight == 0 {
213                 return false // accept but ignore profile with no sample
214         }
215
216         if !seenStartLine {
217                 // TODO(prattic): If Function.start_line is missing we could
218                 // fall back to using absolute line numbers, which is better
219                 // than nothing.
220                 log.Fatal("PGO profile missing Function.start_line data (Go version of profiled application too old? Go 1.20+ automatically adds this to profiles)")
221         }
222
223         return true
224 }
225
226 // initializeIRGraph builds the IRGraph by visting all the ir.Func in decl list
227 // of a package.
228 func (p *Profile) initializeIRGraph() {
229         // Bottomup walk over the function to create IRGraph.
230         ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
231                 for _, n := range list {
232                         p.VisitIR(n, recursive)
233                 }
234         })
235 }
236
237 // VisitIR traverses the body of each ir.Func and use NodeMap to determine if
238 // we need to add an edge from ir.Func and any node in the ir.Func body.
239 func (p *Profile) VisitIR(fn *ir.Func, recursive bool) {
240         g := p.WeightedCG
241
242         if g.IRNodes == nil {
243                 g.IRNodes = make(map[string]*IRNode)
244         }
245         if g.OutEdges == nil {
246                 g.OutEdges = make(map[*IRNode][]*IREdge)
247         }
248         if g.InEdges == nil {
249                 g.InEdges = make(map[*IRNode][]*IREdge)
250         }
251         name := ir.PkgFuncName(fn)
252         node := new(IRNode)
253         node.AST = fn
254         if g.IRNodes[name] == nil {
255                 g.IRNodes[name] = node
256         }
257         // Create the key for the NodeMapKey.
258         nodeinfo := NodeMapKey{
259                 CallerName:     name,
260                 CalleeName:     "",
261                 CallSiteOffset: 0,
262         }
263         // If the node exists, then update its node weight.
264         if weights, ok := p.NodeMap[nodeinfo]; ok {
265                 g.IRNodes[name].Flat = weights.NFlat
266                 g.IRNodes[name].Cum = weights.NCum
267         }
268
269         // Recursively walk over the body of the function to create IRGraph edges.
270         p.createIRGraphEdge(fn, g.IRNodes[name], name)
271 }
272
273 // NodeLineOffset returns the line offset of n in fn.
274 func NodeLineOffset(n ir.Node, fn *ir.Func) int {
275         // See "A note on line numbers" at the top of the file.
276         line := int(base.Ctxt.InnermostPos(n.Pos()).RelLine())
277         startLine := int(base.Ctxt.InnermostPos(fn.Pos()).RelLine())
278         return line - startLine
279 }
280
281 // addIREdge adds an edge between caller and new node that points to `callee`
282 // based on the profile-graph and NodeMap.
283 func (p *Profile) addIREdge(caller *IRNode, callername string, call ir.Node, callee *ir.Func) {
284         g := p.WeightedCG
285
286         // Create an IRNode for the callee.
287         calleenode := new(IRNode)
288         calleenode.AST = callee
289         calleename := ir.PkgFuncName(callee)
290
291         // Create key for NodeMapKey.
292         nodeinfo := NodeMapKey{
293                 CallerName:     callername,
294                 CalleeName:     calleename,
295                 CallSiteOffset: NodeLineOffset(call, caller.AST),
296         }
297
298         // Create the callee node with node weight.
299         if g.IRNodes[calleename] == nil {
300                 g.IRNodes[calleename] = calleenode
301                 nodeinfo2 := NodeMapKey{
302                         CallerName:     calleename,
303                         CalleeName:     "",
304                         CallSiteOffset: 0,
305                 }
306                 if weights, ok := p.NodeMap[nodeinfo2]; ok {
307                         g.IRNodes[calleename].Flat = weights.NFlat
308                         g.IRNodes[calleename].Cum = weights.NCum
309                 }
310         }
311
312         if weights, ok := p.NodeMap[nodeinfo]; ok {
313                 caller.Flat = weights.NFlat
314                 caller.Cum = weights.NCum
315
316                 // Add edge in the IRGraph from caller to callee.
317                 info := &IREdge{Src: caller, Dst: g.IRNodes[calleename], Weight: weights.EWeight, CallSiteOffset: nodeinfo.CallSiteOffset}
318                 g.OutEdges[caller] = append(g.OutEdges[caller], info)
319                 g.InEdges[g.IRNodes[calleename]] = append(g.InEdges[g.IRNodes[calleename]], info)
320         } else {
321                 nodeinfo.CalleeName = ""
322                 nodeinfo.CallSiteOffset = 0
323                 if weights, ok := p.NodeMap[nodeinfo]; ok {
324                         caller.Flat = weights.NFlat
325                         caller.Cum = weights.NCum
326                         info := &IREdge{Src: caller, Dst: g.IRNodes[calleename], Weight: 0, CallSiteOffset: nodeinfo.CallSiteOffset}
327                         g.OutEdges[caller] = append(g.OutEdges[caller], info)
328                         g.InEdges[g.IRNodes[calleename]] = append(g.InEdges[g.IRNodes[calleename]], info)
329                 } else {
330                         info := &IREdge{Src: caller, Dst: g.IRNodes[calleename], Weight: 0, CallSiteOffset: nodeinfo.CallSiteOffset}
331                         g.OutEdges[caller] = append(g.OutEdges[caller], info)
332                         g.InEdges[g.IRNodes[calleename]] = append(g.InEdges[g.IRNodes[calleename]], info)
333                 }
334         }
335 }
336
337 // createIRGraphEdge traverses the nodes in the body of ir.Func and add edges between callernode which points to the ir.Func and the nodes in the body.
338 func (p *Profile) createIRGraphEdge(fn *ir.Func, callernode *IRNode, name string) {
339         var doNode func(ir.Node) bool
340         doNode = func(n ir.Node) bool {
341                 switch n.Op() {
342                 default:
343                         ir.DoChildren(n, doNode)
344                 case ir.OCALLFUNC:
345                         call := n.(*ir.CallExpr)
346                         // Find the callee function from the call site and add the edge.
347                         callee := inlCallee(call.X)
348                         if callee != nil {
349                                 p.addIREdge(callernode, name, n, callee)
350                         }
351                 case ir.OCALLMETH:
352                         call := n.(*ir.CallExpr)
353                         // Find the callee method from the call site and add the edge.
354                         callee := ir.MethodExprName(call.X).Func
355                         p.addIREdge(callernode, name, n, callee)
356                 }
357                 return false
358         }
359         doNode(fn)
360 }
361
362 // WeightInPercentage converts profile weights to a percentage.
363 func WeightInPercentage(value int64, total int64) float64 {
364         return (float64(value) / float64(total)) * 100
365 }
366
367 // PrintWeightedCallGraphDOT prints IRGraph in DOT format.
368 func (p *Profile) PrintWeightedCallGraphDOT(edgeThreshold float64) {
369         fmt.Printf("\ndigraph G {\n")
370         fmt.Printf("forcelabels=true;\n")
371
372         // List of functions in this package.
373         funcs := make(map[string]struct{})
374         ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
375                 for _, f := range list {
376                         name := ir.PkgFuncName(f)
377                         funcs[name] = struct{}{}
378                 }
379         })
380
381         // Determine nodes of DOT.
382         nodes := make(map[string]*ir.Func)
383         for name, _ := range funcs {
384                 if n, ok := p.WeightedCG.IRNodes[name]; ok {
385                         for _, e := range p.WeightedCG.OutEdges[n] {
386                                 if _, ok := nodes[ir.PkgFuncName(e.Src.AST)]; !ok {
387                                         nodes[ir.PkgFuncName(e.Src.AST)] = e.Src.AST
388                                 }
389                                 if _, ok := nodes[ir.PkgFuncName(e.Dst.AST)]; !ok {
390                                         nodes[ir.PkgFuncName(e.Dst.AST)] = e.Dst.AST
391                                 }
392                         }
393                         if _, ok := nodes[ir.PkgFuncName(n.AST)]; !ok {
394                                 nodes[ir.PkgFuncName(n.AST)] = n.AST
395                         }
396                 }
397         }
398
399         // Print nodes.
400         for name, ast := range nodes {
401                 if n, ok := p.WeightedCG.IRNodes[name]; ok {
402                         nodeweight := WeightInPercentage(n.Flat, p.TotalNodeWeight)
403                         color := "black"
404                         if ast.Inl != nil {
405                                 fmt.Printf("\"%v\" [color=%v,label=\"%v,freq=%.2f,inl_cost=%d\"];\n", ir.PkgFuncName(ast), color, ir.PkgFuncName(ast), nodeweight, ast.Inl.Cost)
406                         } else {
407                                 fmt.Printf("\"%v\" [color=%v, label=\"%v,freq=%.2f\"];\n", ir.PkgFuncName(ast), color, ir.PkgFuncName(ast), nodeweight)
408                         }
409                 }
410         }
411         // Print edges.
412         ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
413                 for _, f := range list {
414                         name := ir.PkgFuncName(f)
415                         if n, ok := p.WeightedCG.IRNodes[name]; ok {
416                                 for _, e := range p.WeightedCG.OutEdges[n] {
417                                         edgepercent := WeightInPercentage(e.Weight, p.TotalEdgeWeight)
418                                         if edgepercent > edgeThreshold {
419                                                 fmt.Printf("edge [color=red, style=solid];\n")
420                                         } else {
421                                                 fmt.Printf("edge [color=black, style=solid];\n")
422                                         }
423
424                                         fmt.Printf("\"%v\" -> \"%v\" [label=\"%.2f\"];\n", ir.PkgFuncName(n.AST), ir.PkgFuncName(e.Dst.AST), edgepercent)
425                                 }
426                         }
427                 }
428         })
429         fmt.Printf("}\n")
430 }
431
432 // RedirectEdges deletes and redirects out-edges from node cur based on
433 // inlining information via inlinedCallSites.
434 //
435 // CallSiteInfo.Callee must be nil.
436 func (p *Profile) RedirectEdges(cur *IRNode, inlinedCallSites map[CallSiteInfo]struct{}) {
437         g := p.WeightedCG
438
439         for i, outEdge := range g.OutEdges[cur] {
440                 if _, found := inlinedCallSites[CallSiteInfo{LineOffset: outEdge.CallSiteOffset, Caller: cur.AST}]; !found {
441                         for _, InEdge := range g.InEdges[cur] {
442                                 if _, ok := inlinedCallSites[CallSiteInfo{LineOffset: InEdge.CallSiteOffset, Caller: InEdge.Src.AST}]; ok {
443                                         weight := g.calculateWeight(InEdge.Src, cur)
444                                         g.redirectEdge(InEdge.Src, cur, outEdge, weight, i)
445                                 }
446                         }
447                 } else {
448                         g.remove(cur, i)
449                 }
450         }
451 }
452
453 // redirectEdges deletes the cur node out-edges and redirect them so now these
454 // edges are the parent node out-edges.
455 func (g *IRGraph) redirectEdges(parent *IRNode, cur *IRNode) {
456         for _, outEdge := range g.OutEdges[cur] {
457                 outEdge.Src = parent
458                 g.OutEdges[parent] = append(g.OutEdges[parent], outEdge)
459         }
460         delete(g.OutEdges, cur)
461 }
462
463 // redirectEdge deletes the cur-node's out-edges and redirect them so now these
464 // edges are the parent node out-edges.
465 func (g *IRGraph) redirectEdge(parent *IRNode, cur *IRNode, outEdge *IREdge, weight int64, idx int) {
466         outEdge.Src = parent
467         outEdge.Weight = weight * outEdge.Weight
468         g.OutEdges[parent] = append(g.OutEdges[parent], outEdge)
469         g.remove(cur, idx)
470 }
471
472 // remove deletes the cur-node's out-edges at index idx.
473 func (g *IRGraph) remove(cur *IRNode, i int) {
474         if len(g.OutEdges[cur]) >= 2 {
475                 g.OutEdges[cur][i] = g.OutEdges[cur][len(g.OutEdges[cur])-1]
476                 g.OutEdges[cur] = g.OutEdges[cur][:len(g.OutEdges[cur])-1]
477         } else {
478                 delete(g.OutEdges, cur)
479         }
480 }
481
482 // calculateWeight calculates the weight of the new redirected edge.
483 func (g *IRGraph) calculateWeight(parent *IRNode, cur *IRNode) int64 {
484         sum := int64(0)
485         pw := int64(0)
486         for _, InEdge := range g.InEdges[cur] {
487                 sum = sum + InEdge.Weight
488                 if InEdge.Src == parent {
489                         pw = InEdge.Weight
490                 }
491         }
492         weight := int64(0)
493         if sum != 0 {
494                 weight = pw / sum
495         } else {
496                 weight = pw
497         }
498         return weight
499 }
500
501 // inlCallee is same as the implementation for inl.go with one change. The change is that we do not invoke CanInline on a closure.
502 func inlCallee(fn ir.Node) *ir.Func {
503         fn = ir.StaticValue(fn)
504         switch fn.Op() {
505         case ir.OMETHEXPR:
506                 fn := fn.(*ir.SelectorExpr)
507                 n := ir.MethodExprName(fn)
508                 // Check that receiver type matches fn.X.
509                 // TODO(mdempsky): Handle implicit dereference
510                 // of pointer receiver argument?
511                 if n == nil || !types.Identical(n.Type().Recv().Type, fn.X.Type()) {
512                         return nil
513                 }
514                 return n.Func
515         case ir.ONAME:
516                 fn := fn.(*ir.Name)
517                 if fn.Class == ir.PFUNC {
518                         return fn.Func
519                 }
520         case ir.OCLOSURE:
521                 fn := fn.(*ir.ClosureExpr)
522                 c := fn.Func
523                 return c
524         }
525         return nil
526 }