1 //===- CompositePass.cpp - Composite pass code ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // CompositePass allows to run set of passes until fixed point is reached.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/Transforms/Passes.h"
15 #include "mlir/Pass/Pass.h"
16 #include "mlir/Pass/PassManager.h"
19 #define GEN_PASS_DEF_COMPOSITEFIXEDPOINTPASS
20 #include "mlir/Transforms/Passes.h.inc"
26 struct CompositeFixedPointPass final
27 : public impl::CompositeFixedPointPassBase
<CompositeFixedPointPass
> {
28 using CompositeFixedPointPassBase::CompositeFixedPointPassBase
;
30 CompositeFixedPointPass(
31 std::string name_
, llvm::function_ref
<void(OpPassManager
&)> populateFunc
,
33 name
= std::move(name_
);
34 maxIter
= maxIterations
;
35 populateFunc(dynamicPM
);
37 llvm::raw_string_ostream
os(pipelineStr
);
38 dynamicPM
.printAsTextualPipeline(os
);
41 LogicalResult
initializeOptions(
43 function_ref
<LogicalResult(const Twine
&)> errorHandler
) override
{
44 if (failed(CompositeFixedPointPassBase::initializeOptions(options
,
48 if (failed(parsePassPipeline(pipelineStr
, dynamicPM
)))
49 return errorHandler("Failed to parse composite pass pipeline");
54 LogicalResult
initialize(MLIRContext
*context
) override
{
56 return emitError(UnknownLoc::get(context
))
57 << "Invalid maxIterations value: " << maxIter
<< "\n";
62 void getDependentDialects(DialectRegistry
®istry
) const override
{
63 dynamicPM
.getDependentDialects(registry
);
66 void runOnOperation() override
{
67 auto op
= getOperation();
68 OperationFingerPrint
fp(op
);
71 int maxIterVal
= maxIter
;
73 if (failed(runPipeline(dynamicPM
, op
)))
74 return signalPassFailure();
76 if (currentIter
++ >= maxIterVal
) {
77 op
->emitWarning("Composite pass \"" + llvm::Twine(name
) +
78 "\"+ didn't converge in " + llvm::Twine(maxIterVal
) +
83 OperationFingerPrint
newFp(op
);
92 llvm::StringRef
getName() const override
{ return name
; }
95 OpPassManager dynamicPM
;
99 std::unique_ptr
<Pass
> mlir::createCompositeFixedPointPass(
100 std::string name
, llvm::function_ref
<void(OpPassManager
&)> populateFunc
,
103 return std::make_unique
<CompositeFixedPointPass
>(std::move(name
),
104 populateFunc
, maxIterations
);