[PATCH] D138554: [clang][Interp] Use placement new to construct opcode arguments into bytecode vector

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 23 01:56:26 PST 2022


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik, sepavloff.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is split-out from https://reviews.llvm.org/D134859 as it showed up there.

When the opcode argument is not trivially copyable, we can't just memcpy it into our code vector. Use placement new to copy it instead.

This is currently dead code without https://reviews.llvm.org/D134859.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138554

Files:
  clang/lib/AST/Interp/ByteCodeEmitter.cpp


Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -163,8 +163,15 @@
   }
 
   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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138554.477420.patch
Type: text/x-patch
Size: 884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221123/1e396708/attachment.bin>


More information about the cfe-commits mailing list