1//===- CompositePass.cpp - Composite pass code ----------------------------===//
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// CompositePass allows to run set of passes until fixed point is reached.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Transforms/Passes.h"
14
15#include "mlir/Pass/Pass.h"
16#include "mlir/Pass/PassManager.h"
17
18namespace mlir {
19#define GEN_PASS_DEF_COMPOSITEFIXEDPOINTPASS
20#include "mlir/Transforms/Passes.h.inc"
21} // namespace mlir
22
23using namespace mlir;
24
25namespace {
26struct CompositeFixedPointPass final
27 : public impl::CompositeFixedPointPassBase<CompositeFixedPointPass> {
28 using CompositeFixedPointPassBase::CompositeFixedPointPassBase;
29
30 CompositeFixedPointPass(
31 std::string name_, llvm::function_ref<void(OpPassManager &)> populateFunc,
32 int maxIterations, ConvergenceFailureAction convergenceFailureActionArg) {
33 name = std::move(name_);
34 maxIter = maxIterations;
35 convergenceFailureAction = convergenceFailureActionArg;
36 populateFunc(dynamicPM);
37
38 llvm::raw_string_ostream os(pipelineStr);
39 llvm::interleave(
40 c: dynamicPM, each_fn: [&](mlir::Pass &pass) { pass.printAsTextualPipeline(os); },
41 between_fn: [&]() { os << ","; });
42 }
43
44 LogicalResult initializeOptions(
45 StringRef options,
46 function_ref<LogicalResult(const Twine &)> errorHandler) override {
47 if (failed(Result: CompositeFixedPointPassBase::initializeOptions(options,
48 errorHandler)))
49 return failure();
50
51 if (failed(Result: parsePassPipeline(pipeline: pipelineStr, pm&: dynamicPM)))
52 return errorHandler("Failed to parse composite pass pipeline");
53
54 return success();
55 }
56
57 LogicalResult initialize(MLIRContext *context) override {
58 if (maxIter <= 0)
59 return emitError(loc: UnknownLoc::get(context))
60 << "Invalid maxIterations value: " << maxIter << "\n";
61
62 return success();
63 }
64
65 void getDependentDialects(DialectRegistry &registry) const override {
66 dynamicPM.getDependentDialects(dialects&: registry);
67 }
68
69 void runOnOperation() override {
70 auto *op = getOperation();
71 OperationFingerPrint fp(op);
72
73 int currentIter = 0;
74 int maxIterVal = maxIter;
75 while (true) {
76 if (failed(Result: runPipeline(pipeline&: dynamicPM, op)))
77 return signalPassFailure();
78
79 if (currentIter++ >= maxIterVal) {
80 std::string message = ("Composite pass \"" + llvm::Twine(name) +
81 "\"+ didn't converge in " +
82 llvm::Twine(maxIterVal) + " iterations")
83 .str();
84 switch (convergenceFailureAction) {
85 case ConvergenceFailureAction::Warn:
86 op->emitWarning(message);
87 break;
88 case ConvergenceFailureAction::Error:
89 op->emitError(message);
90 return signalPassFailure();
91 case ConvergenceFailureAction::Silent:
92 break;
93 }
94 break;
95 }
96
97 OperationFingerPrint newFp(op);
98 if (newFp == fp)
99 break;
100
101 fp = newFp;
102 }
103 }
104
105protected:
106 llvm::StringRef getName() const override { return name; }
107
108private:
109 OpPassManager dynamicPM;
110};
111} // namespace
112
113std::unique_ptr<Pass> mlir::createCompositeFixedPointPass(
114 std::string name, llvm::function_ref<void(OpPassManager &)> populateFunc,
115 int maxIterations, ConvergenceFailureAction convergenceFailureAction) {
116
117 return std::make_unique<CompositeFixedPointPass>(
118 args: std::move(name), args&: populateFunc, args&: maxIterations, args&: convergenceFailureAction);
119}
120