Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 927633c

Browse files
committed
Add a note about libraries and #[target_feature]
1 parent dea3846 commit 927633c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/doc/rustc/src/platform-support/wasm32-unknown-unknown.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,36 @@ You'll need to consult your WebAssembly engine's documentation to learn more
155155
about the supported WebAssembly features the engine has.
156156

157157
[reference]: https://doc.rust-lang.org/reference/attributes/codegen.html#wasm32-or-wasm64
158+
159+
Note that it is still possible for Rust crates and libraries to enable
160+
WebAssembly features on a per-function level. This means that the build
161+
command above may not be sufficent to disable all WebAssembly features. If the
162+
final binary still has SIMD instructions, for example, the function in question
163+
will need to be found and the crate in question will likely contain something
164+
like:
165+
166+
```rust
167+
#[target_feature(enable = "simd128")]
168+
fn foo() {
169+
// ...
170+
}
171+
```
172+
173+
In this situation there is no compiler flag to disable emission of SIMD
174+
instructions and the crate must instead be modified to not include this function
175+
at compile time either by default or through a Cargo feature. For crate authors
176+
it's recommended to avoid `#[target_feature(enable = "...")]` except where
177+
necessary and instead use:
178+
179+
```rust
180+
#[cfg(target_feature = "simd128")]
181+
fn foo() {
182+
// ...
183+
}
184+
```
185+
186+
That is to say instead of enabling target features it's recommended to
187+
conditionally compile code instead. This is notably different to the way native
188+
platforms such as x86\_64 work, and this is due to the fact that WebAssembly
189+
binaries must only contain code the engine understands. Native binaries work so
190+
long as the CPU doesn't execute unknown code dynamically at runtime.

0 commit comments

Comments
 (0)