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, StorageType};
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 address_type: StorageType,
20 ) -> Result<CompiledKernel<C>, CompilationError>;
21}
22
23#[derive(Error, Clone)]
25#[cfg_attr(std_io, derive(serde::Serialize, serde::Deserialize))]
26pub enum CompilationError {
27 #[error(
29 "An unsupported instruction caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
30 )]
31 UnsupportedInstruction {
32 reason: String,
34 #[cfg_attr(std_io, serde(skip))]
36 backtrace: BackTrace,
37 },
38
39 #[error(
41 "An error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
42 )]
43 Generic {
44 reason: String,
46 #[cfg_attr(std_io, serde(skip))]
48 backtrace: BackTrace,
49 },
50 #[error(
52 "A validation error caused the compilation to fail\nCaused by:\n {reason}\nBacktrace:\n{backtrace}"
53 )]
54 Validation {
55 reason: String,
57 #[cfg_attr(std_io, serde(skip))]
59 backtrace: BackTrace,
60 },
61}
62
63impl core::fmt::Debug for CompilationError {
64 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65 write!(f, "{self}")
66 }
67}
68
69pub trait Compiler: Sync + Send + 'static + Clone + core::fmt::Debug {
71 type Representation: core::fmt::Display;
73 type CompilationOptions: Send + Default + core::fmt::Debug;
75
76 fn compile(
78 &mut self,
79 kernel: KernelDefinition,
80 compilation_options: &Self::CompilationOptions,
81 mode: ExecutionMode,
82 addr_type: StorageType,
83 ) -> Result<Self::Representation, CompilationError>;
84
85 fn elem_size(&self, elem: ElemType) -> usize;
87
88 fn extension(&self) -> &'static str;
91}