-
Notifications
You must be signed in to change notification settings - Fork 116
Add integer overflow checking for simd_div and simd_rem #2645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0eb41d
Add overflow checks for simd_div and simd_rem
reisnera f6aa104
Add tests for simd_div/rem with overflow
reisnera 5bb4828
Move check before expression in statement
reisnera 86d508e
Move test from kani to expected
reisnera 457bb36
Merge branch 'main' into simd-div-overflow
reisnera 4f84143
Handle floats vs ints in codegen function
reisnera bb5e471
Add a floating point test for SIMD div and rem
reisnera d247645
Remove inaccurate comment regarding `simd_rem`
reisnera 97b56bb
Add comment regarding `simd_rem` float limitations
reisnera e1a0336
Update tests based on review
reisnera dd3faaa
Remove unnecessary comment
reisnera 722df3e
Update documentation re: supported intrinsics
reisnera d58900b
Merge branch 'main' into simd-div-overflow
adpaco-aws 8e67aa8
Merge branch 'main' into simd-div-overflow
adpaco-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FAILURE\ | ||
attempt to compute simd_div which would overflow | ||
UNREACHABLE\ | ||
assertion failed: quotients.0 == quotients.1 | ||
FAILURE\ | ||
attempt to compute simd_rem which would overflow | ||
UNREACHABLE\ | ||
assertion failed: remainders.0 == remainders.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// Checks that the `simd_div` and `simd_rem` intrinsics check for integer overflows. | ||
|
||
#![feature(repr_simd, platform_intrinsics)] | ||
|
||
#[repr(simd)] | ||
#[allow(non_camel_case_types)] | ||
#[derive(Clone, Copy, PartialEq, Eq)] | ||
pub struct i32x2(i32, i32); | ||
|
||
extern "platform-intrinsic" { | ||
fn simd_div<T>(x: T, y: T) -> T; | ||
fn simd_rem<T>(x: T, y: T) -> T; | ||
} | ||
|
||
unsafe fn do_simd_div(dividends: i32x2, divisors: i32x2) -> i32x2 { | ||
simd_div(dividends, divisors) | ||
} | ||
|
||
unsafe fn do_simd_rem(dividends: i32x2, divisors: i32x2) -> i32x2 { | ||
simd_rem(dividends, divisors) | ||
} | ||
|
||
#[kani::proof] | ||
fn test_simd_div_overflow() { | ||
let dividend = i32::MIN; | ||
let dividends = i32x2(dividend, dividend); | ||
let divisor = -1; | ||
let divisors = i32x2(divisor, divisor); | ||
let quotients = unsafe { do_simd_div(dividends, divisors) }; | ||
assert_eq!(quotients.0, quotients.1); | ||
} | ||
|
||
#[kani::proof] | ||
fn test_simd_rem_overflow() { | ||
let dividend = i32::MIN; | ||
let dividends = i32x2(dividend, dividend); | ||
let divisor = -1; | ||
let divisors = i32x2(divisor, divisor); | ||
let remainders = unsafe { do_simd_rem(dividends, divisors) }; | ||
assert_eq!(remainders.0, remainders.1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! Checks that the `simd_div` intrinsic returns the expected results for floating point numbers. | ||
#![feature(repr_simd, platform_intrinsics)] | ||
|
||
#[repr(simd)] | ||
#[allow(non_camel_case_types)] | ||
#[derive(Clone, Copy, PartialEq, kani::Arbitrary)] | ||
pub struct f32x2(f32, f32); | ||
|
||
impl f32x2 { | ||
fn new_with(f: impl Fn() -> f32) -> Self { | ||
f32x2(f(), f()) | ||
} | ||
|
||
fn non_simd_div(self, divisors: Self) -> Self { | ||
f32x2(self.0 / divisors.0, self.1 / divisors.1) | ||
} | ||
} | ||
|
||
extern "platform-intrinsic" { | ||
fn simd_div<T>(x: T, y: T) -> T; | ||
} | ||
|
||
#[kani::proof] | ||
fn test_simd_div() { | ||
let dividends = f32x2::new_with(|| { | ||
let multiplier = kani::any_where(|&n: &i8| n >= -5 && n <= 5); | ||
0.5 * f32::from(multiplier) | ||
}); | ||
let divisors = f32x2::new_with(|| { | ||
let multiplier = kani::any_where(|&n: &i8| n != 0 && n >= -5 && n <= 5); | ||
0.5 * f32::from(multiplier) | ||
}); | ||
let normal_results = dividends.non_simd_div(divisors); | ||
let simd_results = unsafe { simd_div(dividends, divisors) }; | ||
assert_eq!(normal_results, simd_results); | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/kani/Intrinsics/SIMD/Operators/remainder_float_fixme.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! Checks that the `simd_rem` intrinsic returns the expected results for floating point numbers. | ||
#![feature(repr_simd, platform_intrinsics)] | ||
|
||
#[repr(simd)] | ||
#[allow(non_camel_case_types)] | ||
#[derive(Clone, Copy, PartialEq, kani::Arbitrary)] | ||
pub struct f32x2(f32, f32); | ||
|
||
impl f32x2 { | ||
fn new_with(f: impl Fn() -> f32) -> Self { | ||
f32x2(f(), f()) | ||
} | ||
|
||
fn non_simd_rem(self, divisors: Self) -> Self { | ||
f32x2(self.0 % divisors.0, self.1 % divisors.1) | ||
} | ||
} | ||
|
||
extern "platform-intrinsic" { | ||
fn simd_rem<T>(x: T, y: T) -> T; | ||
} | ||
|
||
#[kani::proof] | ||
fn test_simd_rem() { | ||
let dividends = f32x2::new_with(|| { | ||
let multiplier = kani::any_where(|&n: &i8| n >= -5 && n <= 5); | ||
0.5 * f32::from(multiplier) | ||
}); | ||
let divisors = f32x2::new_with(|| { | ||
let multiplier = kani::any_where(|&n: &i8| n != 0 && n >= -5 && n <= 5); | ||
0.5 * f32::from(multiplier) | ||
}); | ||
let normal_results = dividends.non_simd_rem(divisors); | ||
let simd_results = unsafe { simd_rem(dividends, divisors) }; | ||
assert_eq!(normal_results, simd_results); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.