#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "llvm/ADT/FunctionExtras.h"
+#include "llvm/Support/TypeName.h"
namespace mlir {
return contextAndHasBoundedRecursion.getPointer();
}
+ /// Return readable pattern name. Should only be used for debugging purposes.
+ /// Can be empty.
+ StringRef getDebugName() const { return debugName; }
+
+ /// Set readable pattern name. Should only be used for debugging purposes.
+ void setDebugName(StringRef name) { debugName = name; }
+
protected:
/// This class acts as a special tag that makes the desire to match "any"
/// operation type explicit. This helps to avoid unnecessary usages of this
/// A list of the potential operations that may be generated when rewriting
/// an op with this pattern.
SmallVector<OperationName, 2> generatedOps;
+
+ /// Readable pattern name. Can be empty.
+ StringRef debugName;
};
//===----------------------------------------------------------------------===//
struct FnPattern final : public OpRewritePattern<OpType> {
FnPattern(LogicalResult (*implFn)(OpType, PatternRewriter &rewriter),
MLIRContext *context)
- : OpRewritePattern<OpType>(context), implFn(implFn) {}
+ : OpRewritePattern<OpType>(context), implFn(implFn) {
+ setDebugName(llvm::getTypeName<FnPattern>());
+ }
LogicalResult matchAndRewrite(OpType op,
PatternRewriter &rewriter) const override {
template <typename T, typename... Args>
std::enable_if_t<std::is_base_of<RewritePattern, T>::value>
addImpl(Args &&... args) {
- nativePatterns.emplace_back(
- std::make_unique<T>(std::forward<Args>(args)...));
+ auto pattern = std::make_unique<T>(std::forward<Args>(args)...);
+
+ // Pattern can potentially set name in ctor. Preserve old name if present.
+ if (pattern->getDebugName().empty())
+ pattern->setDebugName(llvm::getTypeName<T>());
+
+ nativePatterns.emplace_back(std::move(pattern));
}
template <typename T, typename... Args>
std::enable_if_t<std::is_base_of<PDLPatternModule, T>::value>
result = success(!onSuccess || succeeded(onSuccess(*bestPattern)));
} else {
const auto *pattern = static_cast<const RewritePattern *>(bestPattern);
+
+ LLVM_DEBUG(llvm::dbgs()
+ << "Trying to match \"" << pattern->getDebugName() << "\"\n");
result = pattern->matchAndRewrite(op, rewriter);
+ LLVM_DEBUG(llvm::dbgs() << "\"" << pattern->getDebugName() << "\" result "
+ << succeeded(result) << "\n");
+
if (succeeded(result) && onSuccess && failed(onSuccess(*pattern)))
result = failure();
}