MLIR  19.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 
13 #include "mlir/Transforms/Passes.h"
14 
15 #include "mlir/Pass/Pass.h"
16 #include "mlir/Pass/PassManager.h"
17 
18 namespace mlir {
19 #define GEN_PASS_DEF_COMPOSITEFIXEDPOINTPASS
20 #include "mlir/Transforms/Passes.h.inc"
21 } // namespace mlir
22 
23 using namespace mlir;
24 
25 namespace {
26 struct 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) {
33  name = std::move(name_);
34  maxIter = maxIterations;
35  populateFunc(dynamicPM);
36 
37  llvm::raw_string_ostream os(pipelineStr);
38  dynamicPM.printAsTextualPipeline(os);
39  }
40 
41  LogicalResult initializeOptions(
42  StringRef options,
43  function_ref<LogicalResult(const Twine &)> errorHandler) override {
44  if (failed(CompositeFixedPointPassBase::initializeOptions(options,
45  errorHandler)))
46  return failure();
47 
48  if (failed(parsePassPipeline(pipelineStr, dynamicPM)))
49  return errorHandler("Failed to parse composite pass pipeline");
50 
51  return success();
52  }
53 
54  LogicalResult initialize(MLIRContext *context) override {
55  if (maxIter <= 0)
56  return emitError(UnknownLoc::get(context))
57  << "Invalid maxIterations value: " << maxIter << "\n";
58 
59  return success();
60  }
61 
62  void getDependentDialects(DialectRegistry &registry) const override {
63  dynamicPM.getDependentDialects(registry);
64  }
65 
66  void runOnOperation() override {
67  auto op = getOperation();
68  OperationFingerPrint fp(op);
69 
70  int currentIter = 0;
71  int maxIterVal = maxIter;
72  while (true) {
73  if (failed(runPipeline(dynamicPM, op)))
74  return signalPassFailure();
75 
76  if (currentIter++ >= maxIterVal) {
77  op->emitWarning("Composite pass \"" + llvm::Twine(name) +
78  "\"+ didn't converge in " + llvm::Twine(maxIterVal) +
79  " iterations");
80  break;
81  }
82 
83  OperationFingerPrint newFp(op);
84  if (newFp == fp)
85  break;
86 
87  fp = newFp;
88  }
89  }
90 
91 protected:
92  llvm::StringRef getName() const override { return name; }
93 
94 private:
95  OpPassManager dynamicPM;
96 };
97 } // namespace
98 
100  std::string name, llvm::function_ref<void(OpPassManager &)> populateFunc,
101  int maxIterations) {
102 
103  return std::make_unique<CompositeFixedPointPass>(std::move(name),
104  populateFunc, maxIterations);
105 }
static llvm::ManagedStatic< PassManagerOptions > options
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class represents a pass manager that runs passes on either a specific operation type,...
Definition: PassManager.h:48
A unique fingerprint for a specific operation, and all of it's internal operations (if includeNested ...
InFlightDiagnostic emitWarning(const Twine &message={})
Emit a warning about this operation, reporting up to any diagnostic handlers that may be listening.
Definition: Operation.cpp:280
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
std::unique_ptr< Pass > createCompositeFixedPointPass(std::string name, llvm::function_ref< void(OpPassManager &)> populateFunc, int maxIterations=10)
Create composite pass, which runs provided set of passes until fixed point or maximum number of itera...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
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.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26