Skip to content

Commit

Permalink
Merge pull request #357 from 0xPolygonMiden/greenhat/i303-cross-ctx-l…
Browse files Browse the repository at this point in the history
…ower

[5/x] Lifting/Lowering for cross-context calls
  • Loading branch information
bitwalker authored Dec 17, 2024
2 parents 7c99e32 + 6e4ea6c commit 429210b
Show file tree
Hide file tree
Showing 48 changed files with 4,259 additions and 238 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.

13 changes: 10 additions & 3 deletions codegen/masm/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use miden_assembly::LibraryPath;
use miden_assembly::{ast, LibraryNamespace, LibraryPath, Span};
use midenc_hir::{
self as hir,
pass::{AnalysisManager, ConversionPass, ConversionResult},
Expand Down Expand Up @@ -108,8 +108,15 @@ impl ConversionPass for ConvertHirToMasm<hir::Module> {
} else {
ModuleKind::Library
};
let name = LibraryPath::new(&module.name).unwrap_or_else(|err| {
panic!("invalid module name '{}': {}", module.name.as_str(), err)
let name = LibraryPath::new(&module.name).unwrap_or_else(|_| {
// Fallback for Wasm CM naming `namespace:package/interface@version`
LibraryPath::new_from_components(
LibraryNamespace::Anon,
[ast::Ident::new_unchecked(Span::new(
module.name.span,
module.name.as_str().into(),
))],
)
});
let mut masm_module = Box::new(masm::Module::new(name, kind));

Expand Down
2 changes: 1 addition & 1 deletion codegen/masm/src/masm/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl Program {

// Assemble library
for module in self.library.modules.iter() {
if lib_modules.contains(&module.id.to_string()) {
if lib_modules.contains(&module.id.as_str().to_string()) {
log::warn!(
"module '{}' is already registered with the assembler as library's module, \
skipping",
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/src/component/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn translate_component(
let component_types = component_types_builder.finish();
let parsed_modules = parsed_component.static_modules;
let translator = ComponentTranslator::new(component_types, parsed_modules, config, session);
translator.translate(linearized_component_translation)
translator.translate(linearized_component_translation, session.diagnostics.as_ref())
}

fn parse<'data>(
Expand Down
37 changes: 20 additions & 17 deletions frontend-wasm/src/component/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use midenc_hir::{
MidenAbiImport, Symbol,
};
use midenc_hir_type::Abi;
use midenc_session::Session;
use midenc_session::{DiagnosticsHandler, Session};
use rustc_hash::FxHashMap;

use super::{
Expand Down Expand Up @@ -73,6 +73,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
pub fn translate(
mut self,
wasm_translation: LinearComponentTranslation,
diagnostics: &DiagnosticsHandler,
) -> WasmResult<midenc_hir::Component> {
let mut component_builder: midenc_hir::ComponentBuilder<'a> =
midenc_hir::ComponentBuilder::new(&self.session.diagnostics);
Expand All @@ -84,6 +85,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
instantiate_module,
&mut component_builder,
&wasm_translation,
diagnostics,
)?;
}
GlobalInitializer::LowerImport {
Expand Down Expand Up @@ -117,7 +119,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
}
}
for (name, export) in &wasm_translation.component.exports {
self.build_export(export, name, None, &mut component_builder)?;
self.build_export(export, name.clone(), None, &mut component_builder)?;
}
Ok(component_builder.build())
}
Expand All @@ -128,6 +130,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
instantiate_module: &InstantiateModule,
component_builder: &mut ComponentBuilder<'_>,
wasm_translation: &LinearComponentTranslation,
diagnostics: &DiagnosticsHandler,
) -> WasmResult<()> {
match instantiate_module {
InstantiateModule::Static(static_module_idx, args) => {
Expand Down Expand Up @@ -163,6 +166,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
idx,
&wasm_translation.component,
component_builder,
diagnostics,
)? {
module_args.push(arg)
}
Expand All @@ -185,10 +189,6 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
)?;
// Skip the shim and fixups core modules, they are a workaround for
// specify the core instance memory and reallod function for the lowering

// TODO:
// Imported function from the shim has empty module name and "0" as a if name.

if ir_module.name.as_str() != "wit-component:shim"
&& ir_module.name.as_str() != "wit-component:fixups"
{
Expand Down Expand Up @@ -218,6 +218,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
idx: usize,
wasm_component: &LinearComponent,
component_builder: &mut ComponentBuilder<'_>,
_diagnostics: &DiagnosticsHandler,
) -> WasmResult<Option<ModuleArgument>> {
match trampoline {
Trampoline::LowerImport {
Expand All @@ -226,16 +227,13 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
options,
} => {
let module_import = module.imports.get(idx).expect("module import not found");
let function_id = module.func_name(module_import.index.unwrap_func()).into();
let func_index = module_import.index.unwrap_func();
let function_id = module.func_name(func_index).into();
let function_id = FunctionIdent {
module: module.name(),
function: function_id,
};

// TODO:
// Find process_list_felt instead empty module name and "0" function name!
// Follow module_import.index through the shim modules/imports/exports?

let runtime_import_idx = self.lower_imports[index];
// dbg!(&module_import);
match self.translate_import(
Expand Down Expand Up @@ -300,7 +298,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
fn translate_import(
&self,
runtime_import_index: RuntimeImportIndex,
signature: TypeFuncIndex,
high_func_ty: TypeFuncIndex,
options: &CanonicalOptions,
wasm_component: &LinearComponent,
) -> WasmResult<Option<midenc_hir::ComponentImport>> {
Expand Down Expand Up @@ -338,7 +336,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
interface: InterfaceIdent::from_full_ident(&full_interface_name),
function: Symbol::intern(import_func_name),
};
let lifted_func_ty = convert_lifted_func_ty(&signature, &self.component_types);
let lifted_func_ty = convert_lifted_func_ty(&high_func_ty, &self.component_types);
let component_import =
midenc_hir::ComponentImport::CanonAbiImport(CanonAbiImport::new(
interface_function,
Expand All @@ -353,16 +351,16 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
fn build_export(
&self,
export: &Export,
name: &String,
name: String,
interface: Option<String>,
component_builder: &mut ComponentBuilder,
) -> WasmResult<()> {
match export {
Export::LiftedFunction { ty, func, options } => {
dbg!(name);
// dbg!(name);
// The inline export does no have an interface name
let interface = interface.unwrap_or_default();
dbg!(&interface);
// dbg!(&interface);
let export_name = InterfaceFunctionIdent::from_full(interface, name.clone());
let export = self.build_export_lifted_function(func, ty, options)?;
component_builder.add_export(export_name, export);
Expand All @@ -372,7 +370,12 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
let interface = Some(name.clone());
// Flatten any(nested) interface instance exports into the IR `Component` exports
for (export_name, export) in exports {
self.build_export(export, export_name, interface.clone(), component_builder)?;
self.build_export(
export,
export_name.clone(),
interface.clone(),
component_builder,
)?;
}
Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions frontend-wasm/src/intrinsics/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pub(crate) fn convert_felt_intrinsics(
let inst = builder.ins().cast(args[0], Felt, span);
vec![inst]
}
"from_u32" => {
assert_eq!(args.len(), 1, "{} takes exactly one argument", func_id);
let inst = builder.ins().bitcast(args[0], Felt, span);
vec![inst]
}
"as_u64" => {
assert_eq!(args.len(), 1, "{} takes exactly one argument", func_id);
// we're casting to i64 instead of u64 because Wasm doesn't have u64
Expand Down
6 changes: 5 additions & 1 deletion hir/src/asm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,11 @@ fn execute_call(
}
match import.signature.cc {
// For now, we're treating all calling conventions the same as SystemV
CallConv::Fast | CallConv::SystemV | CallConv::Kernel => {
CallConv::Fast
| CallConv::SystemV
| CallConv::Kernel
| CallConv::CanonLift
| CallConv::CanonLower => {
// Visit the argument list in reverse (so that the top of the stack on entry
// is the first argument), and allocate elements based on the argument types.
let mut elements_needed = 0;
Expand Down
Loading

0 comments on commit 429210b

Please sign in to comment.