diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 465f3f4e670c2..ea4b9a328da63 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2765,7 +2765,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, } // Apply `nonnull`, `dereferenceable(N)` and `align N` to the `this` argument, - // unless this is a thunk function. + // unless this is a thunk function. Add dead_on_return to the `this` argument + // in base class destructors. // FIXME: fix this properly, https://reviews.llvm.org/D100388 if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() && !FI.arg_begin()->type->isVoidPointerType() && !IsThunk) { @@ -2798,6 +2799,18 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, .getAsAlign(); Attrs.addAlignmentAttr(Alignment); + if (isa_and_nonnull( + CalleeInfo.getCalleeDecl().getDecl())) { + auto *ClassDecl = dyn_cast( + CalleeInfo.getCalleeDecl().getDecl()->getDeclContext()); + assert( + ClassDecl && + "Expected CXXDestructorDecl to have a CXXRecordDecl as its parent."); + if (ClassDecl->getNumBases() == 0 && ClassDecl->getNumVBases() == 0) { + Attrs.addAttribute(llvm::Attribute::DeadOnReturn); + } + } + ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs); }