diff --git a/crates/lean_compiler/src/parser/error.rs b/crates/lean_compiler/src/parser/error.rs index c4bc77c9..01191b04 100644 --- a/crates/lean_compiler/src/parser/error.rs +++ b/crates/lean_compiler/src/parser/error.rs @@ -60,7 +60,7 @@ impl Display for SemanticError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message)?; if let Some(context) = &self.context { - write!(f, " (in {})", context)?; + write!(f, " (in {context})")?; } Ok(()) } diff --git a/crates/lean_compiler/src/parser/grammar.rs b/crates/lean_compiler/src/parser/grammar.rs index 90908c32..78e25f68 100644 --- a/crates/lean_compiler/src/parser/grammar.rs +++ b/crates/lean_compiler/src/parser/grammar.rs @@ -24,7 +24,7 @@ pub fn next_inner<'i>( expected: &str, ) -> Option> { pairs.next().or_else(|| { - eprintln!("Warning: Expected {} but found nothing", expected); + eprintln!("Warning: Expected {expected} but found nothing"); None }) } diff --git a/crates/lean_compiler/src/parser/parsers/literal.rs b/crates/lean_compiler/src/parser/parsers/literal.rs index 7b54a8d2..e3a1d218 100644 --- a/crates/lean_compiler/src/parser/parsers/literal.rs +++ b/crates/lean_compiler/src/parser/parsers/literal.rs @@ -36,7 +36,7 @@ impl Parse<(String, usize)> for ConstantDeclarationParser { ) .ok_or_else(|| { SemanticError::with_context( - format!("Failed to evaluate constant: {}", name), + format!("Failed to evaluate constant: {name}"), "constant declaration", ) })? @@ -123,7 +123,7 @@ impl Parse for ConstExprParser { Ok(value) } else { Err(SemanticError::with_context( - format!("Invalid constant expression in match pattern: {}", text), + format!("Invalid constant expression in match pattern: {text}"), "match pattern", ) .into()) diff --git a/crates/lean_compiler/src/parser/parsers/mod.rs b/crates/lean_compiler/src/parser/parsers/mod.rs index 956d860b..1288b559 100644 --- a/crates/lean_compiler/src/parser/parsers/mod.rs +++ b/crates/lean_compiler/src/parser/parsers/mod.rs @@ -31,7 +31,7 @@ impl ParseContext { pub fn add_constant(&mut self, name: String, value: usize) -> Result<(), SemanticError> { if self.constants.insert(name.clone(), value).is_some() { Err(SemanticError::with_context( - format!("Multiply defined constant: {}", name), + format!("Multiply defined constant: {name}"), "constant declaration", )) } else { diff --git a/crates/lean_compiler/src/parser/parsers/program.rs b/crates/lean_compiler/src/parser/parsers/program.rs index 5e23e15e..5bb3b6e2 100644 --- a/crates/lean_compiler/src/parser/parsers/program.rs +++ b/crates/lean_compiler/src/parser/parsers/program.rs @@ -37,7 +37,7 @@ impl Parse<(Program, BTreeMap)> for ProgramParser { if functions.insert(name.clone(), function).is_some() { return Err(SemanticError::with_context( - format!("Multiply defined function: {}", name), + format!("Multiply defined function: {name}"), "function definition", ) .into()); diff --git a/crates/lean_vm/src/diagnostics/profiler.rs b/crates/lean_vm/src/diagnostics/profiler.rs index 5e845471..6fe43005 100644 --- a/crates/lean_vm/src/diagnostics/profiler.rs +++ b/crates/lean_vm/src/diagnostics/profiler.rs @@ -21,7 +21,7 @@ pub(crate) fn profiling_report( let mut call_stack: Vec = Vec::new(); let mut prev_function_name = String::new(); - for (&line_num, &cycle_count) in instructions.lines.iter().zip(&instructions.cycles) { + for (&line_num, &cycle_count) in instructions.lines.iter().zip(&instructions.lines_cycles) { let (_, current_function_name) = find_function_for_line(line_num, function_locations); if prev_function_name != current_function_name { diff --git a/crates/lean_vm/src/execution/context.rs b/crates/lean_vm/src/execution/context.rs index 4344b4ec..17a78a3b 100644 --- a/crates/lean_vm/src/execution/context.rs +++ b/crates/lean_vm/src/execution/context.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; #[derive(Debug, Clone, Default)] pub struct ExecutionHistory { pub lines: Vec, - pub cycles: Vec, // for each line, how many cycles it took + pub lines_cycles: Vec, // for each line, how many cycles it took } impl ExecutionHistory { @@ -14,11 +14,11 @@ impl ExecutionHistory { pub fn add_line(&mut self, location: SourceLineNumber, cycles: usize) { self.lines.push(location); - self.cycles.push(cycles); + self.lines_cycles.push(cycles); } pub fn total_cycles(&self) -> usize { - self.cycles.iter().sum() + self.lines_cycles.iter().sum() } pub const fn len(&self) -> usize { diff --git a/crates/lean_vm/src/execution/runner.rs b/crates/lean_vm/src/execution/runner.rs index 765de5cb..9b26f7bf 100644 --- a/crates/lean_vm/src/execution/runner.rs +++ b/crates/lean_vm/src/execution/runner.rs @@ -1,6 +1,5 @@ //! VM execution runner -use crate::HintExecutionContext; use crate::core::{ DIMENSION, F, ONE_VEC_PTR, POSEIDON_16_NULL_HASH_PTR, POSEIDON_24_NULL_HASH_PTR, PUBLIC_INPUT_START, VECTOR_LEN, ZERO_VEC_PTR, @@ -12,6 +11,7 @@ use crate::isa::instruction::InstructionContext; use crate::witness::{ WitnessDotProduct, WitnessMultilinearEval, WitnessPoseidon16, WitnessPoseidon24, }; +use crate::{CodeAddress, HintExecutionContext, SourceLineNumber}; use p3_field::PrimeCharacteristicRing; use p3_symmetric::Permutation; use std::collections::BTreeMap; @@ -72,7 +72,7 @@ pub fn execute_bytecode( ) -> ExecutionResult { let mut std_out = String::new(); let mut instruction_history = ExecutionHistory::new(); - execute_bytecode_helper( + let result = execute_bytecode_helper( bytecode, public_input, private_input, @@ -101,7 +101,49 @@ pub fn execute_bytecode( print!("{std_out}"); } panic!("Error during bytecode execution: {err}"); - }) + }); + if profiler { + print_line_cycle_counts(instruction_history); + print_instruction_cycle_counts(bytecode, result.pcs.clone()); + } + result +} + +fn print_line_cycle_counts(history: ExecutionHistory) { + println!("Line by line cycle counts"); + println!("=========================\n"); + + let mut gross_cycle_counts: BTreeMap = BTreeMap::new(); + for (line, cycle_count) in history.lines.iter().zip(history.lines_cycles.iter()) { + let prev_count = gross_cycle_counts.get(line).unwrap_or(&0); + gross_cycle_counts.insert(*line, *prev_count + cycle_count); + } + for (line, cycle_count) in gross_cycle_counts.iter() { + println!("line {line}: {cycle_count} cycles"); + } + println!(); +} + +fn print_instruction_cycle_counts(bytecode: &Bytecode, pcs: Vec) { + println!("Instruction level cycle counts"); + println!("=============================="); + + let mut gross_cycle_counts: BTreeMap = BTreeMap::new(); + for pc in pcs.iter() { + let prev_count = gross_cycle_counts.get(pc).unwrap_or(&0); + gross_cycle_counts.insert(*pc, *prev_count + 1); + } + for (pc, cycle_count) in gross_cycle_counts.iter() { + let instruction = &bytecode.instructions[*pc]; + let hints = bytecode.hints.get(pc); + if let Some(hints) = hints { + for hint in hints { + println!("hint: {hint}"); + } + } + println!("pc {pc}: {cycle_count} cycles: {instruction}"); + } + println!(); } /// Helper function that performs the actual bytecode execution diff --git a/crates/lean_vm/src/isa/hint.rs b/crates/lean_vm/src/isa/hint.rs index 86d20ace..c26fbac4 100644 --- a/crates/lean_vm/src/isa/hint.rs +++ b/crates/lean_vm/src/isa/hint.rs @@ -187,7 +187,7 @@ impl Hint { Self::LocationReport { location } => { ctx.instruction_history.lines.push(*location); ctx.instruction_history - .cycles + .lines_cycles .push(*ctx.cpu_cycles_before_new_line); *ctx.cpu_cycles_before_new_line = 0; }