2018-10-11 17:21:55 -07:00
|
|
|
//===- Canonicalizer.cpp - Canonicalize MLIR operations -------------------===//
|
|
|
|
|
//
|
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-11 17:21:55 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-11 17:21:55 -07:00
|
|
|
//
|
|
|
|
|
// This transformation pass converts operations into their canonical forms by
|
|
|
|
|
// folding constants, applying operation identity transformations etc.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2020-04-07 13:58:12 -07:00
|
|
|
#include "PassDetail.h"
|
2018-10-25 16:44:04 -07:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-02-19 17:17:46 -08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2018-10-11 17:21:55 -07:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2020-03-26 21:48:53 +05:30
|
|
|
|
2018-10-11 17:21:55 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2019-10-24 15:00:36 -07:00
|
|
|
/// Canonicalize operations in nested regions.
|
2020-04-07 13:58:12 -07:00
|
|
|
struct Canonicalizer : public CanonicalizerBase<Canonicalizer> {
|
2019-10-24 15:00:36 -07: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-04-05 06:54:16 +05:30
|
|
|
applyPatternsAndFoldGreedily(op->getRegions(), patterns);
|
2019-10-24 15:00:36 -07:00
|
|
|
}
|
2018-10-11 17:21:55 -07:00
|
|
|
};
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
/// Create a Canonicalizer pass.
|
2019-10-24 15:00:36 -07:00
|
|
|
std::unique_ptr<Pass> mlir::createCanonicalizerPass() {
|
2019-08-17 11:05:35 -07:00
|
|
|
return std::make_unique<Canonicalizer>();
|
2019-02-27 10:59:29 -08:00
|
|
|
}
|