xref: /llvm-project/mlir/lib/Dialect/Tosa/Utils/ConversionUtils.cpp (revision 78503e1a2f505ab4e7008d7df81eca76a546d203)
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/ConversionUtils.h"
14 
15 using namespace mlir;
16 using namespace mlir::tosa;
17 
18 SmallVector<utils::IteratorType>
19 mlir::tosa::getNParallelLoopsAttrs(unsigned nParallelLoops) {
20   return SmallVector<utils::IteratorType>(nParallelLoops,
21                                           utils::IteratorType::parallel);
22 }
23 
24 SmallVector<Value>
25 mlir::tosa::condenseValues(const SmallVector<Value> &values) {
26   SmallVector<Value> condensedValues;
27   for (auto value : values)
28     if (value)
29       condensedValues.push_back(value);
30   return condensedValues;
31 }
32 
33 Value mlir::tosa::clampFloatHelper(Location loc, Value arg, Value min,
34                                    Value max, OpBuilder &rewriter) {
35   Value minValue = rewriter.create<arith::MinFOp>(loc, arg, max);
36   return rewriter.create<arith::MaxFOp>(loc, minValue, min);
37 }
38 
39 Value mlir::tosa::clampIntHelper(Location loc, Value arg, Value min, Value max,
40                                  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