[mlir][tosa] Add lowering to tosa.abs for integer cases
authorRob Suderman <rob.suderman@gmail.com>
Mon, 3 May 2021 20:56:00 +0000 (13:56 -0700)
committerRob Suderman <rob.suderman@gmail.com>
Thu, 13 May 2021 20:55:17 +0000 (13:55 -0700)
Integer case requires decomposing to simple LLVM operatons.

Differential Revision: https://reviews.llvm.org/D101809

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir

index 3934690..06b23cc 100644 (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);
index 1e250c8..c2e6b07 100644 (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
 }