github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/stdlib_test.go (about) 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 package ssa_test 6 7 // This file runs the SSA builder in sanity-checking mode on all 8 // packages beneath $GOROOT and prints some summary information. 9 // 10 // Run with "go test -cpu=8 to" set GOMAXPROCS. 11 12 import ( 13 "go/ast" 14 "go/build" 15 "go/token" 16 "runtime" 17 "testing" 18 "time" 19 20 "llvm.org/llgo/third_party/gotools/go/buildutil" 21 "llvm.org/llgo/third_party/gotools/go/loader" 22 "llvm.org/llgo/third_party/gotools/go/ssa" 23 "llvm.org/llgo/third_party/gotools/go/ssa/ssautil" 24 ) 25 26 func bytesAllocated() uint64 { 27 runtime.GC() 28 var stats runtime.MemStats 29 runtime.ReadMemStats(&stats) 30 return stats.Alloc 31 } 32 33 func TestStdlib(t *testing.T) { 34 // Load, parse and type-check the program. 35 t0 := time.Now() 36 alloc0 := bytesAllocated() 37 38 // Load, parse and type-check the program. 39 ctxt := build.Default // copy 40 ctxt.GOPATH = "" // disable GOPATH 41 conf := loader.Config{Build: &ctxt} 42 if _, err := conf.FromArgs(buildutil.AllPackages(conf.Build), true); err != nil { 43 t.Errorf("FromArgs failed: %v", err) 44 return 45 } 46 47 iprog, err := conf.Load() 48 if err != nil { 49 t.Fatalf("Load failed: %v", err) 50 } 51 52 t1 := time.Now() 53 alloc1 := bytesAllocated() 54 55 // Create SSA packages. 56 var mode ssa.BuilderMode 57 // Comment out these lines during benchmarking. Approx SSA build costs are noted. 58 mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time 59 mode |= ssa.GlobalDebug // +30% space, +18% time 60 prog := ssa.Create(iprog, mode) 61 62 t2 := time.Now() 63 64 // Build SSA. 65 prog.BuildAll() 66 67 t3 := time.Now() 68 alloc3 := bytesAllocated() 69 70 numPkgs := len(prog.AllPackages()) 71 if want := 140; numPkgs < want { 72 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want) 73 } 74 75 // Keep iprog reachable until after we've measured memory usage. 76 if len(iprog.AllPackages) == 0 { 77 print() // unreachable 78 } 79 80 allFuncs := ssautil.AllFunctions(prog) 81 82 // Check that all non-synthetic functions have distinct names. 83 // Synthetic wrappers for exported methods should be distinct too, 84 // except for unexported ones (explained at (*Function).RelString). 85 byName := make(map[string]*ssa.Function) 86 for fn := range allFuncs { 87 if fn.Synthetic == "" || ast.IsExported(fn.Name()) { 88 str := fn.String() 89 prev := byName[str] 90 byName[str] = fn 91 if prev != nil { 92 t.Errorf("%s: duplicate function named %s", 93 prog.Fset.Position(fn.Pos()), str) 94 t.Errorf("%s: (previously defined here)", 95 prog.Fset.Position(prev.Pos())) 96 } 97 } 98 } 99 100 // Dump some statistics. 101 var numInstrs int 102 for fn := range allFuncs { 103 for _, b := range fn.Blocks { 104 numInstrs += len(b.Instrs) 105 } 106 } 107 108 // determine line count 109 var lineCount int 110 prog.Fset.Iterate(func(f *token.File) bool { 111 lineCount += f.LineCount() 112 return true 113 }) 114 115 // NB: when benchmarking, don't forget to clear the debug + 116 // sanity builder flags for better performance. 117 118 t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0)) 119 t.Log("#Source lines: ", lineCount) 120 t.Log("Load/parse/typecheck: ", t1.Sub(t0)) 121 t.Log("SSA create: ", t2.Sub(t1)) 122 t.Log("SSA build: ", t3.Sub(t2)) 123 124 // SSA stats: 125 t.Log("#Packages: ", numPkgs) 126 t.Log("#Functions: ", len(allFuncs)) 127 t.Log("#Instructions: ", numInstrs) 128 t.Log("#MB AST+types: ", int64(alloc1-alloc0)/1e6) 129 t.Log("#MB SSA: ", int64(alloc3-alloc1)/1e6) 130 }