Skip to content

Commit 4f8dc16

Browse files
committed
Revert r366422: [OpenCL] Improve destructor support in C++ for OpenCL
Reason: this commit causes crashes in the clang compiler when building LLVM Support with libc++, see https://bugs.llvm.org/show_bug.cgi?id=42665 for details. llvm-svn: 366429
1 parent 8f5b44a commit 4f8dc16

14 files changed

+113
-234
lines changed

clang/include/clang/AST/DeclCXX.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -2232,38 +2232,30 @@ class CXXMethodDecl : public FunctionDecl {
22322232

22332233
overridden_method_range overridden_methods() const;
22342234

2235-
/// Return the parent of this method declaration, which
2235+
/// Returns the parent of this method declaration, which
22362236
/// is the class in which this method is defined.
22372237
const CXXRecordDecl *getParent() const {
22382238
return cast<CXXRecordDecl>(FunctionDecl::getParent());
22392239
}
22402240

2241-
/// Return the parent of this method declaration, which
2241+
/// Returns the parent of this method declaration, which
22422242
/// is the class in which this method is defined.
22432243
CXXRecordDecl *getParent() {
22442244
return const_cast<CXXRecordDecl *>(
22452245
cast<CXXRecordDecl>(FunctionDecl::getParent()));
22462246
}
22472247

2248-
/// Return the type of the \c this pointer.
2248+
/// Returns the type of the \c this pointer.
22492249
///
22502250
/// Should only be called for instance (i.e., non-static) methods. Note
22512251
/// that for the call operator of a lambda closure type, this returns the
22522252
/// desugared 'this' type (a pointer to the closure type), not the captured
22532253
/// 'this' type.
22542254
QualType getThisType() const;
22552255

2256-
/// Return the type of the object pointed by \c this.
2257-
///
2258-
/// See getThisType() for usage restriction.
2259-
QualType getThisObjectType() const;
2260-
22612256
static QualType getThisType(const FunctionProtoType *FPT,
22622257
const CXXRecordDecl *Decl);
22632258

2264-
static QualType getThisObjectType(const FunctionProtoType *FPT,
2265-
const CXXRecordDecl *Decl);
2266-
22672259
Qualifiers getMethodQualifiers() const {
22682260
return getType()->getAs<FunctionProtoType>()->getMethodQuals();
22692261
}

clang/lib/AST/DeclCXX.cpp

+3-22
Original file line numberDiff line numberDiff line change
@@ -2253,23 +2253,12 @@ CXXMethodDecl::overridden_methods() const {
22532253
return getASTContext().overridden_methods(this);
22542254
}
22552255

2256-
static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2257-
const CXXRecordDecl *Decl) {
2258-
QualType ClassTy = C.getTypeDeclType(Decl);
2259-
return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2260-
}
2261-
22622256
QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
22632257
const CXXRecordDecl *Decl) {
22642258
ASTContext &C = Decl->getASTContext();
2265-
QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2266-
return C.getPointerType(ObjectTy);
2267-
}
2268-
2269-
QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2270-
const CXXRecordDecl *Decl) {
2271-
ASTContext &C = Decl->getASTContext();
2272-
return ::getThisObjectType(C, FPT, Decl);
2259+
QualType ClassTy = C.getTypeDeclType(Decl);
2260+
ClassTy = C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2261+
return C.getPointerType(ClassTy);
22732262
}
22742263

22752264
QualType CXXMethodDecl::getThisType() const {
@@ -2284,14 +2273,6 @@ QualType CXXMethodDecl::getThisType() const {
22842273
getParent());
22852274
}
22862275

2287-
QualType CXXMethodDecl::getThisObjectType() const {
2288-
// Ditto getThisType.
2289-
assert(isInstance() && "No 'this' for static methods!");
2290-
2291-
return CXXMethodDecl::getThisObjectType(getType()->getAs<FunctionProtoType>(),
2292-
getParent());
2293-
}
2294-
22952276
bool CXXMethodDecl::hasInlineBody() const {
22962277
// If this function is a template instantiation, look at the template from
22972278
// which it was instantiated.

clang/lib/CodeGen/CGCXXABI.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class CGCXXABI {
378378
virtual void EmitDestructorCall(CodeGenFunction &CGF,
379379
const CXXDestructorDecl *DD, CXXDtorType Type,
380380
bool ForVirtualBase, bool Delegating,
381-
Address This, QualType ThisTy) = 0;
381+
Address This) = 0;
382382

383383
/// Emits the VTable definitions required for the given record type.
384384
virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
@@ -421,15 +421,11 @@ class CGCXXABI {
421421
llvm::Type *Ty,
422422
SourceLocation Loc) = 0;
423423

424-
using DeleteOrMemberCallExpr =
425-
llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
426-
427424
/// Emit the ABI-specific virtual destructor call.
428-
virtual llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
429-
const CXXDestructorDecl *Dtor,
430-
CXXDtorType DtorType,
431-
Address This,
432-
DeleteOrMemberCallExpr E) = 0;
425+
virtual llvm::Value *
426+
EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor,
427+
CXXDtorType DtorType, Address This,
428+
const CXXMemberCallExpr *CE) = 0;
433429

434430
virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
435431
GlobalDecl GD,

clang/lib/CodeGen/CGCall.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3502,7 +3502,7 @@ struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
35023502
const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
35033503
assert(!Dtor->isTrivial());
35043504
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
3505-
/*Delegating=*/false, Addr, Ty);
3505+
/*Delegating=*/false, Addr);
35063506
} else {
35073507
CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));
35083508
}

clang/lib/CodeGen/CGClass.cpp

+15-25
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,12 @@ namespace {
491491
cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
492492

493493
const CXXDestructorDecl *D = BaseClass->getDestructor();
494-
// We are already inside a destructor, so presumably the object being
495-
// destroyed should have the expected type.
496-
QualType ThisTy = D->getThisObjectType();
497494
Address Addr =
498495
CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThisAddress(),
499496
DerivedClass, BaseClass,
500497
BaseIsVirtual);
501498
CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
502-
/*Delegating=*/false, Addr, ThisTy);
499+
/*Delegating=*/false, Addr);
503500
}
504501
};
505502

@@ -1443,11 +1440,9 @@ void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
14431440
if (DtorType == Dtor_Deleting) {
14441441
RunCleanupsScope DtorEpilogue(*this);
14451442
EnterDtorCleanups(Dtor, Dtor_Deleting);
1446-
if (HaveInsertPoint()) {
1447-
QualType ThisTy = Dtor->getThisObjectType();
1443+
if (HaveInsertPoint())
14481444
EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
1449-
/*Delegating=*/false, LoadCXXThisAddress(), ThisTy);
1450-
}
1445+
/*Delegating=*/false, LoadCXXThisAddress());
14511446
return;
14521447
}
14531448

@@ -1478,9 +1473,8 @@ void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
14781473
EnterDtorCleanups(Dtor, Dtor_Complete);
14791474

14801475
if (!isTryBody) {
1481-
QualType ThisTy = Dtor->getThisObjectType();
14821476
EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
1483-
/*Delegating=*/false, LoadCXXThisAddress(), ThisTy);
1477+
/*Delegating=*/false, LoadCXXThisAddress());
14841478
break;
14851479
}
14861480

@@ -2019,7 +2013,7 @@ void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
20192013
const CXXDestructorDecl *dtor = record->getDestructor();
20202014
assert(!dtor->isTrivial());
20212015
CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
2022-
/*Delegating=*/false, addr, type);
2016+
/*Delegating=*/false, addr);
20232017
}
20242018

20252019
void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
@@ -2369,11 +2363,8 @@ namespace {
23692363
: Dtor(D), Addr(Addr), Type(Type) {}
23702364

23712365
void Emit(CodeGenFunction &CGF, Flags flags) override {
2372-
// We are calling the destructor from within the constructor.
2373-
// Therefore, "this" should have the expected type.
2374-
QualType ThisTy = Dtor->getThisObjectType();
23752366
CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
2376-
/*Delegating=*/true, Addr, ThisTy);
2367+
/*Delegating=*/true, Addr);
23772368
}
23782369
};
23792370
} // end anonymous namespace
@@ -2411,32 +2402,31 @@ CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor
24112402
void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
24122403
CXXDtorType Type,
24132404
bool ForVirtualBase,
2414-
bool Delegating, Address This,
2415-
QualType ThisTy) {
2405+
bool Delegating,
2406+
Address This) {
24162407
CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
2417-
Delegating, This, ThisTy);
2408+
Delegating, This);
24182409
}
24192410

