-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Add the functionality for custom RISC-V intrinsics, support hints in RISC-V #726
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
59a7bd3
Add custom insn macros
Golovanov399 ac5ad94
Wip
Golovanov399 c961df2
Wip
Golovanov399 fed98b8
Add "read-vec" function
Golovanov399 8fb16f0
Add the new HintInputRv32 phantom instruction, add the corresponding …
Golovanov399 57f2ab9
Merge branch 'main' into feat/custom-insn-and-hints
Golovanov399 f6e132c
Add a test
Golovanov399 4582601
Optimize reading a vector
Golovanov399 a3785d0
Address comments, add a test, fix transpiler
Golovanov399 2b44823
Fix rust-v hint program
Golovanov399 3165d5f
chore: mv exit,panic to process
jonathanpwang 895ace5
feat: make `read_vec` more optimal
jonathanpwang 963dee7
fix: () vs !
jonathanpwang a47f3c9
fix import
jonathanpwang 869aa21
fix: ptr was not ptr_start
jonathanpwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// use crate::custom_insn_i; | ||
|
||
use axvm_platform::{custom_insn_i, intrinsics::CUSTOM_0}; | ||
|
||
/// Store the next 4 bytes from the hint stream to [[rd] + imm]_2. | ||
#[macro_export] | ||
macro_rules! hint_store_u32 { | ||
($x:ident, $imm:expr) => { | ||
axvm_platform::custom_insn_i!(axvm_platform::intrinsics::CUSTOM_0, 0b001, $x, "x0", $imm) | ||
}; | ||
} | ||
|
||
/// Reset the hint stream with the next hint. | ||
#[inline(always)] | ||
pub fn hint_input() { | ||
custom_insn_i!(CUSTOM_0, 0b011, "x0", "x0", 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
//! Functions that call custom instructions that use axVM intrinsic instructions. | ||
|
||
mod hash; | ||
/// Library functions for user input/output. | ||
pub mod io; | ||
|
||
pub use hash::*; | ||
pub use io::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! User IO functions | ||
|
||
use alloc::vec::Vec; | ||
use core::alloc::Layout; | ||
|
||
use crate::{hint_store_u32, intrinsics::hint_input}; | ||
|
||
/// Read `size: u32` and then `size` bytes from the hint stream into a vector. | ||
pub fn read_vec() -> Vec<u8> { | ||
hint_input(); | ||
read_vec_by_len(read_u32() as usize) | ||
} | ||
|
||
/// Read the next 4 bytes from the hint stream into a register. | ||
/// Because [hint_store_u32] stores a word to memory, this function first reads to memory and then | ||
/// loads from memory to register. | ||
#[inline(always)] | ||
#[allow(asm_sub_register)] | ||
pub fn read_u32() -> u32 { | ||
let ptr = unsafe { alloc::alloc::alloc(Layout::from_size_align(4, 4).unwrap()) }; | ||
let addr = ptr as u32; | ||
hint_store_u32!(addr, 0); | ||
let result: u32; | ||
unsafe { | ||
core::arch::asm!("lw {rd}, ({rs1})", rd = out(reg) result, rs1 = in(reg) addr); | ||
} | ||
result | ||
} | ||
|
||
/// Read the next `len` bytes from the hint stream into a vector. | ||
fn read_vec_by_len(len: usize) -> Vec<u8> { | ||
let num_words = (len + 3) / 4; | ||
let capacity = num_words * 4; | ||
// Allocate a buffer of the required length that is 4 byte aligned | ||
// Note: this expect message doesn't matter until our panic handler actually cares about it | ||
let layout = Layout::from_size_align(capacity, 4).expect("vec is too large"); | ||
// SAFETY: We populate a `Vec<u8>` by hintstore-ing `num_words` 4 byte words. We set the length to `len` and don't care about the extra `capacity - len` bytes stored. | ||
let ptr_start = unsafe { alloc::alloc::alloc(layout) }; | ||
let mut ptr = ptr_start; | ||
|
||
// Note: if len % 4 != 0, this will discard some last bytes | ||
for _ in 0..num_words { | ||
hint_store_u32!(ptr, 0); | ||
ptr = unsafe { ptr.add(4) }; | ||
} | ||
unsafe { Vec::from_raw_parts(ptr_start, len, capacity) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//! System exit and panic functions. | ||
|
||
/// Exit the program with exit code 0. | ||
pub fn exit() { | ||
axvm_platform::rust_rt::terminate::<0>(); | ||
} | ||
|
||
/// Exit the program with exit code 1. | ||
pub fn panic() { | ||
axvm_platform::rust_rt::terminate::<1>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[build] | ||
target = "riscv32im-risc0-zkvm-elf" | ||
|
||
[unstable] | ||
build-std = ["core", "alloc", "proc_macro", "panic_abort"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[workspace] | ||
[package] | ||
version = "0.1.0" | ||
name = "axvm-hint-program" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
axvm = { path = "../../../axvm" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![no_main] | ||
#![no_std] | ||
use axvm::io::read_vec; | ||
|
||
axvm::entry!(main); | ||
|
||
pub fn main() { | ||
let vec = read_vec(); | ||
assert_eq!(vec.len(), 4); | ||
for i in 0..4 { | ||
assert_eq!(vec[i], i as u8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
pub const CUSTOM_0: u8 = 0x0b; | ||
pub const CUSTOM_1: u8 = 0x2b; | ||
|
||
#[macro_export] | ||
macro_rules! custom_insn_i { | ||
($opcode:expr, $funct3:expr, $rd:literal, $rs1:literal, $imm:expr) => { | ||
unsafe { | ||
core::arch::asm!(concat!( | ||
".insn i {opcode}, {funct3}, ", | ||
$rd, | ||
", ", | ||
$rs1, | ||
", {imm}", | ||
), opcode = const $opcode, funct3 = const $funct3, imm = const $imm) | ||
} | ||
}; | ||
($opcode:expr, $funct3:expr, $x:ident, $rs1:literal, $imm:expr) => { | ||
jonathanpwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { | ||
core::arch::asm!(concat!( | ||
".insn i {opcode}, {funct3}, {rd}, ", | ||
$rs1, | ||
", {imm}", | ||
), opcode = const $opcode, funct3 = const $funct3, rd = in(reg) $x, imm = const $imm) | ||
} | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! custom_insn_r { | ||
($opcode:expr, $funct3:expr, $rd:literal, $rs1:literal, $rs2:literal) => { | ||
unsafe { | ||
core::arch::asm!(concat!( | ||
".insn r {opcode}, {funct3}, ", | ||
$rd, | ||
", ", | ||
$rs1, | ||
", ", | ||
$rs2, | ||
), opcode = const $opcode, funct3 = const $funct3) | ||
} | ||
}; | ||
($opcode:expr, $funct3:expr, $x:ident, $rs1:literal, $rs2:literal) => { | ||
unsafe { | ||
core::arch::asm!(concat!( | ||
".insn r {opcode}, {funct3}, {rd}, ", | ||
$rs1, | ||
", ", | ||
$rs2, | ||
), opcode = const $opcode, funct3 = const $funct3, rd = out(reg) $x) | ||
} | ||
}; | ||
// TODO: implement more variants with like rs1 = in(reg) $y etc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a nightly feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
imm = const $imm
inasm!
macro doesn't work without this. I don't know if it's nightly or not, here is its origin: rust-lang/rust#93332