Skip to content

Commit 47583e4

Browse files
committed
fix: debugging
1 parent dd2e642 commit 47583e4

File tree

8 files changed

+215
-98
lines changed

8 files changed

+215
-98
lines changed

benchmarks/guest/fibonacci/src/main.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,46 @@ use core::ptr;
22

33
openvm::entry!(main);
44

5-
// Explicitly use the C memcpy function to ensure we're using custom memcpy
6-
extern "C" {
7-
// in rust, u8 is 1 byte of memory
8-
fn memcpy(dst: *mut u8, src: *const u8, n: usize) -> *mut u8;
9-
}
10-
11-
/// Test function that explicitly calls memcpy to verify custom implementation
12-
pub fn test_custom_memcpy(dst: &mut [u8], src: &[u8], shift: usize) {
5+
#[no_mangle]
6+
pub fn append<T>(dst: &mut [T], src: &mut [T], shift: usize) {
137
let src_len = src.len();
148
let dst_len = dst.len();
159

16-
// Bounds checking
17-
if shift + src_len > dst_len {
18-
return; // Just return on bounds error
19-
}
20-
2110
unsafe {
22-
let dst_ptr = dst.as_mut_ptr().add(shift);
11+
// The call to add is always safe because `Vec` will never
12+
// allocate more than `isize::MAX` bytes.
13+
let dst_ptr = dst.as_mut_ptr().wrapping_add(shift);
2314
let src_ptr = src.as_ptr();
15+
println!("dst_ptr: {}", dst_ptr as usize); // these have the same pointer destination (basically), in between runs
16+
println!("src_ptr: {}", src_ptr as usize);
17+
println!("src_len: {}", src_len);
2418

25-
// This will definitely use our custom memcpy implementation
26-
memcpy(dst_ptr, src_ptr, src_len);
19+
ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2720
}
2821
}
2922

3023
pub fn main() {
31-
let mut a: [u8; 1000] = [1; 1000];
32-
for i in 0..1000 {
33-
a[i] = 1 as u8;
34-
}
35-
let mut b: [u8; 500] = [2; 500];
36-
for i in 0..500 {
37-
b[i] = 2 as u8;
38-
}
39-
40-
let shift: usize = 3;
24+
const n: usize = 32;
4125

42-
// Test the custom memcpy
43-
test_custom_memcpy(&mut a, &b, shift);
26+
let mut a: [u8; 2 * n] = [0; 2 * n];
27+
let mut b: [u8; n] = [2; n];
4428

45-
for i in 0..1000 {
29+
let shift: usize = 1;
30+
for i in 0..n {
31+
b[i] = i as u8 + 1 as u8;
32+
}
33+
println!("b: {:?}", b);
34+
append(&mut a, &mut b, shift);
35+
let mut idx = 0;
36+
for i in 0..2 * n {
4637
if i < shift || i >= shift + b.len() {
47-
assert_eq!(a[i], 1);
38+
assert_eq!(a[i], 0);
4839
} else {
49-
assert_eq!(a[i], 2);
40+
assert_eq!(a[i], b[idx]);
41+
idx += 1;
5042
}
5143
}
5244

5345
println!("a: {:?}", a);
5446
println!("b: {:?}", b);
5547
}
56-
/*
57-
ok what lolll
58-
memcpy works (??)
59-
*/

crates/vm/src/arch/execution_mode/metered/ctx.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ impl<const PAGE_BITS: usize> ExecutionCtxTrait for MeteredCtx<PAGE_BITS> {
225225
impl<const PAGE_BITS: usize> MeteredExecutionCtxTrait for MeteredCtx<PAGE_BITS> {
226226
#[inline(always)]
227227
fn on_height_change(&mut self, chip_idx: usize, height_delta: u32) {
228+
if chip_idx == 10 {
229+
eprintln!(
230+
"crates/vm/src/arch/execution_mode/metered/ctx.rs::on_height_change: AIR[10] height change: {} -> {} (delta: {})",
231+
self.trace_heights[10],
232+
self.trace_heights[10].wrapping_add(height_delta),
233+
height_delta
234+
);
235+
}
228236
debug_assert!(
229237
chip_idx < self.trace_heights.len(),
230238
"chip_idx out of bounds"

crates/vm/src/arch/interpreter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,16 @@ where
760760
let pre_inst = if let Some((inst, _)) = inst_opt {
761761
tracing::trace!("get_metered_pre_compute_instruction {inst:?}");
762762
let pc = program.pc_base + i as u32 * DEFAULT_PC_STEP;
763+
println!("crates/vm/src/arch/interpreter.rs::get_metered_pre_compute_instructions: inst: opcode {:?}", inst.opcode.as_usize());
763764
if let Some(handler) = get_system_opcode_handler(inst, buf) {
764765
PreComputeInstruction {
765766
handler,
766767
pre_compute: buf,
767768
}
768769
} else if let Some(&executor_idx) = inventory.instruction_lookup.get(&inst.opcode) {
770+
if inst.opcode.as_usize() == 595 { // MULHU opcode
771+
println!("crates/vm/src/arch/interpreter.rs::get_metered_pre_compute_instructions: MULHU instruction (opcode 595) being routed to metered execution, executor_idx: {}", executor_idx);
772+
}
769773
let executor_idx = executor_idx as usize;
770774
let executor = inventory
771775
.executors

crates/vm/src/arch/record_arena.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@ impl<F: Field> MatrixRecordArena<F> {
9090
}
9191

9292
pub fn alloc_buffer(&mut self, num_rows: usize) -> &mut [u8] {
93-
let start = self.trace_offset;
93+
let start: usize = self.trace_offset;
94+
9495
self.trace_offset += num_rows * self.width;
96+
if start >= self.trace_buffer.len() {
97+
eprintln!("crates/vm/src/arch/record_arena.rs::alloc_buffer: start >= self.trace_buffer.len()");
98+
eprintln!("start = {:?}", start);
99+
eprintln!("self.trace_offset = {:?}", self.trace_offset);
100+
eprintln!("self.trace_buffer.len() = {:?}", self.trace_buffer.len());
101+
eprintln!("self.width = {:?}", self.width);
102+
eprintln!("num_rows = {:?}", num_rows);
103+
}
104+
95105
let row_slice = &mut self.trace_buffer[start..self.trace_offset];
96106
let size = size_of_val(row_slice);
97107
let ptr = row_slice as *mut [F] as *mut u8;
@@ -111,6 +121,15 @@ impl<F: Field> Arena for MatrixRecordArena<F> {
111121
fn with_capacity(height: usize, width: usize) -> Self {
112122
let height = next_power_of_two_or_zero(height);
113123
let trace_buffer = F::zero_vec(height * width);
124+
eprintln!(
125+
"crates/vm/src/arch/record_arena.rs::with_capacity: with capacity called, height = {:?}, width = {:?}",
126+
height, width
127+
);
128+
// height * width is wrong?
129+
// i think bug is here? on constructor the trace buffer
130+
// isn't allocated to be the correct size
131+
// eprintln!("height, width = {:?}, {:?}", height, width);
132+
// eprintln!("trace_buffer.len() = {:?}", trace_buffer.len());
114133
Self {
115134
trace_buffer,
116135
width,
@@ -133,6 +152,10 @@ impl<F: Field> RowMajorMatrixArena<F> for MatrixRecordArena<F> {
133152
fn set_capacity(&mut self, trace_height: usize) {
134153
let size = trace_height * self.width;
135154
// PERF: use memset
155+
// eprintln!("set_capacity called");
156+
// eprintln!("size = {:?}", size);
157+
// eprintln!("trace_height = {:?}", trace_height);
158+
// eprintln!("self.width = {:?}", self.width);
136159
self.trace_buffer.resize(size, F::ZERO);
137160
}
138161

@@ -145,6 +168,7 @@ impl<F: Field> RowMajorMatrixArena<F> for MatrixRecordArena<F> {
145168
}
146169

147170
fn into_matrix(mut self) -> RowMajorMatrix<F> {
171+
// eprintln!("into_matrix called");
148172
let width = self.width();
149173
assert_eq!(self.trace_offset() % width, 0);
150174
let rows_used = self.trace_offset() / width;
@@ -158,6 +182,7 @@ impl<F: Field> RowMajorMatrixArena<F> for MatrixRecordArena<F> {
158182
let height = self.trace_buffer.len() / width;
159183
assert!(height.is_power_of_two() || height == 0);
160184
}
185+
// eprintln!("into_matrix done");
161186
RowMajorMatrix::new(self.trace_buffer, self.width)
162187
}
163188
}
@@ -534,7 +559,12 @@ where
534559
[u8]: CustomBorrow<'a, R, MultiRowLayout<M>>,
535560
{
536561
fn alloc(&'a mut self, layout: MultiRowLayout<M>) -> R {
537-
let buffer = self.alloc_buffer(layout.metadata.get_num_rows());
562+
// alloc override of the alloc function in the trait
563+
// eprintln!(
564+
// "layout.metadata.get_num_rows() in alloc override = {:?}",
565+
// layout.metadata.get_num_rows()
566+
// );
567+
let buffer = self.alloc_buffer(layout.metadata.get_num_rows()); //allocating 2 rows
538568
let record: R = buffer.custom_borrow(layout);
539569
record
540570
}

crates/vm/src/arch/vm.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ where
457457
<VB::VmConfig as VmExecutionConfig<Val<E::SC>>>::Executor:
458458
PreflightExecutor<Val<E::SC>, VB::RecordArena>,
459459
{
460+
eprintln!("crates/vm/src/arch/vm.rs::execute_preflight");
461+
eprintln!("=== TRACE HEIGHTS PASSED TO EXECUTE_PREFLIGHT ===");
462+
for (air_idx, &height) in trace_heights.iter().enumerate() {
463+
eprintln!("AIR[{}]: trace_height={}", air_idx, height);
464+
}
460465
debug_assert!(interpreter
461466
.executor_idx_to_air_idx
462467
.iter()
@@ -473,6 +478,34 @@ where
473478
let capacities = zip_eq(trace_heights, main_widths)
474479
.map(|(&h, w)| (h as usize, w))
475480
.collect::<Vec<_>>();
481+
482+
let executor_idx_to_air_idx = self.executor_idx_to_air_idx();
483+
484+
// Debug logging for capacities and AIR mapping
485+
eprintln!("=== CAPACITY DEBUG INFO ===");
486+
for (air_idx, &(height, width)) in capacities.iter().enumerate() {
487+
eprintln!(
488+
"AIR[{}]: height={}, width={}, total_elements={}",
489+
air_idx,
490+
height,
491+
width,
492+
height * width
493+
);
494+
}
495+
496+
eprintln!("=== EXECUTOR TO AIR MAPPING ===");
497+
for (executor_idx, &air_idx) in executor_idx_to_air_idx.iter().enumerate() {
498+
eprintln!("Executor[{}] -> AIR[{}]", executor_idx, air_idx);
499+
}
500+
let executor_inventory = &self.executor().inventory;
501+
502+
// Find all opcodes that map to executor index 14
503+
eprintln!("=== OPCODES FOR EXECUTOR[14] ===");
504+
for (opcode, &executor_idx) in &executor_inventory.instruction_lookup {
505+
if executor_idx == 14 {
506+
eprintln!("Opcode {} -> Executor[{}]", opcode, executor_idx);
507+
}
508+
}
476509
let ctx = PreflightCtx::new_with_capacity(&capacities, instret_end);
477510

478511
let system_config: &SystemConfig = self.config().as_ref();
@@ -1003,7 +1036,6 @@ where
10031036
&trace_heights,
10041037
)?;
10051038
state = Some(to_state);
1006-
10071039
let mut ctx = vm.generate_proving_ctx(system_records, record_arenas)?;
10081040
modify_ctx(seg_idx, &mut ctx);
10091041
let proof = vm.engine.prove(vm.pk(), ctx);

0 commit comments

Comments
 (0)