This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add a new "Pattern Descriptor Language" (PDL) dialect.
ClosedPublic

Authored by rriddle on Jul 24 2020, 9:53 PM.

Details

Summary

PDL presents a high level abstraction for the rewrite pattern infrastructure available in MLIR. This abstraction allows for representing patterns transforming MLIR, as MLIR. This allows for applying all of the benefits that the general MLIR infrastructure provides, to the infrastructure itself. This means that pattern matching can be more easily verified for correctness, targeted by frontends, and optimized.

PDL abstracts over various different aspects of patterns and core MLIR data structures. Patterns are specified via a pdl.pattern operation. These operations contain a region body for the "matcher" code, and terminate with a pdl.rewrite that either dispatches to an external rewriter or contains a region for the rewrite specified via pdl. The types of values in pdl are handle types to MLIR C++ types, with !pdl.attribute, !pdl.operation, and !pdl.type directly mapping to mlir::Attribute, mlir::Operation*, and mlir::Value respectively.

An example pattern is shown below:

mlir
// pdl.pattern contains metadata similarly to a `RewritePattern`.
pdl.pattern : benefit(1) {
  // External input operand values are specified via `pdl.input` operations.
  // Result types are constrainted via `pdl.type` operations.

  %resultType = pdl.type
  %inputOperand = pdl.input
  %root, %results = pdl.operation "foo.op"(%inputOperand) -> %resultType
  pdl.rewrite(%root) {
    pdl.replace %root with (%inputOperand)
  }
}

This is a culmination of the work originally discussed here: https://groups.google.com/a/tensorflow.org/g/mlir/c/j_bn74ByxlQ

Depends On D84577

Diff Detail

Event Timeline

rriddle created this revision.Jul 24 2020, 9:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2020, 9:53 PM
stephenneuendorffer added inline comments.
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
342

pdl.replaced -> pdl.replaced

mlir/include/mlir/Dialect/PDL/IR/PDLTypes.h
25–31

These kinds are going away, but are still needed at the moment, right?

This revision is now accepted and ready to land.Aug 7 2020, 10:40 PM
Mogball added a subscriber: Mogball.Aug 9 2020, 2:16 PM
Mogball added inline comments.Aug 9 2020, 2:28 PM
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
402

Should the region be optional?

mlir/lib/Dialect/PDL/IR/PDL.cpp
242

each of the types...?

Nice, thanks

mlir/include/mlir/Dialect/PDL/IR/PDLBase.td
61

PDL ? (couple of other places, it would seem that the dialect's namespace is pdl but it is PDL dialect, so in comments/messages I'd expect PDL)

mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
41

PDL

48

Could you expand this to explain where 42 etc come into play

202

You might want to define this up above in the dialect description

309

Render is not a very intuitive word here for me ... This appears to be an arbitrary C++ method, e.g., this is the NativeCall equivalent.

377

s/rewriter/rewrite/ ? (or replacement)

380

rewriter makes me think of the pattern rewriter driver, I know you are using it here as in caller vs callee.

408

end of ... region ?

[aren't all these pseudo ops? :) How about just "Implicit terminator of pdl.rewrite region" ?

423

OOC: this captures equality, how would you go about capturing compatibility ? E.g., operation produces a return type that is compatible with its first operand?

mlir/lib/Dialect/PDL/IR/PDL.cpp
187

We need to hoist this one out at some point ... (couldn't quite figure out a name for it, VariadicOperandsOpInterface ...)

226

Is main == root?

229

I don't follow this check

mehdi_amini accepted this revision.Aug 10 2020, 10:21 PM

Nice!

mlir/include/mlir/Dialect/PDL/IR/PDL.h
30

The lint check is also the source of the build failure

mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
52

It'd be useful to show how this example apply_constraint will invoke myConstraint, maybe provide the declaration of myConstraint and the call that will be generated?

320

Same as before for the example

423

Using an apply_constraint ?

rriddle updated this revision to Diff 285521.Aug 13 2020, 4:30 PM
rriddle marked 16 inline comments as done.

Resolve comments

rriddle added inline comments.Aug 13 2020, 4:31 PM
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
52

This is kind of a TODO for when the bytecode comes into play, as right now I don't have a complete idea of what the user facing call will look like.

402

AnyRegion is pretty much the optional equivalent for region when the region would otherwise have a specific size. We tend to just use "empty" as the indicator for whether the region is present or not.

423

Yeah, apply_constraint is generally what will be used. Equality is a simple, and overwhelming common, enough property to model as builtin.

mlir/include/mlir/Dialect/PDL/IR/PDLTypes.h
25–31

Yeah, these will be going away when the other refactoring lands.

mlir/lib/Dialect/PDL/IR/PDL.cpp
226

The main operation here is the operation being replaced.

229

Operand 0 of ReplaceOp is the operation being replaced. We can't infer the type if we are the operation being replace. We can only infer if we are replacing something else.