From 6b9e3f883e6820a1e94448ca81eba62341cd4836 Mon Sep 17 00:00:00 2001 From: zikaeroh Date: Thu, 29 Jul 2021 21:15:52 -0700 Subject: [PATCH] cmd/compile: don't emit write barriers for offsets of global addresses Currently, write barriers aren't emitted for global addresses, but they are emitted for addresses offset of global addresses. This CL changes IsGlobalAddr to recognize offsets of global addresses as globals too, removing write barriers for staticuint64s based addresses. The logic added is the same as used in IsStackAddr. Updates #37612 Change-Id: I537579f85b9ad02987d94f3ee0b4508b90097959 Reviewed-on: https://go-review.googlesource.com/c/go/+/342129 Reviewed-by: Keith Randall Reviewed-by: Cherry Mui Trust: Michael Knyszek Run-TryBot: Cherry Mui TryBot-Result: Go Bot --- src/cmd/compile/internal/ssa/writebarrier.go | 3 +++ test/writebarrier.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/cmd/compile/internal/ssa/writebarrier.go b/src/cmd/compile/internal/ssa/writebarrier.go index 419d91d0d36..d7510965f6b 100644 --- a/src/cmd/compile/internal/ssa/writebarrier.go +++ b/src/cmd/compile/internal/ssa/writebarrier.go @@ -552,6 +552,9 @@ func IsStackAddr(v *Value) bool { // IsGlobalAddr reports whether v is known to be an address of a global (or nil). func IsGlobalAddr(v *Value) bool { + for v.Op == OpOffPtr || v.Op == OpAddPtr || v.Op == OpPtrIndex || v.Op == OpCopy { + v = v.Args[0] + } if v.Op == OpAddr && v.Args[0].Op == OpSB { return true // address of a global } diff --git a/test/writebarrier.go b/test/writebarrier.go index dbf0b6dde28..1b30fa509e5 100644 --- a/test/writebarrier.go +++ b/test/writebarrier.go @@ -289,3 +289,17 @@ func f27(p *int) []interface{} { p, // ERROR "write barrier" } } + +var g28 [256]uint64 + +func f28() []interface{} { + return []interface{}{ + false, // no write barrier + true, // no write barrier + 0, // no write barrier + 1, // no write barrier + uint8(127), // no write barrier + int8(-4), // no write barrier + &g28[5], // no write barrier + } +}