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
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">;
}
//===----------------------------------------------------------------------===//
+// 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
//===----------------------------------------------------------------------===//
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>;
// clang-format off
patterns.add<
AbsFOpLowering,
+ AbsIOpLowering,
CeilOpLowering,
CopySignOpLowering,
CosOpLowering,
.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>,
}
//===----------------------------------------------------------------------===//
+// AbsIOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::AbsIOp::fold(ArrayRef<Attribute> operands) {
+ return constFoldUnaryOp<IntegerAttr>(operands,
+ [](const APInt &a) { return a.abs(); });
+}
+
+//===----------------------------------------------------------------------===//
// AtanOp folder
//===----------------------------------------------------------------------===//
// 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
}
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 {