[PATCH] D134859: [clang][Interp] Implement basic support for floating point values
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 23 00:32:19 PST 2022
tbaeder added a comment.
In D134859#3945709 <https://reviews.llvm.org/D134859#3945709>, @sepavloff wrote:
> In D134859#3943926 <https://reviews.llvm.org/D134859#3943926>, @tbaeder wrote:
>
>> FYI, I noticed the way the floating values are serialized doesn't work if the `APFloat` heap-allocated anything; those values aren't preserved through (de)serialization of course.
>>
>> Reproducer:
>>
>> constexpr double foo() {
>> return __LDBL_MIN__;
>> }
>
> The return statement returns a value of type `long double` while the function returns `double`. If `long double` is wider than `double`, truncation occurs, may be this is the reason?
It also happens when the function returns a `long double`.
I tracked the problem down to the way we emit byte code into a `std::vector`. It's simply `reintepret_cast'`ed to `const char *`, so that won't work for `APFloat`.
This change seems to fix the problem:
diff --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
index ff2136d34872..61e9662013c3 100644
--- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -163,8 +163,15 @@ static void emit(Program &P, std::vector<char> &Code, const T &Val,
}
if constexpr (!std::is_pointer_v<T>) {
- const char *Data = reinterpret_cast<const char *>(&Val);
- Code.insert(Code.end(), Data, Data + Size);
+ if constexpr (std::is_trivially_copyable_v<T>) {
+ const char *Data = reinterpret_cast<const char *>(&Val);
+ Code.insert(Code.end(), Data, Data + Size);
+ } else {
+ // Construct the value directly into our storage vector.
+ size_t ValPos = Code.size();
+ Code.resize(Code.size() + Size);
+ new (Code.data() + ValPos) T(Val);
+ }
} else {
uint32_t ID = P.getOrCreateNativePointer(Val);
const char *Data = reinterpret_cast<const char *>(&ID);
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D134859/new/
https://reviews.llvm.org/D134859
More information about the cfe-commits
mailing list