1 //===- TosaToTensor.cpp - Lowering Tosa to Tensor Dialect -------------===//
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 // These rewriters lower from the Tosa to the Tensor dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Conversion/TosaToTensor/TosaToTensor.h"
14 #include "mlir/Dialect/Tensor/IR/Tensor.h"
15 #include "mlir/Dialect/Tosa/IR/TosaOps.h"
16 #include "mlir/IR/PatternMatch.h"
17 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
18 
19 using namespace mlir;
20 using namespace tosa;
21 
22 namespace {
23 
24 class SliceOpConverter : public OpRewritePattern<tosa::SliceOp> {
25 public:
26   using OpRewritePattern<tosa::SliceOp>::OpRewritePattern;
27 
28   LogicalResult matchAndRewrite(tosa::SliceOp sliceOp,
29                                 PatternRewriter &rewriter) const final {
30     Value input = sliceOp.input();
31     SmallVector<int64_t> strides;
32     strides.resize(sliceOp.getType().template cast<ShapedType>().getRank(), 1);
33 
34     rewriter.replaceOpWithNewOp<tensor::ExtractSliceOp>(
35         sliceOp, sliceOp.getType(), input, ValueRange({}), ValueRange({}),
36         ValueRange({}), sliceOp.start(), sliceOp.size(),
37         rewriter.getI64ArrayAttr(strides));
38     return success();
39   }
40 };
41 
42 } // namespace
43 
44 void mlir::tosa::populateTosaToTensorConversionPatterns(
45     RewritePatternSet *patterns) {
46   patterns->add<SliceOpConverter>(patterns->getContext());
47 }
48