MLIR 24.0.0git
CompositePass.cpp
Go to the documentation of this file.
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
14
15#include "mlir/Pass/Pass.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 dynamicPM, [&](mlir::Pass &pass) { pass.printAsTextualPipeline(os); },
41 [&]() { os << ","; });
42 }
43
44 LogicalResult initializeOptions(
45 StringRef options,
46 function_ref<LogicalResult(const Twine &)> errorHandler) override {
47 if (failed(CompositeFixedPointPassBase::initializeOptions(options,
48 errorHandler)))
49 return failure();
50
51 if (failed(parsePassPipeline(pipelineStr, 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(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(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(runPipeline(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
114 std::string name, llvm::function_ref<void(OpPassManager &)> populateFunc,
115 int maxIterations, ConvergenceFailureAction convergenceFailureAction) {
116
117 return std::make_unique<CompositeFixedPointPass>(
118 std::move(name), populateFunc, maxIterations, convergenceFailureAction);
119}
return success()
LogicalResult initialize(unsigned origNumLoops, ArrayRef< ReassociationIndices > foldedIterationDims)
static llvm::ManagedStatic< PassManagerOptions > options
This class represents a pass manager that runs passes on either a specific operation type,...
Definition PassManager.h:46
void printAsTextualPipeline(raw_ostream &os, bool pretty=false)
Prints out the pass in the textual representation of pipelines.
Definition Pass.cpp:85
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:732
Include the generated interface declarations.
std::unique_ptr< Pass > createCompositeFixedPointPass(std::string name, llvm::function_ref< void(OpPassManager &)> populateFunc, int maxIterations=10, ConvergenceFailureAction convergenceFailureAction=ConvergenceFailureAction::Warn)
Create composite pass, which runs provided set of passes until fixed point or maximum number of itera...
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
ConvergenceFailureAction
Action to take when CompositeFixedPointPass fails to converge within its configured maximum number of...
LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm, raw_ostream &errorStream=llvm::errs())
Parse the textual representation of a pass pipeline, adding the result to 'pm' on success.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147