//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// #include "../PassDetail.h" #include "npcomp/RefBackend/RefBackend.h" #include "mlir/Dialect/SCF/SCF.h" #include "mlir/Dialect/SCF/Transforms.h" #include "mlir/Dialect/StandardOps/IR/Ops.h" #include "mlir/Pass/Pass.h" #include "mlir/Pass/PassRegistry.h" #include "mlir/Transforms/Bufferize.h" #include "mlir/Transforms/DialectConversion.h" #include "npcomp/Dialect/Refback/IR/RefbackOps.h" using namespace mlir; using namespace mlir::NPCOMP; //===----------------------------------------------------------------------===// // Generic "update the types according to the type converter" patterns. // // TODO: These should be upstreamed. There's nothing specific to memref type // conversion about them. //===----------------------------------------------------------------------===// namespace { // This is a type conversion similar to CallOpSignatureConversion. class LowerSelectOpTypes : public OpConversionPattern { public: using OpConversionPattern::OpConversionPattern; LogicalResult matchAndRewrite(SelectOp op, ArrayRef operands, ConversionPatternRewriter &rewriter) const override { SelectOp::Adaptor adaptor(operands); rewriter.updateRootInPlace( op, [&] { op.getResult().setType(adaptor.true_value().getType()); }); return success(); } }; } // namespace //===----------------------------------------------------------------------===// // Further lowerings. //===----------------------------------------------------------------------===// namespace { class LowerTensorToMemrefOp : public OpConversionPattern { public: using OpConversionPattern::OpConversionPattern; LogicalResult matchAndRewrite(TensorToMemrefOp op, ArrayRef operands, ConversionPatternRewriter &rewriter) const override { TensorToMemrefOp::Adaptor adaptor(operands); rewriter.replaceOp(op, adaptor.tensor()); return success(); } }; } // namespace namespace { class LowerTensorLoadOp : public OpConversionPattern { public: using OpConversionPattern::OpConversionPattern; LogicalResult matchAndRewrite(TensorLoadOp op, ArrayRef operands, ConversionPatternRewriter &rewriter) const override { TensorLoadOp::Adaptor adaptor(operands); rewriter.replaceOp(op, adaptor.memref()); return success(); } }; } // namespace //===----------------------------------------------------------------------===// // The pass. //===----------------------------------------------------------------------===// namespace { class LowerStructuralToMemref : public LowerStructuralToMemrefBase { void runOnOperation() { auto func = getOperation(); auto *context = &getContext(); BufferizeTypeConverter typeConverter; OwningRewritePatternList patterns; ConversionTarget target(*context); // All ops whose results are not tensor types are legal. target.markUnknownOpDynamicallyLegal([](Operation *op) { return llvm::all_of(op->getResultTypes(), [](Type type) { return !type.isa(); }); }); populateFuncOpTypeConversionPattern(patterns, context, typeConverter); target.addDynamicallyLegalOp([&](mlir::FuncOp op) { return typeConverter.isSignatureLegal(op.getType()) && typeConverter.isLegal(&op.getBody()); }); scf::populateSCFStructuralTypeConversionsAndLegality(context, typeConverter, patterns, target); patterns.insert(typeConverter, context); patterns.insert(typeConverter, context); patterns.insert(typeConverter, context); target.addIllegalOp(); if (failed(applyFullConversion(func, target, patterns))) return signalPassFailure(); } }; } // namespace std::unique_ptr> mlir::NPCOMP::createLowerStructuralToMemrefPass() { return std::make_unique(); }