Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions crates/environ/src/component/translate/adapt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@
//! time this may want to be revisited if too many adapter modules are being
//! created.

use crate::EntityType;
use crate::component::translate::*;
use crate::fact;
use crate::{EntityType, Memory};
use std::collections::HashSet;

/// Metadata information about a fused adapter.
Expand Down Expand Up @@ -150,10 +150,8 @@ pub enum DataModel {

/// Data is stored in a linear memory.
LinearMemory {
/// An optional memory definition supplied.
memory: Option<dfg::CoreExport<MemoryIndex>>,
/// If `memory` is specified, whether it's a 64-bit memory.
memory64: bool,
/// An optional memory definition supplied, and its type.
memory: Option<(dfg::CoreExport<MemoryIndex>, Memory)>,
/// An optional definition of `realloc` to used.
realloc: Option<dfg::CoreDef>,
},
Expand Down Expand Up @@ -416,12 +414,8 @@ impl PartitionAdapterModules {
DataModel::Gc {} => {
// Nothing to do here yet.
}
DataModel::LinearMemory {
memory,
memory64: _,
realloc,
} => {
if let Some(memory) = memory {
DataModel::LinearMemory { memory, realloc } => {
if let Some((memory, _ty)) = memory {
self.core_export(dfg, memory);
}
if let Some(def) = realloc {
Expand Down
48 changes: 14 additions & 34 deletions crates/environ/src/component/translate/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
//! final `Component`.

use crate::component::translate::*;
use crate::{EntityType, IndexType};
use crate::{EntityType, Memory};
use core::str::FromStr;
use std::borrow::Cow;
use wasmparser::component_types::{ComponentAnyTypeId, ComponentCoreModuleTypeId};
Expand Down Expand Up @@ -1520,34 +1520,25 @@ impl<'a> Inliner<'a> {
frame: &InlinerFrame<'a>,
types: &ComponentTypesBuilder,
memory: MemoryIndex,
) -> (dfg::CoreExport<MemoryIndex>, bool) {
) -> (dfg::CoreExport<MemoryIndex>, Memory) {
let memory = frame.memories[memory].clone().map_index(|i| match i {
EntityIndex::Memory(i) => i,
_ => unreachable!(),
});
let memory64 = match &self.runtime_instances[memory.instance] {
let ty = match &self.runtime_instances[memory.instance] {
InstanceModule::Static(idx) => match &memory.item {
ExportItem::Index(i) => {
let ty = &self.nested_modules[*idx].module.memories[*i];
match ty.idx_type {
IndexType::I32 => false,
IndexType::I64 => true,
}
}
ExportItem::Index(i) => self.nested_modules[*idx].module.memories[*i],
ExportItem::Name(_) => unreachable!(),
},
InstanceModule::Import(ty) => match &memory.item {
ExportItem::Name(name) => match types[*ty].exports[name] {
EntityType::Memory(m) => match m.idx_type {
IndexType::I32 => false,
IndexType::I64 => true,
},
EntityType::Memory(m) => m,
_ => unreachable!(),
},
ExportItem::Index(_) => unreachable!(),
},
};
(memory, memory64)
(memory, ty)
}

/// Translates a `LocalCanonicalOptions` which indexes into the `frame`
Expand All @@ -1562,18 +1553,9 @@ impl<'a> Inliner<'a> {
let data_model = match options.data_model {
LocalDataModel::Gc {} => DataModel::Gc {},
LocalDataModel::LinearMemory { memory, realloc } => {
let (memory, memory64) = memory
.map(|i| {
let (memory, memory64) = self.memory(frame, types, i);
(Some(memory), memory64)
})
.unwrap_or((None, false));
let memory = memory.map(|i| self.memory(frame, types, i));
let realloc = realloc.map(|i| frame.funcs[i].1.clone());
DataModel::LinearMemory {
memory,
memory64,
realloc,
}
DataModel::LinearMemory { memory, realloc }
}
};
let callback = options.callback.map(|i| frame.funcs[i].1.clone());
Expand Down Expand Up @@ -1603,14 +1585,12 @@ impl<'a> Inliner<'a> {
fn canonical_options(&mut self, options: AdapterOptions) -> dfg::OptionsId {
let data_model = match options.data_model {
DataModel::Gc {} => dfg::CanonicalOptionsDataModel::Gc {},
DataModel::LinearMemory {
memory,
memory64: _,
realloc,
} => dfg::CanonicalOptionsDataModel::LinearMemory {
memory: memory.map(|export| self.result.memories.push(export)),
realloc: realloc.map(|def| self.result.reallocs.push(def)),
},
DataModel::LinearMemory { memory, realloc } => {
dfg::CanonicalOptionsDataModel::LinearMemory {
memory: memory.map(|(export, _)| self.result.memories.push(export)),
realloc: realloc.map(|def| self.result.reallocs.push(def)),
}
}
};
let callback = options.callback.map(|def| self.result.callbacks.push(def));
let post_return = options
Expand Down
87 changes: 51 additions & 36 deletions crates/environ/src/fact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@

use crate::component::dfg::CoreDef;
use crate::component::{
Adapter, AdapterOptions as AdapterOptionsDfg, ComponentTypesBuilder, FlatType, InterfaceType,
RuntimeComponentInstanceIndex, StringEncoding, Transcode, TypeFuncIndex,
Adapter, AdapterOptions as AdapterOptionsDfg, CanonicalAbiInfo, ComponentTypesBuilder,
FlatType, InterfaceType, RuntimeComponentInstanceIndex, StringEncoding, Transcode,
TypeFuncIndex,
};
use crate::fact::transcode::Transcoder;
use crate::{EntityRef, FuncIndex, GlobalIndex, MemoryIndex, PrimaryMap, Tunables};
use crate::{ModuleInternedTypeIndex, prelude::*};
use crate::prelude::*;
use crate::{
EntityRef, FuncIndex, GlobalIndex, IndexType, Memory, MemoryIndex, ModuleInternedTypeIndex,
PrimaryMap, Tunables,
};
use std::collections::HashMap;
use wasm_encoder::*;

Expand Down Expand Up @@ -142,27 +146,40 @@ struct AdapterOptions {
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
/// Linear memory.
struct LinearMemoryOptions {
/// Whether or not the `memory` field, if present, is a 64-bit memory.
memory64: bool,
/// An optionally-specified memory where values may travel through for
/// types like lists.
memory: Option<MemoryIndex>,
memory: Option<(MemoryIndex, Memory)>,
/// An optionally-specified function to be used to allocate space for
/// types such as strings as they go into a module.
realloc: Option<FuncIndex>,
}

impl LinearMemoryOptions {
fn ptr(&self) -> ValType {
if self.memory64 {
if self.memory64() {
ValType::I64
} else {
ValType::I32
}
}

fn ptr_size(&self) -> u8 {
if self.memory64 { 8 } else { 4 }
if self.memory64() { 8 } else { 4 }
}

fn memory64(&self) -> bool {
self.memory
.as_ref()
.map(|(_, ty)| ty.idx_type == IndexType::I64)
.unwrap_or(false)
}

fn sizealign(&self, abi: &CanonicalAbiInfo) -> (u32, u32) {
if self.memory64() {
(abi.size64, abi.align64)
} else {
(abi.size32, abi.align32)
}
}
}

Expand Down Expand Up @@ -345,30 +362,32 @@ impl<'a> Module<'a> {

let data_model = match data_model {
crate::component::DataModel::Gc {} => DataModel::Gc {},
crate::component::DataModel::LinearMemory {
memory,
memory64,
realloc,
} => {
let memory = memory.as_ref().map(|memory| {
self.import_memory(
"memory",
&format!("m{}", self.imported_memories.len()),
MemoryType {
minimum: 0,
maximum: None,
shared: false,
memory64: *memory64,
page_size_log2: None,
},
memory.clone().into(),
crate::component::DataModel::LinearMemory { memory, realloc } => {
let memory = memory.as_ref().map(|(memory, ty)| {
(
self.import_memory(
"memory",
&format!("m{}", self.imported_memories.len()),
MemoryType {
minimum: 0,
maximum: None,
shared: ty.shared,
memory64: ty.idx_type == IndexType::I64,
page_size_log2: if ty.page_size_log2 == 16 {
None
} else {
Some(ty.page_size_log2.into())
},
},
memory.clone().into(),
),
*ty,
)
});
let realloc = realloc.as_ref().map(|func| {
let ptr = if *memory64 {
ValType::I64
} else {
ValType::I32
let ptr = match memory.as_ref().unwrap().1.idx_type {
IndexType::I32 => ValType::I32,
IndexType::I64 => ValType::I64,
};
let ty = self.core_types.function(&[ptr, ptr, ptr, ptr], &[ptr]);
self.import_func(
Expand All @@ -378,11 +397,7 @@ impl<'a> Module<'a> {
func.clone(),
)
});
DataModel::LinearMemory(LinearMemoryOptions {
memory64: *memory64,
memory,
realloc,
})
DataModel::LinearMemory(LinearMemoryOptions { memory, realloc })
}
};

Expand Down Expand Up @@ -907,7 +922,7 @@ impl Options {
let flat = types.flat_types(ty)?;
match self.data_model {
DataModel::Gc {} => todo!("CM+GC"),
DataModel::LinearMemory(mem_opts) => Some(if mem_opts.memory64 {
DataModel::LinearMemory(mem_opts) => Some(if mem_opts.memory64() {
flat.memory64
} else {
flat.memory32
Expand Down
2 changes: 1 addition & 1 deletion crates/environ/src/fact/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl ComponentTypesBuilder {
// seems like it would be best to intern this in some sort of map somewhere.
pub(super) fn size_align(&self, opts: &LinearMemoryOptions, ty: &InterfaceType) -> (u32, u32) {
let abi = self.canonical_abi(ty);
if opts.memory64 {
if opts.memory64() {
(abi.size64, abi.align64)
} else {
(abi.size32, abi.align32)
Expand Down
Loading
Loading