2022-09-09 11:29:32 -07:00
// 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.
// WORK IN PROGRESS
2022-10-28 13:52:43 -04:00
// 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
2022-11-02 11:11:03 -04:00
// //line directives (ctxt.InnermostPos(ir.Node.Pos()).RelLine()). Use
// NodeLineOffset to compute line offsets.
2022-10-28 13:52:43 -04:00
//
// 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.
2022-09-09 11:29:32 -07:00
package pgo
import (
2022-10-28 13:52:43 -04:00
"cmd/compile/internal/base"
2022-09-09 11:29:32 -07:00
"cmd/compile/internal/ir"
"cmd/compile/internal/typecheck"
"cmd/compile/internal/types"
"fmt"
"internal/profile"
"log"
"os"
)
2022-10-28 17:34:43 -04:00
// IRGraph is the key datastrcture that is built from profile. It is
// essentially a call graph with nodes pointing to IRs of functions and edges
// carrying weights and callsite information. The graph is bidirectional that
// helps in removing nodes efficiently.
2022-09-09 11:29:32 -07:00
type IRGraph struct {
// Nodes of the graph
IRNodes map [ string ] * IRNode
OutEdges IREdgeMap
InEdges IREdgeMap
}
// IRNode represents a node in the IRGraph.
type IRNode struct {
// Pointer to the IR of the Function represented by this node.
AST * ir . Func
// Flat weight of the IRNode, obtained from profile.
Flat int64
// Cumulative weight of the IRNode.
Cum int64
}
// IREdgeMap maps an IRNode to its successors.
type IREdgeMap map [ * IRNode ] [ ] * IREdge
2022-10-28 17:34:43 -04:00
// IREdge represents a call edge in the IRGraph with source, destination,
// weight, callsite, and line number information.
2022-09-09 11:29:32 -07:00
type IREdge struct {
// Source and destination of the edge in IRNode.
2022-11-02 11:11:03 -04:00
Src , Dst * IRNode
Weight int64
CallSiteOffset int // Line offset from function start line.
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
// NodeMapKey represents a hash key to identify unique call-edges in profile
// and in IR. Used for deduplication of call edges found in profile.
2022-09-09 11:29:32 -07:00
type NodeMapKey struct {
2022-11-02 11:11:03 -04:00
CallerName string
CalleeName string
CallSiteOffset int // Line offset from function start line.
2022-09-09 11:29:32 -07:00
}
// Weights capture both node weight and edge weight.
type Weights struct {
NFlat int64
NCum int64
EWeight int64
}
// CallSiteInfo captures call-site information and its caller/callee.
type CallSiteInfo struct {
2022-11-02 11:11:03 -04:00
LineOffset int // Line offset from function start line.
Caller * ir . Func
Callee * ir . Func
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
// Profile contains the processed PGO profile and weighted call graph used for
// PGO optimizations.
type Profile struct {
// Aggregated NodeWeights and EdgeWeights across the profile. This
// helps us determine the percentage threshold for hot/cold
// partitioning.
TotalNodeWeight int64
TotalEdgeWeight int64
2022-09-09 11:29:32 -07:00
2022-10-28 17:34:43 -04:00
// NodeMap contains all unique call-edges in the profile and their
// aggregated weight.
NodeMap map [ NodeMapKey ] * Weights
2022-09-09 11:29:32 -07:00
2022-10-28 17:34:43 -04:00
// WeightedCG represents the IRGraph built from profile, which we will
// update as part of inlining.
WeightedCG * IRGraph
}
2022-09-09 11:29:32 -07:00
2022-10-28 17:34:43 -04:00
// New generates a profile-graph from the profile.
func New ( profileFile string ) * Profile {
2022-09-09 11:29:32 -07:00
f , err := os . Open ( profileFile )
if err != nil {
log . Fatal ( "failed to open file " + profileFile )
2022-10-28 17:34:43 -04:00
return nil
2022-09-09 11:29:32 -07:00
}
defer f . Close ( )
2022-10-28 17:34:43 -04:00
profile , err := profile . Parse ( f )
2022-09-09 11:29:32 -07:00
if err != nil {
log . Fatal ( "failed to Parse profile file." )
2022-10-28 17:34:43 -04:00
return nil
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
2022-11-03 21:11:23 -04:00
g := newGraph ( profile , & Options {
2022-09-09 11:29:32 -07:00
CallTree : false ,
SampleValue : func ( v [ ] int64 ) int64 { return v [ 1 ] } ,
2022-10-28 17:34:43 -04:00
} )
2022-09-09 11:29:32 -07:00
2022-10-28 17:34:43 -04:00
p := & Profile {
2022-11-02 11:11:03 -04:00
NodeMap : make ( map [ NodeMapKey ] * Weights ) ,
cmd/compile: use edge weights to decide inlineability in PGO
Currently, with PGO, the inliner uses node weights to decide if a
function is inlineable (with a larger budget). But the actual
inlining is determined by the weight of the call edge. There is a
discrepancy that, if a callee node is hot but the call edge is not,
it would not inlined, and marking the callee inlineable would of
no use.
Instead of using two kinds of weights, we just use the edge
weights to decide inlineability. If a function is the callee of a
hot call edge, its inlineability is determined with a larger
threshold. For a function that exceeds the regular inlining budget,
it is still inlined only when the call edge is hot, as it would
exceed the regular inlining cost for non-hot call sites, even if
it is marked inlineable.
For #55022.
Change-Id: I93fa9919fc6bcbb394e6cfe54ec96a96eede08f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/447015
Run-TryBot: Cherry Mui <cherryyz@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-01 11:40:29 -04:00
WeightedCG : & IRGraph {
2022-10-28 17:34:43 -04:00
IRNodes : make ( map [ string ] * IRNode ) ,
} ,
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
// Build the node map and totals from the profile graph.
2022-11-02 11:11:03 -04:00
p . processprofileGraph ( g )
2022-10-28 17:34:43 -04:00
2022-09-09 11:29:32 -07:00
// Create package-level call graph with weights from profile and IR.
2022-10-28 17:34:43 -04:00
p . initializeIRGraph ( )
return p
2022-09-09 11:29:32 -07:00
}
2022-11-02 11:11:03 -04:00
// processprofileGraph builds various maps from the profile-graph.
2022-10-28 17:34:43 -04:00
//
// It initializes NodeMap and Total{Node,Edge}Weight based on the name and
// callsite to compute node and edge weights which will be used later on to
// create edges for WeightedCG.
2022-11-02 11:11:03 -04:00
func ( p * Profile ) processprofileGraph ( g * Graph ) {
2022-09-09 11:29:32 -07:00
nFlat := make ( map [ string ] int64 )
nCum := make ( map [ string ] int64 )
2022-11-02 11:11:03 -04:00
seenStartLine := false
2022-09-09 11:29:32 -07:00
// Accummulate weights for the same node.
2022-11-02 11:11:03 -04:00
for _ , n := range g . Nodes {
2022-09-09 11:29:32 -07:00
canonicalName := n . Info . Name
nFlat [ canonicalName ] += n . FlatValue ( )
nCum [ canonicalName ] += n . CumValue ( )
}
2022-11-02 11:11:03 -04:00
// Process graph and build various node and edge maps which will
2022-10-28 17:34:43 -04:00
// be consumed by AST walk.
2022-11-02 11:11:03 -04:00
for _ , n := range g . Nodes {
seenStartLine = seenStartLine || n . Info . StartLine != 0
2022-10-28 17:34:43 -04:00
p . TotalNodeWeight += n . FlatValue ( )
2022-09-09 11:29:32 -07:00
canonicalName := n . Info . Name
2022-11-02 11:11:03 -04:00
// Create the key to the nodeMapKey.
2022-09-09 11:29:32 -07:00
nodeinfo := NodeMapKey {
2022-11-02 11:11:03 -04:00
CallerName : canonicalName ,
CallSiteOffset : n . Info . Lineno - n . Info . StartLine ,
2022-09-09 11:29:32 -07:00
}
for _ , e := range n . Out {
2022-10-28 17:34:43 -04:00
p . TotalEdgeWeight += e . WeightValue ( )
2022-09-09 11:29:32 -07:00
nodeinfo . CalleeName = e . Dest . Info . Name
2022-10-28 17:34:43 -04:00
if w , ok := p . NodeMap [ nodeinfo ] ; ok {
2022-09-09 11:29:32 -07:00
w . EWeight += e . WeightValue ( )
} else {
weights := new ( Weights )
weights . NFlat = nFlat [ canonicalName ]
weights . NCum = nCum [ canonicalName ]
weights . EWeight = e . WeightValue ( )
2022-10-28 17:34:43 -04:00
p . NodeMap [ nodeinfo ] = weights
2022-09-09 11:29:32 -07:00
}
}
}
2022-11-02 11:11:03 -04:00
if ! seenStartLine {
// TODO(prattic): If Function.start_line is missing we could
// fall back to using absolute line numbers, which is better
// than nothing.
log . Fatal ( "PGO profile missing Function.start_line data" )
}
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
// initializeIRGraph builds the IRGraph by visting all the ir.Func in decl list
// of a package.
func ( p * Profile ) initializeIRGraph ( ) {
2022-09-09 11:29:32 -07:00
// Bottomup walk over the function to create IRGraph.
ir . VisitFuncsBottomUp ( typecheck . Target . Decls , func ( list [ ] * ir . Func , recursive bool ) {
for _ , n := range list {
2022-10-28 17:34:43 -04:00
p . VisitIR ( n , recursive )
2022-09-09 11:29:32 -07:00
}
} )
}
2022-10-28 17:34:43 -04:00
// VisitIR traverses the body of each ir.Func and use NodeMap to determine if
// we need to add an edge from ir.Func and any node in the ir.Func body.
func ( p * Profile ) VisitIR ( fn * ir . Func , recursive bool ) {
g := p . WeightedCG
2022-09-09 11:29:32 -07:00
if g . IRNodes == nil {
g . IRNodes = make ( map [ string ] * IRNode )
}
if g . OutEdges == nil {
g . OutEdges = make ( map [ * IRNode ] [ ] * IREdge )
}
if g . InEdges == nil {
g . InEdges = make ( map [ * IRNode ] [ ] * IREdge )
}
name := ir . PkgFuncName ( fn )
node := new ( IRNode )
node . AST = fn
if g . IRNodes [ name ] == nil {
g . IRNodes [ name ] = node
}
// Create the key for the NodeMapKey.
nodeinfo := NodeMapKey {
2022-11-02 11:11:03 -04:00
CallerName : name ,
CalleeName : "" ,
CallSiteOffset : 0 ,
2022-09-09 11:29:32 -07:00
}
// If the node exists, then update its node weight.
2022-10-28 17:34:43 -04:00
if weights , ok := p . NodeMap [ nodeinfo ] ; ok {
2022-09-09 11:29:32 -07:00
g . IRNodes [ name ] . Flat = weights . NFlat
g . IRNodes [ name ] . Cum = weights . NCum
}
// Recursively walk over the body of the function to create IRGraph edges.
2022-10-28 17:34:43 -04:00
p . createIRGraphEdge ( fn , g . IRNodes [ name ] , name )
2022-09-09 11:29:32 -07:00
}
2022-11-02 11:11:03 -04:00
// 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
}
2022-10-28 17:34:43 -04:00
// addIREdge adds an edge between caller and new node that points to `callee`
// based on the profile-graph and NodeMap.
2022-11-02 11:11:03 -04:00
func ( p * Profile ) addIREdge ( caller * IRNode , callername string , call ir . Node , callee * ir . Func ) {
2022-10-28 17:34:43 -04:00
g := p . WeightedCG
2022-09-09 11:29:32 -07:00
// Create an IRNode for the callee.
calleenode := new ( IRNode )
calleenode . AST = callee
calleename := ir . PkgFuncName ( callee )
// Create key for NodeMapKey.
nodeinfo := NodeMapKey {
2022-11-02 11:11:03 -04:00
CallerName : callername ,
CalleeName : calleename ,
CallSiteOffset : NodeLineOffset ( call , caller . AST ) ,
2022-09-09 11:29:32 -07:00
}
// Create the callee node with node weight.
if g . IRNodes [ calleename ] == nil {
g . IRNodes [ calleename ] = calleenode
nodeinfo2 := NodeMapKey {
2022-11-02 11:11:03 -04:00
CallerName : calleename ,
CalleeName : "" ,
CallSiteOffset : 0 ,
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
if weights , ok := p . NodeMap [ nodeinfo2 ] ; ok {
2022-09-09 11:29:32 -07:00
g . IRNodes [ calleename ] . Flat = weights . NFlat
g . IRNodes [ calleename ] . Cum = weights . NCum
}
}
2022-10-28 17:34:43 -04:00
if weights , ok := p . NodeMap [ nodeinfo ] ; ok {
2022-09-09 11:29:32 -07:00
caller . Flat = weights . NFlat
caller . Cum = weights . NCum
// Add edge in the IRGraph from caller to callee.
2022-11-02 11:11:03 -04:00
info := & IREdge { Src : caller , Dst : g . IRNodes [ calleename ] , Weight : weights . EWeight , CallSiteOffset : nodeinfo . CallSiteOffset }
2022-09-09 11:29:32 -07:00
g . OutEdges [ caller ] = append ( g . OutEdges [ caller ] , info )
g . InEdges [ g . IRNodes [ calleename ] ] = append ( g . InEdges [ g . IRNodes [ calleename ] ] , info )
} else {
nodeinfo . CalleeName = ""
2022-11-02 11:11:03 -04:00
nodeinfo . CallSiteOffset = 0
2022-10-28 17:34:43 -04:00
if weights , ok := p . NodeMap [ nodeinfo ] ; ok {
2022-09-09 11:29:32 -07:00
caller . Flat = weights . NFlat
caller . Cum = weights . NCum
2022-11-02 11:11:03 -04:00
info := & IREdge { Src : caller , Dst : g . IRNodes [ calleename ] , Weight : 0 , CallSiteOffset : nodeinfo . CallSiteOffset }
2022-09-09 11:29:32 -07:00
g . OutEdges [ caller ] = append ( g . OutEdges [ caller ] , info )
g . InEdges [ g . IRNodes [ calleename ] ] = append ( g . InEdges [ g . IRNodes [ calleename ] ] , info )
} else {
2022-11-02 11:11:03 -04:00
info := & IREdge { Src : caller , Dst : g . IRNodes [ calleename ] , Weight : 0 , CallSiteOffset : nodeinfo . CallSiteOffset }
2022-09-09 11:29:32 -07:00
g . OutEdges [ caller ] = append ( g . OutEdges [ caller ] , info )
g . InEdges [ g . IRNodes [ calleename ] ] = append ( g . InEdges [ g . IRNodes [ calleename ] ] , info )
}
}
}
// 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.
2022-10-28 17:34:43 -04:00
func ( p * Profile ) createIRGraphEdge ( fn * ir . Func , callernode * IRNode , name string ) {
2022-09-09 11:29:32 -07:00
var doNode func ( ir . Node ) bool
doNode = func ( n ir . Node ) bool {
switch n . Op ( ) {
default :
ir . DoChildren ( n , doNode )
case ir . OCALLFUNC :
call := n . ( * ir . CallExpr )
// Find the callee function from the call site and add the edge.
2022-11-02 11:11:03 -04:00
callee := inlCallee ( call . X )
if callee != nil {
p . addIREdge ( callernode , name , n , callee )
2022-09-09 11:29:32 -07:00
}
case ir . OCALLMETH :
call := n . ( * ir . CallExpr )
// Find the callee method from the call site and add the edge.
2022-11-02 11:11:03 -04:00
callee := ir . MethodExprName ( call . X ) . Func
p . addIREdge ( callernode , name , n , callee )
2022-09-09 11:29:32 -07:00
}
return false
}
doNode ( fn )
}
// WeightInPercentage converts profile weights to a percentage.
func WeightInPercentage ( value int64 , total int64 ) float64 {
var ratio float64
if total != 0 {
ratio = ( float64 ( value ) / float64 ( total ) ) * 100
}
return ratio
}
// PrintWeightedCallGraphDOT prints IRGraph in DOT format.
cmd/compile: use edge weights to decide inlineability in PGO
Currently, with PGO, the inliner uses node weights to decide if a
function is inlineable (with a larger budget). But the actual
inlining is determined by the weight of the call edge. There is a
discrepancy that, if a callee node is hot but the call edge is not,
it would not inlined, and marking the callee inlineable would of
no use.
Instead of using two kinds of weights, we just use the edge
weights to decide inlineability. If a function is the callee of a
hot call edge, its inlineability is determined with a larger
threshold. For a function that exceeds the regular inlining budget,
it is still inlined only when the call edge is hot, as it would
exceed the regular inlining cost for non-hot call sites, even if
it is marked inlineable.
For #55022.
Change-Id: I93fa9919fc6bcbb394e6cfe54ec96a96eede08f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/447015
Run-TryBot: Cherry Mui <cherryyz@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-01 11:40:29 -04:00
func ( p * Profile ) PrintWeightedCallGraphDOT ( edgeThreshold float64 ) {
2022-09-09 11:29:32 -07:00
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 . Decls , func ( list [ ] * ir . Func , recursive bool ) {
for _ , f := range list {
name := ir . PkgFuncName ( f )
funcs [ name ] = struct { } { }
}
} )
// Determine nodes of DOT.
nodes := make ( map [ string ] * ir . Func )
for name , _ := range funcs {
2022-10-28 17:34:43 -04:00
if n , ok := p . WeightedCG . IRNodes [ name ] ; ok {
for _ , e := range p . WeightedCG . OutEdges [ n ] {
2022-09-09 11:29:32 -07:00
if _ , ok := nodes [ ir . PkgFuncName ( e . Src . AST ) ] ; ! ok {
nodes [ ir . PkgFuncName ( e . Src . AST ) ] = e . Src . AST
}
if _ , ok := nodes [ ir . PkgFuncName ( e . Dst . AST ) ] ; ! ok {
nodes [ ir . PkgFuncName ( e . Dst . AST ) ] = e . Dst . AST
}
}
if _ , ok := nodes [ ir . PkgFuncName ( n . AST ) ] ; ! ok {
nodes [ ir . PkgFuncName ( n . AST ) ] = n . AST
}
}
}
// Print nodes.
for name , ast := range nodes {
2022-10-28 17:34:43 -04:00
if n , ok := p . WeightedCG . IRNodes [ name ] ; ok {
nodeweight := WeightInPercentage ( n . Flat , p . TotalNodeWeight )
2022-09-09 11:29:32 -07:00
color := "black"
if ast . Inl != nil {
fmt . Printf ( "\"%v\" [color=%v,label=\"%v,freq=%.2f,inl_cost=%d\"];\n" , ir . PkgFuncName ( ast ) , color , ir . PkgFuncName ( ast ) , nodeweight , ast . Inl . Cost )
} else {
fmt . Printf ( "\"%v\" [color=%v, label=\"%v,freq=%.2f\"];\n" , ir . PkgFuncName ( ast ) , color , ir . PkgFuncName ( ast ) , nodeweight )
}
}
}
// Print edges.
ir . VisitFuncsBottomUp ( typecheck . Target . Decls , func ( list [ ] * ir . Func , recursive bool ) {
for _ , f := range list {
name := ir . PkgFuncName ( f )
2022-10-28 17:34:43 -04:00
if n , ok := p . WeightedCG . IRNodes [ name ] ; ok {
for _ , e := range p . WeightedCG . OutEdges [ n ] {
edgepercent := WeightInPercentage ( e . Weight , p . TotalEdgeWeight )
2022-09-09 11:29:32 -07:00
if edgepercent > edgeThreshold {
fmt . Printf ( "edge [color=red, style=solid];\n" )
} else {
fmt . Printf ( "edge [color=black, style=solid];\n" )
}
fmt . Printf ( "\"%v\" -> \"%v\" [label=\"%.2f\"];\n" , ir . PkgFuncName ( n . AST ) , ir . PkgFuncName ( e . Dst . AST ) , edgepercent )
}
}
}
} )
fmt . Printf ( "}\n" )
}
2022-10-31 12:20:09 -04:00
// RedirectEdges deletes and redirects out-edges from node cur based on
// inlining information via inlinedCallSites.
//
// CallSiteInfo.Callee must be nil.
2022-10-28 17:34:43 -04:00
func ( p * Profile ) RedirectEdges ( cur * IRNode , inlinedCallSites map [ CallSiteInfo ] struct { } ) {
g := p . WeightedCG
2022-09-09 11:29:32 -07:00
for i , outEdge := range g . OutEdges [ cur ] {
2022-11-02 11:11:03 -04:00
if _ , found := inlinedCallSites [ CallSiteInfo { LineOffset : outEdge . CallSiteOffset , Caller : cur . AST } ] ; ! found {
2022-09-09 11:29:32 -07:00
for _ , InEdge := range g . InEdges [ cur ] {
2022-11-02 11:11:03 -04:00
if _ , ok := inlinedCallSites [ CallSiteInfo { LineOffset : InEdge . CallSiteOffset , Caller : InEdge . Src . AST } ] ; ok {
2022-10-28 17:34:43 -04:00
weight := g . calculateWeight ( InEdge . Src , cur )
g . redirectEdge ( InEdge . Src , cur , outEdge , weight , i )
2022-09-09 11:29:32 -07:00
}
}
} else {
2022-11-02 11:11:03 -04:00
g . remove ( cur , i )
2022-09-09 11:29:32 -07:00
}
}
}
2022-10-28 17:34:43 -04:00
// redirectEdges deletes the cur node out-edges and redirect them so now these
// edges are the parent node out-edges.
func ( g * IRGraph ) redirectEdges ( parent * IRNode , cur * IRNode ) {
for _ , outEdge := range g . OutEdges [ cur ] {
outEdge . Src = parent
g . OutEdges [ parent ] = append ( g . OutEdges [ parent ] , outEdge )
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
delete ( g . OutEdges , cur )
2022-09-09 11:29:32 -07:00
}
2022-10-28 17:34:43 -04:00
// redirectEdge deletes the cur-node's out-edges and redirect them so now these
// edges are the parent node out-edges.
func ( g * IRGraph ) redirectEdge ( parent * IRNode , cur * IRNode , outEdge * IREdge , weight int64 , idx int ) {
2022-09-09 11:29:32 -07:00
outEdge . Src = parent
outEdge . Weight = weight * outEdge . Weight
g . OutEdges [ parent ] = append ( g . OutEdges [ parent ] , outEdge )
2022-11-02 11:11:03 -04:00
g . remove ( cur , idx )
2022-09-09 11:29:32 -07:00
}
// remove deletes the cur-node's out-edges at index idx.
2022-11-02 11:11:03 -04:00
func ( g * IRGraph ) remove ( cur * IRNode , i int ) {
2022-09-09 11:29:32 -07:00
if len ( g . OutEdges [ cur ] ) >= 2 {
2022-11-02 11:11:03 -04:00
g . OutEdges [ cur ] [ i ] = g . OutEdges [ cur ] [ len ( g . OutEdges [ cur ] ) - 1 ]
g . OutEdges [ cur ] = g . OutEdges [ cur ] [ : len ( g . OutEdges [ cur ] ) - 1 ]
2022-09-09 11:29:32 -07:00
} else {
delete ( g . OutEdges , cur )
}
}
2022-10-28 17:34:43 -04:00
// calculateWeight calculates the weight of the new redirected edge.
func ( g * IRGraph ) calculateWeight ( parent * IRNode , cur * IRNode ) int64 {
sum := int64 ( 0 )
pw := int64 ( 0 )
for _ , InEdge := range g . InEdges [ cur ] {
sum = sum + InEdge . Weight
if InEdge . Src == parent {
pw = InEdge . Weight
}
}
weight := int64 ( 0 )
if sum != 0 {
weight = pw / sum
} else {
weight = pw
}
return weight
}
2022-09-09 11:29:32 -07:00
// inlCallee is same as the implementation for inl.go with one change. The change is that we do not invoke CanInline on a closure.
func inlCallee ( 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
}