Skip to content

Add riscv64 CPU feature detection#173

Merged
klauspost merged 1 commit into
klauspost:masterfrom
mengzhuo:riscv64
Jun 16, 2026
Merged

Add riscv64 CPU feature detection#173
klauspost merged 1 commit into
klauspost:masterfrom
mengzhuo:riscv64

Conversation

@mengzhuo

@mengzhuo mengzhuo commented May 30, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

Release Notes

  • New Features
    • Added RISC-V (RV64) CPU detection with vendor/model identification and broader RISC-V feature coverage, including crypto and vector-related features.
    • Added a RISC-V profile value in both standard output and JSON output, plus corresponding CLI display.
    • Added cache line size detection on supported Linux systems.
  • Documentation
    • Updated README with RV64 support details, hwprobe-based detection, /proc/cpuinfo fallback, and an expanded RISC-V features table.
  • Tests
    • Added unit tests for ISA string parsing and the new feature-set helper.

@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
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 Outdated
Comment thread riscv_isa.go Outdated
@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 and feature ID constants, ISA string parsing, Linux hwprobe syscall detection with /proc/cpuinfo fallback, non-Linux minimal detection, architecture-specific dispatchers, generated string methods, and tests 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. Extends FeatureID enum with base ISA flags (RV_IMA, RV_C, RV_F, RV_D, RV_V) and Z-extensions plus crypto suites. Adds exported HasOneOf method to test feature set membership. Adds exported RVProfile method to compute RISC-V application profile level. Defines internal feature-set aggregates for profile and crypto detection.
RISC-V ISA string parsing
riscv_isa.go
parseISAString parses and normalizes RISC-V ISA extension tokens, expands base extensions, and sets corresponding feature flags on CPUInfo. riscvVendorMap and riscvVendorID provide vendor ID mapping for known RISC-V vendors.
Linux RISC-V detection via hwprobe
os_linux_riscv64.go
Defines hwprobe syscall number, key constants, and bitmask mappings for IMA and extension features. riscvHWProbe wraps the syscall. detectOS orchestrates hwprobe probing with cache line sizing and falls back to /proc/cpuinfo and sysfs when hwprobe unavailable. detectFromHWProbe maps hwprobe bits to feature flags. detectFromCPUInfo parses isa/mvendorid/uarch fields. detectCacheLine reads sysfs coherency_line_size.
Non-Linux RISC-V detection
os_other_riscv64.go
detectOS initializes core/thread counts from runtime.NumCPU and returns false.
Architecture detection dispatcher
detect_riscv64.go, detect_ref.go
detect_riscv64.go provides stub entry points: getVectorLength returns (0,0), initCPU assigns no-op function pointers. detect_ref.go build constraint updated to exclude riscv64.
Generated String() methods
featureid_string.go
Regenerated stringer mappings for new FeatureID and Vendor constants with updated name strings and index offsets.
Tests and documentation
cpuid_test.go, README.md, cmd/cpuid/main.go
TestHasOneOf validates HasOneOf behavior. TestParseISAString validates ISA parsing with table-driven test cases. README documents riscv64 platform support, hwprobe detection, /proc/cpuinfo fallback, cache line sizing, and supported feature flags table. cmd/cpuid/main.go package comment and output updated to display RISC-V profile level in both JSON and text modes.
🚥 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 accurately summarizes the main change—adding RISC-V 64-bit CPU feature detection support across multiple files and platform implementations.
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
Contributor 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 Outdated
Comment thread detect_riscv64.go
Comment thread README.md Outdated
@klauspost

Copy link
Copy Markdown
Owner

@mengzhuo PTAL at the remaining comments.

@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: 1

🤖 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 `@riscv_isa.go`:
- Around line 73-77: The combined crypto-suite flags (RV_ZKN, RV_ZKS, RV_ZVKNG,
RV_ZVKSG) are only being set based on whether their constituent individual
features are present, but they should also be set when the bundle token itself
is directly included in the ISA string. Modify each of the four setIf calls to
check an OR condition: in addition to checking hasSetP for the feature set
(rvZKNFeatures, rvZKSFeatures, rvZVKNFeatures, rvZVKSFeatures), also check if
the corresponding bundle token constant (RV_ZKN, RV_ZKS, RV_ZVKNG, RV_ZVKSG) is
already set in the featureSet, so that the combined flag is set whether it comes
from individual features or is specified directly as a bundle token.
🪄 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: 2f7bdced-3cba-4fd3-8e6f-56160a8eecac

📥 Commits

Reviewing files that changed from the base of the PR and between 84d56fd and 27b9438.

📒 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
✅ Files skipped from review due to trivial changes (1)
  • featureid_string.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • detect_ref.go
  • cpuid_test.go
  • os_other_riscv64.go
  • cpuid.go
  • detect_riscv64.go

Comment thread riscv_isa.go Outdated

@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: 1

🤖 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 `@riscv_isa.go`:
- Around line 10-19: The code stores raw ISA tokens in extMap without
normalizing versioned extensions like "zba1p0", causing exact feature lookups to
fail. After trimming whitespace from each extension token (following the `ext =
strings.TrimSpace(ext)` line), normalize the extension by stripping trailing
version information (numeric characters and version markers) so that "zba1p0"
becomes "zba" before storing in extMap. Apply this normalization consistently to
both the single-character extensions extracted from rv64 tokens in the for loop
over ext[4:] and to the regular extension tokens handled in the else branch.
🪄 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: 215ad2ac-4e36-4dd8-a820-b921ac2d794a

📥 Commits

Reviewing files that changed from the base of the PR and between 27b9438 and 40ea899.

📒 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
✅ Files skipped from review due to trivial changes (1)
  • featureid_string.go
🚧 Files skipped from review as they are similar to previous changes (7)
  • cmd/cpuid/main.go
  • os_other_riscv64.go
  • detect_ref.go
  • detect_riscv64.go
  • cpuid_test.go
  • os_linux_riscv64.go
  • cpuid.go

Comment thread riscv_isa.go
@klauspost klauspost merged commit caccf64 into klauspost:master Jun 16, 2026
14 checks passed
@klauspost

Copy link
Copy Markdown
Owner

@mengzhuo Thanks for the contribution!

@klauspost klauspost mentioned this pull request Jun 16, 2026
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