Page MenuHomePhabricator

[mlir][PDL] Add a PDL Interpreter Dialect
ClosedPublic

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

Details

Summary

The PDL Interpreter dialect provides a lower level abstraction compared to the PDL dialect, and is targeted towards low level optimization and interpreter code generation. The dialect operations encapsulates low-level pattern match and rewrite "primitives", such as navigating the IR (Operation::getOperand), creating new operations (OpBuilder::create), etc. Many of the operations within this dialect also fuse branching control flow with some form of a predicate comparison operation. This type of fusion reduces the amount of work that an interpreter must do when executing.

An example of this representation is shown below:

mlir
// The following high level PDL pattern:
pdl.pattern : benefit(1) {
  %resultType = pdl.type
  %inputOperand = pdl.input
  %root, %results = pdl.operation "foo.op"(%inputOperand) -> %resultType
  pdl.rewrite(%root) {
    pdl.replace %root with (%inputOperand)
  }
}

// May be represented in the interpreter dialect as follows:
module {
  func @matcher(%arg0: !pdl.operation) {
    pdl_interp.check_operation_name of %arg0 is "foo.op" -> ^bb2, ^bb1
  ^bb1:
    pdl_interp.return
  ^bb2:
    pdl_interp.check_operand_count of %arg0 is 1 -> ^bb3, ^bb1
  ^bb3:
    pdl_interp.check_result_count of %arg0 is 1 -> ^bb4, ^bb1
  ^bb4:
    %0 = pdl_interp.get_operand 0 of %arg0
    pdl_interp.is_not_null %0 : !pdl.value -> ^bb5, ^bb1
  ^bb5:
    %1 = pdl_interp.get_result 0 of %arg0
    pdl_interp.is_not_null %1 : !pdl.value -> ^bb6, ^bb1
  ^bb6:
    pdl_interp.record_match @rewriters::@rewriter(%0, %arg0 : !pdl.value, !pdl.operation) : benefit(1), loc([%arg0]), root("foo.op") -> ^bb1
  }
  module @rewriters {
    func @rewriter(%arg0: !pdl.value, %arg1: !pdl.operation) {
      pdl_interp.replace %arg1 with(%arg0)
      pdl_interp.return
    }
  }
}

Depends On D84578

Diff Detail

Event Timeline

rriddle created this revision.Jul 24 2020, 9:54 PM
rriddle updated this revision to Diff 281028.Jul 27 2020, 12:56 PM

Remove unnecessary interfaces

stephenneuendorffer added inline comments.
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
25

PDLInterpreter?

mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
16

pdl_interpreter?

This revision is now accepted and ready to land.Aug 7 2020, 10:48 PM
mehdi_amini accepted this revision.Aug 23 2020, 10:26 AM
mehdi_amini added inline comments.
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
90

name in the description does not match the op name?

101

it isn't clear to me how are the constant parsed? What is the type of 42 for example?

Could you give an example of the "myConstraint" signature / impl?

128

It isn't clear from the description what kind of state the rewrite has access to, maybe explain a bit and refer the reader to pdl_interp.record_match ?

143

"equivalence" isn't super clear: is it more that straight equality?

301

Most of the ops above aren't annotated as "NoSideEffect", should they? Aren't all the "check" in scope for "NoSideEffect"?

534

Can you show the result on the sample? (same for the other get_*)

691

replaced->replace

716

Maybe we can do something else than "return"?
"finish"? "done"? "finalize"?

rriddle updated this revision to Diff 287933.Aug 26 2020, 5:08 AM
rriddle marked 10 inline comments as done.

Address comments

rriddle added inline comments.Aug 26 2020, 5:08 AM
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
101

It's an ArrayAttr, 42 is an IntegerAttr which has a default type of i64.

Could you give an example of the "myConstraint" signature / impl?

Waiting for the bytecode before adding signature examples, because they are directly related.

716

Went with finalize.

mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
16

I considered it, but I don't think spelling it out completely improves the readability as much as it hurts it by making everything longer.

This revision was landed with ongoing or failed builds.Aug 26 2020, 5:23 AM
This revision was automatically updated to reflect the committed changes.