cubecl_opt/
transformers.rs1use alloc::{boxed::Box, rc::Rc, vec::Vec};
2use cubecl_core::{CubeDim, post_processing::visitor::InstructionVisitor};
3use cubecl_ir::{Instruction, Processor, Scope};
4
5use crate::Optimizer;
6
7#[derive(Default)]
9pub struct OptimizerBuilder {
10 transformers: Vec<Rc<dyn IrTransformer>>,
11 visitors: Vec<Box<dyn InstructionVisitor>>,
12 processors: Vec<Box<dyn Processor>>,
13}
14
15impl OptimizerBuilder {
16 pub fn with_transformer(mut self, transformer: impl IrTransformer + 'static) -> Self {
18 self.transformers.push(Rc::new(transformer));
19 self
20 }
21
22 pub fn with_visitor(mut self, visitor: impl InstructionVisitor + 'static) -> Self {
23 self.visitors.push(Box::new(visitor));
24 self
25 }
26
27 pub fn with_processor(mut self, processor: impl Processor + 'static) -> Self {
28 self.processors.push(Box::new(processor));
29 self
30 }
31
32 pub fn optimize(self, expand: Scope, cube_dim: CubeDim) -> Optimizer {
34 Optimizer::new(
35 expand,
36 cube_dim,
37 self.transformers,
38 self.visitors,
39 self.processors,
40 )
41 }
42}
43
44pub enum TransformAction {
46 Ignore,
48 Replace(Vec<Instruction>),
50 Remove,
52}
53
54pub trait IrTransformer: core::fmt::Debug {
56 fn maybe_transform(&self, scope: &Scope, inst: &Instruction) -> TransformAction;
58}