Skip to main content

singe_cuda/nvrtc/
context.rs

1use std::sync::Arc;
2
3use super::{CompilationArtifact, OutputKind, Program};
4use crate::{
5    context::Context,
6    error::{Error, Result},
7    jit::JitOptions,
8    library::Library,
9    module::{Module, ModuleImage},
10};
11
12impl Context {
13    /// Retrieves `output` from a compiled NVRTC program and loads it as a module with `jit_options`.
14    ///
15    /// Only PTX, cubin, and Tile IR can be loaded directly.
16    /// LTO IR and `OptiX` IR are rejected; link LTO IR before loading it.
17    ///
18    /// # Errors
19    ///
20    /// Returns an error if `output` is LTO IR or `OptiX` IR, the artifact cannot be retrieved or is empty, the context cannot be bound, the JIT option count exceeds `u32`, CUDA rejects the image or JIT settings, or CUDA returns a null module handle.
21    pub fn load_nvrtc_module(
22        self: &Arc<Self>,
23        program: &Program,
24        output: OutputKind,
25        jit_options: JitOptions<'_>,
26    ) -> Result<Module> {
27        let image = loadable_image(program.artifact(output)?)?;
28        self.load_module(&image, jit_options)
29    }
30
31    /// Retrieves `output` from a compiled NVRTC program and loads it as a CUDA library with `jit_options`.
32    ///
33    /// Only PTX, cubin, and Tile IR can be loaded directly.
34    /// LTO IR and `OptiX` IR are rejected; link LTO IR before loading it.
35    ///
36    /// See [`Self::load_library`] for eager and lazy loading behavior.
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if `output` is LTO IR or `OptiX` IR, the artifact cannot be retrieved or is empty, or [`Self::load_library`] fails.
41    pub fn load_nvrtc_library(
42        self: &Arc<Self>,
43        program: &Program,
44        output: OutputKind,
45        jit_options: JitOptions<'_>,
46    ) -> Result<Library> {
47        let image = loadable_image(program.artifact(output)?)?;
48        self.load_library(&image, jit_options)
49    }
50}
51
52fn loadable_image(artifact: CompilationArtifact) -> Result<ModuleImage<'static>> {
53    artifact
54        .into_image()
55        .map_err(|_| Error::out_of_range("nvrtc output kind"))
56}