-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][pass] Add composite pass utility #87166
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7a681cd
[mlir][pass] Add composite pass utility
Hardcode84 1d7049b
use pipeline string
Hardcode84 c899033
renamings
Hardcode84 5781e46
Docs
Hardcode84 2d21a6f
copy PM
Hardcode84 6e2abe7
remove temp string
Hardcode84 513a8ae
use int
Hardcode84 0006dae
remove flush
Hardcode84 01a8221
use emitError
Hardcode84 13c8d2d
fix more ints
Hardcode84 bb49a5f
Update initializeOptions
Hardcode84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//===- 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, | ||
int maxIterations) { | ||
name = std::move(name_); | ||
maxIter = maxIterations; | ||
populateFunc(dynamicPM); | ||
|
||
llvm::raw_string_ostream os(pipelineStr); | ||
dynamicPM.printAsTextualPipeline(os); | ||
} | ||
|
||
LogicalResult initializeOptions( | ||
StringRef options, | ||
function_ref<LogicalResult(const Twine &)> errorHandler) override { | ||
if (failed(CompositeFixedPointPassBase::initializeOptions(options, | ||
errorHandler))) | ||
return failure(); | ||
|
||
if (failed(parsePassPipeline(pipelineStr, dynamicPM))) | ||
return errorHandler("Failed to parse composite pass pipeline"); | ||
|
||
return success(); | ||
} | ||
|
||
LogicalResult initialize(MLIRContext *context) override { | ||
if (maxIter <= 0) | ||
return emitError(UnknownLoc::get(context)) | ||
<< "Invalid maxIterations value: " << maxIter << "\n"; | ||
|
||
return success(); | ||
} | ||
|
||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
dynamicPM.getDependentDialects(registry); | ||
} | ||
|
||
void runOnOperation() override { | ||
auto op = getOperation(); | ||
OperationFingerPrint fp(op); | ||
|
||
int currentIter = 0; | ||
int maxIterVal = maxIter; | ||
while (true) { | ||
if (failed(runPipeline(dynamicPM, op))) | ||
return signalPassFailure(); | ||
|
||
if (currentIter++ >= maxIterVal) { | ||
op->emitWarning("Composite pass \"" + llvm::Twine(name) + | ||
"\"+ didn't converge in " + llvm::Twine(maxIterVal) + | ||
" iterations"); | ||
break; | ||
} | ||
|
||
OperationFingerPrint newFp(op); | ||
if (newFp == fp) | ||
break; | ||
|
||
fp = newFp; | ||
} | ||
} | ||
|
||
protected: | ||
llvm::StringRef getName() const override { return name; } | ||
|
||
private: | ||
OpPassManager dynamicPM; | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<Pass> mlir::createCompositeFixedPointPass( | ||
std::string name, llvm::function_ref<void(OpPassManager &)> populateFunc, | ||
int maxIterations) { | ||
|
||
return std::make_unique<CompositeFixedPointPass>(std::move(name), | ||
populateFunc, maxIterations); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: mlir-opt %s --log-actions-to=- --test-composite-fixed-point-pass -split-input-file | FileCheck %s | ||
// RUN: mlir-opt %s --log-actions-to=- --composite-fixed-point-pass='name=TestCompositePass pipeline=any(canonicalize,cse)' -split-input-file | FileCheck %s | ||
|
||
// CHECK-LABEL: running `TestCompositePass` | ||
// CHECK: running `Canonicalizer` | ||
// CHECK: running `CSE` | ||
// CHECK-NOT: running `Canonicalizer` | ||
// CHECK-NOT: running `CSE` | ||
func.func @test() { | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: running `TestCompositePass` | ||
// CHECK: running `Canonicalizer` | ||
// CHECK: running `CSE` | ||
// CHECK: running `Canonicalizer` | ||
// CHECK: running `CSE` | ||
// CHECK-NOT: running `Canonicalizer` | ||
// CHECK-NOT: running `CSE` | ||
func.func @test() { | ||
// this constant will be canonicalized away, causing another pass iteration | ||
%0 = arith.constant 1.5 : f32 | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===------ TestCompositePass.cpp --- composite test pass -----------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a pass to test the composite pass utility. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassManager.h" | ||
#include "mlir/Pass/PassRegistry.h" | ||
#include "mlir/Transforms/Passes.h" | ||
|
||
namespace mlir { | ||
namespace test { | ||
void registerTestCompositePass() { | ||
registerPassPipeline( | ||
"test-composite-fixed-point-pass", "Test composite pass", | ||
[](OpPassManager &pm, StringRef optionsStr, | ||
function_ref<LogicalResult(const Twine &)> errorHandler) { | ||
if (!optionsStr.empty()) | ||
return failure(); | ||
|
||
pm.addPass(createCompositeFixedPointPass( | ||
"TestCompositePass", [](OpPassManager &p) { | ||
p.addPass(createCanonicalizerPass()); | ||
p.addPass(createCSEPass()); | ||
})); | ||
return success(); | ||
}, | ||
[](function_ref<void(const detail::PassOptions &)>) {}); | ||
} | ||
} // namespace test | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just replace all this list with
#define GEN_PASS_DECL
? Are there passes we don't want here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are
ViewOpGraph
andLocationSnapshot
passes which are not listed here but listed in separate header files, so if I addGEN_PASS_DECL
here it fails with struct redefinition errors.