Skip to content

Commit ef2735d

Browse files
authored
[Flang] Detect endianness in the preprocessor (#132767)
Summary: Currently we use `TestBigEndian` in CMake to determine endianness. This doesn't work on all platforms and is deprecated since CMake 3.20. Instead of using CMake, we can just use the GNU/Clang preprocessor definitions. The only difficulty is MSVC, mostly because they don't support the same macros. But, as far as I'm aware, MSVC / Windows targets are always little endian, and if not we can just override it for that specific target in the future.
1 parent c221d64 commit ef2735d

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

flang/cmake/modules/FlangCommon.cmake

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,16 @@ if (FLANG_RUNTIME_F128_MATH_LIB)
2424
add_compile_definitions(FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}")
2525
endif()
2626

27-
# The NVPTX target can't emit a binary due to the PTXAS dependency, just
28-
# hard-code this.
29-
if ("${LLVM_RUNTIMES_TARGET}" MATCHES "^nvptx")
30-
add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
31-
else ()
32-
# Check if 128-bit float computations can be done via long double
33-
# Note that '-nostdinc++' might be implied when this code kicks in
34-
# (see 'runtimes/CMakeLists.txt'), so we cannot use 'cfloat' C++ header
35-
# file in the test below.
36-
# Compile it as C.
37-
check_c_source_compiles(
38-
"#include <float.h>
39-
#if LDBL_MANT_DIG != 113
40-
#error LDBL_MANT_DIG != 113
41-
#endif
42-
int main() { return 0; }
43-
"
44-
HAVE_LDBL_MANT_DIG_113)
45-
46-
include(TestBigEndian)
47-
test_big_endian(IS_BIGENDIAN)
48-
if (IS_BIGENDIAN)
49-
add_compile_definitions(FLANG_BIG_ENDIAN=1)
50-
else ()
51-
add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
52-
endif ()
53-
endif ()
27+
# Check if 128-bit float computations can be done via long double
28+
# Note that '-nostdinc++' might be implied when this code kicks in
29+
# (see 'runtimes/CMakeLists.txt'), so we cannot use 'cfloat' C++ header
30+
# file in the test below.
31+
# Compile it as C.
32+
check_c_source_compiles(
33+
"#include <float.h>
34+
#if LDBL_MANT_DIG != 113
35+
#error LDBL_MANT_DIG != 113
36+
#endif
37+
int main() { return 0; }
38+
"
39+
HAVE_LDBL_MANT_DIG_113)

flang/include/flang/Common/api-attrs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,21 @@
189189
#define RT_OPTNONE_ATTR
190190
#endif
191191

192+
/* Detect system endianness if it was not explicitly set. */
193+
#if !defined(FLANG_LITTLE_ENDIAN) && !defined(FLANG_BIG_ENDIAN)
194+
195+
/* We always assume Windows is little endian, otherwise use the GCC compatible
196+
* flags. */
197+
#if defined(_MSC_VER) || defined(_WIN32)
198+
#define FLANG_LITTLE_ENDIAN 1
199+
#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
200+
#define FLANG_LITTLE_ENDIAN 1
201+
#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
202+
#define FLANG_BIG_ENDIAN 1
203+
#else
204+
#error "Unknown or unsupported endianness."
205+
#endif
206+
207+
#endif /* !defined(FLANG_LITTLE_ENDIAN) && !defined(FLANG_BIG_ENDIAN) */
208+
192209
#endif /* !FORTRAN_RUNTIME_API_ATTRS_H_ */

flang/include/flang/Evaluate/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef FORTRAN_EVALUATE_COMMON_H_
1010
#define FORTRAN_EVALUATE_COMMON_H_
1111

12+
#include "flang/Common/api-attrs.h"
1213
#include "flang/Common/enum-set.h"
1314
#include "flang/Common/idioms.h"
1415
#include "flang/Common/indirection.h"

0 commit comments

Comments
 (0)