Skip to content

Commit 775f7f1

Browse files
authored
[flang] lower SPACING f16/bf16 to new runtime APIs (#107541)
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.
1 parent c9bdc25 commit 775f7f1

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

flang/lib/Optimizer/Builder/Runtime/Numeric.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,11 @@ mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder,
625625
mlir::Location loc, mlir::Value x) {
626626
mlir::func::FuncOp func;
627627
mlir::Type fltTy = x.getType();
628-
628+
// TODO: for f16/bf16, there are better alternatives that do not require
629+
// casting the argument (resp. result) to (resp. from) f32, but this requires
630+
// knowing that the target runtime has been compiled with std::float16_t or
631+
// std::bfloat16_t support, which is not an information available here for
632+
// now.
629633
if (fltTy.isF32())
630634
func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing4)>(loc, builder);
631635
else if (fltTy.isF64())
@@ -634,12 +638,17 @@ mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder,
634638
func = fir::runtime::getRuntimeFunc<ForcedSpacing10>(loc, builder);
635639
else if (fltTy.isF128())
636640
func = fir::runtime::getRuntimeFunc<ForcedSpacing16>(loc, builder);
641+
else if (fltTy.isF16())
642+
func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing2By4)>(loc, builder);
643+
else if (fltTy.isBF16())
644+
func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing3By4)>(loc, builder);
637645
else
638646
fir::intrinsicTypeTODO(builder, fltTy, loc, "SPACING");
639647

640648
auto funcTy = func.getFunctionType();
641649
llvm::SmallVector<mlir::Value> args = {
642650
builder.createConvert(loc, funcTy.getInput(0), x)};
643651

644-
return builder.create<fir::CallOp>(loc, func, args).getResult(0);
652+
mlir::Value res = builder.create<fir::CallOp>(loc, func, args).getResult(0);
653+
return builder.createConvert(loc, fltTy, res);
645654
}
+22-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s
2-
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
33

44
! CHECK-LABEL: func @_QPspacing_test(
5-
! CHECK-SAME: %[[x:[^:]+]]: !fir.ref<f32>{{.*}}) -> f32
65
real*4 function spacing_test(x)
76
real*4 :: x
87
spacing_test = spacing(x)
9-
! CHECK: %[[a1:.*]] = fir.load %[[x]] : !fir.ref<f32>
8+
! CHECK: %[[a1:.*]] = fir.load %{{.*}} : !fir.ref<f32>
109
! CHECK: %{{.*}} = fir.call @_FortranASpacing4(%[[a1]]) {{.*}}: (f32) -> f32
1110
end function
1211

1312
! CHECK-LABEL: func @_QPspacing_test2(
14-
! CHECK-SAME: %[[x:[^:]+]]: !fir.ref<f80>{{.*}}) -> f80
1513
real*10 function spacing_test2(x)
1614
real*10 :: x
1715
spacing_test2 = spacing(x)
18-
! CHECK: %[[a1:.*]] = fir.load %[[x]] : !fir.ref<f80>
16+
! CHECK: %[[a1:.*]] = fir.load %{{.*}} : !fir.ref<f80>
1917
! CHECK: %{{.*}} = fir.call @_FortranASpacing10(%[[a1]]) {{.*}}: (f80) -> f80
2018
end function
19+
20+
! CHECK-LABEL: test_real2
21+
subroutine test_real2(x, y)
22+
real(2) :: x, y
23+
y = spacing(x)
24+
! CHECK: %[[CAST_ARG:.*]] = fir.convert %{{.*}} : (f16) -> f32
25+
! CHECK: %[[RT_RES:.*]] = fir.call @_FortranASpacing2By4(%[[CAST_ARG]]){{.*}}: (f32) -> f32
26+
! CHECK: fir.convert %[[RT_RES]] : (f32) -> f16
27+
end subroutine
28+
29+
! CHECK-LABEL: test_real3
30+
subroutine test_real3(x, y)
31+
real(3) :: x, y
32+
y = spacing(x)
33+
! CHECK: %[[CAST_ARG:.*]] = fir.convert %{{.*}} : (bf16) -> f32
34+
! CHECK: %[[RT_RES:.*]] = fir.call @_FortranASpacing3By4(%[[CAST_ARG]]){{.*}}: (f32) -> f32
35+
! CHECK: fir.convert %[[RT_RES]] : (f32) -> bf16
36+
end subroutine

0 commit comments

Comments
 (0)