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 @@ -1978,7 +1978,7 @@ // 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 && + if (DefaultsToAIXPowerAlignment && (FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) { auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) { if (BTy->getKind() == BuiltinType::Double || @@ -1989,16 +1989,25 @@ } }; - const Type *Ty = D->getType()->getBaseElementTypeUnsafe(); - if (const ComplexType *CTy = Ty->getAs()) { - performBuiltinTypeAlignmentUpgrade(CTy->getElementType()->castAs()); - } else if (const BuiltinType *BTy = Ty->getAs()) { - performBuiltinTypeAlignmentUpgrade(BTy); - } else if (const RecordType *RT = Ty->getAs()) { - const RecordDecl *RD = RT->getDecl(); - assert(RD && "Expected non-null RecordDecl."); - const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD); - PreferredAlign = FieldRecord.getPreferredAlignment(); + const Type *Ty = D->getType().getTypePtr(); + const Type *BaseTy = Ty->getBaseElementTypeUnsafe(); + // 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. + if (!(AlignIsRequired && + !(Ty->getTypeClass() == Type::Typedef || FieldPacked))) { + if (const ComplexType *CTy = BaseTy->getAs()) { + performBuiltinTypeAlignmentUpgrade( + CTy->getElementType()->castAs()); + } else if (const BuiltinType *BTy = BaseTy->getAs()) { + performBuiltinTypeAlignmentUpgrade(BTy); + } else if (const RecordType *RT = BaseTy->getAs()) { + const RecordDecl *RD = RT->getDecl(); + assert(RD && "Expected non-null RecordDecl."); + const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD); + PreferredAlign = FieldRecord.getPreferredAlignment(); + } } } 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-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