use alloc::{boxed::Box, rc::Rc, vec::Vec};
use cubecl_core::{CubeDim, post_processing::visitor::InstructionVisitor};
use cubecl_ir::{Instruction, Processor, Scope};
use crate::Optimizer;
#[derive(Default)]
pub struct OptimizerBuilder {
transformers: Vec<Rc<dyn IrTransformer>>,
visitors: Vec<Box<dyn InstructionVisitor>>,
processors: Vec<Box<dyn Processor>>,
}
impl OptimizerBuilder {
pub fn with_transformer(mut self, transformer: impl IrTransformer + 'static) -> Self {
self.transformers.push(Rc::new(transformer));
self
}
pub fn with_visitor(mut self, visitor: impl InstructionVisitor + 'static) -> Self {
self.visitors.push(Box::new(visitor));
self
}
pub fn with_processor(mut self, processor: impl Processor + 'static) -> Self {
self.processors.push(Box::new(processor));
self
}
pub fn optimize(self, expand: Scope, cube_dim: CubeDim) -> Optimizer {
Optimizer::new(
expand,
cube_dim,
self.transformers,
self.visitors,
self.processors,
)
}
}
pub enum TransformAction {
Ignore,
Replace(Vec<Instruction>),
Remove,
}
pub trait IrTransformer: core::fmt::Debug {
fn maybe_transform(&self, scope: &Scope, inst: &Instruction) -> TransformAction;
}