Skip to main content

morok_codegen/
traits.rs

1//! Core traits for code generation.
2
3use crate::{RenderedKernel, Result};
4use morok_ir::UOp;
5use morok_ir::pattern::TypedPatternMatcher;
6use std::sync::Arc;
7
8/// Backend-agnostic code generation interface.
9///
10/// Implementers generate executable code from optimized UOp graphs.
11/// Different backends (LLVM, CUDA, Metal, etc.) implement this trait
12/// to generate code in their respective formats.
13pub trait Renderer {
14    /// Render a UOp graph into executable code.
15    ///
16    /// Takes an optimized UOp graph (typically from the scheduler/optimizer)
17    /// and generates code that can be compiled and executed.
18    ///
19    /// # Arguments
20    ///
21    /// * `uop` - The root UOp of the computation graph
22    /// * `name` - Optional name for the kernel (used for debugging/caching)
23    ///
24    /// # Returns
25    ///
26    /// A `RenderedKernel` containing the generated code and metadata.
27    fn render(&self, uop: &Arc<UOp>, name: Option<&str>) -> Result<RenderedKernel>;
28
29    /// Get the backend name (e.g., "llvm", "cuda", "metal").
30    fn backend_name(&self) -> &str;
31
32    /// Returns decomposition patterns for operations this backend doesn't support.
33    ///
34    /// Backends that support all transcendental operations natively (e.g., LLVM)
35    /// should return `None`. Backends that need decomposition (e.g., CPU interpreter)
36    /// should return a `PatternMatcher` containing the decomposition rules.
37    ///
38    /// # Example
39    ///
40    /// ```ignore
41    /// fn decompositor(&self) -> Option<TypedPatternMatcher<()>> {
42    ///     // LLVM has native transcendentals
43    ///     None
44    /// }
45    /// ```
46    fn decompositor(&self) -> Option<TypedPatternMatcher<()>>;
47}