]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.18] cmd/compile: sign-extend the 2nd argument of the LoweredAtomi...
authorDavid Chase <drchase@google.com>
Mon, 19 Dec 2022 16:59:13 +0000 (11:59 -0500)
committerGopher Robot <gobot@golang.org>
Wed, 28 Dec 2022 22:17:03 +0000 (22:17 +0000)
The function LoweredAtomicCas32 is implemented using the LL-SC instruction pair
on loong64, mips64x, riscv64. However,the LL instruction on loong64, mips64x,
riscv64 is sign-extended, so it is necessary to sign-extend the 2nd parameter
"old" of the LoweredAtomicCas32, so that the instruction BNE after LL can get
the desired result.

The function prototype of LoweredAtomicCas32 in golang:
    func Cas32(ptr *uint32, old, new uint32) bool

When using an intrinsify implementation:
    case 1: (*ptr) <= 0x80000000 && old < 0x80000000
        E.g: (*ptr) = 0x7FFFFFFF, old = Rarg1= 0x7FFFFFFF

        After run the instruction "LL (Rarg0), Rtmp": Rtmp = 0x7FFFFFFF
        Rtmp ! = Rarg1(old) is false, the result we expect

    case 2: (*ptr) >= 0x80000000 && old >= 0x80000000
        E.g: (*ptr) = 0x80000000, old = Rarg1= 0x80000000

        After run the instruction "LL (Rarg0), Rtmp": Rtmp = 0xFFFFFFFF_80000000
        Rtmp ! = Rarg1(old) is true, which we do not expect

When using an non-intrinsify implementation:
    Because Rarg1 is loaded from the stack using sign-extended instructions
    ld.w, the situation described in Case 2 above does not occur

Benchmarks on linux/loong64:
name     old time/op  new time/op  delta
Cas      50.0ns ± 0%  50.1ns ± 0%   ~     (p=1.000 n=1+1)
Cas64    50.0ns ± 0%  50.1ns ± 0%   ~     (p=1.000 n=1+1)
Cas-4    56.0ns ± 0%  56.0ns ± 0%   ~     (p=1.000 n=1+1)
Cas64-4  56.0ns ± 0%  56.0ns ± 0%   ~     (p=1.000 n=1+1)

Benchmarks on Loongson 3A4000 (GOARCH=mips64le, 1.8GHz)
name     old time/op  new time/op  delta
Cas      70.4ns ± 0%  70.3ns ± 0%   ~     (p=1.000 n=1+1)
Cas64    70.7ns ± 0%  70.6ns ± 0%   ~     (p=1.000 n=1+1)
Cas-4    81.1ns ± 0%  80.8ns ± 0%   ~     (p=1.000 n=1+1)
Cas64-4  80.9ns ± 0%  80.9ns ± 0%   ~     (p=1.000 n=1+1)

Fixes #57344

Change-Id: I190a7fc648023b15fa392f7fdda5ac18c1561bac
Reviewed-on: https://go-review.googlesource.com/c/go/+/457135
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Wayne Zuo <wdvxdr@golangcn.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/458357
Run-TryBot: David Chase <drchase@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>

src/cmd/compile/internal/ssa/gen/MIPS64.rules
src/cmd/compile/internal/ssa/gen/RISCV64.rules
src/cmd/compile/internal/ssa/rewriteMIPS64.go
src/cmd/compile/internal/ssa/rewriteRISCV64.go
src/runtime/internal/atomic/atomic_test.go

index 292ff2fc7966092fac267f78a39a1d939e221c12..0d6d30fa4c93c9eeeaffca21574a12a1b6fcb335 100644 (file)
 
 (AtomicAdd(32|64) ...) => (LoweredAtomicAdd(32|64) ...)
 
-(AtomicCompareAndSwap(32|64) ...) => (LoweredAtomicCas(32|64) ...)
+(AtomicCompareAndSwap32 ptr old new mem) => (LoweredAtomicCas32 ptr (SignExt32to64 old) new mem)
+(AtomicCompareAndSwap64 ...) => (LoweredAtomicCas64 ...)
 
 // checks
 (NilCheck ...) => (LoweredNilCheck ...)
index 7aea622c5e81139212726c98f7c72db9ca9be3c3..acef3df3893b9a48eb9ff5bb5819217ed272f41b 100644 (file)
 
 (AtomicAnd32 ...) => (LoweredAtomicAnd32 ...)
 
-(AtomicCompareAndSwap32 ...) => (LoweredAtomicCas32 ...)
+(AtomicCompareAndSwap32 ptr old new mem) => (LoweredAtomicCas32 ptr (SignExt32to64 old) new mem)
 (AtomicCompareAndSwap64 ...) => (LoweredAtomicCas64 ...)
 
 (AtomicExchange32 ...) => (LoweredAtomicExchange32 ...)
