[Mlir-commits] [mlir] 7d9fc95 - [mlir][math] Add `math.absi` op

Jeff Niu llvmlistbot at llvm.org
Mon Aug 8 08:05:12 PDT 2022


Author: Jeff Niu
Date: 2022-08-08T11:05:01-04:00
New Revision: 7d9fc95b85c4ee421aa0c245f9ba5ed851db4154

URL: https://github.com/llvm/llvm-project/commit/7d9fc95b85c4ee421aa0c245f9ba5ed851db4154
DIFF: https://github.com/llvm/llvm-project/commit/7d9fc95b85c4ee421aa0c245f9ba5ed851db4154.diff

LOG: [mlir][math] Add `math.absi` op

Adds an integer absolute value op to the math dialect.

When converting to LLVM, this op is lowered to the LLVM `abs` intrinsic.
When converting to SPIRV, this op is lowered to `spv.GL.SAbs`.

Depends on D131325

Reviewed By: ftynse

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
    mlir/include/mlir/Dialect/Math/IR/MathOps.td
    mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
    mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
    mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
index c7e947b622fb4..937a2fa65ff5f 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
@@ -40,6 +40,7 @@ class LLVM_CountZerosIntrinsicOp<string func, list<Trait> traits = []> :
   let arguments = (ins LLVM_Type:$in, I<1>:$zero_undefined);
 }
 
+def LLVM_AbsOp : LLVM_UnaryIntrinsicOp<"abs">;
 def LLVM_CopySignOp : LLVM_BinarySameArgsIntrinsicOp<"copysign">;
 def LLVM_CosOp : LLVM_UnaryIntrinsicOp<"cos">;
 def LLVM_ExpOp : LLVM_UnaryIntrinsicOp<"exp">;

diff  --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index 27ab3a3b4656d..966a9a1241b13 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -86,6 +86,27 @@ def Math_AbsFOp : Math_FloatUnaryOp<"absf"> {
   let hasFolder = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// AbsIOp
+//===----------------------------------------------------------------------===//
+
+def Math_AbsIOp : Math_IntegerUnaryOp<"absi"> {
+  let summary = "integer absolute-value operation";
+  let description = [{
+    The `absi` operation computes the absolute value. It takes one operand of
+    integer type (i.e., scalar, tensor or vector) and returns one result of the
+    same type.
+
+    Example:
+
+    ```mlir
+    // Scalar absolute value.
+    %a = math.absi %b : i64
+    ```
+  }];
+  let hasFolder = 1;
+}
+
 //===----------------------------------------------------------------------===//
 // AtanOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
index 1ac4bbd7492e8..77fc91d308527 100644
--- a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
+++ b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
@@ -19,6 +19,7 @@ using namespace mlir;
 
 namespace {
 using AbsFOpLowering = VectorConvertToLLVMPattern<math::AbsFOp, LLVM::FAbsOp>;
+using AbsIOpLowering = VectorConvertToLLVMPattern<math::AbsIOp, LLVM::AbsOp>;
 using CeilOpLowering = VectorConvertToLLVMPattern<math::CeilOp, LLVM::FCeilOp>;
 using CopySignOpLowering =
     VectorConvertToLLVMPattern<math::CopySignOp, LLVM::CopySignOp>;
@@ -269,6 +270,7 @@ void mlir::populateMathToLLVMConversionPatterns(LLVMTypeConverter &converter,
   // clang-format off
   patterns.add<
     AbsFOpLowering,
+    AbsIOpLowering,
     CeilOpLowering,
     CopySignOpLowering,
     CosOpLowering,

diff  --git a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
index e53bd2b954947..55a242991bbcf 100644
--- a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
+++ b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
@@ -288,6 +288,7 @@ void populateMathToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
       .add<CountLeadingZerosPattern, Log1pOpPattern<spirv::GLLogOp>,
            ExpM1OpPattern<spirv::GLExpOp>, PowFOpPattern, RoundOpPattern,
            spirv::ElementwiseOpPattern<math::AbsFOp, spirv::GLFAbsOp>,
+           spirv::ElementwiseOpPattern<math::AbsIOp, spirv::GLSAbsOp>,
            spirv::ElementwiseOpPattern<math::CeilOp, spirv::GLCeilOp>,
            spirv::ElementwiseOpPattern<math::CosOp, spirv::GLCosOp>,
            spirv::ElementwiseOpPattern<math::ExpOp, spirv::GLExpOp>,

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 50d65127cd45a..e128435cefdc2 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -30,6 +30,15 @@ OpFoldResult math::AbsFOp::fold(ArrayRef<Attribute> operands) {
                                      [](const APFloat &a) { return abs(a); });
 }
 
+//===----------------------------------------------------------------------===//
+// AbsIOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::AbsIOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOp<IntegerAttr>(operands,
+                                       [](const APInt &a) { return a.abs(); });
+}
+
 //===----------------------------------------------------------------------===//
 // AtanOp folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
index 6378ea6475f25..fc91a55f4264b 100644
--- a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
+++ b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
@@ -2,14 +2,16 @@
 
 // CHECK-LABEL: @ops
 func.func @ops(%arg0: f32, %arg1: f32, %arg2: i32, %arg3: i32, %arg4: f64) {
-// CHECK: = "llvm.intr.exp"(%{{.*}}) : (f32) -> f32
-  %13 = math.exp %arg0 : f32
-// CHECK: = "llvm.intr.exp2"(%{{.*}}) : (f32) -> f32
-  %14 = math.exp2 %arg0 : f32
-// CHECK: = "llvm.intr.sqrt"(%{{.*}}) : (f32) -> f32
-  %19 = math.sqrt %arg0 : f32
-// CHECK: = "llvm.intr.sqrt"(%{{.*}}) : (f64) -> f64
-  %20 = math.sqrt %arg4 : f64
+  // CHECK: = "llvm.intr.exp"(%{{.*}}) : (f32) -> f32
+  %0 = math.exp %arg0 : f32
+  // CHECK: = "llvm.intr.exp2"(%{{.*}}) : (f32) -> f32
+  %1 = math.exp2 %arg0 : f32
+  // CHECK: = "llvm.intr.sqrt"(%{{.*}}) : (f32) -> f32
+  %2 = math.sqrt %arg0 : f32
+  // CHECK: = "llvm.intr.sqrt"(%{{.*}}) : (f64) -> f64
+  %3 = math.sqrt %arg4 : f64
+  // CHECK: = "llvm.intr.abs"(%{{.*}}) : (i32) -> i32
+  %4 = math.absi %arg2 : i32
   func.return
 }
 

diff  --git a/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir b/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
index 1302b47182ce1..662478aa30db6 100644
--- a/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
+++ b/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
@@ -79,6 +79,13 @@ func.func @float32_ternary_vector(%a: vector<4xf32>, %b: vector<4xf32>,
   return
 }
 
+// CHECK-LABEL: @int_unary
+func.func @int_unary(%arg0: i32) {
+  // CHECK: spv.GL.SAbs %{{.*}}
+  %0 = math.absi %arg0 : i32
+  return
+}
+
 // CHECK-LABEL: @ctlz_scalar
 //  CHECK-SAME: (%[[VAL:.+]]: i32)
 func.func @ctlz_scalar(%val: i32) -> i32 {


        


More information about the Mlir-commits mailing list