| 1 |
// Copyright 2013 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Incomplete source tree on Android. |
| 6 |
|
| 7 |
//go:build !android |
| 8 |
|
| 9 |
package ir_test |
| 10 |
|
| 11 |
// This file runs the SSA builder in sanity-checking mode on all |
| 12 |
// packages beneath $GOROOT and prints some summary information. |
| 13 |
// |
| 14 |
// Run with "go test -cpu=8 to" set GOMAXPROCS. |
| 15 |
|
| 16 |
import ( |
| 17 |
"go/ast" |
| 18 |
"go/token" |
| 19 |
"go/types" |
| 20 |
"runtime" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"honnef.co/go/tools/go/ir" |
| 25 |
"honnef.co/go/tools/go/ir/irutil" |
| 26 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 27 |
|
| 28 |
"golang.org/x/tools/go/packages" |
| 29 |
) |
| 30 |
|
| 31 |
func bytesAllocated() uint64 { |
| 32 |
runtime.GC() |
| 33 |
var stats runtime.MemStats |
| 34 |
runtime.ReadMemStats(&stats) |
| 35 |
return stats.Alloc |
| 36 |
} |
| 37 |
|
| 38 |
// TestStdlib loads the entire standard library and its tools and all |
| 39 |
// their dependencies. |
| 40 |
// |
| 41 |
// (As of go1.23, std is transitively closed, so adding the -deps flag |
| 42 |
// doesn't increase its result set. The cmd pseudomodule of course |
| 43 |
// depends on a good chunk of std, but the std+cmd set is also |
| 44 |
// transitively closed, so long as -pgo=off.) |
| 45 |
// |
| 46 |
// Apart from a small number of internal packages that are not |
| 47 |
// returned by the 'std' query, the set is essentially transitively |
| 48 |
// closed, so marginal per-dependency costs are invisible. |
| 49 |
func TestStdlib(t *testing.T) { |
| 50 |
testLoad(t, 500, "std", "cmd") |
| 51 |
} |
| 52 |
|
| 53 |
// TestNetHTTP builds a single SSA package but not its dependencies. |
| 54 |
// It may help reveal costs related to dependencies (e.g. unnecessary building). |
| 55 |
func TestNetHTTP(t *testing.T) { |
| 56 |
testLoad(t, 120, "net/http") |
| 57 |
} |
| 58 |
|
| 59 |
// TestCycles loads two standard libraries that depend on the same |
| 60 |
// generic instantiations. |
| 61 |
// internal/trace/testtrace and net/http both depend on |
| 62 |
// slices.Contains[[]string string] and slices.Index[[]string string] |
| 63 |
// This can under some schedules create a cycle of dependencies |
| 64 |
// where both need to wait on the other to finish building. |
| 65 |
func TestCycles(t *testing.T) { |
| 66 |
testenv.NeedsGo1Point(t, 23) // internal/trace/testtrace was added in 1.23. |
| 67 |
testLoad(t, 120, "net/http", "internal/trace/testtrace") |
| 68 |
} |
| 69 |
|
| 70 |
func testLoad(t *testing.T, minPkgs int, patterns ...string) { |
| 71 |
// Note: most of the commentary below applies to TestStdlib. |
| 72 |
|
| 73 |
if testing.Short() { |
| 74 |
t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)") // ~5s |
| 75 |
} |
| 76 |
testenv.NeedsTool(t, "go") |
| 77 |
|
| 78 |
// Load, parse and type-check the program. |
| 79 |
t0 := time.Now() |
| 80 |
alloc0 := bytesAllocated() |
| 81 |
|
| 82 |
cfg := &packages.Config{Mode: packages.LoadSyntax} |
| 83 |
pkgs, err := packages.Load(cfg, patterns...) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
if packages.PrintErrors(pkgs) > 0 { |
| 88 |
t.Fatal("there were errors loading the packages") |
| 89 |
} |
| 90 |
|
| 91 |
t1 := time.Now() |
| 92 |
alloc1 := bytesAllocated() |
| 93 |
|
| 94 |
// Create SSA packages. |
| 95 |
var mode ir.BuilderMode |
| 96 |
// Comment out these lines during benchmarking. Approx SSA build costs are noted. |
| 97 |
mode |= ir.SanityCheckFunctions // + 2% space, + 4% time |
| 98 |
mode |= ir.GlobalDebug // +30% space, +18% time |
| 99 |
mode |= ir.InstantiateGenerics // + 0% space, + 2% time (unlikely to reproduce outside of stdlib) |
| 100 |
prog, _ := irutil.Packages(pkgs, mode) |
| 101 |
|
| 102 |
t2 := time.Now() |
| 103 |
|
| 104 |
// Build SSA. |
| 105 |
prog.Build() |
| 106 |
|
| 107 |
t3 := time.Now() |
| 108 |
alloc3 := bytesAllocated() |
| 109 |
|
| 110 |
// Sanity check to ensure we haven't dropped large numbers of packages. |
| 111 |
numPkgs := len(prog.AllPackages()) |
| 112 |
if numPkgs < minPkgs { |
| 113 |
t.Errorf("Loaded only %d packages, want at least %d", numPkgs, minPkgs) |
| 114 |
} |
| 115 |
|
| 116 |
// Keep pkgs reachable until after we've measured memory usage. |
| 117 |
if len(pkgs) == 0 { |
| 118 |
panic("unreachable") |
| 119 |
} |
| 120 |
|
| 121 |
srcFuncs := srcFunctions(prog, pkgs) |
| 122 |
allFuncs := irutil.AllFunctions(prog) |
| 123 |
|
| 124 |
// The assertion below is not valid if the program contains |
| 125 |
// variants of the same package, such as the test variants |
| 126 |
// (e.g. package p as compiled for test executable x) obtained |
| 127 |
// when cfg.Tests=true. Profile-guided optimization may |
| 128 |
// lead to similar variation for non-test executables. |
| 129 |
// |
| 130 |
// Ideally, the test would assert that all functions within |
| 131 |
// each executable (more generally: within any singly rooted |
| 132 |
// transitively closed subgraph of the import graph) have |
| 133 |
// distinct names, but that isn't so easy to compute efficiently. |
| 134 |
// Disabling for now. |
| 135 |
if false { |
| 136 |
// Check that all non-synthetic functions have distinct names. |
| 137 |
// Synthetic wrappers for exported methods should be distinct too, |
| 138 |
// except for unexported ones (explained at (*Function).RelString). |
| 139 |
byName := make(map[string]*ir.Function) |
| 140 |
for fn := range allFuncs { |
| 141 |
if fn.Synthetic == "" || ast.IsExported(fn.Name()) { |
| 142 |
str := fn.String() |
| 143 |
prev := byName[str] |
| 144 |
byName[str] = fn |
| 145 |
if prev != nil { |
| 146 |
t.Errorf("%s: duplicate function named %s", |
| 147 |
prog.Fset.Position(fn.Pos()), str) |
| 148 |
t.Errorf("%s: (previously defined here)", |
| 149 |
prog.Fset.Position(prev.Pos())) |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Dump some statistics. |
| 156 |
var numInstrs int |
| 157 |
for fn := range allFuncs { |
| 158 |
for _, b := range fn.Blocks { |
| 159 |
numInstrs += len(b.Instrs) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// determine line count |
| 164 |
var lineCount int |
| 165 |
prog.Fset.Iterate(func(f *token.File) bool { |
| 166 |
lineCount += f.LineCount() |
| 167 |
return true |
| 168 |
}) |
| 169 |
|
| 170 |
// NB: when benchmarking, don't forget to clear the debug + |
| 171 |
// sanity builder flags for better performance. |
| 172 |
|
| 173 |
t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0)) |
| 174 |
t.Log("#Source lines: ", lineCount) |
| 175 |
t.Log("Load/parse/typecheck: ", t1.Sub(t0)) |
| 176 |
t.Log("SSA create: ", t2.Sub(t1)) |
| 177 |
t.Log("SSA build: ", t3.Sub(t2)) |
| 178 |
|
| 179 |
// SSA stats: |
| 180 |
t.Log("#Packages: ", numPkgs) |
| 181 |
t.Log("#SrcFunctions: ", len(srcFuncs)) |
| 182 |
t.Log("#AllFunctions: ", len(allFuncs)) |
| 183 |
t.Log("#Instructions: ", numInstrs) |
| 184 |
t.Log("#MB AST+types: ", int64(alloc1-alloc0)/1e6) |
| 185 |
t.Log("#MB SSA: ", int64(alloc3-alloc1)/1e6) |
| 186 |
} |
| 187 |
|
| 188 |
// srcFunctions gathers all ir.Functions corresponding to syntax. |
| 189 |
// (Includes generics but excludes instances and all wrappers.) |
| 190 |
// |
| 191 |
// This is essentially identical to the SrcFunctions logic in |
| 192 |
// go/analysis/passes/buildssa. |
| 193 |
func srcFunctions(prog *ir.Program, pkgs []*packages.Package) (res []*ir.Function) { |
| 194 |
var addSrcFunc func(fn *ir.Function) |
| 195 |
addSrcFunc = func(fn *ir.Function) { |
| 196 |
res = append(res, fn) |
| 197 |
for _, anon := range fn.AnonFuncs { |
| 198 |
addSrcFunc(anon) |
| 199 |
} |
| 200 |
} |
| 201 |
for _, pkg := range pkgs { |
| 202 |
for _, file := range pkg.Syntax { |
| 203 |
for _, decl := range file.Decls { |
| 204 |
if decl, ok := decl.(*ast.FuncDecl); ok { |
| 205 |
obj := pkg.TypesInfo.Defs[decl.Name].(*types.Func) |
| 206 |
if obj == nil { |
| 207 |
panic("nil *types.Func: " + decl.Name.Name) |
| 208 |
} |
| 209 |
fn := prog.FuncValue(obj) |
| 210 |
if fn == nil { |
| 211 |
panic("nil *ir.Function: " + obj.String()) |
| 212 |
} |
| 213 |
addSrcFunc(fn) |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
return res |
| 219 |
} |