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

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 15:16:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Only for full-byte bitwidths for now.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/IntegralAP.h (+6-2) 
- (modified) clang/test/AST/ByteCode/builtin-bit-cast.cpp (+5) 


``````````diff
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);

``````````

</details>


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


More information about the cfe-commits mailing list