Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,10 @@ bool CIRGenModule::MayDropFunctionReturn(const ASTContext &astContext,
QualType ReturnType) {
// We can't just disard the return value for a record type with a complex
// destructor or a non-trivially copyable type.
if (ReturnType.getCanonicalType()->getAs<RecordType>()) {
llvm_unreachable("NYI");
if (const RecordType *rt =
ReturnType.getCanonicalType()->getAs<RecordType>()) {
if (const auto *classDecl = dyn_cast<CXXRecordDecl>(rt->getDecl()))
return classDecl->hasTrivialDestructor();
}

return ReturnType.isTriviallyCopyableType(astContext);
Expand Down
33 changes: 33 additions & 0 deletions clang/test/CIR/CodeGen/return.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fno-strict-return -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-NOSTRICT
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fno-strict-return -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG-CHECK-NOSTRICT

int &ret0(int &x) {
return x;
Expand Down Expand Up @@ -31,3 +33,34 @@ int unreachable_after_return() {
// CHECK-NEXT: cir.store{{.*}} %3, %0 : !s32i, !cir.ptr<!s32i>
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: }

struct NonTrivialDefaultConstructor {
int x;

NonTrivialDefaultConstructor() { }
};

// CHECK-NOSTRICT-LABEL: @_Z28nonTrivialDefaultConstructorv
// OGCG-CHECK-NOSTRICT-LABEL: @_Z28nonTrivialDefaultConstructorv
NonTrivialDefaultConstructor nonTrivialDefaultConstructor() {
// CHECK-NOSTRICT-NOT: call void @llvm.trap
// CHECK-NOSTRICT-NOT: unreachable
// OGCG-CHECK-NOSTRICT-NOT: call void @llvm.trap
// OGCG-CHECK-NOSTRICT-NOT: unreachable
}

// Functions that return records with non-trivial destructors should always use
// the -fstrict-return optimization.

struct NonTrivialDestructor {
~NonTrivialDestructor();
};

// CHECK-NOSTRICT-LABEL: @_Z20nonTrivialDestructorv
// OGCG-CHECK-NOSTRICT-LABEL: @_Z20nonTrivialDestructorv
NonTrivialDestructor nonTrivialDestructor() {
// CHECK-NOSTRICT: call void @llvm.trap
// CHECK-NOSTRICT-NEXT: unreachable
// OGCG-CHECK-NOSTRICT: call void @llvm.trap
// OGCG-CHECK-NOSTRICT-NEXT: unreachable
}