Skip to main content

rustc_codegen_llvm/llvm/
offload_ffi.rs

1use std::ffi::{CStr, c_char};
2use std::path::PathBuf;
3use std::sync::OnceLock;
4
5use super::ffi::{Module, Value};
6
7type LLVMRustOffloadMapperFn = unsafe extern "C" fn(&Value, &Value, *const &Value);
8type LLVMRustOffloadWrapImagesFn =
9    for<'ll> unsafe extern "C" fn(Module<'ll>, *const c_char) -> bool;
10
11use rustc_session::config::host_tuple;
12use rustc_session::filesearch;
13
14use crate::llvm;
15
16pub(crate) struct RustOffloadWrapper {
17    LLVMRustOffloadMapper: LLVMRustOffloadMapperFn,
18    LLVMRustOffloadWrapImages: LLVMRustOffloadWrapImagesFn,
19    // Keep the dynamic library loaded while the function pointers are used.
20    _lib: libloading::Library,
21}
22
23#[derive(#[automatically_derived]impl ::core::fmt::Debug for RustOffloadLibraryError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::NotFound { err: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotFound err", &__self_0),
            Self::LoadFailed { err: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "LoadFailed err", &__self_0),
        }
    }
}Debug)]
24pub(crate) enum RustOffloadLibraryError {
25    NotFound { err: String },
26    LoadFailed { err: String },
27}
28
29impl From<libloading::Error> for RustOffloadLibraryError {
30    fn from(err: libloading::Error) -> Self {
31        Self::LoadFailed { err: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", err))
    })format!("{err:?}") }
32    }
33}
34
35static OFFLOAD_INSTANCE: OnceLock<RustOffloadWrapper> = OnceLock::new();
36
37impl RustOffloadWrapper {
38    pub(crate) fn get_or_init(
39        sysroot: &rustc_session::config::Sysroot,
40    ) -> Result<&'static RustOffloadWrapper, RustOffloadLibraryError> {
41        OFFLOAD_INSTANCE.get_or_try_init(|| {
42            let w = Self::call_dynamic(sysroot)?;
43            Ok(w)
44        })
45    }
46
47    pub(crate) fn get_instance() -> &'static RustOffloadWrapper {
48        OFFLOAD_INSTANCE
49            .get()
50            .expect("RustOffloadWrapper not initialized. Call get_or_init with sysroot first.")
51    }
52
53    pub(crate) unsafe fn llvm_rust_offload_wrapper(&self, v1: &Value, v2: &Value, vs: &[&Value]) {
54        unsafe { (self.LLVMRustOffloadMapper)(v1, v2, vs.as_ptr()) }
55    }
56
57    pub(crate) unsafe fn llvm_rust_offload_wrap_images<'ll>(
58        &self,
59        host_m: Module<'ll>,
60        device_image_path: &CStr,
61    ) -> bool {
62        unsafe { (self.LLVMRustOffloadWrapImages)(host_m, device_image_path.as_ptr()) }
63    }
64
65    fn call_dynamic(
66        sysroot: &rustc_session::config::Sysroot,
67    ) -> Result<Self, RustOffloadLibraryError> {
68        let rust_offload_path = Self::get_offload_path(sysroot)?;
69        let lib = unsafe { libloading::Library::new(rust_offload_path)? };
70
71        let llvm_rust_offload_wrapper =
72            *unsafe { lib.get::<LLVMRustOffloadMapperFn>(b"LLVMRustOffloadMapper\0")? };
73        let llvm_rust_offload_wrap_images =
74            *unsafe { lib.get::<LLVMRustOffloadWrapImagesFn>(b"LLVMRustOffloadWrapImages\0")? };
75
76        Ok(Self {
77            LLVMRustOffloadMapper: llvm_rust_offload_wrapper,
78            LLVMRustOffloadWrapImages: llvm_rust_offload_wrap_images,
79            _lib: lib,
80        })
81    }
82
83    fn get_offload_path(
84        sysroot: &rustc_session::config::Sysroot,
85    ) -> Result<PathBuf, RustOffloadLibraryError> {
86        let llvm_version_major = llvm::LLVMRustVersionMajor();
87        let mut searched = Vec::new();
88
89        for root in sysroot.all_paths() {
90            let rust_offload_path = filesearch::make_target_lib_path(root, host_tuple())
91                .join(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("libRustOffload-{0}",
                llvm_version_major))
    })format!("libRustOffload-{llvm_version_major}"))
92                .with_extension(std::env::consts::DLL_EXTENSION);
93
94            if !rust_offload_path.is_file() {
95                searched.push(rust_offload_path);
96                continue;
97            }
98
99            return Ok(rust_offload_path);
100        }
101
102        Err(RustOffloadLibraryError::NotFound {
103            err: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("could not find libRustOffload-{1} in the sysroot candidates:\n* {0}",
                searched.iter().map(|p|
                                p.display().to_string()).collect::<Vec<_>>().join("\n* "),
                llvm_version_major))
    })format!(
104                "could not find libRustOffload-{llvm_version_major} in the sysroot candidates:\n* {}",
105                searched.iter().map(|p| p.display().to_string()).collect::<Vec<_>>().join("\n* ")
106            ),
107        })
108    }
109}