Go version
go version go1.25.0 linux/amd64
Output of go env
in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/usr/local/google/home/shaojunyang/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/usr/local/google/home/shaojunyang/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3974812864=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/usr/local/google/home/shaojunyang/work/misc/bce/go.mod'
GOMODCACHE='/usr/local/google/home/shaojunyang/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/usr/local/google/home/shaojunyang/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/google/home/shaojunyang/work/misc/bce/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/usr/local/google/home/shaojunyang/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/google/home/shaojunyang/work/misc/bce/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.0'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
Compiled this program with GOSSAFUNC=addSlice go build .
package adder
//go:noinline
func addArr(a, b [4]uint64) uint64 {
var sum uint64
for i := range 4 {
sum += a[i] + b[i]
}
return sum
}
func addSlice(a, b []uint64) uint64 {
var sum uint64
if len(a) == len(b) {
if len(a) > 4 {
sum += addArr(([4]uint64)(a), ([4]uint64)(a))
a = a[4:]
b = b[4:]
}
for i := range len(a) {
sum += a[i] + b[i]
}
}
return sum
}
What did you see happen?
The for loop in addSlice
has a bound check generated for indexing b
.
What did you expect to see?
I am expecting the compiler won't generate any bound-checks for function addSlice
.
It would be great if the prove
pass can correctly reason about sub-slicing operations and propagate the equality through the control-flow correctly.
Comment From: gabyhelp
Related Issues
- cmd/compile: No BCE for str[len(str):] #53409 (closed)
- cmd/compile: Prove doesn't understand division of slice length #57077 (closed)
- cmd/compile: Improve BCE when using positive int as slice index #28885 (closed)
- cmd/compile: bce: if slice[n] is in bounds, then slice[:n+1] should have bounds check eliminated #32431 (closed)
- cmd/compile: missed trivial bound check elimination #66826 (closed)
- cmd/compile: BCE optimizes less for string than slice #27585
- cmd/compile: flashing optimization indexing into arrays bounded by a modulo bellow or equal to the length only works if the modulo is a sub expression of the indexing #63110
- cmd/compile: missing bounds check elimination for complex conditionals #72132 (closed)
- cmd/compile: some unreasonable in BCE decision making for global slices #47736 (closed)
- cmd/compile: unnecessary bounds check for position len/2 #27249 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: gopherbot
Change https://go.dev/cl/699155 mentions this issue: cmd/compile: eliminate bound check for slices of the same length