aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkhr@golang.org <khr@golang.org>2024-05-03 12:55:34 -0700
committerKeith Randall <khr@google.com>2024-05-06 18:11:05 +0000
commit9d2e28501c7dea1c0ddde00ac683105d0bfe9f63 (patch)
tree3c970d8f2ba1c0315e57a5634eada6508bbc27dc
parent93d8777d244962d1b706c0b695c8b72e9702577e (diff)
downloadgo-9d2e28501c7dea1c0ddde00ac683105d0bfe9f63.tar.gz
go-9d2e28501c7dea1c0ddde00ac683105d0bfe9f63.zip
[release-branch.go1.22] cmd/compile: don't combine loads in generated equality functions
... if the architecture can't do unaligned loads. We already handle this in a few places, but this particular place was added in CL 399542 and missed this additional restriction. Fixes #67212 Change-Id: I45988f11ff3ed45df1c4da3f0931ab1fdb22dbfe Reviewed-on: https://go-review.googlesource.com/c/go/+/583175 Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Derek Parker <parkerderek86@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> (cherry picked from commit 3c72dd513c30df60c0624360e98a77c4ae7ca7c8) Reviewed-on: https://go-review.googlesource.com/c/go/+/583417
-rw-r--r--src/cmd/compile/internal/compare/compare.go7
-rw-r--r--test/fixedbugs/issue67160.go32
2 files changed, 38 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/compare/compare.go b/src/cmd/compile/internal/compare/compare.go
index e165cd67db..cb2f84ef55 100644
--- a/src/cmd/compile/internal/compare/compare.go
+++ b/src/cmd/compile/internal/compare/compare.go
@@ -148,7 +148,7 @@ func calculateCostForType(t *types.Type) int64 {
return EqStructCost(t)
case types.TSLICE:
// Slices are not comparable.
- base.Fatalf("eqStructFieldCost: unexpected slice type")
+ base.Fatalf("calculateCostForType: unexpected slice type")
case types.TARRAY:
elemCost := calculateCostForType(t.Elem())
cost = t.NumElem() * elemCost
@@ -371,6 +371,11 @@ func eqmem(p, q ir.Node, field int, size int64) ir.Node {
}
func eqmemfunc(size int64, t *types.Type) (fn *ir.Name, needsize bool) {
+ if !base.Ctxt.Arch.CanMergeLoads && t.Alignment() < int64(base.Ctxt.Arch.Alignment) && t.Alignment() < t.Size() {
+ // We can't use larger comparisons if the value might not be aligned
+ // enough for the larger comparison. See issues 46283 and 67160.
+ size = 0
+ }
switch size {
case 1, 2, 4, 8, 16:
buf := fmt.Sprintf("memequal%d", int(size)*8)
diff --git a/test/fixedbugs/issue67160.go b/test/fixedbugs/issue67160.go
new file mode 100644
index 0000000000..be45a61420
--- /dev/null
+++ b/test/fixedbugs/issue67160.go
@@ -0,0 +1,32 @@
+// run
+
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test to make sure that we don't try using larger loads for
+// generated equality functions on architectures that can't do
+// unaligned loads.
+
+package main
+
+// T has a big field that wants to be compared with larger loads/stores.
+// T is "special" because of the unnamed field, so it needs a generated equality function.
+// T is an odd number of bytes in size and has alignment 1.
+type T struct {
+ src [8]byte
+ _ byte
+}
+
+// U contains 8 copies of T, each at a different %8 alignment.
+type U [8]T
+
+//go:noinline
+func f(x, y *U) bool {
+ return *x == *y
+}
+
+func main() {
+ var a U
+ _ = f(&a, &a)
+}