Skip to content

Add RISC-V 64-bit architecture support - #125

Merged
lalinsky merged 3 commits into
mainfrom
add-riscv64-support
Nov 4, 2025
Merged

lalinsky merged 3 commits into
mainfrom
add-riscv64-support

Conversation

@lalinsky

@lalinsky lalinsky commented Nov 4, 2025

Copy link
Copy Markdown
Owner

Summary

  • Adds complete RISC-V 64-bit support to the coroutines implementation
  • Adds CI testing for RISC-V builds using QEMU emulation

Implementation Details

This PR extends the existing coroutine system to support RISC-V 64-bit architecture, following the same patterns as the existing x86_64 and aarch64 implementations:

Context Structure (src/coroutines.zig:39-44)

  • Added RISC-V context with sp, fp, and pc registers
  • Includes ExtraContext field for future Windows TIB support (if needed)

Context Initialization (src/coroutines.zig:64-69)

  • Implemented initContext() for RISC-V to properly set up initial stack pointer, frame pointer, and program counter

Context Switching (src/coroutines.zig:312-391)

  • Full assembly implementation that saves/restores all registers:
    • All general-purpose registers (x0-x31)
    • All floating-point registers (f0-f31)
    • Memory clobber for proper compiler awareness

Entry Point (src/coroutines.zig:437-442)

  • Implemented coroEntry() for RISC-V with correct stack layout handling
  • Properly loads context pointer from offset 8 and function pointer from offset 0
  • Sets return address to 0 for safety (matches aarch64 behavior)

CI Updates (.github/workflows/test.yml:21-24)

  • Added RISC-V build/test target using qemu-riscv64-static
  • Uses epoll backend (same as aarch64)
  • Runs coroutine tests under emulation

Test Plan

  • CI will build and test RISC-V target using QEMU
  • Coroutine tests will verify context switching works correctly
  • All existing tests continue to pass on other architectures

This adds complete RISC-V 64-bit support to the coroutines implementation:

- Context structure with sp, fp, and pc registers
- Context initialization in initContext()
- Full context switching with save/restore of all registers
- Entry point (coroEntry) with correct stack layout handling
- CI workflow updates to test RISC-V builds using QEMU

The implementation follows the same patterns as existing x86_64 and
aarch64 architectures, with proper register preservation and calling
convention adherence.
@coderabbitai

coderabbitai Bot commented Nov 4, 2025 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@lalinsky has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 23 minutes and 36 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3311c43 and 854ba1d.

📒 Files selected for processing (1)
  • src/coroutines.zig (7 hunks)

Walkthrough

Adds RISC-V 64 architecture support to the project by extending the CI matrix to test on riscv64-linux via QEMU and implementing the necessary coroutine context management (Context type, initContext, switchContext, coroEntry) for the architecture.

Changes

Cohort / File(s) Summary
CI Configuration
\.github/workflows/test.yml``
Adds riscv64-linux to the test matrix with QEMU binary qemu-riscv64-static and epoll backend
Coroutine Context Management
\src/coroutines.zig``
Extends Context union with riscv64 extern struct variant (sp, fp, pc, extra fields); updates initContext, switchContext, and coroEntry functions with riscv64-specific implementations including inline assembly for register save/restore and context switching
Coroutine Tests
\src/coroutines.zig``
Adds "Coroutine: message passing" test with Channel type and round-robin scheduler to validate inter-coroutine communication

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • RISC-V assembly correctness: The inline asm volatile blocks for switchContext and coroEntry need careful verification against RISC-V ABI conventions for register preservation and calling conventions
  • Context struct alignment: Verify the riscv64 extern struct layout matches actual register sizes and ABI expectations
  • Test coverage: The new message-passing test introduces Channel semantics that should be verified for correctness in concurrent scenarios

Possibly related PRs

Poem

🏗️ Five bytes of instruction set,
RISC-V marches where x86 went—
Context flips through riscv64's asm,
While QEMU tests what silicon can't,
Message-passing 'round the scheduler's wheel.

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly matches the main objective: adding RISC-V 64-bit architecture support. It's concise, specific, and accurately reflects the primary change across both the coroutines implementation and CI updates.
Description check ✅ Passed The description comprehensively covers the changeset with specific line numbers, implementation details for each component (context structure, initialization, switching, entry point), and CI updates. It clearly relates to the actual changes in both src/coroutines.zig and .github/workflows/test.yml.

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.

Adds a comprehensive test demonstrating message passing between two
coroutines using a simple channel implementation:

- Single-slot channels with yield-based blocking
- Sender sends messages 0-9, verifies echoes, then sends EOF (-1)
- Receiver echoes messages and returns the last value received
- Round-robin scheduler coordinates both coroutines
- Validates proper context switching and synchronization
The Closure helper now automatically prepends the coroutine pointer as
the first argument when calling the wrapped function. This simplifies
the API:

Before:
  var closure = Closure(fn).init(.{ &coro, arg1, arg2 });

After:
  var closure = Closure(fn).init(.{ arg1, arg2 });

The coroutine is still passed to the function, but users don't need to
manually provide it in the init tuple. This makes the API cleaner and
less error-prone.
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.

1 participant