[clang] [clang][bytecode] Implement IntegralAP bitcasting (PR #114471)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 16:13:52 PDT 2024


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/114471

>From dcc03dcb4f1fed9fe871c904738a4f49c0716cc4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 31 Oct 2024 23:14:41 +0100
Subject: [PATCH] [clang][bytecode] Implement IntegralAP bitcasting

Only for full-byte bitwidths for now.
---
 clang/lib/AST/ByteCode/IntegralAP.h          | 8 ++++++--
 clang/test/AST/ByteCode/builtin-bit-cast.cpp | 5 +++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/IntegralAP.h b/clang/lib/AST/ByteCode/IntegralAP.h
index 6a37d0a3ba4a51..8ee08dfb5cfe74 100644
--- a/clang/lib/AST/ByteCode/IntegralAP.h
+++ b/clang/lib/AST/ByteCode/IntegralAP.h
@@ -171,10 +171,14 @@ template <bool Signed> class IntegralAP final {
     return IntegralAP<false>(Copy);
   }
 
-  void bitcastToMemory(std::byte *Dest) const { assert(false); }
+  void bitcastToMemory(std::byte *Dest) const {
+    llvm::StoreIntToMemory(V, (uint8_t *)Dest, bitWidth() / 8);
+  }
 
   static IntegralAP bitcastFromMemory(const std::byte *Src, unsigned BitWidth) {
-    return IntegralAP();
+    APInt V(BitWidth, static_cast<uint64_t>(0), Signed);
+    llvm::LoadIntFromMemory(V, (const uint8_t *)Src, BitWidth / 8);
+    return IntegralAP(V);
   }
 
   ComparisonCategoryResult compare(const IntegralAP &RHS) const {
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 58cf486833412f..0e86da9133b33d 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -71,6 +71,11 @@ constexpr bool operator==(const struct bits<N, T, P>& lhs, const struct bits<N,
   return lhs.bits == rhs.bits;
 }
 
+#ifdef __SIZEOF_INT128__
+static_assert(check_round_trip<__int128_t>((__int128_t)34));
+static_assert(check_round_trip<__int128_t>((__int128_t)-34));
+#endif
+
 
 namespace simple {
   constexpr int A = __builtin_bit_cast(int, 10);



More information about the cfe-commits mailing list