Skip to content

Add riscv64 CPU feature detection#173

Open
mengzhuo wants to merge 1 commit into
klauspost:masterfrom
mengzhuo:riscv64
Open

Add riscv64 CPU feature detection#173
mengzhuo wants to merge 1 commit into
klauspost:masterfrom
mengzhuo:riscv64

Conversation

@mengzhuo

@mengzhuo mengzhuo commented May 30, 2026

Copy link
Copy Markdown

Summary by CodeRabbit

Release Notes

  • New Features

    • Added RISC-V (RV64) CPU architecture support with feature detection
    • Added RISC-V vendor identification (SiFive, StarFive, THead, Andes, SpacemiT)
    • Added 22 RISC-V feature constants for extension detection
  • Documentation

    • Updated README with RISC-V support details and feature compatibility table
  • Tests

    • Added ISA string parsing validation test

@klauspost klauspost left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very clean. No objections.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial riscv64 CPU feature detection support to the cpuid package, integrating Linux hwprobe-based detection (with /proc/cpuinfo fallback) and extending the public feature/vendor enums and docs to include RISC-V.

Changes:

  • Add riscv64 OS/CPU detection (riscv_hwprobe syscall on Linux; /proc/cpuinfo fallback).
  • Extend FeatureID and Vendor enums (and generated string mappings) with RISC-V features/vendors.
  • Update documentation and CLI package comments to list riscv64 as supported.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
README.md Documents riscv64 detection approach and lists new RISC-V feature flags.
os_other_riscv64.go Adds non-Linux riscv64 OS detection stub (core counts only).
os_linux_riscv64.go Implements Linux riscv64 feature/vendor detection via riscv_hwprobe and /proc/cpuinfo parsing.
featureid_string.go Updates generated stringer output for new FeatureID/Vendor constants.
detect_riscv64.go Adds riscv64 entry point wiring into the library’s detection pipeline.
detect_ref.go Updates fallback build constraints to exclude riscv64.
cpuid.go Adds RISC-V vendors/features and updates supported-arch comment; extends vendor mapping.
cmd/cpuid/main.go Updates supported-arch comment for the CLI wrapper.
Files not reviewed (1)
  • featureid_string.go: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread os_linux_riscv64.go Outdated
Comment thread os_linux_riscv64.go Outdated
Comment thread os_linux_riscv64.go Outdated
Comment thread os_linux_riscv64.go Outdated
Comment thread detect_riscv64.go
Comment thread cpuid.go Outdated
Comment thread os_linux_riscv64.go Outdated
@klauspost

Copy link
Copy Markdown
Owner

I updated CI and a few other minor things. Looks like there is a minor conflict.

Please take a look at that, and the AI feedback, which looks legit to me.

@mengzhuo mengzhuo force-pushed the riscv64 branch 2 times, most recently from 43bab01 to 17841f8 Compare June 2, 2026 15:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • featureid_string.go: Language not supported

Comment thread detect_riscv64.go Outdated
Comment thread os_linux_riscv64.go
Comment thread riscv_isa.go
c.featureSet.setIf(extMap["ztso"], RV_ZTSO)
c.featureSet.setIf(extMap["zacas"], RV_ZACAS)

hasCrypto := extMap["zknd"] || extMap["zkne"] || extMap["zknh"] ||

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering - aren't these also useful individually? I don't know about the arch, but I imagine each refers to specific features and not the same, or?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... RISC-V has a lots of feature sets, I choice some instead of All ratified extensions.

For example, ZACAS you can do Compare-And-Swap without branching.
https://github.com/zte-riscv/go/pull/68/changes

Right now golang.org/x/sys only add vector/bit related.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can only add minimum extension sets for rva20u64 profile if you want

@klauspost klauspost Jun 3, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I would prefer the detection to be as fine-grained as possible, meaning keeping RV_ZKND, etc as a single feature.

You are welcome to add this:

var RV_CRYPTO_FEATS = CombineFeatures(RV_ZKND, ...)

// HasOneOf returns whether the CPU supports one or more of the requested features.
func (c *CPUInfo) HasOneOf(f Features) bool {
	return c.featureSet.hasOneOf(f)
}

...so callers can do cpuid.CPU.HasOneOf(cpuid.RV_CRYPTO_FEATS)). But I am not sure I understand the value of having one, but not all of these features. Did you mean to AND them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, zvk (crypto) is not mandatory all AES/SHA (NIST suit, N) SM (Chinese suits, S) so that's possible to have only N or S.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but we need to be able to detect them individually, not just "some of these may be available".

@klauspost

Copy link
Copy Markdown
Owner

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds complete RISC-V 64-bit (riscv64) support to the cpuid package, including vendor constants, ISA feature detection via hwprobe syscall or /proc/cpuinfo parsing, architecture-specific dispatchers, and documentation.

