Skip to content
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

Add support for atomic_min* and atomic_umin* intrinsics #1212

Merged
merged 8 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cprover_bindings/src/goto_program/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,14 @@ impl Expr {
pub fn r_ok(self, e: Expr) -> Expr {
self.binop(ROk, e)
}

// Expressions defined on top of other expressions

/// `min(self, e)`
pub fn min(self, e: Expr) -> Expr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are side effects, this will evaluate them twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the new changes, which complete the definition for is_side_effect and assert that neither of these cause side effects.

let cmp = self.clone().lt(e.clone());
cmp.ternary(self, e)
}
}

/// Constructors for self operations
Expand Down
10 changes: 10 additions & 0 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ impl<'tcx> GotocCtx<'tcx> {
"atomic_load_acq" => self.codegen_atomic_load(intrinsic, fargs, p, loc),
"atomic_load_relaxed" => self.codegen_atomic_load(intrinsic, fargs, p, loc),
"atomic_load_unordered" => self.codegen_atomic_load(intrinsic, fargs, p, loc),
"atomic_min" => codegen_atomic_binop!(min),
"atomic_min_acq" => codegen_atomic_binop!(min),
"atomic_min_acqrel" => codegen_atomic_binop!(min),
"atomic_min_rel" => codegen_atomic_binop!(min),
"atomic_min_relaxed" => codegen_atomic_binop!(min),
"atomic_nand" => codegen_atomic_binop!(bitnand),
"atomic_nand_acq" => codegen_atomic_binop!(bitnand),
"atomic_nand_acqrel" => codegen_atomic_binop!(bitnand),
Expand All @@ -430,6 +435,11 @@ impl<'tcx> GotocCtx<'tcx> {
"atomic_store_rel" => self.codegen_atomic_store(intrinsic, fargs, p, loc),
"atomic_store_relaxed" => self.codegen_atomic_store(intrinsic, fargs, p, loc),
"atomic_store_unordered" => self.codegen_atomic_store(intrinsic, fargs, p, loc),
"atomic_umin" => codegen_atomic_binop!(min),
"atomic_umin_acq" => codegen_atomic_binop!(min),
"atomic_umin_acqrel" => codegen_atomic_binop!(min),
"atomic_umin_rel" => codegen_atomic_binop!(min),
"atomic_umin_relaxed" => codegen_atomic_binop!(min),
"atomic_xadd" => codegen_atomic_binop!(plus),
"atomic_xadd_acq" => codegen_atomic_binop!(plus),
"atomic_xadd_acqrel" => codegen_atomic_binop!(plus),
Expand Down
47 changes: 47 additions & 0 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Check that `atomic_min` and other variants (unstable version) return the
// expected result.

#![feature(core_intrinsics)]
use std::intrinsics::{
atomic_min, atomic_min_acq, atomic_min_acqrel, atomic_min_rel, atomic_min_relaxed,
};

#[kani::proof]
fn main() {
let mut a1 = 1 as u8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we only test for u8? Could we make this take a <T> and then test multiple bitwidths?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's pending in #25. But I'd like to do it once all atomic tests are in.

let mut a2 = 1 as u8;
let mut a3 = 1 as u8;
let mut a4 = 1 as u8;
let mut a5 = 1 as u8;

let ptr_a1: *mut u8 = &mut a1;
let ptr_a2: *mut u8 = &mut a2;
let ptr_a3: *mut u8 = &mut a3;
let ptr_a4: *mut u8 = &mut a4;
let ptr_a5: *mut u8 = &mut a5;

let b = 0 as u8;

unsafe {
let x1 = atomic_min(ptr_a1, b);
let x2 = atomic_min_acq(ptr_a2, b);
let x3 = atomic_min_acqrel(ptr_a3, b);
let x4 = atomic_min_rel(ptr_a4, b);
let x5 = atomic_min_relaxed(ptr_a5, b);

assert!(x1 == 1);
assert!(x2 == 1);
assert!(x3 == 1);
assert!(x4 == 1);
assert!(x5 == 1);

assert!(*ptr_a1 == b);
assert!(*ptr_a2 == b);
assert!(*ptr_a3 == b);
assert!(*ptr_a4 == b);
assert!(*ptr_a5 == b);
}
}
22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/min.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/min_acq.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/min_acqrel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/min_rel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMin/min_relaxed.rs

This file was deleted.

47 changes: 47 additions & 0 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Check that `atomic_umin` and other variants (unstable version) return the
// expected result.

#![feature(core_intrinsics)]
use std::intrinsics::{
atomic_umin, atomic_umin_acq, atomic_umin_acqrel, atomic_umin_rel, atomic_umin_relaxed,
};

#[kani::proof]
fn main() {
let mut a1 = 1 as u8;
let mut a2 = 1 as u8;
let mut a3 = 1 as u8;
let mut a4 = 1 as u8;
let mut a5 = 1 as u8;

let ptr_a1: *mut u8 = &mut a1;
let ptr_a2: *mut u8 = &mut a2;
let ptr_a3: *mut u8 = &mut a3;
let ptr_a4: *mut u8 = &mut a4;
let ptr_a5: *mut u8 = &mut a5;

let b = 0 as u8;

unsafe {
let x1 = atomic_umin(ptr_a1, b);
let x2 = atomic_umin_acq(ptr_a2, b);
let x3 = atomic_umin_acqrel(ptr_a3, b);
let x4 = atomic_umin_rel(ptr_a4, b);
let x5 = atomic_umin_relaxed(ptr_a5, b);

assert!(x1 == 1);
assert!(x2 == 1);
assert!(x3 == 1);
assert!(x4 == 1);
assert!(x5 == 1);

assert!(*ptr_a1 == b);
assert!(*ptr_a2 == b);
assert!(*ptr_a3 == b);
assert!(*ptr_a4 == b);
assert!(*ptr_a5 == b);
}
}
22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmin/umin.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmin/umin_acq.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmin/umin_acqrel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmin/umin_rel.rs

This file was deleted.

This file was deleted.