[Mlir-commits] [mlir] b80da04 - [mlir] Add FPToSIOp to Standard dialect.

Hanhan Wang llvmlistbot at llvm.org
Mon May 11 01:26:24 PDT 2020


Author: Hanhan Wang
Date: 2020-05-11T01:26:02-07:00
New Revision: b80da04b4457b45103357b1836426385b38ec974

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

LOG: [mlir] Add FPToSIOp to Standard dialect.

Summary:
Cast from a value interpreted as floating-point to the corresponding signed
integer value. Similar to an element-wise `static_cast` in C++, performs an
element-wise conversion operation.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
    mlir/lib/Dialect/StandardOps/IR/Ops.cpp
    mlir/test/IR/core-ops.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
index 354ff6a89e7c..ecd13171a2fb 100644
--- a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
+++ b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
@@ -1543,6 +1543,26 @@ def FPExtOp : CastOp<"fpext">, Arguments<(ins AnyType:$in)> {
   let hasFolder = 0;
 }
 
+//===----------------------------------------------------------------------===//
+// FPToSIOp
+//===----------------------------------------------------------------------===//
+
+def FPToSIOp : CastOp<"fptosi">, Arguments<(ins AnyType:$in)> {
+  let summary = "cast from floating-point type to integer type";
+  let description = [{
+    Cast from a value interpreted as floating-point to the nearest (rounding
+    towards zero) signed integer value.
+  }];
+
+  let extraClassDeclaration = [{
+    /// Return true if `a` and `b` are valid operand and result pairs for
+    /// the operation.
+    static bool areCastCompatible(Type a, Type b);
+  }];
+
+  let hasFolder = 0;
+}
+
 //===----------------------------------------------------------------------===//
 // FPTruncOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
index 269dd083542c..4e454a1e453b 100644
--- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
+++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
@@ -1661,6 +1661,14 @@ bool FPExtOp::areCastCompatible(Type a, Type b) {
   return false;
 }
 
+//===----------------------------------------------------------------------===//
+// FPToSIOp
+//===----------------------------------------------------------------------===//
+
+bool FPToSIOp::areCastCompatible(Type a, Type b) {
+  return a.isa<FloatType>() && b.isSignlessInteger();
+}
+
 //===----------------------------------------------------------------------===//
 // FPTruncOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/IR/core-ops.mlir b/mlir/test/IR/core-ops.mlir
index 21718864d94b..858460e4e8e1 100644
--- a/mlir/test/IR/core-ops.mlir
+++ b/mlir/test/IR/core-ops.mlir
@@ -545,6 +545,18 @@ func @standard_instrs(tensor<4x4x?xf32>, f32, i32, index, i64, f16) {
   // CHECK: %{{[0-9]+}} = sin %arg0 : tensor<4x4x?xf32>
   %149 = sin %t : tensor<4x4x?xf32>
 
+  // CHECK: = fptosi {{.*}} : f32 to i32
+  %159 = fptosi %f : f32 to i32
+
+  // CHECK: = fptosi {{.*}} : f32 to i64
+  %160 = fptosi %f : f32 to i64
+
+  // CHECK: = fptosi {{.*}} : f16 to i32
+  %161 = fptosi %half : f16 to i32
+
+  // CHECK: = fptosi {{.*}} : f16 to i64
+  %162 = fptosi %half : f16 to i64
+
   return
 }
 


        


More information about the Mlir-commits mailing list