[mlir][PDL] Add a PDL Interpreter Dialect
authorRiver Riddle <riddleriver@gmail.com>
Wed, 26 Aug 2020 12:12:07 +0000 (05:12 -0700)
committerRiver Riddle <riddleriver@gmail.com>
Wed, 26 Aug 2020 12:22:27 +0000 (05:22 -0700)
commitd289a97f91443177b605926668512479c2cee37b
tree23ed110dcadc839d3a61fb093074dd4d79eac9de
parent92c527e5a2b49fb1213ceda97738d4caf414666a
[mlir][PDL] Add a PDL Interpreter Dialect

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
    }
  }
}
```

Differential Revision: https://reviews.llvm.org/D84579
25 files changed:
mlir/include/mlir/Dialect/CMakeLists.txt
mlir/include/mlir/Dialect/PDL/IR/PDLBase.td
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
mlir/include/mlir/Dialect/PDLInterp/CMakeLists.txt [new file with mode: 0644]
mlir/include/mlir/Dialect/PDLInterp/IR/CMakeLists.txt [new file with mode: 0644]
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterp.h [new file with mode: 0644]
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td [new file with mode: 0644]
mlir/include/mlir/IR/Attributes.h
mlir/include/mlir/IR/Builders.h
mlir/include/mlir/IR/OpImplementation.h
mlir/include/mlir/InitAllDialects.h
mlir/lib/Dialect/CMakeLists.txt
mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
mlir/lib/Dialect/PDL/IR/PDL.cpp
mlir/lib/Dialect/PDLInterp/CMakeLists.txt [new file with mode: 0644]
mlir/lib/Dialect/PDLInterp/IR/CMakeLists.txt [new file with mode: 0644]
mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp [new file with mode: 0644]
mlir/lib/IR/Builders.cpp
mlir/lib/Parser/AttributeParser.cpp
mlir/lib/Parser/Parser.cpp
mlir/lib/Parser/Parser.h
mlir/test/Dialect/PDL/invalid.mlir
mlir/test/Dialect/PDL/ops.mlir
mlir/test/Dialect/PDLInterp/ops.mlir [new file with mode: 0644]
mlir/tools/mlir-tblgen/OpFormatGen.cpp