Skip to content

Commit e6239b2

Browse files
committed
chore: clean up
1 parent da376b7 commit e6239b2

File tree

2 files changed

+34
-37
lines changed
  • crates/vm/src/arch/execution_mode/metered
  • extensions/rv32im/circuit/src/common

2 files changed

+34
-37
lines changed

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ pub struct MemoryCtx<const PAGE_BITS: usize> {
107107
pub boundary_idx: usize,
108108
pub merkle_tree_index: Option<usize>,
109109
pub adapter_offset: usize,
110-
pub continuations_enabled: bool,
110+
continuations_enabled: bool,
111111
chunk: u32,
112112
chunk_bits: u32,
113113
pub page_indices: BitSet,
114-
pub page_access_count: usize,
115114
pub addr_space_access_count: RVec<usize>,
116115
pub page_indices_since_checkpoint: Box<[u32]>,
117116
pub page_indices_since_checkpoint_len: usize,
@@ -140,7 +139,6 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
140139
memory_dimensions,
141140
continuations_enabled: config.continuation_enabled,
142141
page_indices: BitSet::new(bitset_size),
143-
page_access_count: 0,
144142
addr_space_access_count: vec![0; addr_space_size].into(),
145143
page_indices_since_checkpoint: vec![0; page_indices_since_checkpoint_cap]
146144
.into_boxed_slice(),
@@ -257,7 +255,10 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
257255
/// Initialize state for a new segment
258256
#[inline(always)]
259257
pub(crate) fn initialize_segment(&mut self, trace_heights: &mut [u32]) {
260-
// Reset trace heights for memory chips
258+
// Clear page indices for the new segment
259+
self.page_indices.clear();
260+
261+
// Reset trace heights for memory chips as 0
261262
// SAFETY: boundary_idx is a compile time constant within bounds
262263
unsafe {
263264
*trace_heights.get_unchecked_mut(self.boundary_idx) = 0;
@@ -277,15 +278,11 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
277278
// Apply height updates for all pages accessed since last checkpoint, and
278279
// initialize page_indices for the new segment.
279280
let mut addr_space_access_count = vec![0; self.addr_space_access_count.len()];
280-
let mut page_access_count = 0;
281-
self.page_indices.clear();
282-
283281
let pages_len = self.page_indices_since_checkpoint_len;
284282
for i in 0..pages_len {
285283
// SAFETY: i is within 0..pages_len and pages_len is the slice length.
286284
let page_id = unsafe { *self.page_indices_since_checkpoint.get_unchecked(i) } as usize;
287285
if self.page_indices.insert(page_id) {
288-
page_access_count += 1;
289286
let (addr_space, _) = self
290287
.memory_dimensions
291288
.index_to_label((page_id as u64) << PAGE_BITS);
@@ -298,9 +295,7 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
298295
}
299296
}
300297
}
301-
302-
self.apply_height_updates(trace_heights, page_access_count, &addr_space_access_count);
303-
298+
self.apply_height_updates(trace_heights, &addr_space_access_count);
304299
self.page_indices_since_checkpoint_len = 0;
305300
}
306301

@@ -312,12 +307,9 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
312307

313308
/// Apply height updates given page counts
314309
#[inline(always)]
315-
fn apply_height_updates(
316-
&self,
317-
trace_heights: &mut [u32],
318-
page_access_count: usize,
319-
addr_space_access_count: &[usize],
320-
) {
310+
fn apply_height_updates(&self, trace_heights: &mut [u32], addr_space_access_count: &[usize]) {
311+
let page_access_count = addr_space_access_count.iter().sum();
312+
321313
// On page fault, assume we add all leaves in a page
322314
let leaves = (page_access_count << PAGE_BITS) as u32;
323315
// SAFETY: boundary_idx is a compile time constant within bounds
@@ -346,7 +338,9 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
346338
}
347339
}
348340

349-
for (address_space, &x) in addr_space_access_count.iter().enumerate() {
341+
for address_space in 0..addr_space_access_count.len() {
342+
// SAFETY: address_space is from 0 to len(), guaranteed to be in bounds
343+
let x = unsafe { *addr_space_access_count.get_unchecked(address_space) };
350344
if x > 0 {
351345
// Initial **and** final handling of touched pages requires send (resp. receive) in
352346
// chunk-sized units for the merkle chip
@@ -365,13 +359,7 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
365359
/// Resolve all lazy updates of each memory access for memory adapters/poseidon2/merkle chip.
366360
#[inline(always)]
367361
pub(crate) fn lazy_update_boundary_heights(&mut self, trace_heights: &mut [u32]) {
368-
let page_access_count = self.addr_space_access_count.iter().sum();
369-
self.apply_height_updates(
370-
trace_heights,
371-
page_access_count,
372-
&self.addr_space_access_count,
373-
);
374-
self.page_access_count = 0;
362+
self.apply_height_updates(trace_heights, &self.addr_space_access_count);
375363
// SAFETY: Resetting array elements to 0 is always safe
376364
unsafe {
377365
std::ptr::write_bytes(

extensions/rv32im/circuit/src/common/mod.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,15 @@ mod aot {
212212
// let end_page_id = ((end_block_id - 1) >> PAGE_BITS) + 1;
213213

214214
// for page_id in start_page_id..end_page_id {
215+
// // Append page_id to page_indices_since_checkpoint
216+
// let len = self.page_indices_since_checkpoint_len;
217+
// // SAFETY: len is within bounds, and we extend length by 1 after writing.
218+
// unsafe {
219+
// *self.page_indices_since_checkpoint.as_mut_ptr().add(len) = page_id;
220+
// }
221+
// self.page_indices_since_checkpoint_len = len + 1;
222+
//
215223
// if self.page_indices.insert(page_id as usize) {
216-
// self.page_access_count += 1;
217224
// // SAFETY: address_space passed is usually a hardcoded constant or derived
218225
// from an // Instruction where it is bounds checked before passing
219226
// unsafe {
@@ -276,17 +283,6 @@ mod aot {
276283
page_indices_since_checkpoint_len
277284
);
278285
let inserted_label = format!(".asm_execute_pc_{pc}_inserted");
279-
// The next section is the implementation of `BitSet::insert` in ASM.
280-
// pub fn insert(&mut self, index: usize) -> bool {
281-
// let word_index = index >> 6;
282-
// let bit_index = index & 63;
283-
// let mask = 1u64 << bit_index;
284-
// let word = unsafe { self.words.get_unchecked_mut(word_index) };
285-
// let was_set = (*word & mask) != 0;
286-
// *word |= mask;
287-
// !was_set
288-
// }
289-
// self.page_indices_since_checkpoint.push(page_id);
290286

291287
// Append page_id to page_indices_since_checkpoint
292288
asm_str += &format!(
@@ -304,6 +300,18 @@ mod aot {
304300
" mov [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_len_offset}], {reg1}\n"
305301
);
306302

303+
// The next section is the implementation of `BitSet::insert` in ASM.
304+
// pub fn insert(&mut self, index: usize) -> bool {
305+
// let word_index = index >> 6;
306+
// let bit_index = index & 63;
307+
// let mask = 1u64 << bit_index;
308+
// let word = unsafe { self.words.get_unchecked_mut(word_index) };
309+
// let was_set = (*word & mask) != 0;
310+
// *word |= mask;
311+
// !was_set
312+
// }
313+
314+
// Start with `ptr_reg = index`
307315
// `reg1 = word_index`
308316
asm_str += &format!(" mov {reg1}, {ptr_reg}\n");
309317
asm_str += &format!(" shr {reg1}, 6\n");
@@ -336,6 +344,7 @@ mod aot {
336344
// self.addr_space_access_count[address_space] += 1;
337345
asm_str += &format!(" add dword ptr [{reg1} + {address_space} * 4], 1\n");
338346
asm_str += &format!("{inserted_label}:\n");
347+
// Inserted, do nothing
339348

340349
Ok(asm_str)
341350
}

0 commit comments

Comments
 (0)