Skip to content

Commit

Permalink
[flang] Allow runtime build with AVOID_NATIVE_INT128_T=1
Browse files Browse the repository at this point in the history
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 1c35c1a)
  • Loading branch information
klausler authored and tru committed Sep 14, 2023
1 parent 0176e87 commit e22c304
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
10 changes: 7 additions & 3 deletions flang/runtime/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ static void StoreLengthToDescriptor(
}

template <int KIND> struct FitsInIntegerKind {
bool operator()(std::int64_t value) {
return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
Fortran::common::TypeCategory::Integer, KIND>>::max();
bool operator()([[maybe_unused]] std::int64_t value) {
if constexpr (KIND >= 8) {
return true;
} else {
return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
Fortran::common::TypeCategory::Integer, KIND>>::max();
}
}
};

Expand Down
4 changes: 3 additions & 1 deletion flang/runtime/io-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::intmax_t>(unit) == unit) {
if constexpr (sizeof(INT) > sizeof(std::intmax_t)) {
errorHandler.SignalError(IostatUnitOverflow);
} else if (static_cast<std::intmax_t>(unit) == unit) {
errorHandler.SignalError(IostatUnitOverflow,
"UNIT number %jd is out of range", static_cast<std::intmax_t>(unit));
} else {
Expand Down
24 changes: 12 additions & 12 deletions flang/runtime/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Ceiling4_8)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Ceiling4_16)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -328,7 +328,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Ceiling8_8)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Ceiling8_16)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -351,7 +351,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Ceiling10_8)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Ceiling10_16)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -374,7 +374,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Ceiling16_8)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Ceiling16_16)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Ceiling<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand Down Expand Up @@ -434,7 +434,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Floor4_8)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Floor4_16)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -456,7 +456,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Floor8_8)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Floor8_16)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -479,7 +479,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Floor10_8)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Floor10_16)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -502,7 +502,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Floor16_8)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Floor16_16)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Floor<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand Down Expand Up @@ -710,7 +710,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Nint4_8)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Nint4_16)(
CppTypeFor<TypeCategory::Real, 4> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -732,7 +732,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Nint8_8)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Nint8_16)(
CppTypeFor<TypeCategory::Real, 8> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -755,7 +755,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Nint10_8)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Nint10_16)(
CppTypeFor<TypeCategory::Real, 10> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand All @@ -778,7 +778,7 @@ CppTypeFor<TypeCategory::Integer, 8> RTNAME(Nint16_8)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 8>>(x);
}
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
CppTypeFor<TypeCategory::Integer, 16> RTNAME(Nint16_16)(
CppTypeFor<TypeCategory::Real, 16> x) {
return Nint<CppTypeFor<TypeCategory::Integer, 16>>(x);
Expand Down
19 changes: 17 additions & 2 deletions flang/runtime/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ inline RT_API_ATTRS RESULT ApplyType(
return FUNC<TypeCategory::Integer, 4>{}(std::forward<A>(x)...);
case 8:
return FUNC<TypeCategory::Integer, 8>{}(std::forward<A>(x)...);
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
case 16:
return FUNC<TypeCategory::Integer, 16>{}(std::forward<A>(x)...);
#endif
Expand Down Expand Up @@ -230,7 +230,7 @@ inline RT_API_ATTRS RESULT ApplyIntegerKind(
return FUNC<4>{}(std::forward<A>(x)...);
case 8:
return FUNC<8>{}(std::forward<A>(x)...);
#ifdef __SIZEOF_INT128__
#if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
case 16:
return FUNC<16>{}(std::forward<A>(x)...);
#endif
Expand Down Expand Up @@ -310,6 +310,11 @@ std::optional<std::pair<TypeCategory, int>> 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;
Expand All @@ -318,6 +323,11 @@ std::optional<std::pair<TypeCategory, int>> 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:
Expand All @@ -329,6 +339,11 @@ std::optional<std::pair<TypeCategory, int>> 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:
Expand Down

0 comments on commit e22c304

Please sign in to comment.