Skip to content

Commit

Permalink
cmd/compile: omit write barriers for slice clears of go:notinheap poi…
Browse files Browse the repository at this point in the history
…nters

Currently,

  for i := range a {
    a[i] = nil
  }

will compile to have write barriers even if a is a slice of pointers
to go:notinheap types. This happens because the optimization that
transforms this into a memclr only asks it a's element type has
pointers, and not if it specifically has heap pointers.

Fix this by changing arrayClear to use HasHeapPointer instead of
types.Haspointers. We probably shouldn't have both of these functions,
since a pointer to a notinheap type is effectively a uintptr, but
that's not going to change in this CL.

Change-Id: I284b85bdec6ae1e641f894e8f577989facdb0cf1
Reviewed-on: https://go-review.googlesource.com/c/152723
Run-TryBot: Austin Clements <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
aclements committed Dec 5, 2018
1 parent c8ca793 commit 6454a09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func arrayClear(n, v1, v2, a *Node) bool {
n.Nbody.Append(nod(OAS, hn, tmp))

var fn *Node
if types.Haspointers(a.Type.Elem()) {
if a.Type.Elem().HasHeapPointer() {
// memclrHasPointers(hp, hn)
Curfn.Func.setWBPos(stmt.Pos)
fn = mkcall("memclrHasPointers", nil, nil, hp, hn)
Expand Down
16 changes: 16 additions & 0 deletions test/notinheap3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ func h() {
_ = append(v1s, v1s...) // no barrier
_ = append(v2s, v2s...) // ERROR "write barrier"
}

// Slice clearing

var (
sliceIH []*ih
sliceNIH []*nih
)

func sliceClear() {
for i := range sliceIH {
sliceIH[i] = nil // ERROR "write barrier"
}
for i := range sliceNIH {
sliceNIH[i] = nil // no barrier
}
}

0 comments on commit 6454a09

Please sign in to comment.