Skip to content

Commit

Permalink
refactor: switch to Package without rodata,
Browse files Browse the repository at this point in the history
which is now stored as `MastForest::advice_map`
  • Loading branch information
greenhat committed Nov 20, 2024
1 parent 92fca7a commit 9c4c9b3
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 88 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ miden-parsing = "0.1"
# miden-processor = { version = "0.10.3" }
# miden-stdlib = { version = "0.10.3", features = ["with-debug-info"] }

# miden-assembly = { version = "0.11.0", path = "../miden-vm-1/assembly" }
# miden-core = { version = "0.11.0", path = "../miden-vm-1/core" }
# miden-processor = { version = "0.11.0", path = "../miden-vm-1/processor" }
# miden-stdlib = { version = "0.11.0", path = "../miden-vm-1/stdlib" }
# miden-package = { version = "0.11.0", path = "../miden-vm-1/package" }
# miden-assembly = { version = "0.11.0", path = "../vm-aux/assembly" }
# miden-core = { version = "0.11.0", path = "../vm-aux/core" }
# miden-processor = { version = "0.11.0", path = "../vm-aux/processor" }
# miden-stdlib = { version = "0.11.0", path = "../vm-aux/stdlib" }
# miden-package = { version = "0.11.0", path = "../vm-aux/package" }

miden-assembly = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211f34c35116e4bb39a0da4279a7c421bcafb634" }
miden-core = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211f34c35116e4bb39a0da4279a7c421bcafb634" }
miden-processor = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211f34c35116e4bb39a0da4279a7c421bcafb634" }
miden-stdlib = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211f34c35116e4bb39a0da4279a7c421bcafb634", features = [
miden-assembly = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "fe4afa2ad655772cd1ef1c30d014c383731ab293" }
miden-core = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "fe4afa2ad655772cd1ef1c30d014c383731ab293" }
miden-processor = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "fe4afa2ad655772cd1ef1c30d014c383731ab293" }
miden-stdlib = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "fe4afa2ad655772cd1ef1c30d014c383731ab293", features = [
"with-debug-info",
] }
miden-package = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211f34c35116e4bb39a0da4279a7c421bcafb634" }
miden-package = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "fe4afa2ad655772cd1ef1c30d014c383731ab293" }

#miden-assembly = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-core = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
Expand Down
21 changes: 0 additions & 21 deletions codegen/masm/src/masm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,3 @@ impl NativePtr {
(self.waddr * 16) + (self.index as u32 * 4) + self.offset as u32
}
}

impl From<miden_package::PtrDesc> for NativePtr {
fn from(value: miden_package::PtrDesc) -> Self {
NativePtr {
waddr: value.waddr,
index: value.index,
offset: value.offset,
addrspace: midenc_hir::AddressSpace::Unknown,
}
}
}

impl From<NativePtr> for miden_package::PtrDesc {
fn from(value: NativePtr) -> Self {
miden_package::PtrDesc {
waddr: value.waddr,
index: value.index,
offset: value.offset,
}
}
}
19 changes: 16 additions & 3 deletions codegen/masm/src/masm/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use miden_assembly::{
ast::{ModuleKind, ProcedureName},
KernelLibrary, Library as CompiledLibrary, LibraryNamespace,
};
use miden_core::crypto::hash::Rpo256;
use miden_core::{crypto::hash::Rpo256, AdviceMap};
use midenc_hir::{
self as hir, diagnostics::Report, DataSegmentTable, Felt, FieldElement, FunctionIdent,
GlobalVariableTable, Ident, SourceSpan,
Expand Down Expand Up @@ -279,7 +279,14 @@ impl Program {
let emit_test_harness = session.get_flag("test_harness");
let main = self.generate_main(self.entrypoint, emit_test_harness);
let main = main.to_ast(debug_mode).map(Box::new)?;
assembler.assemble_program(main).map(Arc::new)
let program = assembler.assemble_program(main)?;
let advice_map: AdviceMap = self
.rodatas()
.iter()
.map(|rodata| (rodata.digest, rodata.to_elements()))
.collect();
let new_prog = program.with_advice_map(advice_map);
Ok(Arc::new(new_prog))
}

pub(crate) fn library(&self) -> &Library {
Expand Down Expand Up @@ -486,7 +493,13 @@ impl Library {
let module = module.to_ast(debug_mode).map(Box::new)?;
modules.push(module);
}
assembler.assemble_library(modules).map(Arc::new)
let lib = assembler.assemble_library(modules)?;
let advice_map: AdviceMap = self
.rodatas()
.iter()
.map(|rodata| (rodata.digest, rodata.to_elements()))
.collect();
Ok(Arc::new(lib.with_advice_map(advice_map)))
}
}

