2018-10-12 08:21:55 +08:00
|
|
|
//===- Canonicalizer.cpp - Canonicalize MLIR operations -------------------===//
|
|
|
|
//
|
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-12 08:21:55 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-12 08:21:55 +08:00
|
|
|
//
|
|
|
|
// This transformation pass converts operations into their canonical forms by
|
|
|
|
// folding constants, applying operation identity transformations etc.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-08 04:58:12 +08:00
|
|
|
#include "PassDetail.h"
|
2019-02-20 09:17:46 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2020-10-27 08:24:17 +08:00
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
2018-10-12 08:21:55 +08:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2020-03-27 00:18:53 +08:00
|
|
|
|
2018-10-12 08:21:55 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
namespace {
|
2019-10-25 06:00:36 +08:00
|
|
|
/// Canonicalize operations in nested regions.
|
2020-04-08 04:58:12 +08:00
|
|
|
struct Canonicalizer : public CanonicalizerBase<Canonicalizer> {
|
2021-12-21 09:18:14 +08:00
|
|
|
Canonicalizer(const GreedyRewriteConfig &config,
|
|
|
|
ArrayRef<std::string> disabledPatterns,
|
|
|
|
ArrayRef<std::string> enabledPatterns)
|
|
|
|
: config(config) {
|
|
|
|
this->disabledPatterns = disabledPatterns;
|
|
|
|
this->enabledPatterns = enabledPatterns;
|
|
|
|
}
|
2021-05-25 12:23:16 +08:00
|
|
|
|
|
|
|
Canonicalizer() {
|
|
|
|
// Default constructed Canonicalizer takes its settings from command line
|
|
|
|
// options.
|
|
|
|
config.useTopDownTraversal = topDownProcessingEnabled;
|
|
|
|
config.enableRegionSimplification = enableRegionSimplification;
|
|
|
|
config.maxIterations = maxIterations;
|
|
|
|
}
|
|
|
|
|
2021-01-09 05:24:07 +08:00
|
|
|
/// Initialize the canonicalizer by building the set of patterns used during
|
|
|
|
/// execution.
|
2021-02-11 09:36:40 +08:00
|
|
|
LogicalResult initialize(MLIRContext *context) override {
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet owningPatterns(context);
|
2021-05-27 16:26:45 +08:00
|
|
|
for (auto *dialect : context->getLoadedDialects())
|
|
|
|
dialect->getCanonicalizationPatterns(owningPatterns);
|
2021-11-18 05:50:28 +08:00
|
|
|
for (RegisteredOperationName op : context->getRegisteredOperations())
|
|
|
|
op.getCanonicalizationPatterns(owningPatterns, context);
|
2021-06-03 02:43:01 +08:00
|
|
|
|
|
|
|
patterns = FrozenRewritePatternSet(std::move(owningPatterns),
|
|
|
|
disabledPatterns, enabledPatterns);
|
2021-02-11 09:36:40 +08:00
|
|
|
return success();
|
2019-10-25 06:00:36 +08:00
|
|
|
}
|
2021-01-09 05:24:07 +08:00
|
|
|
void runOnOperation() override {
|
2021-05-24 02:24:59 +08:00
|
|
|
(void)applyPatternsAndFoldGreedily(getOperation()->getRegions(), patterns,
|
|
|
|
config);
|
2021-01-09 05:24:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:23:16 +08:00
|
|
|
GreedyRewriteConfig config;
|
2021-03-23 08:36:27 +08:00
|
|
|
FrozenRewritePatternSet patterns;
|
2018-10-12 08:21:55 +08:00
|
|
|
};
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|
2018-10-12 08:21:55 +08:00
|
|
|
|
|
|
|
/// Create a Canonicalizer pass.
|
2019-10-25 06:00:36 +08:00
|
|
|
std::unique_ptr<Pass> mlir::createCanonicalizerPass() {
|
2019-08-18 02:05:35 +08:00
|
|
|
return std::make_unique<Canonicalizer>();
|
2019-02-28 02:59:29 +08:00
|
|
|
}
|
2021-05-25 12:23:16 +08:00
|
|
|
|
|
|
|
/// Creates an instance of the Canonicalizer pass with the specified config.
|
|
|
|
std::unique_ptr<Pass>
|
2021-12-23 03:16:09 +08:00
|
|
|
mlir::createCanonicalizerPass(const GreedyRewriteConfig &config,
|
|
|
|
ArrayRef<std::string> disabledPatterns,
|
|
|
|
ArrayRef<std::string> enabledPatterns) {
|
2021-12-21 09:18:14 +08:00
|
|
|
return std::make_unique<Canonicalizer>(config, disabledPatterns,
|
|
|
|
enabledPatterns);
|
2021-05-25 12:23:16 +08:00
|
|
|
}
|