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