Open tdakkota opened 3 months ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
It seems like CL 622240 mostly solved this issue:
$ go version
go version devel go1.24-4b30a40d88 Tue Oct 29 16:47:05 2024 +0000 linux/amd64
$ go tool compile -d=ssa/check_bce/debug=1 test.go
test.go:5:21: Found IsInBounds
func iter(keys, values []string) {
for i := 0; i < min(len(keys), len(values)); i++ {
println(keys[i]) // Found IsInBounds
println(values[i])
}
}
func iter2(keys, values []string) {
l := min(len(keys), len(values))
for i := range l {
println(keys[i])
println(values[i])
}
}
No bound checks in the following code
func iter(keys, values []string) {
l := min(len(keys), len(values))
for i := 0; i < l; i++ {
println(keys[i])
println(values[i])
}
}
func iter2(keys, values []string) {
for i := range min(len(keys), len(values)) {
println(keys[i])
println(values[i])
}
}
Go version
go version go1.22.5 linux/amd64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/wE9DX4D3Iga https://go.godbolt.org/z/11PYfc6x3
What did you see happen?
Bound checks are not eliminated.
What did you expect to see?
Given that
min(len(x), len(y))
is non-negative and less or equal thanlen(x)
andlen(y)
,i
is always in bounds of both slices, so bounds checks could be eliminated.Iteration patterns like this are quite common, especially in columnar data formats.