Skip to content

Commit

Permalink
Use lower max degree by default and let user change it (#1948)
Browse files Browse the repository at this point in the history
Depends on #1946
  • Loading branch information
leonardoalt authored Oct 25, 2024
1 parent e7573dd commit 2113aa0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
50 changes: 45 additions & 5 deletions powdr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,37 @@ pub struct Session {
const DEFAULT_PKEY: &str = "pkey.bin";
const DEFAULT_VKEY: &str = "vkey.bin";

// Minimum and maximum log of number of rows for the RISCV machine.
const DEFAULT_MIN_DEGREE_LOG: u32 = 5;
const DEFAULT_MAX_DEGREE_LOG: u32 = 20;
// Minimum acceptable max degree.
const DEFAULT_MIN_MAX_DEGREE_LOG: u32 = 18;

impl Session {
pub fn new(guest_path: &str, out_path: &str) -> Self {
Session {
pipeline: pipeline_from_guest(guest_path, Path::new(out_path)),
pipeline: pipeline_from_guest(
guest_path,
Path::new(out_path),
DEFAULT_MIN_DEGREE_LOG,
DEFAULT_MAX_DEGREE_LOG,
),
out_path: out_path.into(),
}
.with_backend(powdr_backend::BackendType::Plonky3)
}

/// Create a new session with a specific chunk size, represented by its log2.
/// Example: for a chunk size of 2^20, set chunk_size_log to 20.
pub fn new_with_chunk_size(guest_path: &str, out_path: &str, chunk_size_log: u32) -> Self {
assert!(chunk_size_log >= DEFAULT_MIN_MAX_DEGREE_LOG);
Session {
pipeline: pipeline_from_guest(
guest_path,
Path::new(out_path),
DEFAULT_MIN_DEGREE_LOG,
chunk_size_log,
),
out_path: out_path.into(),
}
.with_backend(powdr_backend::BackendType::Plonky3)
Expand Down Expand Up @@ -137,12 +164,19 @@ fn pil_file_path(asm_name: &Path) -> PathBuf {
asm_name.with_file_name(opt_file_stem).with_extension("pil")
}

pub fn build_guest(guest_path: &str, out_path: &Path) -> (PathBuf, String) {
pub fn build_guest(
guest_path: &str,
out_path: &Path,
min_degree_log: u32,
max_degree_log: u32,
) -> (PathBuf, String) {
riscv::compile_rust(
guest_path,
CompilerOptions::new_gl()
.with_poseidon()
.with_continuations(),
.with_continuations()
.with_min_degree_log(min_degree_log)
.with_max_degree_log(max_degree_log),
out_path,
true,
None,
Expand All @@ -151,10 +185,16 @@ pub fn build_guest(guest_path: &str, out_path: &Path) -> (PathBuf, String) {
.unwrap()
}

pub fn pipeline_from_guest(guest_path: &str, out_path: &Path) -> Pipeline<GoldilocksField> {
pub fn pipeline_from_guest(
guest_path: &str,
out_path: &Path,
min_degree_log: u32,
max_degree_log: u32,
) -> Pipeline<GoldilocksField> {
log::info!("Compiling guest program...");

let (asm_file_path, asm_contents) = build_guest(guest_path, out_path);
let (asm_file_path, asm_contents) =
build_guest(guest_path, out_path, min_degree_log, max_degree_log);

// Create a pipeline from the asm program
Pipeline::<GoldilocksField>::default()
Expand Down
2 changes: 1 addition & 1 deletion riscv/benches/executor_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn executor_benchmark(c: &mut Criterion) {
let executable =
compile_rust_crate_to_riscv("./tests/riscv_data/keccak/Cargo.toml", &tmp_dir, None);
let options = CompilerOptions::new_gl();
let contents = elf::translate(&executable, options.clone());
let contents = elf::translate(&executable, options);
let mut pipeline = Pipeline::<T>::default().from_asm_string(contents, None);
pipeline.compute_optimized_pil().unwrap();
pipeline.compute_fixed_cols().unwrap();
Expand Down
10 changes: 8 additions & 2 deletions riscv/src/large_field/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn translate_program(program: impl RiscVProgram, options: CompilerOptions) -
translate_program_impl(program, options.field, &runtime, options.continuations);

riscv_machine(
options,
&runtime,
&preamble(options.field, &runtime, options.continuations),
initial_mem,
Expand Down Expand Up @@ -184,6 +185,7 @@ fn translate_program_impl(
}

fn riscv_machine(
options: CompilerOptions,
runtime: &Runtime,
preamble: &str,
initial_memory: Vec<String>,
Expand All @@ -207,11 +209,15 @@ let initial_memory: (fe, fe)[] = [
}}
"#,
runtime.submachines_import(),
1 << powdr_linker::MIN_DEGREE_LOG,
1 << (options
.min_degree_log
.unwrap_or(powdr_linker::MIN_DEGREE_LOG as u32)),
// We expect some machines (e.g. register memory) to use up to 4x the number
// of rows as main. By setting the max degree of main to be smaller by a factor
// of 4, we ensure that we don't run out of rows in those machines.
1 << (*powdr_linker::MAX_DEGREE_LOG - 2),
1 << options
.max_degree_log
.unwrap_or(*powdr_linker::MAX_DEGREE_LOG as u32 - 2),
runtime.submachines_declare(),
preamble,
initial_memory
Expand Down
26 changes: 24 additions & 2 deletions riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod small_field;
static TARGET_STD: &str = "riscv32im-risc0-zkvm-elf";
static TARGET_NO_STD: &str = "riscv32imac-unknown-none-elf";

#[derive(Default, Clone)]
#[derive(Copy, Default, Clone)]
pub struct RuntimeLibs {
pub arith: bool,
pub keccak: bool,
Expand Down Expand Up @@ -58,11 +58,13 @@ impl RuntimeLibs {
}
}
}
#[derive(Clone)]
#[derive(Copy, Clone)]
pub struct CompilerOptions {
pub field: KnownField,
pub libs: RuntimeLibs,
pub continuations: bool,
pub min_degree_log: Option<u32>,
pub max_degree_log: Option<u32>,
}

impl CompilerOptions {
Expand All @@ -71,6 +73,8 @@ impl CompilerOptions {
field,
libs,
continuations,
min_degree_log: None,
max_degree_log: None,
}
}

Expand All @@ -79,6 +83,8 @@ impl CompilerOptions {
field: KnownField::BabyBearField,
libs: RuntimeLibs::new(),
continuations: false,
min_degree_log: None,
max_degree_log: None,
}
}

Expand All @@ -87,6 +93,22 @@ impl CompilerOptions {
field: KnownField::GoldilocksField,
libs: RuntimeLibs::new(),
continuations: false,
min_degree_log: None,
max_degree_log: None,
}
}

pub fn with_min_degree_log(self, log_min_degree: u32) -> Self {
Self {
min_degree_log: Some(log_min_degree),
..self
}
}

pub fn with_max_degree_log(self, log_max_degree: u32) -> Self {
Self {
max_degree_log: Some(log_max_degree),
..self
}
}

Expand Down
2 changes: 1 addition & 1 deletion riscv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn verify_riscv_asm_file(asm_file: &Path, options: CompilerOptions, use_pie:

let case_name = asm_file.file_stem().unwrap().to_str().unwrap();

let powdr_asm = powdr_riscv::elf::translate(&executable, options.clone());
let powdr_asm = powdr_riscv::elf::translate(&executable, options);

match options.field {
KnownField::BabyBearField => {
Expand Down
4 changes: 2 additions & 2 deletions riscv/tests/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn features_with_options<T: FieldElement>(options: CompilerOptions) {
);

log::info!("Verifying {case} converted from ELF file");
let from_elf = powdr_riscv::elf::translate(&executable, options.clone());
let from_elf = powdr_riscv::elf::translate(&executable, options);
verify_riscv_asm_string::<T, usize>(
&format!("{case}_from_elf.asm"),
&from_elf,
Expand All @@ -403,7 +403,7 @@ fn features_with_options<T: FieldElement>(options: CompilerOptions) {
);

log::info!("Verifying {case} converted from ELF file");
let from_elf = powdr_riscv::elf::translate(&executable, options.clone());
let from_elf = powdr_riscv::elf::translate(&executable, options);
verify_riscv_asm_string::<T, usize>(
&format!("{case}_from_elf.asm"),
&from_elf,
Expand Down

0 comments on commit 2113aa0

Please sign in to comment.