Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch replaces the manual endian detection logic in ADT/bit.h
with CMake's CMAKE_CXX_BYTE_ORDER, freeing ourselves from the work
that the build system is already capable of.

The public interface, llvm::endianness, remains unchanged.

This patch replaces the manual endian detection logic in ADT/bit.h
with CMake's CMAKE_CXX_BYTE_ORDER, freeing ourselves from the work
that the build system is already capable of.

The public interface, llvm::endianness, remains unchanged.
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This patch replaces the manual endian detection logic in ADT/bit.h
with CMake's CMAKE_CXX_BYTE_ORDER, freeing ourselves from the work
that the build system is already capable of.

The public interface, llvm::endianness, remains unchanged.


Full diff: https://github.com/llvm/llvm-project/pull/164054.diff

3 Files Affected:

  • (modified) llvm/CMakeLists.txt (+9)
  • (modified) llvm/include/llvm/ADT/bit.h (+2-27)
  • (modified) llvm/include/llvm/Config/config.h.cmake (+3)
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index c450ee5a3d72e..37873544c52c7 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -1079,6 +1079,15 @@ if(NOT LLVM_ENABLE_NEW_PASS_MANAGER)
                       " no longer supported.")
 endif()
 
+# Determine host endianness.
+if(CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN")
+  set(LLVM_HOST_IS_BIG_ENDIAN 1)
+elseif(CMAKE_CXX_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
+  set(LLVM_HOST_IS_BIG_ENDIAN 0)
+else()
+  message(FATAL_ERROR "Unsupported CMAKE_CXX_BYTE_ORDER: ${CMAKE_CXX_BYTE_ORDER}")
+endif()
+
 include(HandleLLVMOptions)
 
 ######
diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h
index 66c4f94813241..37d9c931c0792 100644
--- a/llvm/include/llvm/ADT/bit.h
+++ b/llvm/include/llvm/ADT/bit.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_BIT_H
 #define LLVM_ADT_BIT_H
 
+#include "llvm/Config/config.h"
 #include "llvm/Support/Compiler.h"
 #include <cstddef> // for std::size_t
 #include <cstdint>
@@ -28,32 +29,6 @@
 #include <cstdlib>  // for _byteswap_{ushort,ulong,uint64}
 #endif
 
-#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) ||            \
-    defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) ||  \
-    defined(__OpenBSD__) || defined(__DragonFly__) || defined(__managarm__)
-#include <endian.h>
-#elif defined(_AIX)
-#include <sys/machine.h>
-#elif defined(__sun)
-/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
-#include <sys/types.h>
-#define BIG_ENDIAN 4321
-#define LITTLE_ENDIAN 1234
-#if defined(_BIG_ENDIAN)
-#define BYTE_ORDER BIG_ENDIAN
-#else
-#define BYTE_ORDER LITTLE_ENDIAN
-#endif
-#elif defined(__MVS__)
-#define BIG_ENDIAN 4321
-#define LITTLE_ENDIAN 1234
-#define BYTE_ORDER BIG_ENDIAN
-#else
-#if !defined(BYTE_ORDER) && !defined(_WIN32)
-#include <machine/endian.h>
-#endif
-#endif
-
 #ifdef _MSC_VER
 // Declare these intrinsics manually rather including intrin.h. It's very
 // expensive, and bit.h is popular via MathExtras.h.
@@ -71,7 +46,7 @@ namespace llvm {
 enum class endianness {
   big,
   little,
-#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
+#if LLVM_HOST_IS_BIG_ENDIAN
   native = big
 #else
   native = little
diff --git a/llvm/include/llvm/Config/config.h.cmake b/llvm/include/llvm/Config/config.h.cmake
index ce83de8e4cba9..7a21cf9bcde4f 100644
--- a/llvm/include/llvm/Config/config.h.cmake
+++ b/llvm/include/llvm/Config/config.h.cmake
@@ -10,6 +10,9 @@
 /* Bug report URL. */
 #define BUG_REPORT_URL "${BUG_REPORT_URL}"
 
+/* Define to 1 if the host is a big endian machine, and to 0 otherwise. */
+#cmakedefine01 LLVM_HOST_IS_BIG_ENDIAN
+
 /* Define to 1 to enable backtraces, and to 0 otherwise. */
 #cmakedefine01 ENABLE_BACKTRACES
 

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this compatible with bazel? I know the support level is much lower than cmake, but maybe worth checking that they can do something sensible here.

@kazutakahirata
Copy link
Contributor Author

Is this compatible with bazel? I know the support level is much lower than cmake, but maybe worth checking that they can do something sensible here.

Yes. For bazel, config.h is a static file, and #defines are hardcoded, supplied locally with some macro tricks, or from the command line -DFOO=BAR.

@kazutakahirata
Copy link
Contributor Author

@kuhar Shall we scrap this PR? While we could clean up the existing code with CMake's endian detection, we'll probably move to C++20 within one or two years. (We've switched to C++17 on August 5, 2022 b135650) Once we adopt C++20, we can easily replace the whole block with using endianness = std::endian; to accommodate gradual migration in an arbitrary order without touching any other file. That is, garbage collection is much easier than touching this file and removing the gadgets that this PR is trying to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants