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 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: 'static + Send + Sync>(
&self,
) -> SimplifiedPatternMatcher<D>
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>
impl<C> Add<&SimplifiedPatternMatcher<C>> for SimplifiedPatternMatcher<C>
Source§type Output = SimplifiedPatternMatcher<C>
type Output = SimplifiedPatternMatcher<C>
+ operator.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.