Skip to content

Rename wasm32 memory intrinsics #560

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

Merged
merged 1 commit into from
Sep 6, 2018
Merged
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
4 changes: 2 additions & 2 deletions ci/docker/wasm32-unknown-unknown/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ RUN make -C wabt -j$(nproc)
ENV PATH=$PATH:/wabt/bin

# Install `wasm-bindgen-test-runner`
RUN curl -L https://github.com/rustwasm/wasm-bindgen/releases/download/0.2.16/wasm-bindgen-0.2.16-x86_64-unknown-linux-musl.tar.gz \
RUN curl -L https://github.com/rustwasm/wasm-bindgen/releases/download/0.2.19/wasm-bindgen-0.2.19-x86_64-unknown-linux-musl.tar.gz \
| tar xzf -
ENV PATH=$PATH:/wasm-bindgen-0.2.16-x86_64-unknown-linux-musl
ENV PATH=$PATH:/wasm-bindgen-0.2.19-x86_64-unknown-linux-musl
ENV CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner

# Install `node`
Expand Down
55 changes: 55 additions & 0 deletions coresimd/wasm32/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[cfg(test)]
use stdsimd_test::assert_instr;
#[cfg(test)]
use wasm_bindgen_test::wasm_bindgen_test;

extern "C" {
#[link_name = "llvm.wasm.memory.grow.i32"]
fn llvm_memory_grow(mem: i32, pages: i32) -> i32;
#[link_name = "llvm.wasm.memory.size.i32"]
fn llvm_memory_size(mem: i32) -> i32;
}

/// Corresponding intrinsic to wasm's [`memory.size` instruction][instr]
///
/// This function, when called, will return the current memory size in units of
/// pages.
///
/// The argument `mem` is the numerical index of which memory to return the
/// size of. Note that currently wasm only supports one memory, so specifying
/// a nonzero value will likely result in a runtime validation error of the
/// wasm module.
///
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
#[inline]
#[cfg_attr(test, assert_instr("memory.size", mem = 0))]
#[rustc_args_required_const(0)]
pub unsafe fn size(mem: i32) -> i32 {
if mem != 0 {
::intrinsics::abort();
}
llvm_memory_size(0)
}

/// Corresponding intrinsic to wasm's [`memory.grow` instruction][instr]
///
/// This function, when called, will attempt to grow the default linear memory
/// by the specified `delta` of pages. If memory is successfully grown then the
/// previous size of memory, in pages, is returned. If memory cannot be grown
/// then -1 is returned.
///
/// The argument `mem` is the numerical index of which memory to return the
/// size of. Note that currently wasm only supports one memory, so specifying
/// a nonzero value will likely result in a runtime validation error of the
/// wasm module.
///
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
#[inline]
#[cfg_attr(test, assert_instr("memory.grow", mem = 0))]
#[rustc_args_required_const(0)]
pub unsafe fn grow(mem: i32, delta: i32) -> i32 {
if mem != 0 {
::intrinsics::abort();
}
llvm_memory_grow(0, delta)
}
35 changes: 12 additions & 23 deletions coresimd/wasm32/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! WASM32 intrinsics

#![allow(deprecated)]

#[macro_use]
#[cfg(all(not(test), feature = "wasm_simd128"))]
Expand All @@ -15,37 +16,25 @@ use stdsimd_test::assert_instr;
#[cfg(test)]
use wasm_bindgen_test::wasm_bindgen_test;

extern "C" {
#[link_name = "llvm.wasm.grow.memory.i32"]
fn llvm_grow_memory(pages: i32) -> i32;
#[link_name = "llvm.wasm.current.memory.i32"]
fn llvm_current_memory() -> i32;
}

/// Corresponding intrinsic to wasm's [`current_memory` instruction][instr]
///
/// This function, when called, will return the current memory size in units of
/// pages.
///
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
#[inline]
#[cfg_attr(test, assert_instr("memory.size"))]
#[rustc_deprecated(reason = "renamed to memory::size", since = "1.30.0")]
#[unstable(feature = "stdsimd", issue = "27731")]
#[allow(deprecated)]
#[doc(hidden)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should open an issue to delete these by some date (e.g. halloween).

pub unsafe fn current_memory() -> i32 {
llvm_current_memory()
memory::size(0)
}

/// Corresponding intrinsic to wasm's [`grow_memory` instruction][instr]
///
/// This function, when called, will attempt to grow the default linear memory
/// by the specified number of pages. If memory is successfully grown then the
/// previous size of memory, in pages, is returned. If memory cannot be grown
/// then -1 is returned.
///
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
#[inline]
#[cfg_attr(test, assert_instr("memory.grow"))]
#[rustc_deprecated(reason = "renamed to memory::grow", since = "1.30.0")]
#[unstable(feature = "stdsimd", issue = "27731")]
#[allow(deprecated)]
#[doc(hidden)]
pub unsafe fn grow_memory(delta: i32) -> i32 {
llvm_grow_memory(delta)
memory::grow(0, delta)
}

pub mod atomic;
pub mod memory;
2 changes: 1 addition & 1 deletion crates/coresimd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ stdsimd-test = { version = "0.*", path = "../stdsimd-test" }
stdsimd = { version = "0.0.3", path = "../stdsimd" }

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.2.16"
wasm-bindgen-test = "0.2.19"

[features]
# Internal-usage only: denies all warnings.
Expand Down
2 changes: 1 addition & 1 deletion crates/stdsimd-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ backtrace = "0.3"
cc = "1.0"
lazy_static = "1.0"
rustc-demangle = "0.1.8"
wasm-bindgen = "0.2.16"
wasm-bindgen = "0.2.19"

[features]
default = []
4 changes: 2 additions & 2 deletions examples/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub unsafe extern "C" fn page_alloc() -> *mut u8 {
return ret as *mut u8;
}

let ret = grow_memory(1);
let ret = memory::grow(0, 1);

// if we failed to allocate a page then return null
if ret == -1 {
Expand All @@ -39,7 +39,7 @@ pub unsafe extern "C" fn page_free(page: *mut u8) {

#[no_mangle]
pub unsafe extern "C" fn memory_used() -> usize {
(page_size() * (current_memory() as u32)) as usize
(page_size() * (memory::size(0) as u32)) as usize
}

fn page_size() -> u32 {
Expand Down