Skip to content

Commit d7b669b

Browse files
mizvekovtstellar
authored andcommitted
[clang] don't mark as Elidable CXXConstruct expressions used in NRVO
See PR51862. The consumers of the Elidable flag in CXXConstructExpr assume that an elidable construction just goes through a single copy/move construction, so that the source object is immediately passed as an argument and is the same type as the parameter itself. With the implementation of P2266 and after some adjustments to the implementation of P1825, we started (correctly, as per standard) allowing more cases where the copy initialization goes through user defined conversions. With this patch we stop using this flag in NRVO contexts, to preserve code that relies on that assumption. This causes no known functional changes, we just stop firing some asserts in a cople of included test cases. Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D109800 (cherry picked from commit d9308aa)
1 parent ee6913c commit d7b669b

13 files changed

+122
-33
lines changed

clang/include/clang/Sema/Initialization.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ class alignas(8) InitializedEntity {
298298

299299
/// Create the initialization entity for the result of a function.
300300
static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
301-
QualType Type, bool NRVO) {
302-
return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
301+
QualType Type) {
302+
return InitializedEntity(EK_Result, ReturnLoc, Type);
303303
}
304304

305305
static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc,
@@ -308,20 +308,20 @@ class alignas(8) InitializedEntity {
308308
}
309309

310310
static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
311-
QualType Type, bool NRVO) {
312-
return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
311+
QualType Type) {
312+
return InitializedEntity(EK_BlockElement, BlockVarLoc, Type);
313313
}
314314

315315
static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc,
316-
QualType Type, bool NRVO) {
316+
QualType Type) {
317317
return InitializedEntity(EK_LambdaToBlockConversionBlockElement,
318-
BlockVarLoc, Type, NRVO);
318+
BlockVarLoc, Type);
319319
}
320320

321321
/// Create the initialization entity for an exception object.
322322
static InitializedEntity InitializeException(SourceLocation ThrowLoc,
323-
QualType Type, bool NRVO) {
324-
return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
323+
QualType Type) {
324+
return InitializedEntity(EK_Exception, ThrowLoc, Type);
325325
}
326326

327327
/// Create the initialization entity for an object allocated via new.

clang/lib/AST/ExprConstant.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -9931,10 +9931,19 @@ bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
99319931
return false;
99329932