index 1fbd556b5cbcd7d32cb9ad6424d5c5d2a14580e7..6a0fd3ad6ee20352bb2eb4b72fc125de7cc8646c 100644 (file)
@@ -52,8 +52,7 @@ func rewriteValueMIPS64(v *Value) bool {
                v.Op = OpMIPS64LoweredAtomicAdd64
                return true
        case OpAtomicCompareAndSwap32:
-               v.Op = OpMIPS64LoweredAtomicCas32
-               return true
+               return rewriteValueMIPS64_OpAtomicCompareAndSwap32(v)
        case OpAtomicCompareAndSwap64:
                v.Op = OpMIPS64LoweredAtomicCas64
                return true
@@ -697,6 +696,27 @@ func rewriteValueMIPS64_OpAddr(v *Value) bool {
                return true
        }
 }
+func rewriteValueMIPS64_OpAtomicCompareAndSwap32(v *Value) bool {
+       v_3 := v.Args[3]
+       v_2 := v.Args[2]
+       v_1 := v.Args[1]
+       v_0 := v.Args[0]
+       b := v.Block
+       typ := &b.Func.Config.Types
+       // match: (AtomicCompareAndSwap32 ptr old new mem)
+       // result: (LoweredAtomicCas32 ptr (SignExt32to64 old) new mem)
+       for {
+               ptr := v_0
+               old := v_1
+               new := v_2
+               mem := v_3
+               v.reset(OpMIPS64LoweredAtomicCas32)
+               v0 := b.NewValue0(v.Pos, OpSignExt32to64, typ.Int64)
+               v0.AddArg(old)
+               v.AddArg4(ptr, v0, new, mem)
+               return true
+       }
+}
 func rewriteValueMIPS64_OpAvg64u(v *Value) bool {
        v_1 := v.Args[1]
        v_0 := v.Args[0]
index 6828d97ff8f7df0e26d9360b776110f5aeeb8c7b..b277979061ecc8620b32de15b78e68b8dc6dfaf4 100644 (file)
@@ -61,8 +61,7 @@ func rewriteValueRISCV64(v *Value) bool {
        case OpAtomicAnd8:
                return rewriteValueRISCV64_OpAtomicAnd8(v)
        case OpAtomicCompareAndSwap32:
-               v.Op = OpRISCV64LoweredAtomicCas32
-               return true
+               return rewriteValueRISCV64_OpAtomicCompareAndSwap32(v)
        case OpAtomicCompareAndSwap64:
                v.Op = OpRISCV64LoweredAtomicCas64
                return true
@@ -765,6 +764,27 @@ func rewriteValueRISCV64_OpAtomicAnd8(v *Value) bool {
                return true
        }
 }
+func rewriteValueRISCV64_OpAtomicCompareAndSwap32(v *Value) bool {
+       v_3 := v.Args[3]
+       v_2 := v.Args[2]
+       v_1 := v.Args[1]
+       v_0 := v.Args[0]
+       b := v.Block
+       typ := &b.Func.Config.Types
+       // match: (AtomicCompareAndSwap32 ptr old new mem)
+       // result: (LoweredAtomicCas32 ptr (SignExt32to64 old) new mem)
+       for {
+               ptr := v_0
+               old := v_1
+               new := v_2
+               mem := v_3
+               v.reset(OpRISCV64LoweredAtomicCas32)
+               v0 := b.NewValue0(v.Pos, OpSignExt32to64, typ.Int64)
+               v0.AddArg(old)
+               v.AddArg4(ptr, v0, new, mem)
+               return true
+       }
+}
 func rewriteValueRISCV64_OpAtomicOr8(v *Value) bool {
        v_2 := v.Args[2]
        v_1 := v.Args[1]
index 2ae60b8507ef5831e2d5345514224696c0b85623..2427bfd211ee9cd84e858783d3a2c63473b7c2fe 100644 (file)
@@ -345,6 +345,36 @@ func TestBitwiseContended(t *testing.T) {
        }
 }
 
+func TestCasRel(t *testing.T) {
+       const _magic = 0x5a5aa5a5
+       var x struct {
+               before uint32
+               i      uint32
+               after  uint32
+               o      uint32
+               n      uint32
+       }
+
+       x.before = _magic
+       x.after = _magic
+       for j := 0; j < 32; j += 1 {
+               x.i = (1 << j) + 0
+               x.o = (1 << j) + 0
+               x.n = (1 << j) + 1
+               if !atomic.CasRel(&x.i, x.o, x.n) {
+                       t.Fatalf("should have swapped %#x %#x", x.o, x.n)
+               }
+
+               if x.i != x.n {
+                       t.Fatalf("wrong x.i after swap: x.i=%#x x.n=%#x", x.i, x.n)
+               }
+
+               if x.before != _magic || x.after != _magic {
+                       t.Fatalf("wrong magic: %#x _ %#x != %#x _ %#x", x.before, x.after, _magic, _magic)
+               }
+       }
+}
+
 func TestStorepNoWB(t *testing.T) {
        var p [2]*int
        for i := range p {