Skip to content
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

Integrate ingress #99

Merged
merged 9 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions benches/fib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use p3_baby_bear::BabyBear;
use p3_field::AbstractField;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{mem::take, time::Duration};
use std::time::Duration;

use loam::{
lair::{
Expand All @@ -14,8 +14,8 @@ use loam::{
},
lurk::{
eval::build_lurk_toplevel,
memory::Memory,
zstore::{PoseidonBabyBearHasher, ZStore},
reader::{read, ReadData},
state::State,
},
};

Expand All @@ -39,43 +39,44 @@ fn build_lurk_expr(arg: usize) -> String {
)
}

fn setup<'a, H: Hasher<BabyBear>>(
fn setup<H: Hasher<BabyBear>>(
arg: usize,
mem: &mut Memory<BabyBear, PoseidonBabyBearHasher>,
store: &ZStore<BabyBear, PoseidonBabyBearHasher>,
toplevel: &'a Toplevel<BabyBear, H>,
toplevel: &Toplevel<BabyBear, H>,
) -> (
List<BabyBear>,
FuncChip<'a, BabyBear, H>,
FuncChip<'_, BabyBear, H>,
QueryRecord<BabyBear>,
) {
let mut queries = QueryRecord::new_with_init_mem(toplevel, take(&mut mem.map));
let code = build_lurk_expr(arg);
let ReadData {
tag: expr_tag,
digest: expr_digest,
hashes,
} = read(State::init_lurk_state().rccell(), &code).unwrap();

mem.map = take(&mut queries.mem_queries);
let expr = mem.read_and_ingress(&build_lurk_expr(arg), store).unwrap();
queries.mem_queries = take(&mut mem.map);
let mut queries = QueryRecord::new(toplevel);
queries.inject_queries("hash_32_8", toplevel, hashes);

let env = BabyBear::zero();
let mut full_input = [BabyBear::zero(); 24];
full_input[0] = expr_tag;
full_input[8..16].copy_from_slice(&expr_digest);

let args: List<_> = [expr.tag, expr.raw, env].into();
let args: List<_> = full_input.into();
let lurk_main = FuncChip::from_name("lurk_main", toplevel);

let eval = FuncChip::from_name("eval", toplevel);

(args, eval, queries)
(args, lurk_main, queries)
}

