Skip to content

Commit 3c72dd5

Browse files
randall77gopherbot
authored andcommitted
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 #67160 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>
1 parent cc16599 commit 3c72dd5

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/cmd/compile/internal/compare/compare.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func calculateCostForType(t *types.Type) int64 {
147147
return EqStructCost(t)
148148
case types.TSLICE:
149149
// Slices are not comparable.
150-
base.Fatalf("eqStructFieldCost: unexpected slice type")
150+
base.Fatalf("calculateCostForType: unexpected slice type")
151151
case types.TARRAY:
152152
elemCost := calculateCostForType(t.Elem())
153153
cost = t.NumElem() * elemCost
@@ -370,6 +370,11 @@ func eqmem(p, q ir.Node, field int, size int64) ir.Node {
370370
}
371371

372372
func eqmemfunc(size int64, t *types.Type) (fn *ir.Name, needsize bool) {
373+
if !base.Ctxt.Arch.CanMergeLoads && t.Alignment() < int64(base.Ctxt.Arch.Alignment) && t.Alignment() < t.Size() {
374+
// We can't use larger comparisons if the value might not be aligned
375+
// enough for the larger comparison. See issues 46283 and 67160.
376+
size = 0
377+
}
373378
switch size {
374379
case 1, 2, 4, 8, 16:
375380
buf := fmt.Sprintf("memequal%d", int(size)*8)

test/fixedbugs/issue67160.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// run
2+
3+
// Copyright 2024 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Test to make sure that we don't try using larger loads for
8+
// generated equality functions on architectures that can't do
9+
// unaligned loads.
10+
11+
package main
12+
13+
// T has a big field that wants to be compared with larger loads/stores.
14+
// T is "special" because of the unnamed field, so it needs a generated equality function.
15+
// T is an odd number of bytes in size and has alignment 1.
16+
type T struct {
17+
src [8]byte
18+
_ byte
19+
}
20+
21+
// U contains 8 copies of T, each at a different %8 alignment.
22+
type U [8]T
23+
24+
//go:noinline
25+
func f(x, y *U) bool {
26+
return *x == *y
27+
}
28+
29+
func main() {
30+
var a U
31+
_ = f(&a, &a)
32+
}

0 commit comments

Comments
 (0)