diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 020df6275570..d22594ae8442 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -1494,6 +1494,9 @@ class VarDecl : public DeclaratorDecl, public Redeclarable { NonParmVarDeclBits.EscapingByref = true; } + /// Determines if this variable's alignment is dependent. + bool hasDependentAlignment() const; + /// Retrieve the variable declaration from which this variable could /// be instantiated, if it is an instantiation (rather than a non-template). VarDecl *getTemplateInstantiationPattern() const; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 5047dc19b0c6..a92d3726e847 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -2534,6 +2534,13 @@ bool VarDecl::isNonEscapingByref() const { return hasAttr() && !NonParmVarDeclBits.EscapingByref; } +bool VarDecl::hasDependentAlignment() const { + return getType()->isDependentType() || + llvm::any_of(specific_attrs(), [](const AlignedAttr *AA) { + return AA->isAlignmentDependent(); + }); +} + VarDecl *VarDecl::getTemplateInstantiationPattern() const { const VarDecl *VD = this; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 007546455232..9e1f42a15e55 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13310,16 +13310,6 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { CheckCompleteDecompositionDeclaration(DD); } -/// Determines if a variable's alignment is dependent. -static bool hasDependentAlignment(VarDecl *VD) { - if (VD->getType()->isDependentType()) - return true; - for (auto *I : VD->specific_attrs()) - if (I->isAlignmentDependent()) - return true; - return false; -} - /// Check if VD needs to be dllexport/dllimport due to being in a /// dllexport/import function. void Sema::CheckStaticLocalForDllExport(VarDecl *VD) { @@ -13408,7 +13398,7 @@ void Sema::FinalizeDeclaration(Decl *ThisDecl) { if (unsigned MaxAlign = Context.getTargetInfo().getMaxTLSAlign()) { // Protect the check so that it's not performed on dependent types and // dependent alignments (we can't determine the alignment in that case). - if (VD->getTLSKind() && !hasDependentAlignment(VD) && + if (VD->getTLSKind() && !VD->hasDependentAlignment() && !VD->isInvalidDecl()) { CharUnits MaxAlignChars = Context.toCharUnitsFromBits(MaxAlign); if (Context.getDeclAlign(VD) > MaxAlignChars) { diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 1e86f382f060..506c06b412b6 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -3395,7 +3395,7 @@ Sema::NamedReturnInfo Sema::getNamedReturnInfo(const VarDecl *VD) { // Variables with higher required alignment than their type's ABI // alignment cannot use NRVO. - if (!VDType->isDependentType() && VD->hasAttr() && + if (!VD->hasDependentAlignment() && Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VDType)) Info.S = NamedReturnInfo::MoveEligible; diff --git a/clang/test/CodeGen/nrvo-tracking.cpp b/clang/test/CodeGen/nrvo-tracking.cpp index 7893140e1010..2d6eb9efeca2 100644 --- a/clang/test/CodeGen/nrvo-tracking.cpp +++ b/clang/test/CodeGen/nrvo-tracking.cpp @@ -1,9 +1,9 @@ // RUN: %clang_cc1 -std=c++20 -fblocks -Wno-return-stack-address -triple x86_64-unknown-unknown-gnu -emit-llvm -O1 -fexperimental-new-pass-manager -o - %s | FileCheck %s -struct X { - X(); - X(const X&); - X(X&&); +struct alignas(4) X { + X(); + X(const X &); + X(X &&); }; #define L(A, B, C) void l##A() { \ @@ -210,3 +210,75 @@ void b_attr() { }; }()(); } + +namespace test_alignas { + +template X t1() { + X a [[gnu::aligned(A)]]; + return a; +} + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t1ILi1EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: ret void +template X t1<1>(); + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t1ILi4EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: ret void +template X t1<4>(); + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t1ILi8EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: call {{.*}} @_ZN1XC1EOS_ +// CHECK-NEXT: call void @llvm.lifetime.end +template X t1<8>(); + +template X t2() { + X a [[gnu::aligned(1)]] [[gnu::aligned(A)]] [[gnu::aligned(2)]]; + return a; +} + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t2ILi1EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: ret void +template X t2<1>(); + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t2ILi4EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: ret void +template X t2<4>(); + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t2ILi8EEE1Xv +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: call {{.*}} @_ZN1XC1EOS_ +// CHECK-NEXT: call void @llvm.lifetime.end +template X t2<8>(); + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t3Ev +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: ret void +X t3() { + X a [[gnu::aligned(1)]]; + return a; +} + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t4Ev +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: call {{.*}} @_ZN1XC1EOS_ +// CHECK-NEXT: call void @llvm.lifetime.end +X t4() { + X a [[gnu::aligned(8)]]; + return a; +} + +// CHECK-LABEL: define{{.*}} void @_ZN12test_alignas2t5Ev +// CHECK: call {{.*}} @_ZN1XC1Ev +// CHECK-NEXT: call {{.*}} @_ZN1XC1EOS_ +// CHECK-NEXT: call void @llvm.lifetime.end +X t5() { + X a [[gnu::aligned(1)]] [[gnu::aligned(8)]]; + return a; +} + +} // namespace test_alignas