This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Introduce a helper function for staged pattern application
ClosedPublic

Authored by nicolasvasilache on May 6 2020, 1:27 PM.

Details

Summary

This revision introduces a helper function to allow applying rewrite patterns, interleaved with more global transformations, in a staged fashion:

  1. the first stage consists of an OwningRewritePatternList. The RewritePattern in this list are applied once and in order.
  2. the second stage consists of a single OwningRewritePattern that is applied greedily until convergence.
  3. the third stage consists of applying a lambda, generally used for non-local transformation effects.

This allows creating custom fused transformations where patterns can be ordered and applied at a finer granularity than a sequence of traditional compiler passes.

A test that exercises these behaviors is added.

Diff Detail

Event Timeline

Herald added a reviewer: aartbik. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript

Add missing comment.

rriddle added inline comments.May 6 2020, 3:01 PM
mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
386

Only use std::function if you need to store it, otherwise use function_ref.

ftynse requested changes to this revision.May 7 2020, 1:57 AM
ftynse added inline comments.
mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
375

it's a list of OwningRewritePatternLists

376

this does not match the code

377

OwningRewritePatternList

384

ArrayRef

385

const &

mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp
206

const &

208

May be you want to report which stage

208

Don't we want to bail out if we could not converge in one of the applications?

mlir/test/lib/Transforms/TestLinalgTransforms.cpp
176

Could we just add a constructor template to OwningRewritePatternList that forwards its arguments to insert? This would remove the need to std::move and call insert here, and looks generally useful for lists that need only one call to insert.

This revision now requires changes to proceed.May 7 2020, 1:57 AM
nicolasvasilache marked 10 inline comments as done.

Address review.

Address review.

ftynse accepted this revision.May 9 2020, 2:28 AM
ftynse added inline comments.
mlir/include/mlir/IR/PatternMatch.h
394

Doc doesn't match the code

397

This looks like it's calling the move constructor of T (pattern class), not constructing the pattern in-place from the arguments. I was thinking about literally forwarding to insert, something like

template <typename... Ts, typename ConstructorArg,
            typename... ConstructorArgs,
            typename = std::enable_if_t<sizeof...(Ts) != 0>>
OwningRewritePatternList(ConstructorArg &&arg, ConstructorArgs &&... args) {
  insert<Ts...>(std::forward<ConstructorArg>(arg), std::forward<ConstructorArgs>(args)...);
}

but I guess the pattern-move constructor works too, if you fix the documentation.

nicolasvasilache marked 3 inline comments as done.May 11 2020, 1:28 PM
nicolasvasilache added inline comments.
mlir/include/mlir/IR/PatternMatch.h
397

Yeah, didn't manage to get the combination of emplace_back + templated ctor to work so I went with the move constructor. Feel free to give it a shot if you find the templatefu better (I don't necessarily).

nicolasvasilache marked an inline comment as done.

Disable test temporarily.

This revision was not accepted when it landed; it landed in state Needs Review.May 11 2020, 2:02 PM
This revision was automatically updated to reflect the committed changes.
rriddle added inline comments.May 27 2020, 12:22 PM
mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
386

nit: Drop the llvm::

mlir/include/mlir/IR/PatternMatch.h
397

You aren't properly forwarding t here, you need std::forward<T>(t)

mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp
205

nit: Drop the llvm::

208

Why aren't these llvm::dbgs calls wrapped by LLVM_DEBUG?