A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
cranelift: avoid unsupported vector types in inline copies (#14524)
SUNG JE PARKfc039986419b
diff --git a/cranelift/codegen/src/isa/mod.rs b/cranelift/codegen/src/isa/mod.rsindex 5f33e632fb..dfc5277d18 100644--- a/cranelift/codegen/src/isa/mod.rs+++ b/cranelift/codegen/src/isa/mod.rs@@ -389,6 +389,13 @@ pub trait TargetIsa: fmt::Display + Send + Sync {/// not detected.fn has_native_fma(&self) -> bool;+ /// Returns whether this ISA supports vector loads and stores of `ty`.+ ///+ /// By default, vectors up to 128 bits are supported.+ fn supports_vector_load_store(&self, ty: Type) -> bool {+ ty.is_vector() && ty.bits() <= 128+ }+/// Returns whether this ISA has instructions for `ceil`, `floor`, etc.fn has_round(&self) -> bool;diff --git a/cranelift/codegen/src/isa/riscv64/lower/isle.rs b/cranelift/codegen/src/isa/riscv64/lower/isle.rsindex 4154c9c82d..e5da2eccb6 100644--- a/cranelift/codegen/src/isa/riscv64/lower/isle.rs+++ b/cranelift/codegen/src/isa/riscv64/lower/isle.rs@@ -223,28 +223,8 @@ impl generated_code::Context for RV64IsleContext<'_, '_, MInst, Riscv64Backend>// F128 is currently stored in a pair of integer registersF128 => true,- // The base vector extension supports all integer types, up to 64 bits- // as long as they fit in a register- ty if self.ty_vec_fits_in_register(ty).is_some()- && lane_type.is_int()- && lane_type.bits() <= 64 =>- {- true- }-- // If the vector type has floating point lanes then the spec states:- //- // Vector instructions where any floating-point vector operand’s EEW is not a- // supported floating-point type width (which includes when FLEN < SEW) are reserved.- //- // So we also have to check if we support the scalar version of the type.- ty if self.ty_vec_fits_in_register(ty).is_some()- && lane_type.is_float()- && self.ty_supported(lane_type).is_some()- // Additionally the base V spec only supports 32 and 64 bit floating point types.- && (lane_type.bits() == 32 || lane_type.bits() == 64 || (lane_type.bits() == 16 && self.backend.isa_flags.has_zvfh())) =>- {- true+ ty if self.ty_vec_fits_in_register(ty).is_some() => {+ self.backend.supports_vector_element_type(lane_type)}// Otherwise do not matchdiff --git a/cranelift/codegen/src/isa/riscv64/mod.rs b/cranelift/codegen/src/isa/riscv64/mod.rsindex 562576a6fc..a2c94c6cff 100644--- a/cranelift/codegen/src/isa/riscv64/mod.rs+++ b/cranelift/codegen/src/isa/riscv64/mod.rs@@ -36,6 +36,19 @@ pub struct Riscv64Backend {}impl Riscv64Backend {+ fn supports_vector_element_type(&self, ty: Type) -> bool {+ use ir::types::*;++ match ty {+ I8 | I16 | I32 | I64 => true,+ // FP16 vector operations require Zvfh.+ F16 => self.isa_flags.has_f() && self.isa_flags.has_zvfh(),+ F32 => self.isa_flags.has_f(),+ F64 => self.isa_flags.has_d(),+ _ => false,+ }+ }+/// Create a new riscv64 backend with the given (shared) flags.pub fn new_with_flags(triple: Triple,@@ -213,6 +226,12 @@ impl TargetIsa for Riscv64Backend {true}+ fn supports_vector_load_store(&self, ty: Type) -> bool {+ ty.is_vector()+ && u64::from(ty.bits()) <= self.isa_flags.min_vec_reg_size()+ && self.supports_vector_element_type(ty.lane_type())+ }+fn has_round(&self) -> bool {true}diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rsindex 739bd3c0f2..493e6d704b 100644--- a/crates/cranelift/src/func_environ.rs+++ b/crates/cranelift/src/func_environ.rs@@ -4784,20 +4784,22 @@ impl FuncEnvironment<'_> {(2, ir::types::I16),(1, ir::types::I8),];- // 12 covers the worst case under the 128-byte cap: n=127 decomposes into- // 7×i8x16 + i64 + i32 + i16 + i8 = 11 chunks. Sized so both `SmallVec`s- // stay inline.- let mut chunks: SmallVec<[(i32, ir::Type); 12]> = smallvec![];+ // Without vectors, n=127 decomposes into 15×i64 + i32 + i16 + i8 = 18+ // chunks, the worst case under the 128-byte cap. Keep both `SmallVec`s inline.+ let mut chunks: SmallVec<[(i32, ir::Type); 18]> = smallvec![];let mut offset = 0u64;let mut remaining = bytes;for &(width, ty) in WIDTHS {+ if ty.is_vector() && !self.isa.supports_vector_load_store(ty) {+ continue;+ }while remaining >= width {chunks.push((i32::try_from(offset).unwrap(), ty));offset += width;remaining -= width;}}- let vals: SmallVec<[ir::Value; 12]> = chunks+ let vals: SmallVec<[ir::Value; 18]> = chunks.iter().map(|&(off, ty)| builder.ins().load(ty, load_flags, src_addr, off)).collect();diff --git a/tests/all/module.rs b/tests/all/module.rsindex 3d5764797c..fbdba880c1 100644--- a/tests/all/module.rs+++ b/tests/all/module.rs@@ -93,6 +93,34 @@ fn aot_compiles() -> Result<()> {Ok(())}+#[test]+#[cfg_attr(miri, ignore)]+fn memory_copy_without_riscv_v() -> Result<()> {+ let mut config = Config::new();+ config+ .strategy(Strategy::Cranelift)+ .target("riscv64")?+ .wasm_simd(false)+ .wasm_relaxed_simd(false);+ unsafe {+ config.cranelift_flag_set("has_v", "false");+ }+ let engine = Engine::new(&config)?;++ // Inline copies must not require vector instructions when SIMD is unavailable.+ engine.precompile_module(+ br#"+ (module+ (memory 1)+ (func (export "copy") (param i32 i32)+ (memory.copy (local.get 0) (local.get 1) (i32.const 16)))+ )+ "#,+ )?;++ Ok(())+}+#[test]#[cfg_attr(miri, ignore)]fn serialize_deterministic() {