cubecl_runtime/
compiler.rs1use crate::kernel::{CompiledKernel, KernelDefinition, KernelMetadata};
2use alloc::string::String;
3use cubecl_common::{ExecutionMode, backtrace::BackTrace};
4use cubecl_ir::ElemType;
5use thiserror::Error;
6
7pub trait CubeTask<C: Compiler>: KernelMetadata + Send + Sync {
10 fn compile(
12 &self,
13 compiler: &mut C,
14 compilation_options: &C::CompilationOptions,
15 mode: ExecutionMode,
16 ) -> Result<CompiledKernel<C>, CompilationError>;
17}
18
19#[derive(Error, Clone)]
21#[cfg_attr(std_io, derive(serde::Serialize, serde::Deserialize))]
22pub enum CompilationError {
23 #[error(
25 "An unsupported instruction caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
26 )]
27 UnsupportedInstruction {
28 reason: String,
30 #[cfg_attr(std_io, serde(skip))]
32 backtrace: BackTrace,
33 },
34
35 #[error(
37 "An error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
38 )]
39 Generic {
40 reason: String,
42 #[cfg_attr(std_io, serde(skip))]
44 backtrace: BackTrace,
45 },
46 #[error(
48 "A validation error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
49 )]
50 Validation {
51 reason: String,
53 #[cfg_attr(std_io, serde(skip))]
55 backtrace: BackTrace,
56 },
57}
58
59impl core::fmt::Debug for CompilationError {
60 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
61 f.write_fmt(format_args!("{self}"))
62 }
63}
64
65pub trait Compiler: Sync + Send + 'static + Clone + core::fmt::Debug {
67 type Representation: core::fmt::Display;
69 type CompilationOptions: Send + Default + core::fmt::Debug;
71
72 fn compile(
74 &mut self,
75 kernel: KernelDefinition,
76 compilation_options: &Self::CompilationOptions,
77 mode: ExecutionMode,
78 ) -> Result<Self::Representation, CompilationError>;
79
80 fn elem_size(&self, elem: ElemType) -> usize;
82
83 fn extension(&self) -> &'static str;
86}