diff --git a/flang/include/flang/Common/Fortran-consts.h b/flang/include/flang/Common/Fortran-consts.h new file mode 100644 index 0000000000000..eedcdae335c40 --- /dev/null +++ b/flang/include/flang/Common/Fortran-consts.h @@ -0,0 +1,44 @@ +//===-- include/flang/Common/Fortran-consts.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_COMMON_FORTRAN_CONSTS_H_ +#define FORTRAN_COMMON_FORTRAN_CONSTS_H_ + +#include "flang/Common/enum-class.h" +#include + +namespace Fortran::common { + +// Fortran has five kinds of intrinsic data types, plus the derived types. +ENUM_CLASS(TypeCategory, Integer, Real, Complex, Character, Logical, Derived) +ENUM_CLASS(VectorElementCategory, Integer, Unsigned, Real) + +ENUM_CLASS(IoStmtKind, None, Backspace, Close, Endfile, Flush, Inquire, Open, + Print, Read, Rewind, Wait, Write) + +// Defined I/O variants +ENUM_CLASS( + DefinedIo, ReadFormatted, ReadUnformatted, WriteFormatted, WriteUnformatted) + +// Fortran arrays may have up to 15 dimensions (See Fortran 2018 section 5.4.6). +static constexpr int maxRank{15}; + +// Floating-point rounding modes; these are packed into a byte to save +// room in the runtime's format processing context structure. These +// enumerators are defined with the corresponding values returned from +// llvm.get.rounding. +enum class RoundingMode : std::uint8_t { + ToZero, // ROUND=ZERO, RZ - truncation + TiesToEven, // ROUND=NEAREST, RN - default IEEE rounding + Up, // ROUND=UP, RU + Down, // ROUND=DOWN, RD + TiesAwayFromZero, // ROUND=COMPATIBLE, RC - ties round away from zero +}; + +} // namespace Fortran::common +#endif /* FORTRAN_COMMON_FORTRAN_CONSTS_H_ */ diff --git a/flang/include/flang/Common/Fortran.h b/flang/include/flang/Common/Fortran.h index cb109ad574cf6..72e4348a42a3f 100644 --- a/flang/include/flang/Common/Fortran.h +++ b/flang/include/flang/Common/Fortran.h @@ -14,6 +14,7 @@ #include "enum-set.h" #include "idioms.h" +#include "flang/Common/Fortran-consts.h" #include #include #include @@ -21,10 +22,6 @@ namespace Fortran::common { class LanguageFeatureControl; -// Fortran has five kinds of intrinsic data types, plus the derived types. -ENUM_CLASS(TypeCategory, Integer, Real, Complex, Character, Logical, Derived) -ENUM_CLASS(VectorElementCategory, Integer, Unsigned, Real) - constexpr bool IsNumericTypeCategory(TypeCategory category) { return category == TypeCategory::Integer || category == TypeCategory::Real || category == TypeCategory::Complex; @@ -47,9 +44,6 @@ const char *AsFortran(RelationalOperator); ENUM_CLASS(Intent, Default, In, Out, InOut) -ENUM_CLASS(IoStmtKind, None, Backspace, Close, Endfile, Flush, Inquire, Open, - Print, Read, Rewind, Wait, Write) - // Union of specifiers for all I/O statements. ENUM_CLASS(IoSpecKind, Access, Action, Advance, Asynchronous, Blank, Decimal, Delim, Direct, Encoding, End, Eor, Err, Exist, File, Fmt, Form, Formatted, @@ -61,29 +55,11 @@ ENUM_CLASS(IoSpecKind, Access, Action, Advance, Asynchronous, Blank, Decimal, Dispose, // nonstandard ) -// Defined I/O variants -ENUM_CLASS( - DefinedIo, ReadFormatted, ReadUnformatted, WriteFormatted, WriteUnformatted) const char *AsFortran(DefinedIo); -// Floating-point rounding modes; these are packed into a byte to save -// room in the runtime's format processing context structure. These -// enumerators are defined with the corresponding values returned from -// llvm.get.rounding. -enum class RoundingMode : std::uint8_t { - ToZero, // ROUND=ZERO, RZ - truncation - TiesToEven, // ROUND=NEAREST, RN - default IEEE rounding - Up, // ROUND=UP, RU - Down, // ROUND=DOWN, RD - TiesAwayFromZero, // ROUND=COMPATIBLE, RC - ties round away from zero -}; - // Fortran label. Must be in [1..99999]. using Label = std::uint64_t; -// Fortran arrays may have up to 15 dimensions (See Fortran 2018 section 5.4.6). -static constexpr int maxRank{15}; - // CUDA subprogram attribute combinations ENUM_CLASS(CUDASubprogramAttrs, Host, Device, HostDevice, Global, Grid_Global) diff --git a/flang/include/flang/Common/format.h b/flang/include/flang/Common/format.h index 67d37bee32ab3..de6967139c5ca 100644 --- a/flang/include/flang/Common/format.h +++ b/flang/include/flang/Common/format.h @@ -10,7 +10,7 @@ #define FORTRAN_COMMON_FORMAT_H_ #include "enum-set.h" -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include // Define a FormatValidator class template to validate a format expression diff --git a/flang/include/flang/Common/target-rounding.h b/flang/include/flang/Common/target-rounding.h new file mode 100644 index 0000000000000..c0c9f6c49b26a --- /dev/null +++ b/flang/include/flang/Common/target-rounding.h @@ -0,0 +1,37 @@ +//===-- include/flang/Common/target-rounding.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_COMMON_TARGET_ROUNDING_H_ +#define FORTRAN_COMMON_TARGET_ROUNDING_H_ + +#include "flang/Common/Fortran-consts.h" +#include "flang/Common/enum-set.h" + +namespace Fortran::common { + +// Floating-point rounding control +struct Rounding { + common::RoundingMode mode{common::RoundingMode::TiesToEven}; + // When set, emulate status flag behavior peculiar to x86 + // (viz., fail to set the Underflow flag when an inexact product of a + // multiplication is rounded up to a normal number from a subnormal + // in some rounding modes) +#if __x86_64__ || __riscv || __loongarch__ + bool x86CompatibleBehavior{true}; +#else + bool x86CompatibleBehavior{false}; +#endif +}; + +// These are ordered like the bits in a common fenv.h header file. +ENUM_CLASS(RealFlag, InvalidArgument, Denorm, DivideByZero, Overflow, Underflow, + Inexact) +using RealFlags = common::EnumSet; + +} // namespace Fortran::common +#endif /* FORTRAN_COMMON_TARGET_ROUNDING_H_ */ diff --git a/flang/include/flang/Evaluate/common.h b/flang/include/flang/Evaluate/common.h index d493e5fe04417..915e95169c7f8 100644 --- a/flang/include/flang/Evaluate/common.h +++ b/flang/include/flang/Evaluate/common.h @@ -16,6 +16,7 @@ #include "flang/Common/idioms.h" #include "flang/Common/indirection.h" #include "flang/Common/restorer.h" +#include "flang/Common/target-rounding.h" #include "flang/Parser/char-block.h" #include "flang/Parser/message.h" #include @@ -32,6 +33,8 @@ class IntrinsicProcTable; class TargetCharacteristics; using common::ConstantSubscript; +using common::RealFlag; +using common::RealFlags; using common::RelationalOperator; // Integers are always ordered; reals may not be. @@ -128,11 +131,6 @@ static constexpr bool Satisfies(RelationalOperator op, Relation relation) { return false; // silence g++ warning } -// These are ordered like the bits in a common fenv.h header file. -ENUM_CLASS(RealFlag, InvalidArgument, Denorm, DivideByZero, Overflow, Underflow, - Inexact) -using RealFlags = common::EnumSet; - template struct ValueWithRealFlags { A AccumulateFlags(RealFlags &f) { f |= flags; diff --git a/flang/include/flang/Evaluate/target.h b/flang/include/flang/Evaluate/target.h index b347c549e012d..9b5c4db022aa0 100644 --- a/flang/include/flang/Evaluate/target.h +++ b/flang/include/flang/Evaluate/target.h @@ -15,24 +15,13 @@ #include "flang/Common/Fortran.h" #include "flang/Common/enum-class.h" #include "flang/Common/enum-set.h" +#include "flang/Common/target-rounding.h" #include "flang/Evaluate/common.h" #include namespace Fortran::evaluate { -// Floating-point rounding control -struct Rounding { - common::RoundingMode mode{common::RoundingMode::TiesToEven}; - // When set, emulate status flag behavior peculiar to x86 - // (viz., fail to set the Underflow flag when an inexact product of a - // multiplication is rounded up to a normal number from a subnormal - // in some rounding modes) -#if __x86_64__ || __riscv || __loongarch__ - bool x86CompatibleBehavior{true}; -#else - bool x86CompatibleBehavior{false}; -#endif -}; +using common::Rounding; ENUM_CLASS(IeeeFeature, Denormal, Divide, Flags, Halting, Inf, Io, NaN, Rounding, Sqrt, Standard, Subnormal, UnderflowControl) diff --git a/flang/include/flang/Runtime/cpp-type.h b/flang/include/flang/Runtime/cpp-type.h index f6180b4a9ab60..b8c543d34e6fa 100644 --- a/flang/include/flang/Runtime/cpp-type.h +++ b/flang/include/flang/Runtime/cpp-type.h @@ -11,7 +11,7 @@ #ifndef FORTRAN_RUNTIME_CPP_TYPE_H_ #define FORTRAN_RUNTIME_CPP_TYPE_H_ -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include "flang/Common/float128.h" #include "flang/Common/float80.h" #include "flang/Common/uint128.h" diff --git a/flang/include/flang/Runtime/type-code.h b/flang/include/flang/Runtime/type-code.h index 8e7314e0af1ef..dd3a9f2690ee7 100644 --- a/flang/include/flang/Runtime/type-code.h +++ b/flang/include/flang/Runtime/type-code.h @@ -9,7 +9,7 @@ #ifndef FORTRAN_RUNTIME_TYPE_CODE_H_ #define FORTRAN_RUNTIME_TYPE_CODE_H_ -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include "flang/Common/optional.h" #include "flang/ISO_Fortran_binding_wrapper.h" #include diff --git a/flang/runtime/format.h b/flang/runtime/format.h index 5329f2482d3e4..815bf70685e64 100644 --- a/flang/runtime/format.h +++ b/flang/runtime/format.h @@ -13,7 +13,7 @@ #include "environment.h" #include "io-error.h" -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include "flang/Common/optional.h" #include "flang/Decimal/decimal.h" #include "flang/Runtime/freestanding-tools.h" diff --git a/flang/runtime/non-tbp-dio.h b/flang/runtime/non-tbp-dio.h index 05038a264ed99..8429d790fea57 100644 --- a/flang/runtime/non-tbp-dio.h +++ b/flang/runtime/non-tbp-dio.h @@ -22,7 +22,8 @@ #ifndef FORTRAN_RUNTIME_NON_TBP_DIO_H_ #define FORTRAN_RUNTIME_NON_TBP_DIO_H_ -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" +#include "flang/Common/api-attrs.h" #include namespace Fortran::runtime::typeInfo { diff --git a/flang/runtime/type-info.h b/flang/runtime/type-info.h index c3f3595e32ef2..32403b1db5169 100644 --- a/flang/runtime/type-info.h +++ b/flang/runtime/type-info.h @@ -13,7 +13,7 @@ // flang/module/__fortran_type_info.f90. #include "terminator.h" -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include "flang/Common/bit-population-count.h" #include "flang/Common/optional.h" #include "flang/Runtime/descriptor.h" diff --git a/flang/unittests/Evaluate/fp-testing.cpp b/flang/unittests/Evaluate/fp-testing.cpp index 94d8d5086d000..1a1d7425d5824 100644 --- a/flang/unittests/Evaluate/fp-testing.cpp +++ b/flang/unittests/Evaluate/fp-testing.cpp @@ -7,8 +7,8 @@ #include #endif +using Fortran::common::RealFlag; using Fortran::common::RoundingMode; -using Fortran::evaluate::RealFlag; ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment( #if __x86_64__ diff --git a/flang/unittests/Evaluate/fp-testing.h b/flang/unittests/Evaluate/fp-testing.h index 22dfa2d7d80c6..9091963a99b32 100644 --- a/flang/unittests/Evaluate/fp-testing.h +++ b/flang/unittests/Evaluate/fp-testing.h @@ -1,12 +1,12 @@ #ifndef FORTRAN_TEST_EVALUATE_FP_TESTING_H_ #define FORTRAN_TEST_EVALUATE_FP_TESTING_H_ -#include "flang/Evaluate/target.h" +#include "flang/Common/target-rounding.h" #include +using Fortran::common::RealFlags; +using Fortran::common::Rounding; using Fortran::common::RoundingMode; -using Fortran::evaluate::RealFlags; -using Fortran::evaluate::Rounding; class ScopedHostFloatingPointEnvironment { public: diff --git a/flang/unittests/Runtime/Complex.cpp b/flang/unittests/Runtime/Complex.cpp index 46f3ad2f2712b..d714da24dc4e5 100644 --- a/flang/unittests/Runtime/Complex.cpp +++ b/flang/unittests/Runtime/Complex.cpp @@ -13,7 +13,7 @@ #pragma clang diagnostic ignored "-Wc99-extensions" #endif -#include "flang/Common/Fortran.h" +#include "flang/Common/Fortran-consts.h" #include "flang/Runtime/cpp-type.h" #include "flang/Runtime/entry-names.h"