Skip to content

Commit da376b7

Browse files
committed
fix: avoid stack push/pop
1 parent 76e5eb7 commit da376b7

File tree

2 files changed

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

2 files changed

+25
-27
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
190190
let end_page_id = ((end_block_id - 1) >> PAGE_BITS) + 1;
191191

192192
for page_id in start_page_id..end_page_id {
193+
// Append page_id to page_indices_since_checkpoint
194+
let len = self.page_indices_since_checkpoint_len;
195+
debug_assert!(len < self.page_indices_since_checkpoint.len());
196+
// SAFETY: len is within bounds, and we extend length by 1 after writing.
197+
unsafe {
198+
*self.page_indices_since_checkpoint.as_mut_ptr().add(len) = page_id;
199+
}
200+
self.page_indices_since_checkpoint_len = len + 1;
201+
193202
if self.page_indices.insert(page_id as usize) {
194203
// SAFETY: address_space passed is usually a hardcoded constant or derived from an
195204
// Instruction where it is bounds checked before passing
@@ -199,15 +208,6 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
199208
.get_unchecked_mut(address_space as usize) += 1;
200209
}
201210
}
202-
203-
// Append page_id to page_indices_since_checkpoint
204-
let len = self.page_indices_since_checkpoint_len;
205-
debug_assert!(len < self.page_indices_since_checkpoint.len());
206-
// SAFETY: len is within bounds, and we extend length by 1 after writing.
207-
unsafe {
208-
*self.page_indices_since_checkpoint.as_mut_ptr().add(len) = page_id;
209-
}
210-
self.page_indices_since_checkpoint_len = len + 1;
211211
}
212212
}
213213

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,22 @@ mod aot {
288288
// }
289289
// self.page_indices_since_checkpoint.push(page_id);
290290

291-
// Start with `ptr_reg = index`
292-
asm_str += &format!(" push {ptr_reg}\n");
291+
// Append page_id to page_indices_since_checkpoint
292+
asm_str += &format!(
293+
" mov {reg1}, [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_len_offset}]\n"
294+
);
295+
asm_str += &format!(
296+
" mov {reg2}, [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_ptr_offset}]\n"
297+
);
298+
let ptr_reg_32 = convert_x86_reg(ptr_reg, Width::W32).ok_or_else(|| {
299+
AotError::Other(format!("unsupported ptr_reg for 32-bit store: {ptr_reg}"))
300+
})?;
301+
asm_str += &format!(" mov dword ptr [{reg2} + {reg1} * 4], {ptr_reg_32}\n");
302+
asm_str += &format!(" add {reg1}, 1\n");
303+
asm_str += &format!(
304+
" mov [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_len_offset}], {reg1}\n"
305+
);
306+
293307
// `reg1 = word_index`
294308
asm_str += &format!(" mov {reg1}, {ptr_reg}\n");
295309
asm_str += &format!(" shr {reg1}, 6\n");
@@ -322,22 +336,6 @@ mod aot {
322336
// self.addr_space_access_count[address_space] += 1;
323337
asm_str += &format!(" add dword ptr [{reg1} + {address_space} * 4], 1\n");
324338
asm_str += &format!("{inserted_label}:\n");
325-
// Append page_id to page_indices_since_checkpoint
326-
asm_str += &format!(" pop {ptr_reg}\n");
327-
asm_str += &format!(
328-
" mov {reg1}, [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_len_offset}]\n"
329-
);
330-
asm_str += &format!(
331-
" mov {reg2}, [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_ptr_offset}]\n"
332-
);
333-
let ptr_reg_32 = convert_x86_reg(ptr_reg, Width::W32).ok_or_else(|| {
334-
AotError::Other(format!("unsupported ptr_reg for 32-bit store: {ptr_reg}"))
335-
})?;
336-
asm_str += &format!(" mov dword ptr [{reg2} + {reg1} * 4], {ptr_reg_32}\n");
337-
asm_str += &format!(" add {reg1}, 1\n");
338-
asm_str += &format!(
339-
" mov [{REG_EXEC_STATE_PTR} + {page_indices_since_checkpoint_len_offset}], {reg1}\n"
340-
);
341339

342340
Ok(asm_str)
343341
}

0 commit comments

Comments
 (0)