[llvm] r245859 - [WebAssembly] Implement floating point rounding operators.

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 11:23:14 PDT 2015


Author: djg
Date: Mon Aug 24 13:23:13 2015
New Revision: 245859

URL: http://llvm.org/viewvc/llvm-project?rev=245859&view=rev
Log:
[WebAssembly] Implement floating point rounding operators.

Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
    llvm/trunk/test/CodeGen/WebAssembly/fp32.ll
    llvm/trunk/test/CodeGen/WebAssembly/fp64.ll

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp?rev=245859&r1=245858&r2=245859&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp Mon Aug 24 13:23:13 2015
@@ -120,10 +120,13 @@ WebAssemblyTargetLowering::WebAssemblyTa
                     ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
       setCondCodeAction(CC, T, Expand);
     // Expand floating-point library function operators.
-    for (auto Op : {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOWI, ISD::FPOW,
-                    ISD::FLOG, ISD::FLOG2, ISD::FLOG10, ISD::FEXP, ISD::FEXP2,
-                    ISD::FMINNAN, ISD::FMAXNAN})
+    for (auto Op : {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOWI, ISD::FPOW})
       setOperationAction(Op, T, Expand);
+    // Note supported floating-point library function operators that otherwise
+    // default to expand.
+    for (auto Op : {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT,
+                    ISD::FRINT})
+      setOperationAction(Op, T, Legal);
   }
 
   for (auto T : {MVT::i32, MVT::i64}) {

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td?rev=245859&r1=245858&r2=245859&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td Mon Aug 24 13:23:13 2015
@@ -16,17 +16,20 @@ defm FADD : BinaryFP<fadd>;
 defm FSUB : BinaryFP<fsub>;
 defm FMUL : BinaryFP<fmul>;
 defm FDIV : BinaryFP<fdiv>;
+defm SQRT : UnaryFP<fsqrt>;
+
 defm FABS : UnaryFP<fabs>;
 defm FNEG : UnaryFP<fneg>;
 defm COPYSIGN : BinaryFP<fcopysign>;
 
-/*
- * TODO(jfb): add and test these:
- * defm CEIL : UnaryFP<fceil>;
- * defm FLOOR : UnaryFP<ffloor>;
- * defm TRUNC : UnaryFP<ftrunc>;
- * defm NEARESTINT : UnaryFP<fnearbyint>;
- */
+defm CEIL : UnaryFP<fceil>;
+defm FLOOR : UnaryFP<ffloor>;
+defm TRUNC : UnaryFP<ftrunc>;
+defm NEARESTINT : UnaryFP<fnearbyint>;
+
+// WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
+def : Pat<(frint f32:$src), (NEARESTINT_F32 f32:$src)>;
+def : Pat<(frint f64:$src), (NEARESTINT_F64 f64:$src)>;
 
 defm EQ : ComparisonFP<SETOEQ>;
 defm NE : ComparisonFP<SETUNE>;
@@ -49,8 +52,6 @@ def : Pat<(setle f64:$lhs, f64:$rhs), (L
 def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
 def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
 
-defm SQRT : UnaryFP<fsqrt>;
-
 /*
  * TODO(jfb): Add the following for 32-bit and 64-bit.
  *

Modified: llvm/trunk/test/CodeGen/WebAssembly/fp32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/fp32.ll?rev=245859&r1=245858&r2=245859&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/fp32.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/fp32.ll Mon Aug 24 13:23:13 2015
@@ -8,6 +8,11 @@ target triple = "wasm32-unknown-unknown"
 declare float @llvm.fabs.f32(float)
 declare float @llvm.copysign.f32(float, float)
 declare float @llvm.sqrt.f32(float)
+declare float @llvm.ceil.f32(float)
+declare float @llvm.floor.f32(float)
+declare float @llvm.trunc.f32(float)
+declare float @llvm.nearbyint.f32(float)
+declare float @llvm.rint.f32(float)
 
 ; CHECK-LABEL: fadd32:
 ; CHECK-NEXT: (setlocal @0 (argument 1))
@@ -67,3 +72,38 @@ define float @sqrt32(float %x) {
   %a = call float @llvm.sqrt.f32(float %x)
   ret float %a
 }
+
+; CHECK-LABEL: ceil32:
+; CHECK: (setlocal @1 (ceil @0))
+define float @ceil32(float %x) {
+  %a = call float @llvm.ceil.f32(float %x)
+  ret float %a
+}
+
+; CHECK-LABEL: floor32:
+; CHECK: (setlocal @1 (floor @0))
+define float @floor32(float %x) {
+  %a = call float @llvm.floor.f32(float %x)
+  ret float %a
+}
+
+; CHECK-LABEL: trunc32:
+; CHECK: (setlocal @1 (trunc @0))
+define float @trunc32(float %x) {
+  %a = call float @llvm.trunc.f32(float %x)
+  ret float %a
+}
+
+; CHECK-LABEL: nearestint32:
+; CHECK: (setlocal @1 (nearestint @0))
+define float @nearestint32(float %x) {
+  %a = call float @llvm.nearbyint.f32(float %x)
+  ret float %a
+}
+
+; CHECK-LABEL: nearestint32_via_rint:
+; CHECK: (setlocal @1 (nearestint @0))
+define float @nearestint32_via_rint(float %x) {
+  %a = call float @llvm.rint.f32(float %x)
+  ret float %a
+}

Modified: llvm/trunk/test/CodeGen/WebAssembly/fp64.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/fp64.ll?rev=245859&r1=245858&r2=245859&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/fp64.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/fp64.ll Mon Aug 24 13:23:13 2015
@@ -8,6 +8,11 @@ target triple = "wasm32-unknown-unknown"
 declare double @llvm.fabs.f64(double)
 declare double @llvm.copysign.f64(double, double)
 declare double @llvm.sqrt.f64(double)
+declare double @llvm.ceil.f64(double)
+declare double @llvm.floor.f64(double)
+declare double @llvm.trunc.f64(double)
+declare double @llvm.nearbyint.f64(double)
+declare double @llvm.rint.f64(double)
 
 ; CHECK-LABEL: fadd64:
 ; CHECK-NEXT: (setlocal @0 (argument 1))
@@ -67,3 +72,38 @@ define double @sqrt64(double %x) {
   %a = call double @llvm.sqrt.f64(double %x)
   ret double %a
 }
+
+; CHECK-LABEL: ceil64:
+; CHECK: (setlocal @1 (ceil @0))
+define double @ceil64(double %x) {
+  %a = call double @llvm.ceil.f64(double %x)
+  ret double %a
+}
+
+; CHECK-LABEL: floor64:
+; CHECK: (setlocal @1 (floor @0))
+define double @floor64(double %x) {
+  %a = call double @llvm.floor.f64(double %x)
+  ret double %a
+}
+
+; CHECK-LABEL: trunc64:
+; CHECK: (setlocal @1 (trunc @0))
+define double @trunc64(double %x) {
+  %a = call double @llvm.trunc.f64(double %x)
+  ret double %a
+}
+
+; CHECK-LABEL: nearestint64:
+; CHECK: (setlocal @1 (nearestint @0))
+define double @nearestint64(double %x) {
+  %a = call double @llvm.nearbyint.f64(double %x)
+  ret double %a
+}
+
+; CHECK-LABEL: nearestint64_via_rint:
+; CHECK: (setlocal @1 (nearestint @0))
+define double @nearestint64_via_rint(double %x) {
+  %a = call double @llvm.rint.f64(double %x)
+  ret double %a
+}




More information about the llvm-commits mailing list