Skip to content

Commit

Permalink
fix starlark-rust build on nightly
Browse files Browse the repository at this point in the history
Summary:
This diff fixes broken CI for [starlark-rust-oss](https://www.internalfb.com/sandcastle/workflow/2436447398409056479)
We run OSS build on the latest Rust toolchain. In the latest nightly (1.76) [Simd<>::LANES](https://doc.rust-lang.org/std/simd/struct.Simd.html#associatedconstant.LANES) was renamed to [Simd<>::LEN](https://doc.rust-lang.org/nightly/std/simd/struct.Simd.html#associatedconstant.LEN).
In non-oss builds we use 1.74 version of toolchain, so this diff conditionally fixes name of the const.

Reviewed By: stepancheg

Differential Revision: D51808711

fbshipit-source-id: 8e78127f3cf2267c1b80aa0f86f80887076b2554
  • Loading branch information
Yury Samkevich authored and facebook-github-bot committed Dec 5, 2023
1 parent 7d4e885 commit c201c5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions starlark_map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#![deny(rustdoc::broken_intra_doc_links)]
#![cfg_attr(rust_nightly, feature(core_intrinsics))]
#![cfg_attr(rust_nightly, feature(portable_simd))]
#![cfg_attr(rust_nightly, feature(cfg_version))]

mod hash_value;
mod hashed;
Expand Down
22 changes: 15 additions & 7 deletions starlark_map/src/vec_map/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,43 @@ pub(crate) fn find_hash_in_array_without_simd(array: &[u32], hash: u32) -> Optio
pub(crate) fn find_hash_in_array(array: &[u32], hash: u32) -> Option<usize> {
#[cfg(rust_nightly)]
unsafe {
// TODO(yurysamkevich): remove conditional compilation after updating rust toolchain
#[cfg(version("1.76"))]
use std::simd::cmp::SimdPartialEq;
use std::simd::*;

// 128-bit SIMD is available on x86_64 and aarch64.
// Also shorter SIMD works better for shorter arrays.
type T = Simd<u32, 4>;

if array.len() < T::LANES {
#[cfg(version("1.76"))]
const LANES: usize = T::LEN;
#[cfg(not(version("1.76")))]
const LANES: usize = T::LANES;

if array.len() < LANES {
find_hash_in_array_without_simd(array, hash)
} else {
let mut i = 0;
let hash = T::splat(hash);

// Process 4 elements at a time except last <= 4 elements.
while i + T::LANES < array.len() {
let next_hashes = T::from_slice(array.get_unchecked(i..i + T::LANES));
while i + LANES < array.len() {
let next_hashes = T::from_slice(array.get_unchecked(i..i + LANES));
let eq = next_hashes.simd_eq(hash);
if eq.any() {
return Some(i + eq.to_bitmask().trailing_zeros() as usize);
}
i += T::LANES;
i += LANES;
}

// Process last <= 4 elements.
debug_assert!(i >= array.len() - T::LANES);
debug_assert!(i >= array.len() - LANES);
debug_assert!(i < array.len());
let next_hashes = T::from_slice(array.get_unchecked(array.len() - T::LANES..));
let next_hashes = T::from_slice(array.get_unchecked(array.len() - LANES..));
let eq = next_hashes.simd_eq(hash);
if eq.any() {
Some(array.len() - T::LANES + eq.to_bitmask().trailing_zeros() as usize)
Some(array.len() - LANES + eq.to_bitmask().trailing_zeros() as usize)
} else {
None
}
Expand Down

0 comments on commit c201c5e

Please sign in to comment.