99339933
// Avoid materializing a temporary for an elidable copy/move constructor.
9934-
if (E->isElidable() && !ZeroInit)
9935-
if (const MaterializeTemporaryExpr *ME
9936-
= dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
9934+
if (E->isElidable() && !ZeroInit) {
9935+
// FIXME: This only handles the simplest case, where the source object
9936+
// is passed directly as the first argument to the constructor.
9937+
// This should also handle stepping though implicit casts and
9938+
// and conversion sequences which involve two steps, with a
9939+
// conversion operator followed by a converting constructor.
9940+
const Expr *SrcObj = E->getArg(0);
9941+
assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
9942+
assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
9943+
if (const MaterializeTemporaryExpr *ME =
9944+
dyn_cast<MaterializeTemporaryExpr>(SrcObj))
99379945
return Visit(ME->getSubExpr());
9946+
}
99389947

99399948
if (ZeroInit && !ZeroInitialization(E, T))
99409949
return false;

clang/lib/CodeGen/CGExprCXX.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,18 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
609609
return;
610610

611611
// Elide the constructor if we're constructing from a temporary.
612-
// The temporary check is required because Sema sets this on NRVO
613-
// returns.
614612
if (getLangOpts().ElideConstructors && E->isElidable()) {
615-
assert(getContext().hasSameUnqualifiedType(E->getType(),
616-
E->getArg(0)->getType()));
617-
if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
618-
EmitAggExpr(E->getArg(0), Dest);
619-
return;
620-
}
613+
// FIXME: This only handles the simplest case, where the source object
614+
// is passed directly as the first argument to the constructor.
615+
// This should also handle stepping though implicit casts and
616+
// conversion sequences which involve two steps, with a
617+
// conversion operator followed by a converting constructor.
618+
const Expr *SrcObj = E->getArg(0);
619+
assert(SrcObj->isTemporaryObject(getContext(), CD->getParent()));
620+
assert(
621+
getContext().hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
622+
EmitAggExpr(SrcObj, Dest);
623+
return;
621624
}
622625

623626
if (const ArrayType *arrayType

clang/lib/Sema/Sema.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ static void checkEscapingByref(VarDecl *VD, Sema &S) {
20102010
Expr *VarRef =
20112011
new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc);
20122012
ExprResult Result;
2013-
auto IE = InitializedEntity::InitializeBlock(Loc, T, false);
2013+
auto IE = InitializedEntity::InitializeBlock(Loc, T);
20142014
if (S.getLangOpts().CPlusPlus2b) {
20152015
auto *E = ImplicitCastExpr::Create(S.Context, T, CK_NoOp, VarRef, nullptr,
20162016
VK_XValue, FPOptionsOverride());

clang/lib/Sema/SemaCoroutine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
15331533
if (GroType->isVoidType()) {
15341534
// Trigger a nice error message.
15351535
InitializedEntity Entity =
1536-
InitializedEntity::InitializeResult(Loc, FnRetType, false);
1536+
InitializedEntity::InitializeResult(Loc, FnRetType);
15371537
S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue);
15381538
noteMemberDeclaredHere(S, ReturnValue, Fn);
15391539
return false;

clang/lib/Sema/SemaDeclCXX.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -15262,8 +15262,17 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1526215262
// can be omitted by constructing the temporary object
1526315263
// directly into the target of the omitted copy/move
1526415264
if (ConstructKind == CXXConstructExpr::CK_Complete && Constructor &&
15265+
// FIXME: Converting constructors should also be accepted.
15266+
// But to fix this, the logic that digs down into a CXXConstructExpr
15267+
// to find the source object needs to handle it.
15268+
// Right now it assumes the source object is passed directly as the
15269+
// first argument.
1526515270
Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) {
1526615271
Expr *SubExpr = ExprArgs[0];
15272+
// FIXME: Per above, this is also incorrect if we want to accept
15273+
// converting constructors, as isTemporaryObject will
15274+
// reject temporaries with different type from the
15275+
// CXXRecord itself.
1526715276
Elidable = SubExpr->isTemporaryObject(
1526815277
Context, cast<CXXRecordDecl>(FoundDecl->getDeclContext()));
1526915278
}

clang/lib/Sema/SemaExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15683,7 +15683,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc,
1568315683
if (!Result.isInvalid()) {
1568415684
Result = PerformCopyInitialization(
1568515685
InitializedEntity::InitializeBlock(Var->getLocation(),
15686-
Cap.getCaptureType(), false),
15686+
Cap.getCaptureType()),
1568715687
Loc, Result.get());
1568815688
}
1568915689

clang/lib/Sema/SemaExprCXX.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,8 @@ ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
893893
if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex))
894894
return ExprError();
895895

896-
InitializedEntity Entity = InitializedEntity::InitializeException(
897-
OpLoc, ExceptionObjectTy,
898-
/*NRVO=*/NRInfo.isCopyElidable());
896+
InitializedEntity Entity =
897+
InitializedEntity::InitializeException(OpLoc, ExceptionObjectTy);
899898
ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRInfo, Ex);
900899
if (Res.isInvalid())
901900
return ExprError();

clang/lib/Sema/SemaLambda.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1975,8 +1975,7 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
19751975
CallOperator->markUsed(Context);
19761976

19771977
ExprResult Init = PerformCopyInitialization(
1978-
InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType(),
1979-
/*NRVO=*/false),
1978+
InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType()),
19801979
CurrentLocation, Src);
19811980
if (!Init.isInvalid())
19821981
Init = ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);

