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> {
|
2019-10-25 06:00:36 +08:00
|
|
|
void runOnOperation() override {
|
|
|
|
OwningRewritePatternList patterns;
|
|
|
|
|
|
|
|
// TODO: Instead of adding all known patterns from the whole system lazily
|
|
|
|
// add and cache the canonicalization patterns for ops we see in practice
|
|
|
|
// when building the worklist. For now, we just grab everything.
|
|
|
|
auto *context = &getContext();
|
|
|
|
for (auto *op : context->getRegisteredOperations())
|
|
|
|
op->getCanonicalizationPatterns(patterns, context);
|
|
|
|
|
|
|
|
Operation *op = getOperation();
|
2020-10-27 08:25:01 +08:00
|
|
|
applyPatternsAndFoldGreedily(op->getRegions(), std::move(patterns));
|
2019-10-25 06:00:36 +08:00
|
|
|
}
|
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
|
|
|
}
|