Skip to content

Commit

Permalink
cmd/compile: intrinsify Sub64 on riscv64
Browse files Browse the repository at this point in the history
After this CL, the performance difference in crypto/elliptic
benchmarks on linux/riscv64 are:

name                 old time/op    new time/op    delta
ScalarBaseMult/P256    1.64ms ± 1%    1.60ms ± 1%   -2.36%  (p=0.008 n=5+5)
ScalarBaseMult/P224    1.53ms ± 1%    1.47ms ± 2%   -4.24%  (p=0.008 n=5+5)
ScalarBaseMult/P384    5.12ms ± 2%    5.03ms ± 2%     ~     (p=0.095 n=5+5)
ScalarBaseMult/P521    22.3ms ± 2%    13.8ms ± 1%  -37.89%  (p=0.008 n=5+5)
ScalarMult/P256        4.49ms ± 2%    4.26ms ± 2%   -5.13%  (p=0.008 n=5+5)
ScalarMult/P224        4.33ms ± 1%    4.09ms ± 1%   -5.59%  (p=0.008 n=5+5)
ScalarMult/P384        16.3ms ± 1%    15.5ms ± 2%   -4.78%  (p=0.008 n=5+5)
ScalarMult/P521         101ms ± 0%      47ms ± 2%  -53.36%  (p=0.008 n=5+5)

Change-Id: I31cf0506e27f9d85f576af1813630a19c20dda8a
Reviewed-on: https://go-review.googlesource.com/c/go/+/420095
Reviewed-by: Cherry Mui <[email protected]>
Reviewed-by: Joel Sing <[email protected]>
Reviewed-by: David Chase <[email protected]>
Run-TryBot: Wayne Zuo <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
wdvxdr1123 authored and 4a6f656c committed Aug 27, 2022
1 parent 969f48a commit a621973
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/cmd/compile/internal/ssa/gen/RISCV64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
(Select1 (Add64carry x y c)) =>
(OR (SLTU <typ.UInt64> s:(ADD <typ.UInt64> x y) x) (SLTU <typ.UInt64> (ADD <typ.UInt64> s c) s))

(Select0 (Sub64borrow x y c)) => (SUB (SUB <typ.UInt64> x y) c)
(Select1 (Sub64borrow x y c)) =>
(OR (SLTU <typ.UInt64> x s:(SUB <typ.UInt64> x y)) (SLTU <typ.UInt64> s (SUB <typ.UInt64> s c)))

// (x + y) / 2 => (x / 2) + (y / 2) + (x & y & 1)
(Avg64u <t> x y) => (ADD (ADD <t> (SRLI <t> [1] x) (SRLI <t> [1] y)) (ANDI <t> [1] (AND <t> x y)))

Expand Down
36 changes: 36 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteRISCV64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cmd/compile/internal/ssagen/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4732,8 +4732,8 @@ func InitTables() {
func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value {
return s.newValue3(ssa.OpSub64borrow, types.NewTuple(types.Types[types.TUINT64], types.Types[types.TUINT64]), args[0], args[1], args[2])
},
sys.AMD64, sys.ARM64, sys.PPC64, sys.S390X)
alias("math/bits", "Sub", "math/bits", "Sub64", sys.ArchAMD64, sys.ArchARM64, sys.ArchS390X)
sys.AMD64, sys.ARM64, sys.PPC64, sys.S390X, sys.RISCV64)
alias("math/bits", "Sub", "math/bits", "Sub64", sys.ArchAMD64, sys.ArchARM64, sys.ArchS390X, sys.ArchRISCV64)
addF("math/bits", "Div64",
func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value {
// check for divide-by-zero/overflow and panic with appropriate message
Expand Down
8 changes: 8 additions & 0 deletions test/codegen/mathbits.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ func Sub(x, y, ci uint) (r, co uint) {
// ppc64:"SUBC", "SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", "SUBE", "SUBZE", "NEG"
// s390x:"SUBE"
// riscv64: "SUB","SLTU"
return bits.Sub(x, y, ci)
}

Expand All @@ -630,6 +631,7 @@ func SubC(x, ci uint) (r, co uint) {
// ppc64:"SUBC", "SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", "SUBE", "SUBZE", "NEG"
// s390x:"SUBE"
// riscv64: "SUB","SLTU"
return bits.Sub(x, 7, ci)
}

Expand All @@ -639,6 +641,7 @@ func SubZ(x, y uint) (r, co uint) {
// ppc64:"SUBC", -"SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", -"SUBE", "SUBZE", "NEG"
// s390x:"SUBC"
// riscv64: "SUB","SLTU"
return bits.Sub(x, y, 0)
}

Expand All @@ -648,6 +651,7 @@ func SubR(x, y, ci uint) uint {
// ppc64:"SUBC", "SUBE", -"SUBZE", -"NEG"
// ppc64le:"SUBC", "SUBE", -"SUBZE", -"NEG"
// s390x:"SUBE"
// riscv64: "SUB",-"SLTU"
r, _ := bits.Sub(x, y, ci)
return r
}
Expand All @@ -669,6 +673,7 @@ func Sub64(x, y, ci uint64) (r, co uint64) {
// ppc64:"SUBC", "SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", "SUBE", "SUBZE", "NEG"
// s390x:"SUBE"
// riscv64: "SUB","SLTU"
return bits.Sub64(x, y, ci)
}

Expand All @@ -678,6 +683,7 @@ func Sub64C(x, ci uint64) (r, co uint64) {
// ppc64:"SUBC", "SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", "SUBE", "SUBZE", "NEG"
// s390x:"SUBE"
// riscv64: "SUB","SLTU"
return bits.Sub64(x, 7, ci)
}

Expand All @@ -687,6 +693,7 @@ func Sub64Z(x, y uint64) (r, co uint64) {
// ppc64:"SUBC", -"SUBE", "SUBZE", "NEG"
// ppc64le:"SUBC", -"SUBE", "SUBZE", "NEG"
// s390x:"SUBC"
// riscv64: "SUB","SLTU"
return bits.Sub64(x, y, 0)
}

Expand All @@ -696,6 +703,7 @@ func Sub64R(x, y, ci uint64) uint64 {
// ppc64:"SUBC", "SUBE", -"SUBZE", -"NEG"
// ppc64le:"SUBC", "SUBE", -"SUBZE", -"NEG"
// s390x:"SUBE"
// riscv64: "SUB",-"SLTU"
r, _ := bits.Sub64(x, y, ci)
return r
}
Expand Down

0 comments on commit a621973

Please sign in to comment.