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-01-09 05:24:07 +08:00
|
|
|
/// Initialize the canonicalizer by building the set of patterns used during
|
|
|
|
/// execution.
|
|
|
|
void initialize(MLIRContext *context) override {
|
|
|
|
OwningRewritePatternList owningPatterns;
|
2019-10-25 06:00:36 +08:00
|
|
|
for (auto *op : context->getRegisteredOperations())
|
2021-01-09 05:24:07 +08:00
|
|
|
op->getCanonicalizationPatterns(owningPatterns, context);
|
|
|
|
patterns = std::move(owningPatterns);
|
2019-10-25 06:00:36 +08:00
|
|
|
}
|
2021-01-09 05:24:07 +08:00
|
|
|
void runOnOperation() override {
|
|
|
|
applyPatternsAndFoldGreedily(getOperation()->getRegions(), patterns);
|
|
|
|
}
|
|
|
|
|
|
|
|
FrozenRewritePatternList patterns;
|
2018-10-12 08:21:55 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|