[go] internal/bytealg: vector implementation of count 1 byte for riscv64

6 views
Skip to first unread message

Meng Zhuo (Gerrit)

unread,
Mar 12, 2025, 10:21:25 AM3/12/25
to goph...@pubsubhelper.golang.org, Meng Zhuo, golang-co...@googlegroups.com

Meng Zhuo has uploaded the change for review

Commit message

internal/bytealg: vector implementation of count 1 byte for riscv64

This CL provide a vector implementation of count 1 byte

goos: linux
goarch: riscv64
pkg: bytes
cpu: Spacemit(R) X60
│ sec/op │ sec/op vs base │
CountSingle/10 42.12n ± 0% 44.49n ± 0% +5.64% (p=0.000 n=10)
CountSingle/32 85.74n ± 0% 44.49n ± 0% -48.11% (p=0.000 n=10)
CountSingle/4K 8371.5n ± 0% 396.7n ± 0% -95.26% (p=0.000 n=10)
CountSingle/4M 8929.1µ ± 0% 756.9µ ± 0% -91.52% (p=0.000 n=10)
CountSingle/64M 159.78m ± 0% 13.89m ± 0% -91.30% (p=0.000 n=10)
geomean 33.65µ 6.073µ -81.95%

│ /root/scalar_count.log │ /root/vector_count.log │
│ B/s │ B/s vs base │
CountSingle/10 226.4Mi ± 0% 214.3Mi ± 0% -5.34% (p=0.000 n=10)
CountSingle/32 355.9Mi ± 0% 685.9Mi ± 0% +92.71% (p=0.000 n=10)
CountSingle/4K 466.6Mi ± 0% 9846.5Mi ± 0% +2010.19% (p=0.000 n=10)
CountSingle/4M 448.0Mi ± 0% 5284.9Mi ± 0% +1079.74% (p=0.000 n=10)
CountSingle/64M 400.5Mi ± 0% 4606.2Mi ± 0% +1049.98% (p=0.000 n=10)
geomean 368.0Mi 1.991Gi +454.08%
Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4

Change diff

diff --git a/src/internal/bytealg/count_riscv64.s b/src/internal/bytealg/count_riscv64.s
index 3f255cd..344427e 100644
--- a/src/internal/bytealg/count_riscv64.s
+++ b/src/internal/bytealg/count_riscv64.s
@@ -2,48 +2,53 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

+#include "asm_riscv64.h"
#include "go_asm.h"
#include "textflag.h"

-TEXT ·Count<ABIInternal>(SB),NOSPLIT,$0-40
- // X10 = b_base
- // X11 = b_len
- // X12 = b_cap (unused)
- // X13 = byte to count (want in X12)
- AND $0xff, X13, X12
- MOV ZERO, X14 // count
- ADD X10, X11 // end
+TEXT ·Count<ABIInternal>(SB),NOSPLIT|NOFRAME,$0
+ MOV X13, X12 // b_cap not used
+ JMP count<>(SB)

- PCALIGN $16
-loop:
- BEQ X10, X11, done
- MOVBU (X10), X15
- ADD $1, X10
- BNE X12, X15, loop
- ADD $1, X14
- JMP loop
+TEXT ·CountString<ABIInternal>(SB),NOSPLIT|NOFRAME,$0
+ JMP count<>(SB)

-done:
- MOV X14, X10
- RET
-
-TEXT ·CountString<ABIInternal>(SB),NOSPLIT,$0-32
- // X10 = s_base
+TEXT count<>(SB),NOSPLIT|NOFRAME,$0-0
+ // X10 = s_base, return as counter
// X11 = s_len
// X12 = byte to count
- AND $0xff, X12
- MOV ZERO, X14 // count
- ADD X10, X11 // end
+ MOV X10, X14 // src pointer
+ MOV ZERO, X10 // reset counter
+ AND $0xff, X12, X12 // make sure it's a byte to compare
+ SUB $8, X11, X5
+ BLEZ X5, count_scalar
+#ifndef hasV
+ MOVB internal∕cpu·RISCV64+const_offsetRISCV64HasV(SB), X5
+ BEQZ X5, count_scalar
+#endif
+ PCALIGN $16
+count_vector_loop:
+ VSETVLI X11, E8, M8, TA, MA, X5
+ VLE8V (X14), V8
+ VMSEQVX X12, V8, V0
+ VCPOPM V0, X15
+ ADD X15, X10 // add counter
+ ADD X5, X14
+ SUB X5, X11, X11
+ BEQZ X11, done
+ JMP count_vector_loop

PCALIGN $16
-loop:
- BEQ X10, X11, done
- MOVBU (X10), X15
- ADD $1, X10
- BNE X12, X15, loop
+count_scalar:
+ ADD X14, X11 // end pointer
+ PCALIGN $16
+count_scalar_loop:
+ BEQ X14, X11, done
+ MOVBU (X14), X15
ADD $1, X14
- JMP loop
+ BNE X12, X15, count_scalar_loop
+ ADD $1, X10
+ JMP count_scalar_loop

done:
- MOV X14, X10
RET

Change information

Files:
  • M src/internal/bytealg/count_riscv64.s
Change size: M
Delta: 1 file changed, 37 insertions(+), 32 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
Gerrit-Change-Number: 657036
Gerrit-PatchSet: 1
Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Meng Zhuo (Gerrit)

unread,
Mar 13, 2025, 4:51:13 AM3/13/25
to Meng Zhuo, goph...@pubsubhelper.golang.org, Joel Sing, Mark Ryan, Pengcheng Wang, golang-co...@googlegroups.com
Attention needed from Joel Sing, Mark Ryan and Pengcheng Wang

New activity on the change

Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Mark Ryan
  • Pengcheng Wang
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
Gerrit-Change-Number: 657036
Gerrit-PatchSet: 1
Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Pengcheng Wang <wangpeng...@bytedance.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Attention: Joel Sing <jo...@sing.id.au>
Gerrit-Attention: Pengcheng Wang <wangpeng...@bytedance.com>
Gerrit-Comment-Date: Thu, 13 Mar 2025 08:51:08 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Mark Ryan (Gerrit)

unread,
Mar 13, 2025, 8:29:22 AM3/13/25
to Meng Zhuo, goph...@pubsubhelper.golang.org, Joel Sing, Pengcheng Wang, golang-co...@googlegroups.com
Attention needed from Joel Sing, Meng Zhuo and Pengcheng Wang

Mark Ryan added 4 comments

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Mark Ryan . unresolved

I tested this locally and it seems to work fine. I also see the expected speedups. We need to wait until https://go-review.googlesource.com/c/go/+/646779/6 and https://go-review.googlesource.com/c/go/+/646775/6 are merged before merging this.

File src/internal/bytealg/count_riscv64.s
Line 22, Patchset 1 (Latest): AND $0xff, X12, X12 // make sure it's a byte to compare
Mark Ryan . unresolved

AND $0xff, X12

Line 23, Patchset 1 (Latest): SUB $8, X11, X5
Mark Ryan . resolved

This looks a little low and the benchmarks in the commit message (the degradation in CountSingle/10) suggests that this might indeed be the case. However, I did benchmark this locally on a banana Pi with GORISCV64=rva23u64 and I didn't see any slowdown in CountSingle/10 so maybe it's okay.

Line 37, Patchset 1 (Latest): SUB X5, X11, X11
Mark Ryan . unresolved