24202411
namespace {
24212412
struct CallLocalDtor final : EHScopeStack::Cleanup {
24222413
const CXXDestructorDecl *Dtor;
24232414
Address Addr;
2424-
QualType Ty;
24252415

2426-
CallLocalDtor(const CXXDestructorDecl *D, Address Addr, QualType Ty)
2427-
: Dtor(D), Addr(Addr), Ty(Ty) {}
2416+
CallLocalDtor(const CXXDestructorDecl *D, Address Addr)
2417+
: Dtor(D), Addr(Addr) {}
24282418

24292419
void Emit(CodeGenFunction &CGF, Flags flags) override {
24302420
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
24312421
/*ForVirtualBase=*/false,
2432-
/*Delegating=*/false, Addr, Ty);
2422+
/*Delegating=*/false, Addr);
24332423
}
24342424
};
24352425
} // end anonymous namespace
24362426

24372427
void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
2438-
QualType T, Address Addr) {
2439-
EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr, T);
2428+
Address Addr) {
2429+
EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
24402430
}
24412431

24422432
void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) {
@@ -2446,7 +2436,7 @@ void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) {
24462436

24472437
const CXXDestructorDecl *D = ClassDecl->getDestructor();
24482438
assert(D && D->isUsed() && "destructor not marked as used!");
2449-
PushDestructorCleanup(D, T, Addr);
2439+
PushDestructorCleanup(D, Addr);
24502440
}
24512441

24522442
void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) {

clang/lib/CodeGen/CGDecl.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,11 @@ namespace {
480480

481481
template <class Derived>
482482
struct DestroyNRVOVariable : EHScopeStack::Cleanup {
483-
DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag)
484-
: NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
483+
DestroyNRVOVariable(Address addr, llvm::Value *NRVOFlag)
484+
: NRVOFlag(NRVOFlag), Loc(addr) {}
485485

486486
llvm::Value *NRVOFlag;
487487
Address Loc;
488-
QualType Ty;
489488

490489
void Emit(CodeGenFunction &CGF, Flags flags) override {
491490
// Along the exceptions path we always execute the dtor.
@@ -512,24 +511,26 @@ namespace {
512511

513512
struct DestroyNRVOVariableCXX final
514513
: DestroyNRVOVariable<DestroyNRVOVariableCXX> {
515-
DestroyNRVOVariableCXX(Address addr, QualType type,
516-
const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag)
517-
: DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag),
518-
Dtor(Dtor) {}
514+
DestroyNRVOVariableCXX(Address addr, const CXXDestructorDecl *Dtor,
515+
llvm::Value *NRVOFlag)
516+
: DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, NRVOFlag),
517+
Dtor(Dtor) {}
519518

520519
const CXXDestructorDecl *Dtor;
521520

522521
void emitDestructorCall(CodeGenFunction &CGF) {
523522
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
524523
/*ForVirtualBase=*/false,
525-
/*Delegating=*/false, Loc, Ty);
524+
/*Delegating=*/false, Loc);
526525
}
527526
};
528527

