Skip to content

Commit 8fbd4d7

Browse files
randall77cherrymui
authored andcommitted
[release-branch.go1.21] 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 #67164 (Reroll of CL 583303) Change-Id: Id067cc2e39d7d199672f2017f7f53991fb23f8d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/583799 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 77cfa29 commit 8fbd4d7

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
@@ -148,7 +148,7 @@ func calculateCostForType(t *types.Type) int64 {
148148
return EqStructCost(t)
149149
case types.TSLICE:
150150
// Slices are not comparable.
151-
base.Fatalf("eqStructFieldCost: unexpected slice type")
151+
base.Fatalf("calculateCostForType: unexpected slice type")
152152
case types.TARRAY:
153153
elemCost := calculateCostForType(t.Elem())
154154
cost = t.NumElem() * elemCost
@@ -374,6 +374,11 @@ func eqmem(p ir.Node, q ir.Node, field *types.Sym, size int64) ir.Node {
374374
}
375375

376376
func eqmemfunc(size int64, t *types.Type) (fn *ir.Name, needsize bool) {
377+
if !base.Ctxt.Arch.CanMergeLoads && t.Alignment() < int64(base.Ctxt.Arch.Alignment) && t.Alignment() < t.Size() {
378+
// We can't use larger comparisons if the value might not be aligned
379+
// enough for the larger comparison. See issues 46283 and 67160.
380+
size = 0
381+
}
377382
switch size {
378383
default:
379384
fn = typecheck.LookupRuntime("memequal")

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)