[libc-commits] [PATCH] D148739: [libc] Use __builtin_bit_cast only when src and dest types are trivially copyable
Mikhail Ramalho via Phabricator via libc-commits
libc-commits at lists.llvm.org
Wed Apr 19 13:04:55 PDT 2023
mikhail.ramalho created this revision.
mikhail.ramalho added reviewers: michaelrj, sivachandra.
Herald added subscribers: libc-commits, asb, ecnelises, tschuett, s.egerton, PkmX, simoncook, arichardson.
Herald added projects: libc-project, All.
mikhail.ramalho requested review of this revision.
Herald added a subscriber: pcwang-thead.
This patch fixes the following compilation error:
/home/mgadelha/tools/llvm-project/libc/src/__support/CPP/bit.h:29:10: error: __builtin_bit_cast source type must be trivially copyable
return __builtin_bit_cast(To, from);
^
/home/mgadelha/tools/llvm-project/libc/src/__support/FPUtil/FPBits.h:119:47: note: in instantiation of function template specialization '__llvm_libc::cpp::bit_cast<long double, __llvm_libc::cpp::UInt<128>>' requested here
LIBC_INLINE T get_val() const { return cpp::bit_cast<T>(bits); }
This happen when building for a 32 bit target (in my case it was
riscv32) without int128 support, so we use UInt<128> instead, which
is not trivially copyable.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D148739
Files:
libc/src/__support/CPP/bit.h
Index: libc/src/__support/CPP/bit.h
===================================================================
--- libc/src/__support/CPP/bit.h
+++ libc/src/__support/CPP/bit.h
@@ -11,6 +11,8 @@
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
+#include <type_traits>
+
namespace __llvm_libc::cpp {
#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
@@ -26,8 +28,10 @@
template <class To, class From> constexpr To bit_cast(const From &from) {
static_assert(sizeof(To) == sizeof(From), "To and From must be of same size");
#if defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
- return __builtin_bit_cast(To, from);
-#else
+ if constexpr (std::is_trivially_copyable_v<To> &&
+ std::is_trivially_copyable_v<From>)
+ return __builtin_bit_cast(To, from);
+#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
To to;
char *dst = reinterpret_cast<char *>(&to);
const char *src = reinterpret_cast<const char *>(&from);
@@ -38,7 +42,6 @@
dst[i] = src[i];
#endif // defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE)
return to;
-#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
}
} // namespace __llvm_libc::cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148739.515057.patch
Type: text/x-patch
Size: 1136 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230419/b1d186c0/attachment.bin>
More information about the libc-commits
mailing list