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