Skip to content
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: condition in for loop body is incorrectly optimised away #53600

Closed
kortschak opened this issue Jun 29, 2022 · 4 comments
Closed
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kortschak
Copy link
Contributor

kortschak commented Jun 29, 2022

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.

@gopherbot
Copy link

gopherbot commented Jun 29, 2022

Change https://go.dev/cl/415219 mentions this issue: cmd/compile: Fix prove pass when upper condition is <= maxint

@randall77
Copy link
Contributor

randall77 commented Jun 29, 2022

@gopherbot please open backport issues for 1.17 and 1.18.

This is a rare bug but results in miscompilation of loops with termination conditions "<= C" for large C.

@gopherbot
Copy link

gopherbot commented Jun 29, 2022

Backport issue(s) opened: #53617 (for 1.17), #53618 (for 1.18).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

@dr2chase dr2chase added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 30, 2022
@gopherbot
Copy link

gopherbot commented Jul 2, 2022

Change https://go.dev/cl/415874 mentions this issue: cmd/compile: rework induction variable detector

@dmitshur dmitshur added this to the Go1.19 milestone Jul 4, 2022
gopherbot pushed a commit that referenced this issue Jul 6, 2022
Induction variable detection is still not quite right. I've added
another failing test.

Redo the overflow/underflow detector so it is more obviously correct.

Update #53600
Fixes #53653
Fixes #53663

Change-Id: Id95228e282fdbf6bd80b26e1c41d62e935ba08ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/415874
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: David Chase <drchase@google.com>
jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
When the terminating condition is <= X, we need to make sure that
X+step doesn't overflow.

Fixes golang#53600

Change-Id: I36e5384d05b4d7168e48db6094200fcae409bfe5
Reviewed-on: https://go-review.googlesource.com/c/go/+/415219
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: David Chase <drchase@google.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
Induction variable detection is still not quite right. I've added
another failing test.

Redo the overflow/underflow detector so it is more obviously correct.

Update golang#53600
Fixes golang#53653
Fixes golang#53663

Change-Id: Id95228e282fdbf6bd80b26e1c41d62e935ba08ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/415874
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants