Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/core/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ range-set-blaze = "0.1.16"

# profiling
goblin = { version = "0.9", optional = true }
gimli = { version = "0.31.1", optional = true }
object = { version = "0.36.7", optional = true }
rustc-demangle = { version = "0.1.18", optional = true }
gecko_profile = { version = "0.4.0", optional = true }
indicatif = { version = "0.17.8", optional = true }
Expand All @@ -61,6 +63,8 @@ test-artifacts = { path = "../../test-artifacts" }
bigint-rug = ["sp1-curves/bigint-rug"]
profiling = [
"dep:goblin",
"dep:gimli",
"dep:object",
"dep:rustc-demangle",
"dep:gecko_profile",
"dep:indicatif",
Expand Down
79 changes: 71 additions & 8 deletions crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ pub enum ExecutorMode {
ShapeCollection,
}

/// Represents different types of stack operations that can occur during program execution.
#[derive(Debug)]
pub enum StackEvent {
/// Indicates a push operation onto the stack.
Push,
/// Indicates a pop operation from the stack.
Pop,
/// Indicates a combined pop and push operation on the stack.
PopPush,
}

impl StackEvent {
/// Returns `true` if the event represents pushing onto the stack.
#[must_use]
pub fn is_push(&self) -> bool {
matches!(self, StackEvent::Push | StackEvent::PopPush)
}

/// Returns `true` if the event represents popping from the stack.
#[must_use]
pub fn is_pop(&self) -> bool {
matches!(self, StackEvent::Pop | StackEvent::PopPush)
}
}

/// Information about event counts which are relevant for shape fixing.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct LocalCounts {
Expand Down Expand Up @@ -1697,12 +1722,18 @@ impl<'a> Executor<'a> {
// Fetch the instruction at the current program counter.
let instruction = self.fetch();

#[cfg(feature = "profiling")]
let previous_pc = self.state.pc;

// Log the current state of the runtime.
self.log(&instruction);
self.log();

// Execute the instruction.
self.execute_instruction(&instruction)?;

#[cfg(feature = "profiling")]
self.profile(&instruction, previous_pc.into());

// Increment the clock.
self.state.global_clk += 1;

Expand Down Expand Up @@ -2319,16 +2350,48 @@ impl<'a> Executor<'a> {
}

#[inline]
fn log(&mut self, _: &Instruction) {
#[cfg(feature = "profiling")]
fn log(&self) {
if !self.unconstrained && self.state.global_clk % 10_000_000 == 0 {
tracing::info!("clk = {} pc = 0x{:x?}", self.state.global_clk, self.state.pc);
}
}

#[cfg(feature = "profiling")]
#[inline]
fn profile(&mut self, i: &Instruction, previous_pc: u64) {
if let Some((ref mut profiler, _)) = self.profiler {
if !self.unconstrained {
profiler.record(self.state.global_clk, self.state.pc as u64);
}
}
let stack_event = match i.opcode {
// A JAL instruction should push the return address onto a return-address
// stack (RAS) only when rd=x1 or x5.
Opcode::JAL => {
if i.op_a == Register::X1 as u8 || i.op_a == Register::X5 as u8 {
Some(StackEvent::Push)
} else {
None
}
}
Opcode::JALR => {
let rd_link = i.op_a == Register::X1 as u8 || i.op_a == Register::X5 as u8;
let rs1_link =
i.op_b == Register::X1 as u32 || i.op_b == Register::X5 as u32;
match (rd_link, rs1_link) {
(false, false) => None,
(false, true) => Some(StackEvent::Pop),
(true, true) if i.op_a as u32 != i.op_b => Some(StackEvent::PopPush),
(true, _) => Some(StackEvent::Push),
}
}
_ => None,
};

if !self.unconstrained && self.state.global_clk % 10_000_000 == 0 {
tracing::info!("clk = {} pc = 0x{:x?}", self.state.global_clk, self.state.pc);
profiler.record(
self.state.global_clk,
self.state.pc as u64,
previous_pc,
stack_event.as_ref(),
);
}
}
}
}
Expand Down
Loading
Loading