Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory: Create newtype ContextId #1117

Merged
merged 17 commits into from
Oct 25, 2023
3 changes: 2 additions & 1 deletion miden/src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use miden::{
math::{Felt, StarkField},
DefaultHost, StackInputs, Word,
};
use processor::ContextId;
use rustyline::{error::ReadlineError, DefaultEditor};

/// This work is in continuation to the amazing work done by team `Scribe`
Expand Down Expand Up @@ -276,7 +277,7 @@ fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec<Felt>), ProgramErro
}

// loads the memory at the latest clock cycle.
let mem_state = chiplets.get_mem_state_at(0, system.clk());
let mem_state = chiplets.get_mem_state_at(ContextId::root(), system.clk());
// loads the stack along with the overflow values at the latest clock cycle.
let stack_state = stack.get_state_at(system.clk());

Expand Down
36 changes: 18 additions & 18 deletions miden/tests/integration/exec_iters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use processor::{AsmOpInfo, VmState};
use processor::{AsmOpInfo, ContextId, VmState};
use test_utils::{build_debug_test, Felt, ToElements, ONE};
use vm_core::{AssemblyOp, Operation};

Expand All @@ -19,7 +19,7 @@ fn test_exec_iter() {
let expected_states = vec![
VmState {
clk: 0,
ctx: 0,
ctx: ContextId::root(),
op: None,
asmop: None,
stack: [16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1].to_elements(),
Expand All @@ -28,7 +28,7 @@ fn test_exec_iter() {
},
VmState {
clk: 1,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Span),
asmop: None,
stack: [16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1].to_elements(),
Expand All @@ -37,7 +37,7 @@ fn test_exec_iter() {
},
VmState {
clk: 2,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Pad),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 3, "mem_storew.1".to_string(), false),
Expand All @@ -49,7 +49,7 @@ fn test_exec_iter() {
},
VmState {
clk: 3,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Incr),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 3, "mem_storew.1".to_string(), false),
Expand All @@ -61,7 +61,7 @@ fn test_exec_iter() {
},
VmState {
clk: 4,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::MStoreW),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 3, "mem_storew.1".to_string(), false),
Expand All @@ -73,7 +73,7 @@ fn test_exec_iter() {
},
VmState {
clk: 5,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Drop),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 4, "dropw".to_string(), false),
Expand All @@ -85,7 +85,7 @@ fn test_exec_iter() {
},
VmState {
clk: 6,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Drop),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 4, "dropw".to_string(), false),
Expand All @@ -97,7 +97,7 @@ fn test_exec_iter() {
},
VmState {
clk: 7,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Drop),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 4, "dropw".to_string(), false),
Expand All @@ -109,7 +109,7 @@ fn test_exec_iter() {
},
VmState {
clk: 8,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Drop),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 4, "dropw".to_string(), false),
Expand All @@ -121,7 +121,7 @@ fn test_exec_iter() {
},
VmState {
clk: 9,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Push(Felt::new(17))),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("#main".to_string(), 1, "push.17".to_string(), false),
Expand All @@ -133,7 +133,7 @@ fn test_exec_iter() {
},
VmState {
clk: 10,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Noop),
asmop: None,
stack: [17, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0].to_elements(),
Expand All @@ -142,7 +142,7 @@ fn test_exec_iter() {
},
VmState {
clk: 11,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Push(ONE)),
asmop: None,
stack: [1, 17, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0].to_elements(),
Expand All @@ -151,7 +151,7 @@ fn test_exec_iter() {
},
VmState {
clk: 12,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::FmpUpdate),
asmop: None,
stack: [17, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0].to_elements(),
Expand All @@ -160,7 +160,7 @@ fn test_exec_iter() {
},
VmState {
clk: 13,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Pad),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("foo".to_string(), 4, "loc_store.0".to_string(), false),
Expand All @@ -172,7 +172,7 @@ fn test_exec_iter() {
},
VmState {
clk: 14,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::FmpAdd),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("foo".to_string(), 4, "loc_store.0".to_string(), false),
Expand All @@ -185,7 +185,7 @@ fn test_exec_iter() {
},
VmState {
clk: 15,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::MStore),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("foo".to_string(), 4, "loc_store.0".to_string(), false),
Expand All @@ -200,7 +200,7 @@ fn test_exec_iter() {
},
VmState {
clk: 16,
ctx: 0,
ctx: ContextId::root(),
op: Some(Operation::Drop),
asmop: Some(AsmOpInfo::new(
AssemblyOp::new("foo".to_string(), 4, "loc_store.0".to_string(), false),
Expand Down
2 changes: 1 addition & 1 deletion processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ miden-air = { package = "miden-air", path = "../air", version = "0.8", default-f
winter-prover = { package = "winter-prover", version = "0.6", default-features = false }

[dev-dependencies]
logtest = { version = "2.0", default-features = false }
logtest = { version = "2.0", default-features = false }
miden-assembly = { package = "miden-assembly", path = "../assembly", version = "0.8", default-features = false }
test-utils = { package = "miden-test-utils", path = "../test-utils" }
winter-fri = { package = "winter-fri", version = "0.6" }
Expand Down
19 changes: 10 additions & 9 deletions processor/src/chiplets/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
BTreeMap, ChipletsBus, ColMatrix, Felt, FieldElement, RangeChecker, StarkField, TraceFragment,
Vec, Word, EMPTY_WORD, ONE,
};
use crate::system::ContextId;
use miden_air::trace::chiplets::memory::{
ADDR_COL_IDX, CLK_COL_IDX, CTX_COL_IDX, D0_COL_IDX, D1_COL_IDX, D_INV_COL_IDX, V_COL_RANGE,
};
Expand Down Expand Up @@ -74,7 +75,7 @@ const INIT_MEM_VALUE: Word = EMPTY_WORD;
#[derive(Default)]
pub struct Memory {
/// Memory segment traces sorted by their execution context ID.
trace: BTreeMap<u32, MemorySegmentTrace>,
trace: BTreeMap<ContextId, MemorySegmentTrace>,

/// Total number of entries in the trace (across all contexts); tracked separately so that we
/// don't have to sum up lengths of all address trace vectors for all contexts all the time.
Expand All @@ -96,7 +97,7 @@ impl Memory {
///
/// Unlike read() which modifies the memory access trace, this method returns the value at the
/// specified address (if one exists) without altering the memory access trace.
pub fn get_value(&self, ctx: u32, addr: u32) -> Option<Word> {
pub fn get_value(&self, ctx: ContextId, addr: u32) -> Option<Word> {
match self.trace.get(&ctx) {
Some(segment) => segment.get_value(addr),
None => None,
Expand All @@ -105,15 +106,15 @@ impl Memory {

/// Returns the word at the specified context/address which should be used as the "old value" for a
/// write request. It will be the previously stored value, if one exists, or initialized memory.
pub fn get_old_value(&self, ctx: u32, addr: u32) -> Word {
pub fn get_old_value(&self, ctx: ContextId, addr: u32) -> Word {
// get the stored word or return [0, 0, 0, 0], since the memory is initialized with zeros
self.get_value(ctx, addr).unwrap_or(INIT_MEM_VALUE)
}

/// Returns the entire memory state for the specified execution context at the specified cycle.
/// The state is returned as a vector of (address, value) tuples, and includes addresses which
/// have been accessed at least once.
pub fn get_state_at(&self, ctx: u32, clk: u32) -> Vec<(u64, Word)> {
pub fn get_state_at(&self, ctx: ContextId, clk: u32) -> Vec<(u64, Word)> {
if clk == 0 {
return vec![];
}
Expand All @@ -131,13 +132,13 @@ impl Memory {
///
/// If the specified address hasn't been previously written to, four ZERO elements are
/// returned. This effectively implies that memory is initialized to ZERO.
pub fn read(&mut self, ctx: u32, addr: u32, clk: u32) -> Word {
pub fn read(&mut self, ctx: ContextId, addr: u32, clk: u32) -> Word {
self.num_trace_rows += 1;
self.trace.entry(ctx).or_default().read(addr, Felt::from(clk))
}

/// Writes the provided word at the specified context/address.
pub fn write(&mut self, ctx: u32, addr: u32, clk: u32, value: Word) {
pub fn write(&mut self, ctx: ContextId, addr: u32, clk: u32, value: Word) {
self.num_trace_rows += 1;
self.trace.entry(ctx).or_default().write(addr, Felt::from(clk), value);
}
Expand Down Expand Up @@ -168,7 +169,7 @@ impl Memory {

// compute delta as difference between context IDs, addresses, or clock cycles
let delta = if prev_ctx != ctx {
(ctx - prev_ctx) as u64
(u32::from(ctx) - u32::from(prev_ctx)).into()
} else if prev_addr != addr {
(addr - prev_addr) as u64
} else {
Expand Down Expand Up @@ -270,7 +271,7 @@ impl Memory {

/// Returns the context, address, and clock cycle of the first trace row, or None if the trace
/// is empty.
fn get_first_row_info(&self) -> Option<(u32, u32, Felt)> {
fn get_first_row_info(&self) -> Option<(ContextId, u32, Felt)> {
let (ctx, segment) = match self.trace.iter().next() {
Some((&ctx, segment)) => (ctx, segment),
None => return None,
Expand Down Expand Up @@ -316,7 +317,7 @@ impl MemoryLookup {
}
}

pub fn from_ints(label: u8, ctx: u32, addr: u32, clk: u32, word: Word) -> Self {
pub fn from_ints(label: u8, ctx: ContextId, addr: u32, clk: u32, word: Word) -> Self {
Self {
label,
ctx: Felt::from(ctx),
Expand Down
Loading