[clang] [clang][bytecode] Cleanup primitive descriptor ctor/dtor handling (PR #155401)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 26 05:20:02 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/155401

Use switches instead of if statements and COMPOSITE_TYPE_SWITCH and remove some leftover move functions.

>From b98a518955edd40f3377c9bda74ca84a73e55d91 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Aug 2025 13:58:16 +0200
Subject: [PATCH] [clang][bytecode] Cleanup primitive descriptor ctor/dtor
 handling

Use switches instead of if statements and COMPOSITE_TYPE_SWITCH and
remove some leftover move functions.
---
 clang/lib/AST/ByteCode/Descriptor.cpp | 80 ++++++++++-----------------
 clang/lib/AST/ByteCode/PrimType.h     | 12 ----
 2 files changed, 29 insertions(+), 63 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 68536d4e2d777..647de56b28013 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -49,14 +49,6 @@ static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
   reinterpret_cast<T *>(Ptr)->~T();
 }
 
-template <typename T>
-static void moveTy(Block *, std::byte *Src, std::byte *Dst,
-                   const Descriptor *) {
-  auto *SrcPtr = reinterpret_cast<T *>(Src);
-  auto *DstPtr = reinterpret_cast<T *>(Dst);
-  new (DstPtr) T(std::move(*SrcPtr));
-}
-
 template <typename T>
 static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool, bool, bool,
                         const Descriptor *D) {
@@ -85,28 +77,6 @@ static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
   }
 }
 
-template <typename T>
-static void moveArrayTy(Block *, std::byte *Src, std::byte *Dst,
-                        const Descriptor *D) {
-  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.
-    SrcIMP = std::nullopt;
-  }
-  Src += sizeof(InitMapPtr);
-  Dst += sizeof(InitMapPtr);
-  if constexpr (!needsCtor<T>()) {
-    std::memcpy(Dst, Src, D->getNumElems() * D->getElemSize());
-  } else {
-    for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
-      auto *SrcPtr = &reinterpret_cast<T *>(Src)[I];
-      auto *DstPtr = &reinterpret_cast<T *>(Dst)[I];
-      new (DstPtr) T(std::move(*SrcPtr));
-    }
-  }
-}
-
 static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
                           bool IsMutable, bool IsVolatile, bool IsActive,
                           bool InUnion, const Descriptor *D) {
@@ -144,12 +114,14 @@ static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
       D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
 
   unsigned ElemOffset = 0;
-  for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
+  auto Dtor = D->ElemDesc->DtorFn;
+  assert(Dtor &&
+         "a composite array without an elem dtor shouldn't have a dtor itself");
+  for (unsigned I = 0; I != NumElems; ++I, ElemOffset += ElemSize) {
     auto *ElemPtr = Ptr + ElemOffset;
     auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
     auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
-    if (auto Fn = D->ElemDesc->DtorFn)
-      Fn(B, ElemLoc, D->ElemDesc);
+    Dtor(B, ElemLoc, D->ElemDesc);
   }
 }
 
@@ -265,34 +237,40 @@ static bool needsRecordDtor(const Record *R) {
   return false;
 }
 
-static BlockCtorFn getCtorPrim(PrimType Type) {
-  // Floating types are special. They are primitives, but need their
-  // constructor called.
-  if (Type == PT_Float)
+static BlockCtorFn getCtorPrim(PrimType T) {
+  switch (T) {
+  case PT_Float:
     return ctorTy<PrimConv<PT_Float>::T>;
-  if (Type == PT_IntAP)
+  case PT_IntAP:
     return ctorTy<PrimConv<PT_IntAP>::T>;
-  if (Type == PT_IntAPS)
+  case PT_IntAPS:
     return ctorTy<PrimConv<PT_IntAPS>::T>;
-  if (Type == PT_MemberPtr)
+  case PT_Ptr:
+    return ctorTy<PrimConv<PT_Ptr>::T>;
+  case PT_MemberPtr:
     return ctorTy<PrimConv<PT_MemberPtr>::T>;
-
-  COMPOSITE_TYPE_SWITCH(Type, return ctorTy<T>, return nullptr);
+  default:
+    return nullptr;
+  }
+  llvm_unreachable("Unhandled PrimType");
 }
 
-static BlockDtorFn getDtorPrim(PrimType Type) {
-  // Floating types are special. They are primitives, but need their
-  // destructor called, since they might allocate memory.
-  if (Type == PT_Float)
+static BlockDtorFn getDtorPrim(PrimType T) {
+  switch (T) {
+  case PT_Float:
     return dtorTy<PrimConv<PT_Float>::T>;
-  if (Type == PT_IntAP)
+  case PT_IntAP:
     return dtorTy<PrimConv<PT_IntAP>::T>;
-  if (Type == PT_IntAPS)
+  case PT_IntAPS:
     return dtorTy<PrimConv<PT_IntAPS>::T>;
-  if (Type == PT_MemberPtr)
+  case PT_Ptr:
+    return dtorTy<PrimConv<PT_Ptr>::T>;
+  case PT_MemberPtr:
     return dtorTy<PrimConv<PT_MemberPtr>::T>;
-
-  COMPOSITE_TYPE_SWITCH(Type, return dtorTy<T>, return nullptr);
+  default:
+    return nullptr;
+  }
+  llvm_unreachable("Unhandled PrimType");
 }
 
 static BlockCtorFn getCtorArrayPrim(PrimType Type) {
diff --git a/clang/lib/AST/ByteCode/PrimType.h b/clang/lib/AST/ByteCode/PrimType.h
index 6e3a49cadcbf1..c98240c7dfb6b 100644
--- a/clang/lib/AST/ByteCode/PrimType.h
+++ b/clang/lib/AST/ByteCode/PrimType.h
@@ -271,15 +271,3 @@ static inline bool aligned(const void *P) {
     default:;                                                                  \
     }                                                                          \
   } while (0)
-
-#define COMPOSITE_TYPE_SWITCH(Expr, B, D)                                      \
-  do {                                                                         \
-    switch (Expr) {                                                            \
-      TYPE_SWITCH_CASE(PT_Ptr, B)                                              \
-    default: {                                                                 \
-      D;                                                                       \
-      break;                                                                   \
-    }                                                                          \
-    }                                                                          \
-  } while (0)
-#endif



More information about the cfe-commits mailing list