Skip to content

[flang] lower SPACING f16/bf16 to new runtime APIs #107541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

jeanPerier
Copy link
Contributor

Use APIs added in #106575

This is needed to fix HDF5 builds that are blocked by SPACING TODOs for REAL(2) and currently needs manual hacks.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Sep 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

Use APIs added in #106575

This is needed to fix HDF5 builds that are blocked by SPACING TODOs for REAL(2) and currently needs manual hacks.


Full diff: https://github.com/llvm/llvm-project/pull/107541.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Builder/Runtime/Numeric.cpp (+11-2)
  • (modified) flang/test/Lower/Intrinsics/spacing.f90 (+22-6)
diff --git a/flang/lib/Optimizer/Builder/Runtime/Numeric.cpp b/flang/lib/Optimizer/Builder/Runtime/Numeric.cpp
index d98288419d68f7..c13064a284d127 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Numeric.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Numeric.cpp
@@ -625,7 +625,11 @@ mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder,
                                      mlir::Location loc, mlir::Value x) {
   mlir::func::FuncOp func;
   mlir::Type fltTy = x.getType();
-
+  // TODO: for f16/bf16, there are better alternatives that do not require
+  // casting the argument (resp. result) to (resp. from) f32, but this requires
+  // knowing that the target runtime has been compiled with std::float16_t or
+  // std::bfloat16_t support, which is not an information available here for
+  // now.
   if (fltTy.isF32())
     func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing4)>(loc, builder);
   else if (fltTy.isF64())
@@ -634,6 +638,10 @@ mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder,
     func = fir::runtime::getRuntimeFunc<ForcedSpacing10>(loc, builder);
   else if (fltTy.isF128())
     func = fir::runtime::getRuntimeFunc<ForcedSpacing16>(loc, builder);
+  else if (fltTy.isF16())
+    func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing2By4)>(loc, builder);
+  else if (fltTy.isBF16())
+    func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing3By4)>(loc, builder);
   else
     fir::intrinsicTypeTODO(builder, fltTy, loc, "SPACING");
 
@@ -641,5 +649,6 @@ mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder,
   llvm::SmallVector<mlir::Value> args = {
       builder.createConvert(loc, funcTy.getInput(0), x)};
 
-  return builder.create<fir::CallOp>(loc, func, args).getResult(0);
+  mlir::Value res = builder.create<fir::CallOp>(loc, func, args).getResult(0);
+  return builder.createConvert(loc, fltTy, res);
 }
diff --git a/flang/test/Lower/Intrinsics/spacing.f90 b/flang/test/Lower/Intrinsics/spacing.f90
index 39b35ebd533abc..151f4e2a6d236e 100644
--- a/flang/test/Lower/Intrinsics/spacing.f90
+++ b/flang/test/Lower/Intrinsics/spacing.f90
@@ -1,20 +1,36 @@
-! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s
-! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
 
 ! CHECK-LABEL: func @_QPspacing_test(
-! CHECK-SAME: %[[x:[^:]+]]: !fir.ref<f32>{{.*}}) -> f32
 real*4 function spacing_test(x)
   real*4 :: x
   spacing_test = spacing(x)
-! CHECK: %[[a1:.*]] = fir.load %[[x]] : !fir.ref<f32>
+! CHECK: %[[a1:.*]] = fir.load %{{.*}} : !fir.ref<f32>
 ! CHECK: %{{.*}} = fir.call @_FortranASpacing4(%[[a1]]) {{.*}}: (f32) -> f32
 end function
 
 ! CHECK-LABEL: func @_QPspacing_test2(
-! CHECK-SAME: %[[x:[^:]+]]: !fir.ref<f80>{{.*}}) -> f80
 real*10 function spacing_test2(x)
   real*10 :: x
   spacing_test2 = spacing(x)
-! CHECK: %[[a1:.*]] = fir.load %[[x]] : !fir.ref<f80>
+! CHECK: %[[a1:.*]] = fir.load %{{.*}} : !fir.ref<f80>
 ! CHECK: %{{.*}} = fir.call @_FortranASpacing10(%[[a1]]) {{.*}}: (f80) -> f80
 end function
+
+! CHECK-LABEL: test_real2
+subroutine test_real2(x, y)
+  real(2) :: x, y
+  y = spacing(x)
+! CHECK: %[[CAST_ARG:.*]] = fir.convert %{{.*}} : (f16) -> f32
+! CHECK: %[[RT_RES:.*]] = fir.call @_FortranASpacing2By4(%[[CAST_ARG]]){{.*}}: (f32) -> f32
+! CHECK: fir.convert %[[RT_RES]] : (f32) -> f16
+end subroutine
+
+! CHECK-LABEL: test_real3
+subroutine test_real3(x, y)
+  real(3) :: x, y
+  y = spacing(x)
+! CHECK: %[[CAST_ARG:.*]] = fir.convert %{{.*}} : (bf16) -> f32
+! CHECK: %[[RT_RES:.*]] = fir.call @_FortranASpacing3By4(%[[CAST_ARG]]){{.*}}: (f32) -> f32
+! CHECK: fir.convert %[[RT_RES]] : (f32) -> bf16
+end subroutine

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jeanPerier jeanPerier merged commit 775f7f1 into llvm:main Sep 6, 2024
11 checks passed
@jeanPerier jeanPerier deleted the plug-real2-spacing branch September 6, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants