Skip to content

Commit

Permalink
cmd/compile/internal/ssa: delay rewrite cycle detection for huge funcs
Browse files Browse the repository at this point in the history
The SSA rewrite pass has some logic that looks to see whether a
suspiciously large number of rewrites is happening, and if so, turns
on logic to try to detect rewrite cycles. The cycle detection logic is
quite expensive (hashes the entire function), meaning that for very
large functions we might get a successful compilation in a minute or
two with no cycle detection, but take a couple of hours once cycle
detection kicks in.

This patch moves from a fixed limit of 1000 iterations to a limit set
partially based on the size of the function (meaning that we'll wait
longer before turning cycle detection for a large func).

Fixes #66773.

Change-Id: I72f8524d706f15b3f0150baf6abeab2a5d3e15c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/578215
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
  • Loading branch information
thanm authored and pull[bot] committed Dec 28, 2024
1 parent 6eb4764 commit 3f410e9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cmd/compile/internal/ssa/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter, deadcode deadValu
if debug > 1 {
fmt.Printf("%s: rewriting for %s\n", f.pass.name, f.Name)
}
// if the number of rewrite iterations reaches itersLimit we will
// at that point turn on cycle detection. Instead of a fixed limit,
// size the limit according to func size to allow for cases such
// as the one in issue #66773.
itersLimit := f.NumBlocks()
if itersLimit < 20 {
itersLimit = 20
}
var iters int
var states map[string]bool
for {
Expand Down Expand Up @@ -154,7 +162,7 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter, deadcode deadValu
break
}
iters++
if (iters > 1000 || debug >= 2) && change {
if (iters > itersLimit || debug >= 2) && change {
// We've done a suspiciously large number of rewrites (or we're in debug mode).
// As of Sep 2021, 90% of rewrites complete in 4 iterations or fewer
// and the maximum value encountered during make.bash is 12.
Expand Down

0 comments on commit 3f410e9

Please sign in to comment.