[mlir][tosa] Add lowering to tosa.abs for integer cases

Integer case requires decomposing to simple LLVM operatons.

Differential Revision: https://reviews.llvm.org/D101809
This commit is contained in:
Rob Suderman 2021-05-03 13:56:00 -07:00
parent b47539a14d
commit f97d970a49
2 changed files with 16 additions and 0 deletions

View File

@ -102,6 +102,15 @@ createLinalgBodyCalculationForElementwiseOp(Operation *op, ValueRange args,
if (isa<tosa::AbsOp>(op) && elementTy.isa<FloatType>())
return rewriter.create<mlir::AbsFOp>(loc, resultTypes, args);
if (isa<tosa::AbsOp>(op) && elementTy.isa<IntegerType>()) {
auto zero =
rewriter.create<mlir::ConstantOp>(loc, rewriter.getZeroAttr(elementTy));
auto cmp =
rewriter.create<mlir::CmpIOp>(loc, CmpIPredicate::sgt, args[0], zero);
auto neg = rewriter.create<mlir::SubIOp>(loc, zero, args[0]);
return rewriter.create<mlir::SelectOp>(loc, cmp, args[0], neg);
}
// tosa::AddOp
if (isa<tosa::AddOp>(op) && elementTy.isa<FloatType>())
return rewriter.create<mlir::AddFOp>(loc, resultTypes, args);

View File

@ -400,6 +400,13 @@ func @test_simple_i32(%arg0: tensor<1xi32>) -> () {
// CHECK: sitofp
%24 = "tosa.cast"(%0) : (tensor<1xi32>) -> tensor<1xf32>
// CHECK: linalg.generic
// CHECK: constant 0
// CHECK: cmpi sgt
// CHECK: subi
// CHECK: select
%25 = "tosa.abs"(%arg0) : (tensor<1xi32>) -> tensor<1xi32>
return
}