1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::{ArenaPtr, Context, OpObj, RewriteResult};

/// The trait for unified rewriting operations.
///
/// TODO: design a better API for this.
pub trait PatternRewriter {
    /// Erase an operation from the IR.
    fn erase_op(&self, ctx: &mut Context, op: ArenaPtr<OpObj>) -> RewriteResult<()>;

    /// Replace an operation with new operation(s)
    fn replace_op(
        &self,
        ctx: &mut Context,
        op: ArenaPtr<OpObj>,
        new_ops: Vec<ArenaPtr<OpObj>>,
    ) -> RewriteResult<()>;
}

/// A pattern for rewriting operations.
///
/// TODO: design a better API for this.
pub trait RewritePattern<Rewriter: PatternRewriter> {
    /// Try to match the pattern with the given operation.
    fn matches(&self, ctx: &Context, op: ArenaPtr<OpObj>) -> RewriteResult<()>;

    /// Rewrite the operation with the pattern.
    fn rewrite(
        &self,
        ctx: &mut Context,
        op: ArenaPtr<OpObj>,
        rewriter: &Rewriter,
    ) -> RewriteResult<()>;
}