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.
1641574554SRiver Riddle struct TestConstantFold
1787d6bf37SRiver Riddle     : public PassWrapper<TestConstantFold, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anone37b23d50111::TestConstantFold18*5e50dd04SRiver Riddle   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestConstantFold)
19*5e50dd04SRiver Riddle 
20b5e22e6dSMehdi Amini   StringRef getArgument() const final { return "test-constant-fold"; }
getDescription__anone37b23d50111::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;
29dac75ae5SNicolas Vasilache };
30be0a7e9fSMehdi Amini } // namespace
31dac75ae5SNicolas Vasilache 
foldOperation(Operation * op,OperationFolder & helper)32dac75ae5SNicolas Vasilache void TestConstantFold::foldOperation(Operation *op, OperationFolder &helper) {
33dac75ae5SNicolas Vasilache   auto processGeneratedConstants = [this](Operation *op) {
34dac75ae5SNicolas Vasilache     existingConstants.push_back(op);
35dac75ae5SNicolas Vasilache   };
36dac75ae5SNicolas Vasilache 
37dac75ae5SNicolas Vasilache   // Attempt to fold the specified operation, including handling unused or
38dac75ae5SNicolas Vasilache   // duplicated constants.
39dac75ae5SNicolas Vasilache   (void)helper.tryToFold(op, processGeneratedConstants);
40dac75ae5SNicolas Vasilache }
41dac75ae5SNicolas Vasilache 
runOnOperation()4241574554SRiver Riddle void TestConstantFold::runOnOperation() {
43dac75ae5SNicolas Vasilache   existingConstants.clear();
44dac75ae5SNicolas Vasilache 
4587d6bf37SRiver Riddle   // Collect and fold the operations within the operation.
46dac75ae5SNicolas Vasilache   SmallVector<Operation *, 8> ops;
4787d6bf37SRiver Riddle   getOperation()->walk([&](Operation *op) { ops.push_back(op); });
48dac75ae5SNicolas Vasilache 
49dac75ae5SNicolas Vasilache   // Fold the constants in reverse so that the last generated constants from
50dac75ae5SNicolas Vasilache   // folding are at the beginning. This creates somewhat of a linear ordering to
51dac75ae5SNicolas Vasilache   // the newly generated constants that matches the operation order and improves
52dac75ae5SNicolas Vasilache   // the readability of test cases.
536563b1c4SRiver Riddle   OperationFolder helper(&getContext());
54dac75ae5SNicolas Vasilache   for (Operation *op : llvm::reverse(ops))
55dac75ae5SNicolas Vasilache     foldOperation(op, helper);
56dac75ae5SNicolas Vasilache 
57dac75ae5SNicolas Vasilache   // By the time we are done, we may have simplified a bunch of code, leaving
58dac75ae5SNicolas Vasilache   // around dead constants. Check for them now and remove them.
59dac75ae5SNicolas Vasilache   for (auto *cst : existingConstants) {
60dac75ae5SNicolas Vasilache     if (cst->use_empty())
61dac75ae5SNicolas Vasilache       cst->erase();
62dac75ae5SNicolas Vasilache   }
63dac75ae5SNicolas Vasilache }
64dac75ae5SNicolas Vasilache 
65c6477050SMehdi Amini namespace mlir {
6672c65b69SAlexander Belyaev namespace test {
registerTestConstantFold()67b5e22e6dSMehdi Amini void registerTestConstantFold() { PassRegistration<TestConstantFold>(); }
6872c65b69SAlexander Belyaev } // namespace test
69c6477050SMehdi Amini } // namespace mlir
70