diff --git a/.mailmap b/.mailmap --- a/.mailmap +++ b/.mailmap @@ -35,4 +35,5 @@ Jon Roelofs LLVM GN Syncbot Martin Storsjö +Ramkumar Ramachandra Saleem Abdulrasool diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h --- a/mlir/include/mlir/Conversion/Passes.h +++ b/mlir/include/mlir/Conversion/Passes.h @@ -56,7 +56,6 @@ #include "mlir/Conversion/TosaToArith/TosaToArith.h" #include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h" #include "mlir/Conversion/TosaToSCF/TosaToSCF.h" -#include "mlir/Conversion/TosaToTensor/TosaToTensor.h" #include "mlir/Conversion/VectorToGPU/VectorToGPU.h" #include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h" #include "mlir/Conversion/VectorToSCF/VectorToSCF.h" diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td --- a/mlir/include/mlir/Conversion/Passes.td +++ b/mlir/include/mlir/Conversion/Passes.td @@ -895,23 +895,6 @@ let constructor = "tosa::createTosaToSCF()"; } -//===----------------------------------------------------------------------===// -// TosaToTensor -//===----------------------------------------------------------------------===// - -def TosaToTensor : Pass<"tosa-to-tensor"> { - let summary = "Lower TOSA to the Tensor dialect"; - let dependentDialects = [ - "tensor::TensorDialect", - ]; - let description = [{ - Pass that converts TOSA operations to the equivalent operations using the - operations in the Tensor dialect. - }]; - - let constructor = "tosa::createTosaToTensor()"; -} - //===----------------------------------------------------------------------===// // VectorToGPU //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Conversion/CMakeLists.txt b/mlir/lib/Conversion/CMakeLists.txt --- a/mlir/lib/Conversion/CMakeLists.txt +++ b/mlir/lib/Conversion/CMakeLists.txt @@ -46,7 +46,6 @@ add_subdirectory(TosaToArith) add_subdirectory(TosaToLinalg) add_subdirectory(TosaToSCF) -add_subdirectory(TosaToTensor) add_subdirectory(VectorToLLVM) add_subdirectory(VectorToGPU) add_subdirectory(VectorToSCF) diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp --- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp +++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp @@ -2312,6 +2312,43 @@ } }; +class SliceOpConverter : public OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(tosa::SliceOp sliceOp, + PatternRewriter &rewriter) const final { + Location loc = sliceOp.getLoc(); + Value input = sliceOp.getInput(); + SmallVector strides, sizes, starts; + starts = extractFromI64ArrayAttr(sliceOp.getStart()); + strides.resize(sliceOp.getType().template cast().getRank(), 1); + + SmallVector dynSizes; + for (const auto &i : llvm::enumerate(sliceOp.getSize())) { + int64_t size = i.value().cast().getInt(); + size_t index = i.index(); + sizes.push_back(size == -1 ? ShapedType::kDynamic : size); + if (!ShapedType::isDynamic(sizes.back())) + continue; + + auto dim = rewriter.create(loc, input, index); + auto offset = rewriter.create( + loc, rewriter.getIndexAttr(starts[index])); + dynSizes.push_back(rewriter.create(loc, dim, offset)); + } + + auto newSliceOp = rewriter.create( + loc, sliceOp.getType(), input, ValueRange({}), dynSizes, ValueRange({}), + rewriter.getDenseI64ArrayAttr(starts), + rewriter.getDenseI64ArrayAttr(sizes), + rewriter.getDenseI64ArrayAttr(strides)); + + rewriter.replaceOp(sliceOp, newSliceOp.getResult()); + return success(); + } +}; + } // namespace void mlir::tosa::populateTosaToLinalgConversionPatterns( @@ -2376,6 +2413,7 @@ ReshapeConverterCollapseExpand, RescaleConverter, ReverseConverter, + SliceOpConverter, TableConverter, TileConverter, TransposeConverter>(patterns->getContext()); diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp --- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp +++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp @@ -55,7 +55,6 @@ target.addLegalOp(); target.addLegalOp(); target.addLegalOp(); - target.addLegalOp(); target.markUnknownOpDynamicallyLegal([](Operation *) { return true; }); diff --git a/mlir/lib/Conversion/TosaToTensor/CMakeLists.txt b/mlir/lib/Conversion/TosaToTensor/CMakeLists.txt deleted file mode 100644 --- a/mlir/lib/Conversion/TosaToTensor/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -add_mlir_conversion_library(MLIRTosaToTensor - TosaToTensor.cpp - TosaToTensorPass.cpp - - ADDITIONAL_HEADER_DIRS - ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Tosa - ${MLIR_MAIN_INCLUDE_DIR}/mlir/IR - - DEPENDS - MLIRConversionPassIncGen - - LINK_LIBS PUBLIC - MLIRTensorDialect - MLIRIR - MLIRPass - MLIRTosaDialect - MLIRTosaTransforms - MLIRSupport - ) diff --git a/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp b/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp deleted file mode 100644 --- a/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp +++ /dev/null @@ -1,67 +0,0 @@ -//===- TosaToTensor.cpp - Lowering Tosa to Tensor Dialect -------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// These rewriters lower from the Tosa to the Tensor dialect. -// -//===----------------------------------------------------------------------===// - -#include "mlir/Conversion/TosaToTensor/TosaToTensor.h" -#include "mlir/Dialect/Arith/IR/Arith.h" -#include "mlir/Dialect/Tensor/IR/Tensor.h" -#include "mlir/Dialect/Tosa/IR/TosaOps.h" -#include "mlir/IR/PatternMatch.h" -#include "mlir/Transforms/GreedyPatternRewriteDriver.h" - -using namespace mlir; -using namespace tosa; - -namespace { - -class SliceOpConverter : public OpRewritePattern { -public: - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(tosa::SliceOp sliceOp, - PatternRewriter &rewriter) const final { - Location loc = sliceOp.getLoc(); - Value input = sliceOp.getInput(); - SmallVector strides, sizes, starts; - starts = extractFromI64ArrayAttr(sliceOp.getStart()); - strides.resize(sliceOp.getType().template cast().getRank(), 1); - - SmallVector dynSizes; - for (const auto &i : llvm::enumerate(sliceOp.getSize())) { - int64_t size = i.value().cast().getInt(); - size_t index = i.index(); - sizes.push_back(size == -1 ? ShapedType::kDynamic : size); - if (!ShapedType::isDynamic(sizes.back())) - continue; - - auto dim = rewriter.create(loc, input, index); - auto offset = rewriter.create( - loc, rewriter.getIndexAttr(starts[index])); - dynSizes.push_back(rewriter.create(loc, dim, offset)); - } - - auto newSliceOp = rewriter.create( - sliceOp.getLoc(), sliceOp.getType(), input, ValueRange({}), dynSizes, - ValueRange({}), rewriter.getDenseI64ArrayAttr(starts), - rewriter.getDenseI64ArrayAttr(sizes), - rewriter.getDenseI64ArrayAttr(strides)); - - rewriter.replaceOp(sliceOp, newSliceOp.getResult()); - return success(); - } -}; - -} // namespace - -void mlir::tosa::populateTosaToTensorConversionPatterns( - RewritePatternSet *patterns) { - patterns->add(patterns->getContext()); -} diff --git a/mlir/lib/Conversion/TosaToTensor/TosaToTensorPass.cpp b/mlir/lib/Conversion/TosaToTensor/TosaToTensorPass.cpp deleted file mode 100644 --- a/mlir/lib/Conversion/TosaToTensor/TosaToTensorPass.cpp +++ /dev/null @@ -1,53 +0,0 @@ -//===- TosaToTensorPass.cpp - Lowering Tosa to Tensor Dialect -------------===// -// -// 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 transformation pass legalizes Tosa operations to the Tensor dialect. -// -//===----------------------------------------------------------------------===// - -#include "mlir/Conversion/TosaToTensor/TosaToTensor.h" - -#include "mlir/Dialect/Arith/IR/Arith.h" -#include "mlir/Dialect/Tensor/IR/Tensor.h" -#include "mlir/Dialect/Tosa/IR/TosaOps.h" -#include "mlir/Dialect/Tosa/Transforms/Passes.h" -#include "mlir/IR/PatternMatch.h" -#include "mlir/Pass/PassManager.h" -#include "mlir/Transforms/DialectConversion.h" -#include "mlir/Transforms/GreedyPatternRewriteDriver.h" - -namespace mlir { -#define GEN_PASS_DEF_TOSATOTENSOR -#include "mlir/Conversion/Passes.h.inc" -} // namespace mlir - -using namespace mlir; -using namespace tosa; - -namespace { -struct TosaToTensor : public impl::TosaToTensorBase { -public: - void runOnOperation() override { - RewritePatternSet patterns(&getContext()); - ConversionTarget target(getContext()); - target.addIllegalOp(); - target.addLegalDialect(); - target.addLegalDialect(); - - mlir::tosa::populateTosaToTensorConversionPatterns(&patterns); - - if (failed(applyPartialConversion(getOperation(), target, - std::move(patterns)))) - signalPassFailure(); - } -}; -} // namespace - -std::unique_ptr mlir::tosa::createTosaToTensor() { - return std::make_unique(); -} diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir --- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir +++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir @@ -948,7 +948,7 @@ // ----- // CHECK-LABEL: @concat_non_axis_dyn -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: // CHECK-SAME: %[[ARG1:[0-9a-zA-Z_]*]] func.func @concat_non_axis_dyn(%arg0: tensor<5x?xf32>, %arg1: tensor<6x?xf32>) -> () { // CHECK: %[[AXIS:.+]] = arith.constant 0 @@ -969,8 +969,8 @@ // ----- // CHECK-LABEL: @concat_axis_dyn -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: -// CHECK-SAME: %[[ARG1:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: %[[ARG1:[0-9a-zA-Z_]*]]: func.func @concat_axis_dyn(%arg0: tensor, %arg1: tensor) -> () { // CHECK: %[[AXIS:.+]] = arith.constant 0 // CHECK: %[[STRIDE:.+]] = arith.constant 1 @@ -994,7 +994,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)> // CHECK-LABEL: @rescale_i8 -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @rescale_i8(%arg0 : tensor<2xi8>) -> () { // CHECK: [[C0:%.+]] = arith.constant 19689 // CHECK: [[C1:%.+]] = arith.constant 15 @@ -1048,7 +1048,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)> // CHECK-LABEL: @rescale_i8_dyn_batch -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @rescale_i8_dyn_batch(%arg0 : tensor) -> () { // CHECK: %[[C0:.+]] = arith.constant 0 // CHECK: %[[BATCH:.+]] = tensor.dim %[[ARG0]], %[[C0]] @@ -1070,7 +1070,7 @@ // CHECK: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> // CHECK-LABEL: @rescale_dyn -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @rescale_dyn(%arg0 : tensor<1x?x?x32xi32>) -> () { // CHECK: %[[C1:.+]] = arith.constant 1 // CHECK: %[[DIM1:.+]] = tensor.dim %[[ARG0]], %[[C1]] @@ -1087,7 +1087,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)> // CHECK-LABEL: @rescale_ui8 -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @rescale_ui8(%arg0 : tensor<2xui8>) -> () { // CHECK: [[C0:%.+]] = arith.constant 19689 // CHECK: [[C1:%.+]] = arith.constant 15 @@ -1119,7 +1119,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)> // CHECK-LABEL: @rescale_per_channel -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @rescale_per_channel(%arg0 : tensor<3xi8>) -> (tensor<3xi8>) { // CHECK: [[MULTIPLIERS:%.+]] = arith.constant dense<[42, 43, 0]> // CHECK: [[SHIFTS:%.+]] = arith.constant dense<[14, 15, 0]> @@ -1172,7 +1172,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)> // CHECK-LABEL: @reverse -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @reverse(%arg0: tensor<5x4xi32>) -> () { // CHECK: %[[C0:.+]] = arith.constant 0 // CHECK: %[[RDIM:.+]] = tensor.dim %[[ARG0]], %[[C0]] @@ -1207,7 +1207,7 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)> // CHECK-LABEL: @reverse_dyn -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @reverse_dyn(%arg0: tensor) -> () { // CHECK: %[[C0_1:.+]] = arith.constant 0 // CHECK: %[[D0_1:.+]] = tensor.dim %[[ARG0]], %[[C0_1]] @@ -1263,7 +1263,7 @@ // CHECK-DAG: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> // CHECK-LABEL: @tile_dyn_input -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @tile_dyn_input(%arg0 : tensor) -> () { // CHECK: %[[CST0:.+]] = arith.constant 0 // CHECK: %[[DYN:.+]] = tensor.dim %[[ARG0]], %[[CST0]] : tensor @@ -1284,7 +1284,7 @@ // CHECK-DAG: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> // CHECK-LABEL: @tile_dyn_multiples -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @tile_dyn_multiples(%arg0 : tensor<2x3xi8>) -> () { // CHECK: %[[CST1:.+]] = arith.constant 1 // CHECK: %[[DYN:.+]] = tensor.dim %[[ARG0]], %[[CST1]] : tensor<2x3xi8> @@ -1302,7 +1302,7 @@ // ----- // CHECK-LABEL: @pad_float -// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: +// CHECK-SAME: (%[[ARG0:[0-9a-zA-Z_]*]]: func.func @pad_float(%arg0 : tensor<1x2xf32>) -> (tensor<4x9xf32>) { %0 = arith.constant dense<[[1, 2], [3, 4]]> : tensor<2x2xi32> // TODO: Output contains multiple "arith.constant 1 : index". @@ -1635,6 +1635,29 @@ // ----- +// CHECK-LABEL: @slice +// CHECK-SAME: %[[ARG0:[0-9a-zA-Z_]*]]: +func.func @slice(%arg0: tensor<6xf32>) -> (tensor<1xf32>) { + // CHECK: [[SLICE:%.+]] = tensor.extract_slice %arg0[2] [1] [1] + %0 = "tosa.slice"(%arg0) {start = [2], size = [1]} : (tensor<6xf32>) -> (tensor<1xf32>) + return %0 : tensor<1xf32> +} + +// ----- + +// CHECK-LABEL: func @slice_dyn +func.func @slice_dyn(%arg0: tensor) -> (tensor) { + // CHECK: %[[C0:.+]] = arith.constant 0 : index + // CHECK: %[[DIM:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[C2:.+]] = arith.constant 2 : index + // CHECK: %[[SUB:.+]] = arith.subi %[[DIM]], %[[C2]] + // CHECK: tensor.extract_slice %arg0[2] [%[[SUB]]] [1] + %0 = "tosa.slice"(%arg0) {start = [2], size = [-1]} : (tensor) -> (tensor) + return %0 : tensor +} + +// ----- + // Regression test for using the wrong rank. // CHECK-DAG: affine_map<(d0, d1, d2, d3) -> (d0, d2, d3)> diff --git a/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir b/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir deleted file mode 100644 --- a/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir +++ /dev/null @@ -1,21 +0,0 @@ -// RUN: mlir-opt --split-input-file --tosa-to-tensor %s -o -| FileCheck %s - -// CHECK-LABLE: func @slice -func.func @slice(%arg0: tensor<6xf32>) ->() { - // CHECK: [[SLICE:%.+]] = tensor.extract_slice %arg0[2] [1] [1] - %0 = "tosa.slice"(%arg0) {start = [2], size = [1]} : (tensor<6xf32>) -> (tensor<1xf32>) - return -} - -// ----- - -// CHECK-LABLE: func @slice_dyn -func.func @slice_dyn(%arg0: tensor) -> (tensor) { - // CHECK: %[[C0:.+]] = arith.constant 0 : index - // CHECK: %[[DIM:.+]] = tensor.dim %arg0, %[[C0]] - // CHECK: %[[C2:.+]] = arith.constant 2 : index - // CHECK: %[[SUB:.+]] = arith.subi %[[DIM]], %[[C2]] - // CHECK: tensor.extract_slice %arg0[2] [%[[SUB]]] [1] - %0 = "tosa.slice"(%arg0) {start = [2], size = [-1]} : (tensor) -> (tensor) - return %0 : tensor -}