Skip to content

cmd/compile: condition in for loop body is incorrectly optimised away #53600

@kortschak

Description

@kortschak

What version of Go are you using (go version)?

$ go version
go version go1.18.3 linux/amd64

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
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/user/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4057835266=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The program at https://go.dev/play/p/QnP9ojw3vCV does not terminate. A similar program https://go.dev/play/p/86YpX7rbHHT with the for condition clause removed does correct terminate.

If optimisation is turned off, the the first program behaves as expected.

What did you expect to see?

The output from both programs to be

9223372036854775807 false
done

Program exited.

What did you see instead?

The first program prints the following and does not terminate.

9223372036854775807 false
-9223372036854775808 true
-9223372036854775807 true
-9223372036854775806 true
...

The assembly generated from the first program appears to have the condition body omptimised away (https://godbolt.org/z/9jW1GaKfv) unless the for condition clause is removed (https://godbolt.org/z/c3Pvnb3ad) or optimisations are turned off (https://godbolt.org/z/o7o6x3bzd). This appears to be independent of GOARCH (at least within {amd64, arm64}.

See also discussion at https://groups.google.com/g/golang-nuts/c/3_-g6hBfo9s.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions