Skip to content

Commit 3058e88

Browse files
authored
[clang][bytecode] Cleanup primitive descriptor ctor/dtor handling (#155401)
Use switches instead of if statements and COMPOSITE_TYPE_SWITCH and remove some leftover move functions.
1 parent 5586572 commit 3058e88

File tree

2 files changed

+29
-61
lines changed

2 files changed

+29
-61
lines changed

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
4949
reinterpret_cast<T *>(Ptr)->~T();
5050
}
5151

52-
template <typename T>
53-
static void moveTy(Block *, std::byte *Src, std::byte *Dst,
54-
const Descriptor *) {
55-
auto *SrcPtr = reinterpret_cast<T *>(Src);
56-
auto *DstPtr = reinterpret_cast<T *>(Dst);
57-
new (DstPtr) T(std::move(*SrcPtr));
58-
}
59-
6052
template <typename T>
6153
static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool, bool, bool,
6254
const Descriptor *D) {
@@ -85,28 +77,6 @@ static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
8577
}
8678
}
8779

88-
template <typename T>
89-
static void moveArrayTy(Block *, std::byte *Src, std::byte *Dst,
90-
const Descriptor *D) {
91-
InitMapPtr &SrcIMP = *reinterpret_cast<InitMapPtr *>(Src);
92-
if (SrcIMP) {
93-
// We only ever invoke the moveFunc when moving block contents to a
94-
// DeadBlock. DeadBlocks don't need InitMaps, so we destroy them here.
95-
SrcIMP = std::nullopt;
96-
}
97-
Src += sizeof(InitMapPtr);
98-
Dst += sizeof(InitMapPtr);
99-
if constexpr (!needsCtor<T>()) {
100-
std::memcpy(Dst, Src, D->getNumElems() * D->getElemSize());
101-
} else {
102-
for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
103-
auto *SrcPtr = &reinterpret_cast<T *>(Src)[I];
104-
auto *DstPtr = &reinterpret_cast<T *>(Dst)[I];
105-
new (DstPtr) T(std::move(*SrcPtr));
106-
}
107-
}
108-
}
109-
11080
static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
11181
bool IsMutable, bool IsVolatile, bool IsActive,
11282
bool InUnion, const Descriptor *D) {
@@ -144,12 +114,14 @@ static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
144114
D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
145115

146116
unsigned ElemOffset = 0;
147-
for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
117+
auto Dtor = D->ElemDesc->DtorFn;
118+
assert(Dtor &&
119+
"a composite array without an elem dtor shouldn't have a dtor itself");
120+
for (unsigned I = 0; I != NumElems; ++I, ElemOffset += ElemSize) {
148121
auto *ElemPtr = Ptr + ElemOffset;
149122
auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
150123
auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
151-
if (auto Fn = D->ElemDesc->DtorFn)
152-
Fn(B, ElemLoc, D->ElemDesc);
124+
Dtor(B, ElemLoc, D->ElemDesc);
153125
}
154126
}
155127

@@ -265,34 +237,40 @@ static bool needsRecordDtor(const Record *R) {
265237
return false;
266238
}
267239

268-
static BlockCtorFn getCtorPrim(PrimType Type) {
269-
// Floating types are special. They are primitives, but need their
270-
// constructor called.
271-
if (Type == PT_Float)
240+
static BlockCtorFn getCtorPrim(PrimType T) {
241+
switch (T) {
242+
case PT_Float:
272243
return ctorTy<PrimConv<PT_Float>::T>;
273-
if (Type == PT_IntAP)
244+
case PT_IntAP:
274245
return ctorTy<PrimConv<PT_IntAP>::T>;
275-
if (Type == PT_IntAPS)
246+
case PT_IntAPS:
276247
return ctorTy<PrimConv<PT_IntAPS>::T>;
277-
if (Type == PT_MemberPtr)
248+
case PT_Ptr:
249+
return ctorTy<PrimConv<PT_Ptr>::T>;
250+
case PT_MemberPtr:
278251
return ctorTy<PrimConv<PT_MemberPtr>::T>;
279-
280-
COMPOSITE_TYPE_SWITCH(Type, return ctorTy<T>, return nullptr);
252+
default:
253+
return nullptr;
254+
}
255+
llvm_unreachable("Unhandled PrimType");
281256
}
282257

283-
static BlockDtorFn getDtorPrim(PrimType Type) {
284-
// Floating types are special. They are primitives, but need their
285-
// destructor called, since they might allocate memory.
286-
if (Type == PT_Float)
258+
static BlockDtorFn getDtorPrim(PrimType T) {
259+
switch (T) {
260+
case PT_Float:
287261
return dtorTy<PrimConv<PT_Float>::T>;
288-
if (Type == PT_IntAP)
262+
case PT_IntAP:
289263
return dtorTy<PrimConv<PT_IntAP>::T>;
290-
if (Type == PT_IntAPS)
264+
case PT_IntAPS:
291265
return dtorTy<PrimConv<PT_IntAPS>::T>;
292-
if (Type == PT_MemberPtr)
266+
case PT_Ptr:
267+
return dtorTy<PrimConv<PT_Ptr>::T>;
268+
case PT_MemberPtr:
293269
return dtorTy<PrimConv<PT_MemberPtr>::T>;
294-
295-
COMPOSITE_TYPE_SWITCH(Type, return dtorTy<T>, return nullptr);
270+
default:
271+
return nullptr;
272+
}
273+
llvm_unreachable("Unhandled PrimType");
296274
}
297275

298276
static BlockCtorFn getCtorArrayPrim(PrimType Type) {

clang/lib/AST/ByteCode/PrimType.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,4 @@ static inline bool aligned(const void *P) {
272272
} \
273273
} while (0)
274274

275-
#define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
276-
do { \
277-
switch (Expr) { \
278-
TYPE_SWITCH_CASE(PT_Ptr, B) \
279-
default: { \
280-
D; \
281-
break; \
282-
} \
283-
} \
284-
} while (0)
285275
#endif

0 commit comments

Comments
 (0)