cubecl_runtime/
compiler.rs1use crate::kernel::KernelDefinition;
2use alloc::string::{String, ToString};
3use cubecl_environment::backtrace::BackTrace;
4use thiserror::Error;
5
6#[derive(Error, Clone)]
8#[cfg_attr(serializable, derive(serde::Serialize, serde::Deserialize))]
9pub enum CompilationError {
10 #[error(
12 "An unsupported instruction caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
13 )]
14 UnsupportedInstruction {
15 reason: String,
17 #[cfg_attr(serializable, serde(skip))]
19 backtrace: BackTrace,
20 },
21
22 #[error(
24 "An error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
25 )]
26 Generic {
27 reason: String,
29 #[cfg_attr(serializable, serde(skip))]
31 backtrace: BackTrace,
32 },
33 #[error(
35 "A validation error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
36 )]
37 Validation {
38 reason: String,
40 #[cfg_attr(serializable, serde(skip))]
42 backtrace: BackTrace,
43 },
44}
45
46impl core::fmt::Debug for CompilationError {
47 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48 write!(f, "{self}")
49 }
50}
51
52impl From<pliron::result::Error> for CompilationError {
53 fn from(value: pliron::result::Error) -> Self {
54 CompilationError::Validation {
55 reason: value.to_string(),
56 backtrace: BackTrace::capture(),
57 }
58 }
59}
60
61pub trait Compiler: Sync + Send + 'static + Clone + core::fmt::Debug {
63 type Representation: core::fmt::Display;
65 type CompilationOptions: Send + Default + core::fmt::Debug;
67
68 fn compile(
70 &mut self,
71 kernel: KernelDefinition,
72 compilation_options: &Self::CompilationOptions,
73 ) -> Result<Self::Representation, CompilationError>;
74
75 fn buffer_io(
85 _repr: &Self::Representation,
86 ) -> Option<alloc::vec::Vec<crate::kernel::BufferIOAttr>> {
87 None
88 }
89
90 fn extension(&self) -> &'static str;
93
94 fn lang_tag(&self) -> &'static str;
100}