Skip to content

[clang][bytecode][NFC] Get rid of const_casts in Move fns #105698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2024

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Descriptor.cpp (+10-13)
  • (modified) clang/lib/AST/ByteCode/Descriptor.h (+1-1)
diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 47b8885c9ae379..170203fe818775 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -31,10 +31,9 @@ static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
 }
 
 template <typename T>
-static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
+static void moveTy(Block *, std::byte *Src, std::byte *Dst,
                    const Descriptor *) {
-  // FIXME: Get rid of the const_cast.
-  auto *SrcPtr = reinterpret_cast<T *>(const_cast<std::byte *>(Src));
+  auto *SrcPtr = reinterpret_cast<T *>(Src);
   auto *DstPtr = reinterpret_cast<T *>(Dst);
   new (DstPtr) T(std::move(*SrcPtr));
 }
@@ -63,11 +62,9 @@ static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
 }
 
 template <typename T>
-static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst,
+static void moveArrayTy(Block *, std::byte *Src, std::byte *Dst,
                         const Descriptor *D) {
-  // FIXME: Get rid of the const_cast.
-  InitMapPtr &SrcIMP =
-      *reinterpret_cast<InitMapPtr *>(const_cast<std::byte *>(Src));
+  InitMapPtr &SrcIMP = *reinterpret_cast<InitMapPtr *>(Src);
   if (SrcIMP) {
     // We only ever invoke the moveFunc when moving block contents to a
     // DeadBlock. DeadBlocks don't need InitMaps, so we destroy them here.
@@ -76,7 +73,7 @@ static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst,
   Src += sizeof(InitMapPtr);
   Dst += sizeof(InitMapPtr);
   for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
-    auto *SrcPtr = &reinterpret_cast<T *>(const_cast<std::byte *>(Src))[I];
+    auto *SrcPtr = &reinterpret_cast<T *>(Src)[I];
     auto *DstPtr = &reinterpret_cast<T *>(Dst)[I];
     new (DstPtr) T(std::move(*SrcPtr));
   }
@@ -126,7 +123,7 @@ static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
   }
 }
 
-static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst,
+static void moveArrayDesc(Block *B, std::byte *Src, std::byte *Dst,
                           const Descriptor *D) {
   const unsigned NumElems = D->getNumElems();
   const unsigned ElemSize =
@@ -134,11 +131,11 @@ static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst,
 
   unsigned ElemOffset = 0;
   for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
-    const auto *SrcPtr = Src + ElemOffset;
+    auto *SrcPtr = Src + ElemOffset;
     auto *DstPtr = Dst + ElemOffset;
 
-    const auto *SrcDesc = reinterpret_cast<const InlineDescriptor *>(SrcPtr);
-    const auto *SrcElemLoc = reinterpret_cast<const std::byte *>(SrcDesc + 1);
+    auto *SrcDesc = reinterpret_cast<InlineDescriptor *>(SrcPtr);
+    auto *SrcElemLoc = reinterpret_cast<std::byte *>(SrcDesc + 1);
     auto *DstDesc = reinterpret_cast<InlineDescriptor *>(DstPtr);
     auto *DstElemLoc = reinterpret_cast<std::byte *>(DstDesc + 1);
 
@@ -233,7 +230,7 @@ static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
     destroyBase(B, Ptr, F.Desc, F.Offset);
 }
 
-static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst,
+static void moveRecord(Block *B, std::byte *Src, std::byte *Dst,
                        const Descriptor *D) {
   assert(D);
   assert(D->ElemRecord);
diff --git a/clang/lib/AST/ByteCode/Descriptor.h b/clang/lib/AST/ByteCode/Descriptor.h
index 41899c3bd6831a..82f90430f7f4e5 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -44,7 +44,7 @@ using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
 /// blocks are persisted: the move function copies all inline descriptors and
 /// non-trivial fields, as existing pointers might need to reference those
 /// descriptors. Data is not copied since it cannot be legally read.
-using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
+using BlockMoveFn = void (*)(Block *Storage, std::byte *SrcFieldPtr,
                              std::byte *DstFieldPtr,
                              const Descriptor *FieldDesc);
 

@tbaederr tbaederr merged commit a69ba0a into llvm:main Aug 23, 2024
9 of 11 checks passed
cjdb pushed a commit to cjdb/llvm-project that referenced this pull request Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants