[go] cmd/internal/obj/riscv: implement better bit pattern encoding

10 views
Skip to first unread message

Joel Sing (Gerrit)

unread,
Sep 10, 2025, 10:03:24 AMSep 10
to Mark Ryan, Meng Zhuo, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Mark Ryan and Meng Zhuo

Joel Sing has uploaded the change for review

Joel Sing would like Mark Ryan and Meng Zhuo to review this change.

Commit message

cmd/internal/obj/riscv: implement better bit pattern encoding

Replace the extractBitAndShift function with an encodeBitPattern function.
This allows the caller to specify a slice of bits that are to be extracted
and encoded, rather than making multiple function calls and combining the
results.
Change-Id: I3d51caa10ecf714f2ad2fb66d38376202c4e0628

Change diff

diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go
index a30c6db..542bef4 100644
--- a/src/cmd/internal/obj/riscv/obj.go
+++ b/src/cmd/internal/obj/riscv/obj.go
@@ -1437,10 +1437,14 @@
wantImmU(ctxt, ins, ins.imm, 32)
}

-// extractBitAndShift extracts the specified bit from the given immediate,
-// before shifting it to the requested position and returning it.
-func extractBitAndShift(imm uint32, bit, pos int) uint32 {
- return ((imm >> bit) & 1) << pos
+// encodeBitPattern encodes an immediate value by extracting the specified
+// bit pattern from the given immediate.
+func encodeBitPattern(imm uint32, pattern []int) uint32 {
+ outImm := uint32(0)
+ for _, bit := range pattern {
+ outImm = outImm<<1 | (imm>>bit)&1
+ }
+ return outImm
}

// encodeR encodes an R-type RISC-V instruction.
@@ -1665,31 +1669,14 @@
// encodeCBImmediate encodes an immediate for a CB-type RISC-V instruction.
func encodeCBImmediate(imm uint32) uint32 {
// Bit order - [8|4:3|7:6|2:1|5]
- bits := extractBitAndShift(imm, 8, 7)
- bits |= extractBitAndShift(imm, 4, 6)
- bits |= extractBitAndShift(imm, 3, 5)
- bits |= extractBitAndShift(imm, 7, 4)
- bits |= extractBitAndShift(imm, 6, 3)
- bits |= extractBitAndShift(imm, 2, 2)
- bits |= extractBitAndShift(imm, 1, 1)
- bits |= extractBitAndShift(imm, 5, 0)
+ bits := encodeBitPattern(imm, []int{8, 4, 3, 7, 6, 2, 1, 5})
return (bits>>5)<<10 | (bits&0x1f)<<2
}

// encodeCJImmediate encodes an immediate for a CJ-type RISC-V instruction.
func encodeCJImmediate(imm uint32) uint32 {
// Bit order - [11|4|9:8|10|6|7|3:1|5]
- bits := extractBitAndShift(imm, 11, 10)
- bits |= extractBitAndShift(imm, 4, 9)
- bits |= extractBitAndShift(imm, 9, 8)
- bits |= extractBitAndShift(imm, 8, 7)
- bits |= extractBitAndShift(imm, 10, 6)
- bits |= extractBitAndShift(imm, 6, 5)
- bits |= extractBitAndShift(imm, 7, 4)
- bits |= extractBitAndShift(imm, 3, 3)
- bits |= extractBitAndShift(imm, 2, 2)
- bits |= extractBitAndShift(imm, 1, 1)
- bits |= extractBitAndShift(imm, 5, 0)
+ bits := encodeBitPattern(imm, []int{11, 4, 9, 8, 10, 6, 7, 3, 2, 1, 5})
return bits << 2
}

Change information

Files:
  • M src/cmd/internal/obj/riscv/obj.go
Change size: S
Delta: 1 file changed, 10 insertions(+), 23 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Ryan
  • Meng Zhuo
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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 1
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joel Sing (Gerrit)

unread,
Sep 25, 2025, 12:18:09 PMSep 25
to goph...@pubsubhelper.golang.org, Meng Zhuo, Mark Ryan, golang-co...@googlegroups.com
Attention needed from Mark Ryan and Meng Zhuo

Joel Sing voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Ryan
  • Meng Zhuo
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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 1
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Comment-Date: Thu, 25 Sep 2025 16:18:02 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Joel Sing (Gerrit)

unread,
Oct 20, 2025, 12:43:01 AMOct 20
to goph...@pubsubhelper.golang.org, Go LUCI, Meng Zhuo, Mark Ryan, golang-co...@googlegroups.com
Attention needed from Mark Ryan and Meng Zhuo

Joel Sing voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Ryan
  • Meng Zhuo
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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 2
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Comment-Date: Mon, 20 Oct 2025 04:42:55 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Joel Sing (Gerrit)

unread,
Oct 20, 2025, 1:08:14 AMOct 20
to goph...@pubsubhelper.golang.org, Go LUCI, Meng Zhuo, Mark Ryan, golang-co...@googlegroups.com
Attention needed from Joel Sing, Mark Ryan and Meng Zhuo

Joel Sing removed a vote from this change

Removed LUCI-TryBot-Result-1 by Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Mark Ryan
  • Meng Zhuo
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: deleteVote
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 2
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Joel Sing <jo...@sing.id.au>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joel Sing (Gerrit)

unread,
Oct 20, 2025, 1:08:24 AMOct 20
to goph...@pubsubhelper.golang.org, Go LUCI, Meng Zhuo, Mark Ryan, golang-co...@googlegroups.com
Attention needed from Mark Ryan and Meng Zhuo

Joel Sing voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Ryan
  • Meng Zhuo
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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 2
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Comment-Date: Mon, 20 Oct 2025 05:08:17 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Cherry Mui (Gerrit)

unread,
Oct 23, 2025, 9:03:53 AMOct 23
to Joel Sing, goph...@pubsubhelper.golang.org, Go LUCI, Meng Zhuo, Mark Ryan, golang-co...@googlegroups.com
Attention needed from Joel Sing, Mark Ryan and Meng Zhuo

Cherry Mui voted and added 2 comments

Votes added by Cherry Mui

Code-Review+2

2 comments

File src/cmd/internal/obj/riscv/obj.go
Line 1426, Patchset 2 (Latest):// bit pattern from the given immediate.
Cherry Mui . unresolved

Probably explain what a bit pattern is in this context.

Line 1427, Patchset 2 (Latest):func encodeBitPattern(imm uint32, pattern []int) uint32 {
Cherry Mui . unresolved

Minor: Does it read better on the caller side if we make this function variadic?

Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Mark Ryan
  • Meng Zhuo
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement 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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 2
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Joel Sing <jo...@sing.id.au>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Mark Ryan <mark...@rivosinc.com>
Gerrit-Comment-Date: Thu, 23 Oct 2025 13:03:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Mark Ryan (Gerrit)

unread,
Nov 6, 2025, 3:42:11 AM (5 days ago) Nov 6
to Joel Sing, goph...@pubsubhelper.golang.org, Cherry Mui, Go LUCI, Meng Zhuo, golang-co...@googlegroups.com
Attention needed from Joel Sing and Meng Zhuo

Mark Ryan voted and added 1 comment

Votes added by Mark Ryan

Code-Review+1

1 comment

Patchset-level comments
Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Meng Zhuo
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement 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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
Gerrit-Change-Number: 702397
Gerrit-PatchSet: 2
Gerrit-Owner: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Attention: Joel Sing <jo...@sing.id.au>
Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
Gerrit-Comment-Date: Thu, 06 Nov 2025 08:42:06 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Joel Sing (Gerrit)

unread,
Nov 8, 2025, 7:59:49 AM (3 days ago) Nov 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Joel Sing and Meng Zhuo

Joel Sing uploaded new patchset

Joel Sing uploaded patch set #3 to this change.
Following approvals got outdated and were removed:
  • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit

Related details

Attention is currently required from:
  • Joel Sing
  • Meng Zhuo
Submit Requirements:
    • requirement 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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
    Gerrit-Change-Number: 702397
    Gerrit-PatchSet: 3
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Joel Sing (Gerrit)

    unread,
    Nov 8, 2025, 8:03:54 AM (3 days ago) Nov 8
    to goph...@pubsubhelper.golang.org, Mark Ryan, Cherry Mui, Go LUCI, Meng Zhuo, golang-co...@googlegroups.com
    Attention needed from Meng Zhuo

    Joel Sing voted and added 2 comments

    Votes added by Joel Sing

    Commit-Queue+1

    2 comments

    File src/cmd/internal/obj/riscv/obj.go
    Line 1426, Patchset 2:// bit pattern from the given immediate.
    Cherry Mui . resolved

    Probably explain what a bit pattern is in this context.

    Joel Sing

    I've elaborated, hopefully this helps to make it clearer.

    Line 1427, Patchset 2:func encodeBitPattern(imm uint32, pattern []int) uint32 {
    Cherry Mui . resolved

    Minor: Does it read better on the caller side if we make this function variadic?

    Joel Sing

    I considered that, but avoided it since it seemed less clear.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Meng Zhuo
    Submit Requirements:
    • requirement 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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
    Gerrit-Change-Number: 702397
    Gerrit-PatchSet: 3
    Gerrit-Owner: Joel Sing <jo...@sing.id.au>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
    Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
    Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
    Gerrit-Attention: Meng Zhuo <mengzh...@gmail.com>
    Gerrit-Comment-Date: Sat, 08 Nov 2025 13:03:45 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Cherry Mui <cher...@google.com>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Meng Zhuo (Gerrit)

    unread,
    Nov 10, 2025, 2:24:51 AM (yesterday) Nov 10
    to Joel Sing, goph...@pubsubhelper.golang.org, Meng Zhuo, Go LUCI, Mark Ryan, Cherry Mui, golang-co...@googlegroups.com
    Attention needed from Joel Sing

    Meng Zhuo voted and added 1 comment

    Votes added by Meng Zhuo

    Code-Review+2

    1 comment

    Patchset-level comments
    File-level comment, Patchset 3 (Latest):
    Meng Zhuo . resolved

    LGTM, might be useful for CL 717580

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Joel Sing
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement 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: I3d51caa10ecf714f2ad2fb66d38376202c4e0628
      Gerrit-Change-Number: 702397
      Gerrit-PatchSet: 3
      Gerrit-Owner: Joel Sing <jo...@sing.id.au>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Joel Sing <jo...@sing.id.au>
      Gerrit-Reviewer: Mark Ryan <mark...@rivosinc.com>
      Gerrit-Reviewer: Meng Zhuo <mengzh...@gmail.com>
      Gerrit-Attention: Joel Sing <jo...@sing.id.au>
      Gerrit-Comment-Date: Mon, 10 Nov 2025 07:24:46 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages