1 //===- TestConstantFold.cpp - Pass to test constant folding ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Pass/Pass.h"
10 #include "mlir/Transforms/FoldUtils.h"
11 
12 using namespace mlir;
13 
14 namespace {
15 /// Simple constant folding pass.
16 struct TestConstantFold
17     : public PassWrapper<TestConstantFold, OperationPass<FuncOp>> {
18   StringRef getArgument() const final { return "test-constant-fold"; }
19   StringRef getDescription() const final {
20     return "Test operation constant folding";
21   }
22   // All constants in the function post folding.
23   SmallVector<Operation *, 8> existingConstants;
24 
25   void foldOperation(Operation *op, OperationFolder &helper);
26   void runOnOperation() override;
27 };
28 } // namespace
29 
30 void TestConstantFold::foldOperation(Operation *op, OperationFolder &helper) {
31   auto processGeneratedConstants = [this](Operation *op) {
32     existingConstants.push_back(op);
33   };
34 
35   // Attempt to fold the specified operation, including handling unused or
36   // duplicated constants.
37   (void)helper.tryToFold(op, processGeneratedConstants);
38 }
39 
40 // For now, we do a simple top-down pass over a function folding constants.  We
41 // don't handle conditional control flow, block arguments, folding conditional
42 // branches, or anything else fancy.
43 void TestConstantFold::runOnOperation() {
44   existingConstants.clear();
45 
46   // Collect and fold the operations within the function.
47   SmallVector<Operation *, 8> ops;
48   getOperation().walk([&](Operation *op) { ops.push_back(op); });
49 
50   // Fold the constants in reverse so that the last generated constants from
51   // folding are at the beginning. This creates somewhat of a linear ordering to
52   // the newly generated constants that matches the operation order and improves
53   // the readability of test cases.
54   OperationFolder helper(&getContext());
55   for (Operation *op : llvm::reverse(ops))
56     foldOperation(op, helper);
57 
58   // By the time we are done, we may have simplified a bunch of code, leaving
59   // around dead constants.  Check for them now and remove them.
60   for (auto *cst : existingConstants) {
61     if (cst->use_empty())
62       cst->erase();
63   }
64 }
65 
66 namespace mlir {
67 namespace test {
68 void registerTestConstantFold() { PassRegistration<TestConstantFold>(); }
69 } // namespace test
70 } // namespace mlir
71