Skip to content

Commit 8b4e125

Browse files
Jorroporandall77
authored andcommitted
cmd/compile: fix findIndVar so it does not match disjointed loop headers
Fix #63955 parseIndVar, prove and maybe more are on the assumption that the loop header is a single block. This can be wrong, ensure we don't match theses cases we don't know how to handle. In the future we could update them so that they know how to handle such cases but theses cases seems rare so I don't think the value would be really high. We could also run a loop canonicalization pass first which could handle this. The repro case looks weird because I massaged it so it would crash with the previous compiler. Change-Id: I4aa8afae9e90a17fa1085832250fc1139c97faa6 Reviewed-on: https://go-review.googlesource.com/c/go/+/539977 Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 0ccbf63 commit 8b4e125

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/cmd/compile/internal/ssa/loopbce.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ func findIndVar(f *Func) []indVar {
129129
less = false
130130
}
131131

132+
if ind.Block != b {
133+
// TODO: Could be extended to include disjointed loop headers.
134+
// I don't think this is causing missed optimizations in real world code often.
135+
// See https://go.dev/issue/63955
136+
continue
137+
}
138+
132139
// Expect the increment to be a nonzero constant.
133140
if !inc.isGenericIntConst() {
134141
continue

test/fixedbugs/issue63955.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package j
8+
9+
func f(try func() int, shouldInc func() bool, N func(int) int) {
10+
var n int
11+
loop: // we want to have 3 preds here, the function entry and both gotos
12+
if v := try(); v == 42 || v == 1337 { // the two || are to trick findIndVar
13+
if n < 30 { // this aims to be the matched block
14+
if shouldInc() {
15+
n++
16+
goto loop
17+
}
18+
n = N(n) // try to prevent some block joining
19+
goto loop
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)