Skip to content

Commit fe825c9

Browse files
authored
Rename wasm32 memory intrinsics (rust-lang#560)
The official name of the memory intrinsics has changed to `memory.size` and `memory.grow`, so let's reflect that with our naming as well! Additionally they have an argument of which memory to operate on with LLVM and must always be zero currently.
1 parent 60f46da commit fe825c9

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed

ci/docker/wasm32-unknown-unknown/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ RUN make -C wabt -j$(nproc)
1717
ENV PATH=$PATH:/wabt/bin
1818

1919
# Install `wasm-bindgen-test-runner`
20-
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 \
20+
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 \
2121
| tar xzf -
22-
ENV PATH=$PATH:/wasm-bindgen-0.2.16-x86_64-unknown-linux-musl
22+
ENV PATH=$PATH:/wasm-bindgen-0.2.19-x86_64-unknown-linux-musl
2323
ENV CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner
2424

2525
# Install `node`

coresimd/wasm32/memory.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#[cfg(test)]
2+
use stdsimd_test::assert_instr;
3+
#[cfg(test)]
4+
use wasm_bindgen_test::wasm_bindgen_test;
5+
6+
extern "C" {
7+
#[link_name = "llvm.wasm.memory.grow.i32"]
8+
fn llvm_memory_grow(mem: i32, pages: i32) -> i32;
9+
#[link_name = "llvm.wasm.memory.size.i32"]
10+
fn llvm_memory_size(mem: i32) -> i32;
11+
}
12+
13+
/// Corresponding intrinsic to wasm's [`memory.size` instruction][instr]
14+
///
15+
/// This function, when called, will return the current memory size in units of
16+
/// pages.
17+
///
18+
/// The argument `mem` is the numerical index of which memory to return the
19+
/// size of. Note that currently wasm only supports one memory, so specifying
20+
/// a nonzero value will likely result in a runtime validation error of the
21+
/// wasm module.
22+
///
23+
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
24+
#[inline]
25+
#[cfg_attr(test, assert_instr("memory.size", mem = 0))]
26+
#[rustc_args_required_const(0)]
27+
pub unsafe fn size(mem: i32) -> i32 {
28+
if mem != 0 {
29+
::intrinsics::abort();
30+
}
31+
llvm_memory_size(0)
32+
}
33+
34+
/// Corresponding intrinsic to wasm's [`memory.grow` instruction][instr]
35+
///
36+
/// This function, when called, will attempt to grow the default linear memory
37+
/// by the specified `delta` of pages. If memory is successfully grown then the
38+
/// previous size of memory, in pages, is returned. If memory cannot be grown
39+
/// then -1 is returned.
40+
///
41+
/// The argument `mem` is the numerical index of which memory to return the
42+
/// size of. Note that currently wasm only supports one memory, so specifying
43+
/// a nonzero value will likely result in a runtime validation error of the
44+
/// wasm module.
45+
///
46+
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
47+
#[inline]
48+
#[cfg_attr(test, assert_instr("memory.grow", mem = 0))]
49+
#[rustc_args_required_const(0)]
50+
pub unsafe fn grow(mem: i32, delta: i32) -> i32 {
51+
if mem != 0 {
52+
::intrinsics::abort();
53+
}
54+
llvm_memory_grow(0, delta)
55+
}

coresimd/wasm32/mod.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! WASM32 intrinsics
22
3+
#![allow(deprecated)]
34

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

18-
extern "C" {
19-
#[link_name = "llvm.wasm.grow.memory.i32"]
20-
fn llvm_grow_memory(pages: i32) -> i32;
21-
#[link_name = "llvm.wasm.current.memory.i32"]
22-
fn llvm_current_memory() -> i32;
23-
}
24-
25-
/// Corresponding intrinsic to wasm's [`current_memory` instruction][instr]
26-
///
27-
/// This function, when called, will return the current memory size in units of
28-
/// pages.
29-
///
30-
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing
3119
#[inline]
3220
#[cfg_attr(test, assert_instr("memory.size"))]
21+
#[rustc_deprecated(reason = "renamed to memory::size", since = "1.30.0")]
22+
#[unstable(feature = "stdsimd", issue = "27731")]
23+
#[allow(deprecated)]
24+
#[doc(hidden)]
3325
pub unsafe fn current_memory() -> i32 {
34-
llvm_current_memory()
26+
memory::size(0)
3527
}
3628

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

5139
pub mod atomic;
40+
pub mod memory;

crates/coresimd/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ stdsimd-test = { version = "0.*", path = "../stdsimd-test" }
2323
stdsimd = { version = "0.0.3", path = "../stdsimd" }
2424

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

2828
[features]
2929
# Internal-usage only: denies all warnings.

crates/stdsimd-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ backtrace = "0.3"
1010
cc = "1.0"
1111
lazy_static = "1.0"
1212
rustc-demangle = "0.1.8"
13-
wasm-bindgen = "0.2.16"
13+
wasm-bindgen = "0.2.19"
1414

1515
[features]
1616
default = []

examples/wasm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub unsafe extern "C" fn page_alloc() -> *mut u8 {
2020
return ret as *mut u8;
2121
}
2222

23-
let ret = grow_memory(1);
23+
let ret = memory::grow(0, 1);
2424

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

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

4545
fn page_size() -> u32 {

0 commit comments

Comments
 (0)