From e22c30414e5426795b45df79959fa405025a148c Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Thu, 6 Jul 2023 15:03:05 -0700 Subject: [PATCH] [flang] Allow runtime build with AVOID_NATIVE_INT128_T=1 This patch enables the Fortran runtime support library to be built without native 128-bit integer support in the C++ compiler. Experimental: do not merge yet. Differential Revision: https://reviews.llvm.org/D154660 (cherry picked from commit 1c35c1a73907a95ce54b5a0edca513591e2bc069) --- flang/runtime/command.cpp | 10 +++++++--- flang/runtime/io-api.cpp | 4 +++- flang/runtime/numeric.cpp | 24 ++++++++++++------------ flang/runtime/tools.h | 19 +++++++++++++++++-- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp index 6c4f611daaa7cb..b81a0791c5e571 100644 --- a/flang/runtime/command.cpp +++ b/flang/runtime/command.cpp @@ -107,9 +107,13 @@ static void StoreLengthToDescriptor( } template struct FitsInIntegerKind { - bool operator()(std::int64_t value) { - return value <= std::numeric_limits>::max(); + bool operator()([[maybe_unused]] std::int64_t value) { + if constexpr (KIND >= 8) { + return true; + } else { + return value <= std::numeric_limits>::max(); + } } }; diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp index 2c17f10e77693b..f9d60fecb149a5 100644 --- a/flang/runtime/io-api.cpp +++ b/flang/runtime/io-api.cpp @@ -1484,7 +1484,9 @@ static enum Iostat CheckUnitNumberInRangeImpl(INT unit, bool handleError, // Only provide the bad unit number in the message if SignalError can print // it accurately. Otherwise, the generic IostatUnitOverflow message will be // used. - if (static_cast(unit) == unit) { + if constexpr (sizeof(INT) > sizeof(std::intmax_t)) { + errorHandler.SignalError(IostatUnitOverflow); + } else if (static_cast(unit) == unit) { errorHandler.SignalError(IostatUnitOverflow, "UNIT number %jd is out of range", static_cast(unit)); } else { diff --git a/flang/runtime/numeric.cpp b/flang/runtime/numeric.cpp index 5376bcb569fbea..21ee91a8681ee7 100644 --- a/flang/runtime/numeric.cpp +++ b/flang/runtime/numeric.cpp @@ -306,7 +306,7 @@ CppTypeFor RTNAME(Ceiling4_8)( CppTypeFor x) { return Ceiling>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Ceiling4_16)( CppTypeFor x) { return Ceiling>(x); @@ -328,7 +328,7 @@ CppTypeFor RTNAME(Ceiling8_8)( CppTypeFor x) { return Ceiling>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Ceiling8_16)( CppTypeFor x) { return Ceiling>(x); @@ -351,7 +351,7 @@ CppTypeFor RTNAME(Ceiling10_8)( CppTypeFor x) { return Ceiling>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Ceiling10_16)( CppTypeFor x) { return Ceiling>(x); @@ -374,7 +374,7 @@ CppTypeFor RTNAME(Ceiling16_8)( CppTypeFor x) { return Ceiling>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Ceiling16_16)( CppTypeFor x) { return Ceiling>(x); @@ -434,7 +434,7 @@ CppTypeFor RTNAME(Floor4_8)( CppTypeFor x) { return Floor>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Floor4_16)( CppTypeFor x) { return Floor>(x); @@ -456,7 +456,7 @@ CppTypeFor RTNAME(Floor8_8)( CppTypeFor x) { return Floor>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Floor8_16)( CppTypeFor x) { return Floor>(x); @@ -479,7 +479,7 @@ CppTypeFor RTNAME(Floor10_8)( CppTypeFor x) { return Floor>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Floor10_16)( CppTypeFor x) { return Floor>(x); @@ -502,7 +502,7 @@ CppTypeFor RTNAME(Floor16_8)( CppTypeFor x) { return Floor>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Floor16_16)( CppTypeFor x) { return Floor>(x); @@ -710,7 +710,7 @@ CppTypeFor RTNAME(Nint4_8)( CppTypeFor x) { return Nint>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Nint4_16)( CppTypeFor x) { return Nint>(x); @@ -732,7 +732,7 @@ CppTypeFor RTNAME(Nint8_8)( CppTypeFor x) { return Nint>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Nint8_16)( CppTypeFor x) { return Nint>(x); @@ -755,7 +755,7 @@ CppTypeFor RTNAME(Nint10_8)( CppTypeFor x) { return Nint>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Nint10_16)( CppTypeFor x) { return Nint>(x); @@ -778,7 +778,7 @@ CppTypeFor RTNAME(Nint16_8)( CppTypeFor x) { return Nint>(x); } -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T CppTypeFor RTNAME(Nint16_16)( CppTypeFor x) { return Nint>(x); diff --git a/flang/runtime/tools.h b/flang/runtime/tools.h index d22093a2ada07c..72d14982ce44b7 100644 --- a/flang/runtime/tools.h +++ b/flang/runtime/tools.h @@ -131,7 +131,7 @@ inline RT_API_ATTRS RESULT ApplyType( return FUNC{}(std::forward(x)...); case 8: return FUNC{}(std::forward(x)...); -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T case 16: return FUNC{}(std::forward(x)...); #endif @@ -230,7 +230,7 @@ inline RT_API_ATTRS RESULT ApplyIntegerKind( return FUNC<4>{}(std::forward(x)...); case 8: return FUNC<8>{}(std::forward(x)...); -#ifdef __SIZEOF_INT128__ +#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T case 16: return FUNC<16>{}(std::forward(x)...); #endif @@ -310,6 +310,11 @@ std::optional> inline constexpr GetResultType( return std::make_pair(TypeCategory::Integer, maxKind); case TypeCategory::Real: case TypeCategory::Complex: +#if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T) + if (xKind == 16) { + break; + } +#endif return std::make_pair(yCat, yKind); default: break; @@ -318,6 +323,11 @@ std::optional> inline constexpr GetResultType( case TypeCategory::Real: switch (yCat) { case TypeCategory::Integer: +#if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T) + if (yKind == 16) { + break; + } +#endif return std::make_pair(TypeCategory::Real, xKind); case TypeCategory::Real: case TypeCategory::Complex: @@ -329,6 +339,11 @@ std::optional> inline constexpr GetResultType( case TypeCategory::Complex: switch (yCat) { case TypeCategory::Integer: +#if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T) + if (yKind == 16) { + break; + } +#endif return std::make_pair(TypeCategory::Complex, xKind); case TypeCategory::Real: case TypeCategory::Complex: