[flang-commits] [flang] [draft][flang] Query backend support for quad-precision compilation decision (PR #182230)
Shunsuke Watanabe via flang-commits
flang-commits at lists.llvm.org
Mon Mar 9 02:27:59 PDT 2026
https://github.com/s-watanabe314 updated https://github.com/llvm/llvm-project/pull/182230
>From 35dc6ae68b02da85f5597da3ea0d8a78a0a9bc68 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Thu, 12 Feb 2026 17:08:41 +0900
Subject: [PATCH 1/5] [flang] Query backend support for quad-precision
compilation decision
---
flang/include/flang/Tools/TargetSetup.h | 59 ++++++++++++++++++++-----
1 file changed, 48 insertions(+), 11 deletions(-)
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index 002e82aa72484..1e4c1884a362a 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -12,6 +12,10 @@
#include "flang/Common/float128.h"
#include "flang/Evaluate/target.h"
#include "flang/Frontend/TargetOptions.h"
+#include "llvm/CodeGen/TargetLowering.h"
+#include "llvm/CodeGen/ValueTypes.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Module.h"
#include "llvm/Target/TargetMachine.h"
#include <cfloat>
@@ -58,17 +62,50 @@ namespace Fortran::tools {
break;
}
- // Check for kind=16 support. See flang/runtime/Float128Math/math-entries.h.
- // TODO: Take this from TargetInfo::getLongDoubleFormat for cross compilation.
-#ifdef FLANG_RUNTIME_F128_MATH_LIB
- constexpr bool f128Support = true; // use libquadmath wrappers
-#elif HAS_LDBL128
- constexpr bool f128Support = true; // use libm wrappers
-#else
- constexpr bool f128Support = false;
-#endif
-
- if constexpr (!f128Support) {
+ bool f128Support = false;
+ llvm::LLVMContext ctx;
+ std::unique_ptr<llvm::Module> dummyModule =
+ std::make_unique<llvm::Module>("quad-test", ctx);
+ dummyModule->setTargetTriple(targetMachine.getTargetTriple());
+ dummyModule->setDataLayout(targetMachine.createDataLayout());
+
+ llvm::FunctionType *dummyFTy =
+ llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), false);
+ llvm::Function *dummyF = llvm::Function::Create(dummyFTy,
+ llvm::GlobalValue::ExternalLinkage, "quad-test", dummyModule.get());
+
+ const llvm::TargetLowering *dummyTLI =
+ targetMachine.getSubtargetImpl(*dummyF)->getTargetLowering();
+
+ if (dummyTLI) {
+ llvm::EVT fp128EVT = llvm::EVT::getEVT(llvm::Type::getFP128Ty(ctx));
+
+ // Query for fp128 backend support. Based on this, determine whether
+ // compilation is possible on the frontend.
+ bool isLegal = dummyTLI->isTypeLegal(fp128EVT);
+
+ // We might also be able to determine fp128 backend support based on the
+ // LegalizeAction value. This is likely when the value is "Legal" or
+ // "LibCall". See
+ // https://llvm.org/doxygen/TargetLowering_8h_source.html#l00202.
+ llvm::TargetLowering::LegalizeAction LA =
+ dummyTLI->getOperationAction(llvm::ISD::FADD, fp128EVT);
+
+ // We might also be able to determine fp128 backend support based on the
+ // LegalizeTypeAction value. This is likely when the value is "TypeLegal".
+ // See https://llvm.org/doxygen/TargetLowering_8h_source.html#l00212.
+ llvm::TargetLowering::LegalizeTypeAction LTA =
+ dummyTLI->getTypeConversion(ctx, llvm::MVT::f128).first;
+
+ if (isLegal &&
+ (LA == llvm::TargetLowering::Legal ||
+ LA == llvm::TargetLowering::LibCall) &&
+ (LTA == llvm::TargetLowering::TypeLegal)) {
+ f128Support = true;
+ }
+ }
+
+ if (!f128Support) {
targetCharacteristics.DisableType(Fortran::common::TypeCategory::Real, 16);
targetCharacteristics.DisableType(
Fortran::common::TypeCategory::Complex, 16);
>From ec02d120ddd48d89da8720fa7194ad0c72ca78ed Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Wed, 4 Mar 2026 17:39:51 +0900
Subject: [PATCH 2/5] Confirm flang's quad-precision support and set lit config
---
flang/test/lit.cfg.py | 22 +++++++++++++++++++++-
flang/test/lit.site.cfg.py.in | 1 +
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index 3a87f9ea06803..63e0296731d4f 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -205,10 +205,30 @@
openmp_flags_substitution += f" -J {config.openmp_module_dir}"
config.substitutions.append(("%openmp_flags", openmp_flags_substitution))
+def flang_supports_f128():
+ flang_exe = lit.util.which("flang", config.clang_tools_dir)
+
+ if not flang_exe:
+ return False
+
+ try:
+ testcode = b"real(16) :: x\nend"
+ flang_cmd = subprocess.run(
+ [flang_exe, "-fsyntax-only", "-"],
+ input=testcode,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
+ except OSError:
+ return False
+
+ if flang_cmd.returncode == 0:
+ return True
+
# Add features and substitutions to test F128 math support.
# %f128-lib substitution may be used to generate check prefixes
# for LIT tests checking for F128 library support.
-if config.flang_runtime_f128_math_lib or config.have_ldbl_mant_dig_113:
+if config.flang_runtime_f128_math_lib or config.have_ldbl_mant_dig_113 or flang_supports_f128():
config.available_features.add("flang-supports-f128-math")
if config.flang_runtime_f128_math_lib:
config.available_features.add(
diff --git a/flang/test/lit.site.cfg.py.in b/flang/test/lit.site.cfg.py.in
index 2b66dd64b8c13..0739fa658057d 100644
--- a/flang/test/lit.site.cfg.py.in
+++ b/flang/test/lit.site.cfg.py.in
@@ -12,6 +12,7 @@ config.llvm_target_triple_env = "@LLVM_TARGET_TRIPLE_ENV@"
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
config.errc_messages = "@LLVM_LIT_ERRC_MESSAGES@"
config.flang_obj_root = "@FLANG_BINARY_DIR@"
+config.clang_tools_dir = lit_config.substitute(path(r"@CURRENT_TOOLS_DIR@"))
config.flang_tools_dir = lit_config.substitute("@FLANG_TOOLS_DIR@")
config.flang_intrinsic_modules_dir = "@FLANG_INTRINSIC_MODULES_DIR@"
config.flang_headers_dir = "@HEADER_BINARY_DIR@"
>From 6ef055b946c7e93ec0f7fd391c998ba34ad69546 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Mon, 9 Mar 2026 13:32:29 +0900
Subject: [PATCH 3/5] format python code
---
flang/test/lit.cfg.py | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index 63e0296731d4f..eca638a7f230f 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -205,6 +205,7 @@
openmp_flags_substitution += f" -J {config.openmp_module_dir}"
config.substitutions.append(("%openmp_flags", openmp_flags_substitution))
+
def flang_supports_f128():
flang_exe = lit.util.which("flang", config.clang_tools_dir)
@@ -225,10 +226,15 @@ def flang_supports_f128():
if flang_cmd.returncode == 0:
return True
+
# Add features and substitutions to test F128 math support.
# %f128-lib substitution may be used to generate check prefixes
# for LIT tests checking for F128 library support.
-if config.flang_runtime_f128_math_lib or config.have_ldbl_mant_dig_113 or flang_supports_f128():
+if (
+ config.flang_runtime_f128_math_lib
+ or config.have_ldbl_mant_dig_113
+ or flang_supports_f128()
+):
config.available_features.add("flang-supports-f128-math")
if config.flang_runtime_f128_math_lib:
config.available_features.add(
>From 3a1da59827b8b14b593775ee5f1f18289cb8f058 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Mon, 9 Mar 2026 17:32:37 +0900
Subject: [PATCH 4/5] Determine f128Support solely by isTypeLegal
---
flang/include/flang/Tools/TargetSetup.h | 22 +---------------------
1 file changed, 1 insertion(+), 21 deletions(-)
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index 1e4c1884a362a..b13cb19f30f29 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -82,27 +82,7 @@ namespace Fortran::tools {
// Query for fp128 backend support. Based on this, determine whether
// compilation is possible on the frontend.
- bool isLegal = dummyTLI->isTypeLegal(fp128EVT);
-
- // We might also be able to determine fp128 backend support based on the
- // LegalizeAction value. This is likely when the value is "Legal" or
- // "LibCall". See
- // https://llvm.org/doxygen/TargetLowering_8h_source.html#l00202.
- llvm::TargetLowering::LegalizeAction LA =
- dummyTLI->getOperationAction(llvm::ISD::FADD, fp128EVT);
-
- // We might also be able to determine fp128 backend support based on the
- // LegalizeTypeAction value. This is likely when the value is "TypeLegal".
- // See https://llvm.org/doxygen/TargetLowering_8h_source.html#l00212.
- llvm::TargetLowering::LegalizeTypeAction LTA =
- dummyTLI->getTypeConversion(ctx, llvm::MVT::f128).first;
-
- if (isLegal &&
- (LA == llvm::TargetLowering::Legal ||
- LA == llvm::TargetLowering::LibCall) &&
- (LTA == llvm::TargetLowering::TypeLegal)) {
- f128Support = true;
- }
+ f128Support = dummyTLI->isTypeLegal(fp128EVT);
}
if (!f128Support) {
>From f334b0567149509746ad235ffa9890d79d2bd3b8 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Mon, 9 Mar 2026 18:13:34 +0900
Subject: [PATCH 5/5] Always build real(16) for module files regardless of the
target
---
flang/module/ieee_arithmetic.f90 | 55 --------------------------------
flang/tools/f18/CMakeLists.txt | 14 +-------
2 files changed, 1 insertion(+), 68 deletions(-)
diff --git a/flang/module/ieee_arithmetic.f90 b/flang/module/ieee_arithmetic.f90
index 4e938a2daaa91..df84d83cc89e4 100644
--- a/flang/module/ieee_arithmetic.f90
+++ b/flang/module/ieee_arithmetic.f90
@@ -171,7 +171,6 @@ end function ieee_round_ne
#define SPECIFICS_L(G) \
G(1) G(2) G(4) G(8)
-#if FLANG_SUPPORT_R16
#if __x86_64__
#define SPECIFICS_R(G) \
G(2) G(3) G(4) G(8) G(10) G(16)
@@ -179,15 +178,6 @@ end function ieee_round_ne
#define SPECIFICS_R(G) \
G(2) G(3) G(4) G(8) G(16)
#endif
-#else
-#if __x86_64__
-#define SPECIFICS_R(G) \
- G(2) G(3) G(4) G(8) G(10)
-#else
-#define SPECIFICS_R(G) \
- G(2) G(3) G(4) G(8)
-#endif
-#endif
#define SPECIFICS_II(G) \
G(1,1) G(1,2) G(1,4) G(1,8) G(1,16) \
@@ -196,7 +186,6 @@ end function ieee_round_ne
G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \
G(16,1) G(16,2) G(16,4) G(16,8) G(16,16)
-#if FLANG_SUPPORT_R16
#if __x86_64__
#define SPECIFICS_RI(G) \
G(2,1) G(2,2) G(2,4) G(2,8) G(2,16) \
@@ -213,24 +202,7 @@ end function ieee_round_ne
G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \
G(16,1) G(16,2) G(16,4) G(16,8) G(16,16)
#endif
-#else
-#if __x86_64__
-#define SPECIFICS_RI(G) \
- G(2,1) G(2,2) G(2,4) G(2,8) \
- G(3,1) G(3,2) G(3,4) G(3,8) \
- G(4,1) G(4,2) G(4,4) G(4,8) \
- G(8,1) G(8,2) G(8,4) G(8,8) \
- G(10,1) G(10,2) G(10,4) G(10,8)
-#else
-#define SPECIFICS_RI(G) \
- G(2,1) G(2,2) G(2,4) G(2,8) \
- G(3,1) G(3,2) G(3,4) G(3,8) \
- G(4,1) G(4,2) G(4,4) G(4,8) \
- G(8,1) G(8,2) G(8,4) G(8,8)
-#endif
-#endif
-#if FLANG_SUPPORT_R16
#if __x86_64__
#define SPECIFICS_RR(G) \
G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) G(2,16) \
@@ -260,33 +232,6 @@ end function ieee_round_ne
G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8) G(16,8,16) \
G(16,16,2) G(16,16,3) G(16,16,4) G(16,16,8) G(16,16,16)
#endif
-#else
-#if __x86_64__
-#define SPECIFICS_RR(G) \
- G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) \
- G(3,2) G(3,3) G(3,4) G(3,8) G(3,10) \
- G(4,2) G(4,3) G(4,4) G(4,8) G(4,10) \
- G(8,2) G(8,3) G(8,4) G(8,8) G(8,10) \
- G(10,2) G(10,3) G(10,4) G(10,8) G(10,10)
-#define SPECIFICS_rRR(G) \
- G(2,2,2) G(2,2,3) G(4,2,4) G(8,2,8) G(10,2,10) \
- G(2,3,2) G(3,3,3) G(4,3,4) G(8,3,8) G(10,3,10) \
- G(4,4,2) G(4,4,3) G(4,4,4) G(8,4,8) G(10,4,10) \
- G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8) G(10,8,10) \
- G(10,10,2) G(10,10,3) G(10,10,4) G(10,10,8) G(10,10,10)
-#else
-#define SPECIFICS_RR(G) \
- G(2,2) G(2,3) G(2,4) G(2,8) \
- G(3,2) G(3,3) G(3,4) G(3,8) \
- G(4,2) G(4,3) G(4,4) G(4,8) \
- G(8,2) G(8,3) G(8,4) G(8,8)
-#define SPECIFICS_rRR(G) \
- G(2,2,2) G(2,2,3) G(4,2,4) G(8,2,8) \
- G(2,3,2) G(3,3,3) G(4,3,4) G(8,3,8) \
- G(4,4,2) G(4,4,3) G(4,4,4) G(8,4,8) \
- G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8)
-#endif
-#endif
#define IEEE_CLASS_R(XKIND) \
elemental type(ieee_class_type) function ieee_class_a##XKIND(x); \
diff --git a/flang/tools/f18/CMakeLists.txt b/flang/tools/f18/CMakeLists.txt
index 900b341e85c58..daa884e01eaf1 100644
--- a/flang/tools/f18/CMakeLists.txt
+++ b/flang/tools/f18/CMakeLists.txt
@@ -38,13 +38,6 @@ check_cxx_source_compiles(
HAVE_LDBL_MANT_DIG_113)
# Figure out whether we can support REAL(KIND=16)
-if (FLANG_RUNTIME_F128_MATH_LIB)
- set(FLANG_SUPPORT_R16 "1")
-elseif (HAVE_LDBL_MANT_DIG_113)
- set(FLANG_SUPPORT_R16 "1")
-else()
- set(FLANG_SUPPORT_R16 "0")
-endif()
# Init variable to hold extra object files coming from the Fortran modules;
# these module files will be contributed from the CMakeLists in flang/tools/f18.
@@ -100,11 +93,6 @@ if (NOT CMAKE_CROSSCOMPILING)
endif()
endif()
- set(decls "")
- if (FLANG_SUPPORT_R16)
- set(decls "-DFLANG_SUPPORT_R16")
- endif()
-
# Some modules have an implementation part that needs to be added to the
# flang_rt.runtime library.
set(compile_with "-fsyntax-only")
@@ -115,7 +103,7 @@ if (NOT CMAKE_CROSSCOMPILING)
# TODO: We may need to flag this with conditional, in case Flang is built w/o OpenMP support
add_custom_command(OUTPUT ${base}.mod ${object_output}
COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
- COMMAND flang ${opts} ${decls} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
+ COMMAND flang ${opts} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
${FLANG_SOURCE_DIR}/module/${filename}.f90
DEPENDS flang ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${FLANG_SOURCE_DIR}/module/__fortran_builtins.f90 ${depends}
)
More information about the flang-commits
mailing list