1//===- TestSingleFold.cpp - Pass to test single-pass 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
12using namespace mlir;
13
14namespace {
15/// Test pass for single-pass constant folding.
16///
17/// This pass tests the behavior of operations when folded exactly once. Unlike
18/// canonicalization passes that may apply multiple rounds of folding, this pass
19/// ensures that each operation is folded at most once, which is useful for
20/// testing scenarios where the fold implementation should handle complex cases
21/// without requiring multiple iterations.
22///
23/// The pass also removes dead constants after folding to clean up unused
24/// intermediate results.
25struct TestSingleFold : public PassWrapper<TestSingleFold, OperationPass<>>,
26 public RewriterBase::Listener {
27 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSingleFold)
28
29 StringRef getArgument() const final { return "test-single-fold"; }
30 StringRef getDescription() const final {
31 return "Test single-pass operation folding and dead constant elimination";
32 }
33 // All constants in the operation post folding.
34 SmallVector<Operation *> existingConstants;
35
36 void foldOperation(Operation *op, OperationFolder &helper);
37 void runOnOperation() override;
38
39 void notifyOperationInserted(Operation *op,
40 OpBuilder::InsertPoint previous) override {
41 existingConstants.push_back(Elt: op);
42 }
43 void notifyOperationErased(Operation *op) override {
44 auto *it = llvm::find(Range&: existingConstants, Val: op);
45 if (it != existingConstants.end())
46 existingConstants.erase(CI: it);
47 }
48};
49} // namespace
50
51void TestSingleFold::foldOperation(Operation *op, OperationFolder &helper) {
52 // Attempt to fold the specified operation, including handling unused or
53 // duplicated constants.
54 (void)helper.tryToFold(op);
55}
56
57void TestSingleFold::runOnOperation() {
58 existingConstants.clear();
59
60 // Collect and fold the operations within the operation.
61 SmallVector<Operation *, 8> ops;
62 getOperation()->walk<mlir::WalkOrder::PreOrder>(
63 callback: [&](Operation *op) { ops.push_back(Elt: op); });
64
65 // Fold the constants in reverse so that the last generated constants from
66 // folding are at the beginning. This creates somewhat of a linear ordering to
67 // the newly generated constants that matches the operation order and improves
68 // the readability of test cases.
69 OperationFolder helper(&getContext(), /*listener=*/this);
70 for (Operation *op : llvm::reverse(C&: ops))
71 foldOperation(op, helper);
72
73 // By the time we are done, we may have simplified a bunch of code, leaving
74 // around dead constants. Check for them now and remove them.
75 for (auto *cst : existingConstants) {
76 if (cst->use_empty())
77 cst->erase();
78 }
79}
80
81namespace mlir {
82namespace test {
83void registerTestSingleFold() { PassRegistration<TestSingleFold>(); }
84} // namespace test
85} // namespace mlir
86

source code of mlir/test/lib/Transforms/TestSingleFold.cpp