[flang-commits] [flang] 0a0b302 - [flang] Enable scalar real type in lowering

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Mon Feb 14 23:23:42 PST 2022


Author: Valentin Clement
Date: 2022-02-15T08:23:32+01:00
New Revision: 0a0b3029deb2c92e4109b9e4034e10b09145c878

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

LOG: [flang] Enable scalar real type in lowering

This patch enables scalar real type in lowering.
It is tested on function return types.

This patch is part of the upstreaming effort from fir-dev branch.

Depends on D119698

Reviewed By: kiranchandramohan

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

Co-authored-by: Jean Perier <jperier at nvidia.com>

Added: 
    

Modified: 
    flang/lib/Lower/ConvertType.cpp
    flang/test/Lower/basic-function.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertType.cpp b/flang/lib/Lower/ConvertType.cpp
index ca3494704e32a..91d8076acff01 100644
--- a/flang/lib/Lower/ConvertType.cpp
+++ b/flang/lib/Lower/ConvertType.cpp
@@ -23,6 +23,27 @@
 // Intrinsic type translation helpers
 //===--------------------------------------------------------------------===//
 
+static mlir::Type genRealType(mlir::MLIRContext *context, int kind) {
+  if (Fortran::evaluate::IsValidKindOfIntrinsicType(
+          Fortran::common::TypeCategory::Real, kind)) {
+    switch (kind) {
+    case 2:
+      return mlir::FloatType::getF16(context);
+    case 3:
+      return mlir::FloatType::getBF16(context);
+    case 4:
+      return mlir::FloatType::getF32(context);
+    case 8:
+      return mlir::FloatType::getF64(context);
+    case 10:
+      return mlir::FloatType::getF80(context);
+    case 16:
+      return mlir::FloatType::getF128(context);
+    }
+  }
+  llvm_unreachable("REAL type translation not implemented");
+}
+
 template <int KIND>
 int getIntegerBits() {
   return Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer,
@@ -58,7 +79,7 @@ static mlir::Type genFIRType(mlir::MLIRContext *context,
                              Fortran::common::TypeCategory tc, int kind) {
   switch (tc) {
   case Fortran::common::TypeCategory::Real:
-    TODO_NOLOC("genFIRType Real");
+    return genRealType(context, kind);
   case Fortran::common::TypeCategory::Integer:
     return genIntegerType(context, kind);
   case Fortran::common::TypeCategory::Complex:
@@ -107,66 +128,6 @@ mlir::Type genFIRType(mlir::MLIRContext *context) {
   }
 }
 
-template <>
-mlir::Type
-genFIRType<Fortran::common::TypeCategory::Real, 2>(mlir::MLIRContext *context) {
-  return mlir::FloatType::getF16(context);
-}
-
-template <>
-mlir::Type
-genFIRType<Fortran::common::TypeCategory::Real, 3>(mlir::MLIRContext *context) {
-  return mlir::FloatType::getBF16(context);
-}
-
-template <>
-mlir::Type
-genFIRType<Fortran::common::TypeCategory::Real, 4>(mlir::MLIRContext *context) {
-  return mlir::FloatType::getF32(context);
-}
-
-template <>
-mlir::Type
-genFIRType<Fortran::common::TypeCategory::Real, 8>(mlir::MLIRContext *context) {
-  return mlir::FloatType::getF64(context);
-}
-
-template <>
-mlir::Type genFIRType<Fortran::common::TypeCategory::Real, 10>(
-    mlir::MLIRContext *context) {
-  return fir::RealType::get(context, 10);
-}
-
-template <>
-mlir::Type genFIRType<Fortran::common::TypeCategory::Real, 16>(
-    mlir::MLIRContext *context) {
-  return fir::RealType::get(context, 16);
-}
-
-template <>
-mlir::Type
-genFIRType<Fortran::common::TypeCategory::Real>(mlir::MLIRContext *context,
-                                                int kind) {
-  if (Fortran::evaluate::IsValidKindOfIntrinsicType(
-          Fortran::common::TypeCategory::Real, kind)) {
-    switch (kind) {
-    case 2:
-      return genFIRType<Fortran::common::TypeCategory::Real, 2>(context);
-    case 3:
-      return genFIRType<Fortran::common::TypeCategory::Real, 3>(context);
-    case 4:
-      return genFIRType<Fortran::common::TypeCategory::Real, 4>(context);
-    case 8:
-      return genFIRType<Fortran::common::TypeCategory::Real, 8>(context);
-    case 10:
-      return genFIRType<Fortran::common::TypeCategory::Real, 10>(context);
-    case 16:
-      return genFIRType<Fortran::common::TypeCategory::Real, 16>(context);
-    }
-  }
-  llvm_unreachable("REAL type translation not implemented");
-}
-
 template <>
 mlir::Type
 genFIRType<Fortran::common::TypeCategory::Character>(mlir::MLIRContext *context,

diff  --git a/flang/test/Lower/basic-function.f90 b/flang/test/Lower/basic-function.f90
index e95ed88774455..683a2fa65ce9c 100644
--- a/flang/test/Lower/basic-function.f90
+++ b/flang/test/Lower/basic-function.f90
@@ -67,3 +67,34 @@ logical(8) function lfct4()
 end
 ! CHECK-LABEL: func @_QPlfct4() -> !fir.logical<8>
 ! CHECK:         return %{{.*}} : !fir.logical<8>
+
+real(2) function rfct1()
+end
+! CHECK-LABEL: func @_QPrfct1() -> f16
+! CHECK:         return %{{.*}} : f16
+
+real(3) function rfct2()
+end
+! CHECK-LABEL: func @_QPrfct2() -> bf16
+! CHECK:         return %{{.*}} : bf16
+
+real function rfct3()
+end
+! CHECK-LABEL: func @_QPrfct3() -> f32
+! CHECK:         return %{{.*}} : f32
+
+real(8) function rfct4()
+end
+! CHECK-LABEL: func @_QPrfct4() -> f64
+! CHECK:         return %{{.*}} : f64
+
+real(10) function rfct5()
+end
+! CHECK-LABEL: func @_QPrfct5() -> f80
+! CHECK:         return %{{.*}} : f80
+
+real(16) function rfct6()
+end
+! CHECK-LABEL: func @_QPrfct6() -> f128
+! CHECK:         return %{{.*}} : f128
+


        


More information about the flang-commits mailing list