Skip to main content

SimplifiedPatternMatcher

Struct SimplifiedPatternMatcher 

Source
pub struct SimplifiedPatternMatcher<C = ()> { /* private fields */ }
Expand description

High-performance pattern matcher with O(1) OpKey-based dispatch.

§Design

Instead of a single list of patterns that must be linearly scanned, patterns are indexed by their OpKey in a HashMap. When matching:

  1. Extract OpKey from the input UOp
  2. Look up patterns for that key (O(1) HashMap lookup)
  3. Try only those patterns (typically 1-3 per key)
  4. Fall back to wildcard patterns if no match

§Type Parameter

  • C: Context type passed to all pattern closures. Use () for stateless matching.

§Example

Typically used via the patterns! macro:

use morok_macros::patterns;

let matcher = patterns! {
    Add(x, @zero) ~> x,
    Mul(x, @one) ~> x,
};

// Use with graph_rewrite
let result = graph_rewrite(&ast, &matcher, &mut ());

Manual construction (rarely needed):

let mut matcher = SimplifiedPatternMatcher::<()>::new();
matcher.add(
    &[OpKey::Binary(BinaryOp::Add)],
    |uop, _ctx| {
        let Op::Binary(BinaryOp::Add, left, right) = uop.op() else {
            return RewriteResult::NoMatch;
        };
        if is_zero(right) { RewriteResult::Rewritten(left.clone()) }
        else { RewriteResult::NoMatch }
    }
);

Implementations§

Source§

impl<C> SimplifiedPatternMatcher<C>

Source

pub fn new() -> Self

Create a new empty pattern matcher.

Source

pub fn add<F>(&mut self, keys: &[OpKey], closure: F)
where F: Fn(&Arc<UOp>, &mut C) -> RewriteResult + Send + Sync + 'static,

Add pattern for specific OpKey(s).

If keys is empty, the pattern is treated as a wildcard and will be tried for every UOp after all indexed patterns have been tried.

Source

pub fn add_wildcard<F>(&mut self, closure: F)
where F: Fn(&Arc<UOp>, &mut C) -> RewriteResult + Send + Sync + 'static,

Add wildcard pattern (matches any op).

Wildcard patterns are tried after all indexed patterns have been tried.

Source

pub fn len(&self) -> usize

Number of registered patterns.

Source

pub fn is_empty(&self) -> bool

Check if no patterns are registered.

Source

pub fn wildcard_count(&self) -> usize

Number of wildcard patterns (tried for every op).

Source

pub fn indexed_count(&self) -> usize

Number of indexed buckets (unique OpKeys with patterns).

Source

pub fn rewrite(&self, uop: &Arc<UOp>, ctx: &mut C) -> RewriteResult

Attempt to rewrite a UOp using registered patterns.

This is an inherent method that provides the same functionality as Matcher::rewrite() without requiring the trait to be in scope.

§Tracing

Enable debug-level tracing to see pattern matching activity:

RUST_LOG=morok_ir::pattern=debug cargo run
Source§

impl SimplifiedPatternMatcher<()>

Source

pub fn with_context<D: 'static + Send + Sync>( &self, ) -> SimplifiedPatternMatcher<D>

Lift a context-free matcher into any context type.

Since () patterns ignore the context parameter, they can safely run under any D. Each closure is re-wrapped to discard &mut D and pass &mut () to the original. This enables combining context-free matchers with context-dependent ones via +:

let mega = symbolic().with_context::<PcontigConfig>()
    + buffer_removal_with_pcontig(); // TypedPatternMatcher<PcontigConfig>

Trait Implementations§

Source§

impl<C> Add<&SimplifiedPatternMatcher<C>> for SimplifiedPatternMatcher<C>

Source§

type Output = SimplifiedPatternMatcher<C>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &SimplifiedPatternMatcher<C>) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> Add<SimplifiedPatternMatcher<C>> for &SimplifiedPatternMatcher<C>

Source§

type Output = SimplifiedPatternMatcher<C>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: SimplifiedPatternMatcher<C>) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> Add for &SimplifiedPatternMatcher<C>

Source§

type Output = SimplifiedPatternMatcher<C>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> Add for SimplifiedPatternMatcher<C>

Source§

fn add(self, rhs: Self) -> Self::Output

Combine two matchers. Patterns from rhs are appended.

Source§

type Output = SimplifiedPatternMatcher<C>

The resulting type after applying the + operator.
Source§

impl<C> Clone for SimplifiedPatternMatcher<C>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C> Default for SimplifiedPatternMatcher<C>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<C> Matcher<C> for SimplifiedPatternMatcher<C>

Source§

fn rewrite(&self, uop: &Arc<UOp>, ctx: &mut C) -> RewriteResult

Attempt to rewrite a UOp using registered patterns.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more