From adaed094a8316cbf7a8fc07f7a82d52e77bdaa6e Mon Sep 17 00:00:00 2001 From: Rafal Rudnicki Date: Thu, 29 Oct 2026 14:48:32 +0000 Subject: [PATCH] Improve SYCL error handling --- sycl/sycl-rs-sys/src/context-sys.rs | 2 +- sycl/sycl-rs-sys/src/device-sys.rs | 2 +- sycl/sycl-rs/examples/sycl-ls.rs | 7 +++---- sycl/sycl-rs/src/context.rs | 7 +++++-- sycl/sycl-rs/src/info/device-info.rs | 14 +++----------- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/sycl/sycl-rs-sys/src/context-sys.rs b/sycl/sycl-rs-sys/src/context-sys.rs index 3e89230..e227c3e 100644 --- a/sycl/sycl-rs-sys/src/context-sys.rs +++ b/sycl/sycl-rs-sys/src/context-sys.rs @@ -20,6 +20,6 @@ pub mod ffi { #[namespace = "sycl_shims"] type Context = crate::types::ffi::Context; - fn new_context(devices: Vec) -> UniquePtr; + fn new_context(devices: Vec) -> Result>; } } diff --git a/sycl/sycl-rs-sys/src/device-sys.rs b/sycl/sycl-rs-sys/src/device-sys.rs index 55c73e4..30f9424 100644 --- a/sycl/sycl-rs-sys/src/device-sys.rs +++ b/sycl/sycl-rs-sys/src/device-sys.rs @@ -31,7 +31,7 @@ pub mod ffi { fn get_device_type(device: &Device) -> DeviceType; fn get_version(device: &Device) -> String; fn get_name(device: &Device) -> String; - fn get_pci_bdf_address(device: &Device) -> String; + fn get_pci_bdf_address(device: &Device) -> Result; fn enable_peer_access(device: &mut UniquePtr, peer: &Device) -> Result<()>; fn disable_peer_access(device: &mut UniquePtr, peer: &Device) -> Result<()>; diff --git a/sycl/sycl-rs/examples/sycl-ls.rs b/sycl/sycl-rs/examples/sycl-ls.rs index 8a3721e..834754d 100644 --- a/sycl/sycl-rs/examples/sycl-ls.rs +++ b/sycl/sycl-rs/examples/sycl-ls.rs @@ -17,10 +17,9 @@ fn main() { let device_version = device.get_info::(); let platform_version = platform.get_info::(); - let pci_bdf_address = if device.has(Aspect::ExtIntelPciAddress) { - device.get_info::() - } else { - String::from("N/A") + let pci_bdf_address = match device.get_info::() { + Ok(address) => address, + Err(_) => String::from("N/A"), }; let integrated_status = if device.has(Aspect::ExtOneapiIsIntegratedGpu) { diff --git a/sycl/sycl-rs/src/context.rs b/sycl/sycl-rs/src/context.rs index 8ae837c..60ea510 100644 --- a/sycl/sycl-rs/src/context.rs +++ b/sycl/sycl-rs/src/context.rs @@ -21,7 +21,10 @@ impl From> for Context { } impl Context { - pub fn new(devices: &[&Device]) -> Self { + /// Constructs a context for devices from the same platform. + /// + /// Returns an error if the device list is empty or context construction fails. + pub fn new(devices: &[&Device]) -> Result { let devices = devices .iter() .map(|d| DevicePtr { @@ -29,7 +32,7 @@ impl Context { }) .collect::>(); - ffi::new_context(devices).into() + ffi::new_context(devices).map(Into::into) } pub fn create_kernel_bundle_from_source(&self, source: &str) -> Result { diff --git a/sycl/sycl-rs/src/info/device-info.rs b/sycl/sycl-rs/src/info/device-info.rs index 41ec9a2..d7ffcaa 100644 --- a/sycl/sycl-rs/src/info/device-info.rs +++ b/sycl/sycl-rs/src/info/device-info.rs @@ -8,11 +8,7 @@ use sycl_rs_sys::device::ffi; -use crate::{ - device::{Aspect, Device}, - info::Info, - private::Sealed, -}; +use crate::{Result, device::Device, info::Info, private::Sealed}; /// Returns the device type associated with the device. May not return `sycl_rs::info::DeviceType::All` pub struct DeviceType; @@ -48,17 +44,13 @@ impl Info for Name { } /// Returns the PCI BDF address reported by the Intel SYCL extension. -/// Panics when the device does not support the extension. +/// Returns an error when the device does not support the extension or the query fails. pub struct PciBdfAddress; impl Sealed for PciBdfAddress {} impl Info for PciBdfAddress { - type Item = String; + type Item = Result; type Target = Device; fn get_item(target: &Self::Target) -> Self::Item { - assert!( - target.has(Aspect::ExtIntelPciAddress), - "device does not support the Intel PCI address extension" - ); ffi::get_pci_bdf_address(&target.0) } }