2018-10-12 08:21:55 +08:00
|
|
|
//===- Canonicalizer.cpp - Canonicalize MLIR operations -------------------===//
|
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-26 07:44:04 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-02-20 09:17:46 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2018-10-12 08:21:55 +08:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
namespace {
|
2019-10-25 06:00:36 +08:00
|
|
|
/// Canonicalize operations in nested regions.
|
|
|
|
struct Canonicalizer : public OperationPass<Canonicalizer> {
|
|
|
|
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();
|
|
|
|
applyPatternsGreedily(op->getRegions(), 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
|
|
|
}
|
2018-11-07 10:34:18 +08:00
|
|
|
|
|
|
|
static PassRegistration<Canonicalizer> pass("canonicalize",
|
|
|
|
"Canonicalize operations");
|