Skip to content

Commit

Permalink
feature: draft lifting function generation in LiftImportsCrossCtxStage
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Dec 6, 2024
1 parent 9e10242 commit c8dfae9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl MidenAbiImport {
}

/// A component import
#[derive(Debug, Clone)]
#[derive(Debug, Clone, derive_more::From)]
pub enum ComponentImport {
/// A Wasm import that is following the Wasm Component Model Canonical ABI
CanonAbiImport(CanonAbiImport),
Expand Down
1 change: 1 addition & 0 deletions midenc-compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ midenc-hir-transform.workspace = true
midenc-session.workspace = true
thiserror.workspace = true
wat.workspace = true
derive_more.workspace = true
80 changes: 69 additions & 11 deletions midenc-compile/src/stages/lift_cross_ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
use std::collections::BTreeMap;

use midenc_hir::{pass::AnalysisManager, ComponentBuilder, ComponentImport, FunctionIdent};
use midenc_hir::{
diagnostics::Severity, pass::AnalysisManager, types::Abi, AbiParam, CallConv, CanonAbiImport,
ComponentBuilder, ComponentImport, FunctionIdent, FunctionType, InstBuilder, Linkage,
MidenAbiImport, Signature, SourceSpan, Type,
};
use midenc_session::{DiagnosticsHandler, Session};

use super::LinkerInput;
Expand Down Expand Up @@ -43,13 +47,13 @@ impl Stage for LiftImportsCrossCtxStage {
lifted_imports.insert(id, import);
continue;
}
let new_import = generate_lifting_function(
let (new_import, _lifting_func_id) = generate_lifting_function(
&mut component_builder,
id,
import,
import.unwrap_canon_abi_import(),
&session.diagnostics,
)?;
lifted_imports.insert(id, new_import);
lifted_imports.insert(id, new_import.into());

// TODO: find all the calls to the component import and replace them with the generated lifting function
}
Expand All @@ -64,11 +68,65 @@ impl Stage for LiftImportsCrossCtxStage {
}

fn generate_lifting_function(
_component_builder: &mut ComponentBuilder<'_>,
_id: FunctionIdent,
import: ComponentImport,
_diagnostics: &DiagnosticsHandler,
) -> CompilerResult<ComponentImport> {
// TODO: implement the lifting function generation
Ok(import)
component_builder: &mut ComponentBuilder<'_>,
core_import_function: FunctionIdent,
import: &CanonAbiImport,
diagnostics: &DiagnosticsHandler,
) -> CompilerResult<(MidenAbiImport, FunctionIdent)> {
// get or create the module for the interface
let module_id = import.interface_function.interface.full_name;
let mut module_builder = component_builder.module(module_id);
// TODO: put the core function signature (as imported in the core module) in the component import
let import_core_sig = Signature {
params: vec![AbiParam::new(Type::Felt)],
results: vec![AbiParam::new(Type::Felt)],
cc: CallConv::SystemV,
linkage: Linkage::External,
};
let mut builder =
module_builder.function(import.interface_function.function, import_core_sig.clone())?;
let entry = builder.current_block();
let params = builder.block_params(entry).to_vec();

// TODO: analyze the signature and speculate what cross-context Miden ABI signature would
// export have.
// For now just assume passing <16 felts and returning 1 and copy the signature
let export_lowered_sig = Signature {
params: vec![AbiParam::new(Type::Felt)],
results: vec![AbiParam::new(Type::Felt)],
// TODO: add CallConv::CrossCtx
cc: CallConv::SystemV,
linkage: Linkage::External,
};
let dfg = builder.data_flow_graph_mut();
// import the Wasm core function
if dfg.get_import(&core_import_function).is_none() {
dfg.import_function(
core_import_function.module,
core_import_function.function,
export_lowered_sig.clone(),
)
.map_err(|_e| {
let message = format!(
"Lifting function with name {} in module {} with signature {export_lowered_sig:?} \
is already imported (function call) with a different signature",
core_import_function.function, core_import_function.module
);
diagnostics.diagnostic(Severity::Error).with_message(message).into_report()
})?;
}
// TODO: use the span from the caller
// TODO: lower the params from the Wasm CABI to the cross-context Miden ABI
let call = builder.ins().call(core_import_function, &params, SourceSpan::UNKNOWN);
// dbg!(&sig);
// TODO: lift the result from the cross-context Miden ABI to Wasm CABI
let result = builder.first_result(call);
builder.ins().ret(Some(result), SourceSpan::UNKNOWN);
let function_id = builder.build()?;
let component_import = MidenAbiImport::new(FunctionType {
abi: Abi::Canonical,
params: export_lowered_sig.params.into_iter().map(|p| p.ty).collect(),
results: export_lowered_sig.results.into_iter().map(|r| r.ty).collect(),
});
Ok((component_import, function_id))
}
15 changes: 8 additions & 7 deletions midenc-compile/src/stages/lower_cross_ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
use std::collections::BTreeMap;

use midenc_hir::{
diagnostics::Severity, pass::AnalysisManager, types::Abi::Canonical, AbiParam, CallConv,
ComponentBuilder, ComponentExport, FunctionType, InstBuilder, InterfaceFunctionIdent, Linkage,
Signature, SourceSpan, Type,
diagnostics::Severity,
pass::AnalysisManager,
types::Abi::{self, Canonical},
AbiParam, CallConv, ComponentBuilder, ComponentExport, FunctionType, InstBuilder,
InterfaceFunctionIdent, Linkage, Signature, SourceSpan, Type,
};
use midenc_session::{DiagnosticsHandler, Session};

Expand Down Expand Up @@ -126,9 +128,8 @@ fn generate_lowering_function(
dfg.import_function(export.function.module, export.function.function, core_sig)
.map_err(|_e| {
let message = format!(
"Function(callee of the lowering) with name {} in module {} with signature \
{cc_export_sig:?} is already imported (function call) with a different \
signature",
"Lowering function with name {} in module {} with signature {cc_export_sig:?} \
is already imported (function call) with a different signature",
export.function.function, export.function.module
);
diagnostics.diagnostic(Severity::Error).with_message(message).into_report()
Expand All @@ -145,7 +146,7 @@ fn generate_lowering_function(
let component_export = ComponentExport {
function: function_id,
function_ty: FunctionType {
abi: Canonical,
abi: Abi::Canonical,
..export.function_ty
},
..export
Expand Down

0 comments on commit c8dfae9

Please sign in to comment.