#include "mlir/Dialect/MemRef/Transforms/Passes.h"
#include "mlir/Dialect/MemRef/Transforms/Transforms.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Transforms/DialectConversion.h"
namespace mlir {
namespace memref {
#define GEN_PASS_DEF_EXPANDREALLOC
#include "mlir/Dialect/MemRef/Transforms/Passes.h.inc"
}
}
using namespace mlir;
namespace {
struct ExpandReallocOpPattern : public OpRewritePattern<memref::ReallocOp> {
ExpandReallocOpPattern(MLIRContext *ctx, bool emitDeallocs)
: OpRewritePattern(ctx), emitDeallocs(emitDeallocs) {}
LogicalResult matchAndRewrite(memref::ReallocOp op,
PatternRewriter &rewriter) const final {
Location loc = op.getLoc();
assert(op.getType().getRank() == 1 &&
"result MemRef must have exactly one rank");
assert(op.getSource().getType().getRank() == 1 &&
"source MemRef must have exactly one rank");
assert(op.getType().getLayout().isIdentity() &&
"result MemRef must have identity layout (or none)");
assert(op.getSource().getType().getLayout().isIdentity() &&
"source MemRef must have identity layout (or none)");
int64_t inputSize =
cast<BaseMemRefType>(op.getSource().getType()).getDimSize(0);
OpFoldResult currSize = rewriter.getIndexAttr(inputSize);
if (ShapedType::isDynamic(inputSize)) {
Value dimZero = getValueOrCreateConstantIndexOp(rewriter, loc,
rewriter.getIndexAttr(0));
currSize = rewriter.create<memref::DimOp>(loc, op.getSource(), dimZero)
.getResult();
}
int64_t outputSize =
cast<BaseMemRefType>(op.getResult().getType()).getDimSize(0);
OpFoldResult targetSize = ShapedType::isDynamic(outputSize)
? OpFoldResult{op.getDynamicResultSize()}
: rewriter.getIndexAttr(outputSize);
Value lhs = getValueOrCreateConstantIndexOp(rewriter, loc, currSize);
Value rhs = getValueOrCreateConstantIndexOp(rewriter, loc, targetSize);
Value cond = rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::ult,
lhs, rhs);
auto ifOp = rewriter.create<scf::IfOp>(
loc, cond,
[&](OpBuilder &builder, Location loc) {
SmallVector<Value> dynamicSizeOperands;
if (op.getDynamicResultSize())
dynamicSizeOperands.push_back(op.getDynamicResultSize());
Value newAlloc = builder.create<memref::AllocOp>(
loc, op.getResult().getType(), dynamicSizeOperands,
op.getAlignmentAttr());
Value subview = builder.create<memref::SubViewOp>(
loc, newAlloc, ArrayRef<OpFoldResult>{rewriter.getIndexAttr(0)},
ArrayRef<OpFoldResult>{currSize},
ArrayRef<OpFoldResult>{rewriter.getIndexAttr(1)});
builder.create<memref::CopyOp>(loc, op.getSource(), subview);
if (emitDeallocs)
builder.create<memref::DeallocOp>(loc, op.getSource());
builder.create<scf::YieldOp>(loc, newAlloc);
},
[&](OpBuilder &builder, Location loc) {
Value casted = builder.create<memref::ReinterpretCastOp>(
loc, cast<MemRefType>(op.getResult().getType()), op.getSource(),
rewriter.getIndexAttr(0), ArrayRef<OpFoldResult>{targetSize},
ArrayRef<OpFoldResult>{rewriter.getIndexAttr(1)});
builder.create<scf::YieldOp>(loc, casted);
});
rewriter.replaceOp(op, ifOp.getResult(0));
return success();
}
private:
const bool emitDeallocs;
};
struct ExpandReallocPass
: public memref::impl::ExpandReallocBase<ExpandReallocPass> {
ExpandReallocPass(bool emitDeallocs)
: memref::impl::ExpandReallocBase<ExpandReallocPass>() {
this->emitDeallocs.setValue(emitDeallocs);
}
void runOnOperation() override {
MLIRContext &ctx = getContext();
RewritePatternSet patterns(&ctx);
memref::populateExpandReallocPatterns(patterns, emitDeallocs.getValue());
ConversionTarget target(ctx);
target.addLegalDialect<arith::ArithDialect, scf::SCFDialect,
memref::MemRefDialect>();
target.addIllegalOp<memref::ReallocOp>();
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
};
}
void mlir::memref::populateExpandReallocPatterns(RewritePatternSet &patterns,
bool emitDeallocs) {
patterns.add<ExpandReallocOpPattern>(patterns.getContext(), emitDeallocs);
}
std::unique_ptr<Pass> mlir::memref::createExpandReallocPass(bool emitDeallocs) {
return std::make_unique<ExpandReallocPass>(emitDeallocs);
}