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/internal/ssa: incorrect deadcode elim #47594

Closed
wdvxdr1123 opened this issue Aug 8, 2021 · 1 comment
Closed

cmd/compile/internal/ssa: incorrect deadcode elim #47594

wdvxdr1123 opened this issue Aug 8, 2021 · 1 comment

Comments

@wdvxdr1123
Copy link
Contributor

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

$ go version
go version go1.17rc2 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\wdvxdr\AppData\Local\go-build
set GOENV=C:\Users\wdvxdr\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\wdvxdr\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\wdvxdr\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Users\wdvxdr\sdk\go1.17rc2
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\wdvxdr\sdk\go1.17rc2\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17rc2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=xxxxxxxxxx
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\wdvxdr\AppData\Local\Temp\go-build3667860290=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"fmt"
	"time"
)

func main() {
	x := 0
	go func() {
		for {
			x++
		}
	}()
	time.Sleep(time.Second)
	fmt.Println(x)
}

What did you expect to see?

output > 0

What did you see instead?

output: 0

function main.func1 SSA:

b1:-
v1 (?) = InitMem <mem>
v2 (?) = SP <uintptr>
v3 (?) = SB <uintptr>
v4 (?) = LocalAddr <**int> {&x} v2 v1
v5 (10) = Arg <*int> {&x} (&x[*int])
v9 (?) = Const64 <int> [1]
Plain → b2 (+11)
b2: ← b1 b4-
v13 (12) = Phi <mem> v1 v11
v14 (12) = Copy <*int> v5
Plain → b3
b3: ← b2-
v6 (12) = Copy <*int> v5
v7 (12) = Copy <mem> v13
v8 (+12) = Load <int> v5 v13
v10 (12) = Add64 <int> v8 v9
v11 (12) = Store <mem> {int} v5 v10 v13
Plain → b4 (12)
b4: ← b3
Plain → b2 (12)
b5:-
v12 (14) = Unknown <mem>
Ret v12
name &x[*int]: v5 v5 v5

After deadcode elim:

b1:
Plain → b2 (+11)
b2: ← b1 b4
Plain → b3
b3: ← b2
Plain → b4 (+12)
b4: ← b3
Plain → b2 (12)
@randall77
Copy link
Contributor

This is expected. There is no synchronization in your code. It has a data race and the compiler is allowed to optimize such accesses away.
See, e.g. #45826 or #30339 .

@golang golang locked and limited conversation to collaborators Aug 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants