1 //===- PatternApplicator.cpp - Pattern Application Engine -------*- C++ -*-===//
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 // This file implements an applicator that applies pattern rewrites based upon a
10 // user defined cost model.
12 //===----------------------------------------------------------------------===//
14 #include "mlir/Rewrite/PatternApplicator.h"
16 #include "llvm/Support/Debug.h"
18 #define DEBUG_TYPE "pattern-application"
21 using namespace mlir::detail
;
23 PatternApplicator::PatternApplicator(
24 const FrozenRewritePatternSet
&frozenPatternList
)
25 : frozenPatternList(frozenPatternList
) {
26 if (const PDLByteCode
*bytecode
= frozenPatternList
.getPDLByteCode()) {
27 mutableByteCodeState
= std::make_unique
<PDLByteCodeMutableState
>();
28 bytecode
->initializeMutableState(*mutableByteCodeState
);
31 PatternApplicator::~PatternApplicator() = default;
34 /// Log a message for a pattern that is impossible to match.
35 static void logImpossibleToMatch(const Pattern
&pattern
) {
36 llvm::dbgs() << "Ignoring pattern '" << pattern
.getRootKind()
37 << "' because it is impossible to match or cannot lead "
38 "to legal IR (by cost model)\n";
41 /// Log IR after pattern application.
42 static Operation
*getDumpRootOp(Operation
*op
) {
43 return op
->getParentWithTrait
<mlir::OpTrait::IsIsolatedFromAbove
>();
45 static void logSucessfulPatternApplication(Operation
*op
) {
46 llvm::dbgs() << "// *** IR Dump After Pattern Application ***\n";
48 llvm::dbgs() << "\n\n";
52 void PatternApplicator::applyCostModel(CostModel model
) {
53 // Apply the cost model to the bytecode patterns first, and then the native
55 if (const PDLByteCode
*bytecode
= frozenPatternList
.getPDLByteCode()) {
56 for (const auto &it
: llvm::enumerate(bytecode
->getPatterns()))
57 mutableByteCodeState
->updatePatternBenefit(it
.index(), model(it
.value()));
60 // Copy over the patterns so that we can sort by benefit based on the cost
61 // model. Patterns that are already impossible to match are ignored.
63 for (const auto &it
: frozenPatternList
.getOpSpecificNativePatterns()) {
64 for (const RewritePattern
*pattern
: it
.second
) {
65 if (pattern
->getBenefit().isImpossibleToMatch())
66 LLVM_DEBUG(logImpossibleToMatch(*pattern
));
68 patterns
[it
.first
].push_back(pattern
);
71 anyOpPatterns
.clear();
72 for (const RewritePattern
&pattern
:
73 frozenPatternList
.getMatchAnyOpNativePatterns()) {
74 if (pattern
.getBenefit().isImpossibleToMatch())
75 LLVM_DEBUG(logImpossibleToMatch(pattern
));
77 anyOpPatterns
.push_back(&pattern
);
80 // Sort the patterns using the provided cost model.
81 llvm::SmallDenseMap
<const Pattern
*, PatternBenefit
> benefits
;
82 auto cmp
= [&benefits
](const Pattern
*lhs
, const Pattern
*rhs
) {
83 return benefits
[lhs
] > benefits
[rhs
];
85 auto processPatternList
= [&](SmallVectorImpl
<const RewritePattern
*> &list
) {
86 // Special case for one pattern in the list, which is the most common case.
87 if (list
.size() == 1) {
88 if (model(*list
.front()).isImpossibleToMatch()) {
89 LLVM_DEBUG(logImpossibleToMatch(*list
.front()));
95 // Collect the dynamic benefits for the current pattern list.
97 for (const Pattern
*pat
: list
)
98 benefits
.try_emplace(pat
, model(*pat
));
100 // Sort patterns with highest benefit first, and remove those that are
101 // impossible to match.
102 std::stable_sort(list
.begin(), list
.end(), cmp
);
103 while (!list
.empty() && benefits
[list
.back()].isImpossibleToMatch()) {
104 LLVM_DEBUG(logImpossibleToMatch(*list
.back()));
108 for (auto &it
: patterns
)
109 processPatternList(it
.second
);
110 processPatternList(anyOpPatterns
);
113 void PatternApplicator::walkAllPatterns(
114 function_ref
<void(const Pattern
&)> walk
) {
115 for (const auto &it
: frozenPatternList
.getOpSpecificNativePatterns())
116 for (const auto &pattern
: it
.second
)
118 for (const Pattern
&it
: frozenPatternList
.getMatchAnyOpNativePatterns())
120 if (const PDLByteCode
*bytecode
= frozenPatternList
.getPDLByteCode()) {
121 for (const Pattern
&it
: bytecode
->getPatterns())
126 LogicalResult
PatternApplicator::matchAndRewrite(
127 Operation
*op
, PatternRewriter
&rewriter
,
128 function_ref
<bool(const Pattern
&)> canApply
,
129 function_ref
<void(const Pattern
&)> onFailure
,
130 function_ref
<LogicalResult(const Pattern
&)> onSuccess
) {
131 // Before checking native patterns, first match against the bytecode. This
132 // won't automatically perform any rewrites so there is no need to worry about
134 SmallVector
<PDLByteCode::MatchResult
, 4> pdlMatches
;
135 const PDLByteCode
*bytecode
= frozenPatternList
.getPDLByteCode();
137 bytecode
->match(op
, rewriter
, pdlMatches
, *mutableByteCodeState
);
139 // Check to see if there are patterns matching this specific operation type.
140 MutableArrayRef
<const RewritePattern
*> opPatterns
;
141 auto patternIt
= patterns
.find(op
->getName());
142 if (patternIt
!= patterns
.end())
143 opPatterns
= patternIt
->second
;
145 // Process the patterns for that match the specific operation type, and any
146 // operation type in an interleaved fashion.
147 unsigned opIt
= 0, opE
= opPatterns
.size();
148 unsigned anyIt
= 0, anyE
= anyOpPatterns
.size();
149 unsigned pdlIt
= 0, pdlE
= pdlMatches
.size();
150 LogicalResult result
= failure();
152 // Find the next pattern with the highest benefit.
153 const Pattern
*bestPattern
= nullptr;
154 unsigned *bestPatternIt
= &opIt
;
155 const PDLByteCode::MatchResult
*pdlMatch
= nullptr;
157 /// Operation specific patterns.
159 bestPattern
= opPatterns
[opIt
];
160 /// Operation agnostic patterns.
163 bestPattern
->getBenefit() < anyOpPatterns
[anyIt
]->getBenefit())) {
164 bestPatternIt
= &anyIt
;
165 bestPattern
= anyOpPatterns
[anyIt
];
168 if (pdlIt
< pdlE
&& (!bestPattern
|| bestPattern
->getBenefit() <
169 pdlMatches
[pdlIt
].benefit
)) {
170 bestPatternIt
= &pdlIt
;
171 pdlMatch
= &pdlMatches
[pdlIt
];
172 bestPattern
= pdlMatch
->pattern
;
177 // Update the pattern iterator on failure so that this pattern isn't
181 // Check that the pattern can be applied.
182 if (canApply
&& !canApply(*bestPattern
))
185 // Try to match and rewrite this pattern. The patterns are sorted by
186 // benefit, so if we match we can immediately rewrite. For PDL patterns, the
187 // match has already been performed, we just need to rewrite.
188 bool matched
= false;
189 op
->getContext()->executeAction
<ApplyPatternAction
>(
191 rewriter
.setInsertionPoint(op
);
193 // Operation `op` may be invalidated after applying the rewrite
195 Operation
*dumpRootOp
= getDumpRootOp(op
);
199 bytecode
->rewrite(rewriter
, *pdlMatch
, *mutableByteCodeState
);
201 LLVM_DEBUG(llvm::dbgs() << "Trying to match \""
202 << bestPattern
->getDebugName() << "\"\n");
204 const auto *pattern
=
205 static_cast<const RewritePattern
*>(bestPattern
);
206 result
= pattern
->matchAndRewrite(op
, rewriter
);
208 LLVM_DEBUG(llvm::dbgs()
209 << "\"" << bestPattern
->getDebugName() << "\" result "
210 << succeeded(result
) << "\n");
213 // Process the result of the pattern application.
214 if (succeeded(result
) && onSuccess
&& failed(onSuccess(*bestPattern
)))
216 if (succeeded(result
)) {
217 LLVM_DEBUG(logSucessfulPatternApplication(dumpRootOp
));
222 // Perform any necessary cleanups.
224 onFailure(*bestPattern
);
231 if (mutableByteCodeState
)
232 mutableByteCodeState
->cleanupAfterMatchAndRewrite();