Skip to content

Commit 114ff99

Browse files
authored
[flang] Warn when F128 is unsupported (#102147)
This generates `warning: REAL(KIND=16) is not an enabled type for this target` if that type is used in a build not correctly configured to support this type. Uses of `selected_real_kind(30)` return -1.
1 parent 89bbcbe commit 114ff99

File tree

14 files changed

+112
-4
lines changed

14 files changed

+112
-4
lines changed

flang/include/flang/Tools/TargetSetup.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "flang/Evaluate/target.h"
1313
#include "llvm/Target/TargetMachine.h"
14+
#include <cfloat>
1415

1516
namespace Fortran::tools {
1617

@@ -21,9 +22,25 @@ namespace Fortran::tools {
2122

2223
const llvm::Triple &targetTriple{targetMachine.getTargetTriple()};
2324
// FIXME: Handle real(3) ?
24-
if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64)
25+
if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64) {
2526
targetCharacteristics.DisableType(
2627
Fortran::common::TypeCategory::Real, /*kind=*/10);
28+
}
29+
30+
// Figure out if we can support F128: see
31+
// flang/runtime/Float128Math/math-entries.h
32+
#ifdef FLANG_RUNTIME_F128_MATH_LIB
33+
// we can use libquadmath wrappers
34+
constexpr bool f128Support = true;
35+
#elif LDBL_MANT_DIG == 113
36+
// we can use libm wrappers
37+
constexpr bool f128Support = true;
38+
#else
39+
constexpr bool f128Support = false;
40+
#endif
41+
42+
if constexpr (!f128Support)
43+
targetCharacteristics.DisableType(Fortran::common::TypeCategory::Real, 16);
2744

2845
targetCharacteristics.set_compilerOptionsString(compilerOptions)
2946
.set_compilerVersionString(compilerVersion);

flang/module/ieee_arithmetic.f90

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,33 @@ end function ieee_round_ne
161161
G(1) G(2) G(4) G(8) G(16)
162162
#define SPECIFICS_L(G) \
163163
G(1) G(2) G(4) G(8)
164+
165+
#if FLANG_SUPPORT_R16
164166
#if __x86_64__
165167
#define SPECIFICS_R(G) \
166168
G(2) G(3) G(4) G(8) G(10) G(16)
167169
#else
168170
#define SPECIFICS_R(G) \
169171
G(2) G(3) G(4) G(8) G(16)
170172
#endif
173+
#else
174+
#if __x86_64__
175+
#define SPECIFICS_R(G) \
176+
G(2) G(3) G(4) G(8) G(10)
177+
#else
178+
#define SPECIFICS_R(G) \
179+
G(2) G(3) G(4) G(8)
180+
#endif
181+
#endif
182+
171183
#define SPECIFICS_II(G) \
172184
G(1,1) G(1,2) G(1,4) G(1,8) G(1,16) \
173185
G(2,1) G(2,2) G(2,4) G(2,8) G(2,16) \
174186
G(4,1) G(4,2) G(4,4) G(4,8) G(4,16) \
175187
G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \
176188
G(16,1) G(16,2) G(16,4) G(16,8) G(16,16)
189+
190+
#if FLANG_SUPPORT_R16
177191
#if __x86_64__
178192
#define SPECIFICS_RI(G) \
179193
G(2,1) G(2,2) G(2,4) G(2,8) G(2,16) \
@@ -190,7 +204,24 @@ end function ieee_round_ne
190204
G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \
191205
G(16,1) G(16,2) G(16,4) G(16,8) G(16,16)
192206
#endif
207+
#else
208+
#if __x86_64__
209+
#define SPECIFICS_RI(G) \
210+
G(2,1) G(2,2) G(2,4) G(2,8) \
211+
G(3,1) G(3,2) G(3,4) G(3,8) \
212+
G(4,1) G(4,2) G(4,4) G(4,8) \
213+
G(8,1) G(8,2) G(8,4) G(8,8) \
214+
G(10,1) G(10,2) G(10,4) G(10,8)
215+
#else
216+
#define SPECIFICS_RI(G) \
217+
G(2,1) G(2,2) G(2,4) G(2,8) \
218+
G(3,1) G(3,2) G(3,4) G(3,8) \
219+
G(4,1) G(4,2) G(4,4) G(4,8) \
220+
G(8,1) G(8,2) G(8,4) G(8,8)
221+
#endif
222+
#endif
193223

224+
#if FLANG_SUPPORT_R16
194225
#if __x86_64__
195226
#define SPECIFICS_RR(G) \
196227
G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) G(2,16) \
@@ -207,6 +238,22 @@ end function ieee_round_ne
207238
G(8,2) G(8,3) G(8,4) G(8,8) G(8,16) \
208239
G(16,2) G(16,3) G(16,4) G(16,8) G(16,16)
209240
#endif
241+
#else
242+
#if __x86_64__
243+
#define SPECIFICS_RR(G) \
244+
G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) \
245+
G(3,2) G(3,3) G(3,4) G(3,8) G(3,10) \
246+
G(4,2) G(4,3) G(4,4) G(4,8) G(4,10) \
247+
G(8,2) G(8,3) G(8,4) G(8,8) G(8,10) \
248+
G(10,2) G(10,3) G(10,4) G(10,8) G(10,10)
249+
#else
250+
#define SPECIFICS_RR(G) \
251+
G(2,2) G(2,3) G(2,4) G(2,8) \
252+
G(3,2) G(3,3) G(3,4) G(3,8) \
253+
G(4,2) G(4,3) G(4,4) G(4,8) \
254+
G(8,2) G(8,3) G(8,4) G(8,8)
255+
#endif
256+
#endif
210257

211258
#define IEEE_CLASS_R(XKIND) \
212259
elemental type(ieee_class_type) function ieee_class_a##XKIND(x); \
@@ -462,8 +509,10 @@ end function ieee_real_a##AKIND##_i##KKIND;
462509
interface ieee_real
463510
SPECIFICS_I(IEEE_REAL_I)
464511
SPECIFICS_R(IEEE_REAL_R)
512+
#if FLANG_SUPPORT_R16
465513
SPECIFICS_II(IEEE_REAL_II)
466514
SPECIFICS_RI(IEEE_REAL_RI)
515+
#endif
467516
end interface ieee_real
468517
public :: ieee_real
469518
#undef IEEE_REAL_I

flang/test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ llvm_canonicalize_cmake_booleans(
1111

1212
set(FLANG_TOOLS_DIR ${FLANG_BINARY_DIR}/bin)
1313

14+
# Check if 128-bit float computations can be done via long double
15+
check_cxx_source_compiles(
16+
"#include <cfloat>
17+
#if LDBL_MANT_DIG != 113
18+
#error LDBL_MANT_DIG != 113
19+
#endif
20+
int main() { return 0; }
21+
"
22+
HAVE_LDBL_MANT_DIG_113)
23+
1424
# FIXME In out-of-tree builds, "SHLIBDIR" is undefined and passing it to
1525
# `configure_lit_site_cfg` leads to a configuration error. This is currently
1626
# only required by plugins/examples, which are not supported in out-of-tree

flang/test/Evaluate/fold-out_of_range.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
! RUN: %python %S/test_folding.py %s %flang_fc1 -pedantic -triple x86_64-unknown-linux-gnu
22
! UNSUPPORTED: system-windows
33
! REQUIRES: target=x86_64{{.*}}
4+
! REQUIRES: flang-supports-f128-math
45
! Tests folding of OUT_OF_RANGE().
56
module m
67
integer(1), parameter :: i1v(*) = [ -huge(1_1) - 1_1, huge(1_1) ]

flang/test/Evaluate/folding07.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: %python %S/test_folding.py %s %flang_fc1
23
! Test numeric model inquiry intrinsics
34

flang/test/Lower/Intrinsics/ieee_class_queries.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc -emit-fir -o - %s | FileCheck %s
23

34
! CHECK-LABEL: func @_QQmain

flang/test/Lower/Intrinsics/ieee_unordered.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc -emit-fir -hlfir=false -o - %s | FileCheck %s
23

34
! CHECK-LABEL: func @_QQmain

flang/test/Lower/common-block.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc %s -o - | tco | FileCheck %s
23
! RUN: %flang -emit-llvm -S -mmlir -disable-external-name-interop %s -o - | FileCheck %s
34

@@ -78,4 +79,3 @@ subroutine s7()
7879
real(16) r16
7980
common /co1/ r16
8081
end subroutine
81-

flang/test/Semantics/kinds03.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: %python %S/test_symbols.py %s %flang_fc1
23
!DEF: /MainProgram1/ipdt DerivedType
34
!DEF: /MainProgram1/ipdt/k TypeParam INTEGER(4)

flang/test/Semantics/modfile26.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: %python %S/test_modfile.py %s %flang_fc1
23
! Intrinsics SELECTED_INT_KIND, SELECTED_REAL_KIND, PRECISION, RANGE,
34
! RADIX, DIGITS

flang/test/Semantics/realkinds-aarch64-01.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
! REQUIRES: aarch64-registered-target
2+
! REQUIRES: flang-supports-f128-math
23
! RUN: %python %S/test_modfile.py %s %flang_fc1 -triple aarch64-unknown-linux-gnu
34

45
module m1

flang/test/lit.cfg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@
216216
# Add features and substitutions to test F128 math support.
217217
# %f128-lib substitution may be used to generate check prefixes
218218
# for LIT tests checking for F128 library support.
219-
if config.flang_runtime_f128_math_lib:
219+
if config.flang_runtime_f128_math_lib or config.have_ldbl_mant_dig_113:
220220
config.available_features.add("flang-supports-f128-math")
221+
if config.flang_runtime_f128_math_lib:
221222
config.available_features.add(
222223
"flang-f128-math-lib-" + config.flang_runtime_f128_math_lib
223224
)

flang/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ if "openmp" in "@LLVM_ENABLE_RUNTIMES@".lower().split(";"):
3131
else:
3232
config.openmp_module_dir = None
3333
config.flang_runtime_f128_math_lib = "@FLANG_RUNTIME_F128_MATH_LIB@"
34+
config.have_ldbl_mant_dig_113 = "@HAVE_LDBL_MANT_DIG_113@"
3435

3536
import lit.llvm
3637
lit.llvm.initialize(lit_config, config)

flang/tools/f18/CMakeLists.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ set(MODULES_WITHOUT_IMPLEMENTATION
3131

3232
set(MODULES ${MODULES_WITH_IMPLEMENTATION} ${MODULES_WITHOUT_IMPLEMENTATION})
3333

34+
# Check if 128-bit float computations can be done via long double.
35+
check_cxx_source_compiles(
36+
"#include <cfloat>
37+
#if LDBL_MANT_DIG != 113
38+
#error LDBL_MANT_DIG != 113
39+
#endif
40+
int main() { return 0; }
41+
"
42+
HAVE_LDBL_MANT_DIG_113)
43+
44+
# Figure out whether we can support REAL(KIND=16)
45+
if (FLANG_RUNTIME_F128_MATH_LIB)
46+
set(FLANG_SUPPORT_R16 "1")
47+
elseif (HAVE_LDBL_MANT_DIG_113)
48+
set(FLANG_SUPPORT_R16 "1")
49+
else()
50+
set(FLANG_SUPPORT_R16 "0")
51+
endif()
52+
3453
# Init variable to hold extra object files coming from the Fortran modules;
3554
# these module files will be contributed from the CMakeLists in flang/tools/f18.
3655
set(module_objects "")
@@ -76,6 +95,10 @@ if (NOT CMAKE_CROSSCOMPILING)
7695
endif()
7796
endif()
7897

98+
set(decls "")
99+
if (FLANG_SUPPORT_R16)
100+
set(decls "-DFLANG_SUPPORT_R16")
101+
endif()
79102

80103
# Some modules have an implementation part that needs to be added to the
81104
# FortranRuntime library.
@@ -92,7 +115,7 @@ if (NOT CMAKE_CROSSCOMPILING)
92115
# TODO: We may need to flag this with conditional, in case Flang is built w/o OpenMP support
93116
add_custom_command(OUTPUT ${base}.mod ${object_output}
94117
COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
95-
COMMAND flang-new ${opts} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
118+
COMMAND flang-new ${opts} ${decls} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
96119
${FLANG_SOURCE_DIR}/module/${filename}.f90
97120
DEPENDS flang-new ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${FLANG_SOURCE_DIR}/module/__fortran_builtins.f90 ${depends}
98121
)

0 commit comments

Comments
 (0)