2018-10-16 13:59:31 +08:00
|
|
|
//===- PatternMatch.cpp - Base classes for pattern match ------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-16 13:59:31 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-16 13:59:31 +08:00
|
|
|
|
2018-10-26 07:44:04 +08:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-10-09 06:44:34 +08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
|
2018-10-16 13:59:31 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternBenefit
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-16 13:59:31 +08:00
|
|
|
PatternBenefit::PatternBenefit(unsigned benefit) : representation(benefit) {
|
|
|
|
assert(representation == benefit && benefit != ImpossibleToMatchSentinel &&
|
|
|
|
"This pattern match benefit is too large to represent");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short PatternBenefit::getBenefit() const {
|
2020-06-19 04:58:17 +08:00
|
|
|
assert(!isImpossibleToMatch() && "Pattern doesn't match");
|
2018-10-16 13:59:31 +08:00
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-27 08:23:41 +08:00
|
|
|
// Pattern
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-03-24 04:44:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OperationName Root Constructors
|
|
|
|
|
2018-11-12 07:56:49 +08:00
|
|
|
Pattern::Pattern(StringRef rootName, PatternBenefit benefit,
|
2021-03-24 04:44:14 +08:00
|
|
|
MLIRContext *context, ArrayRef<StringRef> generatedNames)
|
|
|
|
: Pattern(OperationName(rootName, context).getAsOpaquePointer(),
|
|
|
|
RootKind::OperationName, generatedNames, benefit, context) {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MatchAnyOpTypeTag Root Constructors
|
|
|
|
|
|
|
|
Pattern::Pattern(MatchAnyOpTypeTag tag, PatternBenefit benefit,
|
|
|
|
MLIRContext *context, ArrayRef<StringRef> generatedNames)
|
|
|
|
: Pattern(nullptr, RootKind::Any, generatedNames, benefit, context) {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MatchInterfaceOpTypeTag Root Constructors
|
|
|
|
|
|
|
|
Pattern::Pattern(MatchInterfaceOpTypeTag tag, TypeID interfaceID,
|
|
|
|
PatternBenefit benefit, MLIRContext *context,
|
|
|
|
ArrayRef<StringRef> generatedNames)
|
|
|
|
: Pattern(interfaceID.getAsOpaquePointer(), RootKind::InterfaceID,
|
|
|
|
generatedNames, benefit, context) {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MatchTraitOpTypeTag Root Constructors
|
|
|
|
|
|
|
|
Pattern::Pattern(MatchTraitOpTypeTag tag, TypeID traitID,
|
|
|
|
PatternBenefit benefit, MLIRContext *context,
|
|
|
|
ArrayRef<StringRef> generatedNames)
|
|
|
|
: Pattern(traitID.getAsOpaquePointer(), RootKind::TraitID, generatedNames,
|
|
|
|
benefit, context) {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// General Constructors
|
|
|
|
|
|
|
|
Pattern::Pattern(const void *rootValue, RootKind rootKind,
|
|
|
|
ArrayRef<StringRef> generatedNames, PatternBenefit benefit,
|
2018-11-12 07:56:49 +08:00
|
|
|
MLIRContext *context)
|
2021-03-24 04:44:14 +08:00
|
|
|
: rootValue(rootValue), rootKind(rootKind), benefit(benefit),
|
|
|
|
contextAndHasBoundedRecursion(context, false) {
|
|
|
|
if (generatedNames.empty())
|
|
|
|
return;
|
2020-06-19 04:58:25 +08:00
|
|
|
generatedOps.reserve(generatedNames.size());
|
|
|
|
std::transform(generatedNames.begin(), generatedNames.end(),
|
|
|
|
std::back_inserter(generatedOps), [context](StringRef name) {
|
|
|
|
return OperationName(name, context);
|
|
|
|
});
|
|
|
|
}
|
2019-05-25 10:35:23 +08:00
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RewritePattern
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void RewritePattern::rewrite(Operation *op, PatternRewriter &rewriter) const {
|
|
|
|
llvm_unreachable("need to implement either matchAndRewrite or one of the "
|
|
|
|
"rewrite functions!");
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult RewritePattern::match(Operation *op) const {
|
|
|
|
llvm_unreachable("need to implement either match or matchAndRewrite!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Out-of-line vtable anchor.
|
|
|
|
void RewritePattern::anchor() {}
|
|
|
|
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PDLValue
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[mlir][PDL] Add support for variadic operands and results in the PDL byte code
Supporting ranges in the byte code requires additional complexity, given that a range can't be easily representable as an opaque void *, as is possible with the existing bytecode value types (Attribute, Type, Value, etc.). To enable representing a range with void *, an auxillary storage is used for the actual range itself, with the pointer being passed around in the normal byte code memory. For type ranges, a TypeRange is stored. For value ranges, a ValueRange is stored. The above problem represents a majority of the complexity involved in this revision, the rest is adapting/adding byte code operations to support the changes made to the PDL interpreter in the parent revision.
After this revision, PDL will have initial end-to-end support for variadic operands/results.
Differential Revision: https://reviews.llvm.org/D95723
2021-03-17 04:12:01 +08:00
|
|
|
void PDLValue::print(raw_ostream &os) const {
|
|
|
|
if (!value) {
|
|
|
|
os << "<NULL-PDLValue>";
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
return;
|
|
|
|
}
|
[mlir][PDL] Add support for variadic operands and results in the PDL byte code
Supporting ranges in the byte code requires additional complexity, given that a range can't be easily representable as an opaque void *, as is possible with the existing bytecode value types (Attribute, Type, Value, etc.). To enable representing a range with void *, an auxillary storage is used for the actual range itself, with the pointer being passed around in the normal byte code memory. For type ranges, a TypeRange is stored. For value ranges, a ValueRange is stored. The above problem represents a majority of the complexity involved in this revision, the rest is adapting/adding byte code operations to support the changes made to the PDL interpreter in the parent revision.
After this revision, PDL will have initial end-to-end support for variadic operands/results.
Differential Revision: https://reviews.llvm.org/D95723
2021-03-17 04:12:01 +08:00
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attribute:
|
|
|
|
os << cast<Attribute>();
|
|
|
|
break;
|
|
|
|
case Kind::Operation:
|
|
|
|
os << *cast<Operation *>();
|
|
|
|
break;
|
|
|
|
case Kind::Type:
|
|
|
|
os << cast<Type>();
|
|
|
|
break;
|
|
|
|
case Kind::TypeRange:
|
|
|
|
llvm::interleaveComma(cast<TypeRange>(), os);
|
|
|
|
break;
|
|
|
|
case Kind::Value:
|
|
|
|
os << cast<Value>();
|
|
|
|
break;
|
|
|
|
case Kind::ValueRange:
|
|
|
|
llvm::interleaveComma(cast<ValueRange>(), os);
|
|
|
|
break;
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 20:38:34 +08:00
|
|
|
void PDLValue::print(raw_ostream &os, Kind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attribute:
|
|
|
|
os << "Attribute";
|
|
|
|
break;
|
|
|
|
case Kind::Operation:
|
|
|
|
os << "Operation";
|
|
|
|
break;
|
|
|
|
case Kind::Type:
|
|
|
|
os << "Type";
|
|
|
|
break;
|
|
|
|
case Kind::TypeRange:
|
|
|
|
os << "TypeRange";
|
|
|
|
break;
|
|
|
|
case Kind::Value:
|
|
|
|
os << "Value";
|
|
|
|
break;
|
|
|
|
case Kind::ValueRange:
|
|
|
|
os << "ValueRange";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PDLPatternModule
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PDLPatternModule::mergeIn(PDLPatternModule &&other) {
|
|
|
|
// Ignore the other module if it has no patterns.
|
|
|
|
if (!other.pdlModule)
|
|
|
|
return;
|
2021-12-11 03:36:07 +08:00
|
|
|
|
|
|
|
// Steal the functions of the other module.
|
|
|
|
for (auto &it : other.constraintFunctions)
|
|
|
|
registerConstraintFunction(it.first(), std::move(it.second));
|
|
|
|
for (auto &it : other.rewriteFunctions)
|
|
|
|
registerRewriteFunction(it.first(), std::move(it.second));
|
|
|
|
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
// Steal the other state if we have no patterns.
|
|
|
|
if (!pdlModule) {
|
|
|
|
pdlModule = std::move(other.pdlModule);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the pattern operations from the other module into this one.
|
|
|
|
Block *block = pdlModule->getBody();
|
|
|
|
block->getOperations().splice(block->end(),
|
|
|
|
other.pdlModule->getBody()->getOperations());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Registry
|
|
|
|
|
|
|
|
void PDLPatternModule::registerConstraintFunction(
|
|
|
|
StringRef name, PDLConstraintFunction constraintFn) {
|
2021-12-11 03:36:07 +08:00
|
|
|
// TODO: Is it possible to diagnose when `name` is already registered to
|
|
|
|
// a function that is not equivalent to `constraintFn`?
|
|
|
|
// Allow existing mappings in the case multiple patterns depend on the same
|
|
|
|
// constraint.
|
|
|
|
constraintFunctions.try_emplace(name, std::move(constraintFn));
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
}
|
2021-03-17 04:11:22 +08:00
|
|
|
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
void PDLPatternModule::registerRewriteFunction(StringRef name,
|
|
|
|
PDLRewriteFunction rewriteFn) {
|
2021-12-11 03:36:07 +08:00
|
|
|
// TODO: Is it possible to diagnose when `name` is already registered to
|
|
|
|
// a function that is not equivalent to `rewriteFn`?
|
|
|
|
// Allow existing mappings in the case multiple patterns depend on the same
|
|
|
|
// rewrite.
|
|
|
|
rewriteFunctions.try_emplace(name, std::move(rewriteFn));
|
[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
2020-12-02 06:30:18 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-02-03 03:32:52 +08:00
|
|
|
// RewriterBase
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-02-03 03:32:52 +08:00
|
|
|
RewriterBase::~RewriterBase() {
|
2018-10-22 10:03:29 +08:00
|
|
|
// Out of line to provide a vtable anchor for the class.
|
|
|
|
}
|
|
|
|
|
2021-01-15 03:57:17 +08:00
|
|
|
/// This method replaces the uses of the results of `op` with the values in
|
|
|
|
/// `newValues` when the provided `functor` returns true for a specific use.
|
|
|
|
/// The number of values in `newValues` is required to match the number of
|
|
|
|
/// results of `op`.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::replaceOpWithIf(
|
2021-01-15 03:57:17 +08:00
|
|
|
Operation *op, ValueRange newValues, bool *allUsesReplaced,
|
|
|
|
llvm::unique_function<bool(OpOperand &) const> functor) {
|
|
|
|
assert(op->getNumResults() == newValues.size() &&
|
|
|
|
"incorrect number of values to replace operation");
|
|
|
|
|
|
|
|
// Notify the rewriter subclass that we're about to replace this root.
|
|
|
|
notifyRootReplaced(op);
|
|
|
|
|
|
|
|
// Replace each use of the results when the functor is true.
|
|
|
|
bool replacedAllUses = true;
|
|
|
|
for (auto it : llvm::zip(op->getResults(), newValues)) {
|
|
|
|
std::get<0>(it).replaceUsesWithIf(std::get<1>(it), functor);
|
|
|
|
replacedAllUses &= std::get<0>(it).use_empty();
|
|
|
|
}
|
|
|
|
if (allUsesReplaced)
|
|
|
|
*allUsesReplaced = replacedAllUses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method replaces the uses of the results of `op` with the values in
|
|
|
|
/// `newValues` when a use is nested within the given `block`. The number of
|
|
|
|
/// values in `newValues` is required to match the number of results of `op`.
|
|
|
|
/// If all uses of this operation are replaced, the operation is erased.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::replaceOpWithinBlock(Operation *op, ValueRange newValues,
|
|
|
|
Block *block, bool *allUsesReplaced) {
|
2021-01-15 03:57:17 +08:00
|
|
|
replaceOpWithIf(op, newValues, allUsesReplaced, [block](OpOperand &use) {
|
|
|
|
return block->getParentOp()->isProperAncestor(use.getOwner());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-03 03:32:52 +08:00
|
|
|
/// This method replaces the results of the operation with the specified list of
|
|
|
|
/// values. The number of provided values must match the number of results of
|
|
|
|
/// the operation.
|
|
|
|
void RewriterBase::replaceOp(Operation *op, ValueRange newValues) {
|
2018-11-23 17:10:26 +08:00
|
|
|
// Notify the rewriter subclass that we're about to replace this root.
|
|
|
|
notifyRootReplaced(op);
|
|
|
|
|
|
|
|
assert(op->getNumResults() == newValues.size() &&
|
|
|
|
"incorrect # of replacement values");
|
2019-08-08 04:48:19 +08:00
|
|
|
op->replaceAllUsesWith(newValues);
|
2018-11-23 17:10:26 +08:00
|
|
|
|
|
|
|
notifyOperationRemoved(op);
|
|
|
|
op->erase();
|
|
|
|
}
|
|
|
|
|
2019-10-17 00:50:28 +08:00
|
|
|
/// This method erases an operation that is known to have no uses. The uses of
|
|
|
|
/// the given operation *must* be known to be dead.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::eraseOp(Operation *op) {
|
2019-10-17 00:50:28 +08:00
|
|
|
assert(op->use_empty() && "expected 'op' to have no uses");
|
|
|
|
notifyOperationRemoved(op);
|
|
|
|
op->erase();
|
|
|
|
}
|
|
|
|
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::eraseBlock(Block *block) {
|
2020-03-31 03:45:40 +08:00
|
|
|
for (auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) {
|
|
|
|
assert(op.use_empty() && "expected 'op' to have no uses");
|
|
|
|
eraseOp(&op);
|
|
|
|
}
|
|
|
|
block->erase();
|
|
|
|
}
|
|
|
|
|
2019-11-06 03:57:03 +08:00
|
|
|
/// Merge the operations of block 'source' into the end of block 'dest'.
|
|
|
|
/// 'source's predecessors must be empty or only contain 'dest`.
|
|
|
|
/// 'argValues' is used to replace the block arguments of 'source' after
|
|
|
|
/// merging.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::mergeBlocks(Block *source, Block *dest,
|
|
|
|
ValueRange argValues) {
|
2019-11-06 03:57:03 +08:00
|
|
|
assert(llvm::all_of(source->getPredecessors(),
|
|
|
|
[dest](Block *succ) { return succ == dest; }) &&
|
|
|
|
"expected 'source' to have no predecessors or only 'dest'");
|
|
|
|
assert(argValues.size() == source->getNumArguments() &&
|
|
|
|
"incorrect # of argument replacement values");
|
|
|
|
|
|
|
|
// Replace all of the successor arguments with the provided values.
|
|
|
|
for (auto it : llvm::zip(source->getArguments(), argValues))
|
2020-01-12 00:54:04 +08:00
|
|
|
std::get<0>(it).replaceAllUsesWith(std::get<1>(it));
|
2019-11-06 03:57:03 +08:00
|
|
|
|
|
|
|
// Splice the operations of the 'source' block into the 'dest' block and erase
|
|
|
|
// it.
|
|
|
|
dest->getOperations().splice(dest->end(), source->getOperations());
|
|
|
|
source->dropAllUses();
|
|
|
|
source->erase();
|
|
|
|
}
|
|
|
|
|
2020-08-20 07:07:42 +08:00
|
|
|
// Merge the operations of block 'source' before the operation 'op'. Source
|
|
|
|
// block should not have existing predecessors or successors.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::mergeBlockBefore(Block *source, Operation *op,
|
|
|
|
ValueRange argValues) {
|
2020-08-20 07:07:42 +08:00
|
|
|
assert(source->hasNoPredecessors() &&
|
|
|
|
"expected 'source' to have no predecessors");
|
|
|
|
assert(source->hasNoSuccessors() &&
|
|
|
|
"expected 'source' to have no successors");
|
|
|
|
|
2020-10-29 03:03:15 +08:00
|
|
|
// Split the block containing 'op' into two, one containing all operations
|
2020-08-20 07:07:42 +08:00
|
|
|
// before 'op' (prologue) and another (epilogue) containing 'op' and all
|
|
|
|
// operations after it.
|
|
|
|
Block *prologue = op->getBlock();
|
|
|
|
Block *epilogue = splitBlock(prologue, op->getIterator());
|
|
|
|
|
|
|
|
// Merge the source block at the end of the prologue.
|
|
|
|
mergeBlocks(source, prologue, argValues);
|
|
|
|
|
|
|
|
// Merge the epilogue at the end the prologue.
|
|
|
|
mergeBlocks(epilogue, prologue);
|
|
|
|
}
|
|
|
|
|
2019-11-06 03:57:03 +08:00
|
|
|
/// Split the operations starting at "before" (inclusive) out of the given
|
|
|
|
/// block into a new block, and return it.
|
2021-02-03 03:32:52 +08:00
|
|
|
Block *RewriterBase::splitBlock(Block *block, Block::iterator before) {
|
2019-11-06 03:57:03 +08:00
|
|
|
return block->splitBlock(before);
|
|
|
|
}
|
|
|
|
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
/// 'op' and 'newOp' are known to have the same number of results, replace the
|
2018-11-24 23:40:55 +08:00
|
|
|
/// uses of op with uses of newOp
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::replaceOpWithResultsOfAnotherOp(Operation *op,
|
|
|
|
Operation *newOp) {
|
2018-11-24 23:40:55 +08:00
|
|
|
assert(op->getNumResults() == newOp->getNumResults() &&
|
|
|
|
"replacement op doesn't match results of original op");
|
|
|
|
if (op->getNumResults() == 1)
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
return replaceOp(op, newOp->getResult(0));
|
|
|
|
return replaceOp(op, newOp->getResults());
|
2018-10-24 01:12:00 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:33:18 +08:00
|
|
|
/// Move the blocks that belong to "region" before the given position in
|
|
|
|
/// another region. The two regions must be different. The caller is in
|
|
|
|
/// charge to update create the operation transferring the control flow to the
|
|
|
|
/// region and pass it the correct block arguments.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::inlineRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before) {
|
2019-06-21 18:26:39 +08:00
|
|
|
parent.getBlocks().splice(before, region.getBlocks());
|
|
|
|
}
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::inlineRegionBefore(Region ®ion, Block *before) {
|
2019-06-21 18:26:39 +08:00
|
|
|
inlineRegionBefore(region, *before->getParent(), before->getIterator());
|
2019-06-11 23:33:18 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 06:44:34 +08:00
|
|
|
/// Clone the blocks that belong to "region" before the given position in
|
|
|
|
/// another region "parent". The two regions must be different. The caller is
|
|
|
|
/// responsible for creating or updating the operation transferring flow of
|
|
|
|
/// control to the region and passing it the correct block arguments.
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before,
|
|
|
|
BlockAndValueMapping &mapping) {
|
2019-10-09 06:44:34 +08:00
|
|
|
region.cloneInto(&parent, before, mapping);
|
|
|
|
}
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before) {
|
2019-10-09 06:44:34 +08:00
|
|
|
BlockAndValueMapping mapping;
|
|
|
|
cloneRegionBefore(region, parent, before, mapping);
|
|
|
|
}
|
2021-02-03 03:32:52 +08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Block *before) {
|
2019-10-09 06:44:34 +08:00
|
|
|
cloneRegionBefore(region, *before->getParent(), before->getIterator());
|
|
|
|
}
|