File pattern_match.h#

↰ Parent directory (tvm/ir/prim)

Public tool for expression-template based pattern matching.

Definition (tvm/ir/prim/pattern_match.h)#

Detailed Description#

It helps to simplify pattern matching and rewrites. All the patterns are generated via expression template during compile time, so the result code should be as efficient as manually written pattern match code.

The code below shows how to use the pattern matcher.

embed:rst:leading-asterisk * .. code-block:: cpp * * * // max(x + z, y + z) => max(x, y) + z * tvm::PVar<PrimExpr> x, y, z; * * // The following code tries to match the declared pattern. * // Match will fill the result of match into PVar if successful. * // Note that z occurs twice in the pattern, * // an equality check is performed to ensure each occurance of z * // is equivalent to each other. * if (max(x + z, y + z).Match(expr)) { * // Eval evaluates a pattern with the current matched value. * // The filled value is valid until the next call to Match. * return (max(x, y) + z).Eval(); * } * * tvm::PrimVar tx(“tx”), ty(“ty”); * tvm::PVar<IntImm> c; * tvm::PVar<Var> v; * // We can match integer and Var, both of which are * // special case container of Expr * TVM_FFI_ICHECK((v * c).Match(tx * 3)); * TVM_FFI_ICHECK_EQ(c.Eval()->value, 3); * // cannot match c to ty * TVM_FFI_ICHECK(!(v * c).Match(tx * ty)); * *

The pattern matcher is not threadsafe, do not use the same PVar in multiple threads.

Please be aware that the filled value in a PVar can be overriden in the next call to Match.

Includes#

Namespaces#

Classes#

Functions#