1dac75ae5SNicolas Vasilache //===- TestConstantFold.cpp - Pass to test constant folding ---------------===//
2dac75ae5SNicolas Vasilache //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6dac75ae5SNicolas Vasilache //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
8dac75ae5SNicolas Vasilache
9dac75ae5SNicolas Vasilache #include "mlir/Pass/Pass.h"
10dac75ae5SNicolas Vasilache #include "mlir/Transforms/FoldUtils.h"
11dac75ae5SNicolas Vasilache
12dac75ae5SNicolas Vasilache using namespace mlir;
13dac75ae5SNicolas Vasilache
14dac75ae5SNicolas Vasilache namespace {
15dac75ae5SNicolas Vasilache /// Simple constant folding pass.
169bdfa8dfSMatthias Springer struct TestConstantFold : public PassWrapper<TestConstantFold, OperationPass<>>,
179bdfa8dfSMatthias Springer public RewriterBase::Listener {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon07a5d0d80111::TestConstantFold185e50dd04SRiver Riddle MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestConstantFold)
195e50dd04SRiver Riddle
20b5e22e6dSMehdi Amini StringRef getArgument() const final { return "test-constant-fold"; }
getDescription__anon07a5d0d80111::TestConstantFold21b5e22e6dSMehdi Amini StringRef getDescription() const final {
22b5e22e6dSMehdi Amini return "Test operation constant folding";
23b5e22e6dSMehdi Amini }
2487d6bf37SRiver Riddle // All constants in the operation post folding.
2587d6bf37SRiver Riddle SmallVector<Operation *> existingConstants;
26dac75ae5SNicolas Vasilache
27dac75ae5SNicolas Vasilache void foldOperation(Operation *op, OperationFolder &helper);
2841574554SRiver Riddle void runOnOperation() override;
299bdfa8dfSMatthias Springer
notifyOperationInserted__anon07a5d0d80111::TestConstantFold305cc0f76dSMatthias Springer void notifyOperationInserted(Operation *op,
315cc0f76dSMatthias Springer OpBuilder::InsertPoint previous) override {
329bdfa8dfSMatthias Springer existingConstants.push_back(op);
339bdfa8dfSMatthias Springer }
notifyOperationErased__anon07a5d0d80111::TestConstantFold34*914e6074SMatthias Springer void notifyOperationErased(Operation *op) override {
354345b205SMehdi Amini auto *it = llvm::find(existingConstants, op);
369bdfa8dfSMatthias Springer if (it != existingConstants.end())
379bdfa8dfSMatthias Springer existingConstants.erase(it);
389bdfa8dfSMatthias Springer }
39dac75ae5SNicolas Vasilache };
40be0a7e9fSMehdi Amini } // namespace
41dac75ae5SNicolas Vasilache
foldOperation(Operation * op,OperationFolder & helper)42dac75ae5SNicolas Vasilache void TestConstantFold::foldOperation(Operation *op, OperationFolder &helper) {
43dac75ae5SNicolas Vasilache // Attempt to fold the specified operation, including handling unused or
44dac75ae5SNicolas Vasilache // duplicated constants.
459bdfa8dfSMatthias Springer (void)helper.tryToFold(op);
46dac75ae5SNicolas Vasilache }
47dac75ae5SNicolas Vasilache
runOnOperation()4841574554SRiver Riddle void TestConstantFold::runOnOperation() {
49dac75ae5SNicolas Vasilache existingConstants.clear();
50dac75ae5SNicolas Vasilache
5187d6bf37SRiver Riddle // Collect and fold the operations within the operation.
52dac75ae5SNicolas Vasilache SmallVector<Operation *, 8> ops;
53794f74e2SMehdi Amini getOperation()->walk<mlir::WalkOrder::PreOrder>([&](Operation *op) { ops.push_back(op); });
54dac75ae5SNicolas Vasilache
55dac75ae5SNicolas Vasilache // Fold the constants in reverse so that the last generated constants from
56dac75ae5SNicolas Vasilache // folding are at the beginning. This creates somewhat of a linear ordering to
57dac75ae5SNicolas Vasilache // the newly generated constants that matches the operation order and improves
58dac75ae5SNicolas Vasilache // the readability of test cases.
599bdfa8dfSMatthias Springer OperationFolder helper(&getContext(), /*listener=*/this);
60dac75ae5SNicolas Vasilache for (Operation *op : llvm::reverse(ops))
61dac75ae5SNicolas Vasilache foldOperation(op, helper);
62dac75ae5SNicolas Vasilache
63dac75ae5SNicolas Vasilache // By the time we are done, we may have simplified a bunch of code, leaving
64dac75ae5SNicolas Vasilache // around dead constants. Check for them now and remove them.
65dac75ae5SNicolas Vasilache for (auto *cst : existingConstants) {
66dac75ae5SNicolas Vasilache if (cst->use_empty())
67dac75ae5SNicolas Vasilache cst->erase();
68dac75ae5SNicolas Vasilache }
69dac75ae5SNicolas Vasilache }
70dac75ae5SNicolas Vasilache
71c6477050SMehdi Amini namespace mlir {
7272c65b69SAlexander Belyaev namespace test {
registerTestConstantFold()73b5e22e6dSMehdi Amini void registerTestConstantFold() { PassRegistration<TestConstantFold>(); }
7472c65b69SAlexander Belyaev } // namespace test
75c6477050SMehdi Amini } // namespace mlir
76