-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: bounds check not eliminated #45078
Comments
Also CC @zdjones |
Does this work for you? func f(x, y []int, i, j int) int {
i0 := i
for i >= 0 && i < len(x) && j >= 0 && j < len(y) && x[i] == y[j] {
i++
j++
}
return i - i0
} Seems to be equivalent, has no bounds checks, generates considerably shorter code. The key piece is the addition of |
Another example (on ARM): in this function the order of i and j in the loop body statement affects the number of bounds checks. func reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
} The natural order,
The reversed order,
|
Because |
I am writing code to count how long a common prefix two slices have, and I can't seem to make it bounds-check-free.
At current master, this function does not eliminate the bounds check for y[j], although it does for x[i]:
If I remove the
i < len(x) &&
and thei++
, then the y[j] bounds check is eliminated:If I leave out the
i++
but bring back the (unnecessary)i < len(x)
, the y[j] bounds check returns:This is a simpler case that also has a bounds check and may share the same root cause:
This equivalent program has no check:
This suggests the problem has to do with either the inverted condition or the use of a short-circuit to exit the loop, neither of which should fundamentally change what is being proved.
The same happens counting backward: h0 and h1 have checks (h0 has just one), but h2 is check-free:
/cc @aclements @randall77 @rasky @brtzsnr (prove authors as best I can tell from git blame)
The text was updated successfully, but these errors were encountered: