[llvm-branch-commits] [mlir] f4bb076 - [mlir][tosa] Add tosa.slice to std.subtensor lowering
Author: Rob Suderman Date: 2021-03-17T17:28:18-07:00 New Revision: f4bb076a4419767cf35a17e3c08f392505a5acd2 URL: https://github.com/llvm/llvm-project/commit/f4bb076a4419767cf35a17e3c08f392505a5acd2 DIFF: https://github.com/llvm/llvm-project/commit/f4bb076a4419767cf35a17e3c08f392505a5acd2.diff LOG: [mlir][tosa] Add tosa.slice to std.subtensor lowering Lowering to subtensor is added for tosa.slice operator. Differential Revision: https://reviews.llvm.org/D98825 Added: Modified: mlir/lib/Conversion/TosaToStandard/TosaToStandard.cpp mlir/lib/Conversion/TosaToStandard/TosaToStandardPass.cpp mlir/test/Conversion/TosaToStandard/tosa-to-standard.mlir Removed: diff --git a/mlir/lib/Conversion/TosaToStandard/TosaToStandard.cpp b/mlir/lib/Conversion/TosaToStandard/TosaToStandard.cpp index 21a8da291aee..6e5411dd5ecb 100644 --- a/mlir/lib/Conversion/TosaToStandard/TosaToStandard.cpp +++ b/mlir/lib/Conversion/TosaToStandard/TosaToStandard.cpp @@ -32,9 +32,28 @@ class ConstOpConverter : public OpRewritePattern { } }; +class SliceOpConverter : public OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(tosa::SliceOp sliceOp, +PatternRewriter &rewriter) const final { +Value input = sliceOp.input(); +SmallVector strides; +strides.resize(sliceOp.getType().template cast().getRank(), 1); + +rewriter.replaceOpWithNewOp( +sliceOp, sliceOp.getType(), input, ValueRange({}), ValueRange({}), +ValueRange({}), sliceOp.start(), sliceOp.size(), +rewriter.getI64ArrayAttr(strides)); + +return success(); + } +}; + } // namespace void mlir::tosa::populateTosaToStandardConversionPatterns( MLIRContext *context, OwningRewritePatternList *patterns) { - patterns->insert(context); + patterns->insert(context); } diff --git a/mlir/lib/Conversion/TosaToStandard/TosaToStandardPass.cpp b/mlir/lib/Conversion/TosaToStandard/TosaToStandardPass.cpp index 225855e78bda..78a0e65da81b 100644 --- a/mlir/lib/Conversion/TosaToStandard/TosaToStandardPass.cpp +++ b/mlir/lib/Conversion/TosaToStandard/TosaToStandardPass.cpp @@ -32,7 +32,8 @@ struct TosaToStandard : public TosaToStandardBase { OwningRewritePatternList patterns; ConversionTarget target(getContext()); target.addIllegalOp(); -target.addLegalOp(); +target.addIllegalOp(); +target.addLegalDialect(); auto *op = getOperation(); mlir::tosa::populateTosaToStandardConversionPatterns(op->getContext(), diff --git a/mlir/test/Conversion/TosaToStandard/tosa-to-standard.mlir b/mlir/test/Conversion/TosaToStandard/tosa-to-standard.mlir index 86304dcba862..94925aec15c7 100644 --- a/mlir/test/Conversion/TosaToStandard/tosa-to-standard.mlir +++ b/mlir/test/Conversion/TosaToStandard/tosa-to-standard.mlir @@ -8,3 +8,11 @@ func @const_test() -> (tensor) { // CHECK: return [[C3]] return %0 : tensor } + +// + +func @slice(%arg0: tensor<6xf32>) ->() { + // CHECK: [[SLICE:%.+]] = subtensor %arg0[2] [1] [1] + %0 = "tosa.slice"(%arg0) {start = [2], size = [1]} : (tensor<6xf32>) -> (tensor<1xf32>) + return +} ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [mlir] 143edec - [mlir][tosa] Shape inference for a few remaining easy cases:
Author: Rob Suderman Date: 2021-08-03T17:20:32-07:00 New Revision: 143edeca6dfe5d9c006cdadb5be1ec4afd483ca3 URL: https://github.com/llvm/llvm-project/commit/143edeca6dfe5d9c006cdadb5be1ec4afd483ca3 DIFF: https://github.com/llvm/llvm-project/commit/143edeca6dfe5d9c006cdadb5be1ec4afd483ca3.diff LOG: [mlir][tosa] Shape inference for a few remaining easy cases: Handles shape inference for identity, cast, and rescale. These were missed during the initialy elementwise work. This includes resize shape propagation which includes both attribute and input type based propagation. Reviewed By: jpienaar Differential Revision: https://reviews.llvm.org/D105845 Added: Modified: mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td mlir/lib/Dialect/Tosa/IR/TosaOps.cpp mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir Removed: diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td index f17e13c66a449..7a29350467814 100644 --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td @@ -1582,7 +1582,10 @@ def Tosa_ScatterOp : Tosa_Op<"scatter", [ //===--===// // Operator: resize //===--===// -def Tosa_ResizeOp : Tosa_Op<"resize", [NoSideEffect]> { +def Tosa_ResizeOp : Tosa_Op<"resize", [ + DeclareOpInterfaceMethods, + NoSideEffect]> { let summary = "Resize operation, supports various resize/upsample modes"; @@ -1617,7 +1620,9 @@ def Tosa_ResizeOp : Tosa_Op<"resize", [NoSideEffect]> { //===--===// // Operator: cast //===--===// -def Tosa_CastOp: Tosa_Op<"cast", [NoSideEffect]> { +def Tosa_CastOp: Tosa_Op<"cast", [NoSideEffect, + DeclareOpInterfaceMethods]> { let summary = "Cast operation"; @@ -1655,7 +1660,9 @@ def Tosa_CastOp: Tosa_Op<"cast", [NoSideEffect]> { //===--===// // Operator: rescale //===--===// -def Tosa_RescaleOp: Tosa_Op<"rescale", [NoSideEffect]> { +def Tosa_RescaleOp: Tosa_Op<"rescale", [NoSideEffect, + DeclareOpInterfaceMethods]> { let summary = "Tosa rescale operator"; let description = [{ @@ -1723,7 +1730,9 @@ def Tosa_ConstOp : Tosa_Op<"const", [ConstantLike, NoSideEffect, //===--===// // Operator: identity //===--===// -def Tosa_IdentityOp: Tosa_Op<"identity", [NoSideEffect]> { +def Tosa_IdentityOp: Tosa_Op<"identity", [NoSideEffect, + DeclareOpInterfaceMethods]> { let summary = "Identity operator"; let description = [{ Returns a tensor with the same shape, size, type diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp index cfc9220c97501..9ae2e95d146ae 100644 --- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp +++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp @@ -345,6 +345,12 @@ static void getI64Values(ArrayAttr arrayAttr, SmallVector &values) { } } +static void getF64Values(ArrayAttr arrayAttr, SmallVector &values) { + for (auto it : arrayAttr) { +values.push_back(it.cast().getValueAsDouble()); + } +} + LogicalResult tosa::ArgMaxOp::inferReturnTypeComponents( MLIRContext *context, ::llvm::Optional location, ValueShapeRange operands, DictionaryAttr attributes, RegionRange regions, @@ -386,13 +392,13 @@ LogicalResult tosa::ConcatOp::inferReturnTypeComponents( // Copy the Operand's rank. if (!hasRankedInput) - outputShape.resize(operandTy.getRank(), -1); + outputShape.resize(operandTy.getRank(), ShapedType::kDynamicSize); // Copy shapes until the dim is non-dynamic. for (int i = 0, s = operandTy.getRank(); i < s; i++) { if (i == axis || operandTy.isDynamicDim(i)) continue; - if (outputShape[i] == -1) + if (outputShape[i] == ShapedType::kDynamicSize) outputShape[i] = operandTy.getDimSize(i); if (outputShape[i] != operandTy.getDimSize(i)) return failure(); @@ -414,7 +420,7 @@ LogicalResult tosa::ConcatOp::inferReturnTypeComponents( // We need to know the length of the concatenation axis of all inputs to // determine the dimension size of the output shape. if (!operandTy.hasRank() || operandTy.isDynamicDim(axis)) { - concatDimSize = -1; + concatDimSize = ShapedType::kDynamicSize; break; } @@ -437,7 +443,7 @@ LogicalResult tosa::FullyConnectedOp::inferReturnTypeComponents( // All shapes are dynamic. SmallVector outShape; -
[llvm-branch-commits] [mlir] 1d973b7 - [MLIR][TOSA] First lowerings from Tosa to Linalg
Author: Rob Suderman Date: 2021-01-14T11:24:23-08:00 New Revision: 1d973b7ded124dd19f766db0c8e07d1c686dfb1b URL: https://github.com/llvm/llvm-project/commit/1d973b7ded124dd19f766db0c8e07d1c686dfb1b DIFF: https://github.com/llvm/llvm-project/commit/1d973b7ded124dd19f766db0c8e07d1c686dfb1b.diff LOG: [MLIR][TOSA] First lowerings from Tosa to Linalg Initial commit to add support for lowering from TOSA to Linalg. The focus is on the essential infrastructure for these lowerings and integration with existing passes. Includes lowerings for a subset of operations including: abs, add, sub, pow, and, or, xor, left shift, right shift, tanh Lit tests are used to validate correctness. Differential Revision: https://reviews.llvm.org/D94247 Added: mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h mlir/lib/Conversion/TosaToLinalg/CMakeLists.txt mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir Modified: mlir/include/mlir/Conversion/Passes.h mlir/include/mlir/Conversion/Passes.td mlir/lib/Conversion/CMakeLists.txt Removed: diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h index 64b99d33ab61..2e07a795b6c7 100644 --- a/mlir/include/mlir/Conversion/Passes.h +++ b/mlir/include/mlir/Conversion/Passes.h @@ -29,6 +29,7 @@ #include "mlir/Conversion/ShapeToStandard/ShapeToStandard.h" #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h" #include "mlir/Conversion/StandardToSPIRV/StandardToSPIRVPass.h" +#include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h" #include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h" #include "mlir/Conversion/VectorToROCDL/VectorToROCDL.h" #include "mlir/Conversion/VectorToSCF/VectorToSCF.h" diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td index 6a6ba6bbb371..e8ca058adedd 100644 --- a/mlir/include/mlir/Conversion/Passes.td +++ b/mlir/include/mlir/Conversion/Passes.td @@ -416,6 +416,20 @@ def ConvertStandardToSPIRV : Pass<"convert-std-to-spirv", "ModuleOp"> { let dependentDialects = ["spirv::SPIRVDialect"]; } +//===--===// +// TosaToLinalg +//===--===// + +def TosaToLinalgOnTensors : FunctionPass<"tosa-to-linalg-on-tensors"> { + let summary = "Lower TOSA to LinAlg on tensors"; + let description = [{ +Pass that converts TOSA operations to the equivalent operations using the +tensor operations in LinAlg. + }]; + + let constructor = "tosa::createTosaToLinalgOnTensors()"; +} + //===--===// // VectorToSCF //===--===// diff --git a/mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h b/mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h new file mode 100644 index ..42493a57bcc2 --- /dev/null +++ b/mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h @@ -0,0 +1,36 @@ +//===-- TosaToLinalg.h - TOSA optimization pass declarations --*- C++ +//-*-===// +// +// 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 declares the passes for the TOSA Linalg Dialect in MLIR. +// +//===--===// + +#ifndef MLIR_CONVERSION_TOSATOLINALG_TOSATOLINALG_H +#define MLIR_CONVERSION_TOSATOLINALG_TOSATOLINALG_H + +#include "mlir/Pass/Pass.h" + +namespace mlir { +namespace tosa { + +std::unique_ptr createTosaToLinalgOnTensors(); + +/// Populates passes to convert from TOSA to Linalg on buffers. At the end of +/// the pass, the function will only contain linalg ops or standard ops if the +/// pipeline succeeds. +void addTosaToLinalgOnTensorsPasses(OpPassManager &pm); + +/// Populates conversion passes from TOSA dialect to Linalg dialect. +void populateTosaToLinalgOnTensorsConversionPatterns( +MLIRContext *context, OwningRewritePatternList *patterns); + +} // namespace tosa +} // namespace mlir + +#endif // MLIR_CONVERSION_TOSATOLINALG_TOSATOLINALG_H diff --git a/mlir/lib/Conversion/CMakeLists.txt b/mlir/lib/Conversion/CMakeLists.txt index 421523267df2..9fc7a40d2d55 100644 --- a/mlir/lib/Conversion/CMakeLists.txt +++ b/mlir/lib/Conversion/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory(ShapeToStandard) add_subdirectory(SPIRVToLLVM) add_subdirectory(StandardToLLVM) add_subdirectory(StandardToSPIRV) +add_subdirectory(TosaToLinal
[llvm-branch-commits] [mlir] f75f391 - [MLIR][Linalg] Refactor transforms to use linalg::getDynOperands helper
Author: Rob Suderman Date: 2021-01-11T16:24:59-08:00 New Revision: f75f391fc68c125f908292bd7dcd6a413cfa591b URL: https://github.com/llvm/llvm-project/commit/f75f391fc68c125f908292bd7dcd6a413cfa591b DIFF: https://github.com/llvm/llvm-project/commit/f75f391fc68c125f908292bd7dcd6a413cfa591b.diff LOG: [MLIR][Linalg] Refactor transforms to use linalg::getDynOperands helper getDynOperands behavior is commonly used in a number of passes. Refactored to use a helper function and avoid code reuse. Differential Revision: https://reviews.llvm.org/D94340 Added: mlir/include/mlir/Dialect/StandardOps/Utils/Utils.h mlir/lib/Dialect/StandardOps/Utils/Utils.cpp Modified: mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp mlir/lib/Dialect/StandardOps/CMakeLists.txt mlir/lib/Transforms/BufferDeallocation.cpp mlir/lib/Transforms/PipelineDataTransfer.cpp Removed: diff --git a/mlir/include/mlir/Dialect/StandardOps/Utils/Utils.h b/mlir/include/mlir/Dialect/StandardOps/Utils/Utils.h new file mode 100644 index ..8d46d9bfd278 --- /dev/null +++ b/mlir/include/mlir/Dialect/StandardOps/Utils/Utils.h @@ -0,0 +1,32 @@ +//===- Utils.h - General transformation utilities ---*- C++ -*-===// +// +// 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 header file defines prototypes for various transformation utilities for +// the StandardOps dialect. These are not passes by themselves but are used +// either by passes, optimization sequences, or in turn by other transformation +// utilities. +// +//===--===// + +#ifndef MLIR_DIALECT_STANDARDOPS_UTILS_UTILS_H +#define MLIR_DIALECT_STANDARDOPS_UTILS_UTILS_H + +#include "mlir/IR/Value.h" + +namespace mlir { + +class Location; +class OpBuilder; + +/// Given an operation, retrieves the value of each dynamic dimension through +/// constructing the necessary DimOp operators. +SmallVector getDynOperands(Location loc, Value val, OpBuilder &b); + +} // end namespace mlir + +#endif // MLIR_DIALECT_STANDARDOPS_UTILS_UTILS_H diff --git a/mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp b/mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp index a3ab6f45b26e..5df7fe982281 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp @@ -13,6 +13,7 @@ #include "mlir/Dialect/Linalg/Transforms/Transforms.h" #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/StandardOps/Transforms/Passes.h" +#include "mlir/Dialect/StandardOps/Utils/Utils.h" #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/IR/BuiltinDialect.h" #include "mlir/IR/Operation.h" @@ -21,18 +22,6 @@ using namespace ::mlir; using namespace ::mlir::linalg; -static SmallVector getDynOperands(Location loc, Value val, -OpBuilder &b) { - SmallVector dynOperands; - auto shapedType = val.getType().cast(); - for (auto dim : llvm::enumerate(shapedType.getShape())) { -if (dim.value() == TensorType::kDynamicSize) { - dynOperands.push_back(b.create(loc, val, dim.index())); -} - } - return dynOperands; -} - static Value cloneMemref(Location loc, Value memref, OpBuilder &b) { auto memrefType = memref.getType().cast(); auto alloc = diff --git a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp index ada9f8c02b89..3012e920a82f 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp @@ -10,7 +10,9 @@ #include "PassDetail.h" #include "mlir/Dialect/Linalg/IR/LinalgOps.h" +#include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/StandardOps/IR/Ops.h" +#include "mlir/Dialect/StandardOps/Utils/Utils.h" #include "mlir/Transforms/DialectConversion.h" using namespace mlir; @@ -62,18 +64,9 @@ getOrCreateOperandsMatchingResultTypes(OpBuilder &b, Operation *op) { // Extract static / dynamic shape mix from the first operand. Value firstOperand = operands.front(); auto rankedTensorType = t.cast(); -SmallVector dynamicShape; -SmallVector staticShape; -dynamicShape.reserve(rankedTensorType.getRank()); -staticShape.reserve(rankedTensorType.getRank()); -unsigned idx = 0; -for (auto shape : rankedTensorType.getShape()) { - staticShape.push_back(shape); - if (rankedTensorType.isDynamicDim(idx)) -dynamicShape.push_back(b.create(loc, firstOperand, idx)); - ++idx; -} -// Crea