| |
| |
| |
|
|
| package inlheur |
|
|
| import ( |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/types" |
| "cmp" |
| "encoding/json" |
| "fmt" |
| "internal/buildcfg" |
| "io" |
| "os" |
| "path/filepath" |
| "slices" |
| "strings" |
| ) |
|
|
| const ( |
| debugTraceFuncs = 1 << iota |
| debugTraceFuncFlags |
| debugTraceResults |
| debugTraceParams |
| debugTraceExprClassify |
| debugTraceCalls |
| debugTraceScoring |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type propAnalyzer interface { |
| nodeVisitPre(n ir.Node) |
| nodeVisitPost(n ir.Node) |
| setResults(funcProps *FuncProps) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| type fnInlHeur struct { |
| props *FuncProps |
| cstab CallSiteTab |
| fname string |
| file string |
| line uint |
| } |
|
|
| var fpmap = map[*ir.Func]fnInlHeur{} |
|
|
| |
| |
| |
| |
| |
| |
| func AnalyzeFunc(fn *ir.Func, canInline func(*ir.Func), budgetForFunc func(*ir.Func) int32, inlineMaxBudget int) { |
| if fpmap == nil { |
| |
| |
| |
| return |
| } |
| if fn.OClosure != nil { |
| |
| return |
| } |
| enableDebugTraceIfEnv() |
| if debugTrace&debugTraceFuncs != 0 { |
| fmt.Fprintf(os.Stderr, "=-= AnalyzeFunc(%v)\n", fn) |
| } |
| |
| |
| |
| funcs := []*ir.Func{fn} |
| ir.VisitFuncAndClosures(fn, func(n ir.Node) { |
| if clo, ok := n.(*ir.ClosureExpr); ok { |
| funcs = append(funcs, clo.Func) |
| } |
| }) |
|
|
| |
| |
| |
| |
| |
| |
| |
| nameFinder := newNameFinder(fn) |
| for i := len(funcs) - 1; i >= 0; i-- { |
| f := funcs[i] |
| if f.OClosure != nil && !f.InlinabilityChecked() { |
| canInline(f) |
| } |
| funcProps := analyzeFunc(f, inlineMaxBudget, nameFinder) |
| revisitInlinability(f, funcProps, budgetForFunc) |
| if f.Inl != nil { |
| f.Inl.Properties = funcProps.SerializeToString() |
| } |
| } |
| disableDebugTrace() |
| } |
|
|
| |
| |
| |
| |
| func TearDown() { |
| fpmap = nil |
| scoreCallsCache.tab = nil |
| scoreCallsCache.csl = nil |
| } |
|
|
| func analyzeFunc(fn *ir.Func, inlineMaxBudget int, nf *nameFinder) *FuncProps { |
| if funcInlHeur, ok := fpmap[fn]; ok { |
| return funcInlHeur.props |
| } |
| funcProps, fcstab := computeFuncProps(fn, inlineMaxBudget, nf) |
| file, line := fnFileLine(fn) |
| entry := fnInlHeur{ |
| fname: fn.Sym().Name, |
| file: file, |
| line: line, |
| props: funcProps, |
| cstab: fcstab, |
| } |
| fn.SetNeverReturns(entry.props.Flags&FuncPropNeverReturns != 0) |
| fpmap[fn] = entry |
| if fn.Inl != nil && fn.Inl.Properties == "" { |
| fn.Inl.Properties = entry.props.SerializeToString() |
| } |
| return funcProps |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func revisitInlinability(fn *ir.Func, funcProps *FuncProps, budgetForFunc func(*ir.Func) int32) { |
| if fn.Inl == nil { |
| return |
| } |
| maxAdj := int32(LargestNegativeScoreAdjustment(fn, funcProps)) |
| budget := budgetForFunc(fn) |
| if fn.Inl.Cost+maxAdj > budget { |
| fn.Inl = nil |
| } |
| } |
|
|
| |
| |
| |
| func computeFuncProps(fn *ir.Func, inlineMaxBudget int, nf *nameFinder) (*FuncProps, CallSiteTab) { |
| if debugTrace&debugTraceFuncs != 0 { |
| fmt.Fprintf(os.Stderr, "=-= starting analysis of func %v:\n%+v\n", |
| fn, fn) |
| } |
| funcProps := new(FuncProps) |
| ffa := makeFuncFlagsAnalyzer(fn) |
| analyzers := []propAnalyzer{ffa} |
| analyzers = addResultsAnalyzer(fn, analyzers, funcProps, inlineMaxBudget, nf) |
| analyzers = addParamsAnalyzer(fn, analyzers, funcProps, nf) |
| runAnalyzersOnFunction(fn, analyzers) |
| for _, a := range analyzers { |
| a.setResults(funcProps) |
| } |
| cstab := computeCallSiteTable(fn, fn.Body, nil, ffa.panicPathTable(), 0, nf) |
| return funcProps, cstab |
| } |
|
|
| func runAnalyzersOnFunction(fn *ir.Func, analyzers []propAnalyzer) { |
| var doNode func(ir.Node) bool |
| doNode = func(n ir.Node) bool { |
| for _, a := range analyzers { |
| a.nodeVisitPre(n) |
| } |
| ir.DoChildren(n, doNode) |
| for _, a := range analyzers { |
| a.nodeVisitPost(n) |
| } |
| return false |
| } |
| doNode(fn) |
| } |
|
|
| func propsForFunc(fn *ir.Func) *FuncProps { |
| if funcInlHeur, ok := fpmap[fn]; ok { |
| return funcInlHeur.props |
| } else if fn.Inl != nil && fn.Inl.Properties != "" { |
| |
| |
| return DeserializeFromString(fn.Inl.Properties) |
| } |
| return nil |
| } |
|
|
| func fnFileLine(fn *ir.Func) (string, uint) { |
| p := base.Ctxt.InnermostPos(fn.Pos()) |
| return filepath.Base(p.Filename()), p.Line() |
| } |
|
|
| func Enabled() bool { |
| return buildcfg.Experiment.NewInliner || UnitTesting() |
| } |
|
|
| func UnitTesting() bool { |
| return base.Debug.DumpInlFuncProps != "" || |
| base.Debug.DumpInlCallSiteScores != 0 |
| } |
|
|
| |
| |
| |
| |
| |
| func DumpFuncProps(fn *ir.Func, dumpfile string) { |
| if fn != nil { |
| if fn.OClosure != nil { |
| |
| return |
| } |
| captureFuncDumpEntry(fn) |
| ir.VisitFuncAndClosures(fn, func(n ir.Node) { |
| if clo, ok := n.(*ir.ClosureExpr); ok { |
| captureFuncDumpEntry(clo.Func) |
| } |
| }) |
| } else { |
| emitDumpToFile(dumpfile) |
| } |
| } |
|
|
| |
| |
| |
| |
| func emitDumpToFile(dumpfile string) { |
| mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC |
| if dumpfile[0] == '+' { |
| dumpfile = dumpfile[1:] |
| mode = os.O_WRONLY | os.O_APPEND | os.O_CREATE |
| } |
| if dumpfile[0] == '%' { |
| dumpfile = dumpfile[1:] |
| d, b := filepath.Dir(dumpfile), filepath.Base(dumpfile) |
| ptag := strings.ReplaceAll(types.LocalPkg.Path, "/", ":") |
| dumpfile = d + "/" + ptag + "." + b |
| } |
| outf, err := os.OpenFile(dumpfile, mode, 0644) |
| if err != nil { |
| base.Fatalf("opening function props dump file %q: %v\n", dumpfile, err) |
| } |
| defer outf.Close() |
| dumpFilePreamble(outf) |
|
|
| atline := map[uint]uint{} |
| sl := make([]fnInlHeur, 0, len(dumpBuffer)) |
| for _, e := range dumpBuffer { |
| sl = append(sl, e) |
| atline[e.line] = atline[e.line] + 1 |
| } |
| sl = sortFnInlHeurSlice(sl) |
|
|
| prevline := uint(0) |
| for _, entry := range sl { |
| idx := uint(0) |
| if prevline == entry.line { |
| idx++ |
| } |
| prevline = entry.line |
| atl := atline[entry.line] |
| if err := dumpFnPreamble(outf, &entry, nil, idx, atl); err != nil { |
| base.Fatalf("function props dump: %v\n", err) |
| } |
| } |
| dumpBuffer = nil |
| } |
|
|
| |
| |
| |
| |
| func captureFuncDumpEntry(fn *ir.Func) { |
| |
| if strings.HasPrefix(fn.Sym().Name, ".eq.") { |
| return |
| } |
| funcInlHeur, ok := fpmap[fn] |
| if !ok { |
| |
| |
| |
| funcInlHeur = fnInlHeur{cstab: callSiteTab} |
| } |
| if dumpBuffer == nil { |
| dumpBuffer = make(map[*ir.Func]fnInlHeur) |
| } |
| if _, ok := dumpBuffer[fn]; ok { |
| return |
| } |
| if debugTrace&debugTraceFuncs != 0 { |
| fmt.Fprintf(os.Stderr, "=-= capturing dump for %v:\n", fn) |
| } |
| dumpBuffer[fn] = funcInlHeur |
| } |
|
|
| |
| |
| func dumpFilePreamble(w io.Writer) { |
| fmt.Fprintf(w, "// DO NOT EDIT (use 'go test -v -update-expected' instead.)\n") |
| fmt.Fprintf(w, "// See cmd/compile/internal/inline/inlheur/testdata/props/README.txt\n") |
| fmt.Fprintf(w, "// for more information on the format of this file.\n") |
| fmt.Fprintf(w, "// %s\n", preambleDelimiter) |
| } |
|
|
| |
| |
| |
| |
| func dumpFnPreamble(w io.Writer, funcInlHeur *fnInlHeur, ecst encodedCallSiteTab, idx, atl uint) error { |
| fmt.Fprintf(w, "// %s %s %d %d %d\n", |
| funcInlHeur.file, funcInlHeur.fname, funcInlHeur.line, idx, atl) |
| |
| fmt.Fprintf(w, "%s// %s\n", funcInlHeur.props.ToString("// "), comDelimiter) |
| data, err := json.Marshal(funcInlHeur.props) |
| if err != nil { |
| return fmt.Errorf("marshal error %v\n", err) |
| } |
| fmt.Fprintf(w, "// %s\n", string(data)) |
| dumpCallSiteComments(w, funcInlHeur.cstab, ecst) |
| fmt.Fprintf(w, "// %s\n", fnDelimiter) |
| return nil |
| } |
|
|
| |
| |
| func sortFnInlHeurSlice(sl []fnInlHeur) []fnInlHeur { |
| slices.SortStableFunc(sl, func(a, b fnInlHeur) int { |
| if a.line != b.line { |
| return cmp.Compare(a.line, b.line) |
| } |
| return strings.Compare(a.fname, b.fname) |
| }) |
| return sl |
| } |
|
|
| |
| |
| const preambleDelimiter = "<endfilepreamble>" |
| const fnDelimiter = "<endfuncpreamble>" |
| const comDelimiter = "<endpropsdump>" |
| const csDelimiter = "<endcallsites>" |
|
|
| |
| |
| var dumpBuffer map[*ir.Func]fnInlHeur |
|
|