| 1 | //===------ TestCompositePass.cpp --- composite test pass -----------------===// |
| 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 | // This file implements a pass to test the composite pass utility. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/IR/Builders.h" |
| 14 | #include "mlir/IR/BuiltinAttributes.h" |
| 15 | #include "mlir/Pass/Pass.h" |
| 16 | #include "mlir/Pass/PassManager.h" |
| 17 | #include "mlir/Pass/PassRegistry.h" |
| 18 | #include "mlir/Transforms/Passes.h" |
| 19 | |
| 20 | using namespace mlir; |
| 21 | |
| 22 | namespace { |
| 23 | /// A pass that increments an attribute on the operation on every run, |
| 24 | /// guaranteeing that it never reaches a fixed point. Used to test |
| 25 | /// `CompositeFixedPointPass`'s convergence-failure handling. |
| 26 | struct TestIncrementAttrPass |
| 27 | : public PassWrapper<TestIncrementAttrPass, OperationPass<>> { |
| 28 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestIncrementAttrPass) |
| 29 | |
| 30 | StringRef getArgument() const final { return "test-increment-attr" ; } |
| 31 | StringRef getDescription() const final { |
| 32 | return "Test pass that increments an attribute on the operation on " |
| 33 | "every run, so it never reaches a fixed point" ; |
| 34 | } |
| 35 | |
| 36 | void runOnOperation() override { |
| 37 | Operation *op = getOperation(); |
| 38 | int64_t counter = 0; |
| 39 | if (auto attr = op->getAttrOfType<IntegerAttr>(name: "test.counter" )) |
| 40 | counter = attr.getInt(); |
| 41 | op->setAttr(name: "test.counter" , value: Builder(op).getI64IntegerAttr(value: counter + 1)); |
| 42 | } |
| 43 | }; |
| 44 | } // namespace |
| 45 | |
| 46 | namespace mlir { |
| 47 | namespace test { |
| 48 | void registerTestCompositePass() { |
| 49 | registerPassPipeline( |
| 50 | arg: "test-composite-fixed-point-pass" , description: "Test composite pass" , |
| 51 | function: [](OpPassManager &pm, StringRef optionsStr, |
| 52 | function_ref<LogicalResult(const Twine &)> errorHandler) { |
| 53 | if (!optionsStr.empty()) |
| 54 | return failure(); |
| 55 | |
| 56 | pm.addPass(pass: createCompositeFixedPointPass( |
| 57 | name: "TestCompositePass" , populateFunc: [](OpPassManager &p) { |
| 58 | p.addPass(pass: createCanonicalizerPass()); |
| 59 | p.addPass(pass: createCSEPass()); |
| 60 | })); |
| 61 | return success(); |
| 62 | }, |
| 63 | optHandler: [](function_ref<void(const detail::PassOptions &)>) {}); |
| 64 | |
| 65 | PassRegistration<TestIncrementAttrPass>(); |
| 66 | } |
| 67 | } // namespace test |
| 68 | } // namespace mlir |
| 69 | |