Changes

RISC-V Feature Detection

Layer / File(s) Summary
Public RISC-V contracts
cpuid.go
Extends Vendor enum with SiFive, StarFive, THead, Andes, SpacemiT and FeatureID enum with base ISA flags (IMA, C, F, D, V) and Z-extensions plus crypto aggregates.
RISC-V ISA string parsing
riscv_isa.go
parseISAString parses and normalizes RISC-V ISA extension tokens, sets corresponding feature flags on CPUInfo, and provides vendor ID mapping via riscvVendorMap and riscvVendorID.
Linux RISC-V detection via hwprobe
os_linux_riscv64.go
Implements hwprobe syscall detection for vendor, model, family, and extension features with cache line sizing. Falls back to /proc/cpuinfo parsing when hwprobe unavailable and derives cache size from sysfs.
Non-Linux RISC-V detection
os_other_riscv64.go
Sets logical/physical core counts from runtime.NumCPU for non-Linux platforms.
Architecture detection dispatcher
detect_riscv64.go, detect_ref.go
detect_riscv64.go provides stub entry points for vector detection and CPU initialization; detect_ref.go build constraint updated to exclude riscv64.
Generated String() methods
featureid_string.go
Regenerated stringer mappings for new FeatureID and Vendor constants.
Tests and documentation
cpuid_test.go, README.md, cmd/cpuid/main.go
TestParseISAString validates ISA parsing. README documents hwprobe detection, /proc/cpuinfo fallback, cache line sizing, and supported feature flags. Package comments updated to reflect riscv64 support.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add riscv64 CPU feature detection' directly and accurately summarizes the main objective of the changeset: introducing CPU feature detection support for the riscv64 architecture.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mengzhuo

mengzhuo commented Jun 8, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

Thanks for reviewing, I'm will commit new code based on your review ASAP.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@mengzhuo Sounds good. Once you push the updates, feel free to tag me for another look.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
os_linux_riscv64.go (1)

138-143: 💤 Low value

Consider clarifying why RV_IMA is set unconditionally.

Line 143 sets RV_IMA unconditionally without checking hwprobe base_behavior. While this assumption is correct—Linux/riscv64 requires IMA support to boot—a brief comment would make the rationale explicit.

Optional clarifying comment
 	c.featureSet.setIf(imaExt&uint64(cryptoMask) != 0, RV_CRYPTO)
 
+	// RV64IMA is mandatory for Linux; all riscv64 systems support it
 	c.featureSet.set(RV_IMA)
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@os_linux_riscv64.go` around lines 138 - 143, Add a short clarifying comment
before the unconditional c.featureSet.set(RV_IMA) explaining that Linux/riscv64
requires IMA support to boot, which is why RV_IMA is set regardless of
hwprobe/base_behavior; reference the surrounding logic that computes imaExt &
cryptoMask and sets RV_CRYPTO so readers know RV_IMA is intentionally
unconditional (symbols: RV_IMA, c.featureSet.set, imaExt, cryptoMask,
RV_CRYPTO).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cpuid.go`:
- Line 348: The RV_CRYPTO comment currently lists only five extensions
(Zknd/Zkne/Zknh/Zksed/Zksh) but the detection logic in riscv_isa.go for
RV_CRYPTO checks eight extensions; update the RV_CRYPTO comment to enumerate all
extensions that the code detects (add Zbkb, Zbkc, Zbkx) or adjust the detection
logic to match the documented list, referencing the RV_CRYPTO constant and the
detection code in riscv_isa.go so the comment and implementation stay
consistent.

