-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Add fast walk-based pattern rewrite driver #113825
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
883902d
[mlir] Add walk pattern rewrite driver
kuhar 34d440a
Iterate over regions
kuhar ccc4e08
Add action
kuhar 62cb821
Coding style
kuhar aaa9f00
Cleanup
kuhar 2b1205f
Cleanup
kuhar d70b289
Typo
kuhar 9d295d8
Clarify docs
kuhar 63d0a9b
Guard forwarding listener
kuhar 4a524ae
Guard against invalid block erasure. Support forwarding to null liste…
kuhar 5e85a8f
Add missing newline
kuhar dd4ed14
Fix typo
kuhar 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
//===- WALKPATTERNREWRITEDRIVER.h - Walk Pattern Rewrite Driver -*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Declares a helper function to walk the given op and apply rewrite patterns. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_TRANSFORMS_WALKPATTERNREWRITEDRIVER_H_ | ||
#define MLIR_TRANSFORMS_WALKPATTERNREWRITEDRIVER_H_ | ||
|
||
#include "mlir/IR/Visitors.h" | ||
#include "mlir/Rewrite/FrozenRewritePatternSet.h" | ||
|
||
namespace mlir { | ||
|
||
/// A fast walk-based pattern rewrite driver. Rewrites ops nested under the | ||
/// given operation by walking it and applying the highest benefit patterns. | ||
/// This rewriter *does not* wait until a fixpoint is reached and *does not* | ||
/// visit modified or newly replaced ops. Also *does not* perform folding or | ||
/// dead-code elimination. | ||
/// | ||
/// This is intended as the simplest and most lightweight pattern rewriter in | ||
/// cases when a simple walk gets the job done. | ||
/// | ||
/// Note: Does not apply patterns to the given operation itself. | ||
void walkAndApplyPatterns(Operation *op, | ||
matthias-springer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const FrozenRewritePatternSet &patterns, | ||
RewriterBase::Listener *listener = nullptr); | ||
|
||
} // namespace mlir | ||
|
||
#endif // MLIR_TRANSFORMS_WALKPATTERNREWRITEDRIVER_H_ |
This file contains hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
//===- WalkPatternRewriteDriver.cpp - A fast walk-based rewriter ---------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implements mlir::walkAndApplyPatterns. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Transforms/WalkPatternRewriteDriver.h" | ||
|
||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/OperationSupport.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/IR/Verifier.h" | ||
#include "mlir/IR/Visitors.h" | ||
#include "mlir/Rewrite/PatternApplicator.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
#define DEBUG_TYPE "walk-rewriter" | ||
|
||
namespace mlir { | ||
|
||
namespace { | ||
struct WalkAndApplyPatternsAction final | ||
: tracing::ActionImpl<WalkAndApplyPatternsAction> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(WalkAndApplyPatternsAction) | ||
using ActionImpl::ActionImpl; | ||
static constexpr StringLiteral tag = "walk-and-apply-patterns"; | ||
void print(raw_ostream &os) const override { os << tag; } | ||
}; | ||
|
||
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
// Forwarding listener to guard against unsupported erasures of non-descendant | ||
// ops/blocks. Because we use walk-based pattern application, erasing the | ||
// op/block from the *next* iteration (e.g., a user of the visited op) is not | ||
// valid. Note that this is only used with expensive pattern API checks. | ||
struct ErasedOpsListener final : RewriterBase::ForwardingListener { | ||
using RewriterBase::ForwardingListener::ForwardingListener; | ||
|
||
void notifyOperationErased(Operation *op) override { | ||
checkErasure(op); | ||
ForwardingListener::notifyOperationErased(op); | ||
} | ||
|
||
void notifyBlockErased(Block *block) override { | ||
checkErasure(block->getParentOp()); | ||
ForwardingListener::notifyBlockErased(block); | ||
} | ||
|
||
void checkErasure(Operation *op) const { | ||
Operation *ancestorOp = op; | ||
while (ancestorOp && ancestorOp != visitedOp) | ||
ancestorOp = ancestorOp->getParentOp(); | ||
|
||
if (ancestorOp != visitedOp) | ||
llvm::report_fatal_error( | ||
"unsupported erasure in WalkPatternRewriter; " | ||
"erasure is only supported for matched ops and their descendants"); | ||
} | ||
|
||
Operation *visitedOp = nullptr; | ||
}; | ||
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
} // namespace | ||
|
||
void walkAndApplyPatterns(Operation *op, | ||
const FrozenRewritePatternSet &patterns, | ||
RewriterBase::Listener *listener) { | ||
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
if (failed(verify(op))) | ||
llvm::report_fatal_error("walk pattern rewriter input IR failed to verify"); | ||
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
|
||
MLIRContext *ctx = op->getContext(); | ||
PatternRewriter rewriter(ctx); | ||
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
ErasedOpsListener erasedListener(listener); | ||
rewriter.setListener(&erasedListener); | ||
#else | ||
rewriter.setListener(listener); | ||
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
|
||
PatternApplicator applicator(patterns); | ||
applicator.applyDefaultCostModel(); | ||
|
||
ctx->executeAction<WalkAndApplyPatternsAction>( | ||
[&] { | ||
for (Region ®ion : op->getRegions()) { | ||
region.walk([&](Operation *visitedOp) { | ||
LLVM_DEBUG(llvm::dbgs() << "Visiting op: "; visitedOp->print( | ||
llvm::dbgs(), OpPrintingFlags().skipRegions()); | ||
llvm::dbgs() << "\n";); | ||
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
erasedListener.visitedOp = visitedOp; | ||
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
if (succeeded(applicator.matchAndRewrite(visitedOp, rewriter))) { | ||
LLVM_DEBUG(llvm::dbgs() << "\tOp matched and rewritten\n";); | ||
} | ||
}); | ||
} | ||
}, | ||
{op}); | ||
|
||
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
if (failed(verify(op))) | ||
llvm::report_fatal_error( | ||
"walk pattern rewriter result IR failed to verify"); | ||
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS | ||
} | ||
|
||
} // namespace mlir |
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.