[llvm] [ADT] Use CMake to detect host endianness (NFC) (PR #164054)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 17 23:53:48 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/164054
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.
>From 7ec118f9726d7175517812888f5f81c3a92f8687 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 12 Oct 2025 22:53:12 -0700
Subject: [PATCH] [ADT] Use CMake to detect host endianness (NFC)
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.
---
llvm/CMakeLists.txt | 9 ++++++++
llvm/include/llvm/ADT/bit.h | 29 ++-----------------------
llvm/include/llvm/Config/config.h.cmake | 3 +++
3 files changed, 14 insertions(+), 27 deletions(-)
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
More information about the llvm-commits
mailing list