xref: /llvm-project/mlir/lib/Dialect/Tosa/Utils/ConversionUtils.cpp (revision 2eb50cee11ccbfac71eeb7687b9f136d95fc7f52)
1 //===- ConversionUtils.cpp ------------------------------------------------===//
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 // Utility functions for TOSA lowering
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Tosa/Utils/CoversionUtils.h"
14 
15 using namespace mlir;
16 using namespace mlir::tosa;
17 
18 SmallVector<StringRef>
19 mlir::tosa::getNParallelLoopsAttrs(unsigned nParallelLoops) {
20   return SmallVector<StringRef>(nParallelLoops, getParallelIteratorTypeName());
21 }
22 
23 SmallVector<Value>
24 mlir::tosa::condenseValues(const SmallVector<Value> &values) {
25   SmallVector<Value> condensedValues;
26   for (auto value : values)
27     if (value)
28       condensedValues.push_back(value);
29   return condensedValues;
30 }
31 
32 Value mlir::tosa::clampFloatHelper(Location loc, Value arg,
33                                    arith::ConstantOp min, arith::ConstantOp max,
34                                    OpBuilder &rewriter) {
35   Value minValue = rewriter.create<arith::MinFOp>(loc, arg, min);
36   return rewriter.create<arith::MaxFOp>(loc, minValue, max);
37 }
38 
39 Value mlir::tosa::clampIntHelper(Location loc, Value arg, arith::ConstantOp min,
40                                  arith::ConstantOp max, OpBuilder &rewriter) {
41   auto smallerThanMin =
42       rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, arg, min);
43   auto minOrArg =
44       rewriter.create<arith::SelectOp>(loc, smallerThanMin, min, arg);
45   auto largerThanMax =
46       rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, max, arg);
47   return rewriter.create<arith::SelectOp>(loc, largerThanMax, max, minOrArg);
48 }
49