//===- Bufferize.cpp - Bufferization for `tensor` dialect ops -------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements bufferization of `tensor` dialect ops // //===----------------------------------------------------------------------===// #include "mlir/Transforms/Bufferize.h" #include "PassDetail.h" #include "mlir/Dialect/StandardOps/IR/Ops.h" #include "mlir/Dialect/Tensor/IR/Tensor.h" #include "mlir/Dialect/Tensor/Transforms/Passes.h" #include "mlir/Transforms/DialectConversion.h" using namespace mlir; namespace { class BufferizeExtractOp : public OpConversionPattern { public: using OpConversionPattern::OpConversionPattern; LogicalResult matchAndRewrite(tensor::ExtractOp op, ArrayRef operands, ConversionPatternRewriter &rewriter) const override { tensor::ExtractOp::Adaptor adaptor(operands); rewriter.replaceOpWithNewOp(op, adaptor.tensor(), adaptor.indices()); return success(); } }; } // namespace void mlir::populateTensorBufferizePatterns( MLIRContext *context, BufferizeTypeConverter &typeConverter, OwningRewritePatternList &patterns) { patterns.insert(typeConverter, context); } namespace { struct TensorBufferizePass : public TensorBufferizeBase { void runOnFunction() override { auto *context = &getContext(); BufferizeTypeConverter typeConverter; OwningRewritePatternList patterns; ConversionTarget target(*context); populateTensorBufferizePatterns(context, typeConverter, patterns); target.addIllegalOp(); target.addLegalDialect(); if (failed( applyPartialConversion(getFunction(), target, std::move(patterns)))) signalPassFailure(); } }; } // namespace std::unique_ptr mlir::createTensorBufferizePass() { return std::make_unique(); }