Skip to content

Commit 8700807

Browse files
committed
cmd/compile: reduce rulegen's output by 200 KiB
First, renove unnecessary "// cond:" lines from the generated files. This shaves off about ~7k lines. Second, join "if cond { break }" statements via "||", which allows us to deduplicate a large number of them. This shaves off another ~25k lines. This change is not for readability or simplicity; but rather, to avoid unnecessary verbosity that makes the generated files larger. All in all, git reports that the generated files overall weigh ~200KiB less, or about 2.7% less. While at it, add a -trace flag to rulegen. Updates #33644. Change-Id: I3fac0290a6066070cc62400bf970a4ae0929470a Reviewed-on: https://go-review.googlesource.com/c/go/+/196498 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 34fe829 commit 8700807

16 files changed

+6324
-37697
lines changed

src/cmd/compile/internal/ssa/gen/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"regexp"
2121
"runtime"
2222
"runtime/pprof"
23+
"runtime/trace"
2324
"sort"
2425
"strings"
2526
"sync"
@@ -101,6 +102,7 @@ var archs []arch
101102

102103
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
103104
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
105+
var tracefile = flag.String("trace", "", "write trace to `file`")
104106

105107
func main() {
106108
flag.Parse()
@@ -115,6 +117,23 @@ func main() {
115117
}
116118
defer pprof.StopCPUProfile()
117119
}
120+
if *tracefile != "" {
121+
f, err := os.Create(*tracefile)
122+
if err != nil {
123+
log.Fatalf("failed to create trace output file: %v", err)
124+
}
125+
defer func() {
126+
if err := f.Close(); err != nil {
127+
log.Fatalf("failed to close trace file: %v", err)
128+
}
129+
}()
130+
131+
if err := trace.Start(f); err != nil {
132+
log.Fatalf("failed to start trace: %v", err)
133+
}
134+
defer trace.Stop()
135+
}
136+
118137
sort.Sort(ArchsByName(archs))
119138

120139
// The generate tasks are run concurrently, since they are CPU-intensive

src/cmd/compile/internal/ssa/gen/rulegen.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,9 @@ func fprint(w io.Writer, n Node) {
587587
}
588588
case *RuleRewrite:
589589
fmt.Fprintf(w, "// match: %s\n", n.match)
590-
fmt.Fprintf(w, "// cond: %s\n", n.cond)
590+
if n.cond != "" {
591+
fmt.Fprintf(w, "// cond: %s\n", n.cond)
592+
}
591593
fmt.Fprintf(w, "// result: %s\n", n.result)
592594
if n.checkOp != "" {
593595
fmt.Fprintf(w, "for v.Op == %s {\n", n.checkOp)
@@ -636,13 +638,26 @@ type bodyBase struct {
636638
canFail bool
637639
}
638640

639-
func (w *bodyBase) add(nodes ...Statement) {
640-
w.list = append(w.list, nodes...)
641-
for _, node := range nodes {
642-
if _, ok := node.(*CondBreak); ok {
643-
w.canFail = true
641+
func (w *bodyBase) add(node Statement) {
642+
var last Statement
643+
if len(w.list) > 0 {
644+
last = w.list[len(w.list)-1]
645+
}
646+
if node, ok := node.(*CondBreak); ok {
647+
w.canFail = true
648+
if last, ok := last.(*CondBreak); ok {
649+
// Add to the previous "if <cond> { break }" via a
650+
// logical OR, which will save verbosity.
651+
last.expr = &ast.BinaryExpr{
652+
Op: token.LOR,
653+
X: last.expr,
654+
Y: node.expr,
655+
}
656+
return
644657
}
645658
}
659+
660+
w.list = append(w.list, node)
646661
}
647662

648663
// declared reports if the body contains a Declare with the given name.

0 commit comments

Comments
 (0)