Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ link.tar.gz
.direnv
.envrc
/tests/tmp
.codex
18 changes: 18 additions & 0 deletions lib/cli/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ pub struct RuntimeOptions {
#[clap(long = "enable-nan-canonicalization")]
enable_nan_canonicalization: bool,

/// Allow unaligned memory accesses.
///
/// Available for Cranelift and Singlepass (RISC-V).
#[clap(long = "enable-unaligned-memory-accesses")]
enable_unaligned_memory_accesses: bool,

#[clap(flatten)]
features: WasmFeatures,
}
Expand Down Expand Up @@ -461,6 +467,9 @@ impl RuntimeOptions {
#[cfg(feature = "singlepass")]
BackendType::Singlepass => {
let mut config = wasmer_compiler_singlepass::Singlepass::new();
if self.enable_unaligned_memory_accesses {
config.allow_unaligned_memory_accesses(true);
}
if self.enable_verifier {
config.enable_verifier();
}
Expand All @@ -486,6 +495,9 @@ impl RuntimeOptions {
#[cfg(feature = "cranelift")]
BackendType::Cranelift => {
let mut config = wasmer_compiler_cranelift::Cranelift::new();
if self.enable_unaligned_memory_accesses {
config.allow_unaligned_memory_accesses(true);
}
if self.enable_verifier {
config.enable_verifier();
}
Expand Down Expand Up @@ -605,6 +617,9 @@ impl BackendType {
#[cfg(feature = "singlepass")]
Self::Singlepass => {
let mut config = wasmer_compiler_singlepass::Singlepass::new();
if runtime_opts.enable_unaligned_memory_accesses {
config.allow_unaligned_memory_accesses(true);
}
let supported_features = config.supported_features_for_target(target);
if runtime_opts.enable_verifier {
config.enable_verifier();
Expand Down Expand Up @@ -636,6 +651,9 @@ impl BackendType {
#[cfg(feature = "cranelift")]
Self::Cranelift => {
let mut config = wasmer_compiler_cranelift::Cranelift::new();
if runtime_opts.enable_unaligned_memory_accesses {
config.allow_unaligned_memory_accesses(true);
}
let supported_features = config.supported_features_for_target(target);
if runtime_opts.enable_verifier {
config.enable_verifier();
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl CraneliftCompiler {
let mut custom_sections = PrimaryMap::new();

#[cfg(not(feature = "rayon"))]
let mut func_translator = FuncTranslator::new();
let mut func_translator = FuncTranslator::new(self.config.allow_unaligned_memory_accesses);
#[cfg(not(feature = "rayon"))]
let results = function_body_inputs
.iter()
Expand Down Expand Up @@ -351,7 +351,7 @@ impl CraneliftCompiler {

translate_function_buckets(
&pool,
FuncTranslator::new,
|| FuncTranslator::new(self.config.allow_unaligned_memory_accesses),
|func_translator, i, input| compile_function(func_translator, i, input),
progress.clone(),
&buckets,
Expand Down
14 changes: 14 additions & 0 deletions lib/compiler-cranelift/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub enum CraneliftOptLevel {
#[derive(Debug, Clone)]
pub struct Cranelift {
enable_nan_canonicalization: bool,
pub(crate) allow_unaligned_memory_accesses: bool,
enable_verifier: bool,
pub(crate) enable_perfmap: bool,
enable_pic: bool,
Expand All @@ -123,6 +124,7 @@ impl Cranelift {
pub fn new() -> Self {
Self {
enable_nan_canonicalization: false,
allow_unaligned_memory_accesses: false,
enable_verifier: false,
opt_level: CraneliftOptLevel::Speed,
enable_pic: false,
Expand All @@ -142,6 +144,14 @@ impl Cranelift {
self
}

/// Enable run-time handling of potentially unaligned memory accesses.
/// Unaligned memory accesses occur when you try to read N bytes of data starting
/// from an address that is not evenly divisible by N.
pub fn allow_unaligned_memory_accesses(&mut self, enable: bool) -> &mut Self {
self.allow_unaligned_memory_accesses = enable;
self
}

/// Set the number of threads to use for compilation.
pub fn num_threads(&mut self, num_threads: NonZero<usize>) -> &mut Self {
self.num_threads = num_threads;
Expand Down Expand Up @@ -286,6 +296,10 @@ impl CompilerConfig for Cranelift {
self.enable_perfmap = true;
}

fn enable_unaligned_memory_accesses(&mut self) {
self.allow_unaligned_memory_accesses = true;
}

fn canonicalize_nans(&mut self, enable: bool) {
self.enable_nan_canonicalization = enable;
}
Expand Down
Loading
Loading