Rewriter
Patterns
FPy provides a pattern-based rewriter for transforming FPy programs.
Patterns are specified using the @fpy2.pattern decorator
- fpy2.pattern(func: Callable[[P], R])
Decorator to parse a Python function into an FPy pattern. Constructs an FPy Pattern from a Python function. FPy is a stricter subset of Python, so this decorator will reject any function that is not valid in FPy.
- class fpy2.rewrite.Pattern
Bases:
ABCAbstract base class for FPy IR patterns.
- abstractmethod format() str
Returns a string representation of the pattern.
Matcher
Given a fpy2.Pattern instance,
the fpy2.rewrite.Matcher class the locations within
an FPy program where the pattern matches.
- class fpy2.rewrite.Matcher(pattern: Pattern)
Bases:
objectFPy pattern matcher.
Matches each instance of self.pattern for a program and returns the list of subsitutions for each match.
- class fpy2.rewrite.LocatedMatch(pattern: Pattern, subst: Subst)
Bases:
objectResult of a pattern match.
- class fpy2.rewrite.ExprMatch(pattern: Pattern, subst: Subst, expr: Expr)
Bases:
LocatedMatchResult of pattern matching on an expression.
- class fpy2.rewrite.StmtMatch(pattern: Pattern, subst: Subst, block: StmtBlock, idx: int)
Bases:
LocatedMatchResult of a pattern match: a location and a substitution.
Applier
Given a fpy2.Pattern instance,
the fpy2.rewrite.Applier class applies a substitution,
a mapping from pattern variable to syntax, to produce a new FPy program.
- class fpy2.rewrite.Applier(pattern: Pattern)
Bases:
objectFPy subsitution applier.
Takes a pattern and a substitution and applies the substitution to produce a program (or program fragment).
- apply(pmatch: LocatedMatch)
Applies the substitution to the pattern. The result is always a valid IR fragment (including locally SSA).
Rewrite
The fpy2.rewrite.Rewrite class combines the matcher and applier
to perform a rewrite replacing l with r where the substitution
produced by matches of l are applied to r.
- class fpy2.rewrite.Rewrite(lhs: Pattern, rhs: Pattern, *, name: str | None = None)
Bases:
objectA rewrite rule from L to R.
- apply(func: Function, *, occurence: int = 0, repeat: int = 1)
Applies the rewrite rule to the given pattern.
Optionally specify: - occurence: which match occurence, in traversal order, to rewrite - repeat: how many times to apply the rewrite rule once a match occurs
Raises ValueError if the rewrite rule does not apply.
- apply_all(func: Function)
Applies the rewrite rule to all matching patterns in the given function.
Raises ValueError if the rewrite rule does not apply.
- name: str | None
the name of the rewrite rule