This is an archive of the discontinued LLVM Phabricator instance.

[mlir:PDL] Expand how native constraint/rewrite functions can be defined
ClosedPublic

Authored by rriddle on Mar 19 2022, 3:09 PM.

Details

Summary

This commit refactors the expected form of native constraint and rewrite
functions, and greatly reduces the necessary user complexity required when
defining a native function. Namely, this commit adds in automatic processing
of the necessary PDLValue glue code, and allows for users to define
constraint/rewrite functions using the C++ types that they actually want to
use.

As an example, lets see a simple example rewrite defined today:

static void rewriteFn(PatternRewriter &rewriter, PDLResultList &results,
                      ArrayRef<PDLValue> args) {
  ValueRange operandValues = args[0].cast<ValueRange>();
  TypeRange typeValues = args[1].cast<TypeRange>();
  ...
  // Create an operation at some point and pass it back to PDL.
  Operation *op = rewriter.create<SomeOp>(...);
  results.push_back(op);
}

After this commit, that same rewrite could be defined as:

static Operation *rewriteFn(PatternRewriter &rewriter ValueRange operandValues,
                            TypeRange typeValues) {
  ...
  // Create an operation at some point and pass it back to PDL.
  return rewriter.create<SomeOp>(...);
}

Diff Detail

Event Timeline

rriddle created this revision.Mar 19 2022, 3:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 19 2022, 3:09 PM
rriddle requested review of this revision.Mar 19 2022, 3:09 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 19 2022, 3:09 PM
rriddle updated this revision to Diff 416743.Mar 19 2022, 3:19 PM
nicolasvasilache accepted this revision.Mar 21 2022, 3:16 AM

Very cool!
The template-fu is strong with this one ..

mlir/include/mlir/IR/PatternMatch.h
390

FMI, why was all this code moved?
Is it just for lexicographic order or something else (e.g. avoid forward declarations) ?

887

nit: grammo

910

Could be useful to list them in the doc specifically: e.g. Attribute, Operation*, Type, Value, TypeRange, ValueRange

931

I am not clear what these types are, could you add 1-2 examples in the doc?

1094

Hmm .. can you comment on this?

This revision is now accepted and ready to land.Mar 21 2022, 3:16 AM
jpienaar accepted this revision.Mar 28 2022, 2:18 PM

Some template magic indeed, but the results are much more user friendly, thanks!

mlir/include/mlir/IR/PatternMatch.h
821

Micro nit: extra space :)

Doesn't some of these also go away with C++17? (I could be confused)

916

Flip order?

if (pdlValue) return success();
return ... ?

981

Is this only a preference or a requirement given what is stored?

1132

I don't quite follow the 0's here (I may just not be following the logic at all, could you add short explanation?)

rriddle updated this revision to Diff 421053.Apr 6 2022, 5:38 PM
rriddle marked 9 inline comments as done.
rriddle added inline comments.Apr 6 2022, 5:39 PM
mlir/include/mlir/IR/PatternMatch.h
390

It allows for the templates to invoke some methods on the rewriter for convenience.

821

Not that I know of, we always need the false to be dependent on the template type, otherwise it asserts in contexts we don't want it to.

981

It's not an inherent "hard" requirement, but it would involve copying the string (which is a waste). Reworded the message a bit.

1094

It's essentially just #ifndef NDEBUG

1132

It's a fold expression. In C++14 you can only do it in certain situations, e.g. initializer_lists. The zeros are just to satisfy having an int expression for the initializer_list.

This revision was landed with ongoing or failed builds.Apr 6 2022, 5:42 PM
This revision was automatically updated to reflect the committed changes.