Expand Down
24 changes: 2 additions & 22 deletions codegen/masm/src/masm/rodata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Rodata {
/// The resulting felts will be in padded out to the nearest number of words, i.e. if the data
/// only takes up 3 felts worth of bytes, then the resulting `Vec` will contain 4 felts, so that
/// the total size is a valid number of words.
pub fn to_elements(&self) -> Result<Vec<miden_processor::Felt>, String> {
pub fn to_elements(&self) -> Vec<miden_processor::Felt> {
use miden_core::FieldElement;
use miden_processor::Felt;

Expand All @@ -65,26 +65,6 @@ impl Rodata {
let padding = (self.size_in_words() * 4).abs_diff(felts.len());
felts.resize(felts.len() + padding, Felt::ZERO);

Ok(felts)
}
}

impl From<miden_package::Rodata> for Rodata {
fn from(pkg_rodata: miden_package::Rodata) -> Self {
Rodata {
digest: pkg_rodata.digest,
start: pkg_rodata.start.into(),
data: Arc::new(pkg_rodata.data.into()),
}
}
}

impl From<Rodata> for miden_package::Rodata {
fn from(rodata: Rodata) -> Self {
miden_package::Rodata {
digest: rodata.digest,
start: rodata.start.into(),
data: <midenc_hir::ConstantData as Clone>::clone(&rodata.data).into(),
}
felts
}
}
2 changes: 1 addition & 1 deletion midenc-compile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn compile(session: Rc<Session>) -> CompilerResult<()> {
log::info!(
"succesfully assembled mast package '{}' with digest {}",
package.name,
DisplayHex::new(&package.digest.as_bytes())
DisplayHex::new(&package.digest().as_bytes())
);
session
.emit(OutputMode::Text, package)
Expand Down
9 changes: 0 additions & 9 deletions midenc-compile/src/stages/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl Stage for AssembleStage {

fn build_package(mast: MastArtifact, masm: &MasmArtifact, session: &Session) -> Package {
let name = session.name.clone();
let digest = mast.digest();

let mut dependencies = Vec::new();
for link_lib in session.options.link_libraries.iter() {
Expand All @@ -97,12 +96,6 @@ fn build_package(mast: MastArtifact, masm: &MasmArtifact, session: &Session) ->
dependencies,
};

// Gater all of the rodata segments for this package
let rodata = match masm {
MasmArtifact::Executable(ref prog) => prog.rodatas().to_vec(),
MasmArtifact::Library(ref lib) => lib.rodatas().to_vec(),
};

// Gather all of the procedure metadata for exports of this package
if let MastArtifact::Library(ref lib) = mast {
let MasmArtifact::Library(ref _masm_lib) = masm else {
Expand All @@ -121,9 +114,7 @@ fn build_package(mast: MastArtifact, masm: &MasmArtifact, session: &Session) ->

miden_package::Package {
name,
digest,
mast,
rodata: rodata.into_iter().map(Into::into).collect(),
manifest,
}
}
13 changes: 5 additions & 8 deletions midenc-debug/src/exec/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Executor {
log::debug!(
"creating executor for package '{}' (digest={})",
package.name,
DisplayHex::new(&package.digest.as_bytes())
DisplayHex::new(&package.digest().as_bytes())
);

let mut exec = Self::new(args);
Expand Down Expand Up @@ -83,10 +83,6 @@ impl Executor {
}
}

let advice_map = package.advice_map();
let advice_inputs = AdviceInputs::default().with_map(advice_map);
exec.with_advice_inputs(advice_inputs);

log::debug!("executor created");

Ok(exec)
Expand Down Expand Up @@ -135,9 +131,10 @@ impl Executor {
assertion_events.borrow_mut().insert(clk, event);
});

let mut process = Process::new_debug(program.kernel().clone(), self.stack, host);
let root_context = process.ctx();
let result = process.execute(program);
let mut process = Process::new_debug(program.kernel().clone(), self.stack);
let process_state: ProcessState = (&process).into();
let root_context = process_state.ctx();
let result = process.execute(program, &mut host);
let mut iter = VmStateIterator::new(process, result.clone());
let mut callstack = CallStack::new(trace_events);
DebugExecutor {
Expand Down
24 changes: 17 additions & 7 deletions midenc-debug/src/exec/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ impl DebuggerHost {
}

impl Host for DebuggerHost {
fn get_advice<P: ProcessState>(
type AdviceProvider = MemAdviceProvider;

fn get_advice(
&mut self,
process: &P,
process: ProcessState,
extractor: AdviceExtractor,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.get_advice(process, &extractor)
}

fn set_advice<P: ProcessState>(
fn set_advice(
&mut self,
process: &P,
process: ProcessState,
injector: AdviceInjector,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.set_advice(process, &injector)
Expand All @@ -76,9 +78,9 @@ impl Host for DebuggerHost {
self.store.get(node_digest)
}

fn on_trace<S: ProcessState>(
fn on_trace(
&mut self,
process: &S,
process: ProcessState,
trace_id: u32,
) -> Result<HostResponse, ExecutionError> {
let event = TraceEvent::from(trace_id);
Expand All @@ -91,7 +93,7 @@ impl Host for DebuggerHost {
Ok(HostResponse::None)
}

fn on_assert_failed<S: ProcessState>(&mut self, process: &S, err_code: u32) -> ExecutionError {
fn on_assert_failed(&mut self, process: ProcessState, err_code: u32) -> ExecutionError {
let clk = process.clk();
if let Some(handler) = self.on_assert_failed.as_mut() {
handler(clk, TraceEvent::AssertionFailed(core::num::NonZeroU32::new(err_code)));
Expand All @@ -110,4 +112,12 @@ impl Host for DebuggerHost {
err_msg,
}
}

fn advice_provider(&self) -> &Self::AdviceProvider {
&self.adv_provider
}

fn advice_provider_mut(&mut self) -> &mut Self::AdviceProvider {
&mut self.adv_provider
}
}
2 changes: 1 addition & 1 deletion midenc-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn run_noninteractively(

println!(
"Executed program with hash {} in {}",
state.package.digest.to_hex(),
state.package.digest().to_hex(),
HumanDuration::from(state.execution_duration),
);

Expand Down

0 comments on commit 9c4c9b3

Please sign in to comment.