[mlir] DialectConversion: support block creation in ConversionPatternRewriter
authorAlex Zinenko <zinenko@google.com>
Fri, 3 Apr 2020 17:53:13 +0000 (19:53 +0200)
committerAlex Zinenko <zinenko@google.com>
Fri, 3 Apr 2020 18:30:03 +0000 (20:30 +0200)
commitf27f1e8c27b1d7cf624877e798999244a72adb41
tree02975c5d48d0d9cc192d15b702481f4c6996d808
parentb60080968856edb7b809c2deca306ee7227d5360
[mlir] DialectConversion: support block creation in ConversionPatternRewriter

PatternRewriter and derived classes provide a set of virtual methods to
manipulate blocks, which ConversionPatternRewriter overrides to keep track of
the manipulations and undo them in case the conversion fails. However, one can
currently create a block only by splitting another block into two. This not
only makes the API inconsistent (`splitBlock` is allowed in conversion
patterns, but `createBlock` is not), but it also make it impossible for one to
create blocks with argument lists different from those of already existing
blocks since in-place block updates are not supported either. Such
functionality precludes dialect conversion infrastructure from being used more
extensively on region-containing ops, for example, for value-returning "if"
operations. At the same time, ConversionPatternRewriter already allows one to
undo block creation as block creation is one of the primitive operations in
already supported region inlining.

Support block creation in conversion patterns by hooking `createBlock` on the
block action undo mechanism. This requires to make `Builder::createBlock`
virtual, similarly to Op insertion. This is a minimal change to the Builder
infrastructure that will later help support additional use cases such as block
signature changes. `createBlock` now additionally takes the types of the block
arguments that are added immediately so as to avoid in-place argument list
manipulation that would be illegal in conversion patterns.
mlir/include/mlir/IR/Builders.h
mlir/include/mlir/Transforms/DialectConversion.h
mlir/lib/IR/Builders.cpp
mlir/lib/Transforms/DialectConversion.cpp
mlir/test/Transforms/test-legalizer.mlir
mlir/test/lib/Dialect/Test/TestPatterns.cpp