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:
- Extract
OpKeyfrom the input UOp - Look up patterns for that key (O(1) HashMap lookup)
- Try only those patterns (typically 1-3 per key)
- 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>
impl<C> SimplifiedPatternMatcher<C>
Sourcepub fn new() -> SimplifiedPatternMatcher<C>
pub fn new() -> SimplifiedPatternMatcher<C>
Create a new empty pattern matcher.
Sourcepub fn add<F>(&mut self, keys: &[OpKey], closure: F)
pub fn add<F>(&mut self, keys: &[OpKey], closure: F)
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.
Sourcepub fn add_wildcard<F>(&mut self, closure: F)
pub fn add_wildcard<F>(&mut self, closure: F)
Add wildcard pattern (matches any op).
Wildcard patterns are tried after all indexed patterns have been tried.
Sourcepub fn wildcard_count(&self) -> usize
pub fn wildcard_count(&self) -> usize
Number of wildcard patterns (tried for every op).
Sourcepub fn indexed_count(&self) -> usize
pub fn indexed_count(&self) -> usize
Number of indexed buckets (unique OpKeys with patterns).
Sourcepub fn rewrite(&self, uop: &Arc<UOp>, ctx: &mut C) -> RewriteResult
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 runSource§impl SimplifiedPatternMatcher
impl SimplifiedPatternMatcher
Sourcepub fn with_context<D>(&self) -> SimplifiedPatternMatcher<D>
pub fn with_context<D>(&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>
impl<C> Add<&SimplifiedPatternMatcher<C>> for SimplifiedPatternMatcher<C>
Source§type Output = SimplifiedPatternMatcher<C>
type Output = SimplifiedPatternMatcher<C>
+ operator.Source§fn add(
self,
rhs: &SimplifiedPatternMatcher<C>,
) -> <SimplifiedPatternMatcher<C> as Add<&SimplifiedPatternMatcher<C>>>::Output
fn add( self, rhs: &SimplifiedPatternMatcher<C>, ) -> <SimplifiedPatternMatcher<C> as Add<&SimplifiedPatternMatcher<C>>>::Output
+ operation. Read moreSource§impl<C> Add<SimplifiedPatternMatcher<C>> for &SimplifiedPatternMatcher<C>
impl<C> Add<SimplifiedPatternMatcher<C>> for &SimplifiedPatternMatcher<C>
Source§type Output = SimplifiedPatternMatcher<C>
type Output = SimplifiedPatternMatcher<C>
+ operator.Source§fn add(
self,
rhs: SimplifiedPatternMatcher<C>,
) -> <&SimplifiedPatternMatcher<C> as Add<SimplifiedPatternMatcher<C>>>::Output
fn add( self, rhs: SimplifiedPatternMatcher<C>, ) -> <&SimplifiedPatternMatcher<C> as Add<SimplifiedPatternMatcher<C>>>::Output
+ operation. Read moreSource§impl<C> Add for &SimplifiedPatternMatcher<C>
impl<C> Add for &SimplifiedPatternMatcher<C>
Source§type Output = SimplifiedPatternMatcher<C>
type Output = SimplifiedPatternMatcher<C>
+ operator.Source§fn add(
self,
rhs: &SimplifiedPatternMatcher<C>,
) -> <&SimplifiedPatternMatcher<C> as Add>::Output
fn add( self, rhs: &SimplifiedPatternMatcher<C>, ) -> <&SimplifiedPatternMatcher<C> as Add>::Output
+ operation. Read moreSource§impl<C> Add for SimplifiedPatternMatcher<C>
impl<C> Add for SimplifiedPatternMatcher<C>
Source§fn add(
self,
rhs: SimplifiedPatternMatcher<C>,
) -> <SimplifiedPatternMatcher<C> as Add>::Output
fn add( self, rhs: SimplifiedPatternMatcher<C>, ) -> <SimplifiedPatternMatcher<C> as Add>::Output
Combine two matchers. Patterns from rhs are appended.
Source§type Output = SimplifiedPatternMatcher<C>
type Output = SimplifiedPatternMatcher<C>
+ operator.Source§impl<C> Clone for SimplifiedPatternMatcher<C>
impl<C> Clone for SimplifiedPatternMatcher<C>
Source§fn clone(&self) -> SimplifiedPatternMatcher<C>
fn clone(&self) -> SimplifiedPatternMatcher<C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<C> Default for SimplifiedPatternMatcher<C>
impl<C> Default for SimplifiedPatternMatcher<C>
Source§fn default() -> SimplifiedPatternMatcher<C>
fn default() -> SimplifiedPatternMatcher<C>
Auto Trait Implementations§
impl<C> Freeze for SimplifiedPatternMatcher<C>
impl<C = ()> !RefUnwindSafe for SimplifiedPatternMatcher<C>
impl<C> Send for SimplifiedPatternMatcher<C>
impl<C> Sync for SimplifiedPatternMatcher<C>
impl<C> Unpin for SimplifiedPatternMatcher<C>
impl<C> UnsafeUnpin for SimplifiedPatternMatcher<C>
impl<C = ()> !UnwindSafe for SimplifiedPatternMatcher<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more