| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package inline |
|
|
| import ( |
| "fmt" |
| "go/constant" |
| "internal/buildcfg" |
| "strconv" |
| "strings" |
|
|
| "cmd/compile/internal/base" |
| "cmd/compile/internal/inline/inlheur" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/logopt" |
| "cmd/compile/internal/pgoir" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/internal/obj" |
| "cmd/internal/pgo" |
| "cmd/internal/src" |
| ) |
|
|
| |
| const ( |
| inlineMaxBudget = 80 |
| inlineExtraAppendCost = 0 |
| |
| inlineExtraCallCost = 57 |
| inlineParamCallCost = 17 |
| inlineExtraPanicCost = 1 |
| inlineExtraThrowCost = inlineMaxBudget |
|
|
| inlineBigFunctionNodes = 5000 |
| inlineBigFunctionMaxCost = 20 |
| inlineClosureCalledOnceCost = 10 * inlineMaxBudget |
| ) |
|
|
| var ( |
| |
| |
| candHotCalleeMap = make(map[*pgoir.IRNode]struct{}) |
|
|
| |
| hasHotCall = make(map[*ir.Func]struct{}) |
|
|
| |
| |
| candHotEdgeMap = make(map[pgoir.CallSiteInfo]struct{}) |
|
|
| |
| inlineHotCallSiteThresholdPercent float64 |
|
|
| |
| |
| |
| |
| inlineCDFHotCallSiteThresholdPercent = float64(99) |
|
|
| |
| inlineHotMaxBudget int32 = 2000 |
| ) |
|
|
| func IsPgoHotFunc(fn *ir.Func, profile *pgoir.Profile) bool { |
| if profile == nil { |
| return false |
| } |
| if n, ok := profile.WeightedCG.IRNodes[ir.LinkFuncName(fn)]; ok { |
| _, ok := candHotCalleeMap[n] |
| return ok |
| } |
| return false |
| } |
|
|
| func HasPgoHotInline(fn *ir.Func) bool { |
| _, has := hasHotCall[fn] |
| return has |
| } |
|
|
| |
| func PGOInlinePrologue(p *pgoir.Profile) { |
| if base.Debug.PGOInlineCDFThreshold != "" { |
| if s, err := strconv.ParseFloat(base.Debug.PGOInlineCDFThreshold, 64); err == nil && s >= 0 && s <= 100 { |
| inlineCDFHotCallSiteThresholdPercent = s |
| } else { |
| base.Fatalf("invalid PGOInlineCDFThreshold, must be between 0 and 100") |
| } |
| } |
| var hotCallsites []pgo.NamedCallEdge |
| inlineHotCallSiteThresholdPercent, hotCallsites = hotNodesFromCDF(p) |
| if base.Debug.PGODebug > 0 { |
| fmt.Printf("hot-callsite-thres-from-CDF=%v\n", inlineHotCallSiteThresholdPercent) |
| } |
|
|
| if x := base.Debug.PGOInlineBudget; x != 0 { |
| inlineHotMaxBudget = int32(x) |
| } |
|
|
| for _, n := range hotCallsites { |
| |
| if callee := p.WeightedCG.IRNodes[n.CalleeName]; callee != nil { |
| candHotCalleeMap[callee] = struct{}{} |
| } |
| |
| if caller := p.WeightedCG.IRNodes[n.CallerName]; caller != nil && caller.AST != nil { |
| csi := pgoir.CallSiteInfo{LineOffset: n.CallSiteOffset, Caller: caller.AST} |
| candHotEdgeMap[csi] = struct{}{} |
| } |
| } |
|
|
| if base.Debug.PGODebug >= 3 { |
| fmt.Printf("hot-cg before inline in dot format:") |
| p.PrintWeightedCallGraphDOT(inlineHotCallSiteThresholdPercent) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func hotNodesFromCDF(p *pgoir.Profile) (float64, []pgo.NamedCallEdge) { |
| cum := int64(0) |
| for i, n := range p.NamedEdgeMap.ByWeight { |
| w := p.NamedEdgeMap.Weight[n] |
| cum += w |
| if pgo.WeightInPercentage(cum, p.TotalWeight) > inlineCDFHotCallSiteThresholdPercent { |
| |
| |
| |
| return pgo.WeightInPercentage(w, p.TotalWeight), p.NamedEdgeMap.ByWeight[:i+1] |
| } |
| } |
| return 0, p.NamedEdgeMap.ByWeight |
| } |
|
|
| |
| func CanInlineFuncs(funcs []*ir.Func, profile *pgoir.Profile) { |
| if profile != nil { |
| PGOInlinePrologue(profile) |
| } |
|
|
| if base.Flag.LowerL == 0 { |
| return |
| } |
|
|
| ir.VisitFuncsBottomUp(funcs, func(funcs []*ir.Func, recursive bool) { |
| for _, fn := range funcs { |
| CanInline(fn, profile) |
| if inlheur.Enabled() { |
| analyzeFuncProps(fn, profile) |
| } |
| } |
| }) |
| } |
|
|
| func simdCreditMultiplier(fn *ir.Func) int32 { |
| for _, field := range fn.Type().RecvParamsResults() { |
| if field.Type.IsSIMD() { |
| return 3 |
| } |
| } |
| |
| |
| |
| |
| for _, v := range fn.ClosureVars { |
| if v.Type().IsSIMD() { |
| return 11 |
| } |
| } |
|
|
| return 1 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func inlineBudget(fn *ir.Func, profile *pgoir.Profile, relaxed bool, verbose bool) int32 { |
| |
| budget := int32(inlineMaxBudget) |
|
|
| budget *= simdCreditMultiplier(fn) |
|
|
| if IsPgoHotFunc(fn, profile) { |
| budget = inlineHotMaxBudget |
| if verbose { |
| fmt.Printf("hot-node enabled increased budget=%v for func=%v\n", budget, ir.PkgFuncName(fn)) |
| } |
| } |
| if relaxed { |
| budget += inlheur.BudgetExpansion(inlineMaxBudget) |
| } |
| if fn.ClosureParent != nil { |
| |
| budget = max(budget, inlineClosureCalledOnceCost) |
| } |
|
|
| return budget |
| } |
|
|
| |
| |
| |
| func CanInline(fn *ir.Func, profile *pgoir.Profile) { |
| if fn.Nname == nil { |
| base.Fatalf("CanInline no nname %+v", fn) |
| } |
|
|
| var reason string |
| if base.Flag.LowerM > 1 || logopt.Enabled() { |
| defer func() { |
| if reason != "" { |
| if base.Flag.LowerM > 1 { |
| fmt.Printf("%v: cannot inline %v: %s\n", ir.Line(fn), fn.Nname, reason) |
| } |
| if logopt.Enabled() { |
| logopt.LogOpt(fn.Pos(), "cannotInlineFunction", "inline", ir.FuncName(fn), reason) |
| } |
| } |
| }() |
| } |
|
|
| reason = InlineImpossible(fn) |
| if reason != "" { |
| return |
| } |
| if fn.Typecheck() == 0 { |
| base.Fatalf("CanInline on non-typechecked function %v", fn) |
| } |
|
|
| n := fn.Nname |
| if n.Func.InlinabilityChecked() { |
| return |
| } |
| defer n.Func.SetInlinabilityChecked(true) |
|
|
| cc := int32(inlineExtraCallCost) |
| if base.Flag.LowerL == 4 { |
| cc = 1 |
| } |
|
|
| |
| relaxed := inlheur.Enabled() |
|
|
| |
| budget := inlineBudget(fn, profile, relaxed, base.Debug.PGODebug > 0) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| visitor := hairyVisitor{ |
| curFunc: fn, |
| debug: isDebugFn(fn), |
| isBigFunc: IsBigFunc(fn), |
| budget: budget, |
| maxBudget: budget, |
| extraCallCost: cc, |
| profile: profile, |
| } |
| if visitor.tooHairy(fn) { |
| reason = visitor.reason |
| return |
| } |
|
|
| n.Func.Inl = &ir.Inline{ |
| Cost: budget - visitor.budget, |
| Dcl: pruneUnusedAutos(n.Func.Dcl, &visitor), |
| HaveDcl: true, |
| CanDelayResults: canDelayResults(fn), |
| } |
| if base.Flag.LowerM != 0 || logopt.Enabled() { |
| noteInlinableFunc(n, fn, budget-visitor.budget) |
| } |
| } |
|
|
| |
| |
| func noteInlinableFunc(n *ir.Name, fn *ir.Func, cost int32) { |
| if base.Flag.LowerM > 1 { |
| fmt.Printf("%v: can inline %v with cost %d as: %v { %v }\n", ir.Line(fn), n, cost, fn.Type(), fn.Body) |
| } else if base.Flag.LowerM != 0 { |
| fmt.Printf("%v: can inline %v\n", ir.Line(fn), n) |
| } |
| |
| if logopt.Enabled() { |
| logopt.LogOpt(fn.Pos(), "canInlineFunction", "inline", ir.FuncName(fn), fmt.Sprintf("cost: %d", cost)) |
| } |
| } |
|
|
| |
| |
| func InlineImpossible(fn *ir.Func) string { |
| var reason string |
| if fn.Nname == nil { |
| reason = "no name" |
| return reason |
| } |
|
|
| |
| if fn.Pragma&ir.Noinline != 0 { |
| reason = "marked go:noinline" |
| return reason |
| } |
|
|
| |
| if base.Flag.Race && fn.Pragma&ir.Norace != 0 { |
| reason = "marked go:norace with -race compilation" |
| return reason |
| } |
|
|
| |
| if base.Debug.Checkptr != 0 && fn.Pragma&ir.NoCheckPtr != 0 { |
| reason = "marked go:nocheckptr" |
| return reason |
| } |
|
|
| |
| |
| if fn.Pragma&ir.CgoUnsafeArgs != 0 { |
| reason = "marked go:cgo_unsafe_args" |
| return reason |
| } |
|
|
| |
| |
| |
| |
| |
| |
| if fn.Pragma&ir.UintptrKeepAlive != 0 { |
| reason = "marked as having a keep-alive uintptr argument" |
| return reason |
| } |
|
|
| |
| |
| if fn.Pragma&ir.UintptrEscapes != 0 { |
| reason = "marked as having an escaping uintptr argument" |
| return reason |
| } |
|
|
| |
| |
| |
| if fn.Pragma&ir.Yeswritebarrierrec != 0 { |
| reason = "marked go:yeswritebarrierrec" |
| return reason |
| } |
|
|
| |
| |
| if len(fn.Body) == 0 && !typecheck.HaveInlineBody(fn) { |
| reason = "no function body" |
| return reason |
| } |
|
|
| return "" |
| } |
|
|
| |
| |
| func canDelayResults(fn *ir.Func) bool { |
| |
| |
| |
| |
|
|
| nreturns := 0 |
| ir.VisitList(fn.Body, func(n ir.Node) { |
| if n, ok := n.(*ir.ReturnStmt); ok { |
| nreturns++ |
| if len(n.Results) == 0 { |
| nreturns++ |
| } |
| } |
| }) |
|
|
| if nreturns != 1 { |
| return false |
| } |
|
|
| |
| for _, param := range fn.Type().Results() { |
| if sym := param.Sym; sym != nil && !sym.IsBlank() { |
| return false |
| } |
| } |
|
|
| return true |
| } |
|
|
| |
| |
| type hairyVisitor struct { |
| |
| curFunc *ir.Func |
| isBigFunc bool |
| debug bool |
| budget int32 |
| maxBudget int32 |
| reason string |
| extraCallCost int32 |
| usedLocals ir.NameSet |
| do func(ir.Node) bool |
| profile *pgoir.Profile |
| } |
|
|
| func isDebugFn(fn *ir.Func) bool { |
| |
| |
| |
| |
| |
| |
| return false |
| } |
|
|
| func (v *hairyVisitor) tooHairy(fn *ir.Func) bool { |
| v.do = v.doNode |
| if ir.DoChildren(fn, v.do) { |
| return true |
| } |
| if v.budget < 0 { |
| v.reason = fmt.Sprintf("function too complex: cost %d exceeds budget %d", v.maxBudget-v.budget, v.maxBudget) |
| return true |
| } |
| return false |
| } |
|
|
| |
| |
| func (v *hairyVisitor) doNode(n ir.Node) bool { |
| if n == nil { |
| return false |
| } |
| if v.debug { |
| fmt.Printf("%v: doNode %v budget is %d\n", ir.Line(n), n.Op(), v.budget) |
| } |
| opSwitch: |
| switch n.Op() { |
| |
| case ir.OCALLFUNC: |
| n := n.(*ir.CallExpr) |
| var cheap bool |
| if n.Fun.Op() == ir.ONAME { |
| name := n.Fun.(*ir.Name) |
| if name.Class == ir.PFUNC { |
| s := name.Sym() |
| fn := s.Name |
| switch s.Pkg.Path { |
| case "internal/abi": |
| switch fn { |
| case "NoEscape": |
| |
| |
| |
| cheap = true |
| } |
| if strings.HasPrefix(fn, "EscapeNonString[") { |
| |
| |
| cheap = true |
| } |
| case "internal/runtime/sys": |
| switch fn { |
| case "GetCallerPC", "GetCallerSP": |
| |
| |
| |
| v.reason = "call to " + fn |
| return true |
| } |
| case "go.runtime": |
| switch fn { |
| case "throw": |
| |
| v.budget -= inlineExtraThrowCost |
| break opSwitch |
| case "panicrangestate": |
| cheap = true |
| case "deferrangefunc": |
| v.reason = "defer call in range func" |
| return true |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if isAtomicCoverageCounterUpdate(n) { |
| return false |
| } |
| } |
| if n.Fun.Op() == ir.OMETHEXPR { |
| if meth := ir.MethodExprName(n.Fun); meth != nil { |
| if fn := meth.Func; fn != nil { |
| s := fn.Sym() |
| if types.RuntimeSymName(s) == "heapBits.nextArena" { |
| |
| |
| |
| cheap = true |
| } |
| |
| |
| |
| |
| if base.Ctxt.Arch.CanMergeLoads && s.Pkg.Path == "encoding/binary" { |
| switch s.Name { |
| case "littleEndian.Uint64", "littleEndian.Uint32", "littleEndian.Uint16", |
| "bigEndian.Uint64", "bigEndian.Uint32", "bigEndian.Uint16", |
| "littleEndian.PutUint64", "littleEndian.PutUint32", "littleEndian.PutUint16", |
| "bigEndian.PutUint64", "bigEndian.PutUint32", "bigEndian.PutUint16", |
| "littleEndian.AppendUint64", "littleEndian.AppendUint32", "littleEndian.AppendUint16", |
| "bigEndian.AppendUint64", "bigEndian.AppendUint32", "bigEndian.AppendUint16": |
| cheap = true |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| |
| extraCost := v.extraCallCost |
|
|
| if n.Fun.Op() == ir.ONAME { |
| name := n.Fun.(*ir.Name) |
| if name.Class == ir.PFUNC { |
| |
| |
| |
| |
| if base.Ctxt.Arch.CanMergeLoads && name.Sym().Pkg.Path == "internal/byteorder" { |
| switch name.Sym().Name { |
| case "LEUint64", "LEUint32", "LEUint16", |
| "BEUint64", "BEUint32", "BEUint16", |
| "LEPutUint64", "LEPutUint32", "LEPutUint16", |
| "BEPutUint64", "BEPutUint32", "BEPutUint16", |
| "LEAppendUint64", "LEAppendUint32", "LEAppendUint16", |
| "BEAppendUint64", "BEAppendUint32", "BEAppendUint16": |
| cheap = true |
| } |
| } |
| } |
| if name.Class == ir.PPARAM || name.Class == ir.PAUTOHEAP && name.IsClosureVar() { |
| extraCost = min(extraCost, inlineParamCallCost) |
| } |
| } |
|
|
| if cheap { |
| if v.debug { |
| if ir.IsIntrinsicCall(n) { |
| fmt.Printf("%v: cheap call is also intrinsic, %v\n", ir.Line(n), n) |
| } |
| } |
| break |
| } |
|
|
| if ir.IsIntrinsicCall(n) { |
| if v.debug { |
| fmt.Printf("%v: intrinsic call, %v\n", ir.Line(n), n) |
| } |
| break |
| } |
|
|
| if callee := inlCallee(v.curFunc, n.Fun, v.profile, false); callee != nil && typecheck.HaveInlineBody(callee) { |
| |
| |
| |
| if ok, _, _ := canInlineCallExpr(v.curFunc, n, callee, v.isBigFunc, false, false); ok { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| v.budget -= callee.Inl.Cost |
| break |
| } |
| } |
|
|
| if v.debug { |
| fmt.Printf("%v: costly OCALLFUNC %v\n", ir.Line(n), n) |
| } |
|
|
| |
| v.budget -= extraCost |
|
|
| case ir.OCALLMETH: |
| base.FatalfAt(n.Pos(), "OCALLMETH missed by typecheck") |
|
|
| |
| case ir.OCALL, ir.OCALLINTER: |
| |
| if v.debug { |
| fmt.Printf("%v: costly OCALL %v\n", ir.Line(n), n) |
| } |
| v.budget -= v.extraCallCost |
|
|
| case ir.OPANIC: |
| n := n.(*ir.UnaryExpr) |
| if n.X.Op() == ir.OCONVIFACE && n.X.(*ir.ConvExpr).Implicit() { |
| |
| |
| |
| v.budget++ |
| } |
| v.budget -= inlineExtraPanicCost |
|
|
| case ir.ORECOVER: |
| |
| v.reason = "call to recover" |
| return true |
|
|
| case ir.OCLOSURE: |
| if base.Debug.InlFuncsWithClosures == 0 { |
| v.reason = "not inlining functions with closures" |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| |
| v.budget -= 15 |
|
|
| case ir.OGO, ir.ODEFER, ir.OTAILCALL: |
| v.reason = "unhandled op " + n.Op().String() |
| return true |
|
|
| case ir.OAPPEND: |
| v.budget -= inlineExtraAppendCost |
|
|
| case ir.OADDR: |
| n := n.(*ir.AddrExpr) |
| |
| if dot, ok := n.X.(*ir.SelectorExpr); ok && (dot.Op() == ir.ODOT || dot.Op() == ir.ODOTPTR) { |
| if _, ok := dot.X.(*ir.Name); ok && dot.Selection.Offset == 0 { |
| v.budget += 2 |
| } |
| } |
|
|
| case ir.ODEREF: |
| |
| n := n.(*ir.StarExpr) |
|
|
| ptr := n.X |
| for ptr.Op() == ir.OCONVNOP { |
| ptr = ptr.(*ir.ConvExpr).X |
| } |
| if ptr.Op() == ir.OADDR { |
| v.budget += 1 |
| } |
|
|
| case ir.OCONVNOP: |
| |
| v.budget++ |
|
|
| case ir.OFALL, ir.OTYPE: |
| |
| return false |
|
|
| case ir.OIF: |
| n := n.(*ir.IfStmt) |
| if ir.IsConst(n.Cond, constant.Bool) { |
| |
| if doList(n.Init(), v.do) { |
| return true |
| } |
| if ir.BoolVal(n.Cond) { |
| return doList(n.Body, v.do) |
| } else { |
| return doList(n.Else, v.do) |
| } |
| } |
|
|
| case ir.ONAME: |
| n := n.(*ir.Name) |
| if n.Class == ir.PAUTO { |
| v.usedLocals.Add(n) |
| } |
|
|
| case ir.OBLOCK: |
| |
| |
| |
| v.budget++ |
|
|
| case ir.OMETHVALUE, ir.OSLICELIT: |
| v.budget-- |
|
|
| case ir.OMETHEXPR: |
| v.budget++ |
|
|
| case ir.OAS2: |
| n := n.(*ir.AssignListStmt) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if len(n.Rhs) > 0 { |
| if init := n.Rhs[0].Init(); len(init) == 1 { |
| if _, ok := init[0].(*ir.AssignListStmt); ok { |
| |
| |
| |
| |
| v.budget += 4*int32(len(n.Lhs)) + 1 |
| } |
| } |
| } |
|
|
| case ir.OAS: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| n := n.(*ir.AssignStmt) |
| if n.X.Op() == ir.OINDEX && isIndexingCoverageCounter(n.X) { |
| return false |
| } |
|
|
| case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR: |
| n := n.(*ir.SliceExpr) |
|
|
| |
| if n.Low != nil && n.Low.Op() == ir.OLITERAL && ir.Int64Val(n.Low) == 0 { |
| v.budget++ |
| } |
| if n.High != nil && n.High.Op() == ir.OLEN && n.High.(*ir.UnaryExpr).X == n.X { |
| v.budget += 2 |
| } |
| } |
|
|
| v.budget-- |
|
|
| |
| if v.budget < 0 && base.Flag.LowerM < 2 && !logopt.Enabled() && !v.debug { |
| v.reason = "too expensive" |
| return true |
| } |
|
|
| return ir.DoChildren(n, v.do) |
| } |
|
|
| |
| |
| |
| func IsBigFunc(fn *ir.Func) bool { |
| budget := inlineBigFunctionNodes |
| return ir.Any(fn, func(n ir.Node) bool { |
| |
| |
| if n, ok := n.(*ir.AssignListStmt); ok && n.Op() == ir.OAS2 && len(n.Rhs) > 0 { |
| if init := n.Rhs[0].Init(); len(init) == 1 { |
| if _, ok := init[0].(*ir.AssignListStmt); ok { |
| budget += 4*len(n.Lhs) + 1 |
| } |
| } |
| } |
|
|
| budget-- |
| return budget <= 0 |
| }) |
| } |
|
|
| |
| |
| |
| func inlineCallCheck(callerfn *ir.Func, call *ir.CallExpr) (bool, bool) { |
| if base.Flag.LowerL == 0 { |
| return false, false |
| } |
| if call.Op() != ir.OCALLFUNC { |
| return false, false |
| } |
| if call.GoDefer || call.NoInline { |
| return false, false |
| } |
|
|
| |
| |
| if base.Debug.Checkptr != 0 && call.Fun.Op() == ir.OMETHEXPR { |
| if method := ir.MethodExprName(call.Fun); method != nil { |
| switch types.ReflectSymName(method.Sym()) { |
| case "Value.UnsafeAddr", "Value.Pointer": |
| return false, false |
| } |
| } |
| } |
|
|
| |
| |
| if fn := ir.StaticCalleeName(call.Fun); fn != nil && fn.Sym().Pkg.Path == "internal/abi" && |
| strings.HasPrefix(fn.Sym().Name, "EscapeNonString[") { |
| return false, true |
| } |
|
|
| if ir.IsIntrinsicCall(call) { |
| return false, true |
| } |
| return true, false |
| } |
|
|
| |
| |
| |
| func InlineCallTarget(callerfn *ir.Func, call *ir.CallExpr, profile *pgoir.Profile) *ir.Func { |
| if mightInline, _ := inlineCallCheck(callerfn, call); !mightInline { |
| return nil |
| } |
| return inlCallee(callerfn, call.Fun, profile, true) |
| } |
|
|
| |
| |
| func TryInlineCall(callerfn *ir.Func, call *ir.CallExpr, bigCaller bool, profile *pgoir.Profile, closureCalledOnce bool) *ir.InlinedCallExpr { |
| mightInline, isIntrinsic := inlineCallCheck(callerfn, call) |
|
|
| |
| if (mightInline || isIntrinsic) && base.Flag.LowerM > 3 { |
| fmt.Printf("%v:call to func %+v\n", ir.Line(call), call.Fun) |
| } |
| if !mightInline { |
| return nil |
| } |
|
|
| if fn := inlCallee(callerfn, call.Fun, profile, false); fn != nil && typecheck.HaveInlineBody(fn) { |
| return mkinlcall(callerfn, call, fn, bigCaller, closureCalledOnce) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func inlCallee(caller *ir.Func, fn ir.Node, profile *pgoir.Profile, resolveOnly bool) (res *ir.Func) { |
| fn = ir.StaticValue(fn) |
| switch fn.Op() { |
| case ir.OMETHEXPR: |
| fn := fn.(*ir.SelectorExpr) |
| n := ir.MethodExprName(fn) |
| |
| |
| |
| 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 |
| if len(c.ClosureVars) != 0 && c.ClosureVars[0].Outer.Curfn != caller { |
| return nil |
| } |
| if !resolveOnly { |
| CanInline(c, profile) |
| } |
| return c |
| } |
| return nil |
| } |
|
|
| var inlgen int |
|
|
| |
| |
| var SSADumpInline = func(*ir.Func) {} |
|
|
| |
| |
| var InlineCall = func(callerfn *ir.Func, call *ir.CallExpr, fn *ir.Func, inlIndex int) *ir.InlinedCallExpr { |
| base.Fatalf("inline.InlineCall not overridden") |
| panic("unreachable") |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func inlineCostOK(n *ir.CallExpr, caller, callee *ir.Func, bigCaller, closureCalledOnce bool) (bool, int32, int32, bool) { |
| maxCost := int32(inlineMaxBudget) |
|
|
| if bigCaller { |
| |
| |
| maxCost = inlineBigFunctionMaxCost |
| } |
|
|
| simdMaxCost := simdCreditMultiplier(callee) * maxCost |
|
|
| if callee.ClosureParent != nil { |
| maxCost *= 2 |
| if closureCalledOnce { |
| maxCost = max(maxCost, inlineClosureCalledOnceCost) |
| } |
| } |
|
|
| maxCost = max(maxCost, simdMaxCost) |
|
|
| metric := callee.Inl.Cost |
| if inlheur.Enabled() { |
| score, ok := inlheur.GetCallSiteScore(caller, n) |
| if ok { |
| metric = int32(score) |
| } |
| } |
|
|
| lineOffset := pgoir.NodeLineOffset(n, caller) |
| csi := pgoir.CallSiteInfo{LineOffset: lineOffset, Caller: caller} |
| _, hot := candHotEdgeMap[csi] |
|
|
| if metric <= maxCost { |
| |
| return true, 0, metric, hot |
| } |
|
|
| |
| |
|
|
| if !hot { |
| |
| return false, maxCost, metric, false |
| } |
|
|
| |
|
|
| if bigCaller { |
| if base.Debug.PGODebug > 0 { |
| fmt.Printf("hot-big check disallows inlining for call %s (cost %d) at %v in big function %s\n", ir.PkgFuncName(callee), callee.Inl.Cost, ir.Line(n), ir.PkgFuncName(caller)) |
| } |
| return false, maxCost, metric, false |
| } |
|
|
| if metric > inlineHotMaxBudget { |
| return false, inlineHotMaxBudget, metric, false |
| } |
|
|
| if !base.PGOHash.MatchPosWithInfo(n.Pos(), "inline", nil) { |
| |
| return false, maxCost, metric, false |
| } |
|
|
| if base.Debug.PGODebug > 0 { |
| fmt.Printf("hot-budget check allows inlining for call %s (cost %d) at %v in function %s\n", ir.PkgFuncName(callee), callee.Inl.Cost, ir.Line(n), ir.PkgFuncName(caller)) |
| } |
|
|
| return true, 0, metric, hot |
| } |
|
|
| |
| func parsePos(pos src.XPos, posTmp []src.Pos) ([]src.Pos, src.Pos) { |
| ctxt := base.Ctxt |
| ctxt.AllPos(pos, func(p src.Pos) { |
| posTmp = append(posTmp, p) |
| }) |
| l := len(posTmp) - 1 |
| return posTmp[:l], posTmp[l] |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func canInlineCallExpr(callerfn *ir.Func, n *ir.CallExpr, callee *ir.Func, bigCaller, closureCalledOnce bool, log bool) (bool, int32, bool) { |
| if callee.Inl == nil { |
| |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf("%s cannot be inlined", ir.PkgFuncName(callee))) |
| } |
| return false, 0, false |
| } |
|
|
| ok, maxCost, callSiteScore, hot := inlineCostOK(n, callerfn, callee, bigCaller, closureCalledOnce) |
| if !ok { |
| |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf("cost %d of %s exceeds max caller cost %d", callee.Inl.Cost, ir.PkgFuncName(callee), maxCost)) |
| } |
| return false, 0, false |
| } |
|
|
| callees, calleeInner := parsePos(n.Pos(), make([]src.Pos, 0, 10)) |
|
|
| for _, p := range callees { |
| if p.Line() == calleeInner.Line() && p.Col() == calleeInner.Col() && p.AbsFilename() == calleeInner.AbsFilename() { |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", fmt.Sprintf("recursive call to %s", ir.FuncName(callerfn))) |
| } |
| return false, 0, false |
| } |
| } |
|
|
| if base.Flag.Cfg.Instrumenting && types.IsNoInstrumentPkg(callee.Sym().Pkg) { |
| |
| |
| |
| |
| |
| |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf("call to runtime function %s in instrumented build", ir.PkgFuncName(callee))) |
| } |
| return false, 0, false |
| } |
|
|
| if base.Flag.Race && types.IsNoRacePkg(callee.Sym().Pkg) { |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf(`call to into "no-race" package function %s in race build`, ir.PkgFuncName(callee))) |
| } |
| return false, 0, false |
| } |
|
|
| if base.Debug.Checkptr != 0 && types.IsRuntimePkg(callee.Sym().Pkg) { |
| |
| if log && logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf(`call to into runtime package function %s in -d=checkptr build`, ir.PkgFuncName(callee))) |
| } |
| return false, 0, false |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| parent := base.Ctxt.PosTable.Pos(n.Pos()).Base().InliningIndex() |
| sym := callee.Linksym() |
| for inlIndex := parent; inlIndex >= 0; inlIndex = base.Ctxt.InlTree.Parent(inlIndex) { |
| if base.Ctxt.InlTree.InlinedFunction(inlIndex) == sym { |
| if log { |
| if base.Flag.LowerM > 1 { |
| fmt.Printf("%v: cannot inline %v into %v: repeated recursive cycle\n", ir.Line(n), callee, ir.FuncName(callerfn)) |
| } |
| if logopt.Enabled() { |
| logopt.LogOpt(n.Pos(), "cannotInlineCall", "inline", ir.FuncName(callerfn), |
| fmt.Sprintf("repeated recursive cycle to %s", ir.PkgFuncName(callee))) |
| } |
| } |
| return false, 0, false |
| } |
| } |
|
|
| return true, callSiteScore, hot |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func mkinlcall(callerfn *ir.Func, n *ir.CallExpr, fn *ir.Func, bigCaller, closureCalledOnce bool) *ir.InlinedCallExpr { |
| ok, score, hot := canInlineCallExpr(callerfn, n, fn, bigCaller, closureCalledOnce, true) |
| if !ok { |
| return nil |
| } |
| if hot { |
| hasHotCall[callerfn] = struct{}{} |
| } |
| typecheck.AssertFixedCall(n) |
|
|
| parent := base.Ctxt.PosTable.Pos(n.Pos()).Base().InliningIndex() |
| sym := fn.Linksym() |
| inlIndex := base.Ctxt.InlTree.Add(parent, n.Pos(), sym, ir.FuncName(fn)) |
|
|
| closureInitLSym := func(n *ir.CallExpr, fn *ir.Func) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if n.Op() != ir.OCALLFUNC { |
| |
| return |
| } |
|
|
| var nf = n.Fun |
| |
| for nf.Op() == ir.OCONVNOP { |
| nf = nf.(*ir.ConvExpr).X |
| } |
| if nf.Op() != ir.OCLOSURE { |
| |
| return |
| } |
|
|
| clo := nf.(*ir.ClosureExpr) |
| if !clo.Func.IsClosure() { |
| |
| return |
| } |
|
|
| ir.InitLSym(fn, true) |
| } |
|
|
| closureInitLSym(n, fn) |
|
|
| if base.Flag.GenDwarfInl > 0 { |
| if !sym.WasInlined() { |
| base.Ctxt.DwFixups.SetPrecursorFunc(sym, fn) |
| sym.Set(obj.AttrWasInlined, true) |
| } |
| } |
|
|
| if base.Flag.LowerM != 0 { |
| if buildcfg.Experiment.NewInliner { |
| fmt.Printf("%v: inlining call to %v with score %d\n", |
| ir.Line(n), fn, score) |
| } else { |
| fmt.Printf("%v: inlining call to %v\n", ir.Line(n), fn) |
| } |
| } |
| if base.Flag.LowerM > 2 { |
| fmt.Printf("%v: Before inlining: %+v\n", ir.Line(n), n) |
| } |
|
|
| res := InlineCall(callerfn, n, fn, inlIndex) |
|
|
| if res == nil { |
| base.FatalfAt(n.Pos(), "inlining call to %v failed", fn) |
| } |
|
|
| if base.Flag.LowerM > 2 { |
| fmt.Printf("%v: After inlining %+v\n\n", ir.Line(res), res) |
| } |
|
|
| if inlheur.Enabled() { |
| inlheur.UpdateCallsiteTable(callerfn, n, res) |
| } |
|
|
| return res |
| } |
|
|
| |
| func CalleeEffects(init *ir.Nodes, callee ir.Node) { |
| for { |
| init.Append(ir.TakeInit(callee)...) |
|
|
| switch callee.Op() { |
| case ir.ONAME, ir.OCLOSURE, ir.OMETHEXPR: |
| return |
|
|
| case ir.OCONVNOP: |
| conv := callee.(*ir.ConvExpr) |
| callee = conv.X |
|
|
| case ir.OINLCALL: |
| ic := callee.(*ir.InlinedCallExpr) |
| init.Append(ic.Body.Take()...) |
| callee = ic.SingleResult() |
|
|
| default: |
| base.FatalfAt(callee.Pos(), "unexpected callee expression: %v", callee) |
| } |
| } |
| } |
|
|
| func pruneUnusedAutos(ll []*ir.Name, vis *hairyVisitor) []*ir.Name { |
| s := make([]*ir.Name, 0, len(ll)) |
| for _, n := range ll { |
| if n.Class == ir.PAUTO { |
| if !vis.usedLocals.Has(n) { |
| |
| |
| base.FatalfAt(n.Pos(), "unused auto: %v", n) |
| continue |
| } |
| } |
| s = append(s, n) |
| } |
| return s |
| } |
|
|
| func doList(list []ir.Node, do func(ir.Node) bool) bool { |
| for _, x := range list { |
| if x != nil { |
| if do(x) { |
| return true |
| } |
| } |
| } |
| return false |
| } |
|
|
| |
| |
| func isIndexingCoverageCounter(n ir.Node) bool { |
| if n.Op() != ir.OINDEX { |
| return false |
| } |
| ixn := n.(*ir.IndexExpr) |
| if ixn.X.Op() != ir.ONAME || !ixn.X.Type().IsArray() { |
| return false |
| } |
| nn := ixn.X.(*ir.Name) |
| |
| |
| |
| return nn.CoverageAuxVar() |
| } |
|
|
| |
| |
| |
| func isAtomicCoverageCounterUpdate(cn *ir.CallExpr) bool { |
| if cn.Fun.Op() != ir.ONAME { |
| return false |
| } |
| name := cn.Fun.(*ir.Name) |
| if name.Class != ir.PFUNC { |
| return false |
| } |
| fn := name.Sym().Name |
| if name.Sym().Pkg.Path != "sync/atomic" || |
| (fn != "AddUint32" && fn != "StoreUint32") { |
| return false |
| } |
| if len(cn.Args) != 2 || cn.Args[0].Op() != ir.OADDR { |
| return false |
| } |
| adn := cn.Args[0].(*ir.AddrExpr) |
| v := isIndexingCoverageCounter(adn.X) |
| return v |
| } |
|
|
| func PostProcessCallSites(profile *pgoir.Profile) { |
| if base.Debug.DumpInlCallSiteScores != 0 { |
| budgetCallback := func(fn *ir.Func, prof *pgoir.Profile) (int32, bool) { |
| v := inlineBudget(fn, prof, false, false) |
| return v, v == inlineHotMaxBudget |
| } |
| inlheur.DumpInlCallSiteScores(profile, budgetCallback) |
| } |
| } |
|
|
| func analyzeFuncProps(fn *ir.Func, p *pgoir.Profile) { |
| canInline := func(fn *ir.Func) { CanInline(fn, p) } |
| budgetForFunc := func(fn *ir.Func) int32 { |
| return inlineBudget(fn, p, true, false) |
| } |
| inlheur.AnalyzeFunc(fn, canInline, budgetForFunc, inlineMaxBudget) |
| } |
|
|