[Mlir-commits] [mlir] [mlir][pass] Add composite pass utility (PR #87166)

Ivan Butygin llvmlistbot at llvm.org
Mon Apr 1 08:07:17 PDT 2024


================
@@ -0,0 +1,106 @@
+//===- CompositePass.cpp - Composite pass code ----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// CompositePass allows to run set of passes until fixed point is reached.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Transforms/Passes.h"
+
+#include "mlir/Pass/Pass.h"
+#include "mlir/Pass/PassManager.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_COMPOSITEFIXEDPOINTPASS
+#include "mlir/Transforms/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+namespace {
+struct CompositeFixedPointPass final
+    : public impl::CompositeFixedPointPassBase<CompositeFixedPointPass> {
+  using CompositeFixedPointPassBase::CompositeFixedPointPassBase;
+
+  CompositeFixedPointPass(
+      std::string name_, llvm::function_ref<void(OpPassManager &)> populateFunc,
+      unsigned maxIterations) {
+    name = std::move(name_);
+    maxIter = maxIterations;
+    populateFunc(dynamicPM);
+
+    llvm::raw_string_ostream os(pipelineStr);
+    dynamicPM.printAsTextualPipeline(os);
+    os.flush();
+  }
+
+  LogicalResult initializeOptions(StringRef options) override {
+    if (failed(CompositeFixedPointPassBase::initializeOptions(options)))
+      return failure();
+
+    if (failed(parsePassPipeline(pipelineStr, dynamicPM))) {
+      llvm::errs() << "Failed to parse composite pass pipeline\n";
+      return failure();
+    }
+
+    return success();
+  }
+
+  LogicalResult initialize(MLIRContext * /*context*/) override {
+    if (maxIter <= 0) {
+      llvm::errs() << "Invalid maxIterations value: " << maxIter << "\n";
----------------
Hardcode84 wrote:

I don't think context even exists as this point, but the calling function provides `errorHandler` callback: https://github.com/llvm/llvm-project/blob/6318dd82732c583fcfa12fe5b8d837e048dbb571/mlir/lib/Pass/PassRegistry.cpp#L41

We probably need to change `initializeOptions` signature to accept error callback as well.

Also, default pass implementation calls `parseFromString` which also diretly writes to `llvm::errs()`
https://github.com/llvm/llvm-project/blob/6318dd82732c583fcfa12fe5b8d837e048dbb571/mlir/lib/Pass/Pass.cpp#L53
https://github.com/llvm/llvm-project/blob/6318dd82732c583fcfa12fe5b8d837e048dbb571/mlir/lib/Pass/PassRegistry.cpp#L283

https://github.com/llvm/llvm-project/pull/87166


More information about the Mlir-commits mailing list