Skip to content

Commit 497dfd3

Browse files
committed
Fix clippy findings
1 parent a68efb3 commit 497dfd3

13 files changed

Lines changed: 118 additions & 21 deletions

File tree

‎sycl/sycl-rs-sys/build.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn main() {
5454
"include/kernel-bundle.hpp",
5555
];
5656

57-
cxx_build::bridges(&rust_sources)
57+
cxx_build::bridges(rust_sources)
5858
.compiler(&compiler_path)
5959
.flag("-fsycl")
6060
.files(&cpp_sources)

‎sycl/sycl-rs-sys/src/event-sys.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::{Arc, atomic::Ordering::Relaxed};
1010

1111
use crate::types::SharedWaker;
1212

13+
#[allow(clippy::missing_safety_doc)]
1314
#[cxx::bridge(namespace = "sycl_shims::event")]
1415
pub mod ffi {
1516
#[namespace = "sycl_shims"]
@@ -29,6 +30,10 @@ pub mod ffi {
2930

3031
fn wait(event: &mut UniquePtr<Event>) -> Result<()>;
3132

33+
/// # Safety
34+
///
35+
/// `waker` must come from `Arc::into_raw` and transfer one strong reference to the
36+
/// callback, which will consume it after the event completes.
3237
unsafe fn register_callback(
3338
queue: &mut UniquePtr<Queue>,
3439
event: &Event,
@@ -41,12 +46,17 @@ pub mod ffi {
4146

4247
extern "Rust" {
4348
type SharedWaker;
49+
/// # Safety
50+
///
51+
/// `ptr` must come from `Arc::into_raw` and represent a strong reference owned by this
52+
/// callback.
4453
unsafe fn wake(ptr: *const SharedWaker);
4554
}
4655
}
4756

48-
// Safety: SharedWaker must by a pointer created by Arc::into_raw. The caller must increment the
49-
// SharedWaker's strong reference count before calling.
57+
/// # Safety
58+
///
59+
/// `ptr` must come from `Arc::into_raw` and represent a strong reference owned by this callback.
5060
unsafe fn wake(ptr: *const SharedWaker) {
5161
unsafe {
5262
(*ptr).done.store(true, Relaxed);

‎sycl/sycl-rs-sys/src/queue-sys.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// SPDX-License-Identifier: MIT OR Apache-2.0
77
//
88

9+
#[allow(clippy::missing_safety_doc)]
910
#[cxx::bridge(namespace = "sycl_shims::queue")]
1011
pub mod ffi {
1112
#[namespace = "sycl_shims"]
@@ -44,6 +45,10 @@ pub mod ffi {
4445

4546
fn clone(queue: &Queue) -> UniquePtr<Queue>;
4647

48+
/// # Safety
49+
///
50+
/// `ptr` must be valid for writes of `num_bytes` bytes and remain valid until the
51+
/// returned event completes. The memory must not be accessed concurrently.
4752
unsafe fn memset(
4853
queue: &mut UniquePtr<Queue>,
4954
ptr: *mut u8,
@@ -52,6 +57,11 @@ pub mod ffi {
5257
dep_events: Vec<EventPtr>,
5358
) -> Result<UniquePtr<Event>>;
5459

60+
/// # Safety
61+
///
62+
/// `src` and `dest` must be valid for reads and writes, respectively, of `num_bytes`
63+
/// bytes and remain valid until the returned event completes. The regions must not
64+
/// overlap or be accessed concurrently.
5565
unsafe fn memcpy(
5666
queue: &mut UniquePtr<Queue>,
5767
dest: *mut u8,
@@ -67,6 +77,10 @@ pub mod ffi {
6777

6878
fn wait(queue: &mut UniquePtr<Queue>) -> Result<()>;
6979

80+
/// # Safety
81+
///
82+
/// Each entry in `args` must match the corresponding kernel parameter's size, layout,
83+
/// and alignment.
7084
unsafe fn launch_1d(
7185
queue: &mut UniquePtr<Queue>,
7286
global_size: Range1,
@@ -75,6 +89,10 @@ pub mod ffi {
7589
args: &[&[u8]],
7690
) -> Result<UniquePtr<Event>>;
7791

92+
/// # Safety
93+
///
94+
/// Each entry in `args` must match the corresponding kernel parameter's size, layout,
95+
/// and alignment.
7896
unsafe fn launch_2d(
7997
queue: &mut UniquePtr<Queue>,
8098
global_size: Range2,
@@ -83,6 +101,10 @@ pub mod ffi {
83101
args: &[&[u8]],
84102
) -> Result<UniquePtr<Event>>;
85103

104+
/// # Safety
105+
///
106+
/// Each entry in `args` must match the corresponding kernel parameter's size, layout,
107+
/// and alignment.
86108
unsafe fn launch_3d(
87109
queue: &mut UniquePtr<Queue>,
88110
global_size: Range3,

‎sycl/sycl-rs-sys/src/types-sys.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ impl SharedWaker {
2424
}
2525
}
2626

27+
impl Default for SharedWaker {
28+
fn default() -> Self {
29+
Self::new()
30+
}
31+
}
32+
2733
#[cxx::bridge(namespace = "sycl_shims")]
2834
pub mod ffi {
2935
unsafe extern "C++" {

‎sycl/sycl-rs-sys/src/usm-sys.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// SPDX-License-Identifier: MIT OR Apache-2.0
77
//
88

9+
#[allow(clippy::missing_safety_doc)]
910
#[cxx::bridge(namespace = "sycl_shims::usm")]
1011
pub mod ffi {
1112
unsafe extern "C++" {
@@ -15,21 +16,37 @@ pub mod ffi {
1516

1617
extern "C++" {
1718
include!("sycl-rs-sys/include/usm.hpp");
19+
/// # Safety
20+
///
21+
/// `queue` must refer to a valid SYCL queue and `alignment` must be a supported power of
22+
/// two. The returned allocation must be released with `free` using a compatible queue.
1823
unsafe fn aligned_alloc_device(
1924
alignment: usize,
2025
num_bytes: usize,
2126
queue: &Queue,
2227
) -> Result<*mut u8>;
28+
/// # Safety
29+
///
30+
/// `queue` must refer to a valid SYCL queue and `alignment` must be a supported power of
31+
/// two. The returned allocation must be released with `free` using a compatible queue.
2332
unsafe fn aligned_alloc_host(
2433
alignment: usize,
2534
num_bytes: usize,
2635
queue: &Queue,
2736
) -> Result<*mut u8>;
37+
/// # Safety
38+
///
39+
/// `queue` must refer to a valid SYCL queue and `alignment` must be a supported power of
40+
/// two. The returned allocation must be released with `free` using a compatible queue.
2841
unsafe fn aligned_alloc_shared(
2942
alignment: usize,
3043
num_bytes: usize,
3144
queue: &Queue,
3245
) -> Result<*mut u8>;
46+
/// # Safety
47+
///
48+
/// `ptr` must be a live USM allocation associated with `queue` and must not be used after
49+
/// this call.
3350
unsafe fn free(ptr: *mut u8, queue: &Queue);
3451
}
3552
}

‎sycl/sycl-rs/examples/kernel_launch.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async fn main() -> sycl_rs::Result<()> {
3737
queue.launch(
3838
NdRange::new([1024], [16]),
3939
&kernel,
40-
(f16::from_f32(3.14), &mut device_array),
40+
(f16::from_f32(std::f32::consts::PI), &mut device_array),
4141
)
4242
}?
4343
.await?;

‎sycl/sycl-rs/examples/kernel_launch_derive.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() -> sycl_rs::Result<()> {
4242
NdRange::new([1024], [16]),
4343
&kernel,
4444
IotaArgs {
45-
start: 3.14_f32,
45+
start: std::f32::consts::PI,
4646
ptr: &mut array,
4747
},
4848
)

‎sycl/sycl-rs/src/event.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Future for EventFuture {
6060
let this = self.project();
6161

6262
// Set the callback on first Future poll (Futures can't be active until polled)
63-
if *this.set_callback == false {
63+
if !*this.set_callback {
6464
*this.set_callback = true;
6565
let mut queue = Queue::new_immediate();
6666
this.shared.waker.register(cx.waker());

‎sycl/sycl-rs/src/kernel.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ impl From<cxx::UniquePtr<types::ffi::Kernel>> for Kernel {
5151

5252
/// Types which can be passed as SYCL kernel arguments.
5353
///
54-
/// Safety: a type implement this trait must mirror the representation and alignment of the
54+
/// # Safety
55+
///
56+
/// A type implementing this trait must mirror the representation and alignment of the
5557
/// corresponding SYCL kernel argument structure.
5658
pub unsafe trait KernelArgument {
5759
/// Converts self to a raw byte representation.
5860
///
59-
/// Safety: This function returns a reference to raw bytes. These bytes will be passed to FFI
61+
/// # Safety
62+
///
63+
/// This function returns a reference to raw bytes. These bytes will be passed to FFI
6064
/// functions. The caller must make sure these functions respect Rust's aliasing rules.
6165
unsafe fn as_raw_arg(&self) -> &[u8];
6266
}
@@ -69,12 +73,16 @@ unsafe impl<T: Pod> KernelArgument for T {
6973

7074
/// Types which describe an argument list for a SYCL kernel.
7175
///
72-
/// Safety: a type implement this trait must mirror the representation and alignment of each
76+
/// # Safety
77+
///
78+
/// A type implementing this trait must mirror the representation and alignment of each
7379
/// corresponding SYCL kernel argument inside the returned array.
7480
pub unsafe trait KernelArgumentList<const ARGC: usize> {
7581
/// Converts each struct member to a raw byte representation.
7682
///
77-
/// Safety: This function returns references to raw bytes. These bytes will be passed to FFI
83+
/// # Safety
84+
///
85+
/// This function returns references to raw bytes. These bytes will be passed to FFI
7886
/// functions. The caller must make sure these functions respect Rust's aliasing rules.
7987
unsafe fn as_raw_arg_list(&self) -> [&[u8]; ARGC];
8088
}

‎sycl/sycl-rs/src/queue.rs‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ use crate::{
2828
/// a kernel.
2929
pub struct Queue(pub(crate) cxx::UniquePtr<ffi::Queue>);
3030

31+
impl Default for Queue {
32+
fn default() -> Self {
33+
Self::new()
34+
}
35+
}
36+
3137
impl Queue {
3238
/// Construct a `Queue` based on the device returned from the default selector.
3339
pub fn new() -> Self {
@@ -77,28 +83,40 @@ impl Queue {
7783
}
7884

7985
/// Allocates memory and creates a host-side [`UsmBox`] that can store an array of T.
80-
/// Safety: the array contents are uninitialized.
86+
///
87+
/// # Safety
88+
///
89+
/// The array contents are uninitialized and must not be read before they are initialized.
8190
pub unsafe fn alloc_uninit_host<T>(&self, len: usize) -> HostUsmBox<T> {
8291
let allocator = UsmAllocator::from(self);
8392
unsafe { UsmBox::new(allocator, len) }
8493
}
8594

8695
/// Allocates memory and creates a shared [`UsmBox`] that can store an array of T.
87-
/// Safety: the array contents are uninitialized.
96+
///
97+
/// # Safety
98+
///
99+
/// The array contents are uninitialized and must not be read before they are initialized.
88100
pub unsafe fn alloc_uninit_shared<T>(&self, len: usize) -> SharedUsmBox<T> {
89101
let allocator = UsmAllocator::from(self);
90102
unsafe { UsmBox::new(allocator, len) }
91103
}
92104

93105
/// Allocates memory and creates a device-side [`UsmBox`] that can store an array of T.
94-
/// Safety: the array contents are uninitialized.
106+
///
107+
/// # Safety
108+
///
109+
/// The array contents are uninitialized and must not be read before they are initialized.
95110
pub unsafe fn alloc_uninit_device<T>(&self, len: usize) -> DeviceUsmBox<T> {
96111
let allocator = UsmAllocator::from(self);
97112
unsafe { UsmBox::new(allocator, len) }
98113
}
99114

100115
/// Sets memory allocated with USM allocations.
101-
/// Safety: the caller must make sure the underlying memory isn't being aliased somewhere else.
116+
///
117+
/// # Safety
118+
///
119+
/// The caller must ensure the underlying memory is not aliased elsewhere.
102120
pub unsafe fn memset<T, A: UsmAlloc>(
103121
&mut self,
104122
array: &mut UsmBox<T, A>,
@@ -108,7 +126,10 @@ impl Queue {
108126
}
109127

110128
/// Sets memory allocated with USM allocations after all specified events finish.
111-
/// Safety: the caller must make sure the underlying memory isn't being aliased somewhere else.
129+
///
130+
/// # Safety
131+
///
132+
/// The caller must ensure the underlying memory is not aliased elsewhere.
112133
pub unsafe fn memset_with_deps<T, A: UsmAlloc>(
113134
&mut self,
114135
array: &mut UsmBox<T, A>,
@@ -128,7 +149,7 @@ impl Queue {
128149

129150
/// Submits a barrier to the queue.
130151
pub fn barrier(&mut self) -> Result<Event> {
131-
self.barrier_with_deps(&[]).map(Into::into)
152+
self.barrier_with_deps(&[])
132153
}
133154

134155
/// Submits a barrier to the queue after all specified events finish.
@@ -153,7 +174,9 @@ impl Queue {
153174
/// Enqueues a kernel object to the queue as an ND-range kernel, using the number of work-items
154175
/// specified by the [`NdRange`] nd_range.
155176
///
156-
/// Safety: The caller must make sure each argument matches the launched SYCL kernel's
177+
/// # Safety
178+
///
179+
/// The caller must make sure each argument matches the launched SYCL kernel's
157180
/// signature, including their respective size, layout and alignment.
158181
pub unsafe fn launch<const ARGC: usize, const DIMENSIONS: usize>(
159182
&mut self,

0 commit comments

Comments
 (0)