Move the ConversionListBuilder utility to PatternMatch.h and rename it to RewriteListBuilder. It has no specific functionality for DialectOpConversion patterns and can be useful for RewritePatterns in general.

--

PiperOrigin-RevId: 248884466
This commit is contained in:
River Riddle 2019-05-18 13:23:38 -07:00 committed by Mehdi Amini
parent 8780d8d8eb
commit 1966d34da4
13 changed files with 49 additions and 51 deletions

View File

@ -397,8 +397,8 @@ public:
void linalg::getDescriptorConverters(mlir::OwningRewritePatternList &patterns,
mlir::MLIRContext *context) {
ConversionListBuilder<DropConsumer, RangeOpConversion, SliceOpConversion,
ViewOpConversion>::build(patterns, context);
RewriteListBuilder<DropConsumer, RangeOpConversion, SliceOpConversion,
ViewOpConversion>::build(patterns, context);
}
namespace {

View File

@ -138,8 +138,8 @@ class StoreOpConversion : public LoadStoreOpConversion<linalg::StoreOp> {
static void getConversions(mlir::OwningRewritePatternList &patterns,
mlir::MLIRContext *context) {
linalg::getDescriptorConverters(patterns, context);
ConversionListBuilder<LoadOpConversion, StoreOpConversion>::build(patterns,
context);
RewriteListBuilder<LoadOpConversion, StoreOpConversion>::build(patterns,
context);
}
void linalg::convertLinalg3ToLLVM(Module &module) {

View File

@ -161,9 +161,8 @@ void TransposeOp::getCanonicalizationPatterns(
// Register our patterns for rewrite by the Canonicalization framework.
void ReshapeOp::getCanonicalizationPatterns(
mlir::OwningRewritePatternList &results, mlir::MLIRContext *context) {
results.push_back(llvm::make_unique<SimplifyReshapeConstant>(context));
results.push_back(llvm::make_unique<SimplifyReshapeReshape>(context));
results.push_back(llvm::make_unique<SimplifyNullReshape>(context));
mlir::RewriteListBuilder<SimplifyReshapeConstant, SimplifyReshapeReshape,
SimplifyNullReshape>::build(results, context);
}
} // namespace toy

View File

@ -125,7 +125,7 @@ protected:
// Initialize the list of converters.
void initConverters(OwningRewritePatternList &patterns,
MLIRContext *context) override {
ConversionListBuilder<MulOpConversion>::build(patterns, context);
RewriteListBuilder<MulOpConversion>::build(patterns, context);
}
};

View File

@ -326,9 +326,9 @@ protected:
/// Initialize the list of converters.
void initConverters(OwningRewritePatternList &patterns,
MLIRContext *context) override {
ConversionListBuilder<AddOpConversion, PrintOpConversion,
ConstantOpConversion, TransposeOpConversion,
ReturnOpConversion>::build(patterns, context);
RewriteListBuilder<AddOpConversion, PrintOpConversion, ConstantOpConversion,
TransposeOpConversion,
ReturnOpConversion>::build(patterns, context);
}
/// Convert a Toy type, this gets called for block and region arguments, and

View File

@ -169,9 +169,8 @@ void TransposeOp::getCanonicalizationPatterns(
// Register our patterns for rewrite by the Canonicalization framework.
void ReshapeOp::getCanonicalizationPatterns(
mlir::OwningRewritePatternList &results, mlir::MLIRContext *context) {
results.push_back(llvm::make_unique<SimplifyReshapeConstant>(context));
results.push_back(llvm::make_unique<SimplifyReshapeReshape>(context));
results.push_back(llvm::make_unique<SimplifyNullReshape>(context));
mlir::RewriteListBuilder<SimplifyReshapeConstant, SimplifyReshapeReshape,
SimplifyNullReshape>::build(results, context);
}
namespace {

View File

@ -628,7 +628,7 @@ protected:
// conversion instances given a list of classes as template parameters.
// These instances will be allocated within `allocator` and their lifetime
// is managed by the Lowering class.
return ConversionListBuilder<
return RewriteListBuilder<
LoadOpConversion, SliceOpConversion, StoreOpConversion,
ViewOpConversion>::build(allocator, context);
}

View File

@ -51,9 +51,8 @@ public:
// This gets called once to set up operation converters.
llvm::DenseSet<DialectOpConversion *>
initConverters(MLIRContext *context) override {
return ConversionListBuilder<MulOpConversion,
PrintOpConversion,
TransposeOpConversion>::build(allocator, context);
RewriteListBuilder<MulOpConversion, PrintOpConversion,
TransposeOpConversion>::build(allocator, context);
}
private:

View File

@ -348,6 +348,29 @@ private:
///
bool applyPatternsGreedily(Function &fn, OwningRewritePatternList &&patterns);
/// Helper class to create a list of rewrite patterns given a list of their
/// types and a list of attributes perfect-forwarded to each of the conversion
/// constructors.
template <typename Arg, typename... Args> struct RewriteListBuilder {
template <typename... ConstructorArgs>
static void build(OwningRewritePatternList &patterns,
ConstructorArgs &&... constructorArgs) {
RewriteListBuilder<Args...>::build(
patterns, std::forward<ConstructorArgs>(constructorArgs)...);
RewriteListBuilder<Arg>::build(
patterns, std::forward<ConstructorArgs>(constructorArgs)...);
}
};
// Template specialization to stop recursion.
template <typename Arg> struct RewriteListBuilder<Arg> {
template <typename... ConstructorArgs>
static void build(OwningRewritePatternList &patterns,
ConstructorArgs &&... constructorArgs) {
patterns.emplace_back(llvm::make_unique<Arg>(
std::forward<ConstructorArgs>(constructorArgs)...));
}
};
} // end namespace mlir
#endif // MLIR_PATTERN_MATCH_H

View File

@ -103,30 +103,6 @@ private:
using RewritePattern::rewrite;
};
// Helper class to create a list of dialect conversion patterns given a list of
// their types and a list of attributes perfect-forwarded to each of the
// conversion constructors.
template <typename Arg, typename... Args> struct ConversionListBuilder {
template <typename... ConstructorArgs>
static void build(OwningRewritePatternList &patterns,
ConstructorArgs &&... constructorArgs) {
ConversionListBuilder<Args...>::build(
patterns, std::forward<ConstructorArgs>(constructorArgs)...);
ConversionListBuilder<Arg>::build(
patterns, std::forward<ConstructorArgs>(constructorArgs)...);
}
};
// Template specialization to stop recursion.
template <typename Arg> struct ConversionListBuilder<Arg> {
template <typename... ConstructorArgs>
static void build(OwningRewritePatternList &patterns,
ConstructorArgs &&... constructorArgs) {
patterns.emplace_back(llvm::make_unique<Arg>(
std::forward<ConstructorArgs>(constructorArgs)...));
}
};
/// Base class for dialect conversion interface. Specific converters must
/// derive this class and implement the pure virtual functions.
///

View File

@ -941,7 +941,7 @@ void LLVMLowering::initConverters(OwningRewritePatternList &patterns,
module = &llvmDialect->getLLVMModule();
// FIXME: this should be tablegen'ed
ConversionListBuilder<
RewriteListBuilder<
AddFOpLowering, AddIOpLowering, AndOpLowering, AllocOpLowering,
BranchOpLowering, CallIndirectOpLowering, CallOpLowering, CmpIOpLowering,
CondBranchOpLowering, ConstLLVMOpLowering, DeallocOpLowering,

View File

@ -588,12 +588,14 @@ namespace {
class Lowering : public LLVMLowering {
protected:
void initAdditionalConverters(OwningRewritePatternList &patterns) override {
return ConversionListBuilder<
BufferAllocOpConversion, BufferDeallocOpConversion,
BufferSizeOpConversion, DimOpConversion, DotOpConversion,
LoadOpConversion, RangeOpConversion, RangeIntersectOpConversion,
SliceOpConversion, StoreOpConversion,
ViewOpConversion>::build(patterns, llvmDialect->getContext(), *this);
RewriteListBuilder<BufferAllocOpConversion, BufferDeallocOpConversion,
BufferSizeOpConversion, DimOpConversion, DotOpConversion,
LoadOpConversion, RangeOpConversion,
RangeIntersectOpConversion, SliceOpConversion,
StoreOpConversion,
ViewOpConversion>::build(patterns,
llvmDialect->getContext(),
*this);
}
Type convertAdditionalType(Type t) override {

View File

@ -378,8 +378,8 @@ struct SimplifyDeadAlloc : public RewritePattern {
void AllocOp::getCanonicalizationPatterns(OwningRewritePatternList &results,
MLIRContext *context) {
results.push_back(llvm::make_unique<SimplifyAllocConst>(context));
results.push_back(llvm::make_unique<SimplifyDeadAlloc>(context));
RewriteListBuilder<SimplifyAllocConst, SimplifyDeadAlloc>::build(results,
context);
}
//===----------------------------------------------------------------------===//