2018-10-15 22:59:31 -07:00
|
|
|
//===- PatternMatch.cpp - Base classes for pattern match ------------------===//
|
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-15 22:59:31 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-15 22:59:31 -07:00
|
|
|
|
2018-10-25 16:44:04 -07:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2023-01-08 14:15:07 -08:00
|
|
|
#include "mlir/IR/IRMapping.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-27 13:13:20 -08:00
|
|
|
|
2018-10-15 22:59:31 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
2020-10-26 17:23:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternBenefit
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-15 22:59:31 -07: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-18 13:58:17 -07:00
|
|
|
assert(!isImpossibleToMatch() && "Pattern doesn't match");
|
2018-10-15 22:59:31 -07:00
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:03:29 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-26 17:23:41 -07:00
|
|
|
// Pattern
|
2018-10-21 19:03:29 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-03-23 13:44:14 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OperationName Root Constructors
|
|
|
|
|
2018-11-11 15:56:49 -08:00
|
|
|
Pattern::Pattern(StringRef rootName, PatternBenefit benefit,
|
2021-03-23 13:44:14 -07: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-11 15:56:49 -08:00
|
|
|
MLIRContext *context)
|
2021-03-23 13:44:14 -07:00
|
|
|
: rootValue(rootValue), rootKind(rootKind), benefit(benefit),
|
|
|
|
contextAndHasBoundedRecursion(context, false) {
|
|
|
|
if (generatedNames.empty())
|
|
|
|
return;
|
2020-06-18 13:58:25 -07:00
|
|
|
generatedOps.reserve(generatedNames.size());
|
|
|
|
std::transform(generatedNames.begin(), generatedNames.end(),
|
|
|
|
std::back_inserter(generatedOps), [context](StringRef name) {
|
|
|
|
return OperationName(name, context);
|
|
|
|
});
|
|
|
|
}
|
2019-05-24 19:35:23 -07:00
|
|
|
|
2020-10-26 17:23:41 -07: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-01 14: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-16 13:12:01 -07: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-01 14: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-16 13:12:01 -07: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-01 14:30:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 18:08:34 +05:30
|
|
|
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-01 14: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-10 19:36:07 +00:00
|
|
|
|
2022-09-08 16:59:39 -07:00
|
|
|
// Steal the functions and config of the other module.
|
2021-12-10 19:36:07 +00:00
|
|
|
for (auto &it : other.constraintFunctions)
|
|
|
|
registerConstraintFunction(it.first(), std::move(it.second));
|
|
|
|
for (auto &it : other.rewriteFunctions)
|
|
|
|
registerRewriteFunction(it.first(), std::move(it.second));
|
2022-09-08 16:59:39 -07:00
|
|
|
for (auto &it : other.configs)
|
|
|
|
configs.emplace_back(std::move(it));
|
|
|
|
for (auto &it : other.configMap)
|
|
|
|
configMap.insert(it);
|
2021-12-10 19:36:07 +00: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-01 14: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());
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:59:39 -07:00
|
|
|
void PDLPatternModule::attachConfigToPatterns(ModuleOp module,
|
|
|
|
PDLPatternConfigSet &configSet) {
|
|
|
|
// Attach the configuration to the symbols within the module. We only add
|
|
|
|
// to symbols to avoid hardcoding any specific operation names here (given
|
|
|
|
// that we don't depend on any PDL dialect). We can't use
|
|
|
|
// cast<SymbolOpInterface> here because patterns may be optional symbols.
|
|
|
|
module->walk([&](Operation *op) {
|
|
|
|
if (op->hasTrait<SymbolOpInterface::Trait>())
|
|
|
|
configMap[op] = &configSet;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
[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-01 14:30:18 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Registry
|
|
|
|
|
|
|
|
void PDLPatternModule::registerConstraintFunction(
|
|
|
|
StringRef name, PDLConstraintFunction constraintFn) {
|
2021-12-10 19:36:07 +00: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-01 14:30:18 -08:00
|
|
|
}
|
2021-03-16 13:11:22 -07: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-01 14:30:18 -08:00
|
|
|
void PDLPatternModule::registerRewriteFunction(StringRef name,
|
|
|
|
PDLRewriteFunction rewriteFn) {
|
2021-12-10 19:36:07 +00: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-01 14:30:18 -08:00
|
|
|
}
|
|
|
|
|
2020-10-26 17:23:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-02-02 11:32:52 -08:00
|
|
|
// RewriterBase
|
2020-10-26 17:23:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2023-02-22 09:12:24 +01:00
|
|
|
bool RewriterBase::Listener::classof(const OpBuilder::Listener *base) {
|
|
|
|
return base->getKind() == OpBuilder::ListenerBase::Kind::RewriterBaseListener;
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:32:52 -08:00
|
|
|
RewriterBase::~RewriterBase() {
|
2018-10-21 19:03:29 -07:00
|
|
|
// Out of line to provide a vtable anchor for the class.
|
|
|
|
}
|
|
|
|
|
2021-01-14 11: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-02 11:32:52 -08:00
|
|
|
void RewriterBase::replaceOpWithIf(
|
2021-01-14 11: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");
|
|
|
|
|
2023-03-06 09:24:39 +01:00
|
|
|
// Notify the listener that we're about to replace this op.
|
2023-02-22 09:12:24 +01:00
|
|
|
if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
|
|
|
|
rewriteListener->notifyOperationReplaced(op, newValues);
|
2021-01-14 11:57:17 -08:00
|
|
|
|
|
|
|
// Replace each use of the results when the functor is true.
|
|
|
|
bool replacedAllUses = true;
|
|
|
|
for (auto it : llvm::zip(op->getResults(), newValues)) {
|
2023-03-06 09:24:39 +01:00
|
|
|
replaceUsesWithIf(std::get<0>(it), std::get<1>(it), functor);
|
2021-01-14 11:57:17 -08:00
|
|
|
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-02 11:32:52 -08:00
|
|
|
void RewriterBase::replaceOpWithinBlock(Operation *op, ValueRange newValues,
|
|
|
|
Block *block, bool *allUsesReplaced) {
|
2021-01-14 11:57:17 -08:00
|
|
|
replaceOpWithIf(op, newValues, allUsesReplaced, [block](OpOperand &use) {
|
|
|
|
return block->getParentOp()->isProperAncestor(use.getOwner());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-02 11: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
|
2023-06-14 08:41:19 +02:00
|
|
|
/// the operation. The replaced op is erased.
|
2021-02-02 11:32:52 -08:00
|
|
|
void RewriterBase::replaceOp(Operation *op, ValueRange newValues) {
|
2023-03-06 09:24:39 +01:00
|
|
|
assert(op->getNumResults() == newValues.size() &&
|
|
|
|
"incorrect # of replacement values");
|
|
|
|
|
2023-06-14 08:41:19 +02:00
|
|
|
// Notify the listener that we're about to replace this op.
|
2023-02-22 09:12:24 +01:00
|
|
|
if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
|
|
|
|
rewriteListener->notifyOperationReplaced(op, newValues);
|
2018-11-23 01:10:26 -08:00
|
|
|
|
2023-03-06 09:24:39 +01:00
|
|
|
// Replace results one-by-one. Also notifies the listener of modifications.
|
|
|
|
for (auto it : llvm::zip(op->getResults(), newValues))
|
|
|
|
replaceAllUsesWith(std::get<0>(it), std::get<1>(it));
|
2018-11-23 01:10:26 -08:00
|
|
|
|
2023-06-14 08:41:19 +02:00
|
|
|
// Erase the op.
|
|
|
|
eraseOp(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method replaces the results of the operation with the specified new op
|
|
|
|
/// (replacement). The number of results of the two operations must match. The
|
|
|
|
/// replaced op is erased.
|
|
|
|
void RewriterBase::replaceOp(Operation *op, Operation *newOp) {
|
|
|
|
assert(op && newOp && "expected non-null op");
|
|
|
|
assert(op->getNumResults() == newOp->getNumResults() &&
|
|
|
|
"ops have different number of results");
|
|
|
|
|
|
|
|
// Notify the listener that we're about to replace this op.
|
2023-02-22 09:12:24 +01:00
|
|
|
if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
|
2023-06-14 08:41:19 +02:00
|
|
|
rewriteListener->notifyOperationReplaced(op, newOp);
|
|
|
|
|
|
|
|
// Replace results one-by-one. Also notifies the listener of modifications.
|
|
|
|
for (auto it : llvm::zip(op->getResults(), newOp->getResults()))
|
|
|
|
replaceAllUsesWith(std::get<0>(it), std::get<1>(it));
|
|
|
|
|
|
|
|
// Erase the old op.
|
|
|
|
eraseOp(op);
|
2018-11-23 01:10:26 -08:00
|
|
|
}
|
|
|
|
|
2019-10-16 09:50:28 -07: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-02 11:32:52 -08:00
|
|
|
void RewriterBase::eraseOp(Operation *op) {
|
2019-10-16 09:50:28 -07:00
|
|
|
assert(op->use_empty() && "expected 'op' to have no uses");
|
2023-02-22 09:12:24 +01:00
|
|
|
if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
|
|
|
|
rewriteListener->notifyOperationRemoved(op);
|
2019-10-16 09:50:28 -07:00
|
|
|
op->erase();
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:32:52 -08:00
|
|
|
void RewriterBase::eraseBlock(Block *block) {
|
2020-03-31 01:15:40 +05:30
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-02-22 10:41:22 +01:00
|
|
|
void RewriterBase::finalizeRootUpdate(Operation *op) {
|
|
|
|
// Notify the listener that the operation was modified.
|
|
|
|
if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
|
|
|
|
rewriteListener->notifyOperationModified(op);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:32:18 +00:00
|
|
|
/// Find uses of `from` and replace them with `to` if the `functor` returns
|
|
|
|
/// true. It also marks every modified uses and notifies the rewriter that an
|
|
|
|
/// in-place operation modification is about to happen.
|
2023-03-06 09:24:39 +01:00
|
|
|
void RewriterBase::replaceUsesWithIf(Value from, Value to,
|
|
|
|
function_ref<bool(OpOperand &)> functor) {
|
2022-12-06 07:30:28 +00:00
|
|
|
for (OpOperand &operand : llvm::make_early_inc_range(from.getUses())) {
|
2023-01-04 20:32:18 +00:00
|
|
|
if (functor(operand))
|
|
|
|
updateRootInPlace(operand.getOwner(), [&]() { operand.set(to); });
|
2022-12-06 07:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 12:38:18 +01:00
|
|
|
void RewriterBase::inlineBlockBefore(Block *source, Block *dest,
|
|
|
|
Block::iterator before,
|
|
|
|
ValueRange argValues) {
|
|
|
|
assert(argValues.size() == source->getNumArguments() &&
|
|
|
|
"incorrect # of argument replacement values");
|
|
|
|
|
|
|
|
// The source block will be deleted, so it should not have any users (i.e.,
|
|
|
|
// there should be no predecessors).
|
2020-08-19 16:07:42 -07:00
|
|
|
assert(source->hasNoPredecessors() &&
|
|
|
|
"expected 'source' to have no predecessors");
|
|
|
|
|
2023-03-06 12:38:18 +01:00
|
|
|
if (dest->end() != before) {
|
|
|
|
// The source block will be inserted in the middle of the dest block, so
|
|
|
|
// the source block should have no successors. Otherwise, the remainder of
|
|
|
|
// the dest block would be unreachable.
|
|
|
|
assert(source->hasNoSuccessors() &&
|
|
|
|
"expected 'source' to have no successors");
|
|
|
|
} else {
|
|
|
|
// The source block will be inserted at the end of the dest block, so the
|
|
|
|
// dest block should have no successors. Otherwise, the inserted operations
|
|
|
|
// will be unreachable.
|
|
|
|
assert(dest->hasNoSuccessors() && "expected 'dest' to have no successors");
|
|
|
|
}
|
2020-08-19 16:07:42 -07:00
|
|
|
|
2023-03-06 12:38:18 +01:00
|
|
|
// Replace all of the successor arguments with the provided values.
|
|
|
|
for (auto it : llvm::zip(source->getArguments(), argValues))
|
|
|
|
replaceAllUsesWith(std::get<0>(it), std::get<1>(it));
|
2020-08-19 16:07:42 -07:00
|
|
|
|
2023-03-06 12:38:18 +01:00
|
|
|
// Move operations from the source block to the dest block and erase the
|
|
|
|
// source block.
|
|
|
|
dest->getOperations().splice(before, source->getOperations());
|
|
|
|
source->erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriterBase::inlineBlockBefore(Block *source, Operation *op,
|
|
|
|
ValueRange argValues) {
|
|
|
|
inlineBlockBefore(source, op->getBlock(), op->getIterator(), argValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriterBase::mergeBlocks(Block *source, Block *dest,
|
|
|
|
ValueRange argValues) {
|
|
|
|
inlineBlockBefore(source, dest, dest->end(), argValues);
|
2020-08-19 16:07:42 -07:00
|
|
|
}
|
|
|
|
|
2019-11-05 11: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-02 11:32:52 -08:00
|
|
|
Block *RewriterBase::splitBlock(Block *block, Block::iterator before) {
|
2019-11-05 11:57:03 -08:00
|
|
|
return block->splitBlock(before);
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:33:18 -07: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-02 11:32:52 -08:00
|
|
|
void RewriterBase::inlineRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before) {
|
2019-06-21 03:26:39 -07:00
|
|
|
parent.getBlocks().splice(before, region.getBlocks());
|
|
|
|
}
|
2021-02-02 11:32:52 -08:00
|
|
|
void RewriterBase::inlineRegionBefore(Region ®ion, Block *before) {
|
2019-06-21 03:26:39 -07:00
|
|
|
inlineRegionBefore(region, *before->getParent(), before->getIterator());
|
2019-06-11 08:33:18 -07:00
|
|
|
}
|
|
|
|
|
2019-10-08 15:44:34 -07: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-02 11:32:52 -08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before,
|
2023-01-08 14:15:07 -08:00
|
|
|
IRMapping &mapping) {
|
2019-10-08 15:44:34 -07:00
|
|
|
region.cloneInto(&parent, before, mapping);
|
|
|
|
}
|
2021-02-02 11:32:52 -08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before) {
|
2023-01-08 14:15:07 -08:00
|
|
|
IRMapping mapping;
|
2019-10-08 15:44:34 -07:00
|
|
|
cloneRegionBefore(region, parent, before, mapping);
|
|
|
|
}
|
2021-02-02 11:32:52 -08:00
|
|
|
void RewriterBase::cloneRegionBefore(Region ®ion, Block *before) {
|
2019-10-08 15:44:34 -07:00
|
|
|
cloneRegionBefore(region, *before->getParent(), before->getIterator());
|
|
|
|
}
|