-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add dynamic borrow checking for dereferencing NumPy arrays.
- Loading branch information
1 parent
61882e3
commit 542b587
Showing
8 changed files
with
221 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,170 @@ | ||
use std::cell::UnsafeCell; | ||
use std::collections::hash_map::{Entry, HashMap}; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use ndarray::{ArrayView, ArrayViewMut, Dimension}; | ||
use pyo3::{FromPyObject, PyAny, PyResult}; | ||
|
||
use crate::array::PyArray; | ||
use crate::dtype::Element; | ||
|
||
thread_local! { | ||
static BORROW_FLAGS: UnsafeCell<HashMap<usize, isize>> = UnsafeCell::new(HashMap::new()); | ||
} | ||
|
||
pub struct PyArrayRef<'a, T, D> { | ||
array: &'a PyArray<T, D>, | ||
view: ArrayView<'a, T, D>, | ||
} | ||
|
||
impl<'a, T, D> Deref for PyArrayRef<'a, T, D> { | ||
type Target = ArrayView<'a, T, D>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.view | ||
} | ||
} | ||
|
||
impl<'py, T: Element, D: Dimension> FromPyObject<'py> for PyArrayRef<'py, T, D> { | ||
fn extract(obj: &'py PyAny) -> PyResult<Self> { | ||
let array: &'py PyArray<T, D> = obj.extract()?; | ||
Ok(array.as_array()) | ||
} | ||
} | ||
|
||
impl<'a, T, D> PyArrayRef<'a, T, D> | ||
where | ||
T: Element, | ||
D: Dimension, | ||
{ | ||
pub(crate) fn try_new(array: &'a PyArray<T, D>) -> Option<Self> { | ||
let address = array as *const PyArray<T, D> as usize; | ||
|
||
BORROW_FLAGS.with(|borrow_flags| { | ||
// SAFETY: Called on a thread local variable in a leaf function. | ||
let borrow_flags = unsafe { &mut *borrow_flags.get() }; | ||
|
||
match borrow_flags.entry(address) { | ||
Entry::Occupied(entry) => { | ||
let readers = entry.into_mut(); | ||
|
||
let new_readers = readers.wrapping_add(1); | ||
|
||
if new_readers <= 0 { | ||
cold(); | ||
return None; | ||
} | ||
|
||
*readers = new_readers; | ||
} | ||
Entry::Vacant(entry) => { | ||
entry.insert(1); | ||
} | ||
} | ||
|
||
// SAFETY: Thread-local borrow flags ensure aliasing discipline on this thread, | ||
// and `PyArray` is neither `Send` nor `Sync` | ||
let view = unsafe { array.as_array_unchecked() }; | ||
|
||
Some(Self { array, view }) | ||
}) | ||
} | ||
} | ||
|
||
impl<'a, T, D> Drop for PyArrayRef<'a, T, D> { | ||
fn drop(&mut self) { | ||
let address = self.array as *const PyArray<T, D> as usize; | ||
|
||
BORROW_FLAGS.with(|borrow_flags| { | ||
// SAFETY: Called on a thread local variable in a leaf function. | ||
let borrow_flags = unsafe { &mut *borrow_flags.get() }; | ||
|
||
let readers = borrow_flags.get_mut(&address).unwrap(); | ||
|
||
*readers -= 1; | ||
|
||
if *readers == 0 { | ||
borrow_flags.remove(&address).unwrap(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
pub struct PyArrayRefMut<'a, T, D> { | ||
array: &'a PyArray<T, D>, | ||
view: ArrayViewMut<'a, T, D>, | ||
} | ||
|
||
impl<'a, T, D> Deref for PyArrayRefMut<'a, T, D> { | ||
type Target = ArrayViewMut<'a, T, D>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.view | ||
} | ||
} | ||
|
||
impl<'a, T, D> DerefMut for PyArrayRefMut<'a, T, D> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.view | ||
} | ||
} | ||
|
||
impl<'py, T: Element, D: Dimension> FromPyObject<'py> for PyArrayRefMut<'py, T, D> { | ||
fn extract(obj: &'py PyAny) -> PyResult<Self> { | ||
let array: &'py PyArray<T, D> = obj.extract()?; | ||
Ok(array.as_array_mut()) | ||
} | ||
} | ||
|
||
impl<'a, T, D> PyArrayRefMut<'a, T, D> | ||
where | ||
T: Element, | ||
D: Dimension, | ||
{ | ||
pub(crate) fn try_new(array: &'a PyArray<T, D>) -> Option<Self> { | ||
let address = array as *const PyArray<T, D> as usize; | ||
|
||
BORROW_FLAGS.with(|borrow_flags| { | ||
// SAFETY: Called on a thread local variable in a leaf function. | ||
let borrow_flags = unsafe { &mut *borrow_flags.get() }; | ||
|
||
match borrow_flags.entry(address) { | ||
Entry::Occupied(entry) => { | ||
let writers = entry.into_mut(); | ||
|
||
if *writers != 0 { | ||
cold(); | ||
return None; | ||
} | ||
|
||
*writers = -1; | ||
} | ||
Entry::Vacant(entry) => { | ||
entry.insert(-1); | ||
} | ||
} | ||
|
||
// SAFETY: Thread-local borrow flags ensure aliasing discipline on this thread, | ||
// and `PyArray` is neither `Send` nor `Sync` | ||
let view = unsafe { array.as_array_mut_unchecked() }; | ||
|
||
Some(Self { array, view }) | ||
}) | ||
} | ||
} | ||
|
||
impl<'a, T, D> Drop for PyArrayRefMut<'a, T, D> { | ||
fn drop(&mut self) { | ||
let address = self.array as *const PyArray<T, D> as usize; | ||
|
||
BORROW_FLAGS.with(|borrow_flags| { | ||
// SAFETY: Called on a thread local variable in a leaf function. | ||
let borrow_flags = unsafe { &mut *borrow_flags.get() }; | ||
|
||
borrow_flags.remove(&address).unwrap(); | ||
}); | ||
} | ||
} | ||
#[cold] | ||
#[inline(always)] | ||
fn cold() {} |
This file contains 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
Oops, something went wrong.