[mlir][PDL] Add support for PDL bytecode and expose PDL support to OwningRewritePatte...
authorRiver Riddle <riddleriver@gmail.com>
Tue, 1 Dec 2020 22:30:18 +0000 (14:30 -0800)
committerRiver Riddle <riddleriver@gmail.com>
Tue, 1 Dec 2020 23:05:50 +0000 (15:05 -0800)
commitabfd1a8b3bc5ad8516a83c3ae7ba9f16032525ad
treebf3a05f6f0fc393c25f486729efaf612cf8c9ade
parentc7dbaec396ef98b8bc6acb7631d2919449986add
[mlir][PDL] Add support for PDL bytecode and expose PDL support to OwningRewritePatternList

PDL patterns are now supported via a new `PDLPatternModule` class. This class contains a ModuleOp with the pdl::PatternOp operations representing the patterns, as well as a collection of registered C++ functions for native constraints/creations/rewrites/etc. that may be invoked via the pdl patterns. Instances of this class are added to an OwningRewritePatternList in the same fashion as C++ RewritePatterns, i.e. via the `insert` method.

The PDL bytecode is an in-memory representation of the PDL interpreter dialect that can be efficiently interpreted/executed. The representation of the bytecode boils down to a code array(for opcodes/memory locations/etc) and a memory buffer(for storing attributes/operations/values/any other data necessary). The bytecode operations are effectively a 1-1 mapping to the PDLInterp dialect operations, with a few exceptions in cases where the in-memory representation of the bytecode can be more efficient than the MLIR representation. For example, a generic `AreEqual` bytecode op can be used to represent AreEqualOp, CheckAttributeOp, and CheckTypeOp.

The execution of the bytecode is split into two phases: matching and rewriting. When matching, all of the matched patterns are collected to avoid the overhead of re-running parts of the matcher. These matched patterns are then considered alongside the native C++ patterns, which rewrite immediately in-place via `RewritePattern::matchAndRewrite`,  for the given root operation. When a PDL pattern is matched and has the highest benefit, it is passed back to the bytecode to execute its rewriter.

Differential Revision: https://reviews.llvm.org/D89107
23 files changed:
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
mlir/include/mlir/IR/BlockSupport.h
mlir/include/mlir/IR/Operation.h
mlir/include/mlir/IR/OperationSupport.h
mlir/include/mlir/IR/PatternMatch.h
mlir/include/mlir/IR/StorageUniquerSupport.h
mlir/include/mlir/Rewrite/FrozenRewritePatternList.h
mlir/include/mlir/Rewrite/PatternApplicator.h
mlir/lib/IR/Block.cpp
mlir/lib/IR/Operation.cpp
mlir/lib/IR/PatternMatch.cpp
mlir/lib/Rewrite/ByteCode.cpp [new file with mode: 0644]
mlir/lib/Rewrite/ByteCode.h [new file with mode: 0644]
mlir/lib/Rewrite/CMakeLists.txt
mlir/lib/Rewrite/FrozenRewritePatternList.cpp
mlir/lib/Rewrite/PatternApplicator.cpp
mlir/test/Rewrite/pdl-bytecode.mlir [new file with mode: 0644]
mlir/test/lib/CMakeLists.txt
mlir/test/lib/Rewrite/CMakeLists.txt [new file with mode: 0644]
mlir/test/lib/Rewrite/TestPDLByteCode.cpp [new file with mode: 0644]
mlir/test/lib/Transforms/TestLinalgTransforms.cpp
mlir/tools/mlir-opt/CMakeLists.txt
mlir/tools/mlir-opt/mlir-opt.cpp