You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\boris\AppData\Local\go-build
set GOENV=C:\Users\boris\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\boris\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\boris\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\boris\AppData\Local\Temp\go-build1984047059=/tmp/go-build -gno-record-gcc-switches
package sum
funcSum() int {
sum:=0fori:=0; i<64; i++ {
sum+=i
}
returnsum
}
Compiling the code above outputs the following assembly code for the Sum function:
Because this loop can be evaluated at compile time, the value of sum can be computed (2016). Therefore, the following assembly code should be generated instead:
0x000000000 (main.go:4) MOVL$2016,AX0x000500005 (main.go:4) RET
The text was updated successfully, but these errors were encountered:
boriskrisanov
changed the title
cmd/compile: Loop not being optimized even though its result can be computed at compile time
cmd/compile: Loop not being optimized even though it can be evaluated at compile time
Jul 19, 2022
It is possible that this optimization could result as a side effect of loop unrolling (#51302) or other related optimizations.
By itself though, I don't think it would be worth the complexity to solve just this issue. There are many other ways to avoid the cost of that loop (code generation, init functions, etc.). So I'm going to close this as declined.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputCompiling the code above outputs the following assembly code for the
Sum
function:Because this loop can be evaluated at compile time, the value of
sum
can be computed (2016). Therefore, the following assembly code should be generated instead:The text was updated successfully, but these errors were encountered: