From 6b4625df7c44293707298483f5c978ab0b7b4efb Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Thu, 19 Sep 2024 20:33:57 +0800 Subject: [PATCH] simd miri --- src/parser.rs | 10 +++++----- src/util/arch/mod.rs | 2 +- src/util/simd/mod.rs | 2 +- src/util/simd/v128.rs | 8 ++++++-- src/util/simd/v256.rs | 2 +- src/util/simd/v512.rs | 2 +- src/util/string.rs | 24 ++++++++++++------------ src/util/utf8.rs | 13 +++++++++++++ 8 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 48c70e5..192e589 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,7 @@ use serde::de::{self, Expected, Unexpected}; use smallvec::SmallVec; use super::reader::{Reader, Reference}; -#[cfg(all(target_feature = "neon", target_arch = "aarch64"))] +#[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] use crate::util::simd::bits::NeonBits; use crate::{ error::{ @@ -836,9 +836,9 @@ where &mut self, buf: &'own mut Vec, ) -> Result> { - #[cfg(all(target_feature = "neon", target_arch = "aarch64"))] + #[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] let mut block: StringBlock; - #[cfg(not(all(target_feature = "neon", target_arch = "aarch64")))] + #[cfg(not(all(target_feature = "neon", target_arch = "aarch64", not(miri))))] let mut block: StringBlock; self.parse_escaped_char(buf)?; @@ -910,9 +910,9 @@ where ) -> Result> { // now reader is start after `"`, so we can directly skipstring let start = self.read.index(); - #[cfg(all(target_feature = "neon", target_arch = "aarch64"))] + #[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] let mut block: StringBlock; - #[cfg(not(all(target_feature = "neon", target_arch = "aarch64")))] + #[cfg(not(all(target_feature = "neon", target_arch = "aarch64", not(miri))))] let mut block: StringBlock; while let Some(chunk) = self.read.peek_n(StringBlock::LANES) { diff --git a/src/util/arch/mod.rs b/src/util/arch/mod.rs index c740702..9dccc80 100644 --- a/src/util/arch/mod.rs +++ b/src/util/arch/mod.rs @@ -2,7 +2,7 @@ cfg_if::cfg_if! { if #[cfg(all(target_arch = "x86_64", target_feature = "pclmulqdq", target_feature = "avx2", target_feature = "sse2"))] { mod x86_64; pub use x86_64::*; - } else if #[cfg(all(target_feature="neon", target_arch="aarch64"))] { + } else if #[cfg(all(target_feature="neon", target_arch="aarch64", not(miri)))] { pub(crate) mod fallback; mod aarch64; pub use aarch64::*; diff --git a/src/util/simd/mod.rs b/src/util/simd/mod.rs index a7431a7..0c69bf5 100644 --- a/src/util/simd/mod.rs +++ b/src/util/simd/mod.rs @@ -18,7 +18,7 @@ cfg_if::cfg_if! { if #[cfg(target_feature = "sse2")] { mod sse2; use self::sse2::*; - } else if #[cfg(all(target_feature="neon", target_arch="aarch64"))] { + } else if #[cfg(all(target_feature="neon", target_arch="aarch64", not(miri)))] { pub(crate) mod neon; use self::neon::*; } else { diff --git a/src/util/simd/v128.rs b/src/util/simd/v128.rs index 40f6171..3fa1ae5 100644 --- a/src/util/simd/v128.rs +++ b/src/util/simd/v128.rs @@ -16,6 +16,8 @@ pub struct Mask128([u8; 16]); impl Simd for Simd128i { const LANES: usize = 16; + + type Element = i8; type Mask = Mask128; unsafe fn loadu(ptr: *const u8) -> Self { @@ -38,8 +40,8 @@ impl Simd for Simd128i { Mask128(mask) } - fn splat(value: u8) -> Self { - Self([value as i8; Self::LANES]) + fn splat(value: i8) -> Self { + Self([value; Self::LANES]) } fn le(&self, rhs: &Self) -> Self::Mask { @@ -61,6 +63,8 @@ impl Simd for Simd128i { impl Simd for Simd128u { const LANES: usize = 16; + + type Element = u8; type Mask = Mask128; unsafe fn loadu(ptr: *const u8) -> Self { diff --git a/src/util/simd/v256.rs b/src/util/simd/v256.rs index 0151564..7ff1d04 100644 --- a/src/util/simd/v256.rs +++ b/src/util/simd/v256.rs @@ -26,7 +26,7 @@ impl Mask for Mask256 { #[inline(always)] fn bitmask(self) -> Self::BitMask { cfg_if::cfg_if! { - if #[cfg(all(target_feature="neon", target_arch="aarch64"))] { + if #[cfg(all(target_feature="neon", target_arch="aarch64", not(miri)))] { use std::arch::aarch64::uint8x16_t; let(v0, v1) = self.0; unsafe { super::neon::to_bitmask32(v0.0, v1.0) } diff --git a/src/util/simd/v512.rs b/src/util/simd/v512.rs index 0b6ead7..4ca9763 100644 --- a/src/util/simd/v512.rs +++ b/src/util/simd/v512.rs @@ -26,7 +26,7 @@ impl Mask for Mask512 { #[inline(always)] fn bitmask(self) -> Self::BitMask { cfg_if::cfg_if! { - if #[cfg(all(target_feature="neon", target_arch="aarch64"))] { + if #[cfg(all(target_feature="neon", target_arch="aarch64", not(miri)))] { use std::arch::aarch64::uint8x16_t; let (v0, v1) = self.0; let (m0, m1) = v0.0; diff --git a/src/util/string.rs b/src/util/string.rs index a571441..78f8b33 100644 --- a/src/util/string.rs +++ b/src/util/string.rs @@ -3,9 +3,9 @@ use std::{ slice::{from_raw_parts, from_raw_parts_mut}, }; -#[cfg(not(all(target_feature = "neon", target_arch = "aarch64")))] +#[cfg(not(all(target_feature = "neon", target_arch = "aarch64", not(miri))))] use crate::util::simd::u8x32; -#[cfg(all(target_feature = "neon", target_arch = "aarch64"))] +#[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] use crate::util::simd::{bits::NeonBits, u8x16}; use crate::{ error::ErrorCode::{ @@ -37,7 +37,7 @@ pub(crate) struct StringBlock { pub(crate) unescaped_bits: B, } -#[cfg(not(all(target_feature = "neon", target_arch = "aarch64")))] +#[cfg(not(all(target_feature = "neon", target_arch = "aarch64", not(miri))))] impl StringBlock { pub(crate) const LANES: usize = 32; @@ -51,7 +51,7 @@ impl StringBlock { } } -#[cfg(all(target_feature = "neon", target_arch = "aarch64"))] +#[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] impl StringBlock { pub(crate) const LANES: usize = 16; @@ -108,9 +108,9 @@ pub(crate) unsafe fn load(ptr: *const u8) -> V { pub(crate) unsafe fn parse_string_inplace( src: &mut *mut u8, ) -> std::result::Result { - #[cfg(all(target_feature = "neon", target_arch = "aarch64"))] + #[cfg(all(target_feature = "neon", target_arch = "aarch64", not(miri)))] let mut block: StringBlock; - #[cfg(not(all(target_feature = "neon", target_arch = "aarch64")))] + #[cfg(not(all(target_feature = "neon", target_arch = "aarch64", not(miri))))] let mut block: StringBlock; let sdst = *src; @@ -513,17 +513,17 @@ fn check_cross_page(ptr: *const u8, step: usize) -> bool { pub fn format_string(value: &str, dst: &mut [MaybeUninit], need_quote: bool) -> usize { assert!(dst.len() >= value.len() * 6 + 32 + 3); - #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] + #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] let mut v: u8x16; - #[cfg(not(all(target_arch = "aarch64", target_feature = "neon")))] + #[cfg(not(all(target_arch = "aarch64", target_feature = "neon", not(miri))))] let mut v: u8x32; - #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] + #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] const LANES: usize = 16; - #[cfg(not(all(target_arch = "aarch64", target_feature = "neon")))] + #[cfg(not(all(target_arch = "aarch64", target_feature = "neon", not(miri))))] const LANES: usize = 32; - #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] + #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] #[inline] fn escaped_mask(v: u8x16) -> NeonBits { let x1f = u8x16::splat(0x1f); // 0x00 ~ 0x20 @@ -533,7 +533,7 @@ pub fn format_string(value: &str, dst: &mut [MaybeUninit], need_quote: bool) v.bitmask() } - #[cfg(not(all(target_arch = "aarch64", target_feature = "neon")))] + #[cfg(not(all(target_arch = "aarch64", target_feature = "neon", not(miri))))] #[inline] fn escaped_mask(v: u8x32) -> u32 { let x1f = u8x32::splat(0x1f); // 0x00 ~ 0x20 diff --git a/src/util/utf8.rs b/src/util/utf8.rs index 6d479fe..28df157 100644 --- a/src/util/utf8.rs +++ b/src/util/utf8.rs @@ -1,5 +1,6 @@ use crate::error::{Error, ErrorCode, Result}; +#[cfg(not(miri))] #[inline(always)] pub(crate) fn from_utf8(data: &[u8]) -> Result<&str> { match simdutf8::basic::from_utf8(data) { @@ -11,6 +12,18 @@ pub(crate) fn from_utf8(data: &[u8]) -> Result<&str> { } } +#[cfg(miri)] +pub(crate) fn from_utf8(data: &[u8]) -> Result<&str> { + match std::str::from_utf8(data) { + Ok(ret) => Ok(ret), + Err(err) => Err(Error::syntax( + ErrorCode::InvalidUTF8, + data, + err.valid_up_to(), + )), + } +} + #[cold] fn from_utf8_compat(data: &[u8]) -> Result<&str> { // compat::from_utf8 is slower than basic::from_utf8