In `@detect_riscv64.go`:
- Around line 1-3: This file lacks the riscv64 build constraint and is being
compiled on non-riscv64 targets causing duplicate symbols; add a build tag
header above the package declaration exactly like other detect_* files by
inserting the modern and legacy build constraints for riscv64 (e.g., "//go:build
riscv64" and the corresponding "// +build riscv64") before "package cpuid" so
the file (detect_riscv64.go) is only built on riscv64 targets.

In `@README.md`:
- Line 542: Remove the undocumented RV_RNG table row from the README: locate the
table entry that lists "RV_RNG" (the undocumented feature ID) and delete that
table row so the README matches the implemented feature IDs in
featureid_string.go (e.g., RV_IMA, RV_C, RV_F, RV_D, RV_V, RV_ZBA–RV_ZBS,
RV_ZICOND, RV_ZIHINTPAUSE, RV_ZICBOM, RV_ZICBOZ, RV_ZICBOP, RV_ZFA, RV_ZFH,
RV_ZFHMIN, RV_ZTSO, RV_ZACAS, RV_CRYPTO, RV_VECTOR_CRYPTO); ensure no other
references to RV_RNG remain in the documentation.

---

Nitpick comments:
In `@os_linux_riscv64.go`:
- Around line 138-143: Add a short clarifying comment before the unconditional
c.featureSet.set(RV_IMA) explaining that Linux/riscv64 requires IMA support to
boot, which is why RV_IMA is set regardless of hwprobe/base_behavior; reference
the surrounding logic that computes imaExt & cryptoMask and sets RV_CRYPTO so
readers know RV_IMA is intentionally unconditional (symbols: RV_IMA,
c.featureSet.set, imaExt, cryptoMask, RV_CRYPTO).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6310fa7b-e27b-4a36-b809-edad53a22084

📥 Commits

Reviewing files that changed from the base of the PR and between 504c492 and 84d56fd.

📒 Files selected for processing (10)
  • README.md
  • cmd/cpuid/main.go
  • cpuid.go
  • cpuid_test.go
  • detect_ref.go
  • detect_riscv64.go
  • featureid_string.go
  • os_linux_riscv64.go
  • os_other_riscv64.go
  • riscv_isa.go

Comment thread cpuid.go
RV_ZFHMIN // Minimal half-precision FP
RV_ZTSO // Total store ordering
RV_ZACAS // Atomic CAS
RV_CRYPTO // Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

RV_CRYPTO comment is incomplete.

The comment lists five extensions (Zknd/Zkne/Zknh/Zksed/Zksh) but the actual detection logic in riscv_isa.go:44-46 checks eight extensions: the five listed plus Zbkb, Zbkc, and Zbkx.

📝 Proposed fix
-	RV_CRYPTO        // Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh)
+	RV_CRYPTO        // Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh/Zbkb/Zbkc/Zbkx)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RV_CRYPTO // Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh)
RV_CRYPTO // Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh/Zbkb/Zbkc/Zbkx)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cpuid.go` at line 348, The RV_CRYPTO comment currently lists only five
extensions (Zknd/Zkne/Zknh/Zksed/Zksh) but the detection logic in riscv_isa.go
for RV_CRYPTO checks eight extensions; update the RV_CRYPTO comment to enumerate
all extensions that the code detects (add Zbkb, Zbkc, Zbkx) or adjust the
detection logic to match the documented list, referencing the RV_CRYPTO constant
and the detection code in riscv_isa.go so the comment and implementation stay
consistent.

Comment thread detect_riscv64.go
Comment thread README.md
| RV_ZTSO | Total store ordering |
| RV_ZACAS | Atomic CAS |
| RV_CRYPTO | Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh/Zbkb/etc) |
| RV_RNG | Entropy source (Zkr) |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Remove undocumented RV_RNG feature.

The RV_RNG feature ID is not defined in the codebase. According to the featureid_string.go constants, the implemented RISC-V features are: RV_IMA, RV_C, RV_F, RV_D, RV_V, RV_ZBA–RV_ZBS, RV_ZICOND, RV_ZIHINTPAUSE, RV_ZICBOM, RV_ZICBOZ, RV_ZICBOP, RV_ZFA, RV_ZFH, RV_ZFHMIN, RV_ZTSO, RV_ZACAS, RV_CRYPTO, and RV_VECTOR_CRYPTO. RV_RNG is absent.

Fix: remove the RV_RNG row
 | RV_ZACAS         | Atomic CAS                                               |
 | RV_CRYPTO        | Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh/Zbkb/etc) |
-| RV_RNG           | Entropy source (Zkr)                                     |
 | RV_VECTOR_CRYPTO | Vector crypto (Zvbb/Zvbc/Zvkg/Zvkned/Zvknhb/etc)         |
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
| RV_RNG | Entropy source (Zkr) |
| RV_ZACAS | Atomic CAS |
| RV_CRYPTO | Scalar cryptography (Zknd/Zkne/Zknh/Zksed/Zksh/Zbkb/etc) |
| RV_VECTOR_CRYPTO | Vector crypto (Zvbb/Zvbc/Zvkg/Zvkned/Zvknhb/etc) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@README.md` at line 542, Remove the undocumented RV_RNG table row from the
README: locate the table entry that lists "RV_RNG" (the undocumented feature ID)
and delete that table row so the README matches the implemented feature IDs in
featureid_string.go (e.g., RV_IMA, RV_C, RV_F, RV_D, RV_V, RV_ZBA–RV_ZBS,
RV_ZICOND, RV_ZIHINTPAUSE, RV_ZICBOM, RV_ZICBOZ, RV_ZICBOP, RV_ZFA, RV_ZFH,
RV_ZFHMIN, RV_ZTSO, RV_ZACAS, RV_CRYPTO, RV_VECTOR_CRYPTO); ensure no other
references to RV_RNG remain in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants