aboutsummaryrefslogtreecommitdiff
path: root/flang
diff options
context:
space:
mode:
authorSlava Zakharin <szakharin@nvidia.com>2024-02-20 12:33:08 -0800
committerGitHub <noreply@github.com>2024-02-20 12:33:08 -0800
commita468d02fe9e544f39f6c0428c23b2396df6a35ff (patch)
treede3d668672adf7d45995f4e815beb1a5eaadf3a6 /flang
parentcd4e2466163c46c4e731f8dfc77a9b6673d26c89 (diff)
downloadllvm-a468d02fe9e544f39f6c0428c23b2396df6a35ff.zip
llvm-a468d02fe9e544f39f6c0428c23b2396df6a35ff.tar.gz
llvm-a468d02fe9e544f39f6c0428c23b2396df6a35ff.tar.bz2
[flang][runtime] Add FortranFloat128Math wrapper library. (#81971)
Implemented few entry points for REAL(16) math in FortranF128Math static library. It is a thin wrapper around GNU libquadmath. Flang driver can always link it, and the dependencies will be brought in as needed. The final Fortran program/library that uses any of the entry points will depend on the underlying third-party library - this dependency has to be resolved somehow. I added FLANG_RUNTIME_F128_MATH_LIB CMake control so that the compiler driver and the runtime library can be built using the same third-party library: this way the linker knows which dependency to link in (under --as-needed). The compiler distribution should specify which third-party library is required for linking/running the apps that use REAL(16). The compiler package may provide a version of the third-party library or at least a stub library that can be used for linking, but the final program execution will still require the actual library.
Diffstat (limited to 'flang')
-rw-r--r--flang/CMakeLists.txt17
-rw-r--r--flang/include/flang/Optimizer/Builder/IntrinsicCall.h19
-rw-r--r--flang/lib/Optimizer/Builder/IntrinsicCall.cpp101
-rw-r--r--flang/runtime/CMakeLists.txt20
-rw-r--r--flang/runtime/Float128Math/CMakeLists.txt56
-rw-r--r--flang/runtime/Float128Math/cabs.cpp24
-rw-r--r--flang/runtime/Float128Math/math-entries.h77
-rw-r--r--flang/runtime/Float128Math/sin.cpp22
-rw-r--r--flang/runtime/Float128Math/sqrt.cpp22
-rw-r--r--flang/test/Lower/Intrinsics/missing-math-runtime.f906
-rw-r--r--flang/tools/flang-driver/driver.cpp3
11 files changed, 327 insertions, 40 deletions
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index f8ad39b..21617ae 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -33,6 +33,17 @@ endif()
option(FLANG_ENABLE_WERROR "Fail and stop building flang if a warning is triggered." OFF)
+# The out of tree builds of the compiler and the Fortran runtime
+# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
+# to be composable. Failure to synchronize this setting may result
+# in linking errors or fatal failures in F128 runtime functions.
+set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
+ "Specifies the target library used for implementing IEEE-754 128-bit float \
+ math in F18 runtime, e.g. it might be libquadmath for targets where \
+ REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
+ is mapped to long double, etc."
+ )
+
# Check for a standalone build and configure as appropriate from
# there.
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
@@ -321,6 +332,12 @@ if (FLANG_REPOSITORY_STRING)
add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
endif()
+if (FLANG_RUNTIME_F128_MATH_LIB)
+ add_compile_definitions(
+ -DFLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}"
+ )
+endif()
+
include(TestBigEndian)
test_big_endian(IS_BIGENDIAN)
if (IS_BIGENDIAN)
diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index 3f1e22e..7cb99d6 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -494,12 +494,13 @@ struct RuntimeFunction {
fir::runtime::FuncTypeBuilderFunc typeGenerator;
};
-/// Callback type for generating lowering for a math operation.
-using MathGeneratorTy = mlir::Value (*)(fir::FirOpBuilder &, mlir::Location,
- llvm::StringRef, mlir::FunctionType,
- llvm::ArrayRef<mlir::Value>);
-
struct MathOperation {
+ // Callback type for generating lowering for a math operation.
+ using MathGeneratorTy = mlir::Value (*)(fir::FirOpBuilder &, mlir::Location,
+ const MathOperation &,
+ mlir::FunctionType,
+ llvm::ArrayRef<mlir::Value>);
+
// Overrides fir::runtime::FuncTypeBuilderFunc to add FirOpBuilder argument.
using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
fir::FirOpBuilder &);
@@ -681,25 +682,25 @@ getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
}
mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef libFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType libFuncType,
llvm::ArrayRef<mlir::Value> args);
template <typename T>
mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef mathLibFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType mathLibFuncType,
llvm::ArrayRef<mlir::Value> args);
template <typename T>
mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef mathLibFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType mathLibFuncType,
llvm::ArrayRef<mlir::Value> args);
mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
mlir::Location loc,
- llvm::StringRef libFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType libFuncType,
llvm::ArrayRef<mlir::Value> args);
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index a353689..3a82be8 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -657,10 +657,61 @@ static llvm::cl::opt<bool>
"instead of libm complex operations"),
llvm::cl::init(false));
+/// Return a string containing the given Fortran intrinsic name
+/// with the type of its arguments specified in funcType
+/// surrounded by the given prefix/suffix.
+static std::string
+prettyPrintIntrinsicName(fir::FirOpBuilder &builder, mlir::Location loc,
+ llvm::StringRef prefix, llvm::StringRef name,
+ llvm::StringRef suffix, mlir::FunctionType funcType) {
+ std::string output = prefix.str();
+ llvm::raw_string_ostream sstream(output);
+ if (name == "pow") {
+ assert(funcType.getNumInputs() == 2 && "power operator has two arguments");
+ std::string displayName{" ** "};
+ sstream << numericMlirTypeToFortran(builder, funcType.getInput(0), loc,
+ displayName)
+ << displayName
+ << numericMlirTypeToFortran(builder, funcType.getInput(1), loc,
+ displayName);
+ } else {
+ sstream << name.upper() << "(";
+ if (funcType.getNumInputs() > 0)
+ sstream << numericMlirTypeToFortran(builder, funcType.getInput(0), loc,
+ name);
+ for (mlir::Type argType : funcType.getInputs().drop_front()) {
+ sstream << ", " << numericMlirTypeToFortran(builder, argType, loc, name);
+ }
+ sstream << ")";
+ }
+ sstream << suffix;
+ return output;
+}
+
+// Generate a call to the Fortran runtime library providing
+// support for 128-bit float math via a third-party library.
+// If the compiler is built without FLANG_RUNTIME_F128_MATH_LIB,
+// this function will report an error.
+static mlir::Value genLibF128Call(fir::FirOpBuilder &builder,
+ mlir::Location loc,
+ const MathOperation &mathOp,
+ mlir::FunctionType libFuncType,
+ llvm::ArrayRef<mlir::Value> args) {
+#ifndef FLANG_RUNTIME_F128_MATH_LIB
+ std::string message = prettyPrintIntrinsicName(
+ builder, loc, "compiler is built without support for '", mathOp.key, "'",
+ libFuncType);
+ fir::emitFatalError(loc, message, /*genCrashDiag=*/false);
+#else // FLANG_RUNTIME_F128_MATH_LIB
+ return genLibCall(builder, loc, mathOp, libFuncType, args);
+#endif // FLANG_RUNTIME_F128_MATH_LIB
+}
+
mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef libFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType libFuncType,
llvm::ArrayRef<mlir::Value> args) {
+ llvm::StringRef libFuncName = mathOp.runtimeFunc;
LLVM_DEBUG(llvm::dbgs() << "Generating '" << libFuncName
<< "' call with type ";
libFuncType.dump(); llvm::dbgs() << "\n");
@@ -718,7 +769,7 @@ mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
mlir::Location loc,
- llvm::StringRef libFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType libFuncType,
llvm::ArrayRef<mlir::Value> args) {
assert(args.size() == 2 && "Incorrect #args to genLibSplitComplexArgsCall");
@@ -762,13 +813,12 @@ mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
cplx2, /*isImagPart=*/true);
splitArgs.push_back(imag2);
- return genLibCall(builder, loc, libFuncName, getSplitComplexArgsType(),
- splitArgs);
+ return genLibCall(builder, loc, mathOp, getSplitComplexArgsType(), splitArgs);
}
template <typename T>
mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef mathLibFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType mathLibFuncType,
llvm::ArrayRef<mlir::Value> args) {
// TODO: we have to annotate the math operations with flags
@@ -791,13 +841,14 @@ mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
// can be also lowered to libm calls for "fast" and "relaxed"
// modes.
mlir::Value result;
+ llvm::StringRef mathLibFuncName = mathOp.runtimeFunc;
if (mathRuntimeVersion == preciseVersion &&
// Some operations do not have to be lowered as conservative
// calls, since they do not affect strict FP behavior.
// For example, purely integer operations like exponentiation
// with integer operands fall into this class.
!mathLibFuncName.empty()) {
- result = genLibCall(builder, loc, mathLibFuncName, mathLibFuncType, args);
+ result = genLibCall(builder, loc, mathOp, mathLibFuncType, args);
} else {
LLVM_DEBUG(llvm::dbgs() << "Generating '" << mathLibFuncName
<< "' operation with type ";
@@ -810,7 +861,7 @@ mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
template <typename T>
mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
- llvm::StringRef mathLibFuncName,
+ const MathOperation &mathOp,
mlir::FunctionType mathLibFuncType,
llvm::ArrayRef<mlir::Value> args) {
mlir::Value result;
@@ -819,11 +870,12 @@ mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
// If we have libm functions, we can attempt to generate the more precise
// version of the complex math operation.
+ llvm::StringRef mathLibFuncName = mathOp.runtimeFunc;
if (!mathLibFuncName.empty()) {
// If we enabled MLIR complex or can use approximate operations, we should
// NOT use libm.
if (!forceMlirComplex && !canUseApprox) {
- result = genLibCall(builder, loc, mathLibFuncName, mathLibFuncType, args);
+ result = genLibCall(builder, loc, mathOp, mathLibFuncType, args);
LLVM_DEBUG(result.dump(); llvm::dbgs() << "\n");
return result;
}
@@ -863,6 +915,10 @@ mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
/// TODO: support remaining Fortran math intrinsics.
/// See https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gfortran/\
/// Intrinsic-Procedures.html for a reference.
+constexpr auto FuncTypeReal16Real16 = genFuncType<Ty::Real<16>, Ty::Real<16>>;
+constexpr auto FuncTypeReal16Complex16 =
+ genFuncType<Ty::Real<16>, Ty::Complex<16>>;
+
static constexpr MathOperation mathOperations[] = {
{"abs", "fabsf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
genMathOp<mlir::math::AbsFOp>},
@@ -874,6 +930,7 @@ static constexpr MathOperation mathOperations[] = {
genComplexMathOp<mlir::complex::AbsOp>},
{"abs", "cabs", genFuncType<Ty::Real<8>, Ty::Complex<8>>,
genComplexMathOp<mlir::complex::AbsOp>},
+ {"abs", RTNAME_STRING(CAbsF128), FuncTypeReal16Complex16, genLibF128Call},
{"acos", "acosf", genFuncType<Ty::Real<4>, Ty::Real<4>>, genLibCall},
{"acos", "acos", genFuncType<Ty::Real<8>, Ty::Real<8>>, genLibCall},
{"acos", "cacosf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>, genLibCall},
@@ -1110,6 +1167,7 @@ static constexpr MathOperation mathOperations[] = {
genMathOp<mlir::math::SinOp>},
{"sin", "sin", genFuncType<Ty::Real<8>, Ty::Real<8>>,
genMathOp<mlir::math::SinOp>},
+ {"sin", RTNAME_STRING(SinF128), FuncTypeReal16Real16, genLibF128Call},
{"sin", "csinf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>,
genComplexMathOp<mlir::complex::SinOp>},
{"sin", "csin", genFuncType<Ty::Complex<8>, Ty::Complex<8>>,
@@ -1122,6 +1180,7 @@ static constexpr MathOperation mathOperations[] = {
genMathOp<mlir::math::SqrtOp>},
{"sqrt", "sqrt", genFuncType<Ty::Real<8>, Ty::Real<8>>,
genMathOp<mlir::math::SqrtOp>},
+ {"sqrt", RTNAME_STRING(SqrtF128), FuncTypeReal16Real16, genLibF128Call},
{"sqrt", "csqrtf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>,
genComplexMathOp<mlir::complex::SqrtOp>},
{"sqrt", "csqrt", genFuncType<Ty::Complex<8>, Ty::Complex<8>>,
@@ -1345,27 +1404,9 @@ static void checkPrecisionLoss(llvm::StringRef name,
// lowering and could be used here. Emit an error and continue
// generating the code with the narrowing cast so that the user
// can get a complete list of the problematic intrinsic calls.
- std::string message("not yet implemented: no math runtime available for '");
- llvm::raw_string_ostream sstream(message);
- if (name == "pow") {
- assert(funcType.getNumInputs() == 2 && "power operator has two arguments");
- std::string displayName{" ** "};
- sstream << numericMlirTypeToFortran(builder, funcType.getInput(0), loc,
- displayName)
- << displayName
- << numericMlirTypeToFortran(builder, funcType.getInput(1), loc,
- displayName);
- } else {
- sstream << name.upper() << "(";
- if (funcType.getNumInputs() > 0)
- sstream << numericMlirTypeToFortran(builder, funcType.getInput(0), loc,
- name);
- for (mlir::Type argType : funcType.getInputs().drop_front()) {
- sstream << ", " << numericMlirTypeToFortran(builder, argType, loc, name);
- }
- sstream << ")";
- }
- sstream << "'";
+ std::string message = prettyPrintIntrinsicName(
+ builder, loc, "not yet implemented: no math runtime available for '",
+ name, "'", funcType);
mlir::emitError(loc, message);
}
@@ -1887,7 +1928,7 @@ IntrinsicLibrary::getRuntimeCallGenerator(llvm::StringRef name,
for (auto [fst, snd] : llvm::zip(actualFuncType.getInputs(), args))
convertedArguments.push_back(builder.createConvert(loc, fst, snd));
mlir::Value result = mathOp->funcGenerator(
- builder, loc, mathOp->runtimeFunc, actualFuncType, convertedArguments);
+ builder, loc, *mathOp, actualFuncType, convertedArguments);
mlir::Type soughtType = soughtFuncType.getResult(0);
return builder.createConvert(loc, soughtType, result);
};
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index dfa9da5..ac89184 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -46,6 +46,23 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
endif ()
include_directories(BEFORE
${FLANG_SOURCE_DIR}/include)
+
+ # The out of tree builds of the compiler and the Fortran runtime
+ # must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
+ # to be composable. Failure to synchronize this setting may result
+ # in linking errors or fatal failures in F128 runtime functions.
+ set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
+ "Specifies the target library used for implementing IEEE-754 128-bit float \
+ math in F18 runtime, e.g. it might be libquadmath for targets where \
+ REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
+ is mapped to long double, etc."
+ )
+
+ if (NOT FLANG_RUNTIME_F128_MATH_LIB STREQUAL "")
+ add_compile_definitions(
+ -DFLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}"
+ )
+ endif()
endif()
include(CheckCXXSymbolExists)
@@ -83,6 +100,9 @@ add_definitions(-U_GLIBCXX_ASSERTIONS)
add_definitions(-U_LIBCPP_ENABLE_ASSERTIONS)
add_subdirectory(FortranMain)
+if (NOT ${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "")
+ add_subdirectory(Float128Math)
+endif()
set(sources
ISO_Fortran_binding.cpp
diff --git a/flang/runtime/Float128Math/CMakeLists.txt b/flang/runtime/Float128Math/CMakeLists.txt
new file mode 100644
index 0000000..f8da4d7
--- /dev/null
+++ b/flang/runtime/Float128Math/CMakeLists.txt
@@ -0,0 +1,56 @@
+#===-- runtime/Float128Math/CMakeLists.txt ---------------------------------===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+#===------------------------------------------------------------------------===#
+
+# FortranFloat128 implements IEEE-754 128-bit float math functions.
+# It is a thin wapper and it currently relies on third-party
+# libraries available for the target.
+# It is distributed as a static library only.
+# Fortran programs/libraries that end up linking any of the provided
+# will have a dependency on the third-party library that is being
+# used for building this FortranFloat128Math library.
+
+if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath" OR
+ ${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "quadmath")
+ check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
+ if(FOUND_QUADMATH_HEADER)
+ add_compile_definitions(HAS_QUADMATHLIB)
+ else()
+ message(FATAL_ERROR
+ "FLANG_RUNTIME_F128_MATH_LIB setting requires quadmath.h "
+ "to be available: ${FLANG_RUNTIME_F128_MATH_LIB}"
+ )
+ endif()
+else()
+ message(FATAL_ERROR
+ "Unsupported third-party library for Fortran F128 math runtime: "
+ "${FLANG_RUNTIME_F128_MATH_LIB}"
+ )
+endif()
+
+set(sources
+ cabs.cpp
+ sin.cpp
+ sqrt.cpp
+ )
+
+include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/..")
+add_flang_library(FortranFloat128Math STATIC INSTALL_WITH_TOOLCHAIN ${sources})
+
+if (DEFINED MSVC)
+ set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
+ add_flang_library(FortranFloat128Math.static STATIC INSTALL_WITH_TOOLCHAIN
+ ${sources}
+ )
+ set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+ add_flang_library(FortranFloat128Math.static_dbg STATIC INSTALL_WITH_TOOLCHAIN
+ ${sources}
+ )
+ add_dependencies(FortranFloat128Math FortranFloat128Math.static
+ FortranFloat128Math.static_dbg
+ )
+endif()
diff --git a/flang/runtime/Float128Math/cabs.cpp b/flang/runtime/Float128Math/cabs.cpp
new file mode 100644
index 0000000..63f2bdf
--- /dev/null
+++ b/flang/runtime/Float128Math/cabs.cpp
@@ -0,0 +1,24 @@
+//===-- runtime/Float128Math/cabs.cpp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "math-entries.h"
+
+namespace Fortran::runtime {
+extern "C" {
+
+#if LDBL_MANT_DIG == 113 || HAS_FLOAT128
+// FIXME: the argument should be CppTypeFor<TypeCategory::Complex, 16>,
+// and it should be translated into the underlying library's
+// corresponding complex128 type.
+CppTypeFor<TypeCategory::Real, 16> RTDEF(CAbsF128)(ComplexF128 x) {
+ return CAbs<RTNAME(CAbsF128)>::invoke(x);
+}
+#endif
+
+} // extern "C"
+} // namespace Fortran::runtime
diff --git a/flang/runtime/Float128Math/math-entries.h b/flang/runtime/Float128Math/math-entries.h
new file mode 100644
index 0000000..91c14b0
--- /dev/null
+++ b/flang/runtime/Float128Math/math-entries.h
@@ -0,0 +1,77 @@
+//===-- runtime/Float128Math/math-entries.h ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
+#define FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
+#include "terminator.h"
+#include "tools.h"
+#include "flang/Common/float128.h"
+#include "flang/Runtime/entry-names.h"
+#include <type_traits>
+
+namespace Fortran::runtime {
+
+// Define a class template to gracefully fail, when
+// there is no specialized template that implements
+// the required function via using the third-party
+// implementation.
+#define DEFINE_FALLBACK(caller) \
+ template <auto F> struct caller { \
+ template <typename... ATs> \
+ [[noreturn]] static std::invoke_result_t<decltype(F), ATs...> invoke( \
+ ATs... args) { \
+ Terminator terminator{__FILE__, __LINE__}; \
+ terminator.Crash("Float128 variant of '%s' is unsupported", #caller); \
+ } \
+ };
+
+// Define template specialization that is calling the third-party
+// implementation. The template is specialized by a function pointer
+// that is the FortranFloat128Math entry point. The signatures
+// of the caller and the callee must match.
+//
+// Defining the specialization for any target library requires
+// adding the generic template via DEFINE_FALLBACK, so that
+// a build with another target library that does not define
+// the same alias can gracefully fail in runtime.
+#define DEFINE_SIMPLE_ALIAS(caller, callee) \
+ template <typename RT, typename... ATs, RT (*p)(ATs...)> struct caller<p> { \
+ static RT invoke(ATs... args) { \
+ static_assert(std::is_invocable_r_v<RT, decltype(callee), ATs...>); \
+ if constexpr (std::is_same_v<RT, void>) { \
+ callee(args...); \
+ } else { \
+ return callee(args...); \
+ } \
+ } \
+ };
+
+// Define fallback callers.
+DEFINE_FALLBACK(CAbs)
+DEFINE_FALLBACK(Sin)
+DEFINE_FALLBACK(Sqrt)
+
+// Define ComplexF128 type that is compatible with
+// the type of results/arguments of libquadmath.
+// TODO: this may need more work for other libraries/compilers.
+#if !defined(_ARCH_PPC) || defined(__LONG_DOUBLE_IEEE128__)
+typedef _Complex float __attribute__((mode(TC))) ComplexF128;
+#else
+typedef _Complex float __attribute__((mode(KC))) ComplexF128;
+#endif
+
+#if HAS_QUADMATHLIB
+// Define wrapper callers for libquadmath.
+#include "quadmath.h"
+DEFINE_SIMPLE_ALIAS(CAbs, cabsq)
+DEFINE_SIMPLE_ALIAS(Sin, sinq)
+DEFINE_SIMPLE_ALIAS(Sqrt, sqrtq)
+#endif
+} // namespace Fortran::runtime
+
+#endif // FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
diff --git a/flang/runtime/Float128Math/sin.cpp b/flang/runtime/Float128Math/sin.cpp
new file mode 100644
index 0000000..013eb9d
--- /dev/null
+++ b/flang/runtime/Float128Math/sin.cpp
@@ -0,0 +1,22 @@
+//===-- runtime/Float128Math/sin.cpp --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "math-entries.h"
+
+namespace Fortran::runtime {
+extern "C" {
+
+#if LDBL_MANT_DIG == 113 || HAS_FLOAT128
+CppTypeFor<TypeCategory::Real, 16> RTDEF(SinF128)(
+ CppTypeFor<TypeCategory::Real, 16> x) {
+ return Sin<RTNAME(SinF128)>::invoke(x);
+}
+#endif
+
+} // extern "C"
+} // namespace Fortran::runtime
diff --git a/flang/runtime/Float128Math/sqrt.cpp b/flang/runtime/Float128Math/sqrt.cpp
new file mode 100644
index 0000000..aafbd85
--- /dev/null
+++ b/flang/runtime/Float128Math/sqrt.cpp
@@ -0,0 +1,22 @@
+//===-- runtime/Float128Math/sqrt.cpp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "math-entries.h"
+
+namespace Fortran::runtime {
+extern "C" {
+
+#if LDBL_MANT_DIG == 113 || HAS_FLOAT128
+CppTypeFor<TypeCategory::Real, 16> RTDEF(SqrtF128)(
+ CppTypeFor<TypeCategory::Real, 16> x) {
+ return Sqrt<RTNAME(SqrtF128)>::invoke(x);
+}
+#endif
+
+} // extern "C"
+} // namespace Fortran::runtime
diff --git a/flang/test/Lower/Intrinsics/missing-math-runtime.f90 b/flang/test/Lower/Intrinsics/missing-math-runtime.f90
index 98d3abb..ff767ba 100644
--- a/flang/test/Lower/Intrinsics/missing-math-runtime.f90
+++ b/flang/test/Lower/Intrinsics/missing-math-runtime.f90
@@ -1,10 +1,14 @@
! There is no quad math runtime available in lowering
! for now. Test that the TODO are emitted correctly.
+! FIXME: the lit config has to flip a feature flag so that
+! the tests can use different checks depending on whether
+! REAL(16) math support is enabled or not.
+! XFAIL: *
! RUN: bbc -emit-fir %s -o /dev/null 2>&1 | FileCheck %s
complex(16) :: a
real(16) :: b
-! CHECK: not yet implemented: no math runtime available for 'ABS(COMPLEX(KIND=16))'
+! CHECK: compiler is built without support for 'ABS(COMPLEX(KIND=16))'
b = abs(a)
end
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index c4e56a8..52136df 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -130,6 +130,9 @@ int main(int argc, const char **argv) {
llvm::sys::getDefaultTargetTriple(), diags,
"flang LLVM compiler");
theDriver.setTargetAndMode(targetandMode);
+#ifdef FLANG_RUNTIME_F128_MATH_LIB
+ theDriver.setFlangF128MathLibrary(FLANG_RUNTIME_F128_MATH_LIB);
+#endif
std::unique_ptr<clang::driver::Compilation> c(
theDriver.BuildCompilation(args));
llvm::SmallVector<std::pair<int, const clang::driver::Command *>, 4>