From 0c5612092deb0a50c5a3d67babc1249049595558 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Tue, 5 Mar 2024 11:04:26 -0500 Subject: cmd/compile: rename cmd/compile/internal/pgo to cmd/compile/internal/pgoir This helps reduce confusion with cmd/internal/pgo, which performs compilation-independent analysis. pgoir associates that data with the IR from the current package compilation. For #58102. Change-Id: I9ef1c8bc41db466d3340f41f6d071b95c09566de Reviewed-on: https://go-review.googlesource.com/c/go/+/569338 Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/devirtualize/pgo.go | 36 +- src/cmd/compile/internal/devirtualize/pgo_test.go | 2 +- src/cmd/compile/internal/gc/main.go | 6 +- src/cmd/compile/internal/inline/inl.go | 2 +- .../inline/inlheur/analyze_func_callsites.go | 4 +- src/cmd/compile/internal/inline/inlheur/scoring.go | 4 +- .../internal/inline/interleaved/interleaved.go | 8 +- src/cmd/compile/internal/noder/unified.go | 4 +- src/cmd/compile/internal/pgo/irgraph.go | 489 -------------------- src/cmd/compile/internal/pgoir/irgraph.go | 491 +++++++++++++++++++++ 10 files changed, 524 insertions(+), 522 deletions(-) delete mode 100644 src/cmd/compile/internal/pgo/irgraph.go create mode 100644 src/cmd/compile/internal/pgoir/irgraph.go diff --git a/src/cmd/compile/internal/devirtualize/pgo.go b/src/cmd/compile/internal/devirtualize/pgo.go index 5cc9fab54c..0a43135420 100644 --- a/src/cmd/compile/internal/devirtualize/pgo.go +++ b/src/cmd/compile/internal/devirtualize/pgo.go @@ -9,7 +9,7 @@ import ( "cmd/compile/internal/inline" "cmd/compile/internal/ir" "cmd/compile/internal/logopt" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/internal/obj" @@ -102,7 +102,7 @@ type CallStat struct { // // The primary benefit of this transformation is enabling inlining of the // direct call. -func ProfileGuided(fn *ir.Func, p *pgo.Profile) { +func ProfileGuided(fn *ir.Func, p *pgoir.Profile) { ir.CurFunc = fn name := ir.LinkFuncName(fn) @@ -184,7 +184,7 @@ func ProfileGuided(fn *ir.Func, p *pgo.Profile) { // Devirtualize interface call if possible and eligible. Returns the new // ir.Node if call was devirtualized, and if so also the callee and weight of // the devirtualized edge. -func maybeDevirtualizeInterfaceCall(p *pgo.Profile, fn *ir.Func, call *ir.CallExpr) (ir.Node, *ir.Func, int64) { +func maybeDevirtualizeInterfaceCall(p *pgoir.Profile, fn *ir.Func, call *ir.CallExpr) (ir.Node, *ir.Func, int64) { if base.Debug.PGODevirtualize < 1 { return nil, nil, 0 } @@ -214,13 +214,13 @@ func maybeDevirtualizeInterfaceCall(p *pgo.Profile, fn *ir.Func, call *ir.CallEx // Devirtualize an indirect function call if possible and eligible. Returns the new // ir.Node if call was devirtualized, and if so also the callee and weight of // the devirtualized edge. -func maybeDevirtualizeFunctionCall(p *pgo.Profile, fn *ir.Func, call *ir.CallExpr) (ir.Node, *ir.Func, int64) { +func maybeDevirtualizeFunctionCall(p *pgoir.Profile, fn *ir.Func, call *ir.CallExpr) (ir.Node, *ir.Func, int64) { if base.Debug.PGODevirtualize < 2 { return nil, nil, 0 } // Bail if this is a direct call; no devirtualization necessary. - callee := pgo.DirectCallee(call.Fun) + callee := pgoir.DirectCallee(call.Fun) if callee != nil { return nil, nil, 0 } @@ -309,7 +309,7 @@ func shouldPGODevirt(fn *ir.Func) bool { fmt.Printf("%v: should not PGO devirtualize %v: %s\n", ir.Line(fn), ir.FuncName(fn), reason) } if logopt.Enabled() { - logopt.LogOpt(fn.Pos(), ": should not PGO devirtualize function", "pgo-devirtualize", ir.FuncName(fn), reason) + logopt.LogOpt(fn.Pos(), ": should not PGO devirtualize function", "pgoir-devirtualize", ir.FuncName(fn), reason) } } }() @@ -336,7 +336,7 @@ func shouldPGODevirt(fn *ir.Func) bool { // constructCallStat builds an initial CallStat describing this call, for // logging. If the call is devirtualized, the devirtualization fields should be // updated. -func constructCallStat(p *pgo.Profile, fn *ir.Func, name string, call *ir.CallExpr) *CallStat { +func constructCallStat(p *pgoir.Profile, fn *ir.Func, name string, call *ir.CallExpr) *CallStat { switch call.Op() { case ir.OCALLFUNC, ir.OCALLINTER, ir.OCALLMETH: default: @@ -350,9 +350,9 @@ func constructCallStat(p *pgo.Profile, fn *ir.Func, name string, call *ir.CallEx Caller: name, } - offset := pgo.NodeLineOffset(call, fn) + offset := pgoir.NodeLineOffset(call, fn) - hotter := func(e *pgo.IREdge) bool { + hotter := func(e *pgoir.IREdge) bool { if stat.Hottest == "" { return true } @@ -384,7 +384,7 @@ func constructCallStat(p *pgo.Profile, fn *ir.Func, name string, call *ir.CallEx case ir.OCALLFUNC: stat.Interface = false - callee := pgo.DirectCallee(call.Fun) + callee := pgoir.DirectCallee(call.Fun) if callee != nil { stat.Direct = true if stat.Hottest == "" { @@ -651,12 +651,12 @@ func interfaceCallRecvTypeAndMethod(call *ir.CallExpr) (*types.Type, *types.Sym) // if available, and its edge weight. extraFn can perform additional // applicability checks on each candidate edge. If extraFn returns false, // candidate will not be considered a valid callee candidate. -func findHotConcreteCallee(p *pgo.Profile, caller *ir.Func, call *ir.CallExpr, extraFn func(callerName string, callOffset int, candidate *pgo.IREdge) bool) (*ir.Func, int64) { +func findHotConcreteCallee(p *pgoir.Profile, caller *ir.Func, call *ir.CallExpr, extraFn func(callerName string, callOffset int, candidate *pgoir.IREdge) bool) (*ir.Func, int64) { callerName := ir.LinkFuncName(caller) callerNode := p.WeightedCG.IRNodes[callerName] - callOffset := pgo.NodeLineOffset(call, caller) + callOffset := pgoir.NodeLineOffset(call, caller) - var hottest *pgo.IREdge + var hottest *pgoir.IREdge // Returns true if e is hotter than hottest. // @@ -664,7 +664,7 @@ func findHotConcreteCallee(p *pgo.Profile, caller *ir.Func, call *ir.CallExpr, e // has arbitrary iteration order, we need to apply additional sort // criteria when e.Weight == hottest.Weight to ensure we have stable // selection. - hotter := func(e *pgo.IREdge) bool { + hotter := func(e *pgoir.IREdge) bool { if hottest == nil { return true } @@ -747,10 +747,10 @@ func findHotConcreteCallee(p *pgo.Profile, caller *ir.Func, call *ir.CallExpr, e // findHotConcreteInterfaceCallee returns the *ir.Func of the hottest callee of an // interface call, if available, and its edge weight. -func findHotConcreteInterfaceCallee(p *pgo.Profile, caller *ir.Func, call *ir.CallExpr) (*ir.Func, int64) { +func findHotConcreteInterfaceCallee(p *pgoir.Profile, caller *ir.Func, call *ir.CallExpr) (*ir.Func, int64) { inter, method := interfaceCallRecvTypeAndMethod(call) - return findHotConcreteCallee(p, caller, call, func(callerName string, callOffset int, e *pgo.IREdge) bool { + return findHotConcreteCallee(p, caller, call, func(callerName string, callOffset int, e *pgoir.IREdge) bool { ctyp := methodRecvType(e.Dst.AST) if ctyp == nil { // Not a method. @@ -795,10 +795,10 @@ func findHotConcreteInterfaceCallee(p *pgo.Profile, caller *ir.Func, call *ir.Ca // findHotConcreteFunctionCallee returns the *ir.Func of the hottest callee of an // indirect function call, if available, and its edge weight. -func findHotConcreteFunctionCallee(p *pgo.Profile, caller *ir.Func, call *ir.CallExpr) (*ir.Func, int64) { +func findHotConcreteFunctionCallee(p *pgoir.Profile, caller *ir.Func, call *ir.CallExpr) (*ir.Func, int64) { typ := call.Fun.Type().Underlying() - return findHotConcreteCallee(p, caller, call, func(callerName string, callOffset int, e *pgo.IREdge) bool { + return findHotConcreteCallee(p, caller, call, func(callerName string, callOffset int, e *pgoir.IREdge) bool { ctyp := e.Dst.AST.Type().Underlying() // If ctyp doesn't match typ it is most likely from a different diff --git a/src/cmd/compile/internal/devirtualize/pgo_test.go b/src/cmd/compile/internal/devirtualize/pgo_test.go index 6ba8e9f907..cff4d63d51 100644 --- a/src/cmd/compile/internal/devirtualize/pgo_test.go +++ b/src/cmd/compile/internal/devirtualize/pgo_test.go @@ -7,7 +7,7 @@ package devirtualize import ( "cmd/compile/internal/base" "cmd/compile/internal/ir" - pgoir "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/internal/obj" diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index 7e5069fced..130feafb24 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -17,7 +17,7 @@ import ( "cmd/compile/internal/logopt" "cmd/compile/internal/loopvar" "cmd/compile/internal/noder" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/pkginit" "cmd/compile/internal/reflectdata" "cmd/compile/internal/rttype" @@ -215,10 +215,10 @@ func Main(archInit func(*ssagen.ArchInfo)) { // Read profile file and build profile-graph and weighted-call-graph. base.Timer.Start("fe", "pgo-load-profile") - var profile *pgo.Profile + var profile *pgoir.Profile if base.Flag.PgoProfile != "" { var err error - profile, err = pgo.New(base.Flag.PgoProfile) + profile, err = pgoir.New(base.Flag.PgoProfile) if err != nil { log.Fatalf("%s: PGO error: %v", base.Flag.PgoProfile, err) } diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index 33f454083f..a17562596f 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -36,7 +36,7 @@ import ( "cmd/compile/internal/inline/inlheur" "cmd/compile/internal/ir" "cmd/compile/internal/logopt" - pgoir "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/internal/obj" diff --git a/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go b/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go index 36ebe18b82..203578726e 100644 --- a/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go +++ b/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go @@ -6,7 +6,7 @@ package inlheur import ( "cmd/compile/internal/ir" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "fmt" "os" @@ -220,7 +220,7 @@ func (cstb *callSiteTableBuilder) nodeVisitPre(n ir.Node) { } case ir.OCALLFUNC: ce := n.(*ir.CallExpr) - callee := pgo.DirectCallee(ce.Fun) + callee := pgoir.DirectCallee(ce.Fun) if callee != nil && callee.Inl != nil { cstb.addCallSite(callee, ce) } diff --git a/src/cmd/compile/internal/inline/inlheur/scoring.go b/src/cmd/compile/internal/inline/inlheur/scoring.go index 3de95d46b4..3ef7c9b79a 100644 --- a/src/cmd/compile/internal/inline/inlheur/scoring.go +++ b/src/cmd/compile/internal/inline/inlheur/scoring.go @@ -7,7 +7,7 @@ package inlheur import ( "cmd/compile/internal/base" "cmd/compile/internal/ir" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/types" "fmt" "os" @@ -638,7 +638,7 @@ var allCallSites CallSiteTab // of the function called, "CallerPos" is the position of the // callsite, and "ScoreFlags" is a digest of the specific properties // we used to make adjustments to callsite score via heuristics. -func DumpInlCallSiteScores(profile *pgo.Profile, budgetCallback func(fn *ir.Func, profile *pgo.Profile) (int32, bool)) { +func DumpInlCallSiteScores(profile *pgoir.Profile, budgetCallback func(fn *ir.Func, profile *pgoir.Profile) (int32, bool)) { var indirectlyDueToPromotion func(cs *CallSite) bool indirectlyDueToPromotion = func(cs *CallSite) bool { diff --git a/src/cmd/compile/internal/inline/interleaved/interleaved.go b/src/cmd/compile/internal/inline/interleaved/interleaved.go index e55b0f1aee..9b2efd7f27 100644 --- a/src/cmd/compile/internal/inline/interleaved/interleaved.go +++ b/src/cmd/compile/internal/inline/interleaved/interleaved.go @@ -12,14 +12,14 @@ import ( "cmd/compile/internal/inline" "cmd/compile/internal/inline/inlheur" "cmd/compile/internal/ir" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "fmt" ) // DevirtualizeAndInlinePackage interleaves devirtualization and inlining on // all functions within pkg. -func DevirtualizeAndInlinePackage(pkg *ir.Package, profile *pgo.Profile) { +func DevirtualizeAndInlinePackage(pkg *ir.Package, profile *pgoir.Profile) { if profile != nil && base.Debug.PGODevirtualize > 0 { // TODO(mdempsky): Integrate into DevirtualizeAndInlineFunc below. ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { @@ -34,7 +34,7 @@ func DevirtualizeAndInlinePackage(pkg *ir.Package, profile *pgo.Profile) { inlheur.SetupScoreAdjustments() } - var inlProfile *pgo.Profile // copy of profile for inlining + var inlProfile *pgoir.Profile // copy of profile for inlining if base.Debug.PGOInline != 0 { inlProfile = profile } @@ -66,7 +66,7 @@ func DevirtualizeAndInlinePackage(pkg *ir.Package, profile *pgo.Profile) { // DevirtualizeAndInlineFunc interleaves devirtualization and inlining // on a single function. -func DevirtualizeAndInlineFunc(fn *ir.Func, profile *pgo.Profile) { +func DevirtualizeAndInlineFunc(fn *ir.Func, profile *pgoir.Profile) { ir.WithFunc(fn, func() { if base.Flag.LowerL != 0 { if inlheur.Enabled() && !fn.Wrapper() { diff --git a/src/cmd/compile/internal/noder/unified.go b/src/cmd/compile/internal/noder/unified.go index 492b00d256..da04ac5a2a 100644 --- a/src/cmd/compile/internal/noder/unified.go +++ b/src/cmd/compile/internal/noder/unified.go @@ -15,7 +15,7 @@ import ( "cmd/compile/internal/base" "cmd/compile/internal/inline" "cmd/compile/internal/ir" - "cmd/compile/internal/pgo" + "cmd/compile/internal/pgoir" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/compile/internal/types2" @@ -175,7 +175,7 @@ func lookupMethod(pkg *types.Pkg, symName string) (*ir.Func, error) { func unified(m posMap, noders []*noder) { inline.InlineCall = unifiedInlineCall typecheck.HaveInlineBody = unifiedHaveInlineBody - pgo.LookupFunc = LookupFunc + pgoir.LookupFunc = LookupFunc data := writePkgStub(m, noders) diff --git a/src/cmd/compile/internal/pgo/irgraph.go b/src/cmd/compile/internal/pgo/irgraph.go deleted file mode 100644 index 418066f8ff..0000000000 --- a/src/cmd/compile/internal/pgo/irgraph.go +++ /dev/null @@ -1,489 +0,0 @@ -// Copyright 2022 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// A note on line numbers: when working with line numbers, we always use the -// binary-visible relative line number. i.e., the line number as adjusted by -// //line directives (ctxt.InnermostPos(ir.Node.Pos()).RelLine()). Use -// NodeLineOffset to compute line offsets. -// -// If you are thinking, "wait, doesn't that just make things more complex than -// using the real line number?", then you are 100% correct. Unfortunately, -// pprof profiles generated by the runtime always contain line numbers as -// adjusted by //line directives (because that is what we put in pclntab). Thus -// for the best behavior when attempting to match the source with the profile -// it makes sense to use the same line number space. -// -// Some of the effects of this to keep in mind: -// -// - For files without //line directives there is no impact, as RelLine() == -// Line(). -// - For functions entirely covered by the same //line directive (i.e., a -// directive before the function definition and no directives within the -// function), there should also be no impact, as line offsets within the -// function should be the same as the real line offsets. -// - Functions containing //line directives may be impacted. As fake line -// numbers need not be monotonic, we may compute negative line offsets. We -// should accept these and attempt to use them for best-effort matching, as -// these offsets should still match if the source is unchanged, and may -// continue to match with changed source depending on the impact of the -// changes on fake line numbers. -// - Functions containing //line directives may also contain duplicate lines, -// making it ambiguous which call the profile is referencing. This is a -// similar problem to multiple calls on a single real line, as we don't -// currently track column numbers. -// -// Long term it would be best to extend pprof profiles to include real line -// numbers. Until then, we have to live with these complexities. Luckily, -// //line directives that change line numbers in strange ways should be rare, -// and failing PGO matching on these files is not too big of a loss. - -package pgo - -import ( - "bufio" - "cmd/compile/internal/base" - "cmd/compile/internal/ir" - "cmd/compile/internal/typecheck" - "cmd/compile/internal/types" - "cmd/internal/pgo" - "fmt" - "os" -) - -// IRGraph is a call graph with nodes pointing to IRs of functions and edges -// carrying weights and callsite information. -// -// Nodes for indirect calls may have missing IR (IRNode.AST == nil) if the node -// is not visible from this package (e.g., not in the transitive deps). Keeping -// these nodes allows determining the hottest edge from a call even if that -// callee is not available. -// -// TODO(prattmic): Consider merging this data structure with Graph. This is -// effectively a copy of Graph aggregated to line number and pointing to IR. -type IRGraph struct { - // Nodes of the graph. Each node represents a function, keyed by linker - // symbol name. - IRNodes map[string]*IRNode -} - -// IRNode represents a node (function) in the IRGraph. -type IRNode struct { - // Pointer to the IR of the Function represented by this node. - AST *ir.Func - // Linker symbol name of the Function represented by this node. - // Populated only if AST == nil. - LinkerSymbolName string - - // Set of out-edges in the callgraph. The map uniquely identifies each - // edge based on the callsite and callee, for fast lookup. - OutEdges map[pgo.NamedCallEdge]*IREdge -} - -// Name returns the symbol name of this function. -func (i *IRNode) Name() string { - if i.AST != nil { - return ir.LinkFuncName(i.AST) - } - return i.LinkerSymbolName -} - -// IREdge represents a call edge in the IRGraph with source, destination, -// weight, callsite, and line number information. -type IREdge struct { - // Source and destination of the edge in IRNode. - Src, Dst *IRNode - Weight int64 - CallSiteOffset int // Line offset from function start line. -} - -// CallSiteInfo captures call-site information and its caller/callee. -type CallSiteInfo struct { - LineOffset int // Line offset from function start line. - Caller *ir.Func - Callee *ir.Func -} - -// Profile contains the processed PGO profile and weighted call graph used for -// PGO optimizations. -type Profile struct { - // Profile is the base data from the raw profile, without IR attribution. - *pgo.Profile - - // WeightedCG represents the IRGraph built from profile, which we will - // update as part of inlining. - WeightedCG *IRGraph -} - -// New generates a profile-graph from the profile or pre-processed profile. -func New(profileFile string) (*Profile, error) { - f, err := os.Open(profileFile) - if err != nil { - return nil, fmt.Errorf("error opening profile: %w", err) - } - defer f.Close() - r := bufio.NewReader(f) - - isSerialized, err := pgo.IsSerialized(r) - if err != nil { - return nil, fmt.Errorf("error processing profile header: %w", err) - } - - var base *pgo.Profile - if isSerialized { - base, err = pgo.FromSerialized(r) - if err != nil { - return nil, fmt.Errorf("error processing serialized PGO profile: %w", err) - } - } else { - base, err = pgo.FromPProf(r) - if err != nil { - return nil, fmt.Errorf("error processing pprof PGO profile: %w", err) - } - } - - if base.TotalWeight == 0 { - return nil, nil // accept but ignore profile with no samples. - } - - // Create package-level call graph with weights from profile and IR. - wg := createIRGraph(base.NamedEdgeMap) - - return &Profile{ - Profile: base, - WeightedCG: wg, - }, nil -} - -// initializeIRGraph builds the IRGraph by visiting all the ir.Func in decl list -// of a package. -func createIRGraph(namedEdgeMap pgo.NamedEdgeMap) *IRGraph { - g := &IRGraph{ - IRNodes: make(map[string]*IRNode), - } - - // Bottomup walk over the function to create IRGraph. - ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { - for _, fn := range list { - visitIR(fn, namedEdgeMap, g) - } - }) - - // Add additional edges for indirect calls. This must be done second so - // that IRNodes is fully populated (see the dummy node TODO in - // addIndirectEdges). - // - // TODO(prattmic): visitIR above populates the graph via direct calls - // discovered via the IR. addIndirectEdges populates the graph via - // calls discovered via the profile. This combination of opposite - // approaches is a bit awkward, particularly because direct calls are - // discoverable via the profile as well. Unify these into a single - // approach. - addIndirectEdges(g, namedEdgeMap) - - return g -} - -// visitIR traverses the body of each ir.Func adds edges to g from ir.Func to -// any called function in the body. -func visitIR(fn *ir.Func, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { - name := ir.LinkFuncName(fn) - node, ok := g.IRNodes[name] - if !ok { - node = &IRNode{ - AST: fn, - } - g.IRNodes[name] = node - } - - // Recursively walk over the body of the function to create IRGraph edges. - createIRGraphEdge(fn, node, name, namedEdgeMap, g) -} - -// createIRGraphEdge traverses the nodes in the body of ir.Func and adds edges -// between the callernode which points to the ir.Func and the nodes in the -// body. -func createIRGraphEdge(fn *ir.Func, callernode *IRNode, name string, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { - ir.VisitList(fn.Body, func(n ir.Node) { - switch n.Op() { - case ir.OCALLFUNC: - call := n.(*ir.CallExpr) - // Find the callee function from the call site and add the edge. - callee := DirectCallee(call.Fun) - if callee != nil { - addIREdge(callernode, name, n, callee, namedEdgeMap, g) - } - case ir.OCALLMETH: - call := n.(*ir.CallExpr) - // Find the callee method from the call site and add the edge. - callee := ir.MethodExprName(call.Fun).Func - addIREdge(callernode, name, n, callee, namedEdgeMap, g) - } - }) -} - -// NodeLineOffset returns the line offset of n in fn. -func NodeLineOffset(n ir.Node, fn *ir.Func) int { - // See "A note on line numbers" at the top of the file. - line := int(base.Ctxt.InnermostPos(n.Pos()).RelLine()) - startLine := int(base.Ctxt.InnermostPos(fn.Pos()).RelLine()) - return line - startLine -} - -// addIREdge adds an edge between caller and new node that points to `callee` -// based on the profile-graph and NodeMap. -func addIREdge(callerNode *IRNode, callerName string, call ir.Node, callee *ir.Func, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { - calleeName := ir.LinkFuncName(callee) - calleeNode, ok := g.IRNodes[calleeName] - if !ok { - calleeNode = &IRNode{ - AST: callee, - } - g.IRNodes[calleeName] = calleeNode - } - - namedEdge := pgo.NamedCallEdge{ - CallerName: callerName, - CalleeName: calleeName, - CallSiteOffset: NodeLineOffset(call, callerNode.AST), - } - - // Add edge in the IRGraph from caller to callee. - edge := &IREdge{ - Src: callerNode, - Dst: calleeNode, - Weight: namedEdgeMap.Weight[namedEdge], - CallSiteOffset: namedEdge.CallSiteOffset, - } - - if callerNode.OutEdges == nil { - callerNode.OutEdges = make(map[pgo.NamedCallEdge]*IREdge) - } - callerNode.OutEdges[namedEdge] = edge -} - -// LookupFunc looks up a function or method in export data. It is expected to -// be overridden by package noder, to break a dependency cycle. -var LookupFunc = func(fullName string) (*ir.Func, error) { - base.Fatalf("pgo.LookupMethodFunc not overridden") - panic("unreachable") -} - -// addIndirectEdges adds indirect call edges found in the profile to the graph, -// to be used for devirtualization. -// -// N.B. despite the name, addIndirectEdges will add any edges discovered via -// the profile. We don't know for sure that they are indirect, but assume they -// are since direct calls would already be added. (e.g., direct calls that have -// been deleted from source since the profile was taken would be added here). -// -// TODO(prattmic): Devirtualization runs before inlining, so we can't devirtualize -// calls inside inlined call bodies. If we did add that, we'd need edges from -// inlined bodies as well. -func addIndirectEdges(g *IRGraph, namedEdgeMap pgo.NamedEdgeMap) { - // g.IRNodes is populated with the set of functions in the local - // package build by VisitIR. We want to filter for local functions - // below, but we also add unknown callees to IRNodes as we go. So make - // an initial copy of IRNodes to recall just the local functions. - localNodes := make(map[string]*IRNode, len(g.IRNodes)) - for k, v := range g.IRNodes { - localNodes[k] = v - } - - // N.B. We must consider edges in a stable order because export data - // lookup order (LookupMethodFunc, below) can impact the export data of - // this package, which must be stable across different invocations for - // reproducibility. - // - // The weight ordering of ByWeight is irrelevant, it just happens to be - // an ordered list of edges that is already available. - for _, key := range namedEdgeMap.ByWeight { - weight := namedEdgeMap.Weight[key] - // All callers in the local package build were added to IRNodes - // in VisitIR. If a caller isn't in the local package build we - // can skip adding edges, since we won't be devirtualizing in - // them anyway. This keeps the graph smaller. - callerNode, ok := localNodes[key.CallerName] - if !ok { - continue - } - - // Already handled this edge? - if _, ok := callerNode.OutEdges[key]; ok { - continue - } - - calleeNode, ok := g.IRNodes[key.CalleeName] - if !ok { - // IR is missing for this callee. VisitIR populates - // IRNodes with all functions discovered via local - // package function declarations and calls. This - // function may still be available from export data of - // a transitive dependency. - // - // TODO(prattmic): Parameterized types/functions are - // not supported. - // - // TODO(prattmic): This eager lookup during graph load - // is simple, but wasteful. We are likely to load many - // functions that we never need. We could delay load - // until we actually need the method in - // devirtualization. Instantiation of generic functions - // will likely need to be done at the devirtualization - // site, if at all. - fn, err := LookupFunc(key.CalleeName) - if err == nil { - if base.Debug.PGODebug >= 3 { - fmt.Printf("addIndirectEdges: %s found in export data\n", key.CalleeName) - } - calleeNode = &IRNode{AST: fn} - - // N.B. we could call createIRGraphEdge to add - // direct calls in this newly-imported - // function's body to the graph. Similarly, we - // could add to this function's queue to add - // indirect calls. However, those would be - // useless given the visit order of inlining, - // and the ordering of PGO devirtualization and - // inlining. This function can only be used as - // an inlined body. We will never do PGO - // devirtualization inside an inlined call. Nor - // will we perform inlining inside an inlined - // call. - } else { - // Still not found. Most likely this is because - // the callee isn't in the transitive deps of - // this package. - // - // Record this call anyway. If this is the hottest, - // then we want to skip devirtualization rather than - // devirtualizing to the second most common callee. - if base.Debug.PGODebug >= 3 { - fmt.Printf("addIndirectEdges: %s not found in export data: %v\n", key.CalleeName, err) - } - calleeNode = &IRNode{LinkerSymbolName: key.CalleeName} - } - - // Add dummy node back to IRNodes. We don't need this - // directly, but PrintWeightedCallGraphDOT uses these - // to print nodes. - g.IRNodes[key.CalleeName] = calleeNode - } - edge := &IREdge{ - Src: callerNode, - Dst: calleeNode, - Weight: weight, - CallSiteOffset: key.CallSiteOffset, - } - - if callerNode.OutEdges == nil { - callerNode.OutEdges = make(map[pgo.NamedCallEdge]*IREdge) - } - callerNode.OutEdges[key] = edge - } -} - -// PrintWeightedCallGraphDOT prints IRGraph in DOT format. -func (p *Profile) PrintWeightedCallGraphDOT(edgeThreshold float64) { - fmt.Printf("\ndigraph G {\n") - fmt.Printf("forcelabels=true;\n") - - // List of functions in this package. - funcs := make(map[string]struct{}) - ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { - for _, f := range list { - name := ir.LinkFuncName(f) - funcs[name] = struct{}{} - } - }) - - // Determine nodes of DOT. - // - // Note that ir.Func may be nil for functions not visible from this - // package. - nodes := make(map[string]*ir.Func) - for name := range funcs { - if n, ok := p.WeightedCG.IRNodes[name]; ok { - for _, e := range n.OutEdges { - if _, ok := nodes[e.Src.Name()]; !ok { - nodes[e.Src.Name()] = e.Src.AST - } - if _, ok := nodes[e.Dst.Name()]; !ok { - nodes[e.Dst.Name()] = e.Dst.AST - } - } - if _, ok := nodes[n.Name()]; !ok { - nodes[n.Name()] = n.AST - } - } - } - - // Print nodes. - for name, ast := range nodes { - if _, ok := p.WeightedCG.IRNodes[name]; ok { - style := "solid" - if ast == nil { - style = "dashed" - } - - if ast != nil && ast.Inl != nil { - fmt.Printf("\"%v\" [color=black, style=%s, label=\"%v,inl_cost=%d\"];\n", name, style, name, ast.Inl.Cost) - } else { - fmt.Printf("\"%v\" [color=black, style=%s, label=\"%v\"];\n", name, style, name) - } - } - } - // Print edges. - ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { - for _, f := range list { - name := ir.LinkFuncName(f) - if n, ok := p.WeightedCG.IRNodes[name]; ok { - for _, e := range n.OutEdges { - style := "solid" - if e.Dst.AST == nil { - style = "dashed" - } - color := "black" - edgepercent := pgo.WeightInPercentage(e.Weight, p.TotalWeight) - if edgepercent > edgeThreshold { - color = "red" - } - - fmt.Printf("edge [color=%s, style=%s];\n", color, style) - fmt.Printf("\"%v\" -> \"%v\" [label=\"%.2f\"];\n", n.Name(), e.Dst.Name(), edgepercent) - } - } - } - }) - fmt.Printf("}\n") -} - -// DirectCallee takes a function-typed expression and returns the underlying -// function that it refers to if statically known. Otherwise, it returns nil. -// -// Equivalent to inline.inlCallee without calling CanInline on closures. -func DirectCallee(fn ir.Node) *ir.Func { - fn = ir.StaticValue(fn) - switch fn.Op() { - case ir.OMETHEXPR: - fn := fn.(*ir.SelectorExpr) - n := ir.MethodExprName(fn) - // Check that receiver type matches fn.X. - // TODO(mdempsky): Handle implicit dereference - // of pointer receiver argument? - if n == nil || !types.Identical(n.Type().Recv().Type, fn.X.Type()) { - return nil - } - return n.Func - case ir.ONAME: - fn := fn.(*ir.Name) - if fn.Class == ir.PFUNC { - return fn.Func - } - case ir.OCLOSURE: - fn := fn.(*ir.ClosureExpr) - c := fn.Func - return c - } - return nil -} diff --git a/src/cmd/compile/internal/pgoir/irgraph.go b/src/cmd/compile/internal/pgoir/irgraph.go new file mode 100644 index 0000000000..cb4333e6d7 --- /dev/null +++ b/src/cmd/compile/internal/pgoir/irgraph.go @@ -0,0 +1,491 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// A note on line numbers: when working with line numbers, we always use the +// binary-visible relative line number. i.e., the line number as adjusted by +// //line directives (ctxt.InnermostPos(ir.Node.Pos()).RelLine()). Use +// NodeLineOffset to compute line offsets. +// +// If you are thinking, "wait, doesn't that just make things more complex than +// using the real line number?", then you are 100% correct. Unfortunately, +// pprof profiles generated by the runtime always contain line numbers as +// adjusted by //line directives (because that is what we put in pclntab). Thus +// for the best behavior when attempting to match the source with the profile +// it makes sense to use the same line number space. +// +// Some of the effects of this to keep in mind: +// +// - For files without //line directives there is no impact, as RelLine() == +// Line(). +// - For functions entirely covered by the same //line directive (i.e., a +// directive before the function definition and no directives within the +// function), there should also be no impact, as line offsets within the +// function should be the same as the real line offsets. +// - Functions containing //line directives may be impacted. As fake line +// numbers need not be monotonic, we may compute negative line offsets. We +// should accept these and attempt to use them for best-effort matching, as +// these offsets should still match if the source is unchanged, and may +// continue to match with changed source depending on the impact of the +// changes on fake line numbers. +// - Functions containing //line directives may also contain duplicate lines, +// making it ambiguous which call the profile is referencing. This is a +// similar problem to multiple calls on a single real line, as we don't +// currently track column numbers. +// +// Long term it would be best to extend pprof profiles to include real line +// numbers. Until then, we have to live with these complexities. Luckily, +// //line directives that change line numbers in strange ways should be rare, +// and failing PGO matching on these files is not too big of a loss. + +// Package pgoir assosciates a PGO profile with the IR of the current package +// compilation. +package pgoir + +import ( + "bufio" + "cmd/compile/internal/base" + "cmd/compile/internal/ir" + "cmd/compile/internal/typecheck" + "cmd/compile/internal/types" + "cmd/internal/pgo" + "fmt" + "os" +) + +// IRGraph is a call graph with nodes pointing to IRs of functions and edges +// carrying weights and callsite information. +// +// Nodes for indirect calls may have missing IR (IRNode.AST == nil) if the node +// is not visible from this package (e.g., not in the transitive deps). Keeping +// these nodes allows determining the hottest edge from a call even if that +// callee is not available. +// +// TODO(prattmic): Consider merging this data structure with Graph. This is +// effectively a copy of Graph aggregated to line number and pointing to IR. +type IRGraph struct { + // Nodes of the graph. Each node represents a function, keyed by linker + // symbol name. + IRNodes map[string]*IRNode +} + +// IRNode represents a node (function) in the IRGraph. +type IRNode struct { + // Pointer to the IR of the Function represented by this node. + AST *ir.Func + // Linker symbol name of the Function represented by this node. + // Populated only if AST == nil. + LinkerSymbolName string + + // Set of out-edges in the callgraph. The map uniquely identifies each + // edge based on the callsite and callee, for fast lookup. + OutEdges map[pgo.NamedCallEdge]*IREdge +} + +// Name returns the symbol name of this function. +func (i *IRNode) Name() string { + if i.AST != nil { + return ir.LinkFuncName(i.AST) + } + return i.LinkerSymbolName +} + +// IREdge represents a call edge in the IRGraph with source, destination, +// weight, callsite, and line number information. +type IREdge struct { + // Source and destination of the edge in IRNode. + Src, Dst *IRNode + Weight int64 + CallSiteOffset int // Line offset from function start line. +} + +// CallSiteInfo captures call-site information and its caller/callee. +type CallSiteInfo struct { + LineOffset int // Line offset from function start line. + Caller *ir.Func + Callee *ir.Func +} + +// Profile contains the processed PGO profile and weighted call graph used for +// PGO optimizations. +type Profile struct { + // Profile is the base data from the raw profile, without IR attribution. + *pgo.Profile + + // WeightedCG represents the IRGraph built from profile, which we will + // update as part of inlining. + WeightedCG *IRGraph +} + +// New generates a profile-graph from the profile or pre-processed profile. +func New(profileFile string) (*Profile, error) { + f, err := os.Open(profileFile) + if err != nil { + return nil, fmt.Errorf("error opening profile: %w", err) + } + defer f.Close() + r := bufio.NewReader(f) + + isSerialized, err := pgo.IsSerialized(r) + if err != nil { + return nil, fmt.Errorf("error processing profile header: %w", err) + } + + var base *pgo.Profile + if isSerialized { + base, err = pgo.FromSerialized(r) + if err != nil { + return nil, fmt.Errorf("error processing serialized PGO profile: %w", err) + } + } else { + base, err = pgo.FromPProf(r) + if err != nil { + return nil, fmt.Errorf("error processing pprof PGO profile: %w", err) + } + } + + if base.TotalWeight == 0 { + return nil, nil // accept but ignore profile with no samples. + } + + // Create package-level call graph with weights from profile and IR. + wg := createIRGraph(base.NamedEdgeMap) + + return &Profile{ + Profile: base, + WeightedCG: wg, + }, nil +} + +// initializeIRGraph builds the IRGraph by visiting all the ir.Func in decl list +// of a package. +func createIRGraph(namedEdgeMap pgo.NamedEdgeMap) *IRGraph { + g := &IRGraph{ + IRNodes: make(map[string]*IRNode), + } + + // Bottomup walk over the function to create IRGraph. + ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { + for _, fn := range list { + visitIR(fn, namedEdgeMap, g) + } + }) + + // Add additional edges for indirect calls. This must be done second so + // that IRNodes is fully populated (see the dummy node TODO in + // addIndirectEdges). + // + // TODO(prattmic): visitIR above populates the graph via direct calls + // discovered via the IR. addIndirectEdges populates the graph via + // calls discovered via the profile. This combination of opposite + // approaches is a bit awkward, particularly because direct calls are + // discoverable via the profile as well. Unify these into a single + // approach. + addIndirectEdges(g, namedEdgeMap) + + return g +} + +// visitIR traverses the body of each ir.Func adds edges to g from ir.Func to +// any called function in the body. +func visitIR(fn *ir.Func, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { + name := ir.LinkFuncName(fn) + node, ok := g.IRNodes[name] + if !ok { + node = &IRNode{ + AST: fn, + } + g.IRNodes[name] = node + } + + // Recursively walk over the body of the function to create IRGraph edges. + createIRGraphEdge(fn, node, name, namedEdgeMap, g) +} + +// createIRGraphEdge traverses the nodes in the body of ir.Func and adds edges +// between the callernode which points to the ir.Func and the nodes in the +// body. +func createIRGraphEdge(fn *ir.Func, callernode *IRNode, name string, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { + ir.VisitList(fn.Body, func(n ir.Node) { + switch n.Op() { + case ir.OCALLFUNC: + call := n.(*ir.CallExpr) + // Find the callee function from the call site and add the edge. + callee := DirectCallee(call.Fun) + if callee != nil { + addIREdge(callernode, name, n, callee, namedEdgeMap, g) + } + case ir.OCALLMETH: + call := n.(*ir.CallExpr) + // Find the callee method from the call site and add the edge. + callee := ir.MethodExprName(call.Fun).Func + addIREdge(callernode, name, n, callee, namedEdgeMap, g) + } + }) +} + +// NodeLineOffset returns the line offset of n in fn. +func NodeLineOffset(n ir.Node, fn *ir.Func) int { + // See "A note on line numbers" at the top of the file. + line := int(base.Ctxt.InnermostPos(n.Pos()).RelLine()) + startLine := int(base.Ctxt.InnermostPos(fn.Pos()).RelLine()) + return line - startLine +} + +// addIREdge adds an edge between caller and new node that points to `callee` +// based on the profile-graph and NodeMap. +func addIREdge(callerNode *IRNode, callerName string, call ir.Node, callee *ir.Func, namedEdgeMap pgo.NamedEdgeMap, g *IRGraph) { + calleeName := ir.LinkFuncName(callee) + calleeNode, ok := g.IRNodes[calleeName] + if !ok { + calleeNode = &IRNode{ + AST: callee, + } + g.IRNodes[calleeName] = calleeNode + } + + namedEdge := pgo.NamedCallEdge{ + CallerName: callerName, + CalleeName: calleeName, + CallSiteOffset: NodeLineOffset(call, callerNode.AST), + } + + // Add edge in the IRGraph from caller to callee. + edge := &IREdge{ + Src: callerNode, + Dst: calleeNode, + Weight: namedEdgeMap.Weight[namedEdge], + CallSiteOffset: namedEdge.CallSiteOffset, + } + + if callerNode.OutEdges == nil { + callerNode.OutEdges = make(map[pgo.NamedCallEdge]*IREdge) + } + callerNode.OutEdges[namedEdge] = edge +} + +// LookupFunc looks up a function or method in export data. It is expected to +// be overridden by package noder, to break a dependency cycle. +var LookupFunc = func(fullName string) (*ir.Func, error) { + base.Fatalf("pgo.LookupMethodFunc not overridden") + panic("unreachable") +} + +// addIndirectEdges adds indirect call edges found in the profile to the graph, +// to be used for devirtualization. +// +// N.B. despite the name, addIndirectEdges will add any edges discovered via +// the profile. We don't know for sure that they are indirect, but assume they +// are since direct calls would already be added. (e.g., direct calls that have +// been deleted from source since the profile was taken would be added here). +// +// TODO(prattmic): Devirtualization runs before inlining, so we can't devirtualize +// calls inside inlined call bodies. If we did add that, we'd need edges from +// inlined bodies as well. +func addIndirectEdges(g *IRGraph, namedEdgeMap pgo.NamedEdgeMap) { + // g.IRNodes is populated with the set of functions in the local + // package build by VisitIR. We want to filter for local functions + // below, but we also add unknown callees to IRNodes as we go. So make + // an initial copy of IRNodes to recall just the local functions. + localNodes := make(map[string]*IRNode, len(g.IRNodes)) + for k, v := range g.IRNodes { + localNodes[k] = v + } + + // N.B. We must consider edges in a stable order because export data + // lookup order (LookupMethodFunc, below) can impact the export data of + // this package, which must be stable across different invocations for + // reproducibility. + // + // The weight ordering of ByWeight is irrelevant, it just happens to be + // an ordered list of edges that is already available. + for _, key := range namedEdgeMap.ByWeight { + weight := namedEdgeMap.Weight[key] + // All callers in the local package build were added to IRNodes + // in VisitIR. If a caller isn't in the local package build we + // can skip adding edges, since we won't be devirtualizing in + // them anyway. This keeps the graph smaller. + callerNode, ok := localNodes[key.CallerName] + if !ok { + continue + } + + // Already handled this edge? + if _, ok := callerNode.OutEdges[key]; ok { + continue + } + + calleeNode, ok := g.IRNodes[key.CalleeName] + if !ok { + // IR is missing for this callee. VisitIR populates + // IRNodes with all functions discovered via local + // package function declarations and calls. This + // function may still be available from export data of + // a transitive dependency. + // + // TODO(prattmic): Parameterized types/functions are + // not supported. + // + // TODO(prattmic): This eager lookup during graph load + // is simple, but wasteful. We are likely to load many + // functions that we never need. We could delay load + // until we actually need the method in + // devirtualization. Instantiation of generic functions + // will likely need to be done at the devirtualization + // site, if at all. + fn, err := LookupFunc(key.CalleeName) + if err == nil { + if base.Debug.PGODebug >= 3 { + fmt.Printf("addIndirectEdges: %s found in export data\n", key.CalleeName) + } + calleeNode = &IRNode{AST: fn} + + // N.B. we could call createIRGraphEdge to add + // direct calls in this newly-imported + // function's body to the graph. Similarly, we + // could add to this function's queue to add + // indirect calls. However, those would be + // useless given the visit order of inlining, + // and the ordering of PGO devirtualization and + // inlining. This function can only be used as + // an inlined body. We will never do PGO + // devirtualization inside an inlined call. Nor + // will we perform inlining inside an inlined + // call. + } else { + // Still not found. Most likely this is because + // the callee isn't in the transitive deps of + // this package. + // + // Record this call anyway. If this is the hottest, + // then we want to skip devirtualization rather than + // devirtualizing to the second most common callee. + if base.Debug.PGODebug >= 3 { + fmt.Printf("addIndirectEdges: %s not found in export data: %v\n", key.CalleeName, err) + } + calleeNode = &IRNode{LinkerSymbolName: key.CalleeName} + } + + // Add dummy node back to IRNodes. We don't need this + // directly, but PrintWeightedCallGraphDOT uses these + // to print nodes. + g.IRNodes[key.CalleeName] = calleeNode + } + edge := &IREdge{ + Src: callerNode, + Dst: calleeNode, + Weight: weight, + CallSiteOffset: key.CallSiteOffset, + } + + if callerNode.OutEdges == nil { + callerNode.OutEdges = make(map[pgo.NamedCallEdge]*IREdge) + } + callerNode.OutEdges[key] = edge + } +} + +// PrintWeightedCallGraphDOT prints IRGraph in DOT format. +func (p *Profile) PrintWeightedCallGraphDOT(edgeThreshold float64) { + fmt.Printf("\ndigraph G {\n") + fmt.Printf("forcelabels=true;\n") + + // List of functions in this package. + funcs := make(map[string]struct{}) + ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { + for _, f := range list { + name := ir.LinkFuncName(f) + funcs[name] = struct{}{} + } + }) + + // Determine nodes of DOT. + // + // Note that ir.Func may be nil for functions not visible from this + // package. + nodes := make(map[string]*ir.Func) + for name := range funcs { + if n, ok := p.WeightedCG.IRNodes[name]; ok { + for _, e := range n.OutEdges { + if _, ok := nodes[e.Src.Name()]; !ok { + nodes[e.Src.Name()] = e.Src.AST + } + if _, ok := nodes[e.Dst.Name()]; !ok { + nodes[e.Dst.Name()] = e.Dst.AST + } + } + if _, ok := nodes[n.Name()]; !ok { + nodes[n.Name()] = n.AST + } + } + } + + // Print nodes. + for name, ast := range nodes { + if _, ok := p.WeightedCG.IRNodes[name]; ok { + style := "solid" + if ast == nil { + style = "dashed" + } + + if ast != nil && ast.Inl != nil { + fmt.Printf("\"%v\" [color=black, style=%s, label=\"%v,inl_cost=%d\"];\n", name, style, name, ast.Inl.Cost) + } else { + fmt.Printf("\"%v\" [color=black, style=%s, label=\"%v\"];\n", name, style, name) + } + } + } + // Print edges. + ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) { + for _, f := range list { + name := ir.LinkFuncName(f) + if n, ok := p.WeightedCG.IRNodes[name]; ok { + for _, e := range n.OutEdges { + style := "solid" + if e.Dst.AST == nil { + style = "dashed" + } + color := "black" + edgepercent := pgo.WeightInPercentage(e.Weight, p.TotalWeight) + if edgepercent > edgeThreshold { + color = "red" + } + + fmt.Printf("edge [color=%s, style=%s];\n", color, style) + fmt.Printf("\"%v\" -> \"%v\" [label=\"%.2f\"];\n", n.Name(), e.Dst.Name(), edgepercent) + } + } + } + }) + fmt.Printf("}\n") +} + +// DirectCallee takes a function-typed expression and returns the underlying +// function that it refers to if statically known. Otherwise, it returns nil. +// +// Equivalent to inline.inlCallee without calling CanInline on closures. +func DirectCallee(fn ir.Node) *ir.Func { + fn = ir.StaticValue(fn) + switch fn.Op() { + case ir.OMETHEXPR: + fn := fn.(*ir.SelectorExpr) + n := ir.MethodExprName(fn) + // Check that receiver type matches fn.X. + // TODO(mdempsky): Handle implicit dereference + // of pointer receiver argument? + if n == nil || !types.Identical(n.Type().Recv().Type, fn.X.Type()) { + return nil + } + return n.Func + case ir.ONAME: + fn := fn.(*ir.Name) + if fn.Class == ir.PFUNC { + return fn.Func + } + case ir.OCLOSURE: + fn := fn.(*ir.ClosureExpr) + c := fn.Func + return c + } + return nil +} -- cgit v1.2.3-54-g00ecf