Skip to content

Commit cd3c67b

Browse files
Merge pull request #347 from thomcc/attrs
Add `#[inline]` to functions which were missing it, and `#[track_caller]` to ones with runtime panics from user input
2 parents 8527625 + 0315db3 commit cd3c67b

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

crates/core_simd/src/iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ macro_rules! impl_traits {
1010
where
1111
LaneCount<LANES>: SupportedLaneCount,
1212
{
13+
#[inline]
1314
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
1415
iter.fold(Simd::splat(0 as $type), Add::add)
1516
}
@@ -19,6 +20,7 @@ macro_rules! impl_traits {
1920
where
2021
LaneCount<LANES>: SupportedLaneCount,
2122
{
23+
#[inline]
2224
fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
2325
iter.fold(Simd::splat(1 as $type), Mul::mul)
2426
}
@@ -28,6 +30,7 @@ macro_rules! impl_traits {
2830
where
2931
LaneCount<LANES>: SupportedLaneCount,
3032
{
33+
#[inline]
3134
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
3235
iter.fold(Simd::splat(0 as $type), Add::add)
3336
}
@@ -37,6 +40,7 @@ macro_rules! impl_traits {
3740
where
3841
LaneCount<LANES>: SupportedLaneCount,
3942
{
43+
#[inline]
4044
fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
4145
iter.fold(Simd::splat(1 as $type), Mul::mul)
4246
}

crates/core_simd/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)]
1717
#![cfg_attr(feature = "generic_const_exprs", feature(generic_const_exprs))]
1818
#![cfg_attr(feature = "generic_const_exprs", allow(incomplete_features))]
19-
#![warn(missing_docs)]
19+
#![warn(missing_docs, clippy::missing_inline_in_public_items)] // basically all items, really
2020
#![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)]
2121
#![unstable(feature = "portable_simd", issue = "86656")]
2222
//! Portable SIMD module.

crates/core_simd/src/masks.rs

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ where
179179
/// Panics if any lane is not 0 or -1.
180180
#[inline]
181181
#[must_use = "method returns a new mask and does not mutate the original value"]
182+
#[track_caller]
182183
pub fn from_int(value: Simd<T, LANES>) -> Self {
183184
assert!(T::valid(value), "all values must be either 0 or -1",);
184185
// Safety: the validity has been checked
@@ -217,6 +218,7 @@ where
217218
/// Panics if `lane` is greater than or equal to the number of lanes in the vector.
218219
#[inline]
219220
#[must_use = "method returns a new bool and does not mutate the original value"]
221+
#[track_caller]
220222
pub fn test(&self, lane: usize) -> bool {
221223
assert!(lane < LANES, "lane index out of range");
222224
// Safety: the lane index has been checked
@@ -240,6 +242,7 @@ where
240242
/// # Panics
241243
/// Panics if `lane` is greater than or equal to the number of lanes in the vector.
242244
#[inline]
245+
#[track_caller]
243246
pub fn set(&mut self, lane: usize, value: bool) {
244247
assert!(lane < LANES, "lane index out of range");
245248
// Safety: the lane index has been checked
@@ -327,6 +330,7 @@ where
327330
T: MaskElement + fmt::Debug,
328331
LaneCount<LANES>: SupportedLaneCount,
329332
{
333+
#[inline]
330334
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331335
f.debug_list()
332336
.entries((0..LANES).map(|lane| self.test(lane)))

crates/core_simd/src/ops.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ where
1515
I: core::slice::SliceIndex<[T]>,
1616
{
1717
type Output = I::Output;
18+
#[inline]
1819
fn index(&self, index: I) -> &Self::Output {
1920
&self.as_array()[index]
2021
}
@@ -26,6 +27,7 @@ where
2627
LaneCount<LANES>: SupportedLaneCount,
2728
I: core::slice::SliceIndex<[T]>,
2829
{
30+
#[inline]
2931
fn index_mut(&mut self, index: I) -> &mut Self::Output {
3032
&mut self.as_mut_array()[index]
3133
}
@@ -118,10 +120,14 @@ macro_rules! for_base_types {
118120

119121
#[inline]
120122
#[must_use = "operator returns a new vector without mutating the inputs"]
123+
// TODO: only useful for int Div::div, but we hope that this
124+
// will essentially always always get inlined anyway.
125+
#[track_caller]
121126
fn $call(self, rhs: Self) -> Self::Output {
122127
$macro_impl!(self, rhs, $inner, $scalar)
123128
}
124-
})*
129+
}
130+
)*
125131
}
126132
}
127133

crates/core_simd/src/ord.rs

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ macro_rules! impl_integer {
9494
}
9595

9696
#[inline]
97+
#[track_caller]
9798
fn simd_clamp(self, min: Self, max: Self) -> Self {
9899
assert!(
99100
min.simd_le(max).all(),
@@ -200,6 +201,7 @@ macro_rules! impl_mask {
200201
}
201202

202203
#[inline]
204+
#[track_caller]
203205
fn simd_clamp(self, min: Self, max: Self) -> Self {
204206
assert!(
205207
min.simd_le(max).all(),
@@ -254,6 +256,7 @@ where
254256
}
255257

256258
#[inline]
259+
#[track_caller]
257260
fn simd_clamp(self, min: Self, max: Self) -> Self {
258261
assert!(
259262
min.simd_le(max).all(),
@@ -303,6 +306,7 @@ where
303306
}
304307

305308
#[inline]
309+
#[track_caller]
306310
fn simd_clamp(self, min: Self, max: Self) -> Self {
307311
assert!(
308312
min.simd_le(max).all(),

crates/core_simd/src/vector.rs

+17
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ where
122122
/// let v = u32x4::splat(0);
123123
/// assert_eq!(v.lanes(), 4);
124124
/// ```
125+
#[inline]
125126
pub const fn lanes(&self) -> usize {
126127
Self::LANES
127128
}
@@ -136,6 +137,7 @@ where
136137
/// let v = u32x4::splat(8);
137138
/// assert_eq!(v.as_array(), &[8, 8, 8, 8]);
138139
/// ```
140+
#[inline]
139141
pub fn splat(value: T) -> Self {
140142
// This is preferred over `[value; N]`, since it's explicitly a splat:
141143
// https://github.com/rust-lang/rust/issues/97804
@@ -156,6 +158,7 @@ where
156158
/// let v: u64x4 = Simd::from_array([0, 1, 2, 3]);
157159
/// assert_eq!(v.as_array(), &[0, 1, 2, 3]);
158160
/// ```
161+
#[inline]
159162
pub const fn as_array(&self) -> &[T; N] {
160163
// SAFETY: `Simd<T, N>` is just an overaligned `[T; N]` with
161164
// potential padding at the end, so pointer casting to a
@@ -167,6 +170,7 @@ where
167170
}
168171

169172
/// Returns a mutable array reference containing the entire SIMD vector.
173+
#[inline]
170174
pub fn as_mut_array(&mut self) -> &mut [T; N] {
171175
// SAFETY: `Simd<T, N>` is just an overaligned `[T; N]` with
172176
// potential padding at the end, so pointer casting to a
@@ -184,6 +188,7 @@ where
184188
///
185189
/// # Safety
186190
/// Reading `ptr` must be safe, as if by `<*const [T; N]>::read_unaligned`.
191+
#[inline]
187192
const unsafe fn load(ptr: *const [T; N]) -> Self {
188193
// There are potentially simpler ways to write this function, but this should result in
189194
// LLVM `load <N x T>`
@@ -204,6 +209,7 @@ where
204209
///
205210
/// # Safety
206211
/// Writing to `ptr` must be safe, as if by `<*mut [T; N]>::write_unaligned`.
212+
#[inline]
207213
const unsafe fn store(self, ptr: *mut [T; N]) {
208214
// There are potentially simpler ways to write this function, but this should result in
209215
// LLVM `store <N x T>`
@@ -216,6 +222,7 @@ where
216222
}
217223

218224
/// Converts an array to a SIMD vector.
225+
#[inline]
219226
pub const fn from_array(array: [T; N]) -> Self {
220227
// SAFETY: `&array` is safe to read.
221228
//
@@ -228,6 +235,7 @@ where
228235
}
229236

230237
/// Converts a SIMD vector to an array.
238+
#[inline]
231239
pub const fn to_array(self) -> [T; N] {
232240
let mut tmp = core::mem::MaybeUninit::uninit();
233241
// SAFETY: writing to `tmp` is safe and initializes it.
@@ -259,6 +267,8 @@ where
259267
/// assert_eq!(v.as_array(), &[1, 2, 3, 4]);
260268
/// ```
261269
#[must_use]
270+
#[inline]
271+
#[track_caller]
262272
pub const fn from_slice(slice: &[T]) -> Self {
263273
assert!(
264274
slice.len() >= Self::LANES,
@@ -287,6 +297,8 @@ where
287297
/// v.copy_to_slice(&mut dest);
288298
/// assert_eq!(&dest, &[1, 2, 3, 4, 0, 0]);
289299
/// ```
300+
#[inline]
301+
#[track_caller]
290302
pub fn copy_to_slice(self, slice: &mut [T]) {
291303
assert!(
292304
slice.len() >= Self::LANES,
@@ -718,6 +730,7 @@ where
718730
LaneCount<N>: SupportedLaneCount,
719731
T: SimdElement,
720732
{
733+
#[inline]
721734
fn clone(&self) -> Self {
722735
*self
723736
}
@@ -862,6 +875,7 @@ where
862875
LaneCount<N>: SupportedLaneCount,
863876
T: SimdElement,
864877
{
878+
#[inline]
865879
fn from(array: [T; N]) -> Self {
866880
Self::from_array(array)
867881
}
@@ -872,6 +886,7 @@ where
872886
LaneCount<N>: SupportedLaneCount,
873887
T: SimdElement,
874888
{
889+
#[inline]
875890
fn from(vector: Simd<T, N>) -> Self {
876891
vector.to_array()
877892
}
@@ -884,6 +899,7 @@ where
884899
{
885900
type Error = core::array::TryFromSliceError;
886901

902+
#[inline]
887903
fn try_from(slice: &[T]) -> Result<Self, core::array::TryFromSliceError> {
888904
Ok(Self::from_array(slice.try_into()?))
889905
}
@@ -896,6 +912,7 @@ where
896912
{
897913
type Error = core::array::TryFromSliceError;
898914

915+
#[inline]
899916
fn try_from(slice: &mut [T]) -> Result<Self, core::array::TryFromSliceError> {
900917
Ok(Self::from_array(slice.try_into()?))
901918
}

0 commit comments

Comments
 (0)