SUB X5,X11

Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Meng Zhuo
  • Pengcheng Wang
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
    Gerrit-Change-Number: 657036
    Gerrit-PatchSet: 1
    Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
    Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
    Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
    Gerrit-Reviewer: Pengcheng Wang <wangpeng...@bytedance.com>
    Gerrit-Attention: Joel Sing <jo...@sing.id.au>
    Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
    Gerrit-Attention: Pengcheng Wang <wangpeng...@bytedance.com>
    Gerrit-Comment-Date: Thu, 13 Mar 2025 12:29:13 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Meng Zhuo (Gerrit)

    unread,
    May 7, 2025, 3:39:34 AM5/7/25
    to Meng Zhuo, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Joel Sing, Meng Zhuo and Pengcheng Wang

    Meng Zhuo uploaded new patchset

    Meng Zhuo uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Joel Sing
    • Meng Zhuo
    • Pengcheng Wang
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
    Gerrit-Change-Number: 657036
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Meng Zhuo (Gerrit)

    unread,
    May 7, 2025, 3:41:58 AM5/7/25
    to Meng Zhuo, goph...@pubsubhelper.golang.org, Joel Sing, Mark Ryan, Pengcheng Wang, golang-co...@googlegroups.com
    Attention needed from Joel Sing, Mark Ryan and Pengcheng Wang

    Meng Zhuo voted and added 3 comments

    Votes added by Meng Zhuo

    Commit-Queue+1

    3 comments

    Patchset-level comments

    I tested this locally and it seems to work fine. I also see the expected speedups. We need to wait until https://go-review.googlesource.com/c/go/+/646779/6 and https://go-review.googlesource.com/c/go/+/646775/6 are merged before merging this.

    Meng Zhuo

    Also CL 646736, for hasV const.

    File src/internal/bytealg/count_riscv64.s
    Line 22, Patchset 1: AND $0xff, X12, X12 // make sure it's a byte to compare
    Mark Ryan . resolved

    AND $0xff, X12

    Meng Zhuo

    Done

    Line 37, Patchset 1: SUB X5, X11, X11
    Mark Ryan . resolved

    SUB X5,X11

    Meng Zhuo

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Joel Sing
    • Mark Ryan
    • Pengcheng Wang
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
      Gerrit-Change-Number: 657036
      Gerrit-PatchSet: 2
      Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
      Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
      Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
      Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
      Gerrit-Reviewer: Pengcheng Wang <wangpeng...@bytedance.com>
      Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
      Gerrit-Attention: Joel Sing <jo...@sing.id.au>
      Gerrit-Attention: Pengcheng Wang <wangpeng...@bytedance.com>
      Gerrit-Comment-Date: Wed, 07 May 2025 07:41:51 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Mark Ryan <mark...@rivosinc.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Meng Zhuo (Gerrit)

      unread,
      Aug 6, 2025, 9:59:58 PM8/6/25
      to Meng Zhuo, goph...@pubsubhelper.golang.org, Go LUCI, Joel Sing, Mark Ryan, Pengcheng Wang, golang-co...@googlegroups.com
      Attention needed from Joel Sing, Mark Ryan and Pengcheng Wang

      Meng Zhuo voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Joel Sing
      • Mark Ryan
      • Pengcheng Wang
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
      Gerrit-Change-Number: 657036
      Gerrit-PatchSet: 3
      Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
      Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
      Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
      Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
      Gerrit-Reviewer: Pengcheng Wang <wangpeng...@bytedance.com>
      Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
      Gerrit-Attention: Joel Sing <jo...@sing.id.au>
      Gerrit-Attention: Pengcheng Wang <wangpeng...@bytedance.com>
      Gerrit-Comment-Date: Thu, 07 Aug 2025 01:59:52 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Pengcheng Wang (Gerrit)

      unread,
      3:24 AM (12 hours ago) 3:24 AM
      to Meng Zhuo, goph...@pubsubhelper.golang.org, Go LUCI, Joel Sing, Mark Ryan, golang-co...@googlegroups.com
      Attention needed from Joel Sing, Mark Ryan and Meng Zhuo

      Pengcheng Wang added 1 comment

      File src/internal/bytealg/count_riscv64.s
      Line 34, Patchset 3 (Latest): VMSEQVX X13, V8, V0
      Pengcheng Wang . unresolved
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Joel Sing
      • Mark Ryan
      • Meng Zhuo
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: If88361e8fc2293fc73d4aa5d95903770516918f4
        Gerrit-Change-Number: 657036
        Gerrit-PatchSet: 3
        Gerrit-Owner: Meng Zhuo <mengzh...@gmail.com>
        Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
        Gerrit-Reviewer: Mark Ryan <mark...@meta.com>
        Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
        Gerrit-Reviewer: Pengcheng Wang <wangpeng...@bytedance.com>
        Gerrit-Attention: Joel Sing <jo...@sing.id.au>
        Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
        Gerrit-Attention: Mark Ryan <mark...@meta.com>
        Gerrit-Comment-Date: Wed, 18 Mar 2026 07:24:21 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages