This is an archive of the discontinued LLVM Phabricator instance.

[PatternMatching] Add convenience insert method to OwningRewritePatternList. NFC.
ClosedPublic

Authored by lattner on Mar 21 2021, 10:24 AM.

Details

Summary

This allows adding a C function pointer as a matchAndRewrite style pattern, which
is a very common case. This adopts it in ExpandTanh to show how it reduces a level
of nesting.

We could allow C++ lambdas here, but that doesn't work as well with type inference
in the common case. Instead of:

patterns.insert(convertTanhOp);

you need to specify:

patterns.insert<math::TanhOp>(convertTanhOp);

which is boilerplate'y. Capturing state like this is very uncommon, so we choose
to require clients to define their own structs and use the non-convenience method
when they need to do so.

Diff Detail

Event Timeline

lattner created this revision.Mar 21 2021, 10:24 AM
lattner requested review of this revision.Mar 21 2021, 10:24 AM
lattner updated this revision to Diff 332167.Mar 21 2021, 10:36 AM

Update the docs to mention the new form.

stellaraccident accepted this revision.Mar 21 2021, 11:04 AM
This revision is now accepted and ready to land.Mar 21 2021, 11:04 AM
rriddle accepted this revision.Mar 21 2021, 12:47 PM
rriddle added inline comments.
mlir/include/mlir/IR/PatternMatch.h
796

I strongly prefer something like function_ref, or even llvm::unique_function here.

797

nit: The name folder doesn't really generalize here.

jpienaar accepted this revision.Mar 21 2021, 1:03 PM

LG, thanks

I really like the reduced boilerplate here :)

lattner added inline comments.Mar 21 2021, 9:13 PM
mlir/include/mlir/IR/PatternMatch.h
796

Did you see the comment about this? The downside of this is that it breaks type inference for the simple and most common case, adding boilerplate back for generality that is served by the existing functionality.

797

I'm not sure what you mean. Do you have a suggested replacement for this?

lattner marked an inline comment as not done.Mar 21 2021, 9:15 PM
lattner added inline comments.
mlir/include/mlir/IR/PatternMatch.h
796

Also, llvm::unique_function would add a new dependency on ADT/FunctionExtras.h, which seems like unnecessary overhead for builds.

silvas accepted this revision.Mar 22 2021, 10:04 AM
silvas added a subscriber: silvas.

Nice!

+1 on the c function to allow better type inference. Unless somebody has an immediate suggestion for how to achieve the same with more advanced metaprogramming, this LGTM.

rriddle added inline comments.Mar 22 2021, 10:17 AM
mlir/include/mlir/IR/PatternMatch.h
796

Sorry about that, I'm just missing all comments the past few days. C-function pointer sounds good for now. We can allow lambdas in the future if necessary (it is possible with template meta-programming, but we should only do that if necessary).

797

Sorry for being terse(didn't have a lot of time to comment over the weekend). Folder reminds me of the fold hooks, and not all patterns have fold-like behavior. Could we rename this to something like FnPattern? This is definitely not a blocker (and really not that important given that this is an implementation detail).

Thanks for adding this!

lattner updated this revision to Diff 332364.Mar 22 2021, 11:03 AM
lattner marked an inline comment as not done.

rename class per River's suggestion

This revision was landed with ongoing or failed builds.Mar 22 2021, 11:18 AM
This revision was automatically updated to reflect the committed changes.