From c3e2c33f5d0f329f4919eb6d18df20fca2af94dc Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Mon, 22 Sep 2025 19:50:44 +0000 Subject: [PATCH 1/8] refactor source line numbering types --- crates/lean_compiler/src/a_simplify_lang.rs | 4 ++-- crates/lean_compiler/src/intermediate_bytecode.rs | 2 +- crates/lean_compiler/src/lang.rs | 2 +- crates/lean_compiler/src/parser.rs | 4 ++-- crates/lean_vm/src/lean_isa.rs | 8 +++++--- crates/lean_vm/src/lib.rs | 2 +- crates/lean_vm/src/runner.rs | 2 +- crates/lean_vm/src/stack_trace.rs | 6 +++--- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/lean_compiler/src/a_simplify_lang.rs b/crates/lean_compiler/src/a_simplify_lang.rs index f3ce3113b..f7f43c61f 100644 --- a/crates/lean_compiler/src/a_simplify_lang.rs +++ b/crates/lean_compiler/src/a_simplify_lang.rs @@ -7,7 +7,7 @@ use crate::{ }, precompiles::Precompile, }; -use lean_vm::LocationInSourceCode; +use lean_vm::SourceLineNumber; use std::{ collections::{BTreeMap, BTreeSet}, fmt::{Display, Formatter}, @@ -137,7 +137,7 @@ pub enum SimpleLine { }, // noop, debug purpose only LocationReport { - location: LocationInSourceCode, + location: SourceLineNumber, }, } diff --git a/crates/lean_compiler/src/intermediate_bytecode.rs b/crates/lean_compiler/src/intermediate_bytecode.rs index 347f1684d..2d25e7ee8 100644 --- a/crates/lean_compiler/src/intermediate_bytecode.rs +++ b/crates/lean_compiler/src/intermediate_bytecode.rs @@ -150,7 +150,7 @@ pub enum IntermediateInstruction { }, // noop, debug purpose only LocationReport { - location: LocationInSourceCode, + location: SourceLineNumber, }, } diff --git a/crates/lean_compiler/src/lang.rs b/crates/lean_compiler/src/lang.rs index d6c770520..8b88713d1 100644 --- a/crates/lean_compiler/src/lang.rs +++ b/crates/lean_compiler/src/lang.rs @@ -356,7 +356,7 @@ pub enum Line { }, // noop, debug purpose only LocationReport { - location: LocationInSourceCode, + location: SourceLineNumber, }, } impl Display for Expression { diff --git a/crates/lean_compiler/src/parser.rs b/crates/lean_compiler/src/parser.rs index 80b90384a..df47719b5 100644 --- a/crates/lean_compiler/src/parser.rs +++ b/crates/lean_compiler/src/parser.rs @@ -175,7 +175,7 @@ fn parse_parameter(pair: Pair<'_, Rule>) -> Result<(String, bool), ParseError> { Ok((first.as_str().to_string(), false)) } -fn on_new_located_line(lines: &mut Vec, line_and_location: (Line, LocationInSourceCode)) { +fn on_new_located_line(lines: &mut Vec, line_and_location: (Line, SourceLineNumber)) { let (line, location) = line_and_location; lines.push(Line::LocationReport { location }); lines.push(line); @@ -185,7 +185,7 @@ fn parse_statement( pair: Pair<'_, Rule>, constants: &BTreeMap, trash_var_count: &mut usize, -) -> Result<(Line, LocationInSourceCode), ParseError> { +) -> Result<(Line, SourceLineNumber), ParseError> { let location = pair.line_col().0; let inner = pair.into_inner().next().unwrap(); let line = match inner.as_rule() { diff --git a/crates/lean_vm/src/lean_isa.rs b/crates/lean_vm/src/lean_isa.rs index 9edd5ee7b..0e2881975 100644 --- a/crates/lean_vm/src/lean_isa.rs +++ b/crates/lean_vm/src/lean_isa.rs @@ -1,4 +1,4 @@ -use crate::{F, LocationInSourceCode}; +use crate::{F, SourceLineNumber}; use p3_field::PrimeCharacteristicRing; use std::{ collections::BTreeMap, @@ -7,10 +7,12 @@ use std::{ pub type Label = String; +pub type CodeAddress = usize; + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Bytecode { pub instructions: Vec, - pub hints: BTreeMap>, // pc -> hints + pub hints: BTreeMap>, pub starting_frame_memory: usize, pub ending_pc: usize, } @@ -129,7 +131,7 @@ pub enum Hint { content: Vec, }, LocationReport { - location: LocationInSourceCode, // debug purpose + location: SourceLineNumber, // debug purpose }, } diff --git a/crates/lean_vm/src/lib.rs b/crates/lean_vm/src/lib.rs index 2e3938982..15513d081 100644 --- a/crates/lean_vm/src/lib.rs +++ b/crates/lean_vm/src/lib.rs @@ -11,7 +11,7 @@ pub use lean_isa::*; pub use memory::*; pub use runner::*; -pub type LocationInSourceCode = usize; +pub type SourceLineNumber = usize; pub const DIMENSION: usize = 5; pub const LOG_VECTOR_LEN: usize = 3; diff --git a/crates/lean_vm/src/runner.rs b/crates/lean_vm/src/runner.rs index 785c98983..64cfccc8d 100644 --- a/crates/lean_vm/src/runner.rs +++ b/crates/lean_vm/src/runner.rs @@ -83,7 +83,7 @@ impl MemOrFpOrConstant { #[derive(Debug, Clone, Default)] pub(crate) struct ExecutionHistory { - pub(crate) lines: Vec, + pub(crate) lines: Vec, pub(crate) cycles: Vec, // for each line, how many cycles it took } diff --git a/crates/lean_vm/src/stack_trace.rs b/crates/lean_vm/src/stack_trace.rs index de656d20f..e52b56f12 100644 --- a/crates/lean_vm/src/stack_trace.rs +++ b/crates/lean_vm/src/stack_trace.rs @@ -2,13 +2,13 @@ use std::collections::BTreeMap; use colored::Colorize; -use crate::LocationInSourceCode; +use crate::SourceLineNumber; const STACK_TRACE_MAX_LINES_PER_FUNCTION: usize = 5; pub(crate) fn pretty_stack_trace( source_code: &str, - instructions: &[LocationInSourceCode], // LocationInSourceCode = usize + instructions: &[SourceLineNumber], // SourceLineNumber = usize function_locations: &BTreeMap, ) -> String { let source_lines: Vec<&str> = source_code.lines().collect(); @@ -131,7 +131,7 @@ pub(crate) fn find_function_for_line( fn count_remaining_lines_in_function( current_idx: usize, - instructions: &[LocationInSourceCode], + instructions: &[SourceLineNumber], function_locations: &BTreeMap, current_function_line: usize, ) -> usize { From 5267587ed847e82bf4468961b57a5a3e84d63912 Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Mon, 22 Sep 2025 20:08:55 +0000 Subject: [PATCH 2/8] add source line numbers to object code pretty printing --- crates/lean_vm/src/lean_isa.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/lean_vm/src/lean_isa.rs b/crates/lean_vm/src/lean_isa.rs index 0e2881975..36c0c2399 100644 --- a/crates/lean_vm/src/lean_isa.rs +++ b/crates/lean_vm/src/lean_isa.rs @@ -294,7 +294,9 @@ impl Display for Hint { Self::Inverse { arg, res_offset } => { write!(f, "m[fp + {res_offset}] = inverse({arg})") } - Self::LocationReport { .. } => Ok(()), + Self::LocationReport { location: line_number } => { + write!(f, "source line number: {line_number}") + } } } } From 0338cdc03030c81d8f9af095d13bdd320aab8dbf Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Mon, 22 Sep 2025 20:44:49 +0000 Subject: [PATCH 3/8] show labels in object code pretty-printing --- crates/lean_compiler/src/c_compile_final.rs | 9 +++++++++ crates/lean_compiler/src/lib.rs | 2 ++ crates/lean_vm/src/lean_isa.rs | 6 ++++++ crates/lean_vm/src/runner.rs | 1 + 4 files changed, 18 insertions(+) diff --git a/crates/lean_compiler/src/c_compile_final.rs b/crates/lean_compiler/src/c_compile_final.rs index 8b4787bc4..830310025 100644 --- a/crates/lean_compiler/src/c_compile_final.rs +++ b/crates/lean_compiler/src/c_compile_final.rs @@ -91,6 +91,15 @@ pub fn compile_to_low_level_bytecode( let mut low_level_bytecode = Vec::new(); let mut hints = BTreeMap::new(); + for (label, pc) in label_to_pc.clone() { + let pc_hints: &mut Vec = + match hints.try_insert(pc, vec![]) { + Ok(pc_hints) => pc_hints, + Err(_) => hints.get_mut(&pc).unwrap(), + }; + pc_hints.push(Hint::Label { label }); + } + let compiler = Compiler { memory_size_per_function: intermediate_bytecode.memory_size_per_function, label_to_pc, diff --git a/crates/lean_compiler/src/lib.rs b/crates/lean_compiler/src/lib.rs index 135c5815c..4d7ddc325 100644 --- a/crates/lean_compiler/src/lib.rs +++ b/crates/lean_compiler/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(map_try_insert)] + use std::collections::BTreeMap; use lean_vm::*; diff --git a/crates/lean_vm/src/lean_isa.rs b/crates/lean_vm/src/lean_isa.rs index 36c0c2399..5ebabe341 100644 --- a/crates/lean_vm/src/lean_isa.rs +++ b/crates/lean_vm/src/lean_isa.rs @@ -133,6 +133,9 @@ pub enum Hint { LocationReport { location: SourceLineNumber, // debug purpose }, + Label { + label: Label, + }, } impl MemOrConstant { @@ -297,6 +300,9 @@ impl Display for Hint { Self::LocationReport { location: line_number } => { write!(f, "source line number: {line_number}") } + Self::Label { label } => { + write!(f, "label: {label}") + } } } } diff --git a/crates/lean_vm/src/runner.rs b/crates/lean_vm/src/runner.rs index 64cfccc8d..2e26aafdf 100644 --- a/crates/lean_vm/src/runner.rs +++ b/crates/lean_vm/src/runner.rs @@ -342,6 +342,7 @@ fn execute_bytecode_helper( instruction_history.cycles.push(cpu_cycles_before_new_line); cpu_cycles_before_new_line = 0; } + Hint::Label { label: _label } => { /* no need to do anything */ } } } From 9c501eee9e2421e9b9e27f8f95f093e62cdb0ba3 Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Wed, 24 Sep 2025 03:02:49 +0000 Subject: [PATCH 4/8] augment jump pretty printing with labels --- crates/lean_compiler/src/c_compile_final.rs | 69 +++++++++++++------ .../src/instruction_encoder.rs | 1 + crates/lean_vm/src/lean_isa.rs | 4 +- crates/lean_vm/src/runner.rs | 1 + 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/crates/lean_compiler/src/c_compile_final.rs b/crates/lean_compiler/src/c_compile_final.rs index 830310025..4659ba96f 100644 --- a/crates/lean_compiler/src/c_compile_final.rs +++ b/crates/lean_compiler/src/c_compile_final.rs @@ -1,6 +1,6 @@ use crate::{F, PUBLIC_INPUT_START, ZERO_VEC_PTR, intermediate_bytecode::*, lang::*}; use lean_vm::*; -use p3_field::PrimeCharacteristicRing; +use p3_field::{PrimeCharacteristicRing, PrimeField32}; use std::collections::BTreeMap; use utils::ToUsize; @@ -28,9 +28,9 @@ impl IntermediateInstruction { struct Compiler { memory_size_per_function: BTreeMap, - label_to_pc: BTreeMap, + label_to_pc: BTreeMap, match_block_sizes: Vec, - match_first_block_starts: Vec, + match_first_block_starts: Vec, } pub fn compile_to_low_level_bytecode( @@ -128,9 +128,9 @@ pub fn compile_to_low_level_bytecode( fn compile_block( compiler: &Compiler, block: &[IntermediateInstruction], - pc_start: usize, + pc_start: CodeAddress, low_level_bytecode: &mut Vec, - hints: &mut BTreeMap>, + hints: &mut BTreeMap>, ) { let try_as_mem_or_constant = |value: &IntermediateValue| { if let Some(cst) = try_as_constant(value, compiler) { @@ -152,6 +152,47 @@ fn compile_block( _ => None, }; + let codegen_jump = + |hints: &BTreeMap>, + low_level_bytecode: &mut Vec, + condition: IntermediateValue, + dest: IntermediateValue, + updated_fp: Option| { + let dest = try_as_mem_or_constant(&dest) + .expect("Fatal: Could not materialize jump destination"); + let label = + match dest { + MemOrConstant::Constant(dest) => { + hints.get(&usize::try_from(dest.as_canonical_u32()).unwrap()) + .map(|hints: &Vec| hints.iter().find_map(|x| { + match x { + Hint::Label { label } => Some(label), + _ => None, + } + })) + .flatten() + .expect("Fatal: Unlabeled jump destination") + .clone() + }, + MemOrConstant::MemoryAfterFp { offset } => { + if offset >= 0 { + format!("fp+{offset}") + } else { + format!("fp-{offset}") + } + }, + }; + let updated_fp = updated_fp + .map(|fp| try_as_mem_or_fp(&fp).unwrap()) + .unwrap_or(MemOrFp::Fp); + low_level_bytecode.push(Instruction::Jump { + condition: try_as_mem_or_constant(&condition).unwrap(), + label, + dest, + updated_fp, + }); + }; + let mut pc = pc_start; for instruction in block { match instruction.clone() { @@ -226,23 +267,11 @@ fn compile_block( dest, updated_fp, } => { - let updated_fp = updated_fp - .map(|fp| try_as_mem_or_fp(&fp).unwrap()) - .unwrap_or(MemOrFp::Fp); - low_level_bytecode.push(Instruction::Jump { - condition: try_as_mem_or_constant(&condition).unwrap(), - dest: try_as_mem_or_constant(&dest).unwrap(), - updated_fp, - }); + codegen_jump(hints, low_level_bytecode, condition, dest, updated_fp) } IntermediateInstruction::Jump { dest, updated_fp } => { - low_level_bytecode.push(Instruction::Jump { - condition: MemOrConstant::one(), - dest: try_as_mem_or_constant(&dest).unwrap(), - updated_fp: updated_fp - .map(|fp| try_as_mem_or_fp(&fp).unwrap()) - .unwrap_or(MemOrFp::Fp), - }); + let one = IntermediateValue::Constant(ConstExpression::Value(ConstantValue::Scalar(1))); + codegen_jump(hints, low_level_bytecode, one, dest, updated_fp) } IntermediateInstruction::Poseidon2_16 { arg_a, arg_b, res } => { low_level_bytecode.push(Instruction::Poseidon2_16 { diff --git a/crates/lean_prover/witness_generation/src/instruction_encoder.rs b/crates/lean_prover/witness_generation/src/instruction_encoder.rs index 6cb8853a4..90012a3d3 100644 --- a/crates/lean_prover/witness_generation/src/instruction_encoder.rs +++ b/crates/lean_prover/witness_generation/src/instruction_encoder.rs @@ -53,6 +53,7 @@ pub fn field_representation(instr: &Instruction) -> [F; N_INSTRUCTION_COLUMNS] { } Instruction::Jump { condition, + label: _, dest, updated_fp, } => { diff --git a/crates/lean_vm/src/lean_isa.rs b/crates/lean_vm/src/lean_isa.rs index 5ebabe341..3e7c6a9e4 100644 --- a/crates/lean_vm/src/lean_isa.rs +++ b/crates/lean_vm/src/lean_isa.rs @@ -57,6 +57,7 @@ pub enum Instruction { }, // res = m[m[fp + shift_0] + shift_1] Jump { condition: MemOrConstant, + label: Label, // for debugging purposes dest: MemOrConstant, updated_fp: MemOrFp, }, @@ -232,12 +233,13 @@ impl Display for Instruction { } Self::Jump { condition, + label, dest, updated_fp, } => { write!( f, - "if {condition} != 0 jump to {dest} with next(fp) = {updated_fp}" + "if {condition} != 0 jump to {label} = {dest} with next(fp) = {updated_fp}" ) } Self::Poseidon2_16 { arg_a, arg_b, res } => { diff --git a/crates/lean_vm/src/runner.rs b/crates/lean_vm/src/runner.rs index 2e26aafdf..554c4c1b8 100644 --- a/crates/lean_vm/src/runner.rs +++ b/crates/lean_vm/src/runner.rs @@ -414,6 +414,7 @@ fn execute_bytecode_helper( } Instruction::Jump { condition, + label: _, dest, updated_fp, } => { From fbbb4eb2a566eede211e25782d01a3f2a4f9caad Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Wed, 24 Sep 2025 03:08:34 +0000 Subject: [PATCH 5/8] augment program pretty printing with function table --- crates/lean_compiler/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/lean_compiler/src/lib.rs b/crates/lean_compiler/src/lib.rs index 4d7ddc325..390963e0b 100644 --- a/crates/lean_compiler/src/lib.rs +++ b/crates/lean_compiler/src/lib.rs @@ -26,7 +26,11 @@ pub fn compile_program(program: &str) -> (Bytecode, BTreeMap) { let intermediate_bytecode = compile_to_intermediate_bytecode(simple_program).unwrap(); // println!("Intermediate Bytecode:\n\n{}", intermediate_bytecode.to_string()); let compiled = compile_to_low_level_bytecode(intermediate_bytecode).unwrap(); - println!("Compiled Program:\n\n{}", compiled.to_string()); + println!("Function Locations: \n"); + for (loc, name) in function_locations.iter() { + println!("{name}: {loc}"); + } + println!("\n\nCompiled Program:\n\n{}", compiled.to_string()); (compiled, function_locations) } From 101c6c3fdf8f33a475f9b2bb58a3a18db069fac9 Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Wed, 24 Sep 2025 13:46:35 +0000 Subject: [PATCH 6/8] refactor --- crates/lean_vm/src/core/types.rs | 3 +++ crates/lean_vm/src/isa/bytecode.rs | 4 +--- crates/lean_vm/src/isa/hint.rs | 3 +-- crates/lean_vm/src/isa/instruction.rs | 3 +-- crates/lean_vm/src/lib.rs | 2 -- 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/lean_vm/src/core/types.rs b/crates/lean_vm/src/core/types.rs index 8bd985b02..63db5bdf3 100644 --- a/crates/lean_vm/src/core/types.rs +++ b/crates/lean_vm/src/core/types.rs @@ -11,3 +11,6 @@ pub type SourceLineNumber = usize; /// String label for bytecode locations pub type Label = String; + +/// Bytecode address (i.e., a value of the program counter) +pub type CodeAddress = usize; diff --git a/crates/lean_vm/src/isa/bytecode.rs b/crates/lean_vm/src/isa/bytecode.rs index 113c5c45c..ea632845e 100644 --- a/crates/lean_vm/src/isa/bytecode.rs +++ b/crates/lean_vm/src/isa/bytecode.rs @@ -1,13 +1,11 @@ //! Bytecode representation and management -use crate::Hint; +use crate::{CodeAddress, Hint}; use super::Instruction; use std::collections::BTreeMap; use std::fmt::{Display, Formatter}; -pub type CodeAddress = usize; - /// Complete bytecode representation with instructions and hints #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Bytecode { diff --git a/crates/lean_vm/src/isa/hint.rs b/crates/lean_vm/src/isa/hint.rs index 284a09b49..cf0587ea1 100644 --- a/crates/lean_vm/src/isa/hint.rs +++ b/crates/lean_vm/src/isa/hint.rs @@ -1,5 +1,4 @@ -use crate::Label; -use crate::core::{DIMENSION, F, LOG_VECTOR_LEN, SourceLineNumber, VECTOR_LEN}; +use crate::core::{Label, DIMENSION, F, LOG_VECTOR_LEN, SourceLineNumber, VECTOR_LEN}; use crate::diagnostics::RunnerError; use crate::execution::{ExecutionHistory, Memory}; use crate::isa::operands::MemOrConstant; diff --git a/crates/lean_vm/src/isa/instruction.rs b/crates/lean_vm/src/isa/instruction.rs index ea76fa9bb..8a16cce7d 100644 --- a/crates/lean_vm/src/isa/instruction.rs +++ b/crates/lean_vm/src/isa/instruction.rs @@ -2,8 +2,7 @@ use super::Operation; use super::operands::{MemOrConstant, MemOrFp, MemOrFpOrConstant}; -use crate::Label; -use crate::core::{DIMENSION, EF, F, VECTOR_LEN}; +use crate::core::{Label, DIMENSION, EF, F, VECTOR_LEN}; use crate::diagnostics::RunnerError; use crate::execution::Memory; use crate::witness::{ diff --git a/crates/lean_vm/src/lib.rs b/crates/lean_vm/src/lib.rs index 3db2eddc6..a13c780cc 100644 --- a/crates/lean_vm/src/lib.rs +++ b/crates/lean_vm/src/lib.rs @@ -12,8 +12,6 @@ pub use execution::*; pub use isa::*; pub use witness::*; -pub type SourceLineNumber = usize; - /// Main execution entry point for the VM pub fn execute_bytecode( bytecode: &Bytecode, From 6e9b85f3588eeb283aa160e4c6127a8384e39364 Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Wed, 24 Sep 2025 13:46:44 +0000 Subject: [PATCH 7/8] cargo fmt --- crates/lean_compiler/src/c_compile_final.rs | 91 ++++++++++----------- crates/lean_vm/src/isa/hint.rs | 12 +-- crates/lean_vm/src/isa/instruction.rs | 2 +- 3 files changed, 50 insertions(+), 55 deletions(-) diff --git a/crates/lean_compiler/src/c_compile_final.rs b/crates/lean_compiler/src/c_compile_final.rs index 4659ba96f..def9eccf7 100644 --- a/crates/lean_compiler/src/c_compile_final.rs +++ b/crates/lean_compiler/src/c_compile_final.rs @@ -92,11 +92,10 @@ pub fn compile_to_low_level_bytecode( let mut hints = BTreeMap::new(); for (label, pc) in label_to_pc.clone() { - let pc_hints: &mut Vec = - match hints.try_insert(pc, vec![]) { - Ok(pc_hints) => pc_hints, - Err(_) => hints.get_mut(&pc).unwrap(), - }; + let pc_hints: &mut Vec = match hints.try_insert(pc, vec![]) { + Ok(pc_hints) => pc_hints, + Err(_) => hints.get_mut(&pc).unwrap(), + }; pc_hints.push(Hint::Label { label }); } @@ -152,46 +151,43 @@ fn compile_block( _ => None, }; - let codegen_jump = - |hints: &BTreeMap>, - low_level_bytecode: &mut Vec, - condition: IntermediateValue, - dest: IntermediateValue, - updated_fp: Option| { - let dest = try_as_mem_or_constant(&dest) - .expect("Fatal: Could not materialize jump destination"); - let label = - match dest { - MemOrConstant::Constant(dest) => { - hints.get(&usize::try_from(dest.as_canonical_u32()).unwrap()) - .map(|hints: &Vec| hints.iter().find_map(|x| { - match x { - Hint::Label { label } => Some(label), - _ => None, - } - })) - .flatten() - .expect("Fatal: Unlabeled jump destination") - .clone() - }, - MemOrConstant::MemoryAfterFp { offset } => { - if offset >= 0 { - format!("fp+{offset}") - } else { - format!("fp-{offset}") - } - }, - }; - let updated_fp = updated_fp - .map(|fp| try_as_mem_or_fp(&fp).unwrap()) - .unwrap_or(MemOrFp::Fp); - low_level_bytecode.push(Instruction::Jump { - condition: try_as_mem_or_constant(&condition).unwrap(), - label, - dest, - updated_fp, - }); + let codegen_jump = |hints: &BTreeMap>, + low_level_bytecode: &mut Vec, + condition: IntermediateValue, + dest: IntermediateValue, + updated_fp: Option| { + let dest = + try_as_mem_or_constant(&dest).expect("Fatal: Could not materialize jump destination"); + let label = match dest { + MemOrConstant::Constant(dest) => hints + .get(&usize::try_from(dest.as_canonical_u32()).unwrap()) + .map(|hints: &Vec| { + hints.iter().find_map(|x| match x { + Hint::Label { label } => Some(label), + _ => None, + }) + }) + .flatten() + .expect("Fatal: Unlabeled jump destination") + .clone(), + MemOrConstant::MemoryAfterFp { offset } => { + if offset >= 0 { + format!("fp+{offset}") + } else { + format!("fp-{offset}") + } + } }; + let updated_fp = updated_fp + .map(|fp| try_as_mem_or_fp(&fp).unwrap()) + .unwrap_or(MemOrFp::Fp); + low_level_bytecode.push(Instruction::Jump { + condition: try_as_mem_or_constant(&condition).unwrap(), + label, + dest, + updated_fp, + }); + }; let mut pc = pc_start; for instruction in block { @@ -266,11 +262,10 @@ fn compile_block( condition, dest, updated_fp, - } => { - codegen_jump(hints, low_level_bytecode, condition, dest, updated_fp) - } + } => codegen_jump(hints, low_level_bytecode, condition, dest, updated_fp), IntermediateInstruction::Jump { dest, updated_fp } => { - let one = IntermediateValue::Constant(ConstExpression::Value(ConstantValue::Scalar(1))); + let one = + IntermediateValue::Constant(ConstExpression::Value(ConstantValue::Scalar(1))); codegen_jump(hints, low_level_bytecode, one, dest, updated_fp) } IntermediateInstruction::Poseidon2_16 { arg_a, arg_b, res } => { diff --git a/crates/lean_vm/src/isa/hint.rs b/crates/lean_vm/src/isa/hint.rs index cf0587ea1..eaf27f4a4 100644 --- a/crates/lean_vm/src/isa/hint.rs +++ b/crates/lean_vm/src/isa/hint.rs @@ -1,4 +1,4 @@ -use crate::core::{Label, DIMENSION, F, LOG_VECTOR_LEN, SourceLineNumber, VECTOR_LEN}; +use crate::core::{DIMENSION, F, LOG_VECTOR_LEN, Label, SourceLineNumber, VECTOR_LEN}; use crate::diagnostics::RunnerError; use crate::execution::{ExecutionHistory, Memory}; use crate::isa::operands::MemOrConstant; @@ -53,9 +53,7 @@ pub enum Hint { location: SourceLineNumber, }, /// Jump destination label (for debugging purposes) - Label { - label: Label, - }, + Label { label: Label }, } /// Execution state for hint processing @@ -173,7 +171,7 @@ impl Hint { *ctx.cpu_cycles_before_new_line = 0; Ok(false) } - Self::Label { label: _ } => { Ok(false) } + Self::Label { label: _ } => Ok(false), } } } @@ -225,7 +223,9 @@ impl Display for Hint { Self::Inverse { arg, res_offset } => { write!(f, "m[fp + {res_offset}] = inverse({arg})") } - Self::LocationReport { location: line_number } => { + Self::LocationReport { + location: line_number, + } => { write!(f, "source line number: {line_number}") } Self::Label { label } => { diff --git a/crates/lean_vm/src/isa/instruction.rs b/crates/lean_vm/src/isa/instruction.rs index 8a16cce7d..b16881bd9 100644 --- a/crates/lean_vm/src/isa/instruction.rs +++ b/crates/lean_vm/src/isa/instruction.rs @@ -2,7 +2,7 @@ use super::Operation; use super::operands::{MemOrConstant, MemOrFp, MemOrFpOrConstant}; -use crate::core::{Label, DIMENSION, EF, F, VECTOR_LEN}; +use crate::core::{DIMENSION, EF, F, Label, VECTOR_LEN}; use crate::diagnostics::RunnerError; use crate::execution::Memory; use crate::witness::{ From 0e2189aec9bde218071cbe72190142c85aaf883c Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Wed, 24 Sep 2025 13:49:07 +0000 Subject: [PATCH 8/8] fix clippy suggestions --- crates/lean_compiler/src/c_compile_final.rs | 9 ++------- crates/packed_pcs/src/lib.rs | 3 +-- crates/utils/src/multilinear.rs | 4 +--- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/crates/lean_compiler/src/c_compile_final.rs b/crates/lean_compiler/src/c_compile_final.rs index def9eccf7..bc6570e85 100644 --- a/crates/lean_compiler/src/c_compile_final.rs +++ b/crates/lean_compiler/src/c_compile_final.rs @@ -161,21 +161,16 @@ fn compile_block( let label = match dest { MemOrConstant::Constant(dest) => hints .get(&usize::try_from(dest.as_canonical_u32()).unwrap()) - .map(|hints: &Vec| { + .and_then(|hints: &Vec| { hints.iter().find_map(|x| match x { Hint::Label { label } => Some(label), _ => None, }) }) - .flatten() .expect("Fatal: Unlabeled jump destination") .clone(), MemOrConstant::MemoryAfterFp { offset } => { - if offset >= 0 { - format!("fp+{offset}") - } else { - format!("fp-{offset}") - } + format!("fp+{offset}") } }; let updated_fp = updated_fp diff --git a/crates/packed_pcs/src/lib.rs b/crates/packed_pcs/src/lib.rs index bc1201abc..c2fba3a90 100644 --- a/crates/packed_pcs/src/lib.rs +++ b/crates/packed_pcs/src/lib.rs @@ -321,8 +321,7 @@ pub fn packed_pcs_global_statements_for_prover< assert_eq!( chunks[0].n_vars, statement.point.0.len(), - "poly: {}", - poly_index + "poly: {poly_index}" ); assert!(chunks[0].offset_in_packed.unwrap() % (1 << chunks[0].n_vars) == 0); diff --git a/crates/utils/src/multilinear.rs b/crates/utils/src/multilinear.rs index d4602739a..25ea10348 100644 --- a/crates/utils/src/multilinear.rs +++ b/crates/utils/src/multilinear.rs @@ -174,9 +174,7 @@ pub fn multilinear_eval_constants_at_right(limit: usize, point: &[F]) assert!( limit <= (1 << n_vars), - "limit {} is too large for n_vars {}", - limit, - n_vars + "limit {limit} is too large for n_vars {n_vars}" ); if limit == 1 << n_vars {