#include <memory>
namespace mlir {
+class ConversionPattern;
class DialectConversion;
-class DialectConversionPattern;
class MLIRContext;
class Module;
class RewritePattern;
}
// RangeOp creates a new range descriptor.
-class RangeOpConversion : public DialectConversionPattern {
+class RangeOpConversion : public ConversionPattern {
public:
explicit RangeOpConversion(MLIRContext *context)
- : DialectConversionPattern(linalg::RangeOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(linalg::RangeOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
}
};
-class ViewOpConversion : public DialectConversionPattern {
+class ViewOpConversion : public ConversionPattern {
public:
explicit ViewOpConversion(MLIRContext *context)
- : DialectConversionPattern(linalg::ViewOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(linalg::ViewOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
}
};
-class SliceOpConversion : public DialectConversionPattern {
+class SliceOpConversion : public ConversionPattern {
public:
explicit SliceOpConversion(MLIRContext *context)
- : DialectConversionPattern(linalg::SliceOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(linalg::SliceOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
// When converting the "some_consumer" operation, don't emit anything and
// effectively drop it.
-class DropConsumer : public DialectConversionPattern {
+class DropConsumer : public ConversionPattern {
public:
explicit DropConsumer(MLIRContext *context)
- : DialectConversionPattern("some_consumer", 1, context) {}
+ : ConversionPattern("some_consumer", 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
namespace {
// Common functionality for Linalg LoadOp and StoreOp conversion to the
// LLVM IR Dialect.
-template <typename Op>
-class LoadStoreOpConversion : public DialectConversionPattern {
+template <typename Op> class LoadStoreOpConversion : public ConversionPattern {
public:
explicit LoadStoreOpConversion(MLIRContext *context)
- : DialectConversionPattern(Op::getOperationName(), 1, context) {}
+ : ConversionPattern(Op::getOperationName(), 1, context) {}
using Base = LoadStoreOpConversion<Op>;
// Compute the pointer to an element of the buffer underlying the view given
/// Lower toy.mul to Linalg `matmul`.
///
-/// This class inherit from `DialectConversionPattern` and override `rewrite`,
+/// This class inherit from `ConversionPattern` and override `rewrite`,
/// similarly to the PatternRewriter introduced in the previous chapter.
/// It will be called by the DialectConversion framework (see `LateLowering`
/// class below).
-class MulOpConversion : public DialectConversionPattern {
+class MulOpConversion : public ConversionPattern {
public:
explicit MulOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::MulOp::getOperationName(), 1, context) {}
+ : ConversionPattern(toy::MulOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
/// Lower a toy.add to an affine loop nest.
///
-/// This class inherit from `DialectConversionPattern` and override `rewrite`,
+/// This class inherit from `ConversionPattern` and override `rewrite`,
/// similarly to the PatternRewriter introduced in the previous chapter.
/// It will be called by the DialectConversion framework (see `LateLowering`
/// class below).
-class AddOpConversion : public DialectConversionPattern {
+class AddOpConversion : public ConversionPattern {
public:
explicit AddOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::AddOp::getOperationName(), 1, context) {}
+ : ConversionPattern(toy::AddOp::getOperationName(), 1, context) {}
/// Lower the `op` by generating IR using the `rewriter` builder. The builder
/// is setup with a new function, the `operands` array has been populated with
/// Lowers `toy.print` to a loop nest calling `printf` on every individual
/// elements of the array.
-class PrintOpConversion : public DialectConversionPattern {
+class PrintOpConversion : public ConversionPattern {
public:
explicit PrintOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::PrintOp::getOperationName(), 1, context) {
- }
+ : ConversionPattern(toy::PrintOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
};
/// Lowers constant to a sequence of store in a buffer.
-class ConstantOpConversion : public DialectConversionPattern {
+class ConstantOpConversion : public ConversionPattern {
public:
explicit ConstantOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::ConstantOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(toy::ConstantOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
};
/// Lower transpose operation to an affine loop nest.
-class TransposeOpConversion : public DialectConversionPattern {
+class TransposeOpConversion : public ConversionPattern {
public:
explicit TransposeOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::TransposeOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(toy::TransposeOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
};
// Lower toy.return to standard return operation.
-class ReturnOpConversion : public DialectConversionPattern {
+class ReturnOpConversion : public ConversionPattern {
public:
explicit ReturnOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::ReturnOp::getOperationName(), 1,
- context) {}
+ : ConversionPattern(toy::ReturnOp::getOperationName(), 1, context) {}
void rewrite(Operation *op, ArrayRef<Value *> operands,
PatternRewriter &rewriter) const override {
are available directly in the type signature.
An operation conversion is defined as special pattern by inheriting from
-`mlir::DialectConversionPattern` and by reimplementing the matching and the
-rewriting functions:
+`mlir::ConversionPattern` and by reimplementing the matching and the rewriting
+functions:
```c++
-class ViewOpConversion : public DialectConversionPattern {
+class ViewOpConversion : public ConversionPattern {
public:
// A conversion constructor, may take arbtirary operands but must be able
// to obtain an MLIRContext from them to call the parent constructor.
}
```
-The `DialectConversionPattern` constructor takes, in addition to the context,
-the name of the main operation to be matched and the "benefit" of a match. These
-operands are intended to be useful for defining an optimization problem across
-multiple possible conversions but are currently ignored by the conversion
-framework.
+The `ConversionPattern` constructor takes, in addition to the context, the name
+of the main operation to be matched and the "benefit" of a match. These operands
+are intended to be useful for defining an optimization problem across multiple
+possible conversions but are currently ignored by the conversion framework.
```c++
ViewOpConversion::ViewOpConversion(MLIRContext *context)
- : DialectConversionPattern(linalg::ViewOp::getOperationName(), 1, context) {}
+ : ConversionPattern(linalg::ViewOp::getOperationName(), 1, context) {}
```
The matching function can be used, for example, to apply different conversions
protected:
// Produce a set of operation conversion patterns. This is called once per
// conversion.
- llvm::DenseSet<DialectConversionPattern *>
+ llvm::DenseSet<ConversionPattern *>
initConverter(MLIRContext *context) override {
allocator.Reset();
// Call a helper function provided by MLIR to build a set of operation
SmallVectorImpl<NamedAttributeList> &convertedArgAttrs) { /*...*/ }
// This gets called once to set up operation converters.
- llvm::DenseSet<DialectConversionPattern *>
+ llvm::DenseSet<ConversionPattern *>
initConverters(MLIRContext *context) override {
RewriteListBuilder<MulOpConversion, PrintOpConversion,
TransposeOpConversion>::build(allocator, context);
```c++
/// Lower a toy.add to an affine loop nest.
///
-/// This class inherit from `DialectConversionPattern` and override `rewrite`,
+/// This class inherit from `ConversionPattern` and override `rewrite`,
/// similarly to the PatternRewriter introduced in the previous chapter.
/// It will be called by the DialectConversion framework (see `LateLowering`
/// class below).
-class AddOpConversion : public DialectConversionPattern {
+class AddOpConversion : public ConversionPattern {
public:
explicit AddOpConversion(MLIRContext *context)
- : DialectConversionPattern(toy::AddOp::getOperationName(), 1, context) {}
+ : ConversionPattern(toy::AddOp::getOperationName(), 1, context) {}
/// Lower the `op` by generating IR using the `rewriter` builder. The builder
/// is setup with a new function, the `operands` array has been populated with
/// Base class for operation conversions targeting the LLVM IR dialect. Provides
/// conversion patterns with an access to the containing LLVMLowering for the
/// purpose of type conversions.
-class LLVMOpLowering : public DialectConversionPattern {
+class LLVMOpLowering : public ConversionPattern {
public:
LLVMOpLowering(StringRef rootOpName, MLIRContext *context,
LLVMLowering &lowering);
class Type;
class Value;
-/// Base class for the dialect conversion patterns that require type changes.
-/// Specific conversions must derive this class and implement least one
-/// `rewrite` method.
+/// Base class for the conversion patterns that require type changes. Specific
+/// conversions must derive this class and implement least one `rewrite` method.
/// NOTE: These conversion patterns can only be used with the 'apply*' methods
/// below.
-class DialectConversionPattern : public RewritePattern {
+class ConversionPattern : public RewritePattern {
public:
- /// Construct an DialectConversionPattern. `rootName` must correspond to the
+ /// Construct an ConversionPattern. `rootName` must correspond to the
/// canonical name of the first operation matched by the pattern.
- DialectConversionPattern(StringRef rootName, PatternBenefit benefit,
- MLIRContext *ctx)
+ ConversionPattern(StringRef rootName, PatternBenefit benefit,
+ MLIRContext *ctx)
: RewritePattern(rootName, benefit, ctx) {}
/// Hook for derived classes to implement matching. Dialect conversion
/// operation matched by the pattern, `operands` is a list of rewritten values
/// that are passed to this operation, `rewriter` can be used to emit the new
/// operations. This function must be reimplemented if the
- /// DialectConversionPattern ever needs to replace an operation that does not
+ /// ConversionPattern ever needs to replace an operation that does not
/// have successors. This function should not fail. If some specific cases of
/// the operation are not supported, these cases should not be matched.
virtual void rewrite(Operation *op, ArrayRef<Value *> operands,
/// of (potentially rewritten) successor blocks, `operands` is a list of lists
/// of rewritten values passed to each of the successors, co-indexed with
/// `destinations`, `rewriter` can be used to emit the new operations. It must
- /// be reimplemented if the DialectConversionPattern ever needs to replace a
+ /// be reimplemented if the ConversionPattern ever needs to replace a
/// terminator operation that has successors. This function should not fail
/// the pass. If some specific cases of the operation are not supported,
/// these cases should not be matched.
LLVMOpLowering::LLVMOpLowering(StringRef rootOpName, MLIRContext *context,
LLVMLowering &lowering_)
- : DialectConversionPattern(rootOpName, /*benefit=*/1, context),
+ : ConversionPattern(rootOpName, /*benefit=*/1, context),
lowering(lowering_) {}
namespace {
// DialectConversionRewriter
//===----------------------------------------------------------------------===//
-/// This class implements a pattern rewriter for DialectConversionPattern
+/// This class implements a pattern rewriter for ConversionPattern
/// patterns. It automatically performs remapping of replaced operation values.
struct DialectConversionRewriter final : public PatternRewriter {
/// This class represents one requested operation replacement via 'replaceOp'.
} // end anonymous namespace
//===----------------------------------------------------------------------===//
-// DialectConversionPattern
+// ConversionPattern
//===----------------------------------------------------------------------===//
/// Rewrite the IR rooted at the specified operation with the result of this
/// pattern. If an unexpected error is encountered (an internal compiler
/// error), it is emitted through the normal MLIR diagnostic hooks and the IR is
/// left in a valid state.
-void DialectConversionPattern::rewrite(Operation *op,
- PatternRewriter &rewriter) const {
+void ConversionPattern::rewrite(Operation *op,
+ PatternRewriter &rewriter) const {
SmallVector<Value *, 4> operands;
auto &dialectRewriter = static_cast<DialectConversionRewriter &>(rewriter);
dialectRewriter.remapValues(op->getOperands(), operands);