fn evaluation(c: &mut Criterion) {
let arg = get_fib_arg();
c.bench_function(&format!("evaluation-{arg}"), |b| {
let mem = &mut Memory::init();
let store = ZStore::<BabyBear, PoseidonBabyBearHasher>::new();
let toplevel = build_lurk_toplevel::<_, _, LurkHasher>(mem, &store);
let (args, eval, queries) = setup(arg, mem, &store, &toplevel);
let toplevel = build_lurk_toplevel::<LurkHasher>();
let (args, lurk_main, queries) = setup(arg, &toplevel);

b.iter_batched(
|| (args.clone(), queries.clone()),
|(args, mut queries)| {
eval.execute_iter(args, &mut queries);
lurk_main.execute_iter(args, &mut queries);
},
BatchSize::SmallInput,
)
Expand All @@ -85,12 +86,10 @@ fn evaluation(c: &mut Criterion) {
fn trace_generation(c: &mut Criterion) {
let arg = get_fib_arg();
c.bench_function(&format!("trace-generation-{arg}"), |b| {
let mem = &mut Memory::init();
let store = ZStore::<BabyBear, PoseidonBabyBearHasher>::new();
let toplevel = build_lurk_toplevel::<_, _, LurkHasher>(mem, &store);
let (args, eval, mut queries) = setup(arg, mem, &store, &toplevel);
let toplevel = build_lurk_toplevel::<LurkHasher>();
let (args, lurk_main, mut queries) = setup(arg, &toplevel);

eval.execute_iter(args, &mut queries);
lurk_main.execute_iter(args, &mut queries);

let func_chips = FuncChip::from_toplevel(&toplevel);

Expand Down
45 changes: 35 additions & 10 deletions src/lair/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,26 @@ macro_rules! block {
let $tgt = $crate::var!($tgt);
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident = Array($arr:expr); $($tail:tt)+ }, $ops:expr) => {{
let size = $arr.len();
$ops.push($crate::lair::expr::OpE::Array($crate::var!($tgt, size), $arr));
let $tgt = $crate::var!($tgt, size);
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident = [$($a:literal),*]; $($tail:tt)+ }, $ops:expr) => {{
let arr = vec!($($crate::lair::field_from_i32($a)),*);
let size = arr.len();
$ops.push($crate::lair::expr::OpE::Array($crate::var!($tgt, size), arr));
let $tgt = $crate::var!($tgt, size);
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident = [$f:literal; $size:literal]; $($tail:tt)+ }, $ops:expr) => {{
let arr = [$f; $size].into_iter().map($crate::lair::field_from_i32).collect::<Vec<_>>();
let size = arr.len();
$ops.push($crate::lair::expr::OpE::Array($crate::var!($tgt, size), arr));
let $tgt = $crate::var!($tgt, size);
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident = add($a:ident, $b:ident); $($tail:tt)+ }, $ops:expr) => {{
$ops.push($crate::lair::expr::OpE::Add($crate::var!($tgt), $a, $b));
let $tgt = $crate::var!($tgt);
Expand Down Expand Up @@ -103,6 +116,12 @@ macro_rules! block {
let $tgt = $crate::var!($tgt);
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident $(: [$size:expr])? = load($arg:ident); $($tail:tt)+ }, $ops:expr) => {{
let out = [$crate::var!($tgt $(, $size)?)].into();
$ops.push($crate::lair::expr::OpE::Load(out, $arg));
let $tgt = $crate::var!($tgt $(, $size)?);
$crate::block!({ $($tail)* }, $ops)
}};
({ let ($($tgt:ident $(: [$size:expr])?),*) = load($arg:ident); $($tail:tt)+ }, $ops:expr) => {{
let out = [$($crate::var!($tgt $(, $size)?)),*].into();
$ops.push($crate::lair::expr::OpE::Load(out, $arg));
Expand Down Expand Up @@ -181,9 +200,11 @@ macro_rules! block {
$(let $tgt = $crate::var!($tgt $(, $size)?);)*
$crate::block!({ $($tail)* }, $ops)
}};
({ let $tgt:ident = Sym($sym:literal, $mem:ident, $store:ident); $($tail:tt)+ }, $ops:expr) => {{
let sym = $mem.read_and_ingress($sym, $store).unwrap().raw;
$ops.push($crate::lair::expr::OpE::Const($crate::var!($tgt), sym));
({ let $tgt:ident = Builtin($sym:literal); $($tail:tt)+ }, $ops:expr) => {{
let idx = $crate::lurk::state::LURK_PACKAGE_NONNIL_SYMBOLS_NAMES
.iter()
.position(|&sym| sym == $sym).expect("Cannot find builtin") as u32;
$ops.push($crate::lair::expr::OpE::Const($crate::var!($tgt), $crate::lair::field_from_u32(idx)));
let $tgt = $crate::var!($tgt);
$crate::block!({ $($tail)* }, $ops)
}};
Expand Down Expand Up @@ -227,12 +248,12 @@ macro_rules! block {
{
$(
vec.push((
$crate::lurk::zstore::Tag::$tag.to_field(),
$crate::lurk::tag::Tag::$tag.to_field(),
$crate::block_init!( $branch )
));
$(
vec.push((
$crate::lurk::zstore::Tag::$other_tag.to_field(),
$crate::lurk::tag::Tag::$other_tag.to_field(),
$crate::block_init!( $branch ),
));
)*
Expand Down Expand Up @@ -292,20 +313,24 @@ macro_rules! block {
let ctrl = $crate::lair::expr::CtrlE::MatchMany($var, cases);
$crate::lair::expr::BlockE { ops, ctrl }
}};
({ match_sym($mem:ident, $store:ident) $var:ident { $( $sym:literal $(| $other_sym:literal)* => $branch:tt )* } $(; $($def:tt)*)? }, $ops:expr) => {{
({ match_builtin $var:ident { $( $sym:literal $(| $other_sym:literal)* => $branch:tt )* } $(; $($def:tt)*)? }, $ops:expr) => {{
let ops = $ops.into();
let mut vec = Vec::new();
{
$(
let sym = $mem.read_and_ingress($sym, $store).unwrap().raw;
let idx = $crate::lurk::state::LURK_PACKAGE_NONNIL_SYMBOLS_NAMES
.iter()
.position(|&sym| sym == $sym).expect("Cannot find builtin") as u32;
vec.push((
sym,
$crate::lair::field_from_u32(idx),
$crate::block_init!( $branch )
));
$(
let other_sym = $mem.read_and_ingress($other_sym, $store).unwrap().raw;
let other_idx = $crate::lurk::state::LURK_PACKAGE_NONNIL_SYMBOLS_NAMES
.iter()
.position(|&sym| sym == $other_sym).expect("Cannot find builtin") as u32;
vec.push((
other_sym,
$crate::lair::field_from_u32(other_idx),
$crate::block_init!( $branch ),
));
)*
Expand Down
7 changes: 1 addition & 6 deletions src/lair/toplevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,7 @@ impl<F: Clone + Ord> BlockE<F> {
.iter()
.flat_map(|a| use_var(a, ctx).to_vec())
.collect();
let mut i = 0;
for v in img.as_slice() {
let idxs = preimg[i..i + v.size].into();
bind(v, idxs, ctx);
i += v.size;
}
img.iter().for_each(|val| bind_new(val, ctx));
ops.push(Op::Hash(preimg));
}
}
Expand Down
Loading