Skip to content

Commit 0cda970

Browse files
authored
[Flang][NFC] Split common headers to reduce dependencies. (#110244)
Fortran.h and target.h are defining symbols where some are used by both, the Fortran runtime (Flang-RT) and Fortran compiler (Flang), and others are used by Flang only. With the upcoming refactoring of the Fortran runtime into its own subproject (#110217), move the declarations that are used by both into new headers to minimize the amount of code that will need to be shared by Flang-RT and Flang. Details: * `Fortran.h`: Flang-RT only uses some enum definitions out of this file, but not `AsFortran` which is defined in `Fortran.cpp`. Moving the enums into `Fortran-consts.h` allows keeping `Fortran.cpp` within Flang. * `target.h`: Contains some floating-point definitions that is used by the non-GTest unittests in `fp-testing.h`. Flang-RT also uses some non-GTest as well. Moving those definitions avoids the dependence on the entire FortranEvaluate library.
1 parent 17dfdd3 commit 0cda970

File tree

14 files changed

+99
-54
lines changed

14 files changed

+99
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- include/flang/Common/Fortran-consts.h -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_COMMON_FORTRAN_CONSTS_H_
10+
#define FORTRAN_COMMON_FORTRAN_CONSTS_H_
11+
12+
#include "flang/Common/enum-class.h"
13+
#include <cstdint>
14+
15+
namespace Fortran::common {
16+
17+
// Fortran has five kinds of intrinsic data types, plus the derived types.
18+
ENUM_CLASS(TypeCategory, Integer, Real, Complex, Character, Logical, Derived)
19+
ENUM_CLASS(VectorElementCategory, Integer, Unsigned, Real)
20+
21+
ENUM_CLASS(IoStmtKind, None, Backspace, Close, Endfile, Flush, Inquire, Open,
22+
Print, Read, Rewind, Wait, Write)
23+
24+
// Defined I/O variants
25+
ENUM_CLASS(
26+
DefinedIo, ReadFormatted, ReadUnformatted, WriteFormatted, WriteUnformatted)
27+
28+
// Fortran arrays may have up to 15 dimensions (See Fortran 2018 section 5.4.6).
29+
static constexpr int maxRank{15};
30+
31+
// Floating-point rounding modes; these are packed into a byte to save
32+
// room in the runtime's format processing context structure. These
33+
// enumerators are defined with the corresponding values returned from
34+
// llvm.get.rounding.
35+
enum class RoundingMode : std::uint8_t {
36+
ToZero, // ROUND=ZERO, RZ - truncation
37+
TiesToEven, // ROUND=NEAREST, RN - default IEEE rounding
38+
Up, // ROUND=UP, RU
39+
Down, // ROUND=DOWN, RD
40+
TiesAwayFromZero, // ROUND=COMPATIBLE, RC - ties round away from zero
41+
};
42+
43+
} // namespace Fortran::common
44+
#endif /* FORTRAN_COMMON_FORTRAN_CONSTS_H_ */

flang/include/flang/Common/Fortran.h

+1-25
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

1515
#include "enum-set.h"
1616
#include "idioms.h"
17+
#include "flang/Common/Fortran-consts.h"
1718
#include <cinttypes>
1819
#include <optional>
1920
#include <string>
2021

2122
namespace Fortran::common {
2223
class LanguageFeatureControl;
2324

24-
// Fortran has five kinds of intrinsic data types, plus the derived types.
25-
ENUM_CLASS(TypeCategory, Integer, Real, Complex, Character, Logical, Derived)
26-
ENUM_CLASS(VectorElementCategory, Integer, Unsigned, Real)
27-
2825
constexpr bool IsNumericTypeCategory(TypeCategory category) {
2926
return category == TypeCategory::Integer || category == TypeCategory::Real ||
3027
category == TypeCategory::Complex;
@@ -47,9 +44,6 @@ const char *AsFortran(RelationalOperator);
4744

4845
ENUM_CLASS(Intent, Default, In, Out, InOut)
4946

50-
ENUM_CLASS(IoStmtKind, None, Backspace, Close, Endfile, Flush, Inquire, Open,
51-
Print, Read, Rewind, Wait, Write)
52-
5347
// Union of specifiers for all I/O statements.
5448
ENUM_CLASS(IoSpecKind, Access, Action, Advance, Asynchronous, Blank, Decimal,
5549
Delim, Direct, Encoding, End, Eor, Err, Exist, File, Fmt, Form, Formatted,
@@ -61,29 +55,11 @@ ENUM_CLASS(IoSpecKind, Access, Action, Advance, Asynchronous, Blank, Decimal,
6155
Dispose, // nonstandard
6256
)
6357

64-
// Defined I/O variants
65-
ENUM_CLASS(
66-
DefinedIo, ReadFormatted, ReadUnformatted, WriteFormatted, WriteUnformatted)
6758
const char *AsFortran(DefinedIo);
6859

69-
// Floating-point rounding modes; these are packed into a byte to save
70-
// room in the runtime's format processing context structure. These
71-
// enumerators are defined with the corresponding values returned from
72-
// llvm.get.rounding.
73-
enum class RoundingMode : std::uint8_t {
74-
ToZero, // ROUND=ZERO, RZ - truncation
75-
TiesToEven, // ROUND=NEAREST, RN - default IEEE rounding
76-
Up, // ROUND=UP, RU
77-
Down, // ROUND=DOWN, RD
78-
TiesAwayFromZero, // ROUND=COMPATIBLE, RC - ties round away from zero
79-
};
80-
8160
// Fortran label. Must be in [1..99999].
8261
using Label = std::uint64_t;
8362

84-
// Fortran arrays may have up to 15 dimensions (See Fortran 2018 section 5.4.6).
85-
static constexpr int maxRank{15};
86-
8763
// CUDA subprogram attribute combinations
8864
ENUM_CLASS(CUDASubprogramAttrs, Host, Device, HostDevice, Global, Grid_Global)
8965

flang/include/flang/Common/format.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define FORTRAN_COMMON_FORMAT_H_
1111

1212
#include "enum-set.h"
13-
#include "flang/Common/Fortran.h"
13+
#include "flang/Common/Fortran-consts.h"
1414
#include <cstring>
1515

1616
// Define a FormatValidator class template to validate a format expression
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- include/flang/Common/target-rounding.h ------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_COMMON_TARGET_ROUNDING_H_
10+
#define FORTRAN_COMMON_TARGET_ROUNDING_H_
11+
12+
#include "flang/Common/Fortran-consts.h"
13+
#include "flang/Common/enum-set.h"
14+
15+
namespace Fortran::common {
16+
17+
// Floating-point rounding control
18+
struct Rounding {
19+
common::RoundingMode mode{common::RoundingMode::TiesToEven};
20+
// When set, emulate status flag behavior peculiar to x86
21+
// (viz., fail to set the Underflow flag when an inexact product of a
22+
// multiplication is rounded up to a normal number from a subnormal
23+
// in some rounding modes)
24+
#if __x86_64__ || __riscv || __loongarch__
25+
bool x86CompatibleBehavior{true};
26+
#else
27+
bool x86CompatibleBehavior{false};
28+
#endif
29+
};
30+
31+
// These are ordered like the bits in a common fenv.h header file.
32+
ENUM_CLASS(RealFlag, InvalidArgument, Denorm, DivideByZero, Overflow, Underflow,
33+
Inexact)
34+
using RealFlags = common::EnumSet<RealFlag, RealFlag_enumSize>;
35+
36+
} // namespace Fortran::common
37+
#endif /* FORTRAN_COMMON_TARGET_ROUNDING_H_ */

flang/include/flang/Evaluate/common.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "flang/Common/idioms.h"
1717
#include "flang/Common/indirection.h"
1818
#include "flang/Common/restorer.h"
19+
#include "flang/Common/target-rounding.h"
1920
#include "flang/Parser/char-block.h"
2021
#include "flang/Parser/message.h"
2122
#include <cinttypes>
@@ -32,6 +33,8 @@ class IntrinsicProcTable;
3233
class TargetCharacteristics;
3334

3435
using common::ConstantSubscript;
36+
using common::RealFlag;
37+
using common::RealFlags;
3538
using common::RelationalOperator;
3639

3740
// Integers are always ordered; reals may not be.
@@ -128,11 +131,6 @@ static constexpr bool Satisfies(RelationalOperator op, Relation relation) {
128131
return false; // silence g++ warning
129132
}
130133

131-
// These are ordered like the bits in a common fenv.h header file.
132-
ENUM_CLASS(RealFlag, InvalidArgument, Denorm, DivideByZero, Overflow, Underflow,
133-
Inexact)
134-
using RealFlags = common::EnumSet<RealFlag, RealFlag_enumSize>;
135-
136134
template <typename A> struct ValueWithRealFlags {
137135
A AccumulateFlags(RealFlags &f) {
138136
f |= flags;

flang/include/flang/Evaluate/target.h

+2-13
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,13 @@
1515
#include "flang/Common/Fortran.h"
1616
#include "flang/Common/enum-class.h"
1717
#include "flang/Common/enum-set.h"
18+
#include "flang/Common/target-rounding.h"
1819
#include "flang/Evaluate/common.h"
1920
#include <cstdint>
2021

2122
namespace Fortran::evaluate {
2223

23-
// Floating-point rounding control
24-
struct Rounding {
25-
common::RoundingMode mode{common::RoundingMode::TiesToEven};
26-
// When set, emulate status flag behavior peculiar to x86
27-
// (viz., fail to set the Underflow flag when an inexact product of a
28-
// multiplication is rounded up to a normal number from a subnormal
29-
// in some rounding modes)
30-
#if __x86_64__ || __riscv || __loongarch__
31-
bool x86CompatibleBehavior{true};
32-
#else
33-
bool x86CompatibleBehavior{false};
34-
#endif
35-
};
24+
using common::Rounding;
3625

3726
ENUM_CLASS(IeeeFeature, Denormal, Divide, Flags, Halting, Inf, Io, NaN,
3827
Rounding, Sqrt, Standard, Subnormal, UnderflowControl)

flang/include/flang/Runtime/cpp-type.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#ifndef FORTRAN_RUNTIME_CPP_TYPE_H_
1212
#define FORTRAN_RUNTIME_CPP_TYPE_H_
1313

14-
#include "flang/Common/Fortran.h"
14+
#include "flang/Common/Fortran-consts.h"
1515
#include "flang/Common/float128.h"
1616
#include "flang/Common/float80.h"
1717
#include "flang/Common/uint128.h"

flang/include/flang/Runtime/type-code.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef FORTRAN_RUNTIME_TYPE_CODE_H_
1010
#define FORTRAN_RUNTIME_TYPE_CODE_H_
1111

12-
#include "flang/Common/Fortran.h"
12+
#include "flang/Common/Fortran-consts.h"
1313
#include "flang/Common/optional.h"
1414
#include "flang/ISO_Fortran_binding_wrapper.h"
1515
#include <utility>

flang/runtime/format.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "environment.h"
1515
#include "io-error.h"
16-
#include "flang/Common/Fortran.h"
16+
#include "flang/Common/Fortran-consts.h"
1717
#include "flang/Common/optional.h"
1818
#include "flang/Decimal/decimal.h"
1919
#include "flang/Runtime/freestanding-tools.h"

flang/runtime/non-tbp-dio.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#ifndef FORTRAN_RUNTIME_NON_TBP_DIO_H_
2323
#define FORTRAN_RUNTIME_NON_TBP_DIO_H_
2424

25-
#include "flang/Common/Fortran.h"
25+
#include "flang/Common/Fortran-consts.h"
26+
#include "flang/Common/api-attrs.h"
2627
#include <cstddef>
2728

2829
namespace Fortran::runtime::typeInfo {

flang/runtime/type-info.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// flang/module/__fortran_type_info.f90.
1414

1515
#include "terminator.h"
16-
#include "flang/Common/Fortran.h"
16+
#include "flang/Common/Fortran-consts.h"
1717
#include "flang/Common/bit-population-count.h"
1818
#include "flang/Common/optional.h"
1919
#include "flang/Runtime/descriptor.h"

flang/unittests/Evaluate/fp-testing.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <xmmintrin.h>
88
#endif
99

10+
using Fortran::common::RealFlag;
1011
using Fortran::common::RoundingMode;
11-
using Fortran::evaluate::RealFlag;
1212

1313
ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
1414
#if __x86_64__

flang/unittests/Evaluate/fp-testing.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef FORTRAN_TEST_EVALUATE_FP_TESTING_H_
22
#define FORTRAN_TEST_EVALUATE_FP_TESTING_H_
33

4-
#include "flang/Evaluate/target.h"
4+
#include "flang/Common/target-rounding.h"
55
#include <fenv.h>
66

7+
using Fortran::common::RealFlags;
8+
using Fortran::common::Rounding;
79
using Fortran::common::RoundingMode;
8-
using Fortran::evaluate::RealFlags;
9-
using Fortran::evaluate::Rounding;
1010

1111
class ScopedHostFloatingPointEnvironment {
1212
public:

flang/unittests/Runtime/Complex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#pragma clang diagnostic ignored "-Wc99-extensions"
1414
#endif
1515

16-
#include "flang/Common/Fortran.h"
16+
#include "flang/Common/Fortran-consts.h"
1717
#include "flang/Runtime/cpp-type.h"
1818
#include "flang/Runtime/entry-names.h"
1919

0 commit comments

Comments
 (0)