1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
use thiserror::Error;
#[allow(non_camel_case_types)] // FIXME: use expect
pub type size_t = ::std::os::raw::c_ulonglong;
#[repr(C)]
pub struct NvptxCompiler {
_private: [u8; 0],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error)]
#[non_exhaustive]
pub enum NvptxError {
#[error("Invalid compiler handle")]
InvalidCompilerHandle,
#[error("Invalid PTX input")]
InvalidInput,
#[error("Compilation failure")]
CompilationFailure,
#[error("Internal error")]
Internal,
#[error("Out of memory")]
OutOfMemory,
#[error("Incomplete compiler invocation")]
CompilerInvocationIncomplete,
#[error("Unsupported PTX version")]
UnsupportedPtxVersion,
#[error("Unsupported dev-side sync")]
UnsupportedDevSideSync,
#[error("Unknown error code")]
UnknownError,
}
impl NvptxError {
const NVPTXCOMPILE_ERROR_COMPILATION_FAILURE: NvptxCompileResult = 3;
const NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE: NvptxCompileResult = 6;
const NVPTXCOMPILE_ERROR_INTERNAL: NvptxCompileResult = 4;
const NVPTXCOMPILE_ERROR_INVALID_COMPILER_HANDLE: NvptxCompileResult = 1;
const NVPTXCOMPILE_ERROR_INVALID_INPUT: NvptxCompileResult = 2;
const NVPTXCOMPILE_ERROR_OUT_OF_MEMORY: NvptxCompileResult = 5;
const NVPTXCOMPILE_ERROR_UNSUPPORTED_DEVSIDE_SYNC: NvptxCompileResult = 8;
const NVPTXCOMPILE_ERROR_UNSUPPORTED_PTX_VERSION: NvptxCompileResult = 7;
const NVPTXCOMPILE_SUCCESS: NvptxCompileResult = 0;
pub const fn try_err_from(result: NvptxCompileResult) -> Result<(), Self> {
match result {
Self::NVPTXCOMPILE_SUCCESS => Ok(()),
Self::NVPTXCOMPILE_ERROR_INVALID_COMPILER_HANDLE => Err(Self::InvalidCompilerHandle),
Self::NVPTXCOMPILE_ERROR_INVALID_INPUT => Err(Self::InvalidInput),
Self::NVPTXCOMPILE_ERROR_COMPILATION_FAILURE => Err(Self::CompilationFailure),
Self::NVPTXCOMPILE_ERROR_INTERNAL => Err(Self::Internal),
Self::NVPTXCOMPILE_ERROR_OUT_OF_MEMORY => Err(Self::OutOfMemory),
Self::NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE => {
Err(Self::CompilerInvocationIncomplete)
},
Self::NVPTXCOMPILE_ERROR_UNSUPPORTED_PTX_VERSION => Err(Self::UnsupportedPtxVersion),
Self::NVPTXCOMPILE_ERROR_UNSUPPORTED_DEVSIDE_SYNC => Err(Self::UnsupportedDevSideSync),
_ => Err(Self::UnknownError),
}
}
}
/// [`NvptxCompilerHandle`] represents a handle to the PTX Compiler.
///
/// To compile a PTX program string, an instance of [`NvptxCompiler`]
/// must be created and the handle to it must be obtained using the
/// API [`nvPTXCompilerCreate`]. Then the compilation can be done
/// using the API [`nvPTXCompilerCompile`].
pub type NvptxCompilerHandle = *mut NvptxCompiler;
/// The [`NvptxCompiler`] APIs return the [`NvptxCompileResult`] codes to
/// indicate the call result"]
pub type NvptxCompileResult = ::std::os::raw::c_int;
extern "C" {
/// Queries the current major and minor version of PTX Compiler APIs being
/// used.
///
/// # Parameters
/// - [out] `major`: Major version of the PTX Compiler APIs
/// - [out] `minor`: Minor version of the PTX Compiler APIs
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
///
/// # Note
/// The version of PTX Compiler APIs follows the CUDA Toolkit versioning.
/// The PTX ISA version supported by a PTX Compiler API version is listed
/// [here](https://docs.nvidia.com/cuda/parallel-thread-execution/#release-notes).
pub fn nvPTXCompilerGetVersion(
major: *mut ::std::os::raw::c_uint,
minor: *mut ::std::os::raw::c_uint,
) -> NvptxCompileResult;
/// Obtains the handle to an instance of the PTX compiler
/// initialized with the given PTX program `ptxCode`.
///
/// # Parameters
/// - [out] `compiler`: Returns a handle to PTX compiler initialized with
/// the PTX program `ptxCode`
/// - [in] `ptxCodeLen`: Size of the PTX program `ptxCode` passed as a
/// string
/// - [in] `ptxCode`: The PTX program which is to be compiled passed as a
/// string
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_OUT_OF_MEMORY`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
pub fn nvPTXCompilerCreate(
compiler: *mut NvptxCompilerHandle,
ptxCodeLen: size_t,
ptxCode: *const ::std::os::raw::c_char,
) -> NvptxCompileResult;
/// Destroys and cleans the already created PTX compiler.
///
/// # Parameters
/// - [in] `compiler`: A handle to the PTX compiler which is to be
/// destroyed.
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_OUT_OF_MEMORY`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
pub fn nvPTXCompilerDestroy(compiler: *mut NvptxCompilerHandle) -> NvptxCompileResult;
/// Compile a PTX program with the given compiler options.
///
/// # Parameters
/// - [in, out] `compiler`: A handle to PTX compiler initialized with the
/// PTX program which is to be compiled. The compiled program can be
/// accessed using the handle.
/// - [in] `numCompileOptions`: Length of the array `compileOptions`
/// - [in] `compileOptions`: Compiler options with which compilation should
/// be done. The compiler options string is a null terminated character
/// array. A valid list of compiler options is available at
/// [link](http://docs.nvidia.com/cuda/ptx-compiler-api/index.html#compile-options).
///
/// # Note
/// `--gpu-name` (`-arch`) is a mandatory option.
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_OUT_OF_MEMORY`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_COMPILATION_FAILURE`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_UNSUPPORTED_PTX_VERSION`]
pub fn nvPTXCompilerCompile(
compiler: NvptxCompilerHandle,
numCompileOptions: ::std::os::raw::c_int,
compileOptions: *const *const ::std::os::raw::c_char,
) -> NvptxCompileResult;
/// Obtains the size of the image of the compiled program.
///
/// # Parameters
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `binaryImageSize`: The size of the image of the compiled program
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE`]
///
/// # Note
/// The [`nvPTXCompilerCompile`] function should be invoked for the handle
/// before calling this API. Otherwise,
/// [`NvptxCompileResult::NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE`]
/// is returned.
pub fn nvPTXCompilerGetCompiledProgramSize(
compiler: NvptxCompilerHandle,
binaryImageSize: *mut size_t,
) -> NvptxCompileResult;
/// Obtains the image of the compiled program.
///
/// # Parameters
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `binaryImage`: The image of the compiled program. The caller
/// should allocate memory for `binaryImage`.
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE`]
///
/// # Note
/// The [`nvPTXCompilerCompile`] function should be invoked for the handle
/// before calling this API. Otherwise,
/// [`NvptxCompileResult::NVPTXCOMPILE_ERROR_COMPILER_INVOCATION_INCOMPLETE`]
/// is returned.
pub fn nvPTXCompilerGetCompiledProgram(
compiler: NvptxCompilerHandle,
binaryImage: *mut ::std::os::raw::c_void,
) -> NvptxCompileResult;
/// Query the size of the error message that was seen previously for the
/// handle.
///
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `errorLogSize`: The size of the error log in bytes which was
/// produced in previous call to [`nvPTXCompilerCompile`].
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
pub fn nvPTXCompilerGetErrorLogSize(
compiler: NvptxCompilerHandle,
errorLogSize: *mut size_t,
) -> NvptxCompileResult;
/// Query the error message that was seen previously for the handle.
///
/// # Parameters
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `errorLog`: The error log which was produced in previous call to
/// [`nvPTXCompilerCompile`]. The caller should allocate memory for
/// `errorLog`.
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
pub fn nvPTXCompilerGetErrorLog(
compiler: NvptxCompilerHandle,
errorLog: *mut ::std::os::raw::c_char,
) -> NvptxCompileResult;
/// Query the size of the information message that was seen previously for
/// the handle.
///
/// # Parameters
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `infoLogSize`: The size of the information log in bytes which
/// was produced in previous call to [`nvPTXCompilerCompile`].
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
pub fn nvPTXCompilerGetInfoLogSize(
compiler: NvptxCompilerHandle,
infoLogSize: *mut size_t,
) -> NvptxCompileResult;
/// Query the information message that was seen previously for the handle.
///
/// # Parameters
/// - [in] `compiler`: A handle to PTX compiler on which
/// [`nvPTXCompilerCompile`] has been performed.
/// - [out] `infoLog`: The information log which was produced in previous
/// call to [`nvPTXCompilerCompile`]. The caller should allocate memory
/// for `infoLog`.
///
/// # Returns
/// - [`NvptxCompileResult::NVPTXCOMPILE_SUCCESS`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INTERNAL`]
/// - [`NvptxCompileResult::NVPTXCOMPILE_ERROR_INVALID_PROGRAM_HANDLE`]
pub fn nvPTXCompilerGetInfoLog(
compiler: NvptxCompilerHandle,
infoLog: *mut ::std::os::raw::c_char,
) -> NvptxCompileResult;
}