Skip to content

Commit 7f90b96

Browse files
committed
cmd/compile: don't elide zero extension on top of signed values
v = ... compute some value, which zeros top 32 bits ... w = zero-extend v We want to remove the zero-extension operation, as it doesn't do anything. But if v is typed as a signed value, and it gets spilled/restored, it might be re-sign-extended upon restore. So the zero-extend isn't actually a NOP when there might be calls or other reasons to spill in between v and w. Fixes #68227 Change-Id: I3b30b8e56c7d70deac1fb09d2becc7395acbadf8 Reviewed-on: https://go-review.googlesource.com/c/go/+/595675 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent ea537cc commit 7f90b96

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/cmd/compile/internal/ssa/rewrite.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,11 @@ func areAdjacentOffsets(off1, off2, size int64) bool {
12871287
// depth limits recursion depth. In AMD64.rules 3 is used as limit,
12881288
// because it catches same amount of cases as 4.
12891289
func zeroUpper32Bits(x *Value, depth int) bool {
1290+
if x.Type.IsSigned() && x.Type.Size() < 8 {
1291+
// If the value is signed, it might get re-sign-extended
1292+
// during spill and restore. See issue 68227.
1293+
return false
1294+
}
12901295
switch x.Op {
12911296
case OpAMD64MOVLconst, OpAMD64MOVLload, OpAMD64MOVLQZX, OpAMD64MOVLloadidx1,
12921297
OpAMD64MOVWload, OpAMD64MOVWloadidx1, OpAMD64MOVBload, OpAMD64MOVBloadidx1,
@@ -1305,7 +1310,7 @@ func zeroUpper32Bits(x *Value, depth int) bool {
13051310
case OpArg: // note: but not ArgIntReg
13061311
// amd64 always loads args from the stack unsigned.
13071312
// most other architectures load them sign/zero extended based on the type.
1308-
return x.Type.Size() == 4 && (x.Type.IsUnsigned() || x.Block.Func.Config.arch == "amd64")
1313+
return x.Type.Size() == 4 && x.Block.Func.Config.arch == "amd64"
13091314
case OpPhi, OpSelect0, OpSelect1:
13101315
// Phis can use each-other as an arguments, instead of tracking visited values,
13111316
// just limit recursion depth.
@@ -1325,11 +1330,14 @@ func zeroUpper32Bits(x *Value, depth int) bool {
13251330

13261331
// zeroUpper48Bits is similar to zeroUpper32Bits, but for upper 48 bits.
13271332
func zeroUpper48Bits(x *Value, depth int) bool {
1333+
if x.Type.IsSigned() && x.Type.Size() < 8 {
1334+
return false
1335+
}
13281336
switch x.Op {
13291337
case OpAMD64MOVWQZX, OpAMD64MOVWload, OpAMD64MOVWloadidx1, OpAMD64MOVWloadidx2:
13301338
return true
13311339
case OpArg: // note: but not ArgIntReg
1332-
return x.Type.Size() == 2 && (x.Type.IsUnsigned() || x.Block.Func.Config.arch == "amd64")
1340+
return x.Type.Size() == 2 && x.Block.Func.Config.arch == "amd64"
13331341
case OpPhi, OpSelect0, OpSelect1:
13341342
// Phis can use each-other as an arguments, instead of tracking visited values,
13351343
// just limit recursion depth.
@@ -1349,11 +1357,14 @@ func zeroUpper48Bits(x *Value, depth int) bool {
13491357

13501358
// zeroUpper56Bits is similar to zeroUpper32Bits, but for upper 56 bits.
13511359
func zeroUpper56Bits(x *Value, depth int) bool {
1360+
if x.Type.IsSigned() && x.Type.Size() < 8 {
1361+
return false
1362+
}
13521363
switch x.Op {
13531364
case OpAMD64MOVBQZX, OpAMD64MOVBload, OpAMD64MOVBloadidx1:
13541365
return true
13551366
case OpArg: // note: but not ArgIntReg
1356-
return x.Type.Size() == 1 && (x.Type.IsUnsigned() || x.Block.Func.Config.arch == "amd64")
1367+
return x.Type.Size() == 1 && x.Block.Func.Config.arch == "amd64"
13571368
case OpPhi, OpSelect0, OpSelect1:
13581369
// Phis can use each-other as an arguments, instead of tracking visited values,
13591370
// just limit recursion depth.

test/fixedbugs/issue68227.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
package main
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
type someType []uint64
14+
15+
func (s *someType) push(v uint64) {
16+
*s = append(*s, v)
17+
}
18+
19+
func (s *someType) problematicFn(x1Lo, x1Hi, x2Lo, x2Hi uint64) {
20+
r1 := int32(int16(x1Lo>>0)) * int32(int16(x2Lo>>0))
21+
g()
22+
r3 := int32(int16(x1Lo>>32)) * int32(int16(x2Lo>>32))
23+
r4 := int32(int16(x1Lo>>48)) * int32(int16(x2Lo>>48))
24+
r5 := int32(int16(x1Hi>>0)) * int32(int16(x2Hi>>0))
25+
r7 := int32(int16(x1Hi>>32)) * int32(int16(x2Hi>>32))
26+
r8 := int32(int16(x1Hi>>48)) * int32(int16(x2Hi>>48))
27+
s.push(uint64(uint32(r1)) | (uint64(uint32(r3+r4)) << 32))
28+
s.push(uint64(uint32(r5)) | (uint64(uint32(r7+r8)) << 32))
29+
}
30+
31+
//go:noinline
32+
func g() {
33+
}
34+
35+
func main() {
36+
s := &someType{}
37+
s.problematicFn(0x1000100010001, 0x1000100010001, 0xffffffffffffffff, 0xffffffffffffffff)
38+
for i := 0; i < 2; i++ {
39+
if got, want := (*s)[i], uint64(0xfffffffeffffffff); got != want {
40+
fmt.Printf("s[%d]=%x, want %x\n", i, got, want)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)