[flang-commits] [flang] 84e1258 - [flang] Use CMake to determine endianness.

Michael Kruse via flang-commits flang-commits at lists.llvm.org
Fri Sep 3 13:46:23 PDT 2021


Author: Michael Kruse
Date: 2021-09-03T15:44:51-05:00
New Revision: 84e1258febe1f179e5d4e1914be7b727d2296336

URL: https://github.com/llvm/llvm-project/commit/84e1258febe1f179e5d4e1914be7b727d2296336
DIFF: https://github.com/llvm/llvm-project/commit/84e1258febe1f179e5d4e1914be7b727d2296336.diff

LOG: [flang] Use CMake to determine endianness.

The preprocessor definitions __BYTE_ORDER__, __ORDER_BIG_ENDIAN__, and
__ORDER_LITTLE_ENDIAN__ are gcc extensions (also supported by clang),
but msvc (and others) do not define them. As a result __BYTE_ORDER__
and __ORDER_BIG_ENDIAN__ both evaluate to 0 by the prepreprocessor,
and __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__, the first `#if` condition
to 1, hence assuming the wrong byte order for x86(_64).

This patch instead uses CMake's TestBigEndian module to determine
target architecture's endianness at configure-time.

Note this also uses the same mechanism for the runtime. If compiling
flang as a cross-compiler, the runtime for the compile-target must be
built separately (Flang does not support the LLVM_ENABLE_RUNTIMES
mechanism yet).

Fixes llvm.org/PR51597

Reviewed By: ijan1, Leporacanthicus

Differential Revision: https://reviews.llvm.org/D109108

Added: 
    

Modified: 
    flang/CMakeLists.txt
    flang/include/flang/Evaluate/common.h
    flang/runtime/environment.h

Removed: 
    


################################################################################
diff  --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index f5e602b6b844..13ecc372a2ac 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -295,6 +295,14 @@ if (FLANG_REPOSITORY_STRING)
   add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
 endif()
 
+include(TestBigEndian)
+test_big_endian(IS_BIGENDIAN)
+if (IS_BIGENDIAN)
+  add_compile_definitions(FLANG_BIG_ENDIAN=1)
+else ()
+  add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
+endif ()
+
 # Configure Flang's Version.inc file.
 configure_file(
   ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in

diff  --git a/flang/include/flang/Evaluate/common.h b/flang/include/flang/Evaluate/common.h
index 284014fb5fb9..dd7cb96eb569 100644
--- a/flang/include/flang/Evaluate/common.h
+++ b/flang/include/flang/Evaluate/common.h
@@ -133,9 +133,9 @@ struct Rounding {
 
 static constexpr Rounding defaultRounding;
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#if FLANG_BIG_ENDIAN
 constexpr bool isHostLittleEndian{false};
-#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#elif FLANG_LITTLE_ENDIAN
 constexpr bool isHostLittleEndian{true};
 #else
 #error host endianness is not known

diff  --git a/flang/runtime/environment.h b/flang/runtime/environment.h
index c04c3133f02a..5e6b7b85b727 100644
--- a/flang/runtime/environment.h
+++ b/flang/runtime/environment.h
@@ -14,9 +14,9 @@
 
 namespace Fortran::runtime {
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#if FLANG_BIG_ENDIAN
 constexpr bool isHostLittleEndian{false};
-#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#elif FLANG_LITTLE_ENDIAN
 constexpr bool isHostLittleEndian{true};
 #else
 #error host endianness is not known


        


More information about the flang-commits mailing list