From 8065794633ffeda168e60fe730a83cd780d11419 Mon Sep 17 00:00:00 2001 From: schaeff Date: Thu, 14 Mar 2024 22:11:35 +0100 Subject: [PATCH] allow expression in degree statement --- airgen/src/lib.rs | 2 +- ast/src/asm_analysis/mod.rs | 3 +-- ast/src/object/display.rs | 2 +- ast/src/object/mod.rs | 6 +++--- ast/src/parsed/asm.rs | 2 +- linker/src/lib.rs | 15 ++++++++------- parser/src/powdr.lalrpop | 2 +- riscv/src/continuations.rs | 8 +++++++- test_data/asm/empty_vm.asm | 2 +- 9 files changed, 24 insertions(+), 18 deletions(-) diff --git a/airgen/src/lib.rs b/airgen/src/lib.rs index 276bad9c6..39eb8bbbc 100644 --- a/airgen/src/lib.rs +++ b/airgen/src/lib.rs @@ -182,7 +182,7 @@ impl<'a> ASMPILConverter<'a> { panic!(); }; - let degree = input.degree.map(|s| s.degree.try_into().unwrap()); + let degree = input.degree.map(|s| s.degree); self.submachines = input.submachines; diff --git a/ast/src/asm_analysis/mod.rs b/ast/src/asm_analysis/mod.rs index 1d7a48f21..053ad43c4 100644 --- a/ast/src/asm_analysis/mod.rs +++ b/ast/src/asm_analysis/mod.rs @@ -10,7 +10,6 @@ use std::{ }; use itertools::Either; -use powdr_number::BigUint; use crate::parsed::{ asm::{ @@ -529,7 +528,7 @@ pub struct OperationSymbol { #[derive(Clone, Debug)] pub struct DegreeStatement { - pub degree: BigUint, + pub degree: Expression, } #[derive(Clone, Debug)] diff --git a/ast/src/object/display.rs b/ast/src/object/display.rs index e3dc95a2f..be728e8c4 100644 --- a/ast/src/object/display.rs +++ b/ast/src/object/display.rs @@ -40,7 +40,7 @@ impl Display for PILGraph { impl Display for Object { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - if let Some(degree) = self.degree { + if let Some(degree) = self.degree.as_ref() { writeln!(f, "// Degree {}", degree)?; } for s in &self.pil { diff --git a/ast/src/object/mod.rs b/ast/src/object/mod.rs index fab5248cc..e5508656c 100644 --- a/ast/src/object/mod.rs +++ b/ast/src/object/mod.rs @@ -43,7 +43,7 @@ pub enum TypeOrExpression { #[derive(Default, Clone)] pub struct Object { - pub degree: Option, + pub degree: Option, /// the pil identities for this machine pub pil: Vec, /// the links from this machine to its children @@ -57,8 +57,8 @@ pub struct Object { } impl Object { - pub fn with_degree(mut self, degree: Option) -> Self { - self.degree = degree; + pub fn with_degree>(mut self, degree: Option) -> Self { + self.degree = degree.map(Into::into); self } } diff --git a/ast/src/parsed/asm.rs b/ast/src/parsed/asm.rs index 275a43e1e..a26235bee 100644 --- a/ast/src/parsed/asm.rs +++ b/ast/src/parsed/asm.rs @@ -475,8 +475,8 @@ pub struct Instruction { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] pub enum MachineStatement { - Degree(SourceRef, BigUint), CallSelectors(SourceRef, String), + Degree(SourceRef, Expression), Pil(SourceRef, PilStatement), Submachine(SourceRef, SymbolPath, String), RegisterDeclaration(SourceRef, String, Option), diff --git a/linker/src/lib.rs b/linker/src/lib.rs index 49c4c283f..b40e29189 100644 --- a/linker/src/lib.rs +++ b/linker/src/lib.rs @@ -15,7 +15,7 @@ use powdr_ast::{ use itertools::Itertools; -const DEFAULT_DEGREE: u64 = 1024; +const DEFAULT_DEGREE: u32 = 1024; const MAIN_OPERATION_NAME: &str = "main"; /// a monolithic linker which outputs a single AIR @@ -27,11 +27,12 @@ pub fn link(graph: PILGraph) -> Result> { .get(&main_machine.location) .unwrap() .degree - .unwrap_or(DEFAULT_DEGREE); + .clone() + .unwrap_or_else(|| DEFAULT_DEGREE.into()); let mut errors = vec![]; - let mut pil = process_definitions(main_degree, graph.definitions); + let mut pil = process_definitions(&main_degree, graph.definitions); for (location, object) in graph.objects.into_iter() { if let Some(degree) = object.degree { @@ -47,7 +48,7 @@ pub fn link(graph: PILGraph) -> Result> { pil.push(PilStatement::Namespace( SourceRef::unknown(), SymbolPath::from_identifier(location.to_string()), - Expression::Number(main_degree.into(), None), + main_degree.clone(), )); pil.extend(object.pil); @@ -90,7 +91,7 @@ pub fn link(graph: PILGraph) -> Result> { // Extract the utilities and sort them into namespaces where possible. fn process_definitions( - main_degree: u64, + main_degree: &Expression, definitions: BTreeMap, ) -> Vec { let mut current_namespace = Default::default(); @@ -125,7 +126,7 @@ fn process_definitions( PilStatement::Namespace( SourceRef::unknown(), namespace.relative_to(&AbsoluteSymbolPath::default()), - Expression::Number(main_degree.into(), None), + main_degree.clone(), ), statement, ] @@ -306,7 +307,7 @@ mod test { #[test] fn compile_empty_vm() { - let expectation = r#"namespace main(8); + let expectation = r#"namespace main((4 + 4)); pol commit _operation_id(i) query std::prover::Query::Hint(2); pol constant _block_enforcer_last_step = [0]* + [1]; let _operation_id_no_change = ((1 - _block_enforcer_last_step) * (1 - instr_return)); diff --git a/parser/src/powdr.lalrpop b/parser/src/powdr.lalrpop index 756c88403..43435c7f2 100644 --- a/parser/src/powdr.lalrpop +++ b/parser/src/powdr.lalrpop @@ -270,7 +270,7 @@ PilStatementInMachine: MachineStatement = { } Degree: MachineStatement = { - "degree" ";" => MachineStatement::Degree(ctx.source_ref(start), deg) + "degree" ";" => MachineStatement::Degree(ctx.source_ref(start), deg) } CallSelectors: MachineStatement = { diff --git a/riscv/src/continuations.rs b/riscv/src/continuations.rs index 060ab8e6a..f0c487a65 100644 --- a/riscv/src/continuations.rs +++ b/riscv/src/continuations.rs @@ -235,7 +235,13 @@ pub fn rust_continuations_dry_run( .fold(None, |acc, (_, m)| acc.or(m.degree.clone())) .unwrap() .degree; - let length = F::from(length).to_degree() as usize; + + let length: usize = match length { + Expression::Number(length, None) => length.try_into().unwrap(), + e => unimplemented!( + "degree {e} is not supported in continuations as we don't have an evaluator yet" + ), + }; loop { log::info!("\nRunning chunk {}...", chunk_index); diff --git a/test_data/asm/empty_vm.asm b/test_data/asm/empty_vm.asm index 7a4921c7d..691c9fa4a 100644 --- a/test_data/asm/empty_vm.asm +++ b/test_data/asm/empty_vm.asm @@ -1,5 +1,5 @@ machine VM { - degree 8; + degree 4 + 4; reg pc[@pc]; } \ No newline at end of file