Skip to content

Commit

Permalink
remove invocation method from component imports/exports
Browse files Browse the repository at this point in the history
since at component boundaries every function is invoked via `call`.
  • Loading branch information
greenhat committed Feb 29, 2024
1 parent 5c2cc0d commit a77c8c8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 100 deletions.
28 changes: 3 additions & 25 deletions frontend-wasm/src/component/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ mod tests {
use miden_hir_type::Type;

use crate::{
component::StaticModuleIndex,
config::{ExportMetadata, ImportMetadata},
test_utils::test_diagnostics,
component::StaticModuleIndex, config::ImportMetadata, test_utils::test_diagnostics,
};

use super::*;
Expand Down Expand Up @@ -105,18 +103,7 @@ mod tests {
);
let wasm = wat::parse_str(wat).unwrap();
let diagnostics = test_diagnostics();
let export_metadata = [(
Symbol::intern("add").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();
let config = WasmTranslationConfig {
export_metadata,
..Default::default()
};
let config = Default::default();
let (mut component_types_builder, parsed_component) =
parse(&config, &wasm, &diagnostics).unwrap();
let component_translation =
Expand Down Expand Up @@ -208,22 +195,13 @@ mod tests {
interface_function_ident.clone(),
ImportMetadata {
digest: RpoDigest::default(),
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();
let export_metadata = [(
Symbol::intern("inc").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();

let config = WasmTranslationConfig {
import_metadata,
export_metadata,
..Default::default()
};
let (mut component_types_builder, parsed_component) =
Expand Down
15 changes: 3 additions & 12 deletions frontend-wasm/src/component/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use miden_diagnostics::DiagnosticsHandler;
use rustc_hash::FxHashMap;

use miden_hir::{
cranelift_entity::PrimaryMap, ComponentBuilder, ComponentExport, FunctionExportName,
FunctionIdent, Ident, InterfaceFunctionIdent, InterfaceIdent, Symbol,
cranelift_entity::PrimaryMap, ComponentBuilder, ComponentExport, FunctionIdent, Ident,
InterfaceFunctionIdent, InterfaceIdent, Symbol,
};
use miden_hir_type::LiftedFunctionType;

Expand Down Expand Up @@ -272,7 +272,6 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
let component_import = miden_hir::ComponentImport {
function_ty: lifted_func_ty,
interface_function,
invoke_method: import_metadata.invoke_method,
digest: import_metadata.digest.clone(),
};
Ok(component_import)
Expand All @@ -288,7 +287,7 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
match export {
Export::LiftedFunction { ty, func, options } => {
let export_name = Symbol::intern(name).into();
let export = self.build_export_lifted_function(&export_name, func, ty, options)?;
let export = self.build_export_lifted_function(func, ty, options)?;
component_builder.add_export(export_name, export);
Ok(())
}
Expand Down Expand Up @@ -316,7 +315,6 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
/// Build an IR Component export from the given lifted Wasm core module function export
fn build_export_lifted_function(
&self,
function_export_name: &FunctionExportName,
func: &CoreDef,
ty: &TypeFuncIndex,
options: &CanonicalOptions,
Expand Down Expand Up @@ -361,16 +359,9 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
}
};
let lifted_func_ty = convert_lifted_func_ty(ty, &self.component_types);
let Some(export_metadata) = self.config.export_metadata.get(function_export_name) else {
return Err(WasmError::MissingExportMetadata(format!(
"Export metadata for interface function {:?} not found",
function_export_name,
)));
};
let export = miden_hir::ComponentExport {
function: func_ident,
function_ty: lifted_func_ty,
invoke_method: export_metadata.invoke_method,
};
Ok(export)
}
Expand Down
15 changes: 1 addition & 14 deletions frontend-wasm/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use miden_core::crypto::hash::RpoDigest;
use miden_hir::{FunctionExportName, FunctionInvocationMethod, InterfaceFunctionIdent};
use miden_hir::InterfaceFunctionIdent;
use rustc_hash::FxHashMap;

/// Represents Miden VM codegen metadata for a function import.
Expand All @@ -9,15 +9,6 @@ use rustc_hash::FxHashMap;
pub struct ImportMetadata {
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
/// The method of calling the function
pub invoke_method: FunctionInvocationMethod,
}

/// Represents function export metadata
#[derive(Debug, Clone)]
pub struct ExportMetadata {
/// The method of calling the function
pub invoke_method: FunctionInvocationMethod,
}

/// Configuration for the WASM translation.
Expand All @@ -37,9 +28,6 @@ pub struct WasmTranslationConfig {
/// each imported function. Having it here might be a temporary solution,
/// later we might want to move it to Wasm custom section.
pub import_metadata: FxHashMap<InterfaceFunctionIdent, ImportMetadata>,

/// Export metadata for calling convention, etc.
pub export_metadata: FxHashMap<FunctionExportName, ExportMetadata>,
}

impl Default for WasmTranslationConfig {
Expand All @@ -49,7 +37,6 @@ impl Default for WasmTranslationConfig {
generate_native_debuginfo: false,
parse_wasm_debuginfo: false,
import_metadata: Default::default(),
export_metadata: Default::default(),
}
}
}
4 changes: 0 additions & 4 deletions hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ pub struct ComponentImport {
pub interface_function: InterfaceFunctionIdent,
/// The component(lifted) type of the imported function
pub function_ty: LiftedFunctionType,
/// The method of calling the function
pub invoke_method: FunctionInvocationMethod,
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
}
Expand Down Expand Up @@ -67,8 +65,6 @@ pub struct ComponentExport {
pub function: FunctionIdent,
/// The component(lifted) type of the exported function
pub function_ty: LiftedFunctionType,
/// The method of calling the function
pub invoke_method: FunctionInvocationMethod,
}

/// A [Component] is a collection of [Module]s that are being compiled together as a package and have exports/imports.
Expand Down
25 changes: 2 additions & 23 deletions tests/integration/src/rust_masm_tests/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::CompilerTest;
use expect_test::expect_file;
use miden_core::crypto::hash::RpoDigest;
use miden_frontend_wasm::ExportMetadata;
use miden_frontend_wasm::ImportMetadata;
use miden_frontend_wasm::WasmTranslationConfig;
use miden_hir::InterfaceFunctionIdent;
Expand All @@ -13,18 +12,7 @@ use miden_hir::Type;
#[test]
fn wcm_add() {
// Has no imports
let export_metadata = [(
Symbol::intern("add").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();
let config = WasmTranslationConfig {
export_metadata,
..Default::default()
};
let config = Default::default();
let mut test = CompilerTest::rust_source_cargo_component("add-comp", config);
let artifact_name = test.source.artifact_name();
test.expect_wasm(expect_file![format!(
Expand All @@ -48,22 +36,13 @@ fn wcm_inc() {
interface_function_ident.clone(),
ImportMetadata {
digest: RpoDigest::default(),
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();
let export_metadata = [(
Symbol::intern("inc").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
)]
.into_iter()
.collect();

let config = WasmTranslationConfig {
import_metadata,
export_metadata,
..Default::default()
};
let mut test = CompilerTest::rust_source_cargo_component("inc-comp", config);
Expand Down
27 changes: 5 additions & 22 deletions tests/integration/src/rust_masm_tests/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::CompilerTest;
use expect_test::expect_file;
use miden_core::crypto::hash::RpoDigest;
use miden_frontend_wasm::ExportMetadata;
use miden_frontend_wasm::ImportMetadata;
use miden_frontend_wasm::WasmTranslationConfig;
use miden_hir::FunctionExportName;
Expand Down Expand Up @@ -38,45 +37,29 @@ fn sdk_basic_wallet() {
create_note_ident.clone(),
ImportMetadata {
digest: RpoDigest::default(),
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
),
(
remove_asset_ident.clone(),
ImportMetadata {
digest: RpoDigest::default(),
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
),
(
add_asset_ident.clone(),
ImportMetadata {
digest: RpoDigest::default(),
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
),
]
.into_iter()
.collect();
let export_metadata: FxHashMap<FunctionExportName, ExportMetadata> = [
(
Symbol::intern("send-asset").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
),
(
Symbol::intern("receive-asset").into(),
ExportMetadata {
invoke_method: miden_hir::FunctionInvocationMethod::Call,
},
),
]
.into_iter()
.collect();
let expected_exports: Vec<FunctionExportName> = vec![
Symbol::intern("send-asset").into(),
Symbol::intern("receive-asset").into(),
];
let config = WasmTranslationConfig {
import_metadata: import_metadata.clone(),
export_metadata: export_metadata.clone(),
..Default::default()
};
let mut test = CompilerTest::rust_source_cargo_component("sdk/basic-wallet", config);
Expand All @@ -91,7 +74,7 @@ fn sdk_basic_wallet() {
for (_, import) in ir.imports() {
assert!(import_metadata.contains_key(&import.interface_function));
}
for (name, _meta) in export_metadata {
for name in expected_exports {
assert!(ir.exports().contains_key(&name));
}
}
Expand Down

0 comments on commit a77c8c8

Please sign in to comment.