diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -164,24 +164,43 @@ template class AbstractTypeReader; } // namespace serialization +enum class AlignRequirementType { + /// The alignment was not explicit in code. + None, + + /// The alignment comes from an alignment attribute on a typedef. + RequiredByTypedef, + + /// The alignment comes from an alignment attribute on a record type. + RequiredByRecord, +}; + struct TypeInfo { uint64_t Width = 0; unsigned Align = 0; - bool AlignIsRequired : 1; + AlignRequirementType AlignRequirement; - TypeInfo() : AlignIsRequired(false) {} - TypeInfo(uint64_t Width, unsigned Align, bool AlignIsRequired) - : Width(Width), Align(Align), AlignIsRequired(AlignIsRequired) {} + TypeInfo() : AlignRequirement(AlignRequirementType::None) {} + TypeInfo(uint64_t Width, unsigned Align, + AlignRequirementType AlignRequirement) + : Width(Width), Align(Align), AlignRequirement(AlignRequirement) {} + bool isAlignRequired() { + return AlignRequirement != AlignRequirementType::None; + } }; struct TypeInfoChars { CharUnits Width; CharUnits Align; - bool AlignIsRequired : 1; + AlignRequirementType AlignRequirement; - TypeInfoChars() : AlignIsRequired(false) {} - TypeInfoChars(CharUnits Width, CharUnits Align, bool AlignIsRequired) - : Width(Width), Align(Align), AlignIsRequired(AlignIsRequired) {} + TypeInfoChars() : AlignRequirement(AlignRequirementType::None) {} + TypeInfoChars(CharUnits Width, CharUnits Align, + AlignRequirementType AlignRequirement) + : Width(Width), Align(Align), AlignRequirement(AlignRequirement) {} + bool isAlignRequired() { + return AlignRequirement != AlignRequirementType::None; + } }; /// Holds long-lived AST nodes (such as types and decls) that can be diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -1859,7 +1859,7 @@ Width = llvm::alignTo(Width, Align); return TypeInfoChars(CharUnits::fromQuantity(Width), CharUnits::fromQuantity(Align), - EltInfo.AlignIsRequired); + EltInfo.AlignRequirement); } TypeInfoChars ASTContext::getTypeInfoInChars(const Type *T) const { @@ -1867,8 +1867,7 @@ return getConstantArrayInfoInChars(*this, CAT); TypeInfo Info = getTypeInfo(T); return TypeInfoChars(toCharUnitsFromBits(Info.Width), - toCharUnitsFromBits(Info.Align), - Info.AlignIsRequired); + toCharUnitsFromBits(Info.Align), Info.AlignRequirement); } TypeInfoChars ASTContext::getTypeInfoInChars(QualType T) const { @@ -1876,7 +1875,7 @@ } bool ASTContext::isAlignmentRequired(const Type *T) const { - return getTypeInfo(T).AlignIsRequired; + return getTypeInfo(T).AlignRequirement != AlignRequirementType::None; } bool ASTContext::isAlignmentRequired(QualType T) const { @@ -1928,7 +1927,7 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { uint64_t Width = 0; unsigned Align = 8; - bool AlignIsRequired = false; + AlignRequirementType AlignRequirement = AlignRequirementType::None; unsigned AS = 0; switch (T->getTypeClass()) { #define TYPE(Class, Base) @@ -1962,7 +1961,7 @@ "Overflow in array type bit size evaluation"); Width = EltInfo.Width * Size; Align = EltInfo.Align; - AlignIsRequired = EltInfo.AlignIsRequired; + AlignRequirement = EltInfo.AlignRequirement; if (!getTargetInfo().getCXXABI().isMicrosoft() || getTargetInfo().getPointerWidth(0) == 64) Width = llvm::alignTo(Width, Align); @@ -1977,7 +1976,7 @@ Align = Width; // If the alignment is not a power of 2, round up to the next power of 2. // This happens for non-power-of-2 length vectors. - if (Align & (Align-1)) { + if (Align & (Align - 1)) { Align = llvm::NextPowerOf2(Align); Width = llvm::alignTo(Width, Align); } @@ -2299,7 +2298,7 @@ getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); if (unsigned AttrAlign = ED->getMaxAlignment()) { Info.Align = AttrAlign; - Info.AlignIsRequired = true; + Info.AlignRequirement = AlignRequirementType::RequiredByRecord; } return Info; } @@ -2309,7 +2308,9 @@ const ASTRecordLayout &Layout = getASTRecordLayout(RD); Width = toBits(Layout.getSize()); Align = toBits(Layout.getAlignment()); - AlignIsRequired = RD->hasAttr(); + AlignRequirement = RD->hasAttr() + ? AlignRequirementType::RequiredByRecord + : AlignRequirementType::None; break; } @@ -2343,10 +2344,10 @@ // attribute(aligned) can only round up) but matches its implementation. if (unsigned AttrAlign = Typedef->getMaxAlignment()) { Align = AttrAlign; - AlignIsRequired = true; + AlignRequirement = AlignRequirementType::RequiredByTypedef; } else { Align = Info.Align; - AlignIsRequired = Info.AlignIsRequired; + AlignRequirement = Info.AlignRequirement; } Width = Info.Width; break; @@ -2392,7 +2393,7 @@ } assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); - return TypeInfo(Width, Align, AlignIsRequired); + return TypeInfo(Width, Align, AlignRequirement); } unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { @@ -2482,7 +2483,7 @@ // When used as part of a typedef, or together with a 'packed' attribute, // the 'aligned' attribute can be used to decrease alignment. - if ((TI.AlignIsRequired && T->getAs() != nullptr) || + if ((TI.isAlignRequired() && T->getAs() != nullptr) || RD->isInvalidDecl()) return ABIAlign; @@ -2507,7 +2508,7 @@ Target->defaultsToAIXPowerAlignment())) // Don't increase the alignment if an alignment attribute was specified on a // typedef declaration. - if (!TI.AlignIsRequired) + if (!TI.isAlignRequired()) return std::max(ABIAlign, (unsigned)getTypeSize(T)); return ABIAlign; diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp --- a/clang/lib/AST/RecordLayoutBuilder.cpp +++ b/clang/lib/AST/RecordLayoutBuilder.cpp @@ -1538,7 +1538,7 @@ TypeInfo FieldInfo = Context.getTypeInfo(D->getType()); uint64_t StorageUnitSize = FieldInfo.Width; unsigned FieldAlign = FieldInfo.Align; - bool AlignIsRequired = FieldInfo.AlignIsRequired; + bool AlignIsRequired = FieldInfo.isAlignRequired(); // UnfilledBitsInLastUnit is the difference between the end of the // last allocated bitfield (i.e. the first bit offset available for @@ -1889,7 +1889,7 @@ bool FieldPacked = Packed || D->hasAttr(); - bool AlignIsRequired = false; + AlignRequirementType AlignRequirement = AlignRequirementType::None; CharUnits FieldSize; CharUnits FieldAlign; // The amount of this class's dsize occupied by the field. @@ -1904,7 +1904,7 @@ // aligned appropriately for their element type. EffectiveFieldSize = FieldSize = IsIncompleteArrayType ? CharUnits::Zero() : TI.Width; - AlignIsRequired = TI.AlignIsRequired; + AlignRequirement = TI.AlignRequirement; }; if (D->getType()->isIncompleteArrayType()) { @@ -1968,6 +1968,16 @@ } } + // When used as part of a typedef, or together with a 'packed' attribute, + // the 'aligned' attribute can be used to decrease alignment. In that case, + // it overrides any computed alignment we have, and there is no need to + // upgrade the alignment. + auto alignedAttrCanDecreaseAlignment = [AlignRequirement, FieldPacked]() { + return AlignRequirement == AlignRequirementType::RequiredByTypedef || + (AlignRequirement == AlignRequirementType::RequiredByRecord && + FieldPacked); + }; + // The AIX `power` alignment rules apply the natural alignment of the // "first member" if it is of a floating-point data type (or is an aggregate // whose recursively "first" member or element is such a type). The alignment @@ -1978,8 +1988,9 @@ // and zero-width bit-fields count as prior members; members of empty class // types marked `no_unique_address` are not considered to be prior members. CharUnits PreferredAlign = FieldAlign; - if (DefaultsToAIXPowerAlignment && !AlignIsRequired && - (FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) { + if (DefaultsToAIXPowerAlignment && + !alignedAttrCanDecreaseAlignment()( + FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) { auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) { if (BTy->getKind() == BuiltinType::Double || BTy->getKind() == BuiltinType::LongDouble) { @@ -1989,12 +2000,13 @@ } }; - const Type *Ty = D->getType()->getBaseElementTypeUnsafe(); - if (const ComplexType *CTy = Ty->getAs()) { - performBuiltinTypeAlignmentUpgrade(CTy->getElementType()->castAs()); - } else if (const BuiltinType *BTy = Ty->getAs()) { + const Type *BaseTy = D->getType()->getBaseElementTypeUnsafe(); + if (const ComplexType *CTy = BaseTy->getAs()) { + performBuiltinTypeAlignmentUpgrade( + CTy->getElementType()->castAs()); + } else if (const BuiltinType *BTy = BaseTy->getAs()) { performBuiltinTypeAlignmentUpgrade(BTy); - } else if (const RecordType *RT = Ty->getAs()) { + } else if (const RecordType *RT = BaseTy->getAs()) { const RecordDecl *RD = RT->getDecl(); assert(RD && "Expected non-null RecordDecl."); const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD); diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -52,7 +52,7 @@ static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { auto TI = Ctx.getTypeInfo(Ty); - return TI.AlignIsRequired ? TI.Align : 0; + return TI.isAlignRequired() ? TI.Align : 0; } static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { @@ -4653,7 +4653,7 @@ llvm::DIType *fieldType; if (capture->isByRef()) { TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy); - auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0; + auto Align = PtrInfo.isAlignRequired() ? PtrInfo.Align : 0; // FIXME: This recomputes the layout of the BlockByRefWrapper. uint64_t xoffset; fieldType = diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -847,7 +847,7 @@ // arguments was not supported and resulted in a compiler error. In 19.14 // and later versions, such arguments are now passed indirectly. TypeInfo Info = getContext().getTypeInfo(RD->getTypeForDecl()); - if (Info.AlignIsRequired && Info.Align > 4) + if (Info.isAlignRequired() && Info.Align > 4) return RAA_Indirect; // If C++ prohibits us from making a copy, construct the arguments directly diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -1831,7 +1831,7 @@ // Pass over-aligned aggregates on Windows indirectly. This behavior was // added in MSVC 2015. - if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32) + if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32) return getIndirectResult(Ty, /*ByVal=*/false, State); // Expand small (<= 128-bit) record types when we know that the stack layout @@ -6984,7 +6984,7 @@ TyAlignForABI = CharUnits::fromQuantity(4); } - TypeInfoChars TyInfo(TySize, TyAlignForABI, false); + TypeInfoChars TyInfo(TySize, TyAlignForABI, AlignRequirementType::None); return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, SlotSize, /*AllowHigherAlign*/ true); } diff --git a/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp b/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp deleted file mode 100644 --- a/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \ -// RUN: FileCheck %s - -// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \ -// RUN: FileCheck %s - -namespace test1 { -struct __attribute__((__aligned__(2))) C { - double x; -} c; - -// CHECK: @{{.*}}test1{{.*}}c{{.*}} = global %"struct.test1::C" zeroinitializer, align 8 -} // namespace test1 - -namespace test2 { -struct __attribute__((__aligned__(2), packed)) C { - double x; -} c; - -// CHECK: @{{.*}}test2{{.*}}c{{.*}} = global %"struct.test2::C" zeroinitializer, align 2 -} // namespace test2 - -namespace test3 { -struct __attribute__((__aligned__(16))) C { - double x; -} c; - -// CHECK: @{{.*}}test3{{.*}}c{{.*}} = global %"struct.test3::C" zeroinitializer, align 16 -} // namespace test3 diff --git a/clang/test/Layout/aix-power-alignment-typedef.cpp b/clang/test/Layout/aix-power-alignment-typedef.cpp --- a/clang/test/Layout/aix-power-alignment-typedef.cpp +++ b/clang/test/Layout/aix-power-alignment-typedef.cpp @@ -37,3 +37,39 @@ // CHECK-NEXT: | nvsize=2, nvalign=2, preferrednvalign=2] } // namespace test2 + +namespace test3 { +typedef double DblArr[] __attribute__((__aligned__(2))); + +union U { + DblArr da; + char x; +}; + +int x = sizeof(U); + +// CHECK: 0 | union test3::U +// CHECK-NEXT: 0 | test3::DblArr da +// CHECK-NEXT: 0 | char x +// CHECK-NEXT: | [sizeof=2, dsize=2, align=2, preferredalign=2, +// CHECK-NEXT: | nvsize=2, nvalign=2, preferrednvalign=2] + +} // namespace test3 + +namespace test4 { +typedef double Dbl __attribute__((__aligned__(2))); + +union U { + Dbl DblArr[]; + char x; +}; + +int x = sizeof(U); + +// CHECK: 0 | union test4::U +// CHECK-NEXT: 0 | test4::Dbl [] DblArr +// CHECK-NEXT: 0 | char x +// CHECK-NEXT: | [sizeof=2, dsize=2, align=2, preferredalign=2, +// CHECK-NEXT: | nvsize=2, nvalign=2, preferrednvalign=2] + +} // namespace test4 diff --git a/clang/test/Layout/aix-type-align-and-pack-attr.cpp b/clang/test/Layout/aix-type-align-and-pack-attr.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Layout/aix-type-align-and-pack-attr.cpp @@ -0,0 +1,59 @@ +// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \ +// RUN: FileCheck %s + +// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \ +// RUN: FileCheck %s + +namespace test1 { +struct __attribute__((__aligned__(2))) S { + double d; +}; + +S s; + +// CHECK: @{{.*}}test1{{.*}}s{{.*}} = global %"struct.test1::S" zeroinitializer, align 8 +} // namespace test1 + +namespace test2 { +struct __attribute__((__aligned__(2), packed)) S { + double d; +}; + +S s; + +// CHECK: @{{.*}}test2{{.*}}s{{.*}} = global %"struct.test2::S" zeroinitializer, align 2 +} // namespace test2 + +namespace test3 { +struct __attribute__((__aligned__(16))) S { + double d; +}; + +S s; + +// CHECK: @{{.*}}test3{{.*}}s{{.*}} = global %"struct.test3::S" zeroinitializer, align 16 +} // namespace test3 + +namespace test4 { +struct __attribute__((aligned(2))) SS { + double d; +}; + +struct S { + struct SS ss; +} s; + +// CHECK: @{{.*}}test4{{.*}}s{{.*}} = global %"struct.test4::S" zeroinitializer, align 8 +} // namespace test4 + +namespace test5 { +struct __attribute__((aligned(2), packed)) SS { + double d; +}; + +struct S { + struct SS ss; +} s; + +// CHECK: @{{.*}}test5{{.*}}s{{.*}} = global %"struct.test5::S" zeroinitializer, align 2 +} // namespace test5