github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/inline/inlheur/dumpscores_test.go (about) 1 // Copyright 2023 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 inlheur 6 7 import ( 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/go-asm/go/testenv" 14 ) 15 16 func TestDumpCallSiteScoreDump(t *testing.T) { 17 td := t.TempDir() 18 testenv.MustHaveGoBuild(t) 19 20 scenarios := []struct { 21 name string 22 promoted int 23 indirectlyPromoted int 24 demoted int 25 unchanged int 26 }{ 27 { 28 name: "dumpscores", 29 promoted: 1, 30 indirectlyPromoted: 1, 31 demoted: 1, 32 unchanged: 5, 33 }, 34 } 35 36 for _, scen := range scenarios { 37 dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td) 38 if err != nil { 39 t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err) 40 } 41 var lines []string 42 if content, err := os.ReadFile(dumpfile); err != nil { 43 t.Fatalf("reading dump %q: error %v", dumpfile, err) 44 } else { 45 lines = strings.Split(string(content), "\n") 46 } 47 prom, indprom, dem, unch := 0, 0, 0, 0 48 for _, line := range lines { 49 switch { 50 case strings.TrimSpace(line) == "": 51 case !strings.Contains(line, "|"): 52 case strings.HasPrefix(line, "#"): 53 case strings.Contains(line, "PROMOTED"): 54 prom++ 55 case strings.Contains(line, "INDPROM"): 56 indprom++ 57 case strings.Contains(line, "DEMOTED"): 58 dem++ 59 default: 60 unch++ 61 } 62 } 63 showout := false 64 if prom != scen.promoted { 65 t.Errorf("testcase %q, got %d promoted want %d promoted", 66 scen.name, prom, scen.promoted) 67 showout = true 68 } 69 if indprom != scen.indirectlyPromoted { 70 t.Errorf("testcase %q, got %d indirectly promoted want %d", 71 scen.name, indprom, scen.indirectlyPromoted) 72 showout = true 73 } 74 if dem != scen.demoted { 75 t.Errorf("testcase %q, got %d demoted want %d demoted", 76 scen.name, dem, scen.demoted) 77 showout = true 78 } 79 if unch != scen.unchanged { 80 t.Errorf("testcase %q, got %d unchanged want %d unchanged", 81 scen.name, unch, scen.unchanged) 82 showout = true 83 } 84 if showout { 85 t.Logf(">> dump output: %s", strings.Join(lines, "\n")) 86 } 87 } 88 } 89 90 // gatherInlCallSitesScoresForFile builds the specified testcase 'testcase' 91 // from testdata/props passing the "-d=dumpinlcallsitescores=1" 92 // compiler option, to produce a dump, then returns the path of the 93 // newly created file. 94 func gatherInlCallSitesScoresForFile(t *testing.T, testcase string, td string) (string, error) { 95 t.Helper() 96 gopath := "testdata/" + testcase + ".go" 97 outpath := filepath.Join(td, testcase+".a") 98 dumpfile := filepath.Join(td, testcase+".callsites.txt") 99 run := []string{testenv.GoToolPath(t), "build", 100 "-gcflags=-d=dumpinlcallsitescores=1", "-o", outpath, gopath} 101 out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput() 102 t.Logf("run: %+v\n", run) 103 if err != nil { 104 return "", err 105 } 106 if err := os.WriteFile(dumpfile, out, 0666); err != nil { 107 return "", err 108 } 109 return dumpfile, err 110 }