clang/lib/Sema/SemaObjCProperty.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1467,8 +1467,7 @@ Decl *Sema::ActOnPropertyImplDecl(Scope *S,
14671467
LoadSelfExpr, true, true);
14681468
ExprResult Res = PerformCopyInitialization(
14691469
InitializedEntity::InitializeResult(PropertyDiagLoc,
1470-
getterMethod->getReturnType(),
1471-
/*NRVO=*/false),
1470+
getterMethod->getReturnType()),
14721471
PropertyDiagLoc, IvarRefExpr);
14731472
if (!Res.isInvalid()) {
14741473
Expr *ResExpr = Res.getAs<Expr>();

clang/lib/Sema/SemaStmt.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -3653,8 +3653,8 @@ StmtResult Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc,
36533653

36543654
// In C++ the return statement is handled via a copy initialization.
36553655
// the C version of which boils down to CheckSingleAssignmentConstraints.
3656-
InitializedEntity Entity = InitializedEntity::InitializeResult(
3657-
ReturnLoc, FnRetType, NRVOCandidate != nullptr);
3656+
InitializedEntity Entity =
3657+
InitializedEntity::InitializeResult(ReturnLoc, FnRetType);
36583658
ExprResult Res = PerformMoveOrCopyInitialization(
36593659
Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
36603660
if (Res.isInvalid()) {
@@ -4085,8 +4085,8 @@ StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
40854085
// the C version of which boils down to CheckSingleAssignmentConstraints.
40864086
if (!HasDependentReturnType && !RetValExp->isTypeDependent()) {
40874087
// we have a non-void function with an expression, continue checking
4088-
InitializedEntity Entity = InitializedEntity::InitializeResult(
4089-
ReturnLoc, RetType, NRVOCandidate != nullptr);
4088+
InitializedEntity Entity =
4089+
InitializedEntity::InitializeResult(ReturnLoc, RetType);
40904090
ExprResult Res = PerformMoveOrCopyInitialization(
40914091
Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
40924092
if (Res.isInvalid()) {

clang/test/CodeGen/nrvo-tracking.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,40 @@ X t5() {
282282
}
283283

284284
} // namespace test_alignas
285+
286+
namespace PR51862 {
287+
288+
template <class T> T test() {
289+
T a;
290+
T b;
291+
if (0)
292+
return a;
293+
return b;
294+
}
295+
296+
struct A {
297+
A();
298+
A(A &);
299+
A(int);
300+
operator int();
301+
};
302+
303+
// CHECK-LABEL: define{{.*}} void @_ZN7PR518624testINS_1AEEET_v
304+
// CHECK: call i32 @_ZN7PR518621AcviEv
305+
// CHECK-NEXT: call void @_ZN7PR518621AC1Ei
306+
// CHECK-NEXT: call void @llvm.lifetime.end
307+
template A test<A>();
308+
309+
struct BSub {};
310+
struct B : BSub {
311+
B();
312+
B(B &);
313+
B(const BSub &);
314+
};
315+
316+
// CHECK-LABEL: define{{.*}} void @_ZN7PR518624testINS_1BEEET_v
317+
// CHECK: call void @_ZN7PR518621BC1ERKNS_4BSubE
318+
// CHECK-NEXT: call void @llvm.lifetime.end
319+
template B test<B>();
320+
321+
} // namespace PR51862
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown-gnu -emit-llvm -O1 -fexperimental-new-pass-manager -o - %s | FileCheck %s
2+
3+
template <class T> T test() {
4+
return T();
5+
}
6+
7+
struct A {
8+
A();
9+
A(A &);
10+
A(int);
11+
operator int();
12+
};
13+
14+
// FIXME: There should be copy elision here.
15+
// CHECK-LABEL: define{{.*}} void @_Z4testI1AET_v
16+
// CHECK: call void @_ZN1AC1Ev
17+
// CHECK-NEXT: call i32 @_ZN1AcviEv
18+
// CHECK-NEXT: call void @_ZN1AC1Ei
19+
// CHECK-NEXT: call void @llvm.lifetime.end
20+
template A test<A>();
21+
22+
struct BSub {};
23+
struct B : BSub {
24+
B();
25+
B(B &);
26+
B(const BSub &);
27+
};
28+
29+
// FIXME: There should be copy elision here.
30+
// CHECK-LABEL: define{{.*}} void @_Z4testI1BET_v
31+
// CHECK: call void @_ZN1BC1Ev
32+
// CHECK: call void @_ZN1BC1ERK4BSub
33+
// CHECK-NEXT: call void @llvm.lifetime.end
34+
template B test<B>();

0 commit comments

Comments
 (0)