Math Dialect match and rewrite patterns

In this file, some match and rewrite patterns has been defined for math operations. At the end of this file, all those rewrite patterns are added to the RewritePatternSet.

  1. When and how are these transformations applied. Are they applied automatically or do I have to specify some options for mlir-opt tool to apply these transformations?

Nothing happens “automatically”, there is always the automation code somewhere. mlir-opt runs passes not patterns, so you need to find a pass that calls this function. Here it is llvm-project/TestPolynomialApproximation.cpp at main · llvm/llvm-project · GitHub. It’s a test pass, so I suppose some downstreams are mixing these patterns with other patterns in their own passes.

Thanks for the reply.
I was actually trying to write my own pass by following code written for the Math dialect. I have written and registered a pass (that performs a pattern match and rewrite transformation) as follows:

namespace {
  struct RedundantTransposeRemoverPass : public PassWrapper<RedundantTransposeRemoverPass, OperationPass<>>{

    RedundantTransposeRemoverPass() = default;

      const RedundantTransposeRemoverPass &pass)
      : PassWrapper(pass) {}

     void runOnOperation() override;

      StringRef getArgument() const final {
    return "remove-redundant-transpose";
  StringRef getDescription() const final {
    return "removes redundant transpose operations";

} //namespace

void RedundantTransposeRemoverPass::runOnOperation() {
  RewritePatternSet patterns(&getContext());


namespace mlir {
namespace mytoy {
void registerRedundantTransposeRemoverPass() {
} // namespace test
} // namespace mlir

Here, mytoy is the namespace of the dialect MyToy that I have registered with the mlir (mlir-opt recognizes and parses the operations associated with this dialect). When I try to run this pass using the following command:

${path_to_build}/bin/mlir-opt -pass-pipeline=remove-redundant-transpose file.mlir -o -

Following error is generated:

MLIR Textual PassPipeline Parser:1:1: error: 'remove-redundant-transpose' does not refer to a registered pass or pass pipeline

What am I missing?

You need to register your pass like here, so mlir-opt can find the pass: Search · registerTestMathPolynomialApproximationPass · GitHub

You also need to apply the patterns when the pass is run like here: llvm-project/TestPolynomialApproximation.cpp at main · llvm/llvm-project · GitHub