-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[mono-vm] Factor specializer pipeline out of the executable and into the specialize crate. #19454
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,28 @@ mod translate; | |
|
|
||
| pub use context::{build_func_id_map, try_build_context, LoweringContext, SlotInfo}; | ||
| pub use display::MicroOpsFunctionDisplay; | ||
| use mono_move_core::MicroOp; | ||
| use move_binary_format::file_format::IdentifierIndex; | ||
| pub use translate::lower_function; | ||
|
|
||
| /// Result of lowering a single non-generic function. | ||
| // TODO: unify with `mono_move_core::Function` once the specializer has access to arenas. | ||
| pub struct LoweredFunction { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this different from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and yes. Adding a TODO to reflect this. |
||
| /// Function name, as an index into the module's identifier pool. | ||
| pub name_idx: IdentifierIndex, | ||
| /// Gas-instrumented micro-ops. | ||
| pub code: Vec<MicroOp>, | ||
| /// Size of the argument region at the start of the frame. | ||
| pub args_size: usize, | ||
| /// Size of the arguments + locals region. | ||
| pub args_and_locals_size: usize, | ||
| /// Total frame footprint (args + locals + metadata + callee slots). | ||
| pub extended_frame_size: usize, | ||
| } | ||
|
|
||
| /// Result of lowering an entire module. | ||
| pub struct LoweredModule { | ||
| /// Per-definition-index results. `None` for functions that were | ||
| /// not lowered (e.g., generic functions). | ||
| pub functions: Vec<Option<LoweredFunction>>, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright (c) Aptos Foundation | ||
| // Licensed pursuant to the Innovation-Enabling Source Code License, available at https://github.com/aptos-labs/aptos-core/blob/main/LICENSE | ||
|
|
||
| //! High-level pipeline: destack → lower → gas instrument → frame layout. | ||
|
|
||
| use crate::{ | ||
| destack, | ||
| lower::{build_func_id_map, lower_function, try_build_context, LoweredFunction, LoweredModule}, | ||
| }; | ||
| use anyhow::Result; | ||
| use mono_move_core::{MicroOpGasSchedule, FRAME_METADATA_SIZE}; | ||
| use mono_move_gas::GasInstrumentor; | ||
| use move_binary_format::CompiledModule; | ||
| use move_vm_types::loaded_data::struct_name_indexing::StructNameIndex; | ||
|
|
||
| /// Run the full specializer pipeline: destack → lower → gas instrument → frame layout. | ||
| // TODO: extend with additional passes (e.g., monomorphization, GC safe-point layout). | ||
| pub fn destack_and_lower_module(module: CompiledModule) -> Result<LoweredModule> { | ||
| // Identity mapping: valid when loading a single module in isolation. | ||
| let struct_name_table: Vec<StructNameIndex> = (0..module.struct_handles.len()) | ||
| .map(|i| StructNameIndex::new(i as u32)) | ||
| .collect(); | ||
| let module_ir = destack(module, &struct_name_table)?; | ||
| let func_id_map = build_func_id_map(&module_ir.module); | ||
|
|
||
| let mut functions = Vec::with_capacity(module_ir.functions.len()); | ||
| for func_ir in &module_ir.functions { | ||
| let Some(func_ir) = func_ir else { | ||
| functions.push(None); | ||
| continue; | ||
| }; | ||
| let lowered = match try_build_context(&module_ir.module, func_ir, &func_id_map)? { | ||
| Some(ctx) => { | ||
| let micro_ops = lower_function(func_ir, &ctx)?; | ||
| let code = GasInstrumentor::new(MicroOpGasSchedule).run(micro_ops); | ||
|
|
||
| let args_size = ctx.home_slots[..func_ir.num_params as usize] | ||
| .iter() | ||
| .map(|s| s.size as usize) | ||
| .sum::<usize>(); | ||
| let args_and_locals_size = ctx.frame_data_size as usize; | ||
| let extended_frame_size = ctx | ||
| .call_sites | ||
| .iter() | ||
| .flat_map(|cs| cs.arg_write_slots.iter().chain(cs.ret_read_slots.iter())) | ||
| .map(|s| (s.offset + s.size) as usize) | ||
| .max() | ||
| // Leaf function: no callee slots needed beyond metadata. | ||
| .unwrap_or(args_and_locals_size + FRAME_METADATA_SIZE); | ||
|
|
||
| Some(LoweredFunction { | ||
| name_idx: func_ir.name_idx, | ||
| code, | ||
| args_size, | ||
| args_and_locals_size, | ||
| extended_frame_size, | ||
| }) | ||
| }, | ||
| None => None, | ||
| }; | ||
| functions.push(lowered); | ||
| } | ||
|
Comment on lines
+23
to
+62
|
||
|
|
||
| Ok(LoweredModule { functions }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code/comment assumes
func_ptrsis indexed by MoveFunctionDefinitionindex, but its length comes fromlowered.functions.len(). IfLoweredModule.functionsis not exactly aligned to definition indices (e.g., if the module contains native function defs that were skipped during destack/lowering),Function::resolve_callscan index the wrong slot or panic. EnsureLoweredModule.functionsis definition-indexed (same length asmodule.function_defs) or add validation that this invariant holds before callingresolve_calls.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.