Skip to content

[flang][NFC] Use LLVM's endianness enum #86516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tarunprabhu
Copy link
Contributor

Replace the FLANG_LITTLE_ENDIAN and FLANG_BIG_ENDIAN preprocessor defines with equivalents from LLVM. This ensures that tools that use flang's headers are not required to provide these preorocessor defines themselves.

Replace the FLANG_LITTLE_ENDIAN and FLANG_BIG_ENDIAN preprocessor defines with
equivalents from LLVM. This ensures that tools that use flang's headers are
not required to provide these preorocessor defines themselves.
@tarunprabhu tarunprabhu added the flang Flang issues not falling into any other category label Mar 25, 2024
Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link

✅ With the latest revision this PR passed the C/C++ code formatter.

std::uint64_t high_{0}, low_{0};
#else
#error host endianness is not known
std::uint64_t low_{0}, high_{0};
Copy link
Contributor

Choose a reason for hiding this comment

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

You're just defaulting to little-endianness here if BYTE_ORDER is undefined. This can be worse than the current situation.

#else
#error host endianness is not known
#endif
constexpr bool isHostLittleEndian =
Copy link
Contributor

Choose a reason for hiding this comment

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

This initialization should use braces and does not need parentheses.

@tarunprabhu tarunprabhu marked this pull request as ready for review March 26, 2024 21:30
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2024

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-runtime

Author: Tarun Prabhu (tarunprabhu)

Changes

Replace the FLANG_LITTLE_ENDIAN and FLANG_BIG_ENDIAN preprocessor defines with equivalents from LLVM. This ensures that tools that use flang's headers are not required to provide these preorocessor defines themselves.


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

3 Files Affected:

  • (modified) flang/include/flang/Common/uint128.h (+3-4)
  • (modified) flang/include/flang/Evaluate/common.h (+3-7)
  • (modified) flang/runtime/environment.h (+4-7)
diff --git a/flang/include/flang/Common/uint128.h b/flang/include/flang/Common/uint128.h
index 03e44eb6997d5b..4a9e5fb2e44c00 100644
--- a/flang/include/flang/Common/uint128.h
+++ b/flang/include/flang/Common/uint128.h
@@ -20,6 +20,7 @@
 #endif
 
 #include "leading-zero-bit-count.h"
+#include "llvm/ADT/bit.h"
 #include <cstdint>
 #include <type_traits>
 
@@ -261,12 +262,10 @@ template <bool IS_SIGNED = false> class Int128 {
     }
   }
   static constexpr std::uint64_t topBit{std::uint64_t{1} << 63};
-#if FLANG_LITTLE_ENDIAN
-  std::uint64_t low_{0}, high_{0};
-#elif FLANG_BIG_ENDIAN
+#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
   std::uint64_t high_{0}, low_{0};
 #else
-#error host endianness is not known
+  std::uint64_t low_{0}, high_{0};
 #endif
 };
 
diff --git a/flang/include/flang/Evaluate/common.h b/flang/include/flang/Evaluate/common.h
index d04c901929e74b..39f63311e7944e 100644
--- a/flang/include/flang/Evaluate/common.h
+++ b/flang/include/flang/Evaluate/common.h
@@ -18,6 +18,7 @@
 #include "flang/Common/restorer.h"
 #include "flang/Parser/char-block.h"
 #include "flang/Parser/message.h"
+#include "llvm/ADT/bit.h"
 #include <cinttypes>
 #include <map>
 #include <set>
@@ -142,13 +143,8 @@ template <typename A> struct ValueWithRealFlags {
   RealFlags flags{};
 };
 
-#if FLANG_BIG_ENDIAN
-constexpr bool isHostLittleEndian{false};
-#elif FLANG_LITTLE_ENDIAN
-constexpr bool isHostLittleEndian{true};
-#else
-#error host endianness is not known
-#endif
+constexpr bool isHostLittleEndian =
+    (llvm::endianness::native == llvm::endianness::little);
 
 // HostUnsignedInt<BITS> finds the smallest native unsigned integer type
 // whose size is >= BITS.
diff --git a/flang/runtime/environment.h b/flang/runtime/environment.h
index 9bc1158509615f..f42312d73f439c 100644
--- a/flang/runtime/environment.h
+++ b/flang/runtime/environment.h
@@ -12,19 +12,16 @@
 #include "flang/Common/optional.h"
 #include "flang/Decimal/decimal.h"
 
+#include "llvm/ADT/bit.h"
+
 struct EnvironmentDefaultList;
 
 namespace Fortran::runtime {
 
 class Terminator;
 
-#if FLANG_BIG_ENDIAN
-constexpr bool isHostLittleEndian{false};
-#elif FLANG_LITTLE_ENDIAN
-constexpr bool isHostLittleEndian{true};
-#else
-#error host endianness is not known
-#endif
+constexpr bool isHostLittleEndian =
+    (llvm::endianness::native == llvm::endianness::little);
 
 // External unformatted I/O data conversions
 enum class Convert { Unknown, Native, LittleEndian, BigEndian, Swap };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using flang for external tools requires specifying host endianness
3 participants