1 //===- BufferizationToMemRef.cpp - Bufferization to MemRef conversion -----===// 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 // This file implements patterns to convert Bufferization dialect to MemRef 10 // dialect. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "../PassDetail.h" 15 #include "mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h" 16 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" 17 #include "mlir/Dialect/Bufferization/IR/Bufferization.h" 18 #include "mlir/Dialect/MemRef/IR/MemRef.h" 19 #include "mlir/IR/BuiltinTypes.h" 20 #include "mlir/Support/LogicalResult.h" 21 #include "mlir/Transforms/DialectConversion.h" 22 23 using namespace mlir; 24 25 namespace { 26 /// The CloneOpConversion transforms all bufferization clone operations into 27 /// memref alloc and memref copy operations. In the dynamic-shape case, it also 28 /// emits additional dim and constant operations to determine the shape. This 29 /// conversion does not resolve memory leaks if it is used alone. 30 struct CloneOpConversion : public OpConversionPattern<bufferization::CloneOp> { 31 using OpConversionPattern<bufferization::CloneOp>::OpConversionPattern; 32 33 LogicalResult 34 matchAndRewrite(bufferization::CloneOp op, OpAdaptor adaptor, 35 ConversionPatternRewriter &rewriter) const override { 36 // Check for unranked memref types which are currently not supported. 37 Type type = op.getType(); 38 if (type.isa<UnrankedMemRefType>()) { 39 return rewriter.notifyMatchFailure( 40 op, "UnrankedMemRefType is not supported."); 41 } 42 MemRefType memrefType = type.cast<MemRefType>(); 43 MemRefLayoutAttrInterface layout; 44 auto allocType = 45 MemRefType::get(memrefType.getShape(), memrefType.getElementType(), 46 layout, memrefType.getMemorySpace()); 47 // Since this implementation always allocates, certain result types of the 48 // clone op cannot be lowered. 49 if (!memref::CastOp::areCastCompatible({allocType}, {memrefType})) 50 return failure(); 51 52 // Transform a clone operation into alloc + copy operation and pay 53 // attention to the shape dimensions. 54 Location loc = op->getLoc(); 55 SmallVector<Value, 4> dynamicOperands; 56 for (int i = 0; i < memrefType.getRank(); ++i) { 57 if (!memrefType.isDynamicDim(i)) 58 continue; 59 Value size = rewriter.createOrFold<arith::ConstantIndexOp>(loc, i); 60 Value dim = rewriter.createOrFold<memref::DimOp>(loc, op.input(), size); 61 dynamicOperands.push_back(dim); 62 } 63 64 // Allocate a memref with identity layout. 65 Value alloc = rewriter.create<memref::AllocOp>(op->getLoc(), allocType, 66 dynamicOperands); 67 // Cast the allocation to the specified type if needed. 68 if (memrefType != allocType) 69 alloc = rewriter.create<memref::CastOp>(op->getLoc(), memrefType, alloc); 70 rewriter.replaceOp(op, alloc); 71 rewriter.create<memref::CopyOp>(loc, op.input(), alloc); 72 return success(); 73 } 74 }; 75 } // namespace 76 77 void mlir::populateBufferizationToMemRefConversionPatterns( 78 RewritePatternSet &patterns) { 79 patterns.add<CloneOpConversion>(patterns.getContext()); 80 } 81 82 namespace { 83 struct BufferizationToMemRefPass 84 : public ConvertBufferizationToMemRefBase<BufferizationToMemRefPass> { 85 BufferizationToMemRefPass() = default; 86 87 void runOnOperation() override { 88 RewritePatternSet patterns(&getContext()); 89 populateBufferizationToMemRefConversionPatterns(patterns); 90 91 ConversionTarget target(getContext()); 92 target.addLegalDialect<memref::MemRefDialect>(); 93 target.addLegalOp<arith::ConstantOp>(); 94 target.addIllegalDialect<bufferization::BufferizationDialect>(); 95 96 if (failed(applyPartialConversion(getOperation(), target, 97 std::move(patterns)))) 98 signalPassFailure(); 99 } 100 }; 101 } // namespace 102 103 std::unique_ptr<Pass> mlir::createBufferizationToMemRefPass() { 104 return std::make_unique<BufferizationToMemRefPass>(); 105 } 106