cloudwego / cloudwego/sonic-rs
sonic-simd fails to build on 32-bit x86 (i686): `arch::x86_64` imported under a `target_feature = "sse2"` gate
- Dominant language
- Rust
- Stars
- 920
- Forks
- 68
- Avg merge
- 2h 5m
- Merged PRs (30d)
- 1
Description
## Summary
`sonic-simd` (and therefore `sonic-rs`) fails to compile on **32-bit x86** targets such as `i686-pc-windows-msvc` / `i686-unknown-linux-gnu`.
## Error
```
error[E0432]: unresolved import `core::arch::x86_64`
--> sonic-simd/src/sse2.rs:2:11
|
2 | arch::x86_64::*,
| ^^^^^^ could not find `x86_64` in `arch`
error: could not compile `sonic-simd` (lib)
```
## Root cause
In `sonic-simd/src/lib.rs`, the SSE2 backend is selected by **CPU feature**:
```rust
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse2")] {
mod sse2;
} else if #[cfg(all(target_feature = "neon", target_arch = "aarch64"))] {
...
} else {
mod v128; // portable fallback
}
}
```
On 32-bit x86 (`i686`), SSE2 is enabled by default, so `target_feature = "sse2"` is **true** and `mod sse2;` is compiled — but `sse2.rs` (and `avx2.rs`, `avx512.rs`) import the intrinsics from `core::arch::x86_64`, which **does not exist** on 32-bit x86. The SSE2/AVX intrinsics it uses (`_mm_loadu_si128`, `_mm_movemask_epi8`, `__m128i`, …) are available in `core::arch::x86` as well, so the modules just need to import the arch module that matches the target.
## Fix
Gate the arch import by `target_arch` in `sse2.rs`, `avx2.rs`, and `avx512.rs`:
```rust
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
use core::ops::{BitAnd, BitOr, BitOrAssign};
```
With this, 32-bit x86 builds use `core::arch::x86`'s SSE2/AVX intrinsics and compile correctly; 64-bit is unchanged.
## Repro
```
rustup target add i686-unknown-linux-gnu
cargo build -p sonic-simd --target i686-unknown-linux-gnu # or i686-pc-windows-msvc
```
I'm happy to send a PR with the 3-file change if that's useful.
Contributor guide
Research direction
Start with sonic-simd/src/lib.rs to understand the SSE2 backend selection, then inspect the architecture imports in sonic-simd/src/sse2.rs, avx2.rs, and avx512.rs. Reproduce with cargo build -p sonic-simd --target i686-unknown-linux-gnu after installing the target, and verify that both 32-bit and 64-bit x86 builds compile successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- build-system, performance
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 82/100