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_max* and atomic_umax* intinsics #1232

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions cprover_bindings/src/goto_program/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,13 @@ impl Expr {
let cmp = self.clone().lt(e.clone());
cmp.ternary(self, e)
}

/// `max(self, e)`
pub fn max(self, e: Expr) -> Expr {
assert!(!self.is_side_effect() && !e.is_side_effect());
let cmp = self.clone().gt(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_max" => codegen_atomic_binop!(max),
"atomic_max_acq" => codegen_atomic_binop!(max),
"atomic_max_acqrel" => codegen_atomic_binop!(max),
"atomic_max_rel" => codegen_atomic_binop!(max),
"atomic_max_relaxed" => codegen_atomic_binop!(max),
"atomic_min" => codegen_atomic_binop!(min),
"atomic_min_acq" => codegen_atomic_binop!(min),
"atomic_min_acqrel" => codegen_atomic_binop!(min),
Expand All @@ -435,6 +440,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_umax" => codegen_atomic_binop!(max),
"atomic_umax_acq" => codegen_atomic_binop!(max),
"atomic_umax_acqrel" => codegen_atomic_binop!(max),
"atomic_umax_rel" => codegen_atomic_binop!(max),
"atomic_umax_relaxed" => codegen_atomic_binop!(max),
"atomic_umin" => codegen_atomic_binop!(min),
"atomic_umin_acq" => codegen_atomic_binop!(min),
"atomic_umin_acqrel" => codegen_atomic_binop!(min),
Expand Down
47 changes: 47 additions & 0 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMax/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_max` and other variants (unstable version) return the
// expected result.

#![feature(core_intrinsics)]
use std::intrinsics::{
atomic_max, atomic_max_acq, atomic_max_acqrel, atomic_max_rel, atomic_max_relaxed,
};

#[kani::proof]
fn main() {
let mut a1 = 0 as u8;
let mut a2 = 0 as u8;
let mut a3 = 0 as u8;
let mut a4 = 0 as u8;
let mut a5 = 0 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 = 1 as u8;

unsafe {
let x1 = atomic_max(ptr_a1, b);
let x2 = atomic_max_acq(ptr_a2, b);
let x3 = atomic_max_acqrel(ptr_a3, b);
let x4 = atomic_max_rel(ptr_a4, b);
let x5 = atomic_max_relaxed(ptr_a5, b);

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

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/AtomicMax/max.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMax/max_acq.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMax/max_acqrel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMax/max_rel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicMax/max_relaxed.rs

This file was deleted.

47 changes: 47 additions & 0 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmax/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_umax` and other variants (unstable version) return the
// expected result.

#![feature(core_intrinsics)]
use std::intrinsics::{
atomic_umax, atomic_umax_acq, atomic_umax_acqrel, atomic_umax_rel, atomic_umax_relaxed,
};

#[kani::proof]
fn main() {
let mut a1 = 0 as u8;
let mut a2 = 0 as u8;
let mut a3 = 0 as u8;
let mut a4 = 0 as u8;
let mut a5 = 0 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 = 1 as u8;

unsafe {
let x1 = atomic_umax(ptr_a1, b);
let x2 = atomic_umax_acq(ptr_a2, b);
let x3 = atomic_umax_acqrel(ptr_a3, b);
let x4 = atomic_umax_rel(ptr_a4, b);
let x5 = atomic_umax_relaxed(ptr_a5, b);

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

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/AtomicUmax/umax.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmax/umax_acq.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmax/umax_acqrel.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/kani/Intrinsics/Atomic/Unstable/AtomicUmax/umax_rel.rs

This file was deleted.

This file was deleted.