Skip to content

cmd/compile: reordering regular loads over atomic add #20335

Closed

Description

@aclements

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

Tested on both go version go1.8.1 linux/amd64 and go version devel +8c49c06b48 Mon May 8 15:16:21 2017 +0000 linux/amd64.

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

linux/amd64

What did you do?

runtime.gchelper ends with the following few lines:

	nproc := work.nproc // work.nproc can change right after we increment work.ndone
	if atomic.Xadd(&work.ndone, +1) == nproc-1 {
		notewakeup(&work.alldone)
	}

What did you expect to see?

I expected the atomic.Xadd to act as a memory fence and force the regular load of work.nproc to happen before the atomic.

What did you see instead?

The compiler reorders the regular load to occur after the atomic.Xadd. I believe this is leading to #19305.

/cc @dr2chase @cherrymui

Activity

  1. added this to the Go1.8.2 milestone on May 11, 2017
  2. randall77 commented on May 11, 2017

    @randall77
    Contributor

    Minimal repro:

    package main
    
    import "sync/atomic"
    
    func f(p, q *int32) bool {
    	x := *q
    	return atomic.AddInt32(p, 1) == x
    }
    

    The *q occurs after the atomic. Something is wrong with the schedule pass

        v9 = XADDLlock <uint32,mem> v12 v7 v1
        v5 = Select0 <uint32> v9
        v14 = Select1 <mem> v9
        v11 = MOVLload <int32> v8 v1
    

    There's more than one memory variable live before v11 (v1 and v14).

  3. self-assigned this
    on May 11, 2017
  4. gopherbot commented on May 11, 2017

    @gopherbot
    Contributor

    CL https://golang.org/cl/43294 mentions this issue.

  5. gopherbot commented on May 11, 2017

    @gopherbot
    Contributor

    CL https://golang.org/cl/43311 mentions this issue.

  6. randall77 commented on May 11, 2017

    @randall77
    Contributor

    Reopen for 1.8.2.

  7. gopherbot commented on May 12, 2017

    @gopherbot
    Contributor

    CL https://golang.org/cl/43411 mentions this issue.

  8. gopherbot commented on May 15, 2017

    @gopherbot
    Contributor

    CL https://golang.org/cl/43495 mentions this issue.

  9. modified the milestones: Go1.8.2, Go1.8.3 on May 18, 2017
  10. added a commit that references this issue on May 23, 2017
    1054085
  11. locked and limited conversation to collaborators on May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions