MLIR  19.0.0git
PatternMatch.h
Go to the documentation of this file.
1 //===- PatternMatch.h - PatternMatcher classes -------==---------*- C++ -*-===//
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 #ifndef MLIR_IR_PATTERNMATCH_H
10 #define MLIR_IR_PATTERNMATCH_H
11 
12 #include "mlir/IR/Builders.h"
13 #include "mlir/IR/BuiltinOps.h"
14 #include "llvm/ADT/FunctionExtras.h"
15 #include "llvm/Support/TypeName.h"
16 #include <optional>
17 
18 namespace mlir {
19 
20 class PatternRewriter;
21 
22 //===----------------------------------------------------------------------===//
23 // PatternBenefit class
24 //===----------------------------------------------------------------------===//
25 
26 /// This class represents the benefit of a pattern match in a unitless scheme
27 /// that ranges from 0 (very little benefit) to 65K. The most common unit to
28 /// use here is the "number of operations matched" by the pattern.
29 ///
30 /// This also has a sentinel representation that can be used for patterns that
31 /// fail to match.
32 ///
34  enum { ImpossibleToMatchSentinel = 65535 };
35 
36 public:
37  PatternBenefit() = default;
38  PatternBenefit(unsigned benefit);
39  PatternBenefit(const PatternBenefit &) = default;
41 
43  bool isImpossibleToMatch() const { return *this == impossibleToMatch(); }
44 
45  /// If the corresponding pattern can match, return its benefit. If the
46  // corresponding pattern isImpossibleToMatch() then this aborts.
47  unsigned short getBenefit() const;
48 
49  bool operator==(const PatternBenefit &rhs) const {
50  return representation == rhs.representation;
51  }
52  bool operator!=(const PatternBenefit &rhs) const { return !(*this == rhs); }
53  bool operator<(const PatternBenefit &rhs) const {
54  return representation < rhs.representation;
55  }
56  bool operator>(const PatternBenefit &rhs) const { return rhs < *this; }
57  bool operator<=(const PatternBenefit &rhs) const { return !(*this > rhs); }
58  bool operator>=(const PatternBenefit &rhs) const { return !(*this < rhs); }
59 
60 private:
61  unsigned short representation{ImpossibleToMatchSentinel};
62 };
63 
64 //===----------------------------------------------------------------------===//
65 // Pattern
66 //===----------------------------------------------------------------------===//
67 
68 /// This class contains all of the data related to a pattern, but does not
69 /// contain any methods or logic for the actual matching. This class is solely
70 /// used to interface with the metadata of a pattern, such as the benefit or
71 /// root operation.
72 class Pattern {
73  /// This enum represents the kind of value used to select the root operations
74  /// that match this pattern.
75  enum class RootKind {
76  /// The pattern root matches "any" operation.
77  Any,
78  /// The pattern root is matched using a concrete operation name.
80  /// The pattern root is matched using an interface ID.
81  InterfaceID,
82  /// The patter root is matched using a trait ID.
83  TraitID
84  };
85 
86 public:
87  /// Return a list of operations that may be generated when rewriting an
88  /// operation instance with this pattern.
89  ArrayRef<OperationName> getGeneratedOps() const { return generatedOps; }
90 
91  /// Return the root node that this pattern matches. Patterns that can match
92  /// multiple root types return std::nullopt.
93  std::optional<OperationName> getRootKind() const {
94  if (rootKind == RootKind::OperationName)
95  return OperationName::getFromOpaquePointer(rootValue);
96  return std::nullopt;
97  }
98 
99  /// Return the interface ID used to match the root operation of this pattern.
100  /// If the pattern does not use an interface ID for deciding the root match,
101  /// this returns std::nullopt.
102  std::optional<TypeID> getRootInterfaceID() const {
103  if (rootKind == RootKind::InterfaceID)
104  return TypeID::getFromOpaquePointer(rootValue);
105  return std::nullopt;
106  }
107 
108  /// Return the trait ID used to match the root operation of this pattern.
109  /// If the pattern does not use a trait ID for deciding the root match, this
110  /// returns std::nullopt.
111  std::optional<TypeID> getRootTraitID() const {
112  if (rootKind == RootKind::TraitID)
113  return TypeID::getFromOpaquePointer(rootValue);
114  return std::nullopt;
115  }
116 
117  /// Return the benefit (the inverse of "cost") of matching this pattern. The
118  /// benefit of a Pattern is always static - rewrites that may have dynamic
119  /// benefit can be instantiated multiple times (different Pattern instances)
120  /// for each benefit that they may return, and be guarded by different match
121  /// condition predicates.
122  PatternBenefit getBenefit() const { return benefit; }
123 
124  /// Returns true if this pattern is known to result in recursive application,
125  /// i.e. this pattern may generate IR that also matches this pattern, but is
126  /// known to bound the recursion. This signals to a rewrite driver that it is
127  /// safe to apply this pattern recursively to generated IR.
129  return contextAndHasBoundedRecursion.getInt();
130  }
131 
132  /// Return the MLIRContext used to create this pattern.
134  return contextAndHasBoundedRecursion.getPointer();
135  }
136 
137  /// Return a readable name for this pattern. This name should only be used for
138  /// debugging purposes, and may be empty.
139  StringRef getDebugName() const { return debugName; }
140 
141  /// Set the human readable debug name used for this pattern. This name will
142  /// only be used for debugging purposes.
143  void setDebugName(StringRef name) { debugName = name; }
144 
145  /// Return the set of debug labels attached to this pattern.
146  ArrayRef<StringRef> getDebugLabels() const { return debugLabels; }
147 
148  /// Add the provided debug labels to this pattern.
150  debugLabels.append(labels.begin(), labels.end());
151  }
152  void addDebugLabels(StringRef label) { debugLabels.push_back(label); }
153 
154 protected:
155  /// This class acts as a special tag that makes the desire to match "any"
156  /// operation type explicit. This helps to avoid unnecessary usages of this
157  /// feature, and ensures that the user is making a conscious decision.
158  struct MatchAnyOpTypeTag {};
159  /// This class acts as a special tag that makes the desire to match any
160  /// operation that implements a given interface explicit. This helps to avoid
161  /// unnecessary usages of this feature, and ensures that the user is making a
162  /// conscious decision.
164  /// This class acts as a special tag that makes the desire to match any
165  /// operation that implements a given trait explicit. This helps to avoid
166  /// unnecessary usages of this feature, and ensures that the user is making a
167  /// conscious decision.
169 
170  /// Construct a pattern with a certain benefit that matches the operation
171  /// with the given root name.
172  Pattern(StringRef rootName, PatternBenefit benefit, MLIRContext *context,
173  ArrayRef<StringRef> generatedNames = {});
174  /// Construct a pattern that may match any operation type. `generatedNames`
175  /// contains the names of operations that may be generated during a successful
176  /// rewrite. `MatchAnyOpTypeTag` is just a tag to ensure that the "match any"
177  /// behavior is what the user actually desired, `MatchAnyOpTypeTag()` should
178  /// always be supplied here.
179  Pattern(MatchAnyOpTypeTag tag, PatternBenefit benefit, MLIRContext *context,
180  ArrayRef<StringRef> generatedNames = {});
181  /// Construct a pattern that may match any operation that implements the
182  /// interface defined by the provided `interfaceID`. `generatedNames` contains
183  /// the names of operations that may be generated during a successful rewrite.
184  /// `MatchInterfaceOpTypeTag` is just a tag to ensure that the "match
185  /// interface" behavior is what the user actually desired,
186  /// `MatchInterfaceOpTypeTag()` should always be supplied here.
187  Pattern(MatchInterfaceOpTypeTag tag, TypeID interfaceID,
188  PatternBenefit benefit, MLIRContext *context,
189  ArrayRef<StringRef> generatedNames = {});
190  /// Construct a pattern that may match any operation that implements the
191  /// trait defined by the provided `traitID`. `generatedNames` contains the
192  /// names of operations that may be generated during a successful rewrite.
193  /// `MatchTraitOpTypeTag` is just a tag to ensure that the "match trait"
194  /// behavior is what the user actually desired, `MatchTraitOpTypeTag()` should
195  /// always be supplied here.
196  Pattern(MatchTraitOpTypeTag tag, TypeID traitID, PatternBenefit benefit,
197  MLIRContext *context, ArrayRef<StringRef> generatedNames = {});
198 
199  /// Set the flag detailing if this pattern has bounded rewrite recursion or
200  /// not.
201  void setHasBoundedRewriteRecursion(bool hasBoundedRecursionArg = true) {
202  contextAndHasBoundedRecursion.setInt(hasBoundedRecursionArg);
203  }
204 
205 private:
206  Pattern(const void *rootValue, RootKind rootKind,
207  ArrayRef<StringRef> generatedNames, PatternBenefit benefit,
208  MLIRContext *context);
209 
210  /// The value used to match the root operation of the pattern.
211  const void *rootValue;
212  RootKind rootKind;
213 
214  /// The expected benefit of matching this pattern.
215  const PatternBenefit benefit;
216 
217  /// The context this pattern was created from, and a boolean flag indicating
218  /// whether this pattern has bounded recursion or not.
219  llvm::PointerIntPair<MLIRContext *, 1, bool> contextAndHasBoundedRecursion;
220 
221  /// A list of the potential operations that may be generated when rewriting
222  /// an op with this pattern.
223  SmallVector<OperationName, 2> generatedOps;
224 
225  /// A readable name for this pattern. May be empty.
226  StringRef debugName;
227 
228  /// The set of debug labels attached to this pattern.
229  SmallVector<StringRef, 0> debugLabels;
230 };
231 
232 //===----------------------------------------------------------------------===//
233 // RewritePattern
234 //===----------------------------------------------------------------------===//
235 
236 /// RewritePattern is the common base class for all DAG to DAG replacements.
237 /// There are two possible usages of this class:
238 /// * Multi-step RewritePattern with "match" and "rewrite"
239 /// - By overloading the "match" and "rewrite" functions, the user can
240 /// separate the concerns of matching and rewriting.
241 /// * Single-step RewritePattern with "matchAndRewrite"
242 /// - By overloading the "matchAndRewrite" function, the user can perform
243 /// the rewrite in the same call as the match.
244 ///
245 class RewritePattern : public Pattern {
246 public:
247  virtual ~RewritePattern() = default;
248 
249  /// Rewrite the IR rooted at the specified operation with the result of
250  /// this pattern, generating any new operations with the specified
251  /// builder. If an unexpected error is encountered (an internal
252  /// compiler error), it is emitted through the normal MLIR diagnostic
253  /// hooks and the IR is left in a valid state.
254  virtual void rewrite(Operation *op, PatternRewriter &rewriter) const;
255 
256  /// Attempt to match against code rooted at the specified operation,
257  /// which is the same operation code as getRootKind().
258  virtual LogicalResult match(Operation *op) const;
259 
260  /// Attempt to match against code rooted at the specified operation,
261  /// which is the same operation code as getRootKind(). If successful, this
262  /// function will automatically perform the rewrite.
264  PatternRewriter &rewriter) const {
265  if (succeeded(match(op))) {
266  rewrite(op, rewriter);
267  return success();
268  }
269  return failure();
270  }
271 
272  /// This method provides a convenient interface for creating and initializing
273  /// derived rewrite patterns of the given type `T`.
274  template <typename T, typename... Args>
275  static std::unique_ptr<T> create(Args &&...args) {
276  std::unique_ptr<T> pattern =
277  std::make_unique<T>(std::forward<Args>(args)...);
278  initializePattern<T>(*pattern);
279 
280  // Set a default debug name if one wasn't provided.
281  if (pattern->getDebugName().empty())
282  pattern->setDebugName(llvm::getTypeName<T>());
283  return pattern;
284  }
285 
286 protected:
287  /// Inherit the base constructors from `Pattern`.
288  using Pattern::Pattern;
289 
290 private:
291  /// Trait to check if T provides a `getOperationName` method.
292  template <typename T, typename... Args>
293  using has_initialize = decltype(std::declval<T>().initialize());
294  template <typename T>
295  using detect_has_initialize = llvm::is_detected<has_initialize, T>;
296 
297  /// Initialize the derived pattern by calling its `initialize` method.
298  template <typename T>
299  static std::enable_if_t<detect_has_initialize<T>::value>
300  initializePattern(T &pattern) {
301  pattern.initialize();
302  }
303  /// Empty derived pattern initializer for patterns that do not have an
304  /// initialize method.
305  template <typename T>
306  static std::enable_if_t<!detect_has_initialize<T>::value>
307  initializePattern(T &) {}
308 
309  /// An anchor for the virtual table.
310  virtual void anchor();
311 };
312 
313 namespace detail {
314 /// OpOrInterfaceRewritePatternBase is a wrapper around RewritePattern that
315 /// allows for matching and rewriting against an instance of a derived operation
316 /// class or Interface.
317 template <typename SourceOp>
319  using RewritePattern::RewritePattern;
320 
321  /// Wrappers around the RewritePattern methods that pass the derived op type.
322  void rewrite(Operation *op, PatternRewriter &rewriter) const final {
323  rewrite(cast<SourceOp>(op), rewriter);
324  }
325  LogicalResult match(Operation *op) const final {
326  return match(cast<SourceOp>(op));
327  }
329  PatternRewriter &rewriter) const final {
330  return matchAndRewrite(cast<SourceOp>(op), rewriter);
331  }
332 
333  /// Rewrite and Match methods that operate on the SourceOp type. These must be
334  /// overridden by the derived pattern class.
335  virtual void rewrite(SourceOp op, PatternRewriter &rewriter) const {
336  llvm_unreachable("must override rewrite or matchAndRewrite");
337  }
338  virtual LogicalResult match(SourceOp op) const {
339  llvm_unreachable("must override match or matchAndRewrite");
340  }
341  virtual LogicalResult matchAndRewrite(SourceOp op,
342  PatternRewriter &rewriter) const {
343  if (succeeded(match(op))) {
344  rewrite(op, rewriter);
345  return success();
346  }
347  return failure();
348  }
349 };
350 } // namespace detail
351 
352 /// OpRewritePattern is a wrapper around RewritePattern that allows for
353 /// matching and rewriting against an instance of a derived operation class as
354 /// opposed to a raw Operation.
355 template <typename SourceOp>
357  : public detail::OpOrInterfaceRewritePatternBase<SourceOp> {
358  /// Patterns must specify the root operation name they match against, and can
359  /// also specify the benefit of the pattern matching and a list of generated
360  /// ops.
362  ArrayRef<StringRef> generatedNames = {})
364  SourceOp::getOperationName(), benefit, context, generatedNames) {}
365 };
366 
367 /// OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for
368 /// matching and rewriting against an instance of an operation interface instead
369 /// of a raw Operation.
370 template <typename SourceOp>
372  : public detail::OpOrInterfaceRewritePatternBase<SourceOp> {
374  : detail::OpOrInterfaceRewritePatternBase<SourceOp>(
375  Pattern::MatchInterfaceOpTypeTag(), SourceOp::getInterfaceID(),
376  benefit, context) {}
377 };
378 
379 /// OpTraitRewritePattern is a wrapper around RewritePattern that allows for
380 /// matching and rewriting against instances of an operation that possess a
381 /// given trait.
382 template <template <typename> class TraitType>
384 public:
387  benefit, context) {}
388 };
389 
390 //===----------------------------------------------------------------------===//
391 // RewriterBase
392 //===----------------------------------------------------------------------===//
393 
394 /// This class coordinates the application of a rewrite on a set of IR,
395 /// providing a way for clients to track mutations and create new operations.
396 /// This class serves as a common API for IR mutation between pattern rewrites
397 /// and non-pattern rewrites, and facilitates the development of shared
398 /// IR transformation utilities.
399 class RewriterBase : public OpBuilder {
400 public:
401  struct Listener : public OpBuilder::Listener {
403  : OpBuilder::Listener(ListenerBase::Kind::RewriterBaseListener) {}
404 
405  /// Notify the listener that the specified block is about to be erased.
406  /// At this point, the block has zero uses.
407  virtual void notifyBlockErased(Block *block) {}
408 
409  /// Notify the listener that the specified operation was modified in-place.
410  virtual void notifyOperationModified(Operation *op) {}
411 
412  /// Notify the listener that the specified operation is about to be replaced
413  /// with another operation. This is called before the uses of the old
414  /// operation have been changed.
415  ///
416  /// By default, this function calls the "operation replaced with values"
417  /// notification.
419  Operation *replacement) {
420  notifyOperationReplaced(op, replacement->getResults());
421  }
422 
423  /// Notify the listener that the specified operation is about to be replaced
424  /// with the a range of values, potentially produced by other operations.
425  /// This is called before the uses of the operation have been changed.
427  ValueRange replacement) {}
428 
429  /// Notify the listener that the specified operation is about to be erased.
430  /// At this point, the operation has zero uses.
431  ///
432  /// Note: This notification is not triggered when unlinking an operation.
433  virtual void notifyOperationErased(Operation *op) {}
434 
435  /// Notify the listener that the pattern failed to match the given
436  /// operation, and provide a callback to populate a diagnostic with the
437  /// reason why the failure occurred. This method allows for derived
438  /// listeners to optionally hook into the reason why a rewrite failed, and
439  /// display it to users.
440  virtual void
442  function_ref<void(Diagnostic &)> reasonCallback) {}
443 
444  static bool classof(const OpBuilder::Listener *base);
445  };
446 
447  /// A listener that forwards all notifications to another listener. This
448  /// struct can be used as a base to create listener chains, so that multiple
449  /// listeners can be notified of IR changes.
451  ForwardingListener(OpBuilder::Listener *listener) : listener(listener) {}
452 
453  void notifyOperationInserted(Operation *op, InsertPoint previous) override {
454  listener->notifyOperationInserted(op, previous);
455  }
456  void notifyBlockInserted(Block *block, Region *previous,
457  Region::iterator previousIt) override {
458  listener->notifyBlockInserted(block, previous, previousIt);
459  }
460  void notifyBlockErased(Block *block) override {
461  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
462  rewriteListener->notifyBlockErased(block);
463  }
464  void notifyOperationModified(Operation *op) override {
465  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
466  rewriteListener->notifyOperationModified(op);
467  }
468  void notifyOperationReplaced(Operation *op, Operation *newOp) override {
469  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
470  rewriteListener->notifyOperationReplaced(op, newOp);
471  }
473  ValueRange replacement) override {
474  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
475  rewriteListener->notifyOperationReplaced(op, replacement);
476  }
477  void notifyOperationErased(Operation *op) override {
478  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
479  rewriteListener->notifyOperationErased(op);
480  }
482  Location loc,
483  function_ref<void(Diagnostic &)> reasonCallback) override {
484  if (auto *rewriteListener = dyn_cast<RewriterBase::Listener>(listener))
485  rewriteListener->notifyMatchFailure(loc, reasonCallback);
486  }
487 
488  private:
489  OpBuilder::Listener *listener;
490  };
491 
492  /// Move the blocks that belong to "region" before the given position in
493  /// another region "parent". The two regions must be different. The caller
494  /// is responsible for creating or updating the operation transferring flow
495  /// of control to the region and passing it the correct block arguments.
496  void inlineRegionBefore(Region &region, Region &parent,
497  Region::iterator before);
498  void inlineRegionBefore(Region &region, Block *before);
499 
500  /// This method replaces the uses of the results of `op` with the values in
501  /// `newValues` when the provided `functor` returns true for a specific use.
502  /// The number of values in `newValues` is required to match the number of
503  /// results of `op`. `allUsesReplaced`, if non-null, is set to true if all of
504  /// the uses of `op` were replaced. Note that in some rewriters, the given
505  /// 'functor' may be stored beyond the lifetime of the rewrite being applied.
506  /// As such, the function should not capture by reference and instead use
507  /// value capture as necessary.
508  virtual void
509  replaceOpWithIf(Operation *op, ValueRange newValues, bool *allUsesReplaced,
510  llvm::unique_function<bool(OpOperand &) const> functor);
511  void replaceOpWithIf(Operation *op, ValueRange newValues,
512  llvm::unique_function<bool(OpOperand &) const> functor) {
513  replaceOpWithIf(op, newValues, /*allUsesReplaced=*/nullptr,
514  std::move(functor));
515  }
516 
517  /// This method replaces the uses of the results of `op` with the values in
518  /// `newValues` when a use is nested within the given `block`. The number of
519  /// values in `newValues` is required to match the number of results of `op`.
520  /// If all uses of this operation are replaced, the operation is erased.
521  void replaceOpWithinBlock(Operation *op, ValueRange newValues, Block *block,
522  bool *allUsesReplaced = nullptr);
523 
524  /// This method replaces the results of the operation with the specified list
525  /// of values. The number of provided values must match the number of results
526  /// of the operation. The replaced op is erased.
527  virtual void replaceOp(Operation *op, ValueRange newValues);
528 
529  /// This method replaces the results of the operation with the specified
530  /// new op (replacement). The number of results of the two operations must
531  /// match. The replaced op is erased.
532  virtual void replaceOp(Operation *op, Operation *newOp);
533 
534  /// Replaces the result op with a new op that is created without verification.
535  /// The result values of the two ops must be the same types.
536  template <typename OpTy, typename... Args>
537  OpTy replaceOpWithNewOp(Operation *op, Args &&...args) {
538  auto newOp = create<OpTy>(op->getLoc(), std::forward<Args>(args)...);
539  replaceOp(op, newOp.getOperation());
540  return newOp;
541  }
542 
543  /// This method erases an operation that is known to have no uses.
544  virtual void eraseOp(Operation *op);
545 
546  /// This method erases all operations in a block.
547  virtual void eraseBlock(Block *block);
548 
549  /// Inline the operations of block 'source' into block 'dest' before the given
550  /// position. The source block will be deleted and must have no uses.
551  /// 'argValues' is used to replace the block arguments of 'source'.
552  ///
553  /// If the source block is inserted at the end of the dest block, the dest
554  /// block must have no successors. Similarly, if the source block is inserted
555  /// somewhere in the middle (or beginning) of the dest block, the source block
556  /// must have no successors. Otherwise, the resulting IR would have
557  /// unreachable operations.
558  virtual void inlineBlockBefore(Block *source, Block *dest,
559  Block::iterator before,
560  ValueRange argValues = std::nullopt);
561 
562  /// Inline the operations of block 'source' before the operation 'op'. The
563  /// source block will be deleted and must have no uses. 'argValues' is used to
564  /// replace the block arguments of 'source'
565  ///
566  /// The source block must have no successors. Otherwise, the resulting IR
567  /// would have unreachable operations.
568  void inlineBlockBefore(Block *source, Operation *op,
569  ValueRange argValues = std::nullopt);
570 
571  /// Inline the operations of block 'source' into the end of block 'dest'. The
572  /// source block will be deleted and must have no uses. 'argValues' is used to
573  /// replace the block arguments of 'source'
574  ///
575  /// The dest block must have no successors. Otherwise, the resulting IR would
576  /// have unreachable operation.
577  void mergeBlocks(Block *source, Block *dest,
578  ValueRange argValues = std::nullopt);
579 
580  /// Split the operations starting at "before" (inclusive) out of the given
581  /// block into a new block, and return it.
582  virtual Block *splitBlock(Block *block, Block::iterator before);
583 
584  /// Unlink this operation from its current block and insert it right before
585  /// `existingOp` which may be in the same or another block in the same
586  /// function.
587  void moveOpBefore(Operation *op, Operation *existingOp);
588 
589  /// Unlink this operation from its current block and insert it right before
590  /// `iterator` in the specified block.
591  void moveOpBefore(Operation *op, Block *block, Block::iterator iterator);
592 
593  /// Unlink this operation from its current block and insert it right after
594  /// `existingOp` which may be in the same or another block in the same
595  /// function.
596  void moveOpAfter(Operation *op, Operation *existingOp);
597 
598  /// Unlink this operation from its current block and insert it right after
599  /// `iterator` in the specified block.
600  void moveOpAfter(Operation *op, Block *block, Block::iterator iterator);
601 
602  /// Unlink this block and insert it right before `existingBlock`.
603  void moveBlockBefore(Block *block, Block *anotherBlock);
604 
605  /// Unlink this block and insert it right before the location that the given
606  /// iterator points to in the given region.
607  void moveBlockBefore(Block *block, Region *region, Region::iterator iterator);
608 
609  /// This method is used to notify the rewriter that an in-place operation
610  /// modification is about to happen. A call to this function *must* be
611  /// followed by a call to either `finalizeOpModification` or
612  /// `cancelOpModification`. This is a minor efficiency win (it avoids creating
613  /// a new operation and removing the old one) but also often allows simpler
614  /// code in the client.
615  virtual void startOpModification(Operation *op) {}
616 
617  /// This method is used to signal the end of an in-place modification of the
618  /// given operation. This can only be called on operations that were provided
619  /// to a call to `startOpModification`.
620  virtual void finalizeOpModification(Operation *op);
621 
622  /// This method cancels a pending in-place modification. This can only be
623  /// called on operations that were provided to a call to
624  /// `startOpModification`.
625  virtual void cancelOpModification(Operation *op) {}
626 
627  /// This method is a utility wrapper around an in-place modification of an
628  /// operation. It wraps calls to `startOpModification` and
629  /// `finalizeOpModification` around the given callable.
630  template <typename CallableT>
631  void modifyOpInPlace(Operation *root, CallableT &&callable) {
632  startOpModification(root);
633  callable();
635  }
636 
637  /// Find uses of `from` and replace them with `to`. It also marks every
638  /// modified uses and notifies the rewriter that an in-place operation
639  /// modification is about to happen.
640  void replaceAllUsesWith(Value from, Value to) {
641  return replaceAllUsesWith(from.getImpl(), to);
642  }
643  template <typename OperandType, typename ValueT>
645  for (OperandType &operand : llvm::make_early_inc_range(from->getUses())) {
646  Operation *op = operand.getOwner();
647  modifyOpInPlace(op, [&]() { operand.set(to); });
648  }
649  }
651  assert(from.size() == to.size() && "incorrect number of replacements");
652  for (auto it : llvm::zip(from, to))
653  replaceAllUsesWith(std::get<0>(it), std::get<1>(it));
654  }
655 
656  /// Find uses of `from` and replace them with `to` if the `functor` returns
657  /// true. It also marks every modified uses and notifies the rewriter that an
658  /// in-place operation modification is about to happen.
659  void replaceUsesWithIf(Value from, Value to,
660  function_ref<bool(OpOperand &)> functor);
662  function_ref<bool(OpOperand &)> functor) {
663  assert(from.size() == to.size() && "incorrect number of replacements");
664  for (auto it : llvm::zip(from, to))
665  replaceUsesWithIf(std::get<0>(it), std::get<1>(it), functor);
666  }
667 
668  /// Find uses of `from` and replace them with `to` except if the user is
669  /// `exceptedUser`. It also marks every modified uses and notifies the
670  /// rewriter that an in-place operation modification is about to happen.
671  void replaceAllUsesExcept(Value from, Value to, Operation *exceptedUser) {
672  return replaceUsesWithIf(from, to, [&](OpOperand &use) {
673  Operation *user = use.getOwner();
674  return user != exceptedUser;
675  });
676  }
677 
678  /// Used to notify the rewriter that the IR failed to be rewritten because of
679  /// a match failure, and provide a callback to populate a diagnostic with the
680  /// reason why the failure occurred. This method allows for derived rewriters
681  /// to optionally hook into the reason why a rewrite failed, and display it to
682  /// users.
683  template <typename CallbackT>
684  std::enable_if_t<!std::is_convertible<CallbackT, Twine>::value, LogicalResult>
685  notifyMatchFailure(Location loc, CallbackT &&reasonCallback) {
686  if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
687  rewriteListener->notifyMatchFailure(
688  loc, function_ref<void(Diagnostic &)>(reasonCallback));
689  return failure();
690  }
691  template <typename CallbackT>
692  std::enable_if_t<!std::is_convertible<CallbackT, Twine>::value, LogicalResult>
693  notifyMatchFailure(Operation *op, CallbackT &&reasonCallback) {
694  if (auto *rewriteListener = dyn_cast_if_present<Listener>(listener))
695  rewriteListener->notifyMatchFailure(
696  op->getLoc(), function_ref<void(Diagnostic &)>(reasonCallback));
697  return failure();
698  }
699  template <typename ArgT>
700  LogicalResult notifyMatchFailure(ArgT &&arg, const Twine &msg) {
701  return notifyMatchFailure(std::forward<ArgT>(arg),
702  [&](Diagnostic &diag) { diag << msg; });
703  }
704  template <typename ArgT>
705  LogicalResult notifyMatchFailure(ArgT &&arg, const char *msg) {
706  return notifyMatchFailure(std::forward<ArgT>(arg), Twine(msg));
707  }
708 
709 protected:
710  /// Initialize the builder.
711  explicit RewriterBase(MLIRContext *ctx,
712  OpBuilder::Listener *listener = nullptr)
713  : OpBuilder(ctx, listener) {}
714  explicit RewriterBase(const OpBuilder &otherBuilder)
715  : OpBuilder(otherBuilder) {}
716  virtual ~RewriterBase();
717 
718 private:
719  void operator=(const RewriterBase &) = delete;
720  RewriterBase(const RewriterBase &) = delete;
721 };
722 
723 //===----------------------------------------------------------------------===//
724 // IRRewriter
725 //===----------------------------------------------------------------------===//
726 
727 /// This class coordinates rewriting a piece of IR outside of a pattern rewrite,
728 /// providing a way to keep track of the mutations made to the IR. This class
729 /// should only be used in situations where another `RewriterBase` instance,
730 /// such as a `PatternRewriter`, is not available.
731 class IRRewriter : public RewriterBase {
732 public:
734  : RewriterBase(ctx, listener) {}
735  explicit IRRewriter(const OpBuilder &builder) : RewriterBase(builder) {}
736 };
737 
738 //===----------------------------------------------------------------------===//
739 // PatternRewriter
740 //===----------------------------------------------------------------------===//
741 
742 /// A special type of `RewriterBase` that coordinates the application of a
743 /// rewrite pattern on the current IR being matched, providing a way to keep
744 /// track of any mutations made. This class should be used to perform all
745 /// necessary IR mutations within a rewrite pattern, as the pattern driver may
746 /// be tracking various state that would be invalidated when a mutation takes
747 /// place.
749 public:
751 
752  /// A hook used to indicate if the pattern rewriter can recover from failure
753  /// during the rewrite stage of a pattern. For example, if the pattern
754  /// rewriter supports rollback, it may progress smoothly even if IR was
755  /// changed during the rewrite.
756  virtual bool canRecoverFromRewriteFailure() const { return false; }
757 };
758 
759 } // namespace mlir
760 
761 // Optionally expose PDL pattern matching methods.
762 #include "PDLPatternMatch.h.inc"
763 
764 namespace mlir {
765 
766 //===----------------------------------------------------------------------===//
767 // RewritePatternSet
768 //===----------------------------------------------------------------------===//
769 
771  using NativePatternListT = std::vector<std::unique_ptr<RewritePattern>>;
772 
773 public:
774  RewritePatternSet(MLIRContext *context) : context(context) {}
775 
776  /// Construct a RewritePatternSet populated with the given pattern.
778  std::unique_ptr<RewritePattern> pattern)
779  : context(context) {
780  nativePatterns.emplace_back(std::move(pattern));
781  }
782  RewritePatternSet(PDLPatternModule &&pattern)
783  : context(pattern.getContext()), pdlPatterns(std::move(pattern)) {}
784 
785  MLIRContext *getContext() const { return context; }
786 
787  /// Return the native patterns held in this list.
788  NativePatternListT &getNativePatterns() { return nativePatterns; }
789 
790  /// Return the PDL patterns held in this list.
791  PDLPatternModule &getPDLPatterns() { return pdlPatterns; }
792 
793  /// Clear out all of the held patterns in this list.
794  void clear() {
795  nativePatterns.clear();
796  pdlPatterns.clear();
797  }
798 
799  //===--------------------------------------------------------------------===//
800  // 'add' methods for adding patterns to the set.
801  //===--------------------------------------------------------------------===//
802 
803  /// Add an instance of each of the pattern types 'Ts' to the pattern list with
804  /// the given arguments. Return a reference to `this` for chaining insertions.
805  /// Note: ConstructorArg is necessary here to separate the two variadic lists.
806  template <typename... Ts, typename ConstructorArg,
807  typename... ConstructorArgs,
808  typename = std::enable_if_t<sizeof...(Ts) != 0>>
809  RewritePatternSet &add(ConstructorArg &&arg, ConstructorArgs &&...args) {
810  // The following expands a call to emplace_back for each of the pattern
811  // types 'Ts'.
812  (addImpl<Ts>(/*debugLabels=*/std::nullopt,
813  std::forward<ConstructorArg>(arg),
814  std::forward<ConstructorArgs>(args)...),
815  ...);
816  return *this;
817  }
818  /// An overload of the above `add` method that allows for attaching a set
819  /// of debug labels to the attached patterns. This is useful for labeling
820  /// groups of patterns that may be shared between multiple different
821  /// passes/users.
822  template <typename... Ts, typename ConstructorArg,
823  typename... ConstructorArgs,
824  typename = std::enable_if_t<sizeof...(Ts) != 0>>
826  ConstructorArg &&arg,
827  ConstructorArgs &&...args) {
828  // The following expands a call to emplace_back for each of the pattern
829  // types 'Ts'.
830  (addImpl<Ts>(debugLabels, arg, args...), ...);
831  return *this;
832  }
833 
834  /// Add an instance of each of the pattern types 'Ts'. Return a reference to
835  /// `this` for chaining insertions.
836  template <typename... Ts>
838  (addImpl<Ts>(), ...);
839  return *this;
840  }
841 
842  /// Add the given native pattern to the pattern list. Return a reference to
843  /// `this` for chaining insertions.
844  RewritePatternSet &add(std::unique_ptr<RewritePattern> pattern) {
845  nativePatterns.emplace_back(std::move(pattern));
846  return *this;
847  }
848 
849  /// Add the given PDL pattern to the pattern list. Return a reference to
850  /// `this` for chaining insertions.
851  RewritePatternSet &add(PDLPatternModule &&pattern) {
852  pdlPatterns.mergeIn(std::move(pattern));
853  return *this;
854  }
855 
856  // Add a matchAndRewrite style pattern represented as a C function pointer.
857  template <typename OpType>
859  add(LogicalResult (*implFn)(OpType, PatternRewriter &rewriter),
860  PatternBenefit benefit = 1, ArrayRef<StringRef> generatedNames = {}) {
861  struct FnPattern final : public OpRewritePattern<OpType> {
862  FnPattern(LogicalResult (*implFn)(OpType, PatternRewriter &rewriter),
863  MLIRContext *context, PatternBenefit benefit,
864  ArrayRef<StringRef> generatedNames)
865  : OpRewritePattern<OpType>(context, benefit, generatedNames),
866  implFn(implFn) {}
867 
868  LogicalResult matchAndRewrite(OpType op,
869  PatternRewriter &rewriter) const override {
870  return implFn(op, rewriter);
871  }
872 
873  private:
874  LogicalResult (*implFn)(OpType, PatternRewriter &rewriter);
875  };
876  add(std::make_unique<FnPattern>(std::move(implFn), getContext(), benefit,
877  generatedNames));
878  return *this;
879  }
880 
881  //===--------------------------------------------------------------------===//
882  // Pattern Insertion
883  //===--------------------------------------------------------------------===//
884 
885  // TODO: These are soft deprecated in favor of the 'add' methods above.
886 
887  /// Add an instance of each of the pattern types 'Ts' to the pattern list with
888  /// the given arguments. Return a reference to `this` for chaining insertions.
889  /// Note: ConstructorArg is necessary here to separate the two variadic lists.
890  template <typename... Ts, typename ConstructorArg,
891  typename... ConstructorArgs,
892  typename = std::enable_if_t<sizeof...(Ts) != 0>>
893  RewritePatternSet &insert(ConstructorArg &&arg, ConstructorArgs &&...args) {
894  // The following expands a call to emplace_back for each of the pattern
895  // types 'Ts'.
896  (addImpl<Ts>(/*debugLabels=*/std::nullopt, arg, args...), ...);
897  return *this;
898  }
899 
900  /// Add an instance of each of the pattern types 'Ts'. Return a reference to
901  /// `this` for chaining insertions.
902  template <typename... Ts>
904  (addImpl<Ts>(), ...);
905  return *this;
906  }
907 
908  /// Add the given native pattern to the pattern list. Return a reference to
909  /// `this` for chaining insertions.
910  RewritePatternSet &insert(std::unique_ptr<RewritePattern> pattern) {
911  nativePatterns.emplace_back(std::move(pattern));
912  return *this;
913  }
914 
915  /// Add the given PDL pattern to the pattern list. Return a reference to
916  /// `this` for chaining insertions.
917  RewritePatternSet &insert(PDLPatternModule &&pattern) {
918  pdlPatterns.mergeIn(std::move(pattern));
919  return *this;
920  }
921 
922  // Add a matchAndRewrite style pattern represented as a C function pointer.
923  template <typename OpType>
925  insert(LogicalResult (*implFn)(OpType, PatternRewriter &rewriter)) {
926  struct FnPattern final : public OpRewritePattern<OpType> {
927  FnPattern(LogicalResult (*implFn)(OpType, PatternRewriter &rewriter),
928  MLIRContext *context)
929  : OpRewritePattern<OpType>(context), implFn(implFn) {
930  this->setDebugName(llvm::getTypeName<FnPattern>());
931  }
932 
933  LogicalResult matchAndRewrite(OpType op,
934  PatternRewriter &rewriter) const override {
935  return implFn(op, rewriter);
936  }
937 
938  private:
939  LogicalResult (*implFn)(OpType, PatternRewriter &rewriter);
940  };
941  add(std::make_unique<FnPattern>(std::move(implFn), getContext()));
942  return *this;
943  }
944 
945 private:
946  /// Add an instance of the pattern type 'T'. Return a reference to `this` for
947  /// chaining insertions.
948  template <typename T, typename... Args>
949  std::enable_if_t<std::is_base_of<RewritePattern, T>::value>
950  addImpl(ArrayRef<StringRef> debugLabels, Args &&...args) {
951  std::unique_ptr<T> pattern =
952  RewritePattern::create<T>(std::forward<Args>(args)...);
953  pattern->addDebugLabels(debugLabels);
954  nativePatterns.emplace_back(std::move(pattern));
955  }
956 
957  template <typename T, typename... Args>
958  std::enable_if_t<std::is_base_of<PDLPatternModule, T>::value>
959  addImpl(ArrayRef<StringRef> debugLabels, Args &&...args) {
960  // TODO: Add the provided labels to the PDL pattern when PDL supports
961  // labels.
962  pdlPatterns.mergeIn(T(std::forward<Args>(args)...));
963  }
964 
965  MLIRContext *const context;
966  NativePatternListT nativePatterns;
967 
968  // Patterns expressed with PDL. This will compile to a stub class when PDL is
969  // not enabled.
970  PDLPatternModule pdlPatterns;
971 };
972 
973 } // namespace mlir
974 
975 #endif // MLIR_IR_PATTERNMATCH_H
static std::string diag(const llvm::Value &value)
Block represents an ordered list of Operations.
Definition: Block.h:30
OpListType::iterator iterator
Definition: Block.h:137
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
Definition: Diagnostics.h:156
This class represents a single IR object that contains a use list.
Definition: UseDefLists.h:195
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Definition: UseDefLists.h:253
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
Definition: PatternMatch.h:731
IRRewriter(MLIRContext *ctx, OpBuilder::Listener *listener=nullptr)
Definition: PatternMatch.h:733
IRRewriter(const OpBuilder &builder)
Definition: PatternMatch.h:735
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class represents a saved insertion point.
Definition: Builders.h:329
This class helps build Operations.
Definition: Builders.h:209
Listener * listener
The optional listener for events of this builder.
Definition: Builders.h:601
This class represents an operand of an operation.
Definition: Value.h:263
OpTraitRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting again...
Definition: PatternMatch.h:383
OpTraitRewritePattern(MLIRContext *context, PatternBenefit benefit=1)
Definition: PatternMatch.h:385
static OperationName getFromOpaquePointer(const void *pointer)
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
result_range getResults()
Definition: Operation.h:410
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Definition: PatternMatch.h:33
PatternBenefit & operator=(const PatternBenefit &)=default
bool operator<(const PatternBenefit &rhs) const
Definition: PatternMatch.h:53
bool operator==(const PatternBenefit &rhs) const
Definition: PatternMatch.h:49
static PatternBenefit impossibleToMatch()
Definition: PatternMatch.h:42
bool operator>=(const PatternBenefit &rhs) const
Definition: PatternMatch.h:58
bool operator<=(const PatternBenefit &rhs) const
Definition: PatternMatch.h:57
PatternBenefit(const PatternBenefit &)=default
bool isImpossibleToMatch() const
Definition: PatternMatch.h:43
bool operator!=(const PatternBenefit &rhs) const
Definition: PatternMatch.h:52
PatternBenefit()=default
bool operator>(const PatternBenefit &rhs) const
Definition: PatternMatch.h:56
unsigned short getBenefit() const
If the corresponding pattern can match, return its benefit. If the.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:748
virtual bool canRecoverFromRewriteFailure() const
A hook used to indicate if the pattern rewriter can recover from failure during the rewrite stage of ...
Definition: PatternMatch.h:756
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
Definition: PatternMatch.h:72
std::optional< TypeID > getRootInterfaceID() const
Return the interface ID used to match the root operation of this pattern.
Definition: PatternMatch.h:102
Pattern(StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
Construct a pattern with a certain benefit that matches the operation with the given root name.
bool hasBoundedRewriteRecursion() const
Returns true if this pattern is known to result in recursive application, i.e.
Definition: PatternMatch.h:128
std::optional< OperationName > getRootKind() const
Return the root node that this pattern matches.
Definition: PatternMatch.h:93
MLIRContext * getContext() const
Return the MLIRContext used to create this pattern.
Definition: PatternMatch.h:133
ArrayRef< StringRef > getDebugLabels() const
Return the set of debug labels attached to this pattern.
Definition: PatternMatch.h:146
void setHasBoundedRewriteRecursion(bool hasBoundedRecursionArg=true)
Set the flag detailing if this pattern has bounded rewrite recursion or not.
Definition: PatternMatch.h:201
ArrayRef< OperationName > getGeneratedOps() const
Return a list of operations that may be generated when rewriting an operation instance with this patt...
Definition: PatternMatch.h:89
PatternBenefit getBenefit() const
Return the benefit (the inverse of "cost") of matching this pattern.
Definition: PatternMatch.h:122
std::optional< TypeID > getRootTraitID() const
Return the trait ID used to match the root operation of this pattern.
Definition: PatternMatch.h:111
void setDebugName(StringRef name)
Set the human readable debug name used for this pattern.
Definition: PatternMatch.h:143
void addDebugLabels(StringRef label)
Definition: PatternMatch.h:152
void addDebugLabels(ArrayRef< StringRef > labels)
Add the provided debug labels to this pattern.
Definition: PatternMatch.h:149
StringRef getDebugName() const
Return a readable name for this pattern.
Definition: PatternMatch.h:139
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
BlockListType::iterator iterator
Definition: Region.h:52
RewritePatternSet & add(PDLPatternModule &&pattern)
Add the given PDL pattern to the pattern list.
Definition: PatternMatch.h:851
RewritePatternSet & add(LogicalResult(*implFn)(OpType, PatternRewriter &rewriter), PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Definition: PatternMatch.h:859
NativePatternListT & getNativePatterns()
Return the native patterns held in this list.
Definition: PatternMatch.h:788
RewritePatternSet & add(std::unique_ptr< RewritePattern > pattern)
Add the given native pattern to the pattern list.
Definition: PatternMatch.h:844
RewritePatternSet(PDLPatternModule &&pattern)
Definition: PatternMatch.h:782
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:893
MLIRContext * getContext() const
Definition: PatternMatch.h:785
void clear()
Clear out all of the held patterns in this list.
Definition: PatternMatch.h:794
RewritePatternSet(MLIRContext *context)
Definition: PatternMatch.h:774
RewritePatternSet & add()
Add an instance of each of the pattern types 'Ts'.
Definition: PatternMatch.h:837
RewritePatternSet(MLIRContext *context, std::unique_ptr< RewritePattern > pattern)
Construct a RewritePatternSet populated with the given pattern.
Definition: PatternMatch.h:777
RewritePatternSet & insert(PDLPatternModule &&pattern)
Add the given PDL pattern to the pattern list.
Definition: PatternMatch.h:917
RewritePatternSet & insert(std::unique_ptr< RewritePattern > pattern)
Add the given native pattern to the pattern list.
Definition: PatternMatch.h:910
PDLPatternModule & getPDLPatterns()
Return the PDL patterns held in this list.
Definition: PatternMatch.h:791
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:809
RewritePatternSet & insert()
Add an instance of each of the pattern types 'Ts'.
Definition: PatternMatch.h:903
RewritePatternSet & addWithLabel(ArrayRef< StringRef > debugLabels, ConstructorArg &&arg, ConstructorArgs &&...args)
An overload of the above add method that allows for attaching a set of debug labels to the attached p...
Definition: PatternMatch.h:825
RewritePatternSet & insert(LogicalResult(*implFn)(OpType, PatternRewriter &rewriter))
Definition: PatternMatch.h:925
RewritePattern is the common base class for all DAG to DAG replacements.
Definition: PatternMatch.h:245
virtual LogicalResult match(Operation *op) const
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
static std::unique_ptr< T > create(Args &&...args)
This method provides a convenient interface for creating and initializing derived rewrite patterns of...
Definition: PatternMatch.h:275
virtual ~RewritePattern()=default
virtual LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
Definition: PatternMatch.h:263
virtual void rewrite(Operation *op, PatternRewriter &rewriter) const
Rewrite the IR rooted at the specified operation with the result of this pattern, generating any new ...
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:399
virtual void replaceOpWithIf(Operation *op, ValueRange newValues, bool *allUsesReplaced, llvm::unique_function< bool(OpOperand &) const > functor)
This method replaces the uses of the results of op with the values in newValues when the provided fun...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the rewriter that the IR failed to be rewritten because of a match failure,...
Definition: PatternMatch.h:685
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Operation *op, CallbackT &&reasonCallback)
Definition: PatternMatch.h:693
void replaceOpWithIf(Operation *op, ValueRange newValues, llvm::unique_function< bool(OpOperand &) const > functor)
Definition: PatternMatch.h:511
virtual void eraseBlock(Block *block)
This method erases all operations in a block.
LogicalResult notifyMatchFailure(ArgT &&arg, const Twine &msg)
Definition: PatternMatch.h:700
virtual Block * splitBlock(Block *block, Block::iterator before)
Split the operations starting at "before" (inclusive) out of the given block into a new block,...
virtual void replaceOp(Operation *op, ValueRange newValues)
This method replaces the results of the operation with the specified list of values.
RewriterBase(const OpBuilder &otherBuilder)
Definition: PatternMatch.h:714
void replaceAllUsesWith(ValueRange from, ValueRange to)
Definition: PatternMatch.h:650
void moveBlockBefore(Block *block, Block *anotherBlock)
Unlink this block and insert it right before existingBlock.
void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
Definition: PatternMatch.h:640
void mergeBlocks(Block *source, Block *dest, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into the end of block 'dest'.
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void replaceOpWithinBlock(Operation *op, ValueRange newValues, Block *block, bool *allUsesReplaced=nullptr)
This method replaces the uses of the results of op with the values in newValues when a use is nested ...
virtual void cancelOpModification(Operation *op)
This method cancels a pending in-place modification.
Definition: PatternMatch.h:625
void replaceAllUsesExcept(Value from, Value to, Operation *exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
Definition: PatternMatch.h:671
RewriterBase(MLIRContext *ctx, OpBuilder::Listener *listener=nullptr)
Initialize the builder.
Definition: PatternMatch.h:711
void moveOpBefore(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
LogicalResult notifyMatchFailure(ArgT &&arg, const char *msg)
Definition: PatternMatch.h:705
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Definition: PatternMatch.h:631
void moveOpAfter(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
void replaceUsesWithIf(Value from, Value to, function_ref< bool(OpOperand &)> functor)
Find uses of from and replace them with to if the functor returns true.
void inlineRegionBefore(Region &region, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
void replaceAllUsesWith(IRObjectWithUseList< OperandType > *from, ValueT &&to)
Definition: PatternMatch.h:644
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into block 'dest' before the given position.
void replaceUsesWithIf(ValueRange from, ValueRange to, function_ref< bool(OpOperand &)> functor)
Definition: PatternMatch.h:661
virtual void startOpModification(Operation *op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
Definition: PatternMatch.h:615
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
Definition: PatternMatch.h:537
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
static TypeID getFromOpaquePointer(const void *pointer)
Definition: TypeID.h:132
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:378
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
detail::ValueImpl * getImpl() const
Definition: Value.h:243
Operation * getOwner() const
Return the owner of this operand.
Definition: UseDefLists.h:38
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:68
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Base class for listeners.
Definition: Builders.h:266
Kind
The kind of listener.
Definition: Builders.h:268
This class represents a listener that may be used to hook into various actions within an OpBuilder.
Definition: Builders.h:287
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
Definition: Builders.h:310
virtual void notifyOperationInserted(Operation *op, InsertPoint previous)
Notify the listener that the specified operation was inserted.
Definition: Builders.h:300
OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting a...
Definition: PatternMatch.h:372
OpInterfaceRewritePattern(MLIRContext *context, PatternBenefit benefit=1)
Definition: PatternMatch.h:373
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:357
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...
Definition: PatternMatch.h:361
This class acts as a special tag that makes the desire to match "any" operation type explicit.
Definition: PatternMatch.h:158
This class acts as a special tag that makes the desire to match any operation that implements a given...
Definition: PatternMatch.h:163
This class acts as a special tag that makes the desire to match any operation that implements a given...
Definition: PatternMatch.h:168
A listener that forwards all notifications to another listener.
Definition: PatternMatch.h:450
void notifyMatchFailure(Location loc, function_ref< void(Diagnostic &)> reasonCallback) override
Notify the listener that the pattern failed to match the given operation, and provide a callback to p...
Definition: PatternMatch.h:481
void notifyOperationInserted(Operation *op, InsertPoint previous) override
Notify the listener that the specified operation was inserted.
Definition: PatternMatch.h:453
void notifyOperationModified(Operation *op) override
Notify the listener that the specified operation was modified in-place.
Definition: PatternMatch.h:464
void notifyOperationReplaced(Operation *op, Operation *newOp) override
Notify the listener that the specified operation is about to be replaced with another operation.
Definition: PatternMatch.h:468
void notifyOperationErased(Operation *op) override
Notify the listener that the specified operation is about to be erased.
Definition: PatternMatch.h:477
void notifyBlockErased(Block *block) override
Notify the listener that the specified block is about to be erased.
Definition: PatternMatch.h:460
ForwardingListener(OpBuilder::Listener *listener)
Definition: PatternMatch.h:451
void notifyOperationReplaced(Operation *op, ValueRange replacement) override
Notify the listener that the specified operation is about to be replaced with the a range of values,...
Definition: PatternMatch.h:472
void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt) override
Notify the listener that the specified block was inserted.
Definition: PatternMatch.h:456
virtual void notifyMatchFailure(Location loc, function_ref< void(Diagnostic &)> reasonCallback)
Notify the listener that the pattern failed to match the given operation, and provide a callback to p...
Definition: PatternMatch.h:441
virtual void notifyOperationModified(Operation *op)
Notify the listener that the specified operation was modified in-place.
Definition: PatternMatch.h:410
virtual void notifyOperationErased(Operation *op)
Notify the listener that the specified operation is about to be erased.
Definition: PatternMatch.h:433
virtual void notifyOperationReplaced(Operation *op, Operation *replacement)
Notify the listener that the specified operation is about to be replaced with another operation.
Definition: PatternMatch.h:418
virtual void notifyBlockErased(Block *block)
Notify the listener that the specified block is about to be erased.
Definition: PatternMatch.h:407
virtual void notifyOperationReplaced(Operation *op, ValueRange replacement)
Notify the listener that the specified operation is about to be replaced with the a range of values,...
Definition: PatternMatch.h:426
static bool classof(const OpBuilder::Listener *base)
OpOrInterfaceRewritePatternBase is a wrapper around RewritePattern that allows for matching and rewri...
Definition: PatternMatch.h:318
virtual void rewrite(SourceOp op, PatternRewriter &rewriter) const
Rewrite and Match methods that operate on the SourceOp type.
Definition: PatternMatch.h:335
void rewrite(Operation *op, PatternRewriter &rewriter) const final
Wrappers around the RewritePattern methods that pass the derived op type.
Definition: PatternMatch.h:322
virtual LogicalResult match(SourceOp op) const
Definition: PatternMatch.h:338
LogicalResult match(Operation *op) const final
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
Definition: PatternMatch.h:325
virtual LogicalResult matchAndRewrite(SourceOp op, PatternRewriter &rewriter) const
Definition: PatternMatch.h:341
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const final
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
Definition: PatternMatch.h:328