[PATCH] D109108: [flang] Use CMake to determine endianness.

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 1 16:18:34 PDT 2021


Meinersbur created this revision.
Meinersbur added reviewers: ijan1, awarzynski, Leporacanthicus, kiranchandramohan, sscalpone, tskeith, clementval.
Meinersbur added a project: Flang.
Herald added subscribers: jdoerfert, pengfei, mgorny.
Meinersbur requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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

This patch instead uses CMake's TestBigEndian <https://cmake.org/cmake/help/latest/module/TestBigEndian.html> 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).

Fixes llvm.org/PR51597


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109108

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


Index: flang/runtime/environment.h
===================================================================
--- flang/runtime/environment.h
+++ 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
Index: flang/include/flang/Evaluate/common.h
===================================================================
--- flang/include/flang/Evaluate/common.h
+++ flang/include/flang/Evaluate/common.h
@@ -133,9 +133,9 @@
 
 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
Index: flang/CMakeLists.txt
===================================================================
--- flang/CMakeLists.txt
+++ flang/CMakeLists.txt
@@ -295,6 +295,14 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109108.370095.patch
Type: text/x-patch
Size: 1587 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210901/8961c7b7/attachment.bin>


More information about the llvm-commits mailing list