529528
struct DestroyNRVOVariableC final
530529
: DestroyNRVOVariable<DestroyNRVOVariableC> {
531530
DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
532-
: DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {}
531+
: DestroyNRVOVariable<DestroyNRVOVariableC>(addr, NRVOFlag), Ty(Ty) {}
532+
533+
QualType Ty;
533534

534535
void emitDestructorCall(CodeGenFunction &CGF) {
535536
CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
@@ -1939,7 +1940,7 @@ void CodeGenFunction::emitAutoVarTypeCleanup(
19391940
if (emission.NRVOFlag) {
19401941
assert(!type->isArrayType());
19411942
CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1942-
EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor,
1943+
EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, dtor,
19431944
emission.NRVOFlag);
19441945
return;
19451946
}

clang/lib/CodeGen/CGExprCXX.cpp

+7-24
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "CodeGenFunction.h"
1314
#include "CGCUDARuntime.h"
1415
#include "CGCXXABI.h"
1516
#include "CGDebugInfo.h"
1617
#include "CGObjCRuntime.h"
17-
#include "CodeGenFunction.h"
1818
#include "ConstantEmitter.h"
19-
#include "TargetInfo.h"
2019
#include "clang/Basic/CodeGenOptions.h"
2120
#include "clang/CodeGen/CGFunctionInfo.h"
2221
#include "llvm/IR/Intrinsics.h"
@@ -91,26 +90,12 @@ RValue CodeGenFunction::EmitCXXMemberOrOperatorCall(
9190
}
9291

9392
RValue CodeGenFunction::EmitCXXDestructorCall(
94-
GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This, QualType ThisTy,
93+
GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This,
9594
llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE) {
96-
const CXXMethodDecl *DtorDecl = cast<CXXMethodDecl>(Dtor.getDecl());
97-
98-
assert(!ThisTy.isNull());
99-
assert(ThisTy->getAsCXXRecordDecl() == DtorDecl->getParent() &&
100-
"Pointer/Object mixup");
101-
102-
LangAS SrcAS = ThisTy.getAddressSpace();
103-
LangAS DstAS = DtorDecl->getMethodQualifiers().getAddressSpace();
104-
if (SrcAS != DstAS) {
105-
QualType DstTy = DtorDecl->getThisType();
106-
llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy);
107-
This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, DstAS,
108-
NewType);
109-
}
110-
11195
CallArgList Args;
112-
commonEmitCXXMemberOrOperatorCall(*this, DtorDecl, This, ImplicitParam,
113-
ImplicitParamTy, CE, Args, nullptr);
96+
commonEmitCXXMemberOrOperatorCall(*this, cast<CXXMethodDecl>(Dtor.getDecl()),
97+
This, ImplicitParam, ImplicitParamTy, CE,
98+
Args, nullptr);
11499
return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee,
115100
ReturnValueSlot(), Args);
116101
}
@@ -360,9 +345,7 @@ RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(
360345
Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(GD, Ty), GD);
361346
}
362347

363-
QualType ThisTy =
364-
IsArrow ? Base->getType()->getPointeeType() : Base->getType();
365-
EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy,
348+
EmitCXXDestructorCall(GD, Callee, This.getPointer(),
366349
/*ImplicitParam=*/nullptr,
367350
/*ImplicitParamTy=*/QualType(), nullptr);
368351
}
@@ -1900,7 +1883,7 @@ static void EmitObjectDelete(CodeGenFunction &CGF,
19001883
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
19011884
/*ForVirtualBase=*/false,
19021885
/*Delegating=*/false,
1903-
Ptr, ElementType);
1886+
Ptr);
19041887
else if (auto Lifetime = ElementType.getObjCLifetime()) {
19051888
switch (Lifetime) {
19061889
case Qualifiers::OCL_None:

clang/lib/CodeGen/CodeGenFunction.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,7 @@ class CodeGenFunction : public CodeGenTypeCache {
675675
/// PushDestructorCleanup - Push a cleanup to call the
676676
/// complete-object variant of the given destructor on the object at
677677
/// the given address.
678-
void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T,
679-
Address Addr);
678+
void PushDestructorCleanup(const CXXDestructorDecl *Dtor, Address Addr);
680679

681680
/// PopCleanupBlock - Will pop the cleanup entry on the stack and
682681
/// process all branch fixups.
@@ -2555,8 +2554,8 @@ class CodeGenFunction : public CodeGenTypeCache {
25552554
static Destroyer destroyCXXObject;
25562555

25572556
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
2558-
bool ForVirtualBase, bool Delegating, Address This,
2559-
QualType ThisTy);
2557+
bool ForVirtualBase, bool Delegating,
2558+
Address This);
25602559

25612560
void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
25622561
llvm::Type *ElementTy, Address NewPtr,
@@ -3678,9 +3677,9 @@ class CodeGenFunction : public CodeGenTypeCache {
36783677
llvm::Value *ImplicitParam,
36793678
QualType ImplicitParamTy, const CallExpr *E,
36803679
CallArgList *RtlArgs);
3681-
RValue EmitCXXDestructorCall(GlobalDecl Dtor, const CGCallee &Callee,
3682-
llvm::Value *This, QualType ThisTy,
3683-
llvm::Value *ImplicitParam,
3680+
RValue EmitCXXDestructorCall(GlobalDecl Dtor,
3681+
const CGCallee &Callee,
3682+
llvm::Value *This, llvm::Value *ImplicitParam,
36843683
QualType ImplicitParamTy, const CallExpr *E);
36853684
RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
36863685
ReturnValueSlot ReturnValue);

0 commit comments

Comments
 (0)