Skip to content

cmd/go: code/instruction with side effects is disappeared #40572

@xiaopengli89

Description

@xiaopengli89

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

$ go version
go version go1.14.5 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tora/.cache/go-build"
GOENV="/home/tora/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tora/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/tora/code/go/proj/foo/go.mod"
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-build205500719=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"fmt"
	"runtime"
	"time"
)

var a int32 = 0

func main() {
	runtime.GOMAXPROCS(2)
	go func() {
		for {
			a = 5
			// If uncomment code below, everything is ok
			// runtime.Gosched()
		}
	}()
	go func() {
		for {
			fmt.Println("a:", a)
		}
	}()
	time.Sleep(200 * time.Second)
}

What did you expect to see?

Finally a: 5 should appear on the screen

What did you see instead?

Always a: 0 appears on the screen, and a = 5 instruction is disappeared. Is this the expected behavior?

0000000000491980 <main.main.func1>:
  491980:       90                      nop
  491981:       eb fd                   jmp    491980 <main